var delayer_count = 0;

function cart_updatepage(divupdate, str)
{
	var goto = str.match(/gotoXurl\(\"([^\"]*)\"\)/);
	
	if (goto && goto[1]) 
	{
		gotoXurl(goto[1]);
	}
    
	document.getElementById('shopping_cart_div').innerHTML = str;
}

function cvxmlhttpPostButton(strURL,divsend,divupdate,button) 
{
    var xmlHttpReq = false;
    var self = this;
    //document.getElementById(divupdate).style.cursor = 'wait';
    // Mozilla/Safari
    
    if (window.XMLHttpRequest) 
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) 
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() 
    {
        if (self.xmlHttpReq.readyState == 4 || self.xmlHttpReq.readyState == "complete") 
        {
            cart_updatepage(divupdate, self.xmlHttpReq.responseText);
	        //document.getElementById(divupdate).style.cursor = 'auto';
        }
    }
    self.xmlHttpReq.send(getquerystring(divsend,button));
}

function spinner_on(whichLayer)
{
	last_spinner = whichLayer;
	divonoff(whichLayer,"block");
}

function divonoff(whichLayer,dvalue)
{
	if (document.getElementById) 
	{
		// this is the way the standards work
		if (document.getElementById(whichLayer)) 
		{
			var style2 = document.getElementById(whichLayer).style;
			style2.display = dvalue;
		}
	}
	else if (document.all) 
	{
		// this is the way old msie versions work
		if (document.all[whichLayer])
		{
			var style2 = document.all[whichLayer].style;
			style2.display = dvalue;
		}
	}
	else if (document.layers) 
	{
		// this is the way nn4 works
		if (document.layers[whichLayer]) 
		{
			var style2 = document.layers[whichLayer].style;
			style2.display = dvalue;
		}
	}
}

function delayer(strURL, divsend, divupdate) 
{
	if (--delayer_count == 0) 
    {
        cvxmlhttpPost(strURL, divsend, divupdate);
    }
}

var request_queue = new Array();
var request_queue_cnt = 0;
function cvxmlhttpPost(strURL, divsend, divupdate) 
{
	if (request_queue_cnt++ > 0) 
	{
		var req = new Array();
		req[0] = strURL;
		req[1] = divsend;
		req[2] = divupdate;
		request_queue.push(req);
		//console.warn("Pushing to queue:"+req[0]+":"+req[1]+":"+req[2]);
		return;
	}
	//console.warn("Executing Req:"+request_queue_cnt);
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) 
	{
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) 
	{
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!strURL) 
	{
		console.error("Broken Request");
		return;
	}
	cvxmlhttpPostRaw(strURL, divsend, divupdate);
}

function cvxmlhttpPostRaw(strURL, divsend, divupdate) 
{
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() 
	{
		if (self.xmlHttpReq.readyState == 4 || self.xmlHttpReq.readyState == "complete") 
		{
			cart_updatepage(divupdate, self.xmlHttpReq.responseText);
			spinner_off();
			if (--request_queue_cnt > 0) 
			{
				var req = request_queue.shift();
				//console.warn("Taking off queue:"+req[0]+":"+req[1]+":"+req[2]);
				setTimeout(function(){cvxmlhttpPostRaw(req[0], req[1], req[2]);}, 100);
			}
		}
	}
	self.xmlHttpReq.send(getquerystring(divsend, false));
}

function getquerystring(divsend,button) 
{
	// already prepared post?
	if (divsend.indexOf("=") > 0) 
	{
		return divsend;
	}
	var qstr = '';
	if (!document.forms[divsend]) 
	{
		alert("no '"+divsend+"' object");
		return "";
	}
	for(i = 0; i < document.forms[divsend].elements.length; i++) 
	{
		if (qstr) 
		{
			qstr += "&";
		}
		// in case of submit buttons use only the clicked one
		if (button && document.forms[divsend].elements[i].name == button.name) 
		{
			if (document.forms[divsend].elements[i].value == button.value) 
			{
				qstr += document.forms[divsend].elements[i].name + '=' + escape(document.forms[divsend].elements[i].value);
			}
		}
		else if (document.forms[divsend].elements[i].type != "radio" || document.forms[divsend].elements[i].checked) 
		{
			qstr += document.forms[divsend].elements[i].name + '=' + escape(document.forms[divsend].elements[i].value);
		}
	}
	//alert(divsend + "<->" + form.elements.length + qstr);
	return qstr;
}

function gotoXurl(url) 
{
	window.location = url;
}

var last_spinner;
var last_super_spinner;
function spinner_off()
{
	if (last_super_spinner) 
	{
		setTimeout(function(){document.getElementById(last_spinner).src = last_super_spinner;}, 500);
	} 
	else if (last_spinner) 
	{
		divonoff(last_spinner,"none");
	}
}

function get_country_name()
{
   var s=document.getElementById("countries_name").value;
   return s;
}