function PopUp(nURL,winName,popW,popH,nShowControls) {
  	w = screen.width ; h = screen.height
  	if (document.layers) {
    		if ((w >= 800) && (w <= 1028)) {w = 1028}
    		if ((h >= 600) && (h <= 780))  {h = 780}
    		if ((w >= 640) && (w <= 800))  {w = 800}
    		if ((h >= 480) && (h <= 600))  {h = 600}
    		if (w <= 640) {w = 640}
    		if (h <= 480) {h = 480}
  	}
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	if (nShowControls=="1") {
		var windowprops = "location=yes,scrollbars=yes,menubar=yes,toolbar=yes,resizable=yes,status=yes" + ",left=" + leftPos + ",top=" + topPos + ",width=" + popW + ",height=" + popH;
	} else {
		var windowprops = "location=no,scrollbars=yes,menubar=no,toolbar=no,resizable=no,status=yes" + ",left=" + leftPos + ",top=" + topPos + ",width=" + popW + ",height=" + popH;
	}
	popup = window.open(nURL,winName,windowprops);
	if (popup.blur) {
	  popup.focus();
	}
}

function gotoHref(theSelect) {
	if(theSelect.value != "") {
		thehref = theSelect.options[theSelect.selectedIndex].value;
		window.location.href = thehref;
	}
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
}

// Next 4 functions exclusively for option dropdowns
function initDropdown(){
	document.forms[1].nOption1.selectedIndex = 0;
	//alert(document.forms[0].nOption1.selectedIndex);
}
function showHideButtons(nNum,nTotalNum,nValue,nUniqueID,xyz_OptionValueArray) {
	// Remember value of dropdown for later use
	xyz_OptionValueArray[nNum] = nValue;
	if (nNum!=nTotalNum) {
		eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_4','','hide')");
		// If option is set to no value, hide all but this one, otherwise show next
		if(nValue!=0){
			// show the next row message and options
			do{ 
				hideAllMessagesExcept(nNum+1,nTotalNum,nUniqueID);
				hideAllOptionsAbove(nNum+1,nTotalNum,nUniqueID);
				nNum++;
			} while (xyz_OptionValueArray[nNum]!=undefined && xyz_OptionValueArray[nNum]!="" && nNum<=nTotalNum)
			if(nNum>=nTotalNum && xyz_OptionValueArray[nTotalNum]!=undefined && xyz_OptionValueArray[nTotalNum]!=""){
				eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_4','','show')");
			}
		} else {
			// show only this row's message
			hideAllMessagesExcept(nNum,nTotalNum,nUniqueID);
			hideAllOptionsAbove(nNum,nTotalNum,nUniqueID);
		}
	} else {
		if(nValue==0){
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_3','','show')");
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_4','','hide')");
		} else {
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_3','','hide')");
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_4','','show')");
		}
	}
}
function hideAllMessagesExcept(nNum,nTotalNum,nUniqueID) {
	for(x=1;x<=nTotalNum;x++){
		if(x!=nNum){ 
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + x + "_3','','hide')");
		} else {
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + x + "_3','','show')");
		}
	}
}
function hideAllOptionsAbove(nNum,nTotalNum,nUniqueID) {
	for(x=1;x<=nTotalNum;x++){
		if(x>nNum){ 
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + x + "_1','','hide')");
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + x + "_2','','hide')");
		} else {
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + x + "_1','','show')");
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + x + "_2','','show')");
		}
	}
}

