//This function makes sure a particular memo field is not too long.
//document.thisform.thiselement.value.length and thiselement maxSize are passed in
function funMemoValidate(formElementLength, maxSize)
{
	if (formElementLength > maxSize)
	{
		var tmpString = 'Sorry! This field must be under ' + maxSize + ' characters.  You have ' + formElementLength + ' characters.  Any additional text will be cutoff when submitting the form.';
		alert(tmpString);
		return false;
	}
	return true;
}

//This function makes sure a particular memo field is numeric.
//document.thisform.thiselement.value.length and thiselement maxSize are passed in
function funNumericCheck(formElement)
{
	//this function goes through and checks each character
	//to see if its numeric
	for (var n = 0; n < formElement.value.length; n++)
	{
		if (formElement.value.substring(n, n+1) < "0" || formElement.value.substring(n, n+1) > "9")
		{
			var tmpString = 'Sorry! This field must contain a number!';
			//alert(tmpString);
			formElement.focus();
			return false;
			
		}
		
	}
	return true;
}



//function to make sure they entered a vaild email address, to be determined by the presence of the @ symbol
function funEmailCheck(formElement)
{
	//this function goes through and checks each character
	//to see if it is the @ sign
	for (var n = 0; n < formElement.value.length; n++)
	{
		if (formElement.value.substring(n, n+1) == "@")
		{
			return true;			
		}		
	}
			var tmpString = 'Sorry! This field must contain valid email address!';
			alert(tmpString);
			formElement.focus();
			return false;	
}



//Javascript field validation functions


function funValidateForm(theForm, university){
var reason = "";
var strSel = "";
var tr24status = "";
var test = "test";
var divPos = "";
var trPos = "";


var find1 = "d";
var find2 = "tr";
var email = "Email";
var guests = "Guest Notification";
var guests2 = "E-mail Graduation Announcements";
var guests3 = "Guest Email Addresses";
var guests4 = "Family and Friends Email Addresses";
var country = "Hometown Country";
var telephone = "Telephone Number";
//var phonetic = "Phonetic";

var match1 = 0;
var match2 = 0;
var divLength = 0;


for(i=0; i<theForm.elements.length; i++){

if(Left(theForm.elements[i].id,3) == 'req') {


test = theForm.elements[i].id;

match1 = test.search(find1);
match2 = test.search(find2);
divLength = match2-match1;


divPos = Right(test,test.length-3);
trPos = Right(test, test.length-3-divLength);
divPos = Left(divPos, divPos.length-trPos.length);

}


  		
  		if (Left(theForm.elements[i].id,3) == 'req' && document.getElementById(trPos).style.display != 'none' && document.getElementById(divPos).style.display != 'none') {
  			
	  		//alert(theForm.elements[i].title + '\n divPos = ' + divPos + ' divDisplay = ' + document.getElementById(divPos).style.display + '\n trPos = ' + trPos + ' trDisplay = ' + document.getElementById(trPos).style.display);
	  		
	  		if (theForm.elements[i].type == 'checkbox') {
	  			var tryme = theForm.elements[i].name;
	  			
	  		}
	  		
	  		
	  		if (theForm.elements[i].type == 'select-one') {
	  			reason += funCheckSelect(theForm.elements[i]);
	  			
	  		}
	  		
	  		if (theForm.elements[i].title  == 'yesnooption' && document.getElementById(test).style.display != 'none') {
	  			if (theForm.elements[i].selectedIndex == 0) {
	  				reason += "Option not chosen!";
	  			}
	  			tr24status += theForm.elements[i].title;
	  			tr24status = Left(tr24status,3);
	  		}
  		
	  		
	  		if (theForm.elements[i].type == 'text' || theForm.elements[i].type == 'textarea') {
	  			if(String(theForm.elements[i].title).search(email) != -1){
					//alert(theForm.elements[i].title);
					reason += funCheckEmail2(theForm.elements[i]);
	  			}
	  			else {
	  				reason += funCheckTextContent(theForm.elements[i]);
	  			}
	  		}
		}
		
		//CHECK MULTIPLE EMAIL ENTRY BOX
		if (String(theForm.elements[i].title).search(guests) != -1 || String(theForm.elements[i].title).search(guests2) != -1 || String(theForm.elements[i].title).search(guests3) != -1 || String(theForm.elements[i].title).search(guests4) != -1) {
			//alert('test23');
			if (university == 'utep') {
				reason += CheckMultiEmailMax5(theForm.elements[i]);
			}
			else {
				reason += CheckMultiEmail(theForm.elements[i]);
			}
		}
				
		//CHECK COUNTRY DROPDOWN TO MAKE SURE THEY CHOSE A VALID OPTION
	  	if(String(theForm.elements[i].title).search(country) != -1){
				//alert('test23');
				reason += funHispanicCountryValidate(theForm.elements[i]);
	  		}
	  	
	  	//CHECK TELEPHONE NUMBER TO MAKE SURE IT IS VALID
	  	if(String(theForm.elements[i].title).search(telephone) != -1){
				//alert('test23');
				reason += funTelephoneValidate(theForm.elements[i]);
	  		}

		//IF THEY HAVE A VALUE IN ANOTHER EMAIL FIELD THAT IS NOT REQUIRED CHECK THAT
	  	if(Left(theForm.elements[i].id,3) != 'req' && String(theForm.elements[i].title).search(email) != -1 && String(theForm.elements[i].title).search(guests) == -1 && String(theForm.elements[i].title).search(guests2) == -1 && String(theForm.elements[i].title).search(guests3) == -1 && String(theForm.elements[i].title).search(guests4) == -1 ){
				//alert('test23');
				reason += funCheckEmail3(theForm.elements[i]);
	  		}
	  		
 }
 
      
  if (reason != "") {
    alert("Please correct the following:\n" + reason );
    return false;
  }

  return true;
}


