var iMainPopWidth  = 700;
var iMainPopHeight = 800;

function GetCtrlByClientID(va, vb){var aa = document.getElementsByTagName(vb);
for (var i = 0; i < aa.length; i++) { if (aa[i].id.match(va + '$')) { return aa[i]; } }
return false;}

function SearchPM() {
	var objS = document.getElementById("txtSearch");
	if (objS)
	{	var strVal = objS.value;
		if (strVal.length > 0)
		{	var strKey = "";
			for(var i = 0; i < strVal.length; i++)
			{	switch( strVal.charAt(i) )
				{
					case "<":
					case ">":
					case "%":
						break;
					default:
						strKey += strVal.charAt(i);
				};
			}
			objS.value = strKey;
			document.location.href = "ProductResults.aspx?SearchKey=" + escape(strKey);
		}
		else{alert("Please enter your search criteria.");}
	}
	else{alert("Search is not active on this page.");}
}
function InitializeTimer()
{	secs = Math.ceil( (Math.random()*9)+1 );
	StopTheClock();
	StartTheTimer();
}
function StopTheClock()
{	if(timerRunning) clearTimeout(timerID);
	timerRunning = false;
}

function StartTheTimer()
{	if (secs==0)
	{	StopTheClock();
		document.forms[0].submit();
	}
	else
	{	secs -= 1;
		timerRunning = true;
		timerID = self.setTimeout("StartTheTimer()", delay);
	}
}
function JoinMailingList() {
	var m = openPopup("http://eepurl.com/n5Lf", 650, 670);
}
function OpenEmailer() {
	var m = openPopup("Emailer.aspx", iMainPopHeight, iMainPopWidth);
}
function Action_OnClick(strAction)
{	if (typeof(Page_ClientValidate) == 'function')
	{	if (Page_ClientValidate())
		{	document.forms[0].hidAction.value = strAction;
			document.forms[0].submit();
		}
	}
	else
	{	document.forms[0].hidAction.value = strAction;
		document.forms[0].submit();
	}
}
function LoadCustomer_OnClick(strAction){document.location.href = "CheckoutLoadCustomer.aspx";}
function Continue_OnClick(){Action_OnClick("continue");}
function SubmitOrder_OnClick(){Action_OnClick("submitorder");}
function GOCheckout_OnClick()
{	var blnOK = false;

	if (typeof(Page_ClientValidate) == 'function')
	{ if (Page_ClientValidate()) { blnOK = true; } }
	else
	{ blnOK = true; }

	if (blnOK) {
		var myForm = document.forms[0];
		if (myForm.radCC.checked) {
			if (pageTracker) pageTracker._linkByPost(myForm);
			myForm.hidAction.value = 'gocheckout';
			myForm.action = document.forms[0].VSADDRESS.value;
			myForm.submit();
		}
		else {
			myForm.hidAction.value = 'gomailform';
			myForm.submit();
		}
	}
}
function KeepShopping_OnClick()
{	if(window.opener) window.opener.focus();
	window.close();
}
function Back_OnClick(strTarget)
{	switch(strTarget)
	{
	case "shoppingbag":
		document.location.href = "OrderItemAdd.aspx";
		break;
	case "shipping":
		document.location.href = "CheckoutShipToAddr.aspx";
		break;
	case "checkout":
		document.location.href = "Checkout.aspx";
		break;
	};
}
function paginate(lngPage)
{	var strURL = document.location.pathname;
	var strQS  = SetQsVar( window.location.search, "PageNum", lngPage );
	document.location.href = strURL + "?" + strQS;
}
function SetQsVar(q, strVarName, strVarValue) 
{	var strRV = "";
	var blnFound = false;

	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;

	var strQSArray = new Array();
	if(q)
	{	for(var i=0; i < this.q.split("&").length; i++) 
		{	strQSArray[i] = this.q.split("&")[i];
			var strVarArray = strQSArray[i].split("=");
			if(strVarArray[0].length)
			{	strRV += strVarArray[0]; // add var name
				strRV += "=";
				if(strVarArray[0] == strVarName)
				{	strRV += strVarValue;
					blnFound = true;
				}
				else { strRV += strVarArray[1]; }
				strRV += "&";
			}
		}
		if(!blnFound) { strRV += strVarName + "=" + strVarValue; }
	}
	else { strRV = strVarName + "=" + strVarValue; }
	return strRV;
}

