womAdd('$("newusername").focus();');
womAdd("updateStateField(''); updateStateField('corp');");

function verifyForm()
{
    clearMessages();
    result = true;
    
    // Change register button/area
    $('regButton').hide();
    $('regMsg').innerHTML = "Please wait while we verify your registration...";

    result = ( result && verifyUserInfo() );

    result = ( result && verifyPassword() );

    result = ( result && verifyAccountInfo() );

    if( result )
    {
        var country = $('country').value;
        var corpcountry = $('corpcountry').value;

        var state = (country == 'US') ? $('state').value : $('state_input').value;
        var corpstate = (corpcountry == 'US') ? $('corpstate').value : $('corpstate_input').value;
        var email_subscribe = ($('email_subscribe').checked) ? 'yes' : 'no';
        
        ajax = new Ajax.Request('/ajax.php', {
            method: 'get',
            parameters: {
                requestType: 'createAccount',
                username: $('newusername').value,
                password: $('passwd').value,
                email: $('email').value,
                name: $('name').value,
                address1: $('address1').value,
                address2: $('address2').value,
                city: $('city').value,
                state: state,
                country: $('country').value,
                zip: $('zip').value,
                phone: $('phone').value,
                fax: $('fax').value,
                norno: $('norno').value,
                asi: $('asi').value,
                ppai: $('ppai').value,
                pppc: $('pppc').value,
                companyname: $('companyname').value,
                corpaddress1: $('corpaddress1').value,
                corpaddress2: $('corpaddress2').value,
                corpcity: $('corpcity').value,
                corpstate: corpstate,
                corpcountry: $('corpcountry').value,
                corpzip: $('corpzip').value,
                corpphone: $('corpphone').value,
                email_subscribe: email_subscribe,
                keycode: ''
            },
            onSuccess: ajaxResponse,
            onFailure: function() {
            	$('regButton').show();
            	$('regMsg').innerHTML = "";
                $("errorMsg").innerHTML = "The system could not create your account. Please try again later or <a href='/special/contact/Contact+Us/'>contact us</a>.";
            }
        });
    }
    else {
    	$('regButton').show();
    	$('regMsg').innerHTML = "";
    }

    return false;
}

ajaxResponse = function(response)
{	
	$('regMsg').innerHTML = "";
	
    if( response.responseJSON.type == "OK" )
    {
    	$("infoMsg").innerHTML = response.responseJSON.html;
    } else {
        $("errorMsg").innerHTML = response.responseJSON.html;
        
        if (response.responseJSON.code == 'unverified') {
        	// Do nothing
    	}
        else if (response.responseJSON.code == 'invalid') {
        	// Let them try to fix the errors
        	$('regButton').show();
        }	
    }
}


function verifyUserInfo()
{
    username  = document.getElementById( "newusername" ).value;
    email = document.getElementById( "email" ).value;
    name = document.getElementById( "name" ).value;

    if( username == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter a username!  ";

        document.getElementById( "usernameRow" ).style.backgroundColor = "red";
        document.getElementById( "newusername" ).focus();
        return false;
    } else if ((username.match("'") != null) || (username.match('"') != null) || (username.match(" ") != null)) {
    	document.getElementById( "errorMsg" ).innerHTML +=
    	    "Username may not contain quotes or spaces  ";
        document.getElementById( "usernameRow" ).style.backgroundColor = "red";
        document.getElementById( "newusername" ).focus();
        return false;
    } else if( email == "" ) {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter an email!  ";

        document.getElementById( "emailRow" ).style.backgroundColor = "red";
        document.getElementById( "email" ).focus();
        return false;
    } else if( name == "" ) {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your name!  ";

        document.getElementById( "nameRow" ).style.backgroundColor = "red";
        document.getElementById( "name" ).focus();
        return false;
    }

    return true;
}

function verifyPassword()
{
    password  = document.getElementById( "passwd" ).value;
    password2 = document.getElementById( "passwd2" ).value;

    if( password != password2 )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "Passwords do not match!  ";

        document.getElementById( "passwdRow" ).style.backgroundColor = "red";
        document.getElementById( "passwd2Row" ).style.backgroundColor = "red";
        document.getElementById( "passwd" ).focus();
        return false;
    } else if( password == "" ) {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter a password!  ";

        document.getElementById( "passwdRow" ).style.backgroundColor = "red";
        document.getElementById( "passwd" ).focus();
        return false;
    }

    return true;
}

