﻿// JScript File
function fnUpdateQuantityOnKeyPress(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27))
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function fnValidateOrder(oForm)
{
    if (!fnValidateShippingFirstName(oForm))
    {
        return false;
    }
    else if (!fnValidateShippingLastName(oForm))
    {
        return false;
    }
    else if(!fnValidateEmail(oForm))
    {
        return false;
    }
    //else if(!fnValidatePhone(oForm))
    //{
    //    return false;
    //}
    else if (!fnValidateShippingAddress1(oForm))
    {
        return false;
    }
    else if (!fnValidateShippingCity(oForm))
    {
        return false;
    }            
    else if (!fnValidateShippingZip(oForm))
    {
        return false;
    }
    /*    
    else if (!fnValidateBillingFirstName(oForm))
    {
        return false;
    }
    else if (!fnValidateBillingLastName(oForm))
    {
        return false;
    }
    else if (!fnValidateBillingAddress1(oForm))
    {
        return false;
    }
    else if (!fnValidateBillingCity(oForm))
    {
        return false;
    }            
    else if (!fnValidateBillingZip(oForm))
    {
        return false;
    }        
    else if (!fnValidateCardType(oForm))
    {
        return false;
    }            
    else if (!fnValidateCreditCardNumber(oForm))
    {
        return false;
    }           
    else if (!fnValidateCVV2(oForm))
    {
        return false;
    }
    */
    return true;
}

function fnValidateShippingFirstName(oForm)
{
    if (fnIsBlank(Trim(oForm.txtShippingFirstName.value)))
    {
        alert("Please enter the Shipping First Name.");
        oForm.txtShippingFirstName.value = "";
        oForm.txtShippingFirstName.focus();
        return false;
    }
    oForm.txtShippingFirstName.value = Trim(oForm.txtShippingFirstName.value);
    return true;
}

function fnValidateShippingLastName(oForm)
{
    if (fnIsBlank(Trim(oForm.txtShippingLastName.value)))
    {
        alert("Please enter the Shipping Last Name.");
        oForm.txtShippingLastName.value = "";
        oForm.txtShippingLastName.focus();
        return false;
    }
    oForm.txtShippingLastName.value = Trim(oForm.txtShippingLastName.value);
    return true;
}

function fnValidatePhone(oForm)
{
    if (fnIsBlank(Trim(oForm.txtPhone.value)))
    {
        alert("Please enter a Phone Number.");
        oForm.txtPhone.value = "";
        oForm.txtPhone.focus();
        return false;
    }
    else if (Trim(oForm.txtPhone.value.replace(/-/g, "")).length < 10)
    {
        alert("Please enter a 10-digit Phone Number.");
        oForm.txtPhone.focus();
        return false;  
    }    
    else if (isNaN(Trim(oForm.txtPhone.value.replace(/-/g, ""))))
    {
        alert("Please enter only numbers with or without dashes for the Phone Number.");
        oForm.txtPhone.focus();
        return false;
    }    
    oForm.txtPhone.value = Trim(oForm.txtPhone.value);
    return true;    
}

function fnValidateShippingAddress1(oForm)
{
    if (fnIsBlank(Trim(oForm.txtShippingAddress1.value)))
    {
        alert("Please enter the Shipping Address.");
        oForm.txtShippingAddress1.value = "";
        oForm.txtShippingAddress1.focus();
        return false;
    }
    oForm.txtShippingAddress1.value = Trim(oForm.txtShippingAddress1.value);
    return true;
}

function fnValidateShippingCity(oForm)
{
    if (fnIsBlank(Trim(oForm.txtShippingCity.value)))
    {
        alert("Please enter the Shipping City.");
        oForm.txtShippingCity.value = "";
        oForm.txtShippingCity.focus();
        return false;
    }
    oForm.txtShippingCity.value = Trim(oForm.txtShippingCity.value);
    return true;    
}

