// JavaScript Document


// Validates the argument to be alphabetic characters, plus a few punctuation chars
var letters="abcdefghijklmnopqrstuvwxyz.-,";
var LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZ.-,";

function isAlpha(c) {
	// is c a String or a character?
	if(c.length>1) {
	  for(j=0;j<c.length;j++) {
		// call isAlpha recursively for each character
		alpha=isAlpha(c.substring(j,j+1));
		if(!alpha) return alpha;
	  }
	  return alpha;
	}
	else {
	  // if c is alpha return true
	  if(letters.indexOf(c)>=0 || LETTERS.indexOf(c)>=0) return true;
	  return false;
	}
}

var EmailBadCharsMsgs, EmailAddressIsGood;
// Validates a string to contain valid email characters.
var ValidEmailAddressCharacters="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz@$_-.";
function CheckStringForValidEmailAddressCharacters(c) {
	// is c a String or a character?
	if(c.length>1) {
	  for(j=0;j<c.length;j++) {
		// call isAlphaEmail recursively for each character
		valid = CheckStringForValidEmailAddressCharacters( c.substring(j,j+1) );
		if (EmailAddressIsGood && !valid) {
			EmailAddressIsGood = false;
		}
	  }
	  return EmailAddressIsGood;
	}
	else {
	  // if c is valid return true
	  if( ValidEmailAddressCharacters.indexOf(c) >= 0 ) {
	  	return true;
	  }
	  else {
	  	if (EmailBadCharsMsgs.length > 0) {
	  	  EmailBadCharsMsgs += ', '
		}
		if (c == ' ') {
		  EmailBadCharsMsgs += 'a space';
		}
	    else { 
		  EmailBadCharsMsgs +=  c;
		}
	     return false;
      }
	}
}

function isEmailAddressValid(c) {
	var posAtSign, posLastDot, allCharactersAreValid;
	EmailAddressIsGood = true; //optimistically assume the best
	
	var strC = new String(c);
	var probstringLength = valProblems.length;
	EmailBadCharsMsgs = '';
	posAtSign = strC.indexOf('@');
	posLastDot = -1;
	if (posAtSign >= 0) {
		posLastDot=strC.indexOf('.', posAtSign)
	}
	
    allCharactersAreValid = CheckStringForValidEmailAddressCharacters(c);
	if ( !allCharactersAreValid || posAtSign<0 || posLastDot<0 ) {
		valProblems += "\n\nThe email address '" + c + "' is invalid.";
		if ( !allCharactersAreValid ) {
			valProblems +=  "\n >>>  The following illegal character";
			if ( EmailBadCharsMsgs.length > 1)
				valProblems += "s were found: ";
			else
				valProblems += " was found: " ;
			valProblems +=  EmailBadCharsMsgs;
		}
		if ( posAtSign < 0 ) 
			valProblems += "\n >>>  The '@' character is missing.";
		else
			if ( posLastDot < 0)
				valProblems += "\n >>>  There is no period following the '@', as in '.com' or '.army.mil'";
		if (probstringLength < valProblems.length) {
			valProblems += '\n';
		}
		return false;
	}
	else
		return true;
}

// Validates a string to contain valid decimal numeric characters.
var Numbers="0123456789";
function isNumeric(c) {
	// is c a String or a character?
	if(c.length>1) {
	  for(j=0;j<c.length;j++) {
		// call isNumeric recursively for each character
		numeric=isNumeric(c.substring(j,j+1));
		if(!numeric) return numeric;
	  }
	  return numeric;
	}
	else {
	  // if c is numeric return true
	  if(Numbers.indexOf(c)>=0) return true;
	  return false;
	}
}


// Validates a string to contain valid zipcode characters.
var NumbersPlusDash="0123456789-";
function isZipCode(c) {
	// is c a String or a character?
	if(c.length>1) {
	  for(j=0;j<c.length;j++) {
		// call isNumeric recursively for each character
		numeric=isZipCode(c.substring(j,j+1));
		if(!numeric) return numeric;
	  }
	  return numeric;
	}
	else {
	  // if c is numeric or a dash return true
	  if(NumbersPlusDash.indexOf(c)>=0) return true;
	  return false;
	}
}

function IsZipCode(c) {
	return isZipCode(c) ;
}