function verifyAccountInfo()
{
    norno = document.getElementById( "norno" ).value;
    nonorno = document.getElementById( "nonorno" ).checked;
    companyname = document.getElementById( "companyname" ).value;
    corpaddress1 = document.getElementById( "corpaddress1" ).value;
    corpcity = document.getElementById( "corpcity" ).value;
    corpstate = document.getElementById( "corpstate" ).value;
    corpstateinput = document.getElementById( "corpstate_input" ).value;
    corpzip = document.getElementById( "corpzip" ).value;
    corpphone = document.getElementById( "corpphone" ).value;

    var i = $('corpcountry').selectedIndex;
    var country = $('corpcountry').options[i].value;

    if( norno == "" && !nonorno )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Norwood Number or indicate that you do not have one!  ";

        document.getElementById( "nornoRow" ).style.backgroundColor = "red";
        document.getElementById( "norno" ).focus();
        return false;
    }

    if( companyname == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Company Name!  ";

        document.getElementById( "companynameRow" ).style.backgroundColor = "red";
        document.getElementById( "companyname" ).focus();
        return false;
    }
    
    if( corpaddress1 == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Billing Address!  ";

        document.getElementById( "corpaddress1Row" ).style.backgroundColor = "red";
        document.getElementById( "corpaddress1" ).focus();
        return false;
    }
    
    if( corpcity == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Billing City!  ";

        document.getElementById( "corpcityRow" ).style.backgroundColor = "red";
        document.getElementById( "corpcity" ).focus();
        return false;
    }

    // US Check
    if( country == "US" && corpstate == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Billing State!  ";

        document.getElementById( "corpstateRow" ).style.backgroundColor = "red";
        document.getElementById( "corpstate" ).focus();
        return false;
    }

    // Other country check
    if( country != "US" && corpstateinput == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Billing State!  ";

        document.getElementById( "corpstateRow" ).style.backgroundColor = "red";
        document.getElementById( "corpstateinput" ).focus();
        return false;
    }
    
    if( corpzip == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Billing Zip!  ";

        document.getElementById( "corpzipRow" ).style.backgroundColor = "red";
        document.getElementById( "corpzip" ).focus();
        return false;
    }
    
    if( corpphone == "" )
    {
        document.getElementById( "errorMsg" ).innerHTML +=
            "You must enter your Billing Phone!  ";

        document.getElementById( "corpphoneRow" ).style.backgroundColor = "red";
        document.getElementById( "corpphone" ).focus();
        return false;
    }
    
    return true;
}

function clearMessages()
{
    document.getElementById( "errorMsg" ).innerHTML = "";
    document.getElementById( "infoMsg" ).innerHTML = "";
  
        document.getElementById( "usernameRow" ).style.backgroundColor = "white";
    document.getElementById( "passwdRow" ).style.backgroundColor = "white";
    document.getElementById( "passwd2Row" ).style.backgroundColor = "white";
    document.getElementById( "emailRow" ).style.backgroundColor = "white";
    document.getElementById( "nameRow" ).style.backgroundColor = "white";
    //document.getElementById( "keycodeRow" ).style.backgroundColor = "white";
    document.getElementById( "companynameRow" ).style.backgroundColor = "white";
    document.getElementById( "corpaddress1Row" ).style.backgroundColor = "white";
    document.getElementById( "corpcityRow" ).style.backgroundColor = "white";
    document.getElementById( "corpstateRow" ).style.backgroundColor = "white";
    document.getElementById( "corpzipRow" ).style.backgroundColor = "white";
    document.getElementById( "corpphoneRow" ).style.backgroundColor = "white";
    document.getElementById( "nornoRow" ).style.backgroundColor = "white";
}

function updateStateField(prefix) {
    var i = $(prefix+'country').selectedIndex;
    var country = $(prefix+'country').options[i].value;
  
        if (country == 'US') {
        $(prefix+'state_text').innerHTML = "State:";
        $(prefix+'state_input').hide();
        $(prefix+'state').show();
    }
    else {
        $(prefix+'state_text').innerHTML = "State/Province:";
        $(prefix+'state').hide();
        $(prefix+'state_input').show();
    }
}
