var whitespace = " \t\n\r";
var specialChar = "'-"
var holdSchoolID;
var bdayHold;

function setFocus(focusFld) {
  if ( ! isFocused ) {
     fFld = focusFld;
     isFocused = 1;
  }
}

function trimString(userInput){      
	var iStart, iEnd;      
	var sTrimmed;      
	var cChar;      
	
	iEnd = userInput.length - 1;      
	iStart = 0;      
	bLoop = true;      
	cChar = userInput.charAt(iStart);      

	//HANDLE SINGLE CHARACTER STRINGS
	if ( iStart == iEnd ) {
		if ((cChar == "\n") || 
			(cChar == "\r") ||                                
			(cChar == "\t") || 
			(cChar == " ")) {
			return "" ;
		} else {
			return cChar ; 
		}
	}
	
	while ((iStart < iEnd) && 
				((cChar == "\n") || 
				(cChar == "\r") ||                                
				(cChar == "\t") || 
				(cChar == " "))) {         
		iStart ++;      
		cChar = userInput.charAt(iStart);      
	}   	   

	cChar = userInput.charAt(iEnd); 
    while ((iEnd >= 0) && 
				((cChar == "\n") || 
				(cChar == "\r") ||                            
				(cChar == "\t") || 
				(cChar == " "))) {         
		iEnd --;         
		cChar = userInput.charAt(iEnd);      
	}      

	if (iStart < iEnd){         
		sTrimmed = userInput.substring(iStart, iEnd + 1);      
	} else {         
		if ( userInput.length == 1 ) { 
			sTrimmed = userInput ;
		} else {
			sTrimmed = "";      
		}
	}      

	return sTrimmed;   
}


/****************************************************************/

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

/****************************************************************/

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
/****************************************************************/

function isValidEmail(s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

/****************************************************************/


function MyValidation(myForm)									
{	

		var strField = RTrim(document[myForm].Email.value);

		if (isWhitespace(strField) || (RTrim(document[myForm].Email.value) == "")) {
			alert("You need to enter information for the Email Field");
			document[myForm].Email.focus();
			document[myForm].Email.select();
			document[myForm].Email.style.background = "#99CCFF";
			document[myForm].Email.style.color = "#000000";
			return false;
		}
	
		if (!isValidEmail(strField)) {
			alert("Please enter a valid email address.");
			document[myForm].Email.focus();
			document[myForm].Email.select();
			document[myForm].Email.style.background = "#99CCFF";
			document[myForm].Email.style.color = "#000000";
			return false;
		}
		
		if(!check_email(strField)) {
			alert("Please enter a valid email address.");
			document[myForm].Email.focus();

			if(document.all || document.getElementByID){
				document[myForm].Email.style.background = "#99CCFF";
				document[myForm].Email.style.color = "#000000";
			}
			return false;
		}


return true;
	
}	


/****************************************************************/

// Checks to see if a required field is blank.  If it is, a warning
// message is displayed...

function ForceEntry(objField, FieldName)
{

	var strField = new String(objField.value);
	if (isWhitespace(strField)) {
		alert("You need to enter information for " + FieldName);
		objField.focus();
		objField.select();
		return false;
	}

	return true;
}
		
/****************************************************************/


function isNumeric(str)
// returns true if str is numeric
// that is it contains only the digits 0-9
// returns false otherwise
// returns false if empty
{
  var len= str.length;
  if (len==0)
    return false;
  //else
  var p=0;
  var ok= true;
  var ch= "";
  while (ok && p<len)
  {
    ch= str.charAt(p);
    if ('0'<=ch && ch<='9')
      p++;
    else
      ok= false;
  }
  return ok;
}




/****************************************************************/

// Right trims the string...  Useful for SQL datatypes of CHAR

function RTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

/****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace(s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function checkBx(field) {
		if (field.checked != true) {
			alert("Please check off that you have read the rules and regulations for the contest.");
			return true;
		}
}

function checkselect(field) {
       var selectchecked=true;
       for (x=1;x<field.length;x++) {
			if (field[x].selected) {
				selectchecked=false;
				holdSchoolID = field.options[field.selectedIndex].text;
			}
       }
       if (selectchecked) {
			return true;
		}
   }



function checkBDay(field) {

       		for (x=1;x<field.length;x++) {
			if (field[x].text == "") {
				return true;
			}
       		}

   }

// returns true if str is alphabetic with addition of characters in plus
// that is only A-Z a-z or space or any of the characters in the string plus
// returns false otherwise
// returns false if empty
	   
function isAlphabeticPlus(str)

{
	var len= str.length;
	
	if ((len==0) || (len<2)) {
		return false;
	}
	
	var p=0;
	var ok= true;
	var ch= "";
	var flagChar = false;
	var flagCt = 0
	
	while (ok && p<len)
	{
		ch= str.charAt(p);
		if ( (('A'<=ch && ch<='Z') || ('a'<=ch && ch<='z')) || (('-' == ch || "'" == ch) && (flagCt < 2)) ) {
			if ('-' == ch || "'" == ch) {
				flagCt++;
			}
			p++;
			if ( flagCt > 1 ) {
				ok = false;
			}
		} 
		else {
		  ok = false;
		}
	}
	return ok;
}

/* PURPOSE:  Since we are using the single tick mark as the
	string delimiter to construct our SQL queries, a string with
	a tick mark in it will cause a SQL error.  Therefore we replace
	all "'" with "''", which eliminates the possibility of a SQL error.
*/

function sqlSafe(s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	return new_s;
}

/****************************************************************/

function makeSafe(i)
{
	i.value = sqlSafe(i.value);
}
   

function check_email(e) {

	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
			return (false);
			}	
		} 

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		} 
	}
}