function RemoveThisClassFromObject(strClassToRemove, strID) {
//alert("got to RemoveThisClassFromObject(" + strClassToRemove + ", " + strID + ")");
	var lengthOfClassToRemove = strID.length;
	var lengthOfOriginalClassString = document.getElementById(strID).className.length;
    var posOfClassToRemove =  document.getElementById(strID).className.indexOf(strClassToRemove);

//alert("1\n\nClass to remove: '" + strClassToRemove + "'" +
//	   "\nClass string from which to remove it: '" + document.getElementById(strID).className +"'" +
//	   "\nlengthOfClassToRemove = " + lengthOfClassToRemove +
 //      "\nlengthOfOriginalClassString = " + lengthOfOriginalClassString +
//	   "\nposOfClassToRemove = " + posOfClassToRemove);
	var strNewClassSet;
	if (posOfClassToRemove == -1) {
		//alert('1');

		//alert('The resulting Class setting for object of ID=' + strID + ' is now\n' +
//"'" + document.getElementById(strID).className + "', whose length is now " + 
//document.getElementById(strID).className.length); 

		return;  //not there, so we can't remove it.
	}
	if ( posOfClassToRemove == 0 ) {		
		//the class name we wish to remove resides at the beginning of the class string
		//Maybe the one we want to remove is the only one there. If so, just null the
		//string
		//alert('2');
		if (document.getElementById(strID).className == strClassToRemove) {
			//alert('3');
			document.getElementById(strID).className = "";
			//alert('4');
			return;
		}
		else {
			//alert('5');
			//it's the first class name in there, but not the only one, so
			//reset the string to it's present content with the undesired 
			//class name trimmed off.
			document.getElementById(strID).className = 
				document.getElementById(strID).className.substr(lengthOfClass+1);
			//alert('6');
			return;
		}	
	}
	
	//So, the class name isn't at the front of the string.  Build up a string 
	//thoat omits it from the current class, then assign that back in.
	//This gets the part before the one to be omitted.
//alert('7');
	strNewClassSet =  document.getElementById(strID).className.substr(0, posOfClassToRemove-1);
//alert('8');
	if ( posOfClassToRemove + lengthOfClassToRemove - 1 == lengthOfOriginalClassString) {
//alert('9');
		//if true, the class to remove is at the end of the string, so the new
		//class set is just everything up to, and not including the one to be
		//removed.
		strNewClassSet =  document.getElementById(strID).className.substr(0, posOfClassToRemove-1);
//alert('10');
	}
	else {
		//there's more stuff following the part we're removing, so we have to
		//join together the part before it and the part after it.
		//This gets the part before:
//alert('11');
		strNewClassSet =  document.getElementById(strID).className.substr(0, posOfClassToRemove-1);
		//This gets the part after:
//alert('12');
		strNewClassSet += " " +
			document.getElementById(strID).className.substr(
			   posOfClassToRemove+lengthOfClassToRemove, 
			   lengthOfOriginalClassString-(posOfClassToRemove+lengthOfClassToRemove) );
	}
	//alert("class string before mod was:" + document.getElementById(strID).className + "\nand after, it will be:" + strNewClassSet);
	document.getElementById(strID).className = strNewClassSet;
//alert('13 - made it all the way through');
}

function AddThisClassToObject(strClassToAdd, strID) { 
	var identity
	identity = document.getElementById(strID);

//alert('in AddThisClassToObject("' +strClassToAdd+'", "' + strID + '") and classname contains "' + identity.className + '"');
	if ( identity.className.indexOf(strClassToAdd) == -1 ) {
		if (identity.className.length == 0) {
		    //if it's the first class name in the string, don't include 
			//separator space.
//alert('determined that ' + strClassToAdd +' wasn\'t there already' + '\nthe classname length is ' + identity.className.length);
			
			identity.className += strClassToAdd;
		}
		else {
			//if other classes are present, include a space before the 
			//additional class name
			identity.className += " " + strClassToAdd;
		}
	}
//alert('The resulting Class setting for object of ID=' + strID + ' is now\n' + "'" + identity.className + "', whose length is now " + identity.className.length); 
}

function MarkAsIncorrect(strID) {
	AddThisClassToObject( "InError", strID );
}

function MarkAsCorrect(strID) {
	RemoveThisClassFromObject( "InError", strID );
}
	
				
function HighlightInYellow(strID) {
	AddThisClassToObject( "InError", strID );
}

function RemoveHighlight(strID) {
	RemoveThisClassFromObject( "InError", strID );
}

function isEmailAddressValid(c, addressType) {
	var posAtSign, posLastDot, allCharactersAreValid;
	EmailAddressIsGood = true; //optimistically assume the best
	
	var strC = new String(c);
	var probstringLength = valProblems.length;
	EmailBadCharsMsgs = '';
	posAtSign = strC.indexOf('@');
	posLastDot = -1;
	if (posAtSign >= 0) {
		posLastDot=strC.indexOf('.', posAtSign)
	}
	
    allCharactersAreValid = CheckStringForValidEmailAddressCharacters(c);
	if ( !allCharactersAreValid || posAtSign<0 || posLastDot<0 ) {
		valProblems += "\n\nThe " + addressType + " email address '" + c + "' is invalid.";
		if ( !allCharactersAreValid ) {
			valProblems +=  "\n >>>  The following illegal character";
			if ( EmailBadCharsMsgs.length > 1)
				valProblems += "s were found: ";
			else
				valProblems += " was found: " ;
			valProblems +=  EmailBadCharsMsgs;
		}
		if ( posAtSign < 0 ) 
			valProblems += "\n >>>  The '@' character is missing.";
		else
			if ( posLastDot < 0)
				valProblems += "\n >>>  There is no period following the '@', as in '.com' or '.army.mil'";
		if (probstringLength < valProblems.length) {
			valProblems += '\n';
		}
		return false;
	}
	else
		return true;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

