// JavaScript Document

		var screenSizeW,screenSizeH;
		
		alertSize();
		function alertSize() {
		  var myWidth = 0, myHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		  }
		//  window.alert( 'Width = ' + myWidth );
		 // window.alert( 'Height = ' + myHeight );
		  screenSizeW=myWidth;
		  screenSizeH=myHeight;
		}
		
		function getScrollXY() {
		  var scrOfX = 0, scrOfY = 0;
		  if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		  }
		  return [ scrOfX, scrOfY ];
		}


/********** GENERAL */	
	/**	Get an element by ID */
		function ke_byID(id_value){
			return document.getElementById(id_value);
		}
		
	/**	Hide an element	*/
		function ke_hide(id_value){
			if (ke_byID(id_value)==undefined) alert(id_value+' is "undefined"');
			ke_byID(id_value).style.display='none';
		}
	/** Display an element */
		function ke_display(id_value){
			if (ke_byID(id_value)==undefined) alert(id_value+' is "undefined"');
			ke_byID(id_value).style.display='inline';
		}
	
	/**	Alternative Hide an element	*/
		function ke_Ahide(id_value){
			if (ke_byID(id_value)==undefined) alert(id_value+' is "undefined"');
			ke_byID(id_value).style.position='absolute';
			ke_byID(id_value).style.visibility='hidden';
		}
	/** Alternative Display an element */
		function ke_Adisplay(id_value){
			if (ke_byID(id_value)==undefined) alert(id_value+' is "undefined"');
			ke_byID(id_value).style.position='relative';
			ke_byID(id_value).style.visibility='visible';
		}
		
		
/********** SELECT.options FUNCTIONS */	
	/** Add option to a SELECT object */
		function ke_SelectAddOption(id_value,text_value,value){
			
						if (ke_byID(id_value)==undefined) alert(id_value+' is "undefined"');
						option=document.createElement('OPTION');
						ke_byID(id_value).options.add(option);
						option.text=text_value;
						option.value=value;
						
		}

	/** Delete option from a SELECT object */
		function ke_SelectDeleteOption(id_value,option_index){
			
						if (ke_byID(id_value)==undefined) alert(id_value+' is "undefined"');
						ke_byID(id_value).options[option_index]=null;
						
		}
		
	/** Empty a SELECT object */
		function ke_SelectEmpty(id_value){
			
						if (ke_byID(id_value)==undefined) alert(id_value+' is "undefined"');
						while (ke_byID(id_value).length>0) ke_SelectDeleteOption(id_value,0);
						
		}
		
		function openBig(id,img)
		{
			window.open('/preview.php?id='+id+'&img='+img,'bigpix','status=no, toolbar=no, menubar=no, location=no, resizeable=yes, width=700, height=660, focus=yes, directories=no, scrollbars=yes');
		}
		
		function no_return()
		{}
		
		
		function maximm(){
				left_x = ke_byID('home_columnleft').offsetHeight;
                center_x = ke_byID('home_columncenter').offsetHeight;
				right_x = ke_byID('home_columnright').offsetHeight;
				var arr_x = new Array();
				arr_x[0] = left_x;
				arr_x[1] = center_x;
				arr_x[2] = right_x;
				tempmax = Math.max(arr_x[0],arr_x[1],arr_x[2]);	
				alert('MAXIMUM'+tempmax);	
		}	
		
		function ajax_search(target,parent,value,file,aux)
		{
				ke_byID(target).style.display=''; 
				ajax_get('/ajax/'+file+'?target='+target+'&parent='+parent+'&value='+value+'&'+aux, target);
				
		}
		
		function center_filter(field)
	{
		var offs=ke_byID('ft_'+field).offsetWidth; 
		
		var ml=86-Math.round(offs/2);
	
		ke_byID('pbrowsing_filter_pc_'+field).style.marginLeft='-'+ml+'px';
	}
	
	function urlencode(str) {
		return str;
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
str = str.replace('+', ' ');
str = unescape(str);
return str;
}

