function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}
function validate_required(field, name)
{
tString=trim(field);
if (tString==null||tString=="")
  {theErrors = (theErrors + ("Please enter " +name +"\n\n"));}
}

function validate_email(email)
{
tString=trim(email);
apos=tString.indexOf("@");
dotpos=tString.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {theErrors = (theErrors + ("Please enter a valid email address\n\n"));}
}

function validate_emailB(email)
{
tString=trim(email);
apos=tString.indexOf("@");
dotpos=tString.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {theErrors = (theErrors + ("Please enter a valid secondary email address\n\n"));}
}

function validate_theEmails(email1, email2)
{
firstEmail=trim(email1);
secondEmail=trim(email2);
if (firstEmail != secondEmail){theErrors = (theErrors + ("Your email fields do not match, please check them\n\n"));}
}

function validate_theEmailsB(email1, email2)
{
firstEmail=trim(email1);
secondEmail=trim(email2);
if (firstEmail != secondEmail){theErrors = (theErrors + ("Your secondary email fields do not match, please check them\n\n"));}
}

function validate_thePasswords(pass1, pass2)
{
firstPass=trim(pass1);
secondPass=trim(pass2);
if (firstPass != secondPass){theErrors = (theErrors + ("Your password fields do not match, please re-type them\n\n"));}
}

function LimitAttach(form, file) {
allowSubmit = false;
extArray = new Array(".gif", ".jpeg", ".jpg", ".png", ".bmp");
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true;; break}
}
if (allowSubmit == true) { imgCount++;return true}
else {fileMessage++; if (fileMessage>1){return true} else {theErrors = (theErrors + ("Please only upload files that end in types: " + (extArray.join(" "))+ "\n\n"))}}
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validatePhone(Phone){
	if (Phone.value!=""){
		if (checkInternationalPhone(Phone)==false){
			theErrors = (theErrors + ("Please enter a valid phone number.  Make sure to include your area code.\n\n"));
			return false;}
	}
}

function validate(chk){
  if (chk.checked)
    return true;
  else
  	if (ran != 1){
    theErrors = (theErrors + ("Please read and check all of the checkboxes in the participation section at the bottom of the application.\n\n"));
	ran = 1;}
}

function validate_form(thisform)
{theErrors = "";
ran = 0;
name = "";
fileMessage=0;
imgCount=0;
appFirst = document.application.appFirst.value
validate_required(appFirst, "your first name");
appLast = document.application.appLast.value
validate_required(appLast, "your last name");
appMonth = document.application.appMonth.value
validate_required(appMonth, "your birth month");
appDay = document.application.appDay.value
validate_required(appDay, "your birth day");
appYear = document.application.appYear.value
validate_required(appYear, "your birth year");
appPhone = document.application.appPhone.value
validate_required(appPhone, "your phone number");
validatePhone(appPhone);
appEmail = document.application.appEmail.value
validate_required(appEmail, "your email address");
appEmail2 = document.application.appEmail2.value
validate_required(appEmail2, "your email address in both fields");
validate_email(appEmail);
validate_theEmails(appEmail, appEmail2);
appEmailB = document.application.appEmailB.value
appEmailB2 = document.application.appEmailB2.value
if (appEmailB != ""){
validate_emailB(appEmailB);
validate_theEmailsB(appEmailB, appEmailB2);}
state = document.application.state.value
validate_required(state, "the state in which you attend school");
appSchoolDrop = document.application.school.value
appSchoolText = document.application.wildCardSchool.value
appPassword = document.application.appPassword.value
appPassword2 = document.application.appPassword2.value
validate_required(appPassword, "a password");
validate_required(appPassword2, "a password in both fields");
validate(document.application.understand1);
validate(document.application.understand2);
validate(document.application.understand3);
validate(document.application.understand4);
validate(document.application.understand5);
validate(document.application.understand6);
appHear = document.application.appHear.value
validate_required(appHear, "how you heard about us");

validate_thePasswords(appPassword, appPassword2);
//if(appSchoolDrop==null||appSchoolDrop==""){theErrors = (theErrors + ("Select the school that you attend or select Wild Card if your school is not listed\n\n"))}
//if(appSchoolDrop=="wildcard"){if(appSchoolText==null||appSchoolText==""){theErrors = (theErrors + ("You have indicated that you are a Wild Card, please enter your school in the text field provided"))}}


appHair = document.application.appHair.value
validate_required(appHair, "your hair color");
appEyes = document.application.appEyes.value
validate_required(appEyes, "your eye color");
appWeight = document.application.appWeight.value
validate_required(appWeight, "your weight");
appBust = document.application.appBust.value
validate_required(appBust, "your bust/cup");
appWaist = document.application.appWaist.value
validate_required(appWaist, "your waist measurement");
appHips = document.application.appHips.value
validate_required(appHips, "your hips measurement");
appDress = document.application.appDress.value
validate_required(appDress, "your dress size");
appShoe = document.application.appShoe.value
validate_required(appShoe, "your shoe size");



appMajor = document.application.appMajor.value
validate_required(appMajor, "your major")
appGradMonth = document.application.appGradMonth.value
validate_required(appGradMonth, "what month you will graduate")
appGradYear = document.application.appGradYear.value
validate_required(appGradYear, "what year you will graduate")
appHeightFeet = document.application.appHeightFeet.value
validate_required(appHeightFeet, "your height in feet")
appHeightInches = document.application.appHeightInches.value
validate_required(appHeightInches, "your height in inches")
LimitAttach(document.application, document.application.image1.value);
LimitAttach(document.application, document.application.image2.value);
LimitAttach(document.application, document.application.image3.value);
LimitAttach(document.application, document.application.image4.value);
LimitAttach(document.application, document.application.image5.value);
LimitAttach(document.application, document.application.image6.value);
LimitAttach(document.application, document.application.image7.value);
if (imgCount<5){morePhotos=(5-imgCount);theErrors = (theErrors + ("You must select " + morePhotos + " more photos for upload\n\n"))}

if (theErrors=="") {alert("You are about to begin uploading your files. Please be patient, depending on your connection speed and file sizes, this may take awhile.  While your files are uploading it will look as if nothing is happening on your browser page...be patient.\n\nOnce your upload is complete you will be taken to a success page, at which point your upload will be complete.\n\nClick OK to begin upload.");alert("REMINDER: If you are not redirected to a page that tells you your application was successful, we did not receive your application.  In which case, please contact support.  Good luck!");var elem = document.getElementById('uploading');elem.style.display = 'block';return true} else{alert(theErrors);return false}

}