var divId = '';
function ajaxLoadContent(url,divId)
{
	http_alc.open("GET", url + "&time=" + new Date().getTime(), true);
	http_alc.onreadystatechange = ajaxLoadContentResponse;
	http_alc.send(null);
}
function ajaxLoadContentResponse()
{
	if (http_alc.readyState == 4)
	{
		document.getElementById(divId).innerHTML = http_alc.responseText;
	}
}
var http_alc = getHTTPObject();

function getIEVersionNumber()
{
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}


function getHTTPObject()
{
	var xmlhttp;
	
	if (getIEVersionNumber() >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	else
		xmlhttp = false;
	
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return xmlhttp;
}

// Flying Cart - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

var tmp_storeid = 0;
var tmp_productsInCart = 0;

function productsInCart ()
{
	if	(http_inCart.readyState == 4)
	{
		tmp_productsInCart = parseInt(http_inCart.responseText);
		
		var defaultCartImg 	= (document.getElementById('defaultCartImg')) ? document.getElementById('defaultCartImg').value : '';
		var fullCartImg 	= (document.getElementById('fullCartImg')) ? document.getElementById('fullCartImg').value : '';
		
		if	(fullCartImg)
		{
			document.getElementById('flyingCartImg').src = (tmp_productsInCart > 0) ? fullCartImg : defaultCartImg;
		}
	}
}

function checkProductsInCart (storeId)
{
	http_inCart.open("GET", getBasePath() + "ajax.flyingCart.getProductsCount.php?time=" + new Date().getTime() + "&storeid=" + storeId, true);
	http_inCart.onreadystatechange = productsInCart;
	http_inCart.send(null);
}

var http_inCart = getHTTPObject();

function addToCart(productId,storeId,getDo,getPage,info,subProducts)
{
	tmp_storeid = (storeId) ? storeId : tmp_storeid;
	
	if (document.getElementById('cartLoading'))
	{
		document.getElementById('cartLoading').style.display = 'block';
	}
	
	productComment = '';
	
	if	(getDo == 'addOne')
	{
		if (typeof openCart == 'function') openCart();
		if (typeof YallaFloatCart_OpenAndClose == 'function') YallaFloatCart_OpenAndClose();
		
		if	(document.getElementById('productDiv_' + productId))
		{
			if (typeof Fly2Cart == 'function') Fly2Cart(productId);
		}
		
		productComment = (document.getElementById('productComment_' + productId)) ? "&productComment=" + Url.encode(document.getElementById('productComment_' + productId).value) : '';
	}
	
	setTimeout("checkProductsInCart ('"+storeId+"')",1000);
	
	ajax_path = getBasePath();
	
	http_cart.open("GET", ajax_path + "ajax.flyingCart.php?time=" + new Date().getTime() + "&id=" + productId + "&storeid=" + storeId + "&do=" + getDo + "&page=" + getPage + "&info=" + info + productComment + "&subProducts="+subProducts, true);
	http_cart.onreadystatechange = addToCart_HttpResponse;
	http_cart.send(null);
}

function addToCart_HttpResponse()
{
	if	(http_cart.readyState == 4)
	{
		if	(document.getElementById('flyingCart'))
		{
			document.getElementById('flyingCart').innerHTML = http_cart.responseText;
		}
		else
		{
			top.location = './?view=cart';
		}
		
		if (document.getElementById('cartLoading'))
		{
			document.getElementById('cartLoading').style.display = 'none';
		}
	}
}
 
var http_cart = getHTTPObject();


function updateProductsComm(productId,storeId,productComments)
{
	http_productsComm.open("GET", getBasePath() + "ajax.flyingCart.productsComm.php?time=" + new Date().getTime() + "&productId=" + productId + "&storeid=" + storeId + "&productComments=" + Url.encode(productComments), true);
    //http_productsComm.open("GET", getBasePath() + "ajax.flyingCart.php?time=" + new Date().getTime() + "&id=" + productId + "&storeid=" + storeId + "&do=comments&productComments=" + Url.encode(productComments) + "&page=cart&info="+ productComment, true);
	http_productsComm.onreadystatechange = updateProductsComm_Response;
	http_productsComm.send(null);
}

function updateProductsComm_Response()
{
	if	(http_productsComm.readyState == 4)
	{
	   /*
		if	(document.getElementById('flyingCart'))
		{
			document.getElementById('flyingCart').innerHTML = http_productsComm.responseText;
		}
		else
		{
			top.location = './?view=cart';
		}
        */
        //alert(http_productsComm.responseText);
        top.location = './?view=cart';
	}
}
 
var http_productsComm = getHTTPObject();

function radioIsCheck (deliveryWay)
{
	if	(!deliveryWay.length)
	{
		return deliveryWay.checked;
	}
	
	for	(var i = 0; i < deliveryWay.length; i++)
		if	(deliveryWay[i].checked)
			return true;
			
	return false;
}

function requireDeliveryWay (lang_must_select)
{
	selfTaking 		= (document.getElementById('selfTaking')) ? document.getElementById('selfTaking') : false;
	deliveryWay 	= (document.yallaCart.deliveryWay) ? document.yallaCart.deliveryWay : false;
	ElsedeliveryWay = (document.getElementById('ElsedeliveryWay')) ? document.getElementById('ElsedeliveryWay') : false;
	
	if	(deliveryWay && selfTaking)
	{
		if	(!radioIsCheck(deliveryWay) && !selfTaking.checked)
		{
			alert (lang_must_select);
			return false;
		}
	}
	else if	(deliveryWay && !selfTaking)
	{
		if	(!radioIsCheck(deliveryWay))
		{
			alert (lang_must_select);
			return false;
		}
	}
	else if	(selfTaking && !deliveryWay && !ElsedeliveryWay)
	{
		if	(!selfTaking.checked)
		{
			alert (lang_must_select);
			return false;
		}
	}
	else if	(selfTaking && !deliveryWay && ElsedeliveryWay)
	{
		if	(!selfTaking.checked && !ElsedeliveryWay.checked)
		{
			alert (lang_must_select);
			return false;
		}
	}
	
	return true;
}

// Compere Products - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //

function addToCompere(productId,storeId,inComperePage)
{
	http_compere.open("GET", getBasePath() + "ajax.compere.php?time=" + new Date().getTime() + "&id=" + productId + "&storeid=" + storeId + "&inComperePage=" + inComperePage, true);
	http_compere.onreadystatechange = addToCompere_HttpResponse;
	http_compere.send(null);
}

function addToCompere_HttpResponse()
{
	if (http_compere.readyState == 4)
	{
		if (http_compere.responseText == 'refresh')
		{
			location.reload(true);
		}
	}
}
 
var http_compere = getHTTPObject();



var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