function isValidPostalCode(src, args) {
	var objCy = GetCtrlByClientID("ddlCountry", "select"); //document.Form1.ddlCountry;

	switch (objCy.options[objCy.selectedIndex].value)
	{	case "USA":
			return isValidZip(src, args);
			break;
		default:
			return true;
	};
}
function isValidZip(src, args)
{	var bOK = false;
	if( args.Value.length >= 5 ) { bOK = (args.Value.match(/^[0-9-]+$/)); }
	args.IsValid=bOK;
}
function openPopup (strUrl,lngWidth,lngHeight){var m = window.open(strUrl,"mypop", "toolbar=no,directories=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,width=" + lngWidth + ",height=" + lngHeight);}
function openPopup2(strUrl,lngWidth,lngHeight){var m = window.open(strUrl,"mypop2","toolbar=no,directories=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,width=" + lngWidth + ",height=" + lngHeight);}
function addToCart()
{	var i;
	var strProducts = "";
	var objCurrent;

	for (i=1;i <= document.forms[0].ProductCount.value; i++)
	{	objCurrent = eval("document.forms[0].ProductID_" + i);
		if (objCurrent)
		{	if (objCurrent.type == "checkbox") { if (objCurrent.checked) { strProducts += ((strProducts.length?",":"") + objCurrent.value); } }
			else if (objCurrent.type == "hidden") { strProducts += ((strProducts.length?",":"") + objCurrent.value); }
		}
	}
	if (strProducts.length)
	{	if(window.name == "mypop") { document.location.href = "OrderItemAdd.aspx?ProductID=" + strProducts; }
		else { var myPop = openPopup("OrderItemAdd.aspx?ProductID=" + strProducts, iMainPopHeight, iMainPopWidth); }
	}
	else { alert( "Please select an item to add to your shopping bag." ); }
}
function addToBag()
{	var i;
	var strProducts = "";

	if(document.forms[0].ProductID)
	{	if(document.forms[0].ProductID.length)
		{	for (i=0;i < document.forms[0].ProductID.length; i++)
			{	if (document.forms[0].ProductID[i].checked)
				{	strProducts += ((strProducts.length?",":"") + document.forms[0].ProductID[i].value);
					document.forms[0].ProductID[i].checked = false;
				}
			}
		}
		else { strProducts += ((strProducts.length?",":"") + document.forms[0].ProductID.value); }
	}
	if (strProducts.length) { var myPop = openPopup("OrderItemAdd.aspx?ProductID=" + strProducts, iMainPopHeight, iMainPopWidth); }
	else { alert( "Please select an item to add to your shopping bag." ); }
}
function goChat() { var myPop = openNamedPopup("PopChat","MonkeyRap/ClientInfo.aspx", 700, 500); }
function contactUs() { var myPop = openNamedPopup("PopContact", "contactUs.aspx", iMainPopHeight, iMainPopWidth); }
function notifyMe(strID) { var myPop = openNamedPopup("PopContact", "contactUs.aspx?ProductID=" + strID, iMainPopHeight, iMainPopWidth); }

function openNamedPopup(strName, strUrl, lngWidth, lngHeight) {
	var m = window.open(strUrl, strName, "toolbar=no,directories=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,width=" + lngWidth + ",height=" + lngHeight); 
}

function viewItem(strId) { var myPop = openPopup("ProductDetail.aspx?ProductID=" + strId, iMainPopHeight, iMainPopWidth); }
function viewCart() { var myPop = openPopup("OrderItemAdd.aspx", iMainPopHeight, iMainPopWidth); }
function privacyPolicy() { var myPop = openPopup("privacyPolicy.aspx", iMainPopHeight, iMainPopWidth); }
function returnPolicy() { var myPop = openPopup("returnPolicy.aspx", iMainPopHeight, iMainPopWidth); }
function orderingInfo() { var myPop = openPopup("orderingInfo.aspx", iMainPopHeight, iMainPopWidth); }
function termsConditions() { var myPop = openPopup("termsConditions.aspx", iMainPopHeight, iMainPopWidth); }
function MM_preloadImages()
{	var d=document;
	if(d.images)
	{	if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
		{	if (a[i].indexOf("#")!=0)
			{	d.MM_p[j]=new Image; 
				d.MM_p[j++].src=a[i];
			}
		}
	}
}
function MM_swapImgRestore()
{	var i,x,a=document.MM_sr;
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d)
{	var p,i,x;
	if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{	d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); 
	return x;
}
function MM_swapImage()
{	var i,j=0,x,a=MM_swapImage.arguments;
	document.MM_sr=new Array;
	for(i=0;i<(a.length-2);i+=3)
	{	if ((x=MM_findObj(a[i]))!=null)
		{	document.MM_sr[j++]=x;
			if(!x.oSrc) x.oSrc=x.src;
			x.src=a[i+2];
		}
	}
}
function correctPNG() {
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])
if ((version >= 5.5) && (document.body.filters)){
for(var i=0; i<document.images.length; i++){
var img = document.images[i]
var imgName = img.src.toUpperCase()
if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
var imgID = (img.id) ? "id='" + img.id + "' " : ""
var imgClass = (img.className) ? "class='" + img.className + "' " : ""
var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
var imgStyle = "display:inline-block;" + img.style.cssText 
if (img.align == "left") imgStyle = "float:left;" + imgStyle
if (img.align == "right") imgStyle = "float:right;" + imgStyle
if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
var strNewHTML = "<span " + imgID + imgClass + imgTitle
+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
img.outerHTML = strNewHTML
i = i-1
}
}
}
}