function utf8_encode ( string ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    string = (string+'').replace(/\r\n/g,"\n");
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}
	
	function get_get(obj)
	   {
		   var getstr="?";
		   arr=obj.split('|');
		   for(i=0;i<arr.length;i++)
		   {
				if(curel=document.getElementsByName(arr[i])[0]){
				
				
				
				if (curel.tagName == "TEXTAREA") {
					getstr += curel.name + "=" + escape(curel.value) + "&";
				}
				
				if (curel.tagName == "INPUT") {
				if (curel.type == "hidden") {
				   getstr += curel.name + "=" + escape(curel.value) + "&";
				}
				if (curel.type == "text") {
				   getstr += curel.name + "=" + escape(curel.value) + "&";
				}
				if (curel.type == "checkbox") {
				   if (curel.checked) {
					  getstr += curel.name + "=" + escape(curel.value) + "&";
				   } else {
					  getstr += curel.name + "=&";
				   }
				}
				if (curel.type == "radio") {
				   if (curel.checked) {
					  getstr += curel.name + "=" + escape(curel.value) + "&";
				   }
				}
			 }   
			 if (curel.tagName == "SELECT") {
				var sel = curel;
				getstr += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
			 }
			   
			   }else{alert(arr[i]);}
		
			
		   }
		return getstr;
   }
   
   
   function get_getid(obj)
	   {
		   var getstr="";
		   arr=obj.split('|');
		   for(i=0;i<arr.length;i++)
		   {
				if(curel=document.getElementById(arr[i])){
				
				
				
				if (curel.tagName == "TEXTAREA") {
					getstr += curel.id + "=" + urlencode(curel.value) + "&";
				}
				
				if (curel.tagName == "INPUT") {
				if (curel.type == "hidden") {
				   getstr += curel.id + "=" + urlencode(curel.value) + "&";
				}
				if (curel.type == "text" || curel.type == "password") {
				   getstr += curel.id + "=" + urlencode(curel.value) + "&";
				}
				if (curel.type == "checkbox") {
				   if (curel.checked) {
					  getstr += curel.id + "=" + urlencode(curel.value) + "&";
				   } else {
					  getstr += curel.id + "=&";
				   }
				}
				if (curel.type == "radio") {
				   if (curel.checked) {
					  getstr += curel.id + "=" + urlencode(curel.value) + "&";
				   }
				}
			 }   
			 if (curel.tagName == "SELECT") {
				var sel = curel;
				getstr += sel.id + "=" + urlencode(sel.options[sel.selectedIndex].value) + "&";
			 }
			   
			   }else{alert(arr[i]);}
		
			
		   }
		   
		   getstr=base64_encode(getstr);
		  //alert(getstr);
		return getstr;
   }
   
   
   function base64_encode( data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Bayron Guevara
    // +   improved by: Thunder.m
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)        
    // -    depends on: utf8_encode
    // *     example 1: base64_encode('Kevin van Zonneveld');
    // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
 
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof window['atob'] == 'function') {
    //    return atob(data);
    //}
        
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
    data = utf8_encode(data);
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1<<16 | o2<<8 | o3;
 
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
 
    return enc;
}

function redirect_uri(uri)
{
		window.location=uri;
}

function strpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
 
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : true;
}

function get_result(elm)
{
	var result=ke_byID(elm).innerHTML;
	
//	alert(strpos(result,'noerror'));
	if(strpos(result,'noerror'))
		window.location='/shoppingCart/';
}

function check_result(elm)
{
	setTimeout("get_result('"+elm+"')",1000);
}



function setLivrare(code, livrare)
{
		ajax_get('/ajax/get_mod_plata.php?code='+code+'&livrare='+livrare,'modplata'); 
		
		setTimeout("ajax_get('/ajax/shopping_cart.php?code="+code+"','cart_content',true)",400);
}

function setPayment(code, payment)
{
		setTimeout("ajax_get('/ajax/shopping_cart.php?code="+code+"&payment="+payment+"','cart_content',true)",400);
}

function insert_address(code, uid, prefix, type)
{
		var street=ke_byID(prefix+'_street').value; ke_byID(prefix+'_street').value='';
		var zip=ke_byID(prefix+'_zip').value; ke_byID(prefix+'_zip').value='';
		var city=ke_byID(prefix+'_city').value; ke_byID(prefix+'_city').value='';
		var state=ke_byID(prefix+'_state').value; ke_byID(prefix+'_state').value='';
		
		
		
		ajax_get('/ajax/insert_address.php?uid='+uid+'&code='+code+'&street='+street+'&zip='+zip+'&city='+city+'&state='+state+'&type='+type,'adr_fact_result');
		
		setTimeout("ajax_get('/ajax/list_addresses.php?code="+code+"&uid="+uid+"&type=shipping','list_addr_shipping',true)",200);
		setTimeout("ajax_get('/ajax/list_addresses.php?code="+code+"&uid="+uid+"&type=billing','list_addr_billing',true)",200);
				 
}


function save_firma(code,uid,retid)
{
	ajax_get('/ajax/save_firma.php?code='+code+'&uid='+uid+'&company_name='+document.getElementById('company_name').value+'&company_regcom='+document.getElementById('company_regcom').value+'&company_cfisc='+document.getElementById('company_cfisc').value+'&company_ciban='+document.getElementById('company_ciban').value+'&company_ciban='+document.getElementById('company_ciban').value+'&company_bank='+document.getElementById('company_bank').value, retid);
	
	setTimeout("ajax_get('/ajax/list_firma.php?code="+code+"&uid="+uid+"',"+retid+","+true+")",600);
}


function register_client(sessid)
{
		ajax_get('/ajax/register_form.php?code='+sessid+'&get='+get_getid('first_name|last_name|email|password|phone|cnp|bi_seria|bi_numar|bi_eliberat'),'form_result'); 
		
		setTimeout("check_result('form_result')",400);
}

function topfilter()
{
	if(document.getElementById('fmarca_val').value=='')
		return alert('Nu ati selectat marca!');
	
	if(document.getElementById('fmodel_val').value=='')
		return alert('Nu ati selectat modelul');
	if(document.getElementById('fclassif_val').value=='')
		return alert('Nu ati selectat clasificarea!');
	
	if(document.getElementById('fcateg_val').value=='')
		window.location='/piese/'+document.getElementById('fmarca_val').value+'/'+document.getElementById('fmodel_val').value+'/'+document.getElementById('fclassif_val').value+'.html';
	else{
		window.location='/piese/'+document.getElementById('fmarca_val').value+'/'+document.getElementById('fmodel_val').value+'/'+document.getElementById('fclassif_val').value+'/-c--s-'+document.getElementById('fcateg_val').value+'.html';	
		}
}

function show_hide(el)
{
	var elem=document.getElementById(el);
	
	if(elem.style.display=='none')
	{
		elem.style.display='';
	}
	else elem.style.display='none';
}