function onBlurMailingAddress() {
    m = document.getElementById('mailaddress').value;
    if ( document.getElementById('shipcheck').checked == true ) {
        document.getElementById('shipaddress').value = m;
    }   

    if ( document.getElementById('billcheck').checked == true ) {
        document.getElementById('billaddress').value = m;
    }   
}

function onBlurMailingCity() {
    m = document.getElementById('mailcity').value;
    if ( document.getElementById('shipcheck').checked == true ) {
        document.getElementById('shipcity').value = m;
    }   

    if ( document.getElementById('billcheck').checked == true ) {
        document.getElementById('billcity').value = m;
    }   
}

function onBlurMailingState() {
    m = document.getElementById('mailstate').value;
    if ( document.getElementById('shipcheck').checked == true ) {
        document.getElementById('shipstate').value = m;
    }   

    if ( document.getElementById('billcheck').checked == true ) {
        document.getElementById('billstate').value = m;
    }   
}

function onBlurMailingZip() {
    m = document.getElementById('mailzip').value;
    if ( document.getElementById('shipcheck').checked == true ) {
        document.getElementById('shipzip').value = m;
    }   

    if ( document.getElementById('billcheck').checked == true ) {
        document.getElementById('billzip').value = m;
    }   
}

function onClickShippingCheck() {
    checkState = document.getElementById('shipcheck').checked;
    if ( checkState == true ) {
        ma = document.getElementById('mailaddress').value;
        mc = document.getElementById('mailcity').value;
        ms = document.getElementById('mailstate').value;
        mz = document.getElementById('mailzip').value;
        if ( ma == "" && mc == "" && ms == "" && mz == "" ) {
            alert("Fill out the mailing address first.");
            location.hash = "mailing";
            document.getElementById('mailaddress').focus();
            document.getElementById('shipcheck').checked = false;
            return(false);
        }
        document.getElementById('shipaddress').value = ma;
        document.getElementById('shipcity').value = mc;
        document.getElementById('shipstate').value = ms;
        document.getElementById('shipzip').value = mz;
        document.getElementById('shipaddress').disabled = true;
        document.getElementById('shipcity').disabled = true;
        document.getElementById('shipstate').disabled = true;
        document.getElementById('shipzip').disabled = true;
    } else {
        document.getElementById('shipaddress').value = "";
        document.getElementById('shipcity').value = "";
        document.getElementById('shipstate').value = "";
        document.getElementById('shipzip').value = "";
        document.getElementById('shipaddress').disabled = false;
        document.getElementById('shipcity').disabled = false;
        document.getElementById('shipstate').disabled = false;
        document.getElementById('shipzip').disabled = false;
    }    
}

function onClickBillingCheck() {
    checkState = document.getElementById('billcheck').checked;
    if ( checkState == true ) {
        ma = document.getElementById('mailaddress').value;
        mc = document.getElementById('mailcity').value;
        ms = document.getElementById('mailstate').value;
        mz = document.getElementById('mailzip').value;
        if ( ma == "" && mc == "" && ms == "" && mz == "" ) {
            alert("Fill out the mailing address first.");
            location.hash = "mailing";
            document.getElementById('mailaddress').focus();
            document.getElementById('billcheck').checked = false;
            return(false);
        }
        document.getElementById('billaddress').value = ma;
        document.getElementById('billcity').value = mc;
        document.getElementById('billstate').value = ms;
        document.getElementById('billzip').value = mz;
        document.getElementById('billaddress').disabled = true;
        document.getElementById('billcity').disabled = true;
        document.getElementById('billstate').disabled = true;
        document.getElementById('billzip').disabled = true;
    } else {
        document.getElementById('billaddress').value = "";
        document.getElementById('billcity').value = "";
        document.getElementById('billstate').value = "";
        document.getElementById('billzip').value = "";
        document.getElementById('billaddress').disabled = false;
        document.getElementById('billcity').disabled = false;
        document.getElementById('billstate').disabled = false;
        document.getElementById('billzip').disabled = false;
    }    
}

