// JScript source code
//--- functions for search field events
function clearItemNum(theField)
{
	if (theField.value == 'Item Number/NSN #')
		theField.value = ''
}

function resetItemNum(theField)
{
	if (theField.value.length == 0)
		theField.value = 'Item Number/NSN #'
}
function clearKeyword(theField)
{
	if (theField.value == 'Keyword(s)')
		theField.value = ''
}

function resetKeyword(theField)
{
	if (theField.value.length == 0)
		theField.value = 'Keyword(s)'
}

//--- Validate Login Form
function ValidLoginForm(theForm)
{
	re = /\s/g; 
    if (theForm.LoginID.value.replace(re,'').length == 0) {alert('User Name is a required field'); theForm.LoginID.focus(); return false}
    if (theForm.Password.value.replace(re,'').length == 0) {alert('Password is a required field'); theForm.Password.focus(); return false}
    return true  
}

//--- Validate Forgotten Password Form
function ValidateForgottenPassword(theForm)
{
	re = /\s/g;
	if (!isValidEmail(theForm.custEmail.value)) { alert('Email is a required field and must be a valid email'); theForm.custEmail.focus(); return false}
    return true  
}

//-- Validate My Account Order Status Search Form
function ValidateOrderStatusSearch(theForm)
{
	re = /\s/g;
	if (theForm.OrderID.value.replace(re,'').length == 0) {alert('Order Number is a required field'); theForm.OrderID.focus(); return false}
	if (theForm.Srch_BillName3.value.replace(re,'').length == 0) {alert('Last Name is a required field'); theForm.Srch_BillName3.focus(); return false}
	if (theForm.Srch_BillPostalCode.value.replace(re,'').length == 0) {alert('Postal Code is a required field'); theForm.Srch_BillPostalCode.focus(); return false}
	return true
}

//--- My Account - change current address book selection
function changeAddress(theField)
{
	ptr = 0;
	for (i = 0; i < ptrArray.length; i++)
	{
		val1 = parseInt(ptrArray[i]);
		val2 = parseInt(theField.options[theField.selectedIndex].value)
		if (val1 == val2)
			ptr = i;
	}
	theField.form.Address.value = addrArray[ptr];
}

//--- My Account - Edit/Delete Address Book Entry
function ValidateEditAddressBook(theForm)
{
	if (theForm.Action.value == 'EditAddressBook')
	{
		if (theForm.ID.selectedIndex == -1)
		{
			alert("An Address Book Entry must be selected");
			return false
		}
		else
			return true
	}
	else if (theForm.Action.value == 'DeleteAddressBook')
	{
		if (theForm.ID.selectedIndex == -1)
			alert("An Address Book Entry must be selected")
		else
			return (confirm('Delete Address Book Entry ' + theForm.ID.options[theForm.ID.selectedIndex].text + '?'))
	}
	return false
}

//--- My Account - Edit/Delete Shopping Lists
function ValidateEditShoppingList(theForm)
{
	if (theForm.Action.value == 'EditShoppingList' || theForm.Action.value == 'FillCartForm')
	{
		if (theForm.ID.selectedIndex == -1)
		{
			alert("A Shopping List Entry must be selected")
			return false
		}
		else
			return true
	}
	else if (theForm.Action.value == 'DeleteShoppingList')
	{
		if (theForm.ID.selectedIndex == -1)
		{
			alert("A Shopping List Entry must be selected")
			return false
		}
		else
			return (confirm('Delete Shopping List ' + document.EditShoppingList.ID.options[document.EditShoppingList.ID.selectedIndex].text + '?'))
	}
	return false								
}