function funValidateFormSLS(theForm){
var reason = "";
var strSel = "";
var tr24status = "";
var test = "test";
var divPos = "";
var trPos = "";


var find1 = "d";
var find2 = "tr";
var email = "Email";
var guests = "Guest Notification";
var country = "Hometown Country";

var match1 = 0;
var match2 = 0;
var divLength = 0;


for(i=0; i<theForm.elements.length; i++){

if(Left(theForm.elements[i].id,3) == 'req') {


test = theForm.elements[i].id;

match1 = test.search(find1);
match2 = test.search(find2);
divLength = match2-match1;


divPos = Right(test,test.length-3);
trPos = Right(test, test.length-3-divLength);
divPos = Left(divPos, divPos.length-trPos.length);

}


  		
  		if (Left(theForm.elements[i].id,3) == 'req' && document.getElementById(trPos).style.display != 'none' && document.getElementById(divPos).style.display != 'none') {
  			
	  		//alert(theForm.elements[i].title + '\n divPos = ' + divPos + ' divDisplay = ' + document.getElementById(divPos).style.display + '\n trPos = ' + trPos + ' trDisplay = ' + document.getElementById(trPos).style.display);
	  		
	  		if (theForm.elements[i].type == 'checkbox') {
	  			var tryme = theForm.elements[i].name;
	  			
	  		}
	  		
	  		
	  		if (theForm.elements[i].type == 'select-one') {
	  			reason += funCheckSelect(theForm.elements[i]);
	  			
	  		}
	  		
	  		if (theForm.elements[i].title  == 'yesnooption' && document.getElementById(test).style.display != 'none') {
	  			if (theForm.elements[i].selectedIndex == 0) {
	  				reason += "Option not chosen!";
	  			}
	  			tr24status += theForm.elements[i].title;
	  			tr24status = Left(tr24status,3);
	  		}
  		
	  		
	  		if (theForm.elements[i].type == 'text' || theForm.elements[i].type == 'textarea') {
	  			if(String(theForm.elements[i].title).search(email) != -1){
					reason += funCheckEmail2(theForm.elements[i]);
	  			}
	  			else {
	  				reason += funCheckTextContent(theForm.elements[i]);
	  			}
	  		}
		}
		
		//CHECK MULTIPLE EMAIL ENTRY BOX
		if(String(theForm.elements[i].title).search(guests) != -1){
				//alert('test23');
				reason += CheckMultiEmail(theForm.elements[i]);
	  		}
	  	
	  	//CHECK COUNTRY DROPDOWN TO MAKE SURE THEY CHOSE A VALID OPTION
	  	if(String(theForm.elements[i].title).search(country) != -1){
				//alert('test23');
				reason += funHispanicCountryValidate(theForm.elements[i]);
	  		}
	  	
 }
 
   //This section is to validate the multiple email addresses entered
	//reason += checkMultiEmail(document.getElementsByName('frmfldNote2'));
	//checkMultiEmail();
      
  if (reason != "") {
    alert("Please correct the following:\n" + reason );
    return false;
  }

  return true;
}

