 
var delayer_count = 0;
function  displayPrice(strURL,divsend,divupdate, moq, thisvalue, products_qty){
    setTimeout(function(){displayActivityCodes(strURL,divsend,divupdate, moq, thisvalue, products_qty)}, 900);
}

function displayActivityCodes(strURL,divsend,divupdate, moq, thisvalue, products_qty) {
    
    if(--delayer_count == 0) {
        var  validate = ajustMoq(moq, thisvalue, products_qty);        
    }

    if (validate){
        delayer_count=1;
        delayer(strURL,divsend,divupdate);
    }
}

function ajustMoq(moq, thisvalue, products_qty){
	var minqty = parseInt(document.getElementsByName(moq)[0].value);
	var thevalue = parseInt(thisvalue.value);
	if (thevalue < minqty){
		alert("The minimum order quantity for this item is "+minqty+" pieces. Please add "+minqty+" pieces or more to your cart.");
		thisvalue.style.color = '#FF0000';
		//thisvalue.value =  parseInt(products_qty);
		spinner_off();
		return false;
	}else{
        	thisvalue.style.color = '#000000';
        	return true;
	}
}


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 cart_updatepage(divupdate,str){

	var goto = str.match(/gotoXurl\(\"([^\"]*)\"\)/);

	if (goto && goto[1]) {
		gotoXurl(goto[1]);
	}


	var subtotal = str.match(/Sub_Total\(([^\)]*)\)/);
	
	if (subtotal && subtotal[1]) {
		document.getElementById('total_price').innerHTML = subtotal[1];
	}

	if (divupdate == 'cart_cont') {
		if (document.getElementById('also_bought').style.height != 'auto') {
			document.getElementById('also_bought').style.display = '';
			document.getElementById('also_bought_content').innerHTML = document.getElementById('what_do').innerHTML;
			if (navigator.appName=="Microsoft Internet Explorer" && parseFloat(navigator.appVersion)<=6) {
				document.getElementById('also_bought').style.height=  'auto';
			} else {
				pulldown_start('also_bought','300');
			}
		}
		document.getElementById('cart_cont').innerHTML = str;
		setTimeout(function(){fade('add_success',255,255,0,255,255,255,5)}, 100);
		return;
	}
	
	var itemtotal = str.match(/Item_Price\[([^\]]*)]\(([^\)]*)\)/);
	
	if (itemtotal && itemtotal[1]) {
		if (document.getElementById('item_price['+itemtotal[1]+']')) {
			document.getElementById('item_price['+itemtotal[1]+']').innerHTML = itemtotal[2];
		}
	} else {
		document.getElementById(divupdate).innerHTML = str;
	}
	var itemtotal = str.match(/Item_Total\[([^\]]*)]\(([^\)]*)\)/);
	if (itemtotal && itemtotal[1]) {
		if (document.getElementById('item_total['+itemtotal[1]+']')) {
			document.getElementById('item_total['+itemtotal[1]+']').innerHTML = itemtotal[2];
		}
	} else {
		document.getElementById(divupdate).innerHTML = str;
	}
	var itemtotal = str.match(/Default_Price\[([^\]]*)]\(([^\)]*)\)\(([^\)]*)\)/);
	if (itemtotal && itemtotal[1]) {
		if (document.getElementById('default_price['+itemtotal[1]+']')) {
			document.getElementById('default_price['+itemtotal[1]+']').innerHTML = itemtotal[2];
			//alert (document.getElementById('default_price['+itemtotal[2]+']').value);
			if ( itemtotal[2] != itemtotal[3] ) {
				document.getElementById('default_price['+itemtotal[1]+']').style.display = "block";
			} else {
				document.getElementById('default_price['+itemtotal[1]+']').style.display = "none";
			}
		}
	} else {
		document.getElementById(divupdate).innerHTML = str;
	}
	var itemtotal = str.match(/Item_Maq_Tax\[([^\]]*)]\(([^\)]*)\)/);
	if (itemtotal && itemtotal[1]) {
		if (document.getElementById('Item_Maq_Tax['+itemtotal[1]+']')) {
			document.getElementById('Item_Maq_Tax['+itemtotal[1]+']').innerHTML = itemtotal[2];
		}
	}
}


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;
}
function check_quant_discont(divupdate,str){
	var itemtotal = str.match(/Default_Price\[([^\]]*)]\(([^\)]*)\)\(([^\)]*)\)/);
	if (itemtotal && itemtotal[1]) {
		if (document.getElementById('default_price['+itemtotal[1]+']')) {
			document.getElementById('default_price['+itemtotal[1]+']').innerHTML = itemtotal[2];
			//alert (document.getElementById('default_price['+itemtotal[2]+']').value);
			if ( itemtotal[2] != itemtotal[3] ) {
				document.getElementById('default_price['+itemtotal[1]+']').style.display = "block";
			} else {
				document.getElementById('default_price['+itemtotal[1]+']').style.display = "none";
			}
		}
	} else {
		document.getElementById(divupdate).innerHTML = str;
	}
}

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
		);
		//document.getElementById(last_spinner).src = last_super_spinner;
		//last_super_spinner = '';
		//last_spinner = '';
	} else if (last_spinner) {
		divonoff(last_spinner,"none");
	}
}
var pullup_div;

function pullup(divid,cnt){
	if (cnt <= 0) return;
	pullup_div = divid;
	document.getElementById(pullup_div).style.height=  cnt + 'px';;
	cnt-=3;
	setTimeout('pullup(pullup_div,'+cnt+');',1);
}