// JavaScript Document
var form;
var formType;

var name;
var position;
var address;
var city;
var state;
var zip;
var dob;
var height;
var weight;
var phone;
var altPhone;
var otherContact;
var insuranceInfo;
var shirtSize;
var shortSize;
var captcha;
var captchaImage;
var printedName;

function init(type){
    form = document.getElementById("form");
    name = document.getElementsByName("Name")[0];
    position = document.getElementsByName("Position")[0];
    address = document.getElementsByName("Street_Address")[0];
    city = document.getElementsByName("City")[0];
    state = document.getElementsByName("State")[0];
    zip = document.getElementsByName("Zip_Code")[0];
    dob = document.getElementsByName("Date_of_Birth")[0];
    height = document.getElementsByName("Height")[0];
    weight = document.getElementsByName("Weight")[0];
    phone = document.getElementsByName("Phone_Number")[0];
    altPhone = document.getElementsByName("Alternate_Number")[0];
    otherContact = document.getElementsByName("Other_Contact")[0];
    insuranceInfo = document.getElementsByName("Insurance_Info")[0];
    shirtSize = document.getElementsByName( "Shirt_Size" )[0];
    shortSize = document.getElementsByName( "Short_Size" )[0];
    captcha = document.getElementById( "captcha" );
    captchaImage = document.getElementById( "captchaImage" );
    printedName = document.getElementsByName("Agreement_Printed_Name")[0];
    formType = type;
	
    if(form.addEventListener){
        form.addEventListener("submit", validate, false);
    }
    else if(form.attachEvent){
        form.attachEvent("onsubmit",validate);
    }
}

function validate(){
    var emptyExp = /(^\s+$)|(^$)/;
    var emailExp = /^[a-zA-Z0-9\._-]+@([a-zA-Z0-9_-]+\.)+[a-zA-Z]{2,6}$/;
    var errorExists = false;
    var errorMsg = "Please enter the following:\r\n\r\n";
    var emptyStateExp = /^(Please Select One...)|(-- UNITED STATES --)|(-- CANADA --)|(-- AUSTRALIA --)$/;

    if(emptyExp.test(name.value)){
        name.style.backgroundColor = "red";
        errorMsg += "Name\r\n";
        errorExists = true;
    }
    else{
        name.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(position.value)){
        position.style.backgroundColor = "red";
        errorMsg += "Position\r\n";
        errorExists = true;
    }
    else{
        position.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(address.value)){
        address.style.backgroundColor = "red";
        errorMsg += "Address\r\n";
        errorExists = true;
    }
    else{
        address.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(city.value)){
        city.style.backgroundColor = "red";
        errorMsg += "City\r\n";
        errorExists = true;
    }
    else{
        city.style.backgroundColor = "white";
    }
	
    if(emptyStateExp.test(state[state.selectedIndex].value)){
        for(var i = 0; i < state.length; i++){
            state[i].style.backgroundColor = "red";
        }
        errorMsg += "State\r\n";
        errorExists = true;
    }
    else{
        for(var i = 0; i < state.length; i++){
            state[i].style.backgroundColor = "white";
        }
    }
	
    if(emptyExp.test(zip.value)){
        zip.style.backgroundColor = "red";
        errorMsg += "Zip\r\n";
        errorExists = true;
    }
    else{
        zip.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(dob.value)){
        dob.style.backgroundColor = "red";
        errorMsg += "Date of Birth\r\n";
        errorExists = true;
    }
    else{
        dob.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(height.value)){
        height.style.backgroundColor = "red";
        errorMsg += "Height\r\n";
        errorExists = true;
    }
    else{
        height.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(weight.value)){
        weight.style.backgroundColor = "red";
        errorMsg += "Weight\r\n";
        errorExists = true;
    }
    else{
        weight.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(phone.value)){
        phone.style.backgroundColor = "red";
        errorMsg += "Phone Number\r\n";
        errorExists = true;
    }
    else{
        phone.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(altPhone.value)){
        altPhone.style.backgroundColor = "red";
        errorMsg += "Alternate Phone Number\r\n";
        errorExists = true;
    }
    else{
        altPhone.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(otherContact.value)){
        otherContact.style.backgroundColor = "red";
        errorMsg += "Other Contact\r\n";
        errorExists = true;
    }
    else{
        otherContact.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(insuranceInfo.value)){
        insuranceInfo.style.backgroundColor = "red";
        errorMsg += "Insurance Info\r\n";
        errorExists = true;
    }
    else{
        insuranceInfo.style.backgroundColor = "white";
    }

    if(emptyExp.test(shirtSize.value)){
        shirtSize.style.backgroundColor = "red";
        errorMsg += "Shirt Size\r\n";
        errorExists = true;
    }
    else{
        shirtSize.style.backgroundColor = "white";
    }
	
    if(emptyExp.test(shortSize.value)){
        shortSize.style.backgroundColor = "red";
        errorMsg += "Short Size\r\n";
        errorExists = true;
    }
    else{
        shortSize.style.backgroundColor = "white";
    }

    if(emptyExp.test(printedName.value)){
        printedName.style.backgroundColor = "red";
        errorMsg += "Printed Name\r\n";
        errorExists = true;
    }
    else if( printedName.value != name.value ){
        printedName.style.backgroundColor = "red";
        name.style.backgroundColor = "red";
        errorMsg += "Printed Name must be the same as Name\r\n";
        errorExists = true;
    }
    else{
        printedName.style.backgroundColor = "white";
    }
	
    if(errorExists){
        alert(errorMsg);
    }
    else{
        var xhr = null;

        if ( window.XMLHttpRequest ){// code for IE7+, Firefox, Chrome, Opera, Safari
            xhr = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xhr = new ActiveXObject( "Microsoft.XMLHTTP" );
        }

        xhr.onreadystatechange = function(){
            if ( xhr.readyState == 4 && xhr.status == 200 ){
                if( "" + xhr.responseText == "true" ){
                    form.action = "sendForm.php";
                    form.submit();
                }
                else {
                    captcha.style.backgroundColor = "red";
                    alert( "Phrase is invalid" );
                    captcha.value = "";
                    captchaImage.src = "captcha/captcha.php#rand=" + Math.random();
                    captcha.focus();
                }
            }
        }

        if( xhr ){
            xhr.open( "GET", "scripts/validateCaptcha.php?captcha=" + captcha.value, true );
            xhr.send();
        }
    }
}