function validateFormOnSubmit(theForm) {
var reason = "";

reason += validateEmpty(theForm.Name,'Name');
reason += validateEmpty(theForm.Address,'Address');
reason += validateEmpty(theForm.City,'City');
reason += validateEmpty(theForm.Zip,'Zip');
reason += checkPass(theForm.Password);
reason += validateEmpty(theForm.Email,'E-mail');
reason += validatePhone(theForm.Phone);
reason += validateEmpty(theForm.Experience,'Experience');

      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


function validateEmpty(fld,err) {
    var error = "";
	var illegalChars = /[\w\s]/; // allow letters, numbers, and underscores
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The " + err + " field has not been filled in.\n"
    } else if (!illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The " + err + " field contains illegal 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 > 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 checkPass(fld) {
var error = "";
var password = fld.value;
var lowercase = "abcdefghijklmnopqrstuvwxyz";
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var number = "0123456789";
var containsLowerCase = contains(password, lowercase);
var containsUpperCase = contains(password, uppercase);
var containsNumbers = contains(password, number);
	if (!containsLowerCase) {
	error = "The password must contain at least one lower case letter.\n";
	fld.style.background = 'Yellow';
	} else if (!containsUpperCase) {
	error = "The password must contain at least one UPPER CASE letter.\n";
	fld.style.background = 'Yellow';
	} else if (!containsNumbers) {
	error = "The password must contain at least one numeral.\n";
	fld.style.background = 'Yellow';
	} else if ((password.length < 8) || (password.length > 25)) {
	error = "The password must be 8-25 characters in length.\n";
	fld.style.background = 'Yellow';
	} else {
	fld.style.background = 'White';
	}
	if (error != "") {
	fld.value="";
	fld.focus();
	}
return error;
}
function contains(password, validChars) {
    for (i = 0; i < password.length; i++) {
        var char = password.charAt(i);
        if (validChars.indexOf(char) > -1) {
            return true;
        }
    }
    return false;
}


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(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';
    } else {
        fld.style.background = 'White';
    }
    return error;
}














function warning() {
alert("All TRNW renewals must use the renewal form. You will now be taken to the renewal form.");
window.location=('form-renewal-old.html');
}
function checkPay(selection) {
tempIndex = selection.selectedIndex;
selectedPayment = selection.options[tempIndex].text;
	if (selectedPayment == "PayPal") {
	alert("You must have a current PayPal account in order to use the PayPal payment option. If you do not have a current PayPal account, please select another payment option");
	}
}
function checkTerms(selection) {
tempIndex = selection.selectedIndex;
selectedTerms = selection.options[tempIndex].text;
	if (selectedTerms == "12 Months Website Access Only for $120") {
	alert("Please note that you will not have access to any of the e-mail forums with a website access only membership. If you want to be able to post your problems to the network, and/or receive the daily e-mail's, then you will need to select a Full Access membership.");
	}
}

function dispAlert() {
alert("The password you provided does not meet the required criteria. Your password must contain at least one number, at least one UPPER CASE letter and at least one lower case letter");
subBut.className="hide";
document.form.Password.value="";
document.form.Password.focus();
}
