//############################################
function hooverItemOn(x)
//############################################
{
//alert(x);
//x.backgroundColor='#edffff';
x.backgroundColor='#00FFFF';
x.color='#000000';
}


//############################################
function hooverItemOff(x,y)
//############################################
{
//alert(x);
if(y != null)
	{
	x.backgroundColor=y;
	}
	else
	{
	x.backgroundColor='#ffffff';
	}
x.color='#000000';
}



//############################################
function getFormFieldName(fieldName)
//############################################
{
// assumes all Form Input Names start with one of the following
//  1. txt
//  2. opt
//  3. requiredtxt
//  4. requiredopt
//  then strips out all underscores (_) and replaces with a space so that "First_Name" becomes "First Name"
if (fieldName.substring(0,11)=="requiredtxt" || fieldName.substring(0,11)=="requiredopt")
	{
        fieldName=fieldName.substring(11);
        }
        else if(fieldName.substring(0,3)=="txt" || fieldName.substring(0,3)=="opt")
	{
        fieldName=fieldName.substring(3);
        }

var t = /_/g; 
fieldName = fieldName.replace(t," ");
return fieldName;
}

//############################################
function trimString(sInString)
//############################################
{
 sInString = sInString.replace( /^\s+/g, "" );// strip leading
 sInString = sInString.replace( /\s+$/g, "" );// strip trailing
 return sInString
}




//############################################
function checkForm(which)
//############################################
{
var passOK = false;
passOK = checkrequired(which);
if(passOK == true)
  {
  //AUSD 
  passOK = checkPhones();
  }

if(passOK == true)
  {
      for (i=0; i<which.elements.length; i++) 
        {
        which.elements[i].disabled = false;
        }
  }

return passOK
}


//############################################
function checkrequired(which)
//############################################
{
  var pass=true;
  var passLen = true;
  var optionalOK = false;
  

  if (document.images)
    {
    for (i=0;i<which.length;i++)
      {
      var tempobj=which.elements[i];


// this if affects all text fields in form -- not just those that are "required"
if (tempobj.type=="text")
{
// change multiple spaces to a single space
tempobj.value = tempobj.value.replace(/\s+/g," ");
}
                       
      if (tempobj.name.substring(0,8)=="required")
	{


        strDate = tempobj.name;
        if (strDate.indexOf("Date") > 0)
          {
          var d=parseDate(tempobj.value);
            if(d==null)
             {
             pass=false;
	     tempobj.focus();
	     tempobj.select();
             break;
             } 
          }
       

	if ((tempobj.type=="text"||tempobj.type=="password" ) && (tempobj.value==""))
          {	
          pass=false;
	  tempobj.focus();
	  break;
          }
        if  (tempobj.name.substring(8,11) == "txt" && tempobj.value.length == 0 )
          {	
          pass=false;
	  tempobj.focus();
	  break;
          }
        if  (tempobj.name.substring(8,11) == "txt" && tempobj.value.length <2 )
	  {
          passLen=false;
          tempobj.focus();
          break;
          }
        if ((tempobj.name.substring(11,8) == "opt") && (tempobj.options[tempobj.selectedIndex].value == 0))
          {
	  pass=false;
	  tempobj.focus();
	  break;
	  }
	}

    }
 }




 if (!pass || !passLen )
   {

   shortFieldName=tempobj.name.substring(11);
   //shortFieldName = shortFieldName.replace("_"," ");
   var t = /_/g; 
   shortFieldName = shortFieldName.replace(t," ");


   if (!pass)
     {
     alert("Please enter a "+shortFieldName+".");
     }

   if (!passLen)
     {
     alert(shortFieldName+" must be at least 2 characters.");
     }

 
   return false;
   }
   else
   {
   return true
  }
}


//############################################
function PCase(STRING)
//############################################
{
var strReturn_Value = "";
var iTemp = STRING.length;
if(iTemp==0)
  {
  return"";
  }
var UcaseNext = false;
strReturn_Value += STRING.charAt(0).toUpperCase();
for(var iCounter=1;iCounter < iTemp;iCounter++)
  {
  if(UcaseNext == true)
    {
    strReturn_Value += STRING.charAt(iCounter).toUpperCase();
    }
    else
    {
    strReturn_Value += STRING.charAt(iCounter).toLowerCase();
    }
  var iChar = STRING.charCodeAt(iCounter);
  if(iChar == 32 || iChar == 45 || iChar == 46)
    {
    UcaseNext = true;
    }
    else
    {
    UcaseNext = false
    }
 if(iChar == 99 || iChar == 67)
    {
    if(STRING.charCodeAt(iCounter-1)==77 || STRING.charCodeAt(iCounter-1)==109)
      {
      UcaseNext = true;
      }
    }
} //End For
return strReturn_Value;
} //End Function


//############################################
function checkEmail (what) 
//############################################
{
strng = trimString(what.value);
commonName = getFormFieldName(what.name)
var error="";
var goodEmail = strng.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

if (!(goodEmail)) 
    { 
       error = commonName + " is not a valid email address.\n";
    }
if (strng == "")
    {
    error = ""
    }
    
if(error != "")
      {
      what.focus();
      alert(error);
      what.select();
     }
return false;
}


//############################################
function checkPhone (what) 
// strip out delimiters and check for 10 digits
//############################################
{
strng=what.value
commonName = getFormFieldName(what.name)
var error = "";

var phoneNumber = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(phoneNumber)) && phoneNumber.length != 0) {
       error = commonName + " contains invalid characters.";
  
    }
     else if (!(phoneNumber.length == 10 || phoneNumber.length == 0)) 
       {
       error = commonName +" is the wrong length. Please enter a 3 digit area code and 7 digit Phone Number\n";
       } 
  if (phoneNumber.length == 10)
      {
        phoneNumber = phoneNumber.substring(0,3) + " " + phoneNumber.substring(3,6) + "-" + phoneNumber.substring(6,10);
        what.value=phoneNumber;
      }
