var errors,first;

function maxlength(textare,len) {
     var q = textare.value.length;
     var r = q - len;
     var msg = "Sorry, you have input "+q+" characters into the "+
       "text area box you just completed. It can return no more than "+
	   len + " characters to be processed. Please abbreviate "+
       "your text by at least "+r+" characters. If you submit as is, "+
	   "the last "+r+" characters will be discarded.";
     if (q > len) alert(msg);
}

function setMenu(dd,val) {
	for (i=0; i<dd.length; i++)
		if (dd.options[i].value == val)
			dd.selectedIndex = i;
}


function checkNull(textobj) {
	var checker = trim(textobj);
	
	return ((checker == "") || (checker == null));
}

function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function checkLength(textobj,len) {
	return textobj.length != len;
}

function checkEmail(textobj) {
	val=true;
	if (!checkNull(textobj)) {
		p=textobj.indexOf('@');
		val = (p<1 || p==(val.length-1));
	}
	return val;
}

function checkPhone(imag,box1,box2,box3) {
	var comm=false;
	if (checkNull(box1) || checkNull(box2) || checkNull(box3))
		comm=true;
	else if (checkLength(box1,3) || checkLength(box2,3) || checkLength(box3,4))
		comm=true;
	else if (isNaN(box1) || isNaN(box2) || isNaN(box3))
		comm=true;
	swapKnown(imag,comm);
}

function swapKnown(nam,corr) { // Calls swap image for specific controls
	
	if (corr) {
		if (!errors)
			if (nam == 'img_Application')
				first='ProjectTitle';
			else if (nam == 'img_Curriculum')
				first='Goals';
			else if (nam == 'img_DateStart' || nam == 'img_DateEnd')
				first='ProjectTitle';
			else if (nam == 'img_County' || nam == 'img_Location')
				first='NameFirst';
			else if (nam == 'img_AZLearnsLabel')
				first='LanguagesSpecified';
			else if (nam == 'img_ProjectServiceArea')
				first='ProjectAreaStudents';
			else if (nam == 'img_Amount')
				first='RequestedPersonnel';
			else if (nam == 'img_password')
			    first='pwd';
			else if (nam == 'img_retype')
			    first='Rpwd';
			else if (nam == 'img_answer')
			    first='answer';
			else if (nam == 'img_personal')
			    first='personal';		
			else if (nam =='img_selectquestion')
			    first='selectquestion';
			else if (nam =='img_newpassword')
			    first='newpwd';
			else if (nam =='img_GrantNumber')
			    first='GrantNumber';
				
			else if (nam =='img_Inputs')
			    first='Inputs';		
			else if (nam =='img_Outputs')
			    first='Outputs';
			else if (nam =='img_Evaluation')
			    first='Evaluation';
			else if (nam =='img_Email')
			    first='Email';
			else if (nam =='img_Startdate')
			    first='startdate';		
			else if (nam =='img_Startmonth')
			    first='startmonth';
			else if (nam =='img_Enddate')
			    first='enddate';
			else if (nam =='img_Endmonth')
			    first='endmonth';
			else if (nam =='img_Startyear')
			    first='startyear';
			else if (nam =='img_Endyear')
			    first='endyear';
			else if (nam =='img_ProjectName')
			    first='ProjectName';
			else
				first=nam.substring(4);
		errors=true;
		MM_swapImage(nam,'','file:///E|/includes/cross',1);
	}
	else 
		MM_swapImage(nam,'','file:///E|/includes/trns',1);
}

function restoreAll() { // Restores all cross.gif to trns.gif - called from 'Reset'
	var theimages=document.images
	imgTemp = new Image();
	imgTemp.src = cross;
	for (i=0;i<theimages.length;i++)
		if (theimages[i].src==imgTemp.src) theimages[i].src=trns
}

