/*
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' Title			: Javascript | General scripts for Commerce Component
' Description	: This file contains general scripts used throughout the  
'				  commerce component.  
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
' History
' 08/24/2005	: David Ellenwood - Moved code from 'javascript.asp' and changed markup for CSS based layouts
'
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

// Sets the height of the colms equal in Pleasoning Seasoning
// Called on body onload

	function adjustColmHeights() {
		colm1Height = document.getElementById("colm1").offsetHeight;
		colm3Height = document.getElementById("colm3").offsetHeight;
		
		//alert(colm1Height);
		//alert(colm3Height);
		
		if(colm1Height <= colm3Height) {
			colm1Height = colm3Height;
			document.getElementById("colm1").style.height = colm1Height +'px';
		} else {
			colm3Height = colm1Height;
			/* if (document.getElementById("compNews")) {
				compNewsHeight = colm3Height - 10
				document.getElementById("compNews").style.height = compNewsHeight + 'px';
			} */
			document.getElementById("colm3").style.height = colm3Height +'px';
		}
	}

// ' ~~~~ Seach Form Validation ~~~~
function searchFormValidator(theForm)
{

  if (theForm.SearchQuery.value == "")
  {
    alert("Please enter a value for the \"Search Keywords\" field.");
    theForm.SearchQuery.focus();
    return (false);
  }

  if (theForm.SearchQuery.value.length > 100)
  {
    alert("Please enter at most 100 characters in the \"Search Keywords\" field.");
    theForm.SearchQuery.focus();
    return (false);
  }
  return (true);
}

// ' View Product Checks
function viewProductCheck() {
	var i;
	var prodV = document.getElementById('productView'); //productView.elements;
	var formElements = prodV.elements;
	var eName;
	var bSingleValidElementFound = false;
	var proceed = true;
	for(i=0; i<formElements.length; i++){
		eName = formElements[i].id;
		
		eValue = formElements[i].value;
		if(eName.indexOf("Quantity_") > -1)
		{
			//alert('name: ' + eName + ' - ' + formElements[i].id);			
			if(eValue!='0' && !Number(eValue))
			{
				proceed=false;
				formElements[i].value = '0';
				//window.alert("Please make sure all item quantities are numbers and greater than or equal to 0.");
			}
			if (eValue>'0' && Number(eValue))
			{
				bSingleValidElementFound=true;
			}
		}
		else
		{
			//alert("number of items is less than 1 or equal to zero");
		}
	}
	if (bSingleValidElementFound==true) //&& proceed=true
	{
		/* if (document.getElementById("chkCustomEmbroidery").checked == true) 
		{
			if (checkEmbroideryForm()==true) {proceed=true;}
			else {proceed= false;}
		}
		else
		{
			proceed = true;			
		} */
	}
	else
	{
		if (bSingleValidElementFound==false) {
			window.alert("Please make sure at least one Size has a quantity of 1 or more and that all item quantities are greater than or equal to 0.");
			proceed = false;
		}
	}
	return proceed;
	
}




// ' ~~~~ Shopping Cart specific scripts ~~~~ 
	
	function ConfirmCartQuantities(){
	var i;
	var formElements = document.frmCartItems.elements;
	var eName;
	var proceed = true;
	for(i=0; i<formElements.length; i++){
		eName = formElements[i].name;
		eValue = formElements[i].value;
		if(eName.indexOf("Quantity_") > -1){
			if(eValue!='0' && !Number(eValue)){
				proceed=false;
				window.alert("Please make sure all item quantities are numbers and greater than or equal to 0.");
			}
		}
	}
	return proceed;
}
function ModifyCartItems() {
	if(ConfirmCartQuantities()){
		document.frmCartItems.submit();
	}
}
function Checkout() {
	document.frmCartItems.elements['action'].value="PrepareOrder"
	if(ConfirmCartQuantities()){
		document.frmCartItems.submit();
	}
}
function ContinueShopping() {
	if(ConfirmCartQuantities()){
		document.frmCartItems.elements['action'].value="Browse";
		document.frmCartItems.submit();
	}
}
function RemoveCartItem(intOrderDetailID){
	if(ConfirmCartQuantities()){
		document.frmCartItems.elements["Quantity_" + intOrderDetailID].value = "0"
		document.frmCartItems.submit();
	}
}


function ApprovedShipping() {
	var varFrmSingleShipping =document.getElementById('SingleShippingForm');
			varFrmSingleShipping.submit();

}
// ' ~~~~ Shipping Address scripts ~~~~

