/* author: Mike F Schulz */

/******************************************************/
/******************************************************/
function isValidDate( month, day, year ) {

  	var myDayStr = day;
	var myMonthStr = month-1;
	var myYearStr = year;

	/* Using form values, create a new date object
	using the setFullYear function */
	var myDate = new Date();
	myDate.setFullYear( myYearStr, myMonthStr, myDayStr );
	
	if ( myDate.getMonth() != myMonthStr ) {
	  return false;
	} else {
	  return true;	  
	}
}

/******************************************************/
/******************************************************/
function is18YearsOrOlder( month, day, year, minYears ) {

	var now = new Date();
	month = month -1;
	nowDay = now.getDate();
	nowMonth = now.getMonth();
	nowYear = now.getFullYear();
	
	if((nowMonth > month)||(nowMonth==month & nowDay>=day)) {
		age = nowYear-year
	} else {	
		year = parseInt(year) + 1;	
		age = nowYear-year;
	}
	
	if( age >= minYears ) return true;
	else return false;

}

/******************************************************/
/******************************************************/
function validateAge(form, dayValue, monthValue, yearValue ) {
		
		
	form.agecheck.value = '';
	if(dayValue == '' || dayValue == 'DD') {
		alert("Please enter your birthday DAY using digits in DD format, e.g. 01 or 22");
		document.agescreener.dd.focus();
		return false;
	}	
	if( containsDigits ( dayValue ) ) {
		alert("Please enter your birthday DAY using digits only");
		document.agescreener.dd.focus();
		return false;
	}		
	if(dayValue.length != 2 ) {
		alert("Please enter your birthday DAY using digits in DD format, e.g. 01 or 22");
		document.agescreener.dd.focus();
		return false;
	}	
	
	if(monthValue == '' || monthValue == 'MM') {
		alert("Please enter your birthday MONTH using digits in MM format, e.g. 02 or 11");
		document.agescreener.mm.focus();
		return false;
	}	
	if( containsDigits ( monthValue ) ) {
		alert("Please enter your birthday MONTH using digits only");
		document.agescreener.mm.focus();
		return false;
	}
	if(monthValue.length != 2 ) {
		alert("Please enter your birthday MONTH using digits in MM format, e.g. 02 or 11");
		document.agescreener.mm.focus();
		return false;
	}	
	
	if(yearValue == '' || yearValue == 'YYYY') {
		alert("Please enter your birthday YEAR using digits in YYYY format, e.g. 1982");
		document.agescreener.yyyy.focus();
		return false;
	}	
	if( containsDigits ( yearValue ) ) {
		alert("Please enter your birthday YEAR using digits only");
		document.agescreener.yyyy.focus();
		return false;
	}
	if(yearValue.length != 4 ) {
		alert("Please enter your birthday YEAR using digits in YYYY format, e.g. 1982");
		document.agescreener.yyyy.focus();
		return false;
	}			
			
	if( ! isValidDate( monthValue, dayValue, yearValue ) ) {
		alert("The date you have specified is invalid, please try again");
		document.agescreener.dd.focus();
		return false;		
	}
	
	if( ! is18YearsOrOlder( monthValue, dayValue, yearValue, 18 ) ) {
		alert("You need to be 18 years of age or older!");
		document.agescreener.dd.focus();
		return false;		
	}

	form.agecheck.value = yearValue+'-'+monthValue+'-'+dayValue;

	return true;
}

/******************************************************/
/******************************************************/



/* author: Mike F Schulz */

/**************************************************
**************************************************/
function containsCharacter(str, ch) {
	for (k=0;k<str.length;k++) {
		if(str.charAt(k) == ch) return true;
	}
	return false;	
}
	
/**************************************************
**************************************************/	
function containsChars(xvar) {
	for (k=0;k<xvar.length;k++) {
		if(isNaN(xvar.charAt(k))) return true;
	}
	return false;			
}