function funValidateFormWharton(theForm){
var reason = "";
var strSel = "";
var tr24status = "";
var test = "test";
var divPos = "";
var trPos = "";


var find1 = "d";
var find2 = "tr";
var email = "Email";

var match1 = 0;
var match2 = 0;
var divLength = 0;


for(i=0; i<theForm.elements.length; i++){

if(Left(theForm.elements[i].id,3) == 'req') {


test = theForm.elements[i].id;

match1 = test.search(find1);
match2 = test.search(find2);
divLength = match2-match1;


divPos = Right(test,test.length-3);
trPos = Right(test, test.length-3-divLength);
divPos = Left(divPos, divPos.length-trPos.length);

}


  		
  		if (Left(theForm.elements[i].id,3) == 'req' && document.getElementById(trPos).style.display != 'none' && document.getElementById(divPos).style.display != 'none') {
  			
	  		//alert(theForm.elements[i].title + '\n divPos = ' + divPos + ' divDisplay = ' + document.getElementById(divPos).style.display + '\n trPos = ' + trPos + ' trDisplay = ' + document.getElementById(trPos).style.display);
	  		
	  		if (theForm.elements[i].type == 'checkbox') {
	  			var tryme = theForm.elements[i].name;
	  			
	  		}
	  		
	  		
	  		if (theForm.elements[i].type == 'select-one') {
	  			reason += funCheckSelect(theForm.elements[i]);
	  			
	  		}
	  		
	  		if (theForm.elements[i].title  == 'yesnooption' && document.getElementById(test).style.display != 'none') {
	  			if (theForm.elements[i].selectedIndex == 0) {
	  				reason += "Option not chosen!";
	  			}
	  			tr24status += theForm.elements[i].title;
	  			tr24status = Left(tr24status,3);
	  		}
  		
	  		
	  		if (theForm.elements[i].type == 'text' || theForm.elements[i].type == 'textarea') {
	  			if(String(theForm.elements[i].title).search(email) != -1){
					reason += funCheckEmail2(theForm.elements[i]);
	  			}
	  			else {
	  				reason += funCheckTextContent(theForm.elements[i]);
	  			}
	  		}
		}
 }
 
  //This section is to validate Wharton Checkboxes
	reason += checkWhartonCheckboxes();
	
  //This section is to validate numeric only entries
  reason +=  WhartonGuestCheck();
      
  if (reason != "") {
    alert("Please correct the following:\n" + reason );
    return false;
  }

  return true;
}

