//Define PopupWindow process
function winopen(filenamewtoken,winWidth,winHeight,whichwindow) {
	closechild(); 
 	if (document.all) {	var xMax = screen.width, yMax = screen.height;
    } else {
		if (document.layers) { var xMax = window.outerWidth, yMax = window.outerHeight;
        } else { var xMax = 800, yMax=600; }
	}
	var xOffset = (xMax - winWidth)/2, yOffset = ((yMax - winHeight)/2) - 30;
	//Whichwindow == 1 -- No Scrolls, no Menu
	//Whichwindow == 2 -- Scrolls, no Menu
	//Whichwindow == 3 -- Scrolls, Menu
	//Whichwindow == 4 -- Custom: for shopping cart popup window
	if (whichwindow == 1) { PopupWindow = window.open(filenamewtoken,'PopupWindow', 'screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,hotkeys=no,resizable=no,width='+winWidth+',height='+winHeight+'');
	} else if (whichwindow == 2) { PopupWindow = window.open(filenamewtoken,'PopupWindow', 'screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,hotkeys=no,resizable=no,width='+winWidth+',height='+winHeight+'');
	} else if (whichwindow == 4) { PopupWindow = window.open(filenamewtoken,'PopupWindow', 'screenX='+1+',screenY='+yOffset+',top='+yOffset+',left='+1+',toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,hotkeys=no,resizable=no,width='+winWidth+',height='+winHeight+'');
	} else { PopupWindow = window.open(filenamewtoken,'PopupWindow', 'screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,hotkeys=no,resizable=no,width='+winWidth+',height='+winHeight+'');
	}
	if (!PopupWindow.opener) { PopupWindow.opener = self; }
	PopupWindow.focus();
	if (PopupWindow && PopupWindow.open && !PopupWindow.closed) {
	 	if (document.all && PopupWindow.document && PopupWindow.document.body && PopupWindow.document.body.clientWidth) {	
			if (Math.abs(winWidth-PopupWindow.document.body.clientWidth) > 30) { winopen(filenamewtoken,winWidth,winHeight,whichwindow); }
	    } else if (PopupWindow.innerWidth) {
			if (Math.abs(winWidth-PopupWindow.window.innerWidth) > 30) { winopen(filenamewtoken,winWidth,winHeight,whichwindow); }
		}
	}
}

//This function closes the PopupWindow if it is open when the main page is redirected
var PopupWindow = '';
function closechild() { if (PopupWindow && PopupWindow.open && !PopupWindow.closed) { PopupWindow.close(); } }

//Pre-Load top navigation rollover imagery
if (window.imageNameArray1) {
	for (var i=0;i<imageNameArray1.length;i++) {
		eval(imageNameArray1[i]+'_up = new Image();'+imageNameArray1[i]+'_up.src = "'+temprooturlpath+'images/navigation/'+imageNameArray1[i]+'_up.gif"');
		eval(imageNameArray1[i]+'_over = new Image();'+imageNameArray1[i]+'_over.src = "'+temprooturlpath+'images/navigation/'+imageNameArray1[i]+'_over.gif"');
	}
}
if (window.imageNameArray2) {
	for (var i=0;i<imageNameArray2.length;i++) {
		eval(imageNameArray2[i]+'_up = new Image();'+imageNameArray2[i]+'_up.src = "'+temprooturlpath+'images/rollover_buttons/'+imageNameArray2[i]+'_up.gif"');
		eval(imageNameArray2[i]+'_over = new Image();'+imageNameArray2[i]+'_over.src = "'+temprooturlpath+'images/rollover_buttons/'+imageNameArray2[i]+'_over.gif"');
		eval(imageNameArray2[i]+'_down = new Image();'+imageNameArray2[i]+'_down.src = "'+temprooturlpath+'images/rollover_buttons/'+imageNameArray2[i]+'_down.gif"');
	}
}