function ShowShippingCheck() {
	var varFrmSingleShipping =document.getElementById('SingleShippingForm');
	if (varFrmSingleShipping.AddressID.length != null) {
		if (varFrmSingleShipping.AddressID[0].checked) {
			
			if(varFrmSingleShipping.txtFirstname.value ==''){
				window.alert("Please provide your first name.")
				varFrmSingleShipping.txtFirstname.focus();
				return;
			}
			if(varFrmSingleShipping.txtLastname.value ==''){
				window.alert("Please provide your last name.")
				varFrmSingleShipping.txtLastname.focus();
				return;
			}
			if(varFrmSingleShipping.txtAddress1.value ==''){
				window.alert("Please provide your address.")
				varFrmSingleShipping.txtAddress1.focus();
				return;
			}
			if(varFrmSingleShipping.txtCity.value ==''){
				window.alert("Please provide your city.")
				varFrmSingleShipping.txtCity.focus();
				return;
			}
			if((varFrmSingleShipping.txtZipcode.value =='')||(varFrmSingleShipping.txtZipcode.value.length != 5)){
				window.alert("Please provide your 5-digit zipcode.")
				varFrmSingleShipping.txtZipcode.focus();
				return;
			}
			if(varFrmSingleShipping.txtPhone.value ==''){
				window.alert("Please provide your phone number.")
				varFrmSingleShipping.txtPhone.focus();
				return;
			}
			varFrmSingleShipping.submit();
		} else {
			varFrmSingleShipping.submit();
		}
		
	} else {
			if (varFrmSingleShipping.AddressID.checked) {
			
			if(varFrmSingleShipping.txtFirstname.value ==''){
				window.alert("Please provide your first name.")
				return;
			}
			if(varFrmSingleShipping.txtLastname.value ==''){
				window.alert("Please provide your last name.")
				return;
			}
			if(varFrmSingleShipping.txtAddress1.value ==''){
				window.alert("Please provide your address.")
				return;
			}
			if(varFrmSingleShipping.txtCity.value ==''){
				window.alert("Please provide your city.")
				return;
			}
			if(varFrmSingleShipping.txtZipcode.value ==''){
				window.alert("Please provide your zipcode.")
				return;
			}
			if(varFrmSingleShipping.txtPhone.value ==''){
				window.alert("Please provide your phone number.")
				return;
			}
			varFrmSingleShipping.submit();
		} else {
			varFrmSingleShipping.submit();
		}
	}
}

function ShowEditShippingCheck() {
	var varFrmSingleShipping =document.getElementById('SingleEditShippingForm');
	if(varFrmSingleShipping.txtFirstname.value ==''){
		window.alert("Please provide your first name.")
		varFrmSingleShipping.txtFirstname.focus();
		return;
	}
	if(varFrmSingleShipping.txtLastname.value ==''){
		window.alert("Please provide your last name.")
		varFrmSingleShipping.txtLastname.focus();
		return;
	}
	if(varFrmSingleShipping.txtAddress1.value ==''){
		window.alert("Please provide your address.")
		varFrmSingleShipping.txtAddress1.focus();
		return;
	}
	if(varFrmSingleShipping.txtCity.value ==''){
		window.alert("Please provide your city.")
		varFrmSingleShipping.txtCity.focus();
		return;
	}
	if((varFrmSingleShipping.txtZipcode.value =='')||(varFrmSingleShipping.txtZipcode.value.length != 5)){
		window.alert("Please provide your 5-digit zipcode.")
		varFrmSingleShipping.txtZipcode.focus();
		return;
	}
	if(varFrmSingleShipping.txtPhone.value ==''){
		window.alert("Please provide your phone number.")
		varFrmSingleShipping.txtPhone.focus();
		return;
	}
	varFrmSingleShipping.submit();
}

// ' ~~~~ Multiple Shipping Cart specific scripts ~~~~ 

function AddAddress(){
	window.alert("AddAddress: Not Implemented Yet")
}
function RemoveLineItem(intLineItem){
	window.alert("RemoveLineItem: Not Implemented Yet")
}
function UpdateProductQuantities(intOrderDetailID){
	window.alert("UpdateProductQuantities: Not Implemented Yet");
}
function SubmitForm(){
	document.frmShipping.submit();
}
function GoToShippingMethods(){
	document.frmShipping.elements['goto'].value="ShowShippingMethods"
	document.frmShipping.submit();
}
function SplitShippingItem(intOrderDetailID){
	document.frmShipping.elements['UpdateShippingAction'].value="SplitShippingItem_" + intOrderDetailID;
	SubmitForm();
}

