var errorLabelList = '';
var errorIDList = '';
var errorDisplayList = '';

var cmMonthArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function isBlank(inputObj) { 
 for(var x = 0; x < inputObj.value.length; x++) {    
  if(inputObj.value.charCodeAt(x) != 32 & inputObj.value.charCodeAt(x) != 13 & inputObj.value.charCodeAt(x) != 10) {
   return false;
  }  
 }
 return true;
}

function isValidEmail(inputObj) {
 if(inputObj.value.indexOf('@') == -1) {	
  return false;
 }
 else {
  var tmp1 = inputObj.value.split("@");  
  if(tmp1[1].indexOf(".") == -1) {
	
	return false;  
  }  
  var tmp2 = tmp1[1].split(".");  
  if(tmp1[0].length < 1 || tmp1[0].length < 2 || tmp2[1].length < 2) {
	return false;  
  }  
 }
 return true;
}

function isNumeric(inputObj) {
 if(isNaN(inputObj.value) || isBlank(inputObj)) {
  return false;
 }
 else {
  return true;
 }
}

function getSelectedRadioObj(radioObj) {  
 for(var x = 0; x < radioObj.length; x++) {
  if(radioObj[x].checked) {   
   return radioObj[x].id;
  }  
 }
 return false;
}

function selectRadioObj(radioObj,val) {
 for(var x = 0; x < radioObj.length; x++) {  
  if(radioObj(x).value == val) {   
   radioObj(x).checked=true;
  }  
 } 
}

function isValidZip(inputObj) {
 if(isNumeric(inputObj) && inputObj.value.length == 5) {   
  return true;
 }
 else {
  return false;
 }
}

function isValidFullZip(inputObj) {}

function isValidPhone(format,inputObj) {
 // format - 0 = (xxx) xxx-xxxx
 // format - 1 = xxx xxx-xxxx
 // format - 2 = xxx.xxx.xxxx
 // format - 3 = xxx-xxxx
 switch(format) {
  case 0: {
   if(inputObj.value.indexOf('(') == -1 || inputObj.value.indexOf(')') == -1 || inputObj.value.indexOf('-') == -1 || inputObj.value.length !=14) {
    return false;
   }
   else {
    return true;
   }
  }
 }
}

function isValidDate(month,day,year) { 
 if(year < 1900 || year > 9999 || isNaN(year)) { 
  return 'year';
 }
 if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
  cmMonthArray[1] = "29";
 } 
 if(month < 1 || month > 12 || isNaN(month)) { 
  return 'month';
 } 
 if(day < 1 || day > cmMonthArray[(month-1)] || isNaN(day)) {  
  return 'day';
 }
 return true;
}

function isValidMonth(month) {
 if(isNaN(month) || month < 1 || month > 12) {
  return false;
 }
 return true;
}

function isValidDay(day, month) {
 if(isNaN(day) || day < 1 || day > cmMonthArray[(month-1)]) {  
  return false;
 }
 return true;
}

function isValidYear(year) {
 var date = new Date();
 var topYear = date.getFullYear()*1+1;
  
 if(isNaN(year) || year < 2006 || year > topYear) {   
  return false;
 }
 return true;  
}

function isStrictAlphaNumeric(inputObj) {  
 for(var x = 0; x < inputObj.value.length; x++) {
  if((inputObj.value.charCodeAt(x) >= 48 && inputObj.value.charCodeAt(x) <= 57) || (inputObj.value.charCodeAt(x) >= 65 && inputObj.value.charCodeAt(x) <= 90) || (inputObj.value.charCodeAt(x) >= 97 && inputObj.value.charCodeAt(x) <= 122) || (inputObj.value.charCodeAt(x) == 45 || inputObj.value.charCodeAt(x) == 95)) {
  }
  else {
   return false;
  }   
 }
 return true;
}

function isAlphaNumeric(inputObj) {  
 for(var x = 0; x < inputObj.value.length; x++) {
  if((inputObj.value.charCodeAt(x) >= 48 && inputObj.value.charCodeAt(x) <= 57) || (inputObj.value.charCodeAt(x) >= 65 && inputObj.value.charCodeAt(x) <= 90) || (inputObj.value.charCodeAt(x) >= 97 && inputObj.value.charCodeAt(x) <= 122) || (inputObj.value.charCodeAt(x) == 45 || inputObj.value.charCodeAt(x) == 95 || inputObj.value.charCodeAt(x) == 32)) {
  }
  else {
   return false;
  }   
 }
 return true;
}

function isAlphaNumericList(inputObj,delimeter) {
 if(!delimeter) { var delimeter = ','; }
 for(var x = 0; x < inputObj.value.length; x++) {
  if((inputObj.value.charCodeAt(x) >= 48 && inputObj.value.charCodeAt(x) <= 57) || (inputObj.value.charCodeAt(x) >= 65 && inputObj.value.charCodeAt(x) <= 90) || (inputObj.value.charCodeAt(x) >= 97 && inputObj.value.charCodeAt(x) <= 122) || (inputObj.value.charCodeAt(x) == 45 || inputObj.value.charCodeAt(x) == 95 || inputObj.value.charCodeAt(x) == 32) || (inputObj.value.charAt(x) == delimeter)) {
  }
  else {
   return false;
  }   
 }
 return true;
}

function validatelogin() {
	if (isBlank(document.loginForm.username)) {
		alert('Please supply a username');
		document.loginForm.username.focus();
		return false;
  }
	if (isBlank(document.loginForm.password)) {
		alert('Please supply a password');
		document.loginForm.password.focus();
		return false;
  }  
}	