function fnValidateShippingZip(oForm)
{
    if (fnIsBlank(Trim(oForm.txtShippingZip.value)))
    {
        alert("Please enter the Shipping Zip.");
        oForm.txtShippingZip.value = "";
        oForm.txtShippingZip.focus();
        return false;
    }
    else if (Trim(oForm.txtShippingZip.value).length < 5)
    {
        alert("Please enter a 5-digit Shipping Zip.");
        oForm.txtShippingZip.focus();
        return false;  
    }    
    else if (isNaN(Trim(oForm.txtShippingZip.value).replace("-", "")))
    {
        alert("Please enter only numbers for the Shipping Zip.");
        oForm.txtShippingZip.focus();
        return false;
    }    
    oForm.txtShippingZip.value = Trim(oForm.txtShippingZip.value);
    return true;    
}

function fnValidateBillingFirstName(oForm)
{
    if (fnIsBlank(Trim(oForm.txtBillingFirstName.value)))
    {
        alert("Please enter the Billing First Name.");
        oForm.txtBillingFirstName.value = "";
        oForm.txtBillingFirstName.focus();
        return false;
    }
    oForm.txtBillingFirstName.value = Trim(oForm.txtBillingFirstName.value);
    return true;
}

function fnValidateBillingLastName(oForm)
{
    if (fnIsBlank(Trim(oForm.txtBillingLastName.value)))
    {
        alert("Please enter the Billing Last Name.");
        oForm.txtBillingLastName.value = "";
        oForm.txtBillingLastName.focus();
        return false;
    }
    oForm.txtBillingLastName.value = Trim(oForm.txtBillingLastName.value);
    return true;
}

function fnValidateBillingAddress1(oForm)
{
    if (fnIsBlank(Trim(oForm.txtBillingAddress1.value)))
    {
        alert("Please enter the Billing Address.");
        oForm.txtBillingAddress1.value = "";
        oForm.txtBillingAddress1.focus();
        return false;
    }
    oForm.txtBillingAddress1.value = Trim(oForm.txtBillingAddress1.value);
    return true;
}

function fnValidateBillingCity(oForm)
{
    if (fnIsBlank(Trim(oForm.txtBillingCity.value)))
    {
        alert("Please enter the Billing City.");
        oForm.txtBillingCity.value = "";
        oForm.txtBillingCity.focus();
        return false;
    }
    oForm.txtBillingCity.value = Trim(oForm.txtBillingCity.value);
    return true;    
}

function fnValidateBillingZip(oForm)
{
    if (fnIsBlank(Trim(oForm.txtBillingZip.value)))
    {
        alert("Please enter the Billing Zip.");
        oForm.txtBillingZip.value = "";
        oForm.txtBillingZip.focus();
        return false;
    }
    else if (Trim(oForm.txtBillingZip.value).length < 5)
    {
        alert("Please enter a 5-digit Billing Zip.");
        oForm.txtBillingZip.focus();
        return false;  
    }    
    else if (isNaN(Trim(oForm.txtBillingZip.value)))
    {
        alert("Please enter only numbers for the Billing Zip.");
        oForm.txtBillingZip.focus();
        return false;
    }        
    oForm.txtBillingZip.value = Trim(oForm.txtBillingZip.value);
    return true;    
}

function fnValidateCardType(oForm)
{
    if (oForm.lstCardTypes.value == "-1")
    {
        alert("Please select a Card Type.");
        oForm.lstCardTypes.focus();
        return false;
    }
    return true;    
}

function fnValidateCreditCardNumber(oForm)
{
    if (fnIsBlank(Trim(oForm.txtCreditCardNumber.value)))
    {
        alert("Please enter a 16-digit credit card number.");
        oForm.txtCreditCardNumber.value = "";
        oForm.txtCreditCardNumber.focus();
        return false;
    }
    else if (Trim(oForm.txtCreditCardNumber.value).length < 16)
    {
        alert("Please enter a 16-digit credit card number.");
        oForm.txtCreditCardNumber.focus();
        return false;  
    }
    else if (isNaN(Trim(oForm.txtCreditCardNumber.value)))
    {
        alert("Please enter only numbers for the credit card number.");
        oForm.txtCreditCardNumber.focus();
        return false;  
    }
    oForm.txtCreditCardNumber.value = Trim(oForm.txtCreditCardNumber.value);
    return true;
}