/**************************************************
**************************************************/	
function containsDigits(xvar) {
	for (k=0;k<xvar.length;k++) {
		if(isNaN(xvar.charAt(k))) return true;
	}
	if(!isNaN(xvar) && xvar < 1) return true;
	return false;			
}

/**************************************************
**************************************************/	
function isFloat(xvar) {
	for (k=0;k<xvar.length;k++) {
		if(isNaN(xvar.charAt(k)) && xvar.charAt(k) != '.') return false;
	}
	return true;		
}
		
/**************************************************
**************************************************/		
function isInteger(xvar) {
	for (k=0;k<xvar.length;k++) {
		if(xvar == "-") return false;
		if(isNaN(xvar.charAt(k)) && xvar.charAt(k) != '-') return false;
	}
	return true;		
}			
	
/**************************************************
**************************************************/	
function checkCellPostFix(el) {
	if(el.value.length != 7) return false;
	for(k=0;k<el.value.length;k++) {
		if(isNaN(el.value.charAt(k))) return false;
	}
	return true;
}		
		
/**************************************************
**************************************************/		
function isCellphone(xvar) {
	if(xvar.length != 10) { 
		alert("Cell Phone number must contain 10 digits");
	 	return false;
	}
	
	if(	!(xvar.substr(0, 3) == "082" || 
		xvar.substr(0, 3) == "083" ||
		xvar.substr(0, 3) == "084" ||
		xvar.substr(0, 3) == "072" ||
		xvar.substr(0, 3) == "073") ) {
			alert("Cell Phone number must begin with 082, 083, 084, 072, or 073");
			return false;		   		
   	}
	
	for (k=0;k<xvar.length;k++) {
		if(xvar == "-") return false;
		if(isNaN(xvar.charAt(k)) && xvar.charAt(k) != '-') { 
			alert("Cell Phone number must contain only digits");
			return false;
		}
	}
	return true;				
}
		
/**************************************************
**************************************************/				
function isWhitespace (s, msg) {
	var i;
	// whitespace characters
	var whitespace = " \t\n\r";
			
	// Is s empty?
	if (isEmpty(s)) {
		alert(msg); 
		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.
	alert(msg);
	return true;
}		
	
/**************************************************
**************************************************/				
function isWhitespaceNoMsg (s) { 
	var i;
	// whitespace characters
	var whitespace = " \t\n\r";

	// 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;
}

/**************************************************
**************************************************/										
function isEmpty(s) {   
	return ((s == null) || (s.length == 0));
}		
	
/**************************************************
**************************************************/					
function isSignedFloat (s) {

	if (isEmpty(s)) { 
       if (isSignedFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedFloat.arguments[1] == true);
    } else {
        var startPos = 0;
        var secondArg = false;

        if (isSignedFloat.arguments.length > 1) secondArg = isSignedFloat.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") ) startPos = 1;    
        return (isFloat(s.substring(startPos, s.length), secondArg))
    }
}	
			
/**************************************************
**************************************************/										
function valEmail(el) {
	var invalidChars = " /:,;";
 	if (el.value == "") {
   		alert("Please enter a Email Address.");
		el.focus();
		return (true);
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
    	if (el.value.indexOf(badChar,0) != -1) {
		   	alert("The Email Address contains an invalid character, please correct it.");
			el.focus();
			return (true);
	    }
	}
	atPos = el.value.indexOf("@",1);
	if (atPos == -1) {
	   	alert("The Email Address must contain an @ character.");
		el.focus();
		return (true);
	}
	if (el.value.indexOf("@",atPos+1) != -1) {
	   	alert("The Email Address must have letters before the @ character.");
		el.focus();
		return (true);
	}
	periodPos = el.value.indexOf(".",atPos);
	if (periodPos == -1) {
	   	alert("The Email Address must contain a . character.");
		el.focus();
		return (true);
	}
	if (periodPos+3 > el.value.length) {
	   	alert("The Email Address must have letters after the . character.");
		el.focus();
		return (true);
	}
	return (false);
}