function funValidateFormBarrett(theForm){
var reason = "";
var strSel = "";
var tr24status = "";
var test = "test";
var divPos = "";
var trPos = "";


var find1 = "d";
var find2 = "tr";
var email = "Email";

var match1 = 0;
var match2 = 0;
var divLength = 0;


for(i=0; i<theForm.elements.length; i++){

if(Left(theForm.elements[i].id,3) == 'req') {

test = theForm.elements[i].id;

match1 = test.search(find1);
match2 = test.search(find2);
divLength = match2-match1;


divPos = Right(test,test.length-3);
trPos = Right(test, test.length-3-divLength);
divPos = Left(divPos, divPos.length-trPos.length);

}

  		
  		if (Left(theForm.elements[i].id,3) == 'req' && document.getElementById(trPos).style.display != 'none' && document.getElementById(divPos).style.display != 'none') {
  			
  			
	  		if (theForm.elements[i].type == 'checkbox') {
	  			var tryme = theForm.elements[i].name;
	  			
	  		}
	  		
	  		
	  		if (theForm.elements[i].type == 'select-one' && theForm.elements[i].name != 'frmfldOption88') {
	  			reason += funCheckSelect(theForm.elements[i]);
	  			
	  		}
	  		
	  		if (theForm.elements[i].title  == 'yesnooption' && document.getElementById(test).style.display != 'none') {
	  			if (theForm.elements[i].selectedIndex == 0) {
	  				reason += "Option not chosen!";
	  			}
	  			tr24status += theForm.elements[i].title;
	  			tr24status = Left(tr24status,3);
	  		}
  		
	  		
	  		if (theForm.elements[i].type == 'text' || theForm.elements[i].type == 'textarea') {
	  			if(String(theForm.elements[i].title).search(email) != -1){
					reason += funCheckEmail2(theForm.elements[i]);
	  			}
	  			else {
	  				reason += funCheckTextContent(theForm.elements[i]);
	  			}
	  		}
	  		
	  		if (theForm.elements[i].name == 'frmfldOption13' || theForm.elements[i].name == 'frmfldOption15') {
	  			reason += funBarretDirectorDepartment(theForm.elements[i]);
	  			
	  		}
	  		
		}
 }
 
 //This section is to validate Barrett Checkboxes
	reason += checkBarretCheckboxes();

  if (reason != "") {
    alert("Please correct the following:\n" + reason + "\nDo not hit the back button on your browser; scroll up and update the sections highlighted in yellow.");
    return false;
  }

  return true;
}

//string functions left and right
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

//check whether one option is checked for required checkboxes
function checkWhartonCheckboxes() {

	 //This section is to validate Wharton Checkboxes
	 var sel = [];
	 var error = "";
		
	 var strSel = "";
	 sel = getSelected(document.getElementsByName('frmfldText34'));
	 for (var item in sel)       
		   strSel += sel[item].value + ", ";
	 if (strSel.length == 0) {
	 	error += "Please select at least one option for Future Plans.\n";
	 }

return error;

}

//check whether one option is checked for required checkboxes
function checkBarretCheckboxes() {

	 //This section is to validate Barrett Checkboxes
	 var sel = [];
	 sel = getSelected(document.getElementsByName('frmfldText22'));
	 var strSel = "";
	 var error = "";
		
	 for (var item in sel)       
		   strSel += sel[item].value + ", ";
	 if (strSel.length == 0) {
	 	error += "Please select at least one option for Scholarships.\n";
	 }
	 
	 var strSel = "";
	 sel = getSelected(document.getElementsByName('frmfldText24'));
	 for (var item in sel)       
		   strSel += sel[item].value + ", ";
	 if (strSel.length == 0) {
	 	error += "Please select at least one option for Academic Awards.\n";
	 }
	 
	 var strSel = "";
	 sel = getSelected(document.getElementsByName('frmfldText27'));
	 for (var item in sel)       
		   strSel += sel[item].value + ", ";
	 if (strSel.length == 0) {
	 	error += "Please select at least one option for Honors Societies.\n";
	 }
	 
	 var strSel = "";
	 sel = getSelected(document.getElementsByName('frmfldText34'));
	 for (var item in sel)       
		   strSel += sel[item].value + ", ";
	 if (strSel.length == 0) {
	 	error += "Please select at least one option for Future Plans.\n";
	 }
	 
	 var strSel = "";
	 sel = getSelected(document.getElementsByName('frmfldText48'));
	 for (var item in sel)       
		   strSel += sel[item].value + ", ";
	 if (strSel.length == 0) {
	 	error += "Please select at least one option for\n  What was [were] the best experience[s] you had as a Barrett student?.\n";
	 }
	 
	 
	 if (strSel.search("Other") > 0){
	 	var entry = document.getElementById('rd24tr85');
		
		if(entry.value != "") {
			entry.style.background = 'White';
		}else{
			entry.style.background = 'Yellow';
			error = "Please fill in the Other field.\n";
			entry.focus();
		}
	  }
	 

return error;

}

