function numbersonly(e, decimal) {
var key;
var keychar;
if (window.event) {
   key = window.event.keyCode;
}
else if (e) {
   key = e.which;
}
else {
   return true;
}
keychar = String.fromCharCode(key);

if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
   return true;
}
else if ((("0123456789").indexOf(keychar) > -1)) {
   return true;
}
//else if (decimal && (keychar == ".")) { 
  //return true;
//}
else
   return false;
}


function validateSMembership(theForm) {
var reason = "";
var check = 0;
  reason += validateEmpty(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += sameEmail(theForm.Email,theForm.CEmail);
  reason += validatePassword(theForm.Pass);
  reason += samePass(theForm.Pass,theForm.CPass);
  reason += validatePhone(theForm.Phone);
  reason += validateEmpty(theForm.Address);
  reason += validateZip(theForm.Zip);
  reason += validateEmpty(theForm.industry);
  reason += validateEmpty(theForm.textarea); 
  if (reason != "") {
    alert(reason);
    return false;
  }
  return true;
}
function validatePMembership(theForm) {
var reason = "";
var check = 0;
  reason += validateEmpty(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += sameEmail(theForm.Email,theForm.CEmail);
  reason += validatePassword(theForm.Pass);
  reason += samePass(theForm.Pass,theForm.CPass);
  reason += validatePhone(theForm.Phone);
  reason += validateEmpty(theForm.Address);
  reason += validateZip(theForm.Zip);
  reason += validateEmpty(theForm.industry);
  reason += validateEmpty(theForm.textarea); 
  if (reason != "") {
    alert(reason);
    return false;
  }
  return true;
}
function validateJMembership(theForm) {
var reason = "";
var check = 0;
  reason += validateEmpty(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += sameEmail(theForm.Email,theForm.CEmail);
  reason += validatePassword(theForm.Pass);
  reason += samePass(theForm.Pass,theForm.CPass);
  reason += validatePhone(theForm.Phone);
  reason += validateEmpty(theForm.Address);
  reason += validateZip(theForm.Zip);
  reason += validateEmpty(theForm.industry);
  reason += validateEmpty(theForm.textarea); 
  if (reason != "") {
    alert(reason);
    return false;
  }
  return true;
}
function validateEMembership(theForm) {
var reason = "";
var check = 0;
  check = check + validateOneChecked(theForm.Share_info);
  check = check + validateOneChecked(theForm.Working_Partner);
  check = check + validateOneChecked(theForm.Seek_Funding_Options);
  check = check + validateOneChecked(theForm.Receive_General_Feedback);
  check = check + validateOneChecked(theForm.WE_HAVE_JOBS);
  check = check + validateOneChecked(theForm.Grant_App);
  reason += validateEmpty(theForm.Name);
  reason += validateEmail(theForm.Email);
  reason += sameEmail(theForm.Email,theForm.CEmail);
  reason += validatePassword(theForm.Pass);
  reason += samePass(theForm.Pass,theForm.CPass);
  reason += validatePhone(theForm.Phone);
  reason += validateEmpty(theForm.Address);
  reason += validateZip(theForm.Zip);
  reason += validateEmpty(theForm.industry); 
  reason += validateEmpty(theForm.Position);
  reason += validateEmpty(theForm.textarea); 
  if (reason != "" || check > 5) {
	  if (check > 5){
		  reason += "You must check at least one reason you are creating a profile \n"
	  }
    alert(reason);
    return false;
  }

  return true;
}

function clearText(field){
    if (field.defaultValue == field.value) field.value = '';
    else if (field.value == '') field.value = field.defaultValue;

}
function samePass(field,otherfield){
	var error = '';
    if (field.value != otherfield.value){
		otherfield.style.background = 'yellow';
		error = 'Password and Confirm Password do not match. \n';
		return error;
	} else {
		otherfield.style.background = '';
		error = '';
		return error;
	}
}
function sameEmail(field,otherfield){
	var error = '';
    if (field.value != otherfield.value){
		otherfield.style.background = 'yellow';
		error = 'Email and Confirm Email do not match. \n';
		return error;
	} else {
		otherfield.style.background = '';
		error = '';
		return error;
	}
}
function validateMailGuide(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmpty(theForm.Zip);
  reason += validateEmpty(theForm.Name); 
  reason += validateEmpty(theForm.Address); 
  reason += validateEmpty(theForm.City);
  reason += validateEmpty(theForm.State);
  if (reason != "") {
    alert("Please correct highlighted fields.");
    return false;
  }

  return true;
}
function validateScholApp(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.SEmail);
  reason += validatePhone(theForm.Phone);
  reason += validateEmpty(theForm.Zip);
  reason += validateEmpty(theForm.Contact); 
  reason += validateEmpty(theForm.Address); 
  reason += validateEmpty(theForm.City);
  reason += validateEmpty(theForm.SchoolName); 
  reason += validateEmpty(theForm.Address); 
  reason += validateEmpty(theForm.GradDate);
  reason += validateEmpty(theForm.Description); 
  reason += validateEmpty(theForm.ScholarshipUse); 
  reason += validateEmpty(theForm.Resume); 
  if (reason != "") {
    alert("Please correct highlighted fields.");
    return false;
  }

  return true;
}
function validateGrantApp(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.BusEmail);
  reason += validatePhone(theForm.BusPhone);
  reason += validateEmpty(theForm.BusName);
  reason += validateEmpty(theForm.BusContact); 
  reason += validateEmpty(theForm.BusAddress); 
  reason += validateEmpty(theForm.BusCity);
  reason += validateEmpty(theForm.BusDescription); 
  reason += validateEmpty(theForm.BusGrantsUse); 
  reason += validateEmpty(theForm.BusPlan); 
  if (reason != "") {
    alert("Please correct highlighted fields.");
    return false;
  }

  return true;
}
function validateGrantApp2(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  //reason += validateEmail(theForm.BusEmail);
  //reason += validatePhone(theForm.BusPhone);
  reason += validateEmpty(theForm.BillingName);
  reason += validateEmpty(theForm.BillingAddress); 
  reason += validateEmpty(theForm.BillingCity); 
  reason += validateEmpty(theForm.BillingZip);
  reason += validateEmpty(theForm.CSC); 
  reason += validateCC(theForm.CCNum); 
  //reason += validateEmpty(theForm.BusPlan); 
  if (reason != "") {
    alert("Please correct highlighted fields.");
    return false;
  }

  return true;
}


function validateSignUp(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.Email2);
  //reason += validatePhone(theForm.Phone);
  reason += validateEmpty(theForm.Name);
  reason += validateZip(theForm.Location);    
  if (reason != "") {
    alert("Please correct highlighted fields.");
    return false;
  }

  return true;
}

