function validateEmpty(fld) {
    var error = "";
  
    fld.value=trim(fld.value);  //trim off whitepace
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateMinLength(fld, fldLen, fldTitle) {
    var error = "";
  
    fld.value=trim(fld.value);  //trim off whitepace
    if (fld.value.length <fldLen) {
        fld.style.background = 'Yellow'; 
        error = fldTitle + " must be at least " + fldLen + " characters.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 20)) {
        fld.style.background = 'Yellow'; 
        error = "The username must be between 5 and 20 characters long.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}

function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 20)) {
        error = "The password must be between 5 and 20 characters long. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one number.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
} 

function validateRadio(fld) {
    var error = "";
    
    // set var radio_choice to false
    var radio_choice = false;

    // Loop from zero to the one minus the number of radio button selections
    for (counter = 0; counter < fld.length; counter++)
    {
        // If a radio button has been selected it will return true
        // (If not it will return false)
        if (fld[counter].checked)
            radio_choice = true; 
    }

    if (!radio_choice)
    {
        // If there were no selections made display an alert box 
        error = "Please select one of the highlighted options.";
        for (counter = 0; counter < fld.length; counter++)
        {
            fld[counter].style.background = 'Yellow';
        }
    }
    else
        for (counter = 0; counter < fld.length; counter++)
        {
            fld[counter].style.background = 'White';
        }
    return error;
     

} 

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}

function validateZip(fld) {
    var error = "";
    var valid = "0123456789-";
    var hyphencount = 0;
    
    if (fld.value.length!=5 && fld.value.length!=10) {
    error="Please enter your 5 digit or 5 digit+4 zip code.\n";
    fld.style.background = 'Yellow';
    }
    for (var i=0; i < fld.value.length; i++) {
    temp = "" + fld.value.substring(i, i+1);
    if (temp == "-") hyphencount++;
    if (valid.indexOf(temp) == "-1") {
    error="Invalid characters in your zip code.  The supported zip code format is 12345 or 12345-6789.  Please try again.";
    fld.style.background = 'Yellow';
    }
    if ((hyphencount > 1) || ((fld.value.length==10) && ""+fld.value.charAt(5)!="-")) {
    error="The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.";
    fld.style.background = 'Yellow';
       }
    }
    return error;
}

//set state codes
function selectFromDropDown(fld, val) {
    for ( var i = 0; i < fld.options.length; i++ ) {
        if ( fld.options[i].value == val ) {
            fld.options[i].selected = true;
            return;
        }
    }
}

function saveStatus(sel, company_id){
document.form1.company_id_to_update.value=company_id;
document.form1.status_to_update.value=sel[sel.selectedIndex].value;
document.form1.submit();

}