//--- Address Book edit
function ValidateEditAddressBookEntry(theForm)
{
	re = /\s/g;
	if (theForm.ProfileName.value.replace(re,'').length == 0) {alert('Address Book Name is a required field'); theForm.ProfileName.focus(); return false} 
    if (theForm.Name1.value.replace(re,'').length == 0) {alert('First Name is a required field'); theForm.Name1.focus(); return false}
    if (theForm.Name3.value.replace(re,'').length == 0) {alert('Last Name is a required field'); theForm.Name3.focus(); return false}
    if (theForm.Address1.value.replace(re,'').length == 0) {alert('Address 1 is a required field'); theForm.Address1.focus(); return false}
    if (theForm.City.value.replace(re,'').length == 0) {alert('City is a required field'); theForm.City.focus(); return false}
    if (theForm.State.selectedIndex < 1) {alert('State is a required field'); theForm.State.focus(); return false}
    if (theForm.PostalCode.value.replace(re,'').length == 0) {alert('Zip Code is a required field'); theForm.PostalCode.focus(); return false}
    if (theForm.Country.selectedIndex < 1) {alert('Country is a required field'); theForm.Country.focus(); return false}
    if (theForm.Phone.value.replace(re,'').length == 0) {alert('Phone is a required field'); theForm.Phone.focus(); return false}
    if (!isValidEmail(theForm.Email.value)) {alert('Email is a required field and must be a valid email'); theForm.Email.focus(); return false}
    if (theForm.Email.value != theForm.ConfirmEmail.value) {alert('Email and confirming email do not match'); theForm.Email.focus(); return false}
	return true
}

//--- Function for Checkout ship info
function UseAddressBookForShipTo(theField)
{
	for (i = 0; i < theField.form.ShipAddressSelection.length; i++)
	{
		if (theField.form.ShipAddressSelection[i].value == 'AddressBook')
			theField.form.ShipAddressSelection[i].checked = true
	}
}

function UseNewShipTo(theField)
{
	for (i = 0; i < theField.form.elements.length; i++)
	{
		if (theField.form.elements[i].name == 'ShipAddressSelection' && theField.form.elements[i].type == 'radio')
		{
			for (j = 0; j < theField.form.ShipAddressSelection.length; j++)
			{
				if (theField.form.ShipAddressSelection[j].value == 'New')
					theField.form.ShipAddressSelection[j].checked = true
			}
		}
	}
}

function FillShipToFields(theField)
{
	theField.form.ShipCompany.value = document.HiddenAddress.Company.value;
	theField.form.ShipName1.value = document.HiddenAddress.Name1.value;
	theField.form.ShipName2.value = document.HiddenAddress.Name2.value;
	theField.form.ShipName3.value = document.HiddenAddress.Name3.value;
	theField.form.ShipAddress1.value = document.HiddenAddress.Address1.value;
	theField.form.ShipAddress2.value = document.HiddenAddress.Address2.value;
	theField.form.ShipCity.value = document.HiddenAddress.City.value;
	theField.form.ShipPostalCode.value = document.HiddenAddress.PostalCode.value;
	theField.form.ShipPhone.value = document.HiddenAddress.Phone.value;
	theField.form.ShipFax.value = document.HiddenAddress.Fax.value;
	theField.form.ShipEmail.value = document.HiddenAddress.Email.value;
	for (i = 0; i < theField.form.ShipState.options.length; i++)
	{
		if (theField.form.ShipState.options[i].value == document.HiddenAddress.State.value)
			theField.form.ShipState.selectedIndex = i;
	}
	for (i = 0; i < theField.form.ShipCountry.options.length; i++)
	{
		if (theField.form.ShipCountry.options[i].value == document.HiddenAddress.Country.value)
			theField.form.ShipCountry.selectedIndex = i;
	}
	UseNewShipTo(theField)
}

function ValidateSetShipInfo(theForm)
{
    needToValidateAddressInfo = false;
    
    if (theForm.ShipAddressSelection.length > 0)
        for (i = 0; i < theForm.ShipAddressSelection.length; i++)
        {
            if (theForm.ShipAddressSelection[i].checked && theForm.ShipAddressSelection[i].value == 'New')
                needToValidateAddressInfo = true   
        }
    else
        needToValidateAddressInfo = true
    if (needToValidateAddressInfo)
    {
    	re = /\s/g;
    	if (theForm.ShipName1.value.replace(re,'').length == 0) {alert('First Name is a required field'); theForm.ShipName1.focus(); return false}
    	if (theForm.ShipName3.value.replace(re,'').length == 0) {alert('Last Name is a required field'); theForm.ShipName3.focus(); return false}
    	if (theForm.ShipAddress1.value.replace(re,'').length == 0) {alert('Address 1 is a required field'); theForm.ShipAddress1.focus(); return false}
    	if (theForm.ShipCity.value.replace(re,'').length == 0) {alert('City is a required field'); theForm.ShipCity.focus(); return false}
    	if (theForm.ShipState.selectedIndex < 1) {alert('State is a required field'); theForm.ShipState.focus(); return false}
    	if (theForm.ShipPostalCode.value.replace(re,'').length == 0) {alert('Zip Code is a required field'); theForm.ShipPostalCode.focus(); return false}
    	if (theForm.ShipCountry.selectedIndex < 1) {alert('Country is a required field'); theForm.ShipCountry.focus(); return false}
    	if (theForm.ShipPhone.value.replace(re,'').length == 0) {alert('Phone is a required field'); theForm.ShipPhone.focus(); return false}
    	if (!isValidEmail(theForm.ShipEmail.value)) {alert('Email is a required field and must be a valid email'); theForm.ShipEmail.focus(); return false}
    	if (theForm.AddToAddressBook.type == 'checkbox')
    	    if (theForm.AddToAddressBook.checked && theForm.ProfileName.value.replace(re,'').length == 0) {alert('Address Book Name is a required field'); theForm.ProfileName.focus(); return false}
    }
    return true
}