function validateContact(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.EmailC);
  reason += validateEmpty(theForm.Name);
  reason += validateEmpty(theForm.Message);    
  if (reason != "") {
    alert("Please correct highlighted fields.");
    return false;
  }

  return true;
}
function validateDownloadEmail(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.EmailDL);
  //reason += validatePhone(theForm.phone);
  //reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateSomeThing(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.Email);
  //reason += validatePhone(theForm.phone);
  //reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}
function validateEmailList(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.Email);
  //reason += validatePhone(theForm.phone);
  //reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert(reason);
    return false;
  }

  return true;
}
function validateEmpty(fld) {
    var error = "";
 
    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 validateAgree(theForm) {
var reason = "";
  reason += validateChecked(theForm.standards);
  reason += validateChecked(theForm.commitment);
  if (reason != "") {
    alert("You must agree to the Turn US Around Ethics, Standards, and Commitment.");
    return false;
  }
  return true;
}

function validateChecked(fld) {
    var error = "";
 
    if (fld.checked == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been checked.\n"
		return error;
    } else {
		fld.style.background = 'White';
		error = "";
		return error;
    }
     
}
function validateOneChecked(fld) {
    var error = 0;
 
    if (fld.checked == 0) {
		error = 1;
		return error;
    } else {
		error = 0;
		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 = "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';
    } else {
        fld.style.background = '';
		error = '';
    }
    return error;
}
function validateZip(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 Zip Code contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 5)) {
        error = "The Zip Code is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = '';
		error = '';
    }
    return error;
}
function validateCC(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a credit card number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The credit card number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length >= 15)) {
        error = "The credit card number is the wrong length.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}