// Function exclusively for option dropdown SKU appendage
function updateSKU(nUnalteredSKU,nSKUArray,nIndex,formName,nUpdateString,nSKUOverrideArray,nSKUPossibleORArray,nTotalNum,nUniqueID) {
	// make sure passed info is valid for processing
	var nAlteredSKU = nUnalteredSKU;
	if(nUpdateString.indexOf("__") != -1) {
		var nUpdateString = nUpdateString.substring((nUpdateString.indexOf("__")+2), nUpdateString.length);
		// append the new item to this product's SKU, start with array then loop through
		nSKUArray[nIndex-1] = nUpdateString;
	} else {
		nSKUArray[nIndex-1] = "";
	}
	for(x=1;x<=nSKUArray.length;x++){
		nValue = nSKUArray[x-1];
		if(nValue!=undefined){ nAlteredSKU = nAlteredSKU + nValue; }
	}
	// On last dropdown, check for match of SKU override...use that instead if match found
	// get the selected index of all available dropdowns
	nOption1Index=1;nOption2Index=1;nOption3Index=1;nOption4Index=1;nOption5Index=1;
	for(x=1;x<=nTotalNum;x++){
		eval ("nOption" + x + "Index = document." + formName + ".nOption" + x + ".selectedIndex;");
	}
	// Loop through the array and look for a match of where we are in the dropdown selections
	var matchedSKU = "";
	for(x=1;x<=nSKUPossibleORArray.length;x++){
		var nOptionIndexStr = nOption1Index +''+ nOption2Index +''+ nOption3Index +''+ nOption4Index +''+ nOption5Index;
		if(nOptionIndexStr==nSKUPossibleORArray[x-1][1]){
			matchedSKU = nSKUPossibleORArray[x-1][0];
		}
	}
	// Now find the override SKU using our matched SKU string
	var nFinalOverride="";
	if (matchedSKU){
		// Loop through all overrides and compare
		for(x=1;x<=nSKUOverrideArray.length;x++){
			// break the override into pieces
			var nCurrentOverride = nSKUOverrideArray[x-1];
			var nCurrentOverrideArray = nCurrentOverride.split("_");
			var nSKUToMatch = nCurrentOverrideArray[0]+"_"+nCurrentOverrideArray[1];
			if(nSKUToMatch==matchedSKU){
				nFinalOverride = nCurrentOverrideArray[2];
			}
		}
	}
	// set SKU (or override, if applicable)
	if(nFinalOverride!=""){
		eval ("document." + formName + ".nProductID.value = nFinalOverride");
		var itemVoided = nFinalOverride.indexOf("[-#VOID#-]");
		if(itemVoided!=-1){
			alert('We currently do NOT have the product in stock\nwhich matches the options you have chosen.\nPlease retry your selection.');
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_4','','hide')");
		}
	} else {
		eval ("document." + formName + ".nProductID.value = nAlteredSKU");
		var itemVoided = nFinalOverride.search("[-#VOID#-]");
		if(itemVoided!=-1){
			alert('We currently do NOT have the product in stock\nwhich matches the options you have chosen.\nPlease retry your selection.');
			eval ("MM_showHideLayers('" + nUniqueID + "_cart" + nTotalNum + "_4','','hide')");
		}
		
	}
}

//Function for logged-in user quick order selection
function hiliteMe(rowObj,nRowNum,nCheckBoxNum,hiliteArray){
	if(!nCheckBoxNum){
		if(rowObj.style.backgroundColor=="#ffffff" || rowObj.style.backgroundColor==""){
			// turn highlighting on
			rowObj.style.backgroundColor='#FEF9D6';
			rowObj.style.color='#000000';
		} else {
			if (hiliteArray[nRowNum-1]==0 || hiliteArray[nRowNum-1]==undefined) {
				// turn highlighting off
				rowObj.style.backgroundColor='#FFFFFF';
				rowObj.style.color='#000000';
			}
		}
	} else {
		// toggle on/off setting of highlighting for this row
		var nCurrSetting = hiliteArray[nCheckBoxNum-1];
		if (nCurrSetting==0 || nCurrSetting==undefined) {hiliteArray[nCheckBoxNum-1]=1;} else {hiliteArray[nCheckBoxNum-1]=0;}
	}
}

//AJAX
var timerId = 0;
submitMinicart = function(div,url,formName) {
	$(div).fade({ duration: 0.4, from: 1, to: 0.6 });
    new Ajax.Updater(div,url,{
        method: "POST", asynchronous: true, parameters: Form.serialize(formName),
        onFailure: function(oXHR){ $(div).update(oXHR.statusText); },							
        onSuccess: function(oXHR){
			$('minicartNote').show();
			$('minicartNote').fade({ duration: 0.4, from: 0, to: 1 });
			$(formName).reset();
			timerId = setTimeout('hideMCNote()',5000);
			$(div).fade({ duration: 0.3, from: 0.6, to: 1 });
			$(div).update(oXHR.responseText);
		}
	});
	return false;
}
function hideMCNote(){
	Effect.Fade('minicartNote', { duration: 0.3, from: 1, to: 0 });
	if(timerId){ clearTimeout(timerId); timerId=0; }
}