//--- Functions for checkout billing info
function UseAddressBookForBillTo(theField)
{
	for (i = 0; i < theField.form.BillAddressSelection.length; i++)
	{
		if (theField.form.BillAddressSelection[i].value == 'AddressBook')
			theField.form.BillAddressSelection[i].checked = true
	}
}

function UseNewBillTo(theField)
{
	for (i = 0; i < document.BillForm.elements.length; i++)
	{
		if (theField.form.elements[i].name == 'BillAddressSelection' && theField.form.elements[i].type == 'radio')
		{
			for (j = 0; j < theField.form.BillAddressSelection.length; j++)
			{
				if (theField.form.BillAddressSelection[j].value == 'New')
					theField.form.BillAddressSelection[j].checked = true
			}
		}
	}
}
function FillBillToFields(theField)
{
	theField.form.BillCompany.value = document.HiddenAddress.Company.value;
	theField.form.BillName1.value = document.HiddenAddress.Name1.value;
	theField.form.BillName2.value = document.HiddenAddress.Name2.value;
	theField.form.BillName3.value = document.HiddenAddress.Name3.value;
	theField.form.BillAddress1.value = document.HiddenAddress.Address1.value;
	theField.form.BillAddress2.value = document.HiddenAddress.Address2.value;
	theField.form.BillCity.value = document.HiddenAddress.City.value;
	theField.form.BillPostalCode.value = document.HiddenAddress.PostalCode.value;
	theField.form.BillPhone.value = document.HiddenAddress.Phone.value;
	theField.form.BillFax.value = document.HiddenAddress.Fax.value;
	theField.form.BillEmail.value = document.HiddenAddress.Email.value;
	for (i = 0; i < theField.form.BillState.options.length; i++)
	{
		if (theField.form.BillState.options[i].value == document.HiddenAddress.State.value)
			theField.form.BillState.selectedIndex = i;
	}
	for (i = 0; i < theField.form.BillCountry.options.length; i++)
	{
		if (theField.form.BillCountry.options[i].value == document.HiddenAddress.Country.value)
			theField.form.BillCountry.selectedIndex = i;
	}
	UseNewBillTo(theField)
}