function formSub() {
    companyName = document.getElementById('companyname');
    if ( companyName.value == "" ) {
        alert("Company name is required.");
        location.hash = "contact";
        companyName.focus();
        return(false);
    }

    phoneNumber = document.getElementById('phonenumber');
    if ( phoneNumber.value == "" ) {
        alert("Phone number is required.");
        location.hash = "contact";
        phoneNumber.focus();
        return(false);
    }

    contactName = document.getElementById('contactname');
    if ( contactName.value == "" ) {
        alert("Contact name is required.");
        location.hash = "contact";
        phoneNumber.focus();
        return(false);
    }

    emailAddress = document.getElementById('emailaddress');
    if ( emailAddress.value == "" ) {
        alert("Email address is required.");
        location.hash = "contact";
        emailAddress.focus();
        return(false);        
    }
    
    e1 = emailAddress.value.indexOf("@");
    e2 = emailAddress.value;
    e2 = e2.substr(e1+1);
    if ( e1 == -1 || e2.indexOf(".") == -1 ) {
        // ensure there is a period following an at-sign in the email address        
        alert("Email address is invalid.");
        location.hash = "contact";
        emailAddress.focus();
        return(false);
    }

    mailAddress = document.getElementById('mailaddress');
    if ( mailAddress.value == "" ) {
        alert("Mailing address is required.");
        location.hash = "mailing";
        mailAddress.focus();
        return(false);
    }

    mailCity = document.getElementById('mailcity');
    if ( mailCity.value == "" ) {
        alert("Mailing city is required.");
        location.hash = "mailing";
        mailCity.focus();
        return(false);
    }

    mailState = document.getElementById('mailstate');
    if ( mailState.value == "" ) {
        alert("Mailing state is required.");
        location.hash = "mailing";
        mailState.focus();
        return(false);
    }
    
    m = mailState.value;
    if ( m.length !== 2 ) {
        alert("Mailing state is invalid.");
        location.hash = "mailing";
        mailState.focus();
        return(false);
    }
   
    mailZip = document.getElementById('mailzip');
    if ( mailZip.value == "" ) {
        alert("Mailing zip is required.");
        location.hash = "mailing";
        mailZip.focus();
        return(false);
    }
    
    shipAddress = document.getElementById('shipaddress');
    if ( shipAddress.value == "" ) {
        alert("Shipping address is required.");
        location.hash = "shipping";
        shipAddress.focus();
        return(false);
    }

    shipCity = document.getElementById('shipcity');
    if ( shipCity.value == "" ) {
        alert("Shipping city is required.");
        location.hash = "shipping";
        shipCity.focus();
        return(false);
    }

    shipState = document.getElementById('shipstate');
    if ( shipState.value == "" ) {
        alert("Shipping state is required.");
        location.hash = "shipping";
        shipState.focus();
        return(false);
    }

    m = shipState.value;
    if ( m.length !== 2 ) {
        alert("Shipping state is invalid.");
        location.hash = "mailing";
        shipState.focus();
        return(false);
    }
   
    shipZip = document.getElementById('shipzip');
    if ( shipZip.value == "" ) {
        alert("Shipping zip is required.");
        location.hash = "shipping";
        shipZip.focus();
        return(false);
    }    

    billAddress = document.getElementById('billaddress');
    if ( billAddress.value == "" ) {
        alert("Billing address is required.");
        billAddress.focus();
        return(false);
    }

    billCity = document.getElementById('billcity');
    if ( billCity.value == "" ) {
        alert("Billing city is required.");
        location.hash = "billing";
        billCity.focus();
        return(false);
    }

    billState = document.getElementById('billstate');
    if ( billState.value == "" ) {
        alert("Billing state is required.");
        location.hash = "billing";
        billState.focus();
        return(false);
    }

    m = billState.value;
    if ( m.length !== 2 ) {
        alert("Billing state is invalid.");
        location.hash = "mailing";
        billState.focus();
        return(false);
    }
   
    billZip = document.getElementById('billzip');
    if ( billZip.value == "" ) {
        alert("Billing zip is required.");
        location.hash = "billing";
        billZip.focus();
        return(false);
    }       
    
    orderText = document.getElementById('ordertext');
    if ( orderText.value == "" ) {
        alert("Order details are required.");
        location.hash = "order";
        orderText.focus();
        return(false);
    }      

    return(true); 
}