function validateRemember() {
	if (isBlank(document.loginForm.username)) {
		alert('Please supply a username');
		document.loginForm.username.focus();
		return false;
  }
}	

function clearError() {
   // Clear Errors

   if (errorLabelList != '') {
   	 var errArray = errorLabelList.split(',');
   	 for (i=0; i < errArray.length; i++) { 
   	 	if (document.getElementById(errArray[i])) { 	 	
   	 	  document.getElementById(errArray[i]).style.border = '0px none';
   	 	}
   	 }   	

   	 document.getElementById('errorMessages').innerHTML = '';
   	 document.getElementById('errorMessages').style.display = 'none';
   	 
   	 errorLabelList = '';   	
   	 errorIDList = '';
   	 errorDisplayList = '';      	  
   }	
}

function setErrors(eLst,eLabelLst,eDispLst) {

  if (eLst != '') {
   	var errDisplay = '<strong>The following errors have occurred:</strong><ul>';
   	errIDArray = eLst.split(',');
   	errLabelArray = eLabelLst.split(',');
   	errDisplayArray = eDispLst.split(',');

   	for (i=0; i < errLabelArray.length; i++) {   		
   		document.getElementById(errLabelArray[i]).style.border='1px solid #FF0000';	 
   		errDisplay = errDisplay + '<li>' + errDisplayArray[i] + '</li>';
   	}   	
   	errDisplay = errDisplay + '</ul>';
   	
   	document.getElementById('errorMessages').innerHTML = errDisplay;
   	document.getElementById('errorMessages').style.display = 'block';
   	return 1;
  }
  return 0;
}  



function validateSurvey() {
	clearError();

	if (document.surveyForm.transitionAwards.value > 5 || document.surveyForm.transitionAwards.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'transistionAwards';
   	  errorLabelList = errorLabelList + 'transistionAwardsLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Transistion Awards must be rated between 1 and 5.';  
	}
	  
	if (document.surveyForm.bridgingGrants.value > 5 || document.surveyForm.bridgingGrants.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'bridgingGrants';
   	  errorLabelList = errorLabelList + 'bridgingGrantsLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Bridging Grants must be rated between 1 and 5.';  
	}
	  
 	if (document.surveyForm.advancedResearchFellowships.value > 5 || document.surveyForm.advancedResearchFellowships.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'advancedResearchFellowships';
   	  errorLabelList = errorLabelList + 'advancedResearchFellowshipsLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Advanced Research Fellowships must be rated between 1 and 5.';  
	}
	
	if (document.surveyForm.studentFellowships.value > 5 || document.surveyForm.studentFellowships.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'studentFellowships';
   	  errorLabelList = errorLabelList + 'studentFellowshipsLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Student Fellowships must be rated between 1 and 5.';  
	}
	if (document.surveyForm.pilotGrants.value > 5 || document.surveyForm.pilotGrants.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'pilotGrants';
   	  errorLabelList = errorLabelList + 'pilotGrantsLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Pilot Grants must be rated between 1 and 5.';  
	}
	
	if (document.surveyForm.seniorFunding.value > 5 || document.surveyForm.seniorFunding.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'seniorFunding';
   	  errorLabelList = errorLabelList + 'seniorFundingLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Senior Funding must be rated between 1 and 5.';  
	}
	
	if (document.surveyForm.smallScaleFunding.value > 5 || document.surveyForm.smallScaleFunding.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'smallScaleFunding';
   	  errorLabelList = errorLabelList + 'smallScaleFundingLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Small Scale Funding must be rated between 1 and 5.';  
	}
	
	if (document.surveyForm.outcomesFunding.value > 5 || document.surveyForm.outcomesFunding.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'outcomesFunding';
   	  errorLabelList = errorLabelList + 'outcomesFundingLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Outcomes Funding must be rated between 1 and 5.';  
	}
	
	if (document.surveyForm.trainingFunding.value > 5 || document.surveyForm.trainingFunding.value < 1) {
     if (errorIDList != '') {
   	 	  errorIDList = errorIDList + ',';
   	 	  errorLabelList = errorLabelList + ',';
   	 	  errorDisplayList = errorDisplayList + ',';
   	  }   	      	   	
   	  errorIDList = errorIDList + 'trainingFunding';
   	  errorLabelList = errorLabelList + 'trainingFundingLabel';   	   	
   	  errorDisplayList = errorDisplayList + 'Training Funding must be rated between 1 and 5.';  
	}
	
   if (setErrors(errorIDList,errorLabelList,errorDisplayList)) {
   	return false;
   }

//  document.getElementById('surveyForm').submit();
}



function validateMeeting() {
	clearErrors();

	if (isBlank(document.getElementById('firstName'))) {
		setError('firstName','Please fill in your First Name');
	}
	if (isBlank(document.getElementById('lastName'))) {
		setError('lastName','Please fill in your Last Name');
	}
	if (isBlank(document.getElementById('phone'))) {
		setError('phone','Please fill in your Office Phone');
	}
	if (isBlank(document.getElementById('email'))) {
		setError('email','Please fill in your Email');
	}
	else if (!isValidEmail(document.getElementById('email'))) {
		setError('email','Please fill out your Email in the correct format, ie <i>name@company.com</i>');
	}
	if(!getSelectedRadioObj(document.getElementById('eventForm').gender)) {
		setError('gender','Please select your Gender');
	}

	if (formHasErrors()) { displayErrorMessages(); return false; }
	else return true;

}