function ValidateSetBillInfo(theForm)
{
	if (!ValidatePaymentOptions(theForm))
		return false

    needToValidateAddressInfo = false;
    
    if (theForm.BillAddressSelection.length > 0)
        for (i = 0; i < theForm.BillAddressSelection.length; i++)
        {
            if (theForm.BillAddressSelection[i].checked && theForm.BillAddressSelection[i].value == 'New')
                needToValidateAddressInfo = true   
        }
    else
        needToValidateAddressInfo = true
    if (needToValidateAddressInfo)
    {
    	re = /\s/g;
    	if (theForm.BillName1.value.replace(re,'').length == 0) {alert('First Name is a required field'); theForm.BillName1.focus(); return false}
    	if (theForm.BillName3.value.replace(re,'').length == 0) {alert('Last Name is a required field'); theForm.BillName3.focus(); return false}
    	if (theForm.BillAddress1.value.replace(re,'').length == 0) {alert('Address 1 is a required field'); theForm.BillAddress1.focus(); return false}
    	if (theForm.BillCity.value.replace(re,'').length == 0) {alert('City is a required field'); theForm.BillCity.focus(); return false}
    	if (theForm.BillState.selectedIndex < 1) {alert('State is a required field'); theForm.BillState.focus(); return false}
    	if (theForm.BillPostalCode.value.replace(re,'').length == 0) {alert('Zip Code is a required field'); theForm.BillPostalCode.focus(); return false}
    	if (theForm.BillCountry.selectedIndex < 1) {alert('Country is a required field'); theForm.BillCountry.focus(); return false}
    	if (theForm.BillPhone.value.replace(re,'').length == 0) {alert('Phone is a required field'); theForm.BillPhone.focus(); return false}
    	if (!isValidEmail(theForm.BillEmail.value)) {alert('Email is a required field and must be a valid email'); theForm.BillEmail.focus(); return false}
    	if (theForm.AddToAddressBook.type == 'checkbox')
    	    if (theForm.AddToAddressBook.checked && theForm.ProfileName.value.replace(re,'').length == 0) {alert('Address Book Name is a required field'); theForm.ProfileName.focus(); return false}
    }
    return true
}

function ValidateShoppingListForm(theForm)
{
   	re = /\s/g;
   	confirmDelete = false;
   	confirmList = '';
   	
   	if (theForm.ListName.value.replace(re,'').length == 0) {alert('List Name is a required field'); theForm.ListName.focus(); return false}
   	
   	for (i = 0; i < theForm.elements.length; i++)
   	{
   	    if (theForm.elements[i].name.indexOf('DefaultQty_') == 0)
		    if (theForm.elements[i].value.replace(re,'').length > 0 && ((theForm.elements[i].value.indexOf(",") != -1)
			     || isNaN(parseInt(theForm.elements[i].value))
			     || theForm.elements[i].value != parseInt(theForm.elements[i].value)))
			{alert("Default qty field must contain an integer. It should not contain commas.");	theForm.elements[i].focus(); return false;}
		if (theForm.elements[i].name.indexOf('Delete_') == 0)
		{
		    if (theForm.elements[i].checked)
		    {
		        confirmDelete = true;
		        confirmList += eval('theForm.ProdID_' + theForm.elements[i].name.substring(7) + '.value') + '\n'
		    }
		}
	}
	if (confirmDelete)
	    return confirm('Delete the following items from the list?\n' + confirmList)
    return true
}

function ValidateFillFromList(theForm)
{
   	re = /\s/g;
   	for (i = 0; i < theForm.elements.length; i++)
   	{
   	    if (theForm.elements[i].name.indexOf('Quantity_') == 0)
		    if (theForm.elements[i].value.replace(re,'').length > 0 && ((theForm.elements[i].value.indexOf(",") != -1)
			     || isNaN(parseInt(theForm.elements[i].value))
			     || theForm.elements[i].value != parseInt(theForm.elements[i].value)))
			{alert("Quantity field must contain an integer. It should not contain commas."); theForm.elements[i].focus(); return false;}
    }
    return true
}

function ValidateBrandFilter(theForm)
{
    if (theForm.SrchPath.selectedIndex < 1) {alert("Please select a brand"); theForm.SearchPath.focus();return false}
    return true
}

function ValidateCartButton(theForm)
{
    var isNav4, isNav6, isIE4;

    if (navigator.appVersion.charAt(0) == "4")
    {
        if (navigator.appName.indexOf("Explorer") >= 0)
        {
            isIE4 = true;
        }
        else
        {
            isNav4 = true;
        }
    }
    else if (navigator.appVersion.charAt(0) > "4")
    {
        isNav6 = true;
    }

    if (isNav6)
    {
        var styleObject = document.getElementById("SubmitButton" );
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ "display" ] = "none";
        }
        styleObject = document.getElementById("Processing");
        if (styleObject != null)
        {
            styleObject = styleObject.style;
            styleObject[ "display" ] = "inline";
        }
    }
    else if (isNav4)
    {
        document["SubmitButton"]["display"] = "none";
        document["Processing"]["display"] = "inline";
    }
    else if (isIE4)
    {
         document.all["SubmitButton"].style["display"] = "none";
         document.all["Processing"].style["display"] = "inline";
    }
     return true
}