if(error != "")
      {
      what.focus();
      alert(error);
      what.select();
     }
return false;
}




//############################################
function checkPassword (strng) 
// between 6-8 chars, uppercase, lowercase, and numeral
//############################################
{
var error = "";
if (strng == "") {
   error = "You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    



//############################################
function checkLoginName (strng, commonName, allFields)
// 4-10 chars, uc, lc, and underscore only.
//############################################
{
//alert("string: " + strng);
var error = "";
var illegalChars = /\W/; // allow letters, numbers, and underscores
if (strng == "" )
   {
   error = "Please enter your " + commonName +".\n";
   }
   else if ((strng.length < 4) || (strng.length > 10))
       {
       error = "The username is the wrong length.\n";
       }
       else if (illegalChars.test(strng))
          {
          error = "The username contains illegal characters.\n";
          } 
if (allFields == true)
  {
  return error;
  }
  else if (error != "")
  {
  alert(error);
  }
return error;
}       


//############################################
function checkProperName (what, long) 
//############################################
{
strng = trimString(what.value);
//alert(strng.length);
what.value = strng;
commonName = getFormFieldName(what.name)
var error = "";
var illegalChars = /[^A-Za-z.,\-\s]/; // allow only letters, period, comma, hypen, space


if (illegalChars.test(strng))
  {
  error = commonName + " includes non-alpha characters.\n";
  alert(error);
  what.focus();
  what.select();
  }
  else
  {
  checkLength(what,long)
  }
//what.focus()
//return error;
return false;
}       

//############################################
function checkAddress (what) 
//############################################
{
strng = trimString(what.value);
what.value = strng;
commonName = getFormFieldName(what.name)
var error = "";
var illegalChars = /[^A-Za-z0-9#.,\-\s]/; // allow only letters, numbers, Number Sign, period, comma, hypen, space
if(strng != "")
  {
var reqChars = /\s/; // at least one space is required

if (!(reqChars.test(strng)))
      {
      error = commonName + " must contain a space.\n";
      } 
if (illegalChars.test(strng))
      {
      error = commonName + " includes invalid characters.\n";
      } 
if(error != "")
      {
      what.focus();
      alert(error);
      //what.focus();
      what.select();
     //return false
     }
//what.focus();
}
return false;
}       

//############################################
function checkCity (what) 
//############################################
{
strng = trimString(what.value);
what.value = strng;
commonName = getFormFieldName(what.name)
var error = "";
var illegalChars = /[^A-Za-z.,\-\s]/; // allow only letters, period, comma, hypen, space
if (illegalChars.test(strng))
          {
          error = "The " + commonName + " can not contain non-alpha characters.\n";
          } 
if (error != "")
  {
  alert(error);
  what.focus();
  what.select();
  return false
  }
  else
  {
  checkLength(what,4)
  }
//what.focus()
return error;
}       


//############################################
function isEmpty(strng) 
// non-empty textbox
//############################################
{
var error = "";
  if (strng.length == 0) {
     error = "The mandatory text area has not been filled in.\n"
  }
return error;	  
}

//############################################
function isDifferent(strng) 
// was textbox altered
//############################################
{
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}


//############################################
function checkSelect(what, commonName, allFields) 
// valid selector from dropdown list
//############################################
{
choice = what.selectedIndex;
var error = "";
  if (choice == 0) 
    {
    error = "Please select " + commonName + ".\n";
    }    
  if (allFields == true)
    {
    if (error != "") 
      {
      what.focus();
      }
    return error;
    }
  else if (error != "")
    {
    alert(error);
    what.focus();
    return false;
    }
//what.focus();
return error;
}    


//############################################
function checkZipCode (what)
//############################################
{
//strng = what.value;
strng = trimString(what.value);
what.value = strng;
commonName = getFormFieldName(what.name)
var error = "";
//var stripped = strng.replace(/[^0-9]/g, ''); //strip out non-numeric characters
if (strng != "")
   {
     reZip = new RegExp(/(^\d{5}$)|(^\d{9}$)|(^\d{5}-\d{4}$)/);
 
     if (!reZip.test(strng))
       {
          //alert("Zip Code Is Not Valid");
          //return false;
          error = commonName + " must be 5 or 9 digits.\n";
       }
       else if (strng.length==9)
         {
         //alert(strng);
         what.value = strng.substr(0,5) + "-" + strng.substr(5,4)
         }
   }
if (error != "")
  {
  alert(error);
  what.focus();
  what.select();
  return false;
  }
//what.focus();
return error;
}    


//############################################
function checkLength(what, long)
//############################################
{
error="";
strng = what.value;
commonName = getFormFieldName(what.name)

if (strng !="" &&   strng.length < long )
  {
  error = commonName + " must be at least " + long + " characters.\n";
  } 

if (error != "")
  {
  alert(error);
  what.focus();
  what.select();

  return false
  }

//what.focus();
return error;
}       



//############################################
function checkTab(e) 
//############################################
{
  if(e.which==13){
    var aFld = e.target;
    var elem = aFld.form.elements;
    for(var i=0;i<elem.length;i++){
      if(elem[i]==aFld){
        if(i<elem.length-1){
          elem[i+1].focus();
        } else {
          elem[0].focus();
        }
      }
    }
    return false;
  } else {
    if(e.keyCode==13){
      e.keyCode = 9;
    }
  }
  return true;
}