﻿function validateForm(theForm) {
            var reason = "";
            
              //reason += validateUsername(theForm.username);
              //reason += validatePassword(theForm.pwd);
              //reason += validatePhone(theForm.phone);
              reason += validateEmpty(theForm.name);
              //reason += validateEmail(theForm.email);
              //reason += validatePhone(theForm.phone);
              //reason += validateEmpty(theForm.street);
              //reason += validateEmpty(theForm.city);
              //reason += validateEmpty(theForm.state);
              //reason += validateEmpty(theForm.zip);
                  
              if (reason != "") {
                alert("Some fields need correction:\n" + reason);
                return false;
              }
            
              //alert("All fields are filled correctly");
              return true;
            }
            function validateEmpty(fld) {
                var error = "";
             
                if (fld.value.length == 0) {
                    fld.style.background = 'Yellow'; 
                    error = "\n- " + fld.name.toUpperCase() + " is a required field\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 > 15)) {
                    fld.style.background = 'Yellow'; 
                    error = "The username is the wrong length.\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 < 7) || (fld.value.length > 15)) {
                    error = "The password is the wrong length. \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 numeral.\n";
                    fld.style.background = 'Yellow';
                } else {
                    fld.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 = "\n- EMAIL is a required field.\n";
                } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
                    fld.style.background = 'Yellow';
                    error = "\n- Please enter a valid email address.\n";
                } else if (fld.value.match(illegalChars)) {
                    fld.style.background = 'Yellow';
                    error = "\n- 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 = "\n- PHONE is a required field.\n";
                    fld.style.background = 'Yellow';
                } else if (isNaN(parseInt(stripped))) {
                    error = "\n- The phone number contains illegal characters.\n";
                    fld.style.background = 'Yellow';
                } else if (!(stripped.length == 10)) {
                    error = "\n- The phone number is the wrong length. Make sure you included an area code.\n";
                    fld.style.background = 'Yellow';
                }
                return error;
            }
            
            
// Forcing first letter to capitalize
           
function capitalizeMe(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                newVal += val[c].substring(0,1).toUpperCase() +
val[c].substring(1,val[c].length) + ' ';
        }
        obj.value = newVal;
}



// Populate 'Country' automatically to either USA or Canada

var states = new Array();
states["AL"] = "USA";
states["AK"] = "USA";
states["AB"] = "Canada";
states["AZ"] = "USA";
states["AZ"] = "USA";
states["AR"] = "USA";
states["BC"] = "Canada";
states["CA"] = "USA";
states["CO"] = "USA";
states["CT"] = "USA";
states["DE"] = "USA";
states["DC"] = "USA";
states["FL"] = "USA";
states["GA"] = "USA";
states["HI"] = "USA";
states["ID"] = "USA";
states["IL"] = "USA";
states["IN"] = "USA";
states["IA"] = "USA";
states["KS"] = "USA";
states["KY"] = "USA";
states["LA"] = "USA";
states["ME"] = "USA";
states["MB"] = "Canada";
states["MD"] = "USA";
states["MA"] = "USA";
states["MI"] = "USA";
states["MN"] = "USA";
states["MS"] = "USA";
states["MO"] = "USA";
states["MT"] = "USA";
states["NE"] = "USA";
states["NV"] = "USA";
states["NB"] = "Canada";
states["NH"] = "USA";
states["NJ"] = "USA";
states["NM"] = "USA";
states["NY"] = "USA";
states["NL"] = "Canada";
states["NC"] = "USA";
states["ND"] = "USA";
states["NS"] = "Canada";
states["OH"] = "USA";
states["OK"] = "USA";
states["ON"] = "Canada";
states["OR"] = "USA";
states["PA"] = "USA";
states["PE"] = "Canada";
states["QC"] = "Canada";
states["RI"] = "USA";
states["SK"] = "Canada";
states["SC"] = "USA";
states["SD"] = "USA";
states["TN"] = "USA";
states["TX"] = "USA";
states["UT"] = "USA";
states["VT"] = "USA";
states["VA"] = "USA";
states["WA"] = "USA";
states["WV"] = "USA";
states["WI"] = "USA";
states["WY"] = "USA";




function printCountry(){
var text = document.getElementsByName('state')[0].options[document.getElementsByName('state')[0].selectedIndex].value;
  document.getElementsByName('country')[0].value = states[text];
}