// ' ~~~~ "ShowPayment" specific scripts ~~~~ 

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isArray(a) {
	alert(a.constructor);
    return isObject(a) && a.constructor == '[NodeList]';
}

function validateNewAddress(AddressToValidate) {
	if(AddressToValidate.txtFirstname.value ==''){
		window.alert("Please provide your first name.")
		return(false);
	}
	if(AddressToValidate.txtLastname.value ==''){
		window.alert("Please provide your last name.")
		return(false);
	}
	if(AddressToValidate.txtAddress1.value ==''){
		window.alert("Please provide your address.")
		return(false);
	}
	if(AddressToValidate.txtCity.value ==''){
		window.alert("Please provide your city.")
		return(false);
	}
	if(AddressToValidate.txtZipcode.value ==''){
		window.alert("Please provide your zipcode.")
		return(false);
	}
	if(AddressToValidate.txtPhone.value ==''){
		window.alert("Please provide your phone number.")
		return(false);
	}
	return(true);
}

function validateNewCard(CardToValidate) {
	if(CardToValidate.CCType.value ==''){
		window.alert("Please choose a credit card Type.")
		CardToValidate.CCType.focus();
		return(false);
	}
	if(CardToValidate.CCNumber.value ==''){
		window.alert("Please enter a valid credit card number.")
		CardToValidate.CCNumber.focus();		
		return(false);
	}
	if(CardToValidate.CCMonth.value ==''){
		window.alert("Please select an expire month for your credit card.")
		CardToValidate.CCMonth.focus();
		return(false);
	}
	if(CardToValidate.CCYear.value ==''){
		window.alert("Please select an expire year for your credit card.")
		CardToValidate.CCYear.focus();
		return(false);
	}
	//note: specific for visa and mastercard, length of 3 should be good
	if((CardToValidate.CCCode.value =='')||(CardToValidate.CCCode.value.length !=3)||(!Number(CardToValidate.CCCode.value))){
		window.alert("Please enter a valid 3-digit Credit Card CVV2 code.")
		CardToValidate.CCCode.focus();
		return(false);
	}	
	
	return(true);
}

function PaymentFormCheck() {											//  Function to validate the Payment Address fieldset on fb_ShowShipping
	varPaymentForm = document.getElementById('PaymentForm');
	
	var FormValid = true;
	var AddressSelected = false;
	var PaymentSelected = false;
	
	if (varPaymentForm.BillToAddress.length != null) {						//  Check to see if there is more than one address to select...;
		for (i = 0; i < varPaymentForm.BillToAddress.length; i++) {		//  ...If there is more than one address, then make sure one of them is selected
			if (varPaymentForm.BillToAddress[i].checked) {
				AddressSelected = true;
				if (varPaymentForm.BillToAddress[i].value == 0) {
					FormValid = validateNewAddress(varPaymentForm);
				}
			}
		}
				
	} else if (varPaymentForm.BillToAddress.checked) {					// Checks to see that the ONLY payment address option (Enter New Address) is selected
		AddressSelected = true;
		FormValid = validateNewAddress(varPaymentForm);					// Validates new Address information	
	}
	if (!AddressSelected) {
		alert("Please Select a Payment Address");
		FormValid = false;
	}
	
	if (FormValid)
	{
		if  (varPaymentForm.PaymentType.length != null) {
			for (j = 0; j < varPaymentForm.PaymentType.length; j++) {
				if (varPaymentForm.PaymentType[j].checked) {
					PaymentSelected = true;
					if (varPaymentForm.PaymentType[j].value == 'CreditCard') {
						FormValid = validateNewCard(varPaymentForm);
					} else if (varPaymentForm.PaymentType[j].value == 'PO') {
						if (varPaymentForm.PONumber.value == '') {
							alert('Please enter a Pre-Approved Account Number');
							FormValid = false;
						}
					}
				}
			}
		} else if (varPaymentForm.PaymentType.checked) {
			PaymentSelected = true;
			FormValid = validateNewCard(varPaymentForm);					// Validates new Address information	
		}

		if (!PaymentSelected) {
			alert("Please Select a Payment Method");
			FormValid = false;
		}
	}
	

		
	if (FormValid) {
		varPaymentForm.submit();
	}
}

