// h4s.js
//
// H4S Script Library

var dtCh = "/";
var minYear = 1800;
var maxYear = 2100;
var wordcheck = new Array(13);
wordcheck[0] = " shit";
wordcheck[1] = " fuck";
wordcheck[2] = " ass";
wordcheck[3] = " cunt";
wordcheck[4] = " pussy";
wordcheck[5] = " cock";
wordcheck[6] = " cum";
wordcheck[7] = " penis";
wordcheck[8] = " nuts";
wordcheck[9] = " fag";
wordcheck[10] = " bitch";
wordcheck[11] = " sex";
wordcheck[12] = " nigger";

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function validateDate(theField)
{
    if( theField.value == "" )
        return true;
    else if (isDate(theField.value)==false)
    {
        theField.focus();
        return false;
    }
    return true;
 }


function verifyFields(theForm)
{
  if(theForm.FirstName.value.length < 1 || theForm.LastName.value.length < 1 ||
    theForm.Street1.value.length < 1 || theForm.City.value.length < 1 || theForm.State.value.length < 1 ||
    theForm.ZipCode.value.length < 1 || theForm.HomeAreaCode.value.length < 1 || theForm.HomeNumber.value.length < 1 ||
    theForm.Email.value.length < 1 || theForm.Password.value.length < 1 || theForm.passwordverify.value.length < 1 ||
	theForm.DefaultCollegeID.value.length < 2 || theForm.agree_to_terms.value.length < 1)
  {
    alert('Sorry - but one or more required fields are blank.  Please try again.');
    return false;
  }
  else if(theForm.Password.value != theForm.passwordverify.value)
  {
    alert('Sorry - but the password and verify password must match.  Please try again.');
	theForm.Password.focus();
    return false;
  }
  else if(checkProfanity(theForm.FirstName.value) || checkProfanity(theForm.LastName.value) || checkProfanity(theForm.Street1.value) || checkProfanity(theForm.City.value) || checkProfanity(theForm.Email.value) )
  {
    alert('Sorry - profanity not allowed.');
    return false;
  }
  else if(!theForm.agree_to_terms.checked)
  {
    alert('Sorry - but you must review and check the Terms of Service.');
    return false;
  }
  else if( validateEmail( theForm.Email ) == false )
  {
	return false;
  }
  else if( validatePhone( theForm ) == false )
  {
	return false;
  }

  return true;
}

function validateNumber(theField)
{
  var strng = theField.value;

  if(strng.search(/[^0-9\.]+/) != -1)
  {
    alert('Sorry - this field may only contain numbers!');
    theField.focus();
	return false;
  }

  return true;
}

function validateDesc(theField)
{
  var strng = theField.value;

  if( checkProfanity( strng ) == true )
  {
    alert('Sorry - profanity not allowed.');
    theField.focus();
  }
  else if(strng.length > 750)
  {
    alert('Sorry - this field is too long.  Please delete some text (max. 750).');
    theField.focus();
  }

}

function validateEmail(theField)
{
  var strng = theField.value;

  if( checkProfanity( strng ) == true )
  {
    alert('Sorry - profanity not allowed.');
    theField.focus();
	return false;
  }
  else if(strng.search(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/) )
  {
    alert('Sorry - you must enter a valid email address!');
    theField.focus();
	return false;
  }
  return true;
}

function validatePhone(theForm)
{
  var sNumber = theForm.HomeNumber.value;

  if(validateNumber(theForm.HomeAreaCode) == false)
  {
    return false;
  }
  
  if(sNumber.search(/[^0-9\.\-]+/) != -1)
  {
    alert('Sorry - this field may only contain numbers!');
    theForm.HomeNumber.focus();
	return false;
  }
  
  if(sNumber.indexOf("-") < 0)
  {
    alert('Sorry - this field must contain a dash (-) in between the numbers.');
    theForm.HomeNumber.focus();
	return false;
  }
  
  return true;
}

function checkProfanity( theString )
{
    for( i=0; i<wordcheck.length; i++)
    {
        if( theString.toLowerCase().indexOf( wordcheck[i] ) > -1 )
            return true;
    }
    return false;
}

function validateProfanity( theField )
{
    var strng = theField.value;
    if( strng != "" && checkProfanity(strng) == true )
    {
        alert('Sorry - profanity not allowed.');
        theField.focus();
    }
}