//Check for numeric input for number of guests for Wharton MBA
function WhartonGuestCheck() {
	var numericExpression = /^[0-9]+$/;
	var error = "";
	var entry = document.getElementById('rd0tr18');

	if(entry.value.length > 0){
		if(entry.value.match(numericExpression)){
			entry.style.background = 'White';
		}else{
			entry.style.background = 'Yellow';
			error = "Please only use the 0-9 characters for Number of Guests.\n";
			entry.focus();
		}
	}

return error;

}

//Check that a list of email addresses entered are all valid
function CheckMultiEmail(elem){
    var error="";
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
   
   	var emailList = elem.value.replace(/,/g,';').split(';');
   	
	if(emailList[0] != "") { 
	   for (var i = 0; i < emailList.length; i++) {
	   
	   //alert(emailList[i].replace(/^\s+|\s+$/g, ''));
	   
	   	
	   	if(emailList[i].replace(/^\s+|\s+$/g, '').match(emailExp)) {
	   	//alert('valid email');
	   	elem.style.background = 'White';
	   	}else{
	   	elem.style.background = 'Yellow';
	   	//alert('invalid email');
	   	error = emailList[i].replace(/^\s+|\s+$/g, '') + " is not a valid email address.\n";
	   	elem.focus();
	   	break;
	   	}
	   
	   }
	}

	
    return error;
}

//Check that a list of email addresses entered are all valid -- ERROR IF THERE ARE MORE THAN 5 EMAIL ADDRESSES
function CheckMultiEmailMax5(elem){
    var error="";
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
   
   	var emailList = elem.value.replace(/,/g,';').split(';');
   	
   	if(emailList.length > 5) {
   	//alert('more than 5');
   		elem.style.background = 'Yellow';
   		error = "Please enter no more than 5 guest email addresses.\n";
	   	elem.focus();
   	}
   	
	if(emailList[0] != "" && error == "") { 
	   for (var i = 0; i < emailList.length; i++) {
	   
	   //alert(emailList[i].replace(/^\s+|\s+$/g, ''));
	   
	   	
	   	if(emailList[i].replace(/^\s+|\s+$/g, '').match(emailExp)) {
	   	//alert('valid email');
	   	elem.style.background = 'White';
	   	}else{
	   	elem.style.background = 'Yellow';
	   	//alert('invalid email');
	   	error = emailList[i].replace(/^\s+|\s+$/g, '') + " is not a valid email address.\n";
	   	elem.focus();
	   	break;
	   	}
	   
	   }
	}

	
    return error;
}

//Check that a list of email addresses entered are all valid
function CheckMultiEmail1() {
    var error="";
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    var entry = document.getElementById('rd16tr37');
	var emailList = entry.value.split(';');
	
	for (var i = 0; i < emailList.length; i++) {
		if(emailList[i].match(emailExp)) {
			error += "";
		}else{
		error = "One or more email addresses entered is invalid";
		break;
		}
	}
return error;
} 

//check for valid email address if they entered anything in a non-required email field
function funCheckEmail3(elem){
    var error="";
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
      
   	if(elem.value != ""){
		if(elem.value.match(emailExp)){
			elem.style.background = 'White';
		}else{
			elem.style.background = 'Yellow';
	        error = "Please enter a valid " + elem.title + ".\n";
	        elem.focus();
		}
	}else{
        elem.style.background = 'White';
	}
	
    return error;
}


