function PageLoad() {
    // Incase back is clicked
    ExtendedCost(document.getElementById("Quantity1"));
    ExtendedCost(document.getElementById("Quantity2"));
    ExtendedCost(document.getElementById("Quantity3"));
    ExtendedCost(document.getElementById("Quantity4"));
    ExtendedCost(document.getElementById("Quantity5"));
    isSameShip();
    // Start at the top
    document.getElementById('Quantity1').focus();
}

function ClearZero(object) {
    if (object.value == 0)
        object.value = "";
}

function ExtendedCost(object) {
    var item = object.id.substring(object.id.length - 1);
    if (isUnsignedInteger(object.value)) {
        object.value = object.value * 1 // remove leading zeros
        var extPrice = document.getElementById('UnitPrice' + item).innerHTML * object.value;
        document.getElementById('ExtCost' + item).innerHTML = '$' + extPrice.toFixed(2);
    }
    else if (object.value.length != 0) { // UnitPrice not a number
        object.value = "0";
        document.getElementById('ExtCost' + item).innerHTML = "$0.00";
    }
    else { // UnitPrice blank
        document.getElementById('ExtCost' + item).innerHTML = "$0.00";
    }
    var total =
        parseFloat(document.getElementById('ExtCost1').innerHTML.substring(1)) +
        parseFloat(document.getElementById('ExtCost2').innerHTML.substring(1)) +
        parseFloat(document.getElementById('ExtCost3').innerHTML.substring(1)) +
        parseFloat(document.getElementById('ExtCost4').innerHTML.substring(1)) +
        parseFloat(document.getElementById('ExtCost5').innerHTML.substring(1));
    document.getElementById('Total').innerHTML = '$' + total.toFixed(2);
}

function CheckZero(object) {
    if (object.value == "")
        object.value = "0";
}

function isUnsignedInteger(s) {
    return (s.toString().search(/^[0-9]+$/) == 0);
}

function validateName(object) {
    //allow only letters and spaces
    string = object.value;
    string1 = '';
    for (x = 0; x < string.length; x++) {
        c = string.charCodeAt(x);
        if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == 32)) string1 += String.fromCharCode(c);
    }
    object.value = string1;
}

function validatePhone(object) {
    //111-111-1111
    string = object.value;
    string1 = '';
    for (x = 0; x < string.length; x++) {
        c = string.charCodeAt(x);
        if (c >= 48 && c <= 57 || (c == 45))
            string1 += String.fromCharCode(c);
    }
    object.value = string1;
}

function validatePhoneFinal(object) {
    //111-111-1111
    string = object.value;
    string1 = '';
    for (x = 0; x < string.length; x++) {
        c = string.charCodeAt(x);
        if (c >= 48 && c <= 57)
            string1 += String.fromCharCode(c);
    }
    if (string1.length == 10) { //insert dashes
        string = string1.slice(0, 3) + '-' + string1.slice(3, 6) + '-' + string1.slice(6, 10);
    }
    else string = '';
    object.value = string;
}

function validateEmail(object) {
    //my.name@domain
    string = object.value;
    string1 = '';
    for (x = 0; x < string.length; x++) {
        c = string.charCodeAt(x);
        if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == 64) || (c >= 48 && c <= 57) || (c == 46) || (c == 45) || (c == 95))
            string1 += String.fromCharCode(c);
    }
    object.value = string1;
}

function validateEmailFinal(object) {
    //my.name@domain
    string = object.value;
    string1 = '';
    var array1 = string.split('@');
    if (array1.length == 2) // has @
    {
        for (x = 0; x < string.length; x++)
        {
            c = string.charCodeAt(x);
            if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == 64) || (c >= 48 && c <= 57) || (c == 46) || (c == 45) || (c == 95))
                string1 += String.fromCharCode(c);
        }
        if (string1.length > 5) string = string1;
    }
    else
        string = '';
    object.value = string;
}

function validateStreet(object) {
    //alert(String.fromCharCode(32)); // space
    //alert(String.fromCharCode(44)); // comma
    //alert(String.fromCharCode(46)); // period
    //alert(String.fromCharCode(48, 49, 50, 51, 52, 53, 54, 55, 56, 57)); // numbers

    //allow only letters, numbers,spaces, commas and periods
    string = object.value;
    string1 = '';
    for (x = 0; x < string.length; x++) {
        c = string.charCodeAt(x);
        if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == 32) || (c >= 48 && c <= 57) || (c == 46) || (c == 44))
            string1 += String.fromCharCode(c);
    }
    object.value = string1;
}

function validateCity(object) {
    string = object.value;
    string1 = '';
    for (x = 0; x < string.length; x++) {
        c = string.charCodeAt(x);
        if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == 32) || (c == 46))
            string1 += String.fromCharCode(c);
    }
    object.value = string1;
}