function fnValidateCVV2(oForm)
{
    if (fnIsBlank(Trim(oForm.txtCVV2.value)))
    {
        alert("Please enter a 3-digit card verification number.");
        oForm.txtCVV2.focus();
        return false;
    }
    else if (Trim(oForm.txtCVV2.value).length < 3)
    {
        alert("Please enter a 3-digit card verification number.");
        oForm.txtCVV2.focus();
        return false;  
    }
    else if (isNaN(Trim(oForm.txtCVV2.value)))
    {
        alert("Please enter only numbers for the card verification number.");
        oForm.txtCVV2.focus();
        return false;  
    }
    oForm.txtCVV2.value = Trim(oForm.txtCVV2.value);    
    return true;
}



function fnValidateHolidayCollection(sender, args)
{
    //var cMissingItems = "";
    var bReturn = true;

    //loop through all dropdowns, and if they equal -1, then add to error list to be displayed

    for (var i = 0; i<document.forms[0].elements.length; i++) {
        if (document.forms[0].elements[i].tagName.toUpperCase() == "SELECT") {
            if (document.forms[0].elements[i].selectedIndex == "0") {
                //cMissingItems += "   *" + document.forms[0].elements[i].options[document.forms[0].elements[i].selectedIndex].innerText + "\n";
                alert("Please select from ALL dropdowns.")
                bReturn = false;
                break;
            }
        }
    }
    /*   
    if (cMissingItems != "")
    {
        alert("The following required selections are missing:\n\n" + cMissingItems);
        return false;
    }
    
    return true;    
    */
    return bReturn;
}

function fnSubmitPersonalization(oLink)
{
    document.forms[0].hidLinkPersonalization.value = oLink.toString();
    document.forms[0].submit();
}

function fnCopyBilling()
{
    document.forms[0].txtShippingFirstName.value = document.forms[0].txtBillingFirstName.value;
    document.forms[0].txtShippingLastName.value = document.forms[0].txtBillingLastName.value;
    document.forms[0].txtShippingAddress1.value = document.forms[0].txtBillingAddress1.value;
    document.forms[0].txtShippingAddress2.value = document.forms[0].txtBillingAddress2.value;
    document.forms[0].txtShippingCity.value = document.forms[0].txtBillingCity.value;
    document.forms[0].lstShippingStates.value = document.forms[0].lstBillingStates.value;
    document.forms[0].txtShippingZip.value = document.forms[0].txtBillingZip.value;
}



function fnUpdateTax(oState)
{
    if (oState.value == "OH")
    {
       var iSalesTax = new Number(parseFloat(Trim(tdSubTotal.innerText.substring(1))) * 0.0675);
       
       iSalesTax = iSalesTax.toFixed(2);
       
       var cSalesTax = "$" + iSalesTax;

       var iDecimalLocation = cSalesTax.indexOf(".");

        if (iDecimalLocation > 0)
        {
            if (cSalesTax.substring(iDecimalLocation + 1).length < 2)
            {
                cSalesTax = cSalesTax + "0";
            }
        }
        
        tdSalesTax.innerText = cSalesTax + "    ";// + "&nbsp;&nbsp;&nbsp;";

        var iGrandTotal = new Number(parseFloat(Trim(tdGrandTotal.innerText.substring(1))));
       
        iGrandTotal = parseFloat(iSalesTax) + iGrandTotal;
        
        iGrandTotal = iGrandTotal.toFixed(2);
        
        var cGrandTotal = "$" + iGrandTotal;
       
        tdGrandTotal.innerText = cGrandTotal + "    ";// + "&nbsp;&nbsp;&nbsp;";
    }
    else
    {
        if (Trim(tdSalesTax.innerText) != "$0.00")
        {
            var iSalesTax = new Number(parseFloat(Trim(tdSubTotal.innerText.substring(1))) * 0.0675);

            iSalesTax = iSalesTax.toFixed(2);

            cSalesTax = "$0.00";

            tdSalesTax.innerText = cSalesTax + "    ";// + "&nbsp;&nbsp;&nbsp;";

            var iGrandTotal = new Number(parseFloat(Trim(tdGrandTotal.innerText.substring(1))));
           
            iGrandTotal = iGrandTotal - iSalesTax;
            
            iGrandTotal = iGrandTotal.toFixed(2);
            
            var cGrandTotal = "$" + iGrandTotal;
            
            tdGrandTotal.innerText = cGrandTotal + "    ";// + "&nbsp;&nbsp;&nbsp;";    
        }
    }   
}