//Mouse fire functions
var deactivatemouse = 0;
function mouseup(name) { if (deactivatemouse == 0) { document[name].src = eval(name + "_up.src"); } }
function mouseover(name) { if (deactivatemouse == 0) { document[name].src = eval(name + "_over.src"); } }
function mousedown(name) { if (deactivatemouse == 0) { document[name].src = eval(name + "_down.src"); } }

//Enter key press trap
function TrapEnterKeyPress(e) {
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode; 
	if (keyCode == 13) { return false; } else { return true; }
}

//Adjusts the states dropdown list
function AdjustStates(whichOne) {
	eval('document.module_form.StateList'+whichOne+'.options.length = 0');
	var tempindex = eval('document.module_form.CountryList'+whichOne+'.selectedIndex');
	if (ArrayCounter[tempindex] == 0) {
		var newoption = new Option(' Not Applicable  ', '', false, false);
		eval('document.module_form.StateList'+whichOne+'.options[0] = newoption');
	} else {
		for (j=0; j<ArrayCounter[tempindex]; j=j+1) {
			var newoption = new Option(' '+StateNameArray[tempindex][j]+'  ', StateAbbArray[tempindex][j], false, false);
			eval('document.module_form.StateList'+whichOne+'.options[j] = newoption');
		}
	}
	eval('document.module_form.StateList'+whichOne+'.selectIndex = 0');
}

//Logged-in User Session Timeout functions
function DisplayBadSessionNews() {
	self.focus();
	window.alert("Your user session has run out because of inactivity and for security purposes you have been automatically logged out. Please login in again before continuing.");
 	document.module_form.SessionOverrideFlag.value = 1;
	if (document.module_form.genericcyle) { document.module_form.genericcyle.value=1; }
	document.module_form.submit(); 
}
function InitiateSessionTimeoutMonitoring(loggedinusersessionlengthinminutes) {
	var myTimer = null;
	myTimer = setTimeout('DisplayBadSessionNews();',((loggedinusersessionlengthinminutes+1)*60000)); 
}
function DisplayBadCheckoutSessionNews() {
	self.focus();
	window.alert("Your checkout session has been interrupted due to user inactivity. You may reinitiate the checkout process at your convenience.");
 	document.module_form.SessionOverrideFlag.value = 1;
 	document.module_form.submit(); 
}
function InitiateCheckoutSessionTimeoutMonitoring(loggedinusersessionlengthinminutes) {
	var myTimer = null;
	myTimer = setTimeout('DisplayBadCheckoutSessionNews();',((loggedinusersessionlengthinminutes+1)*60000)); 
}
function WindowManagement(someminutes) {
  	var myTimer = null;
	myTimer = setTimeout('window.close();',(someminutes*60000));
}