function validateState(object) {
    string = object.value.toUpperCase();
    switch (string) {
        case 'AL':
        case 'AK':
        case 'AZ':
        case 'AR':
        case 'CA':
        case 'CO':
        case 'CT':
        case 'DE':
        case 'DC':
        case 'FL':
        case 'GA':
        case 'HI':
        case 'ID':
        case 'IL':
        case 'IN':
        case 'IA':
        case 'KS':
        case 'KY':
        case 'LA':
        case 'ME':
        case 'MD':
        case 'MA':
        case 'MI':
        case 'MN':
        case 'MS':
        case 'MO':
        case 'MT':
        case 'NE':
        case 'NV':
        case 'NH':
        case 'NJ':
        case 'NM':
        case 'NY':
        case 'NC':
        case 'ND':
        case 'OH':
        case 'OK':
        case 'OR':
        case 'PA':
        case 'RI':
        case 'SC':
        case 'SD':
        case 'TN':
        case 'TX':
        case 'UT':
        case 'VT':
        case 'VA':
        case 'WA':
        case 'WV':
        case 'WI':
        case 'WY':
            object.value = string;
            break;
        default:
            object.value = '';
            break;
    }
}

function validateZip(object, stage) {
    //exactly 5 numbers
    string = object.value;
    string1 = '';
    for (x = 0; x < string.length; x++) {
        c = string.charCodeAt(x);
        if ((c >= 48 && c <= 57)) string1 += String.fromCharCode(c);
    }
    object.value = string1;
    if (object.value.length != 5 && stage == 2)
        object.value = '';
}

function isSameShip() {
    if (document.getElementById('ShipSame').checked) {
        document.getElementById('ShipStreetLabel').style.visibility = 'hidden';
        document.getElementById('ShipCityLabel').style.visibility = 'hidden';
        document.getElementById('ShipStateLabel').style.visibility = 'hidden';
        document.getElementById('ShipZipLabel').style.visibility = 'hidden';
        document.getElementById('ShipStreet').style.visibility = 'hidden';
        document.getElementById('ShipCity').style.visibility = 'hidden';
        document.getElementById('ShipState').style.visibility = 'hidden';
        document.getElementById('ShipZip').style.visibility = 'hidden';
    }
    else {
        document.getElementById('ShipStreetLabel').style.visibility = 'visible';
        document.getElementById('ShipCityLabel').style.visibility = 'visible';
        document.getElementById('ShipStateLabel').style.visibility = 'visible';
        document.getElementById('ShipZipLabel').style.visibility = 'visible';
        document.getElementById('ShipStreet').style.visibility = 'visible';
        document.getElementById('ShipCity').style.visibility = 'visible';
        document.getElementById('ShipState').style.visibility = 'visible';
        document.getElementById('ShipZip').style.visibility = 'visible';
    }
}

function ValidateOrder(order) {
    with (order) {
        if (document.getElementById('Total').innerText == "$0.00") { alert("Please enter a quantity before proceeding to checkout."); document.getElementById('Quantity1').focus(); return false; }
        else if (ValidateField(FirstName, "Please enter your first name before proceeding to checkout.") == false) { return false; }
        else if (ValidateField(LastName, "Please enter your last name before proceeding to checkout.") == false) { return false; }
        else if (ValidateField(Phone, "Please enter your phone before proceeding to checkout.") == false) { return false; }
        else if (ValidateField(Email, "Please enter your email address before proceeding to checkout.") == false) { return false; }
        else if (ValidateField(BillStreet, "Please enter your street name before proceeding to checkout.") == false) { return false; }
        else if (ValidateField(BillCity, "Please enter your city before proceeding to checkout.") == false) { return false; }
        else if (ValidateField(BillState, "Please enter your state before proceeding to checkout.") == false) { return false; }
        else if (ValidateField(BillZip, "Please enter your zip code before proceeding to checkout.") == false) { return false; }
        else if (document.getElementById('ShipSame').checked == false) {
            if (ValidateField(ShipStreet, "Please enter your shipping street name before proceeding to checkout.") == false) { return false; }
            else if (ValidateField(ShipCity, "Please enter your shipping city before proceeding to checkout.") == false) { return false; }
            else if (ValidateField(ShipState, "Please enter your shipping state before proceeding to checkout.") == false) { return false; }
            else if (ValidateField(ShipZip, "Please enter your shipping zip code before proceeding to checkout.") == false) { return false; }
            else { return true; }
        }
        else { return true; }
    }
}

function ValidateField(field, alertText) {
    with (field) {
        if (value == null || value == "") { alert(alertText); focus(); return false; }
        else { return true }
    }
}
