function highlightError(field) {

  field.style.backgroundColor = "#FED09E";

}



function unHighlightError(field) {

  field.style.backgroundColor = "";

}



function validateRegFormOnClick(quiet){



    var error_header="The following fields are required for registration.\n\n";

    var error_message="";

    var myform=document.registrationForm;



    // User Name

    /*myform.pi_username.value  = trim(myform.pi_username.value);

    if (myform.pi_username.value=="") {

        error_message+="Please specify a Username\n";

    }

    if (myform.pi_username.value.length < 6) {

        error_message+="Your Username should be at least 6 characters long \n";

        highlightError(myform.pi_username);

    } else {

        unHighlightError(myform.pi_username);

    }
*/

if (myform.pi_username != null)
{

// User Name
    myform.pi_username.value  = trim(myform.pi_username.value);
    if (myform.pi_username.value=="") {
        error_message+="Please specify a Username\n";
    }
    if (myform.pi_username.value.length < 6) {
        error_message+="Your Username should be at least 6 characters long \n";
        highlightError(myform.pi_username);
    } else {
        unHighlightError(myform.pi_username);
    }
}




    // Password

    myform.pi_password.value   = trim(myform.pi_password.value);

    myform.pi_password_conf.value  = trim(myform.pi_password_conf.value);

    if (myform.pi_password.value=="") {

        error_message+="Please specify a Password\n";

    }

    if (myform.pi_password.value.length < 5) {

        error_message+="Your Password should be at least 5 characters long\n";

        highlightError(myform.pi_password);

    } else {

        unHighlightError(myform.pi_password);

    }



    if (myform.pi_password_conf.value=="") {

        error_message+='Please retype your Password in the "Re-type Password" field\n';

        highlightError(myform.pi_password_conf);

    }

    if (myform.pi_password_conf.value != myform.pi_password.value) {

        error_message+='Your "Password" and "Re-type Password" do not match\n';

        highlightError(myform.pi_password_conf);

    } else if (myform.pi_password_conf.length > 4) {

        unHighlightError(myform.pi_password_conf);

    }





    // First Name

    myform.pi_first_name.value  = trim(myform.pi_first_name.value);

    if (myform.pi_first_name.value=="") {

        error_message+="Please specify a First Name\n";

        highlightError(myform.pi_first_name);

    } else {

        unHighlightError(myform.pi_first_name);

    }



    // Last Name

    myform.pi_last_name.value  = trim(myform.pi_last_name.value);

    if (myform.pi_last_name.value=="") {

        error_message+="Please specify a Last Name\n";

        highlightError(myform.pi_last_name);

    } else {

        unHighlightError(myform.pi_last_name);

    }



    // Gender

    genderFilledIn = false;

    for ( i = 0; i < 2; i++ ) {

        if( myform.pi_gender[i].checked == true ) {

            genderFilledIn = true;

        }

    }

    if (genderFilledIn == false) {

        error_message+="Please specify your Gender\n";

        highlightError(myform.pi_gender[0]);

        highlightError(myform.pi_gender[1]);

    } else {

        unHighlightError(myform.pi_gender[0]);

        unHighlightError(myform.pi_gender[1]);

    }



    // Age Range

    if (myform.pi_age_range.value=="-1") {

        error_message+="Please select your Age Range\n";

        highlightError(myform.pi_age_range);

    } else {

        unHighlightError(myform.pi_age_range);

    }



    // E-Mail

    myform.pi_email.value  = trim(myform.pi_email.value);

    if (myform.pi_email.value=="") {

        error_message+="Please specify an E-mail Address\n";

        highlightError(myform.pi_email);

    } else if (invalidEmail(myform.pi_email.value)) {

        error_message+="Please make sure your Email Address is entered correctly (e.g. myname@company.com)\n";

        highlightError(myform.pi_email);

    } else {

        unHighlightError(myform.pi_email);

    }



    // Postal Code

    myform.pi_postal_code.value  = trim(myform.pi_postal_code.value);

    if (myform.pi_postal_code.value=="") {

        error_message+="Please specify a Postal Code or Zip Code\n";

        highlightError(myform.pi_postal_code);

    } else {

        unHighlightError(myform.pi_postal_code);

    }



    // Access

    accessFilledIn = false;



    access = new Array(4);

    access[0] = myform.pi_use_home_yn.checked;

    access[1] = myform.pi_use_work_yn.checked;

    access[2] = myform.pi_use_mobile_yn.checked;

    access[3] = myform.pi_use_other_yn.checked;





    for ( i = 0; i < 4; i++ ) {

        if( access[i] == true ) {

            accessFilledIn = true;

        }

    }

    if (accessFilledIn == false) {

        error_message+="Please specify where you access the Globe website\n";

        highlightError(myform.pi_use_home_yn);

        highlightError(myform.pi_use_work_yn);

        highlightError(myform.pi_use_mobile_yn);

        highlightError(myform.pi_use_other_yn);

    } else {

        unHighlightError(myform.pi_use_home_yn);

        unHighlightError(myform.pi_use_work_yn);

        unHighlightError(myform.pi_use_mobile_yn);

        unHighlightError(myform.pi_use_other_yn);

    }



    if (myform.pi_use_home_yn.checked == true) {

        myform.pi_use_home_yn.value = "Y";

    } else {

        myform.pi_use_home_yn.value = "N";

    }

    if (myform.pi_use_work_yn.checked == true) {

        myform.pi_use_work_yn.value = "Y";

    } else {

        myform.pi_use_work_yn.value = "N";

    }

    if (myform.pi_use_mobile_yn.checked == true) {

        myform.pi_use_mobile_yn.value = "Y";

    } else {

        myform.pi_use_mobile_yn.value = "N";

    }

    if (myform.pi_use_other_yn.checked == true) {

        myform.pi_use_other_yn.value = "Y";

    } else {

        myform.pi_use_other_yn.value = "N";

    }



    // Agreement

    if (myform.pi_read_term_yn.checked == true) {

        myform.pi_read_term_yn.value = "Y";

    } else {

        myform.pi_read_term_yn.value = "N";

    }

    if (myform.pi_read_term_yn.checked == false) {

        error_message+="\n Please check off your agreement to the Privacy Policy and Terms and Conditions.  Unfortunately, if you do not agree, we cannot accept your registration.\n";

        highlightError(myform.pi_read_term_yn);

    } else {

        unHighlightError(myform.pi_read_term_yn);

    }



    // Send Info

    if (myform.pi_send_info_yn.checked == true) {

        myform.pi_send_info_yn.value = "Y";

    } else {

        myform.pi_send_info_yn.value = "N";

    }



    // Remember Me

    if (myform.pi_remember_me_yn.checked == true) {

        myform.pi_remember_me_yn.value = "Y";

    } else {

        myform.pi_remember_me_yn.value = "N";

    }



    // Output error string if there were any errors

    if (!quiet && (error_message!="")) {

        alert(error_header + error_message);

        return false;

    } else {

        return true;

    }

}







// Trim whitespace from around a string

function trim(str) {

  // first trim leading spaces and then trim trailing spaces

  str = str.replace(/^\s*/,"").replace(/\s*$/,"");

  return str;

}









// A simple e-mail address validator which determines if it's basic form is correct

function invalidEmail(email) {

    var invalid = false;

    if (email.indexOf("@")<1){



        // No characters before @ symbol or @ does not exist

        invalid = true;

    }else{

        var right = email.substring((email.indexOf("@") + 1), email.length);



        // No .(dot) in portion after @ symbol

        if (right.indexOf(".")==-1){ invalid = true; }else{



            // No characters between @ and first .(dot)

            if (right.substring(0, right.indexOf("."))<1){ invalid = true; }else{



                // No characters after first .(dot)

                if (right.substring((right.indexOf(".") + 1), right.length)<1){ invalid = true; }

            }

        }

    }

    return invalid;

}