//This function validates user product variation selections
var veryTempDropDownListValue = 0;
var tester=new Image();
function CheckSelectedVariationCombination() {
	var tempErrorString = "Product Variation [";
	for (k=1; k<document.module_form.VariationHiddenFieldCount.value; k=k+1) {
		for (j=0; j<SKUVariationValueArray.length; j=j+1) {
			if (eval('document.module_form.Variation'+k+'.value') == SKUVariationValueArray[j]) {
			 	if (k > 1) { tempErrorString = tempErrorString + "-->"; }
				tempErrorString = tempErrorString + eval('document.module_form.Variation'+k+'ParentCategoryName.value');
				var t1 = SKUVariationNameArray[j].replace('&quote;','\"');
				t1 = t1.replace('&ordm;','deg');
				t1 = t1.replace('&amp;','&');
				tempErrorString = tempErrorString + t1;
			}
		}
	}
	tempErrorString = tempErrorString + "] Is Not Available For Purchase -- Returning To the Last Selected Variation.";

	var itemFound = 0;
	var tempCounter = 0;
	var tempVarArrayString = '';
	var miniVarArrayString = '';
	var tempSKUID = 0;
	for (k=0; k<SKUVariationStringArray.length; k=k+1) {
		tempCounter = 1;
		tempVarArrayString = ','+SKUVariationStringArray[k]+',';
		for (j=1; j<document.module_form.VariationHiddenFieldCount.value; j=j+1) {
			miniVarArrayString = ','+eval('document.module_form.Variation'+j+'.value')+',';
			if (tempVarArrayString.indexOf(miniVarArrayString) > -1) { tempCounter = tempCounter + 1; }
		}
		if (tempCounter >= document.module_form.VariationHiddenFieldCount.value) { 
			document.module_form.ProductSKUID.value = SKUIDArray[k];
			tempSKUID = SKUIDArray[k];
			itemFound = 1; 
			break; 
		}
	}

	if (itemFound == 0) {
		alert(tempErrorString);
		for (k=1; k<document.module_form.VariationHiddenFieldCount.value; k=k+1) {
			eval('document.module_form.Variation'+k+'.selectedIndex = document.module_form.Variation'+k+'SelectedIndex.value');
		}
	} else {
		for (k=1; k<document.module_form.VariationHiddenFieldCount.value; k=k+1) {
			for (j=0; j<eval('document.module_form.Variation'+k+'.length'); j=j+1) {
				if (eval('document.module_form.Variation'+k+'['+j+'].value == document.module_form.Variation'+k+'.value')) {
					eval('document.module_form.Variation'+k+'SelectedIndex.value = '+j);
				}
			}
		}
		if (document.module_form.displayedImageName && document.module_form.displayedImageName.value != '' && document['alternateProductImageDisplay']) {
			var tester=new Image();
			tester.onload=function() { document['alternateProductImageDisplay'].src = tester.src; };
			tester.onerror=function() { document['alternateProductImageDisplay'].src = document.module_form.displayedImageName.value.replace('[[',''); };
			tester.src=document.module_form.displayedImageName.value.replace('[[','_'+tempSKUID);
			document.module_form.ProductSKUID.value=tempSKUID;
		}
	}
}

// Adjust Address Checkout Form Field Values When 'Use Shipping' Checkbox is clicked
function adjustAddressCheckoutFormBillToValues() {
	if (document.module_form.SameAsShipToCheckBox.checked) {
		document.module_form.OrderBillToCompanyName.value = '';
		document.module_form.OrderBillToEmployeeID.value = '';
		document.module_form.OrderBillToFirstName.value = '';
		document.module_form.OrderBillToLastName.value = '';
		document.module_form.OrderBillToAddress1.value = '';
		document.module_form.OrderBillToAddress2.value = '';
		document.module_form.OrderBillToCity.value = '';
		document.module_form.OrderBillToZip.value = '';
		document.module_form.OrderBillToPhone.value = '';
		document.module_form.OrderBillToFax.value = '';
		document.module_form.OrderBillToEmail.value = '';
	}
}