//Check for numeric input
function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		return false;
	}
}



//brings up no email upload popup
function funCheckEmail2(elem){
    var error="";
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
   
   
   	if(elem.value.replace(/^\s+|\s+$/g, '').match(emailExp)){
		elem.style.background = 'White';
	}else{
		elem.style.background = 'Yellow';
        error = "Please enter a valid " + elem.title + ".\n";
        error = error + "   " + elem.value.replace(/^\s+|\s+$/g, '') + " is not a valid email address.\n";
        elem.focus();
	}
	
    return error;
}

//check for empty required text field
function funCheckTextContent(elem){
    var error="";
   
   	if(elem.value != ""){
		elem.style.background = 'White';
	}else{
		elem.style.background = 'Yellow';
        error = "Please fill in " + elem.title + ".\n";
        elem.focus();
	}
	
    return error;
}

//check for to make sure they selected a required option
function funCheckSelect(elem){
    var error="";
    var selected_index = elem.selectedIndex;
   
   
    if(elem.options[selected_index].text != ""){
		elem.style.background = 'White';
	}else{
		elem.style.background = 'Yellow';
        error = "Please select " + elem.title + ".\n";
        elem.focus();
	}
	
    return error;
}

//check to make sure they selected a valid option -- for Hispanic Country Dropdown
//Give error if they choose a campus dividing line
function funHispanicCountryValidate(elem){
    var error="";
    var selected_index = elem.selectedIndex;
   
   
    if(Left(elem.options[selected_index].text,1) != "-"){
		elem.style.background = 'White';
	}else{
		elem.style.background = 'Yellow';
        	error = "Please select a valid Country.\n";
        elem.focus();
	}
	
    return error;
}

//check telephone number to make sure it is 10 digits
function funTelephoneValidate(elem)
  {
    var FmtStr="";
    var index = 0;
    var LimitCheck;
    var error="";
    var PhoneNumber = elem.value

    LimitCheck = PhoneNumber.length;
    while (index != LimitCheck)
      {
        if (isNaN(parseInt(PhoneNumber.charAt(index))))
          { }
        else
          { FmtStr = FmtStr + PhoneNumber.charAt(index); }
        index = index + 1;
      }
    if (FmtStr.length == 10)
      {
        elem.style.background = 'White';
      }
    else
      {
        elem.style.background = 'Yellow';
        	error = "Please enter a 10-digit phone number.\n";
        elem.focus();
      }
    return error;
  }


//check for to make sure they selected a valid option -- for Barrett Thesis Director Dept.
//Give error if they choose a campus
//Also give error if they choose "Not Listed" and don't fill in next box.
function funBarretDirectorDepartment(elem){
    var error="";
    var selected_index = elem.selectedIndex;
   
   
    if(Left(elem.options[selected_index].text,1) != "-"){
		elem.style.background = 'White';
	}else{
		elem.style.background = 'Yellow';
		if (elem.name == 'frmfldOption13')
        	error = "Please select a valid Department.\n";
        if (elem.name == 'frmfldOption15')
        	error = "Please select a valid Co-Director Department.\n";
        elem.focus();
	}
	
	if(elem.options[selected_index].text == "Not Listed"){
		if (elem.name == 'frmfldOption13')
			var entry = document.getElementById('rd21tr50');
		
		if (elem.name == 'frmfldOption15')
			var entry = document.getElementById('rd21tr57');
		
		if(entry.value != "") {
			entry.style.background = 'White';
		}else{
			entry.style.background = 'Yellow';
			if (elem.name == 'frmfldOption13')
				error = "Please enter a Department.\n";
			if (elem.name == 'frmfldOption15')
				error = "Please enter a Co-Director Department.\n";
			entry.focus();
		}
					
	}
	
    return error;
}

