// JavaScript Document

/********** 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=680, focus=yes, directories=no, scrollbars=yes');
		}

		function openBig2(){
			window.open('/preview2.php','bigpix','status=no, toolbar=no, menubar=no, location=no, resizeable=yes, width=700, height=680, focus=yes, directories=no, scrollbars=yes');
		}
		

		function openHarta()

		{

			window.open('/harta.php','harta','status=no, toolbar=no, menubar=no, location=no, resizeable=yes, width=850, height=600, focus=yes, directories=no, scrollbars=yes');

		}

		

		function openRecomanda(id)

		{

			window.open('/recomanda/~000-0!id='+id,'recommend','status=no, toolbar=no, menubar=no, location=no, resizeable=yes, width=700, height=660, focus=yes, directories=no, scrollbars=yes');

		}

		

		function openOpinia(id)

		{

			window.open('/opinia.php?id='+id,'recommend','status=no, toolbar=no, menubar=no, location=no, resizeable=yes, width=700, height=660, focus=yes, directories=no, scrollbars=yes');

		}

		

		function email_check(str) {



		var at="@"

		var dot="."

		var lat=str.indexOf(at)

		var lstr=str.length

		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){

		  

		   return false

		}



		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		  

		   return false

		}



		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		    

		    return false

		}



		 if (str.indexOf(at,(lat+1))!=-1){

		    

		    return false

		 }



		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		    

		    return false

		 }



		 if (str.indexOf(dot,(lat+2))==-1){

		    

		    return false

		 }

		

		 if (str.indexOf(" ")!=-1){

		    

		    return false

		 }



 		 return true					

	}



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='/cartNou/';

}



function check_result(elm)

{

	setInterval("get_result('"+elm+"')",300);

}







function setLivrare(code, livrare)

{

		ke_byID('livrare_code').value=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 setBilling(code,billing)

{

	ajax_get('/ajax/set_addr_bill.php?code='+code+'&billing='+billing,'emtyfield'); 

}



function setShippingAdr(code, addr)

{

			ajax_get('/ajax/set_addr_shipp.php?code='+code+'&addr='+addr,'emtyfield'); 

		

			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)",500);

		setTimeout("ajax_get('/ajax/list_addresses.php?code="+code+"&uid="+uid+"&type=billing','list_addr_billing',true)",500);

				 

}





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)

{

	if(ke_byID('password').value!=ke_byID('re_password').value)

	{

		alert('Parolele sunt diferite!');

		return false;

	}

		ajax_get('/ajax/register_form.php?code='+sessid+'&get='+get_getid('first_name|last_name|email|password|newsletter'),'form_result'); 

		

		setTimeout("check_result('form_result')",1000);

}



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';

}



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 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 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 check(el)
{
	var elements = document.getElementsByName(el);
	
	for(i=0;i<elements.length;i++)
	{
		elements.item(i).checked = true;
	}
}

function uncheck(el)
{
	var elements = document.getElementsByName(el);
	
	for(i=0;i<elements.length;i++)
	{
		elements.item(i).checked = false;
	}
}


function checkOnePage()
	{
		
		if(ke_byID('person1').checked == false && ke_byID('person2').checked == false)
		{
			alert('Nu ati selectat tipul persoanei!');
			return false;
		}
	
		if(ke_byID('person1').checked == true)
		{ 
			if(ke_byID('last_name').value=='')
			{
				alert('Nu ati completat numele!');
				return false;
			}
			
			if(ke_byID('first_name').value=='')
			{
				alert('Nu ati completat prenumele!');
				return false;
			}
			
			if(ke_byID('cnp').value=='')
			{
				alert('Nu ati completat cnp-ul!');
				return false;
			}
			
			if(ke_byID('serie_buletin').value=='')
			{
				alert('Nu ati completat seria buletinului!');
				return false;
			}
			
			if(ke_byID('nr_buletin').value=='')
			{
				alert('Nu ati completat numarul buletinului!');
				return false;
			}
		}
		
		
		if(ke_byID('person2').checked == true)
		{
			if(ke_byID('company_name').value=='')
			{
				alert('Nu ati completat numele companiei!');
				return false;
			}
			
			if(ke_byID('company_cfisc').value=='')
			{
				alert('Nu ati completat CUI-ul!');
				return false;
			}
			
			if(ke_byID('company_regcom').value=='')
			{
				alert('Nu ati completat J-ul!');
				return false;
			}
			
		}
		

		if(ke_byID('mod_livrare').value=='')

			{

				alert('Nu ati selectat modalitatea de livrare!');

				return false;

			}

			

		if(ke_byID('mod_plata').value=='')

			{

				alert('Nu ati selectat modalitatea de plata!');

				return false;

			}

		if(ke_byID('adrshipping').value=='' || ke_byID('adrshipping').value==0 || ke_byID('adrshipping').value==null)

			{

				alert('Nu ati selectat adresa de livrare!');

				return false;

			}

		

		if(ke_byID('adrbilling').value=='' || ke_byID('adrbilling').value==0 || ke_byID('adrbilling').value==null)

			{

				alert('Nu ati selectat adresa de facturare!');

				return false;

			}

		if(ke_byID('adrshipping').value=='' && ke_byID('livrare_code').value!='sediu')

			{

				//alert(ke_byID('mod_livrare').value);

				alert('Nu ati selectat adresa de livrare!');

				return false;

			}
			
		return true;
	}