// Adjust Address Checkout Form Field Values When 'Fill' button is clicked
function adjustHistoricalAddressCheckoutFormFieldValues(whichfieldgroup) {
	if (whichfieldgroup == 'ship') {
		var fieldgroupindex = document.module_form.HistoricalShip[document.module_form.HistoricalShip.selectedIndex].value - 1;
		document.module_form.OrderShipToCompanyName.value = ShippingArray[fieldgroupindex][0];
		document.module_form.OrderShipToFirstName.value = ShippingArray[fieldgroupindex][1];
		document.module_form.OrderShipToLastName.value = ShippingArray[fieldgroupindex][2];
		document.module_form.OrderShipToAddress1.value = ShippingArray[fieldgroupindex][3];
		document.module_form.OrderShipToAddress2.value = ShippingArray[fieldgroupindex][4];
		document.module_form.OrderShipToCity.value = ShippingArray[fieldgroupindex][5];
		document.module_form.OrderShipToZip.value = ShippingArray[fieldgroupindex][7];
		document.module_form.OrderShipToPhone.value = ShippingArray[fieldgroupindex][9];
		document.module_form.OrderShipToFax.value = ShippingArray[fieldgroupindex][10];
		document.module_form.OrderShipToEmail.value = ShippingArray[fieldgroupindex][11];
		document.module_form.OrderShipToEmployeeID.value = ShippingArray[fieldgroupindex][12];
		for (i = 0; i < document.module_form.CountryList1.length; i++) {
			if (document.module_form.CountryList1[i].value == ShippingArray[fieldgroupindex][8]) {
				document.module_form.CountryList1.selectedIndex = i;
				break;
			}
		}
		var stateFound = 0;
		for (i = 0; i < document.module_form.StateList1.length; i++) {
			if (document.module_form.StateList1[i].value == ShippingArray[fieldgroupindex][6]) {
				document.module_form.StateList1.selectedIndex = i;
				document.module_form.javascriptCountryStateProblemsCycleCounter.value = 0;
				stateFound = 1;
				break;
			}
		}
		if (stateFound == 0 && (!document.module_form.javascriptCountryStateProblems || (document.module_form.javascriptCountryStateProblems && document.module_form.javascriptCountryStateProblems.value < 0))) {
			document.module_form.StateList1[0].value = ShippingArray[fieldgroupindex][6];
			document.module_form.javascriptCountryStateProblems.value=fieldgroupindex;
			document.module_form.javascriptCountryStateProblemType.value='ship';
			if (document.module_form.javascriptCountryStateProblemsCycleCounter.value < 3) {
				document.module_form.javascriptCountryStateProblemsCycleCounter.value = document.module_form.javascriptCountryStateProblemsCycleCounter.value + 1;
				document.module_form.submitRequest.disabled=true;
				document.module_form.submit();
			}
		}
	} else {
		var fieldgroupindex = document.module_form.HistoricalBill[document.module_form.HistoricalBill.selectedIndex].value - 1;
		document.module_form.OrderBillToCompanyName.value = BillingArray[fieldgroupindex][0];
		document.module_form.OrderBillToFirstName.value = BillingArray[fieldgroupindex][1];
		document.module_form.OrderBillToLastName.value = BillingArray[fieldgroupindex][2];
		document.module_form.OrderBillToAddress1.value = BillingArray[fieldgroupindex][3];
		document.module_form.OrderBillToAddress2.value = BillingArray[fieldgroupindex][4];
		document.module_form.OrderBillToCity.value = BillingArray[fieldgroupindex][5];
		document.module_form.OrderBillToZip.value = BillingArray[fieldgroupindex][7];
		document.module_form.OrderBillToPhone.value = BillingArray[fieldgroupindex][9];
		document.module_form.OrderBillToFax.value = BillingArray[fieldgroupindex][10];
		document.module_form.OrderBillToEmail.value = BillingArray[fieldgroupindex][11];
		document.module_form.OrderBillToEmployeeID.value = BillingArray[fieldgroupindex][12];
		document.module_form.SameAsShipToCheckBox.checked = false;
		for (i = 0; i < document.module_form.CountryList2.length; i++) {
			if (document.module_form.CountryList2[i].value == BillingArray[fieldgroupindex][8]) {
				document.module_form.CountryList2.selectedIndex = i;
				break;
			}
		}
		var stateFound = 0;
		for (i = 0; i < document.module_form.StateList2.length; i++) {
			if (document.module_form.StateList2[i].value == BillingArray[fieldgroupindex][6]) {
				document.module_form.StateList2.selectedIndex = i;
				document.module_form.javascriptCountryStateProblemsCycleCounter.value = 0;
				stateFound = 1;
				break;
			}
		}
		if (stateFound == 0 && (!document.module_form.javascriptCountryStateProblems || (document.module_form.javascriptCountryStateProblems && document.module_form.javascriptCountryStateProblems.value < 0))) {
			document.module_form.StateList2[0].value = BillingArray[fieldgroupindex][6];
			document.module_form.javascriptCountryStateProblems.value=fieldgroupindex;
			document.module_form.javascriptCountryStateProblemType.value='bill';
			if (document.module_form.javascriptCountryStateProblemsCycleCounter.value < 3) {
				document.module_form.javascriptCountryStateProblemsCycleCounter.value = document.module_form.javascriptCountryStateProblemsCycleCounter.value + 1;
				document.module_form.submitRequest.disabled=true;
				document.module_form.submit();
			}
		}
	}
}