var xmlhttp=false;
var can_make_request = true;
var warned_user = false;
var posX = 0;
var posY = 0;

function ajx_open_http()
{
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
}

function ajx_can_request()
{
	can_make_request = true;
}


// Shopping Cart:
function sh_make_request( url )
{
	if ( can_make_request )
	{
		ajx_open_http();
		can_make_request = false;
		xmlhttp.open( "GET" , url , true );
		xmlhttp.onreadystatechange = sh_request;

		xmlhttp.send(null);
	}
	else
	{
		setTimeout( 'sh_make_request("'+url+'")' , 10 );
	}
}

function sh_request()
{
	if (xmlhttp.readyState==4)
	{		
		resp = xmlhttp.responseText;
		if ( !resp )
		{
			setTimeout( "ajx_can_request()" , 10 );
			return;
		}
		var values = resp.split( "|||", 4 );
        
		
		var cnr = document.getElementById('cos_nr');
		if ( cnr )
		{
			cnr.innerHTML = values[1];
		}
        
		var spanInfoCom = document.getElementById( 'info_comanda_' + values[2] );
		spanInfoCom.style.display = 'block';
		
		
		var btn_comanda_on = document.getElementById( 'btn_comanda_on_' + values[2] );
		if( btn_comanda_on )
		{
			btn_comanda_on.src = 'images/bt_comanda_gri.gif';
		}	
		
		var pos = getScrollPosition();
		var divCos;
		if( parseInt( values[3] ) == 1 )
		{
		    divCos = document.getElementById('div_adaugat_in_cos2');
			var comanda_related = document.getElementById('link_produse_recomandate');
			comanda_related.href = 'produse-recomandate-p' + values[2];
		}
		else
		{
			divCos = document.getElementById('div_adaugat_in_cos');
		}
		/*if( parseInt( values[3] ) == 1 )
		{
			//window.location = 'produse-recomandate-p' + values[2] ;
			alert( "aici" );
			divCos = document.getElementById('div_adaugat_in_cos_related');
			//var anchor_prods_related = document.getElementById('produse_recomandate');
			//anchor_prods_related.href = 'produse-recomandate-p' + values[2] ;
		}
		else
		{
			divCos = document.getElementById('div_adaugat_in_cos');
		}*/
		
		if( divCos )
		{
			divCos.style.display = 'block';	
			divCos.style.top = ( screen.height/2 - 250 + pos.y ) + 'px';
			divCos.style.left = ( screen.width/2 - 150 + pos.x ) + 'px';			
		}
		
		
		setTimeout( "ajx_can_request()" , 10 );
	}
}


function update_cart( _pid )
{
	var sid = 0;
	if ( typeof( stofe ) != 'undefined' )
	{
		for ( var i=0; i< stofe.length; i++ )
		{
			var elem = document.getElementById('stofe' + '[' + stofe[i] + ']');
			if ( elem.checked )
			{
				var sid = elem.value;
			}
		}
	}	

	// create url:	
	url = "update_cos.php?prod_id=" + _pid + "&stofa_id=" + sid;
	//alert(url);
	setTimeout( 'sh_make_request("'+url+'")' , 10 );
}

function update_comanda_curenta( _pid )
{
	var sid = 0;
	var stofe = eval( 'stofe_' + _pid ); 	
	if ( typeof( stofe ) != 'undefined' )
	{	
		for ( var i=0; i< stofe.length; i++ )
		{
			var elem = document.getElementById('stofe_' + _pid + '[' + stofe[i] + ']');		
			if ( elem.checked )
			{			
				var sid = elem.value;
			}
		}
	}	

	// create url:	
	url = "cumparaturi.html?pid_change=" + _pid + "&stofa_id=" + sid;
	//alert(url);
	document.location = url;
}


// Filtre:
function actulizare_make_request( url )
{
	if ( can_make_request )
	{
		ajx_open_http();
		can_make_request = false;
		xmlhttp.open( "GET" , url , true );
		xmlhttp.onreadystatechange = actulizare_request;

		xmlhttp.send(null);
	}
	else
	{
		setTimeout( 'actulizare_make_request("'+url+'")' , 10 );
	}
}


function actulizare_request()
{
	if (xmlhttp.readyState==4)
	{		
		resp = xmlhttp.responseText;
		if ( !resp )
		{
			setTimeout( "ajx_can_request()" , 10 );
			return;
		}

		var div_lista_produse = document.getElementById( 'listaProduse' );
		div_lista_produse.innerHTML = resp;
		
		setTimeout( "ajx_can_request()" , 10 );
	}
}


function actualizeaza_lista( cid, filtru )
{
	// create url:	
	url = "update_lista_produse.php?cid=" + cid + "&filtru=" + filtru;
	setTimeout( 'actulizare_make_request("'+url+'")' , 10 );
}


function findPos(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	posX = curleft;
	posY = curtop;
	
	return [curleft,curtop];
}


function scrollingDetector(){
if (navigator.appName == "Microsoft Internet Explorer"){
	return document.body.scrollTop;
}
else {
	return window.pageYOffset;
}

}


function getScrollPosition() 
{  
   
         var x = 0;  
         var y = 0;  
   
         if( typeof( window.pageYOffset ) == 'number' ) {  
             x = window.pageXOffset;  
             y = window.pageYOffset;  
         } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {  
             x = document.documentElement.scrollLeft;  
             y = document.documentElement.scrollTop;  
         } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {  
             x = document.body.scrollLeft;  
             y = document.body.scrollTop;  
         }  
   
         var position = {  
             'x' : x,  
             'y' : y  
         }  
   
         return position;  
   
}  
