function setLanguage( lang ){
	$.ajax({
   		type: "POST",
   		url: root+"index.php/ajax/ajaxSetLanguage",
   		data: {
			lang : lang
		},
   		success: function(msg){
			window.location.reload(); 
		}
 	});	
}


function searchKUp(ev){
	if( ev.keyCode == 13 ){
		search();
	}
}


function utf8_decode (str_data) {
    // Converts a UTF-8 encoded string to ISO-8859-1  
    // 
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/utf8_decode    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'    var tmp_arr = [],
        i = 0,
        ac = 0,
        c1 = 0,
        c2 = 0,        c3 = 0;
 
    str_data += '';
 
    while (i < str_data.length) {        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if (c1 > 191 && c1 < 224) {            c2 = str_data.charCodeAt(i + 1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i + 1);            c3 = str_data.charCodeAt(i + 2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    } 
    return tmp_arr.join('');
}


function doSearch(){
	data = $('#searchInput').val();
	window.location = root + 'index.php/catalog/search/' + data;
}

function search(){
	$('#main').fadeOut( 200, function(){ $('#wait').fadeIn(400, function(){doSearch();} ); });
}


function displayWaitCursor(){
	$('#main').fadeOut( 200, function(){ $('#wait').fadeIn(400); });
}


function onSearchFocus( el, def ){
	if( el.val() == def ){
		el.val('');
		el.css('color','black');
		el.css('font-style', '' );
	}
}

function onSearchBlur( el, def ){
	if( el.val() == '' ){
		// reset to default
		el.val( def );
		el.css('color','gray');
		el.css('font-style', 'italic' );
	}
	
}


function SelSmallTab( t_index, count, pageprefix ){

	for( i=0; i<count; i++ ){
		 var lt = $('#tl_'+i);
		 var ct = $('#tc_'+i);
		 var rt = $('#tr_'+i);
		 
		 if( i == t_index ){
			 lt.css( 'background-image', "url( '"+root+"images/tab_la.png' )" );
			 ct.css( 'background-image', "url( '"+root+"images/tab_ca.png' )" );
			 rt.css( 'background-image', "url( '"+root+"images/tab_ra.png' )" );
			 lt.css( 'margin-top', '0px' );
			 ct.css( 'margin-top', '0px' );
			 rt.css( 'margin-top', '0px' );
			 lt.css( 'height', '35px' );
			 ct.css( 'height', '35px' );
			 rt.css( 'height', '35px' );
			 ct.css( 'line-height', '40px' );
			 //$('#'+pageprefix+i ).css( 'display', 'block' );
		 }
		 else{
			 lt.css( 'background-image', "url( '"+root+"images/tab_l.png' )" );
			 ct.css( 'background-image', "url( '"+root+"images/tab_c.png' )" );
			 rt.css( 'background-image', "url( '"+root+"images/tab_r.png' )" );
			 lt.css( 'margin-top', '6px' );
			 ct.css( 'margin-top', '6px' );
			 rt.css( 'margin-top', '6px' );
			 lt.css( 'height', '28px' );
			 ct.css( 'height', '28px' );
			 rt.css( 'height', '28px' );
			 ct.css( 'line-height', '36px' );
			 //$('#'+pageprefix+i ).css( 'display', 'none' );
		 }
	}
}


function logoutUser(){

	$.ajax({
   		type: "POST",
   		url: root+"index.php/ajax/logoutUser",
   		data: '',
   		success: function(msg){
			window.location.reload(); 
		}
 	});	
	
}

function loginCustomer( showCOAPage ){
	
	$( "#loginProgressbar" ).progressbar({
		value: 100
	});
	
	$('#loginbtn').attr('disabled', 'disabled' );
	
	$.ajax({
   		type: "POST",
   		url: root+"index.php/ajax/loginUser",
   		data: {
			email : $('#emailLogin').val(),
			pwd : MD5( $('#pwdLogin').val() )
		},
   		success: function(msg){
			if( msg != 'success' ){
				$.alerts.cancelButton ="";
				jConfirm( $('#loginErrorMessage').html(), 'eDiscount Multimedia' );
				$( '#loginbtn' ).attr('disabled', '' );
			}
			else{
				//window.location.href ='/checkout';
				if( showCOAPage ){
					window.location = root+'checkout/address';
				}
				else{
					window.location.reload();
				}
			}
			$( '#loginProgressbar' ).progressbar("destroy");
			
		},
		error: function(){
			$( '#loginProgressbar' ).progressbar("destroy");
			$( '#loginbtn' ).attr('disabled', '' );
		}
	});
}