function Approved() {
	varPaymentForm = document.getElementById('PaymentForm');
	
	var FormValid = true;
	var AddressSelected = false;
	var PaymentSelected = false;
	
	if (varPaymentForm.BillToAddress.length != null) {						//  Check to see if there is more than one address to select...;
		for (i = 0; i < varPaymentForm.BillToAddress.length; i++) {		//  ...If there is more than one address, then make sure one of them is selected
			if (varPaymentForm.BillToAddress[i].checked) {
				AddressSelected = true;
				/* if (varPaymentForm.BillToAddress[i].value == 0) {
					FormValid = validateNewAddress(varPaymentForm);
				} */
			}
		}
				
	} else if (varPaymentForm.BillToAddress.checked) {					// Checks to see that the ONLY payment address option (Enter New Address) is selected
		AddressSelected = true;
		//FormValid = validateNewAddress(varPaymentForm);					// Validates new Address information	
	}
	
	
	if (!AddressSelected) {
		alert("Please Select a Payment Address");
		FormValid = false;
	}

	if (FormValid) {
		varPaymentForm.submit();
	}
}

function Manager() {
	varPaymentForm = document.getElementById('PaymentForm');
	
	var FormValid = true;
	var AddressSelected = false;
	var PaymentSelected = false;
	
	if (varPaymentForm.BillToAddress.length != null) {						//  Check to see if there is more than one address to select...;
		for (i = 0; i < varPaymentForm.BillToAddress.length; i++) {		//  ...If there is more than one address, then make sure one of them is selected
			if (varPaymentForm.BillToAddress[i].checked) {
				AddressSelected = true;
				/* if (varPaymentForm.BillToAddress[i].value == 0) {
					FormValid = validateNewAddress(varPaymentForm);
				} */
			}
		}
				
	} else if (varPaymentForm.BillToAddress.checked) {					// Checks to see that the ONLY payment address option (Enter New Address) is selected
		AddressSelected = true;
		//FormValid = validateNewAddress(varPaymentForm);					// Validates new Address information	
	}
	if (varPaymentForm.CompanyCode.value == '') {
		alert('Please enter a Company Code.');
		FormValid = false;
	}
	if (varPaymentForm.DeptCode.value == '') {
		alert('Please enter a Department Code');
		FormValid = false;
	}
	if (varPaymentForm.ExpenseCatCode.value == '') {
		alert('Please enter an Expense Category Code');
		FormValid = false;
	}
	
	if (!AddressSelected) {
		alert("Please Select a Payment Address");
		FormValid = false;
	}

	if (FormValid) {
		varPaymentForm.submit();
	}
}

function ClearCC(){
	document.varPaymentForm.ClearCreditCard.value='1';
	PaymentFormCheck();
}


//	' ~~~~ Open and close window (like the audio player window) ~~~~ 
//	' ~~~~ var newWindow stores the object specifically used for the Audio Player ~~~~


var newWindow;
function open_Window(name, winName, winDressing, winWidth, winHeight) 
{
	if (winWidth == null) winWidth = 500;
	if (winHeight == null) winHeight = 350;
	newWindow = window.open(name, winName, 'width=' + winWidth + ',height=' + winHeight + ',' + winDressing);
	newWindow.focus();
}

function closeNewWindow() {
 
  if (newWindow  && newWindow.open && !newWindow.closed)
  {
	   newWindow.close();
  }

}

function open_Centered_Window(fileLocation, winName, winDressing, winWidth, winHeight)
{
	if (winWidth == null) winWidth = 500;
	if (winHeight == null) winHeight = 350;
	var winLeft = (screen.width - winWidth) / 2;
	var winTop = (screen.height - winHeight) / 2;
	var features = 'height='+winHeight+',width='+winWidth+',top='+winTop+',left='+winLeft+','+winDressing;
	
	win = window.open(fileLocation, winName, features)
	
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//	' ~~~~ Preload Images for Navigation Rollovers, etc.

function preLoadImages() {
	var args = preLoadImages.arguments;
	document.imageArray = new Array(args.length);
	for(var i=0; i<args.length; i++) {
		document.imageArray[i] = new Image;
		document.imageArray[i].src = args[i];
	}
}

//	' ~~~~ Simple MouseOver/MouseOut for Navigation Rollovers, etc.

function ImageSwitch (src) {
	pattern = ".jpg";
	NewSrc = src.replace(pattern,"DN"+pattern);
	//alert("New: " + NewSrc);
	return NewSrc;
}

function ImageReplace (src) {
	pattern = "DN.";
	NewSrc = src.replace(pattern,".");
	//alert("New: " + NewSrc);
	return NewSrc;
}