/*
  -------------------------------------------------------------------------
	                Validation Script in JavaScript
                                Version 1.0
             JavaScript Code for Family Friendly Homes Website
                             Brisbane - AUSTRALIA
	
	http://www.familyfriendlyhomes.com.au/
    -------------------------------------------------------------------------  
*/
/************************************************************************************
*This file contains various validation functions to test text boxes from a form for
*correct input.
************************************************************************************/

//-----------------------------------variables---------------------------------------

//array of various white space characters
var whitespace = " \t\n\r";
//-----------------------------------functions--------------------------------------

/*************************************isEmpty()**************************************
*this tests for an empty string
*pre: the string to be tested
*post: Returns true if the given string is empty.
*************************************************************************************/
function isEmpty(s)
{
        return ((s == null) || (s.length == 0));
}

/*************************************isWhitespace()**********************************
*tests for whitespace characters in the string
*pre: the string to be tested.
*post: Returns true if the given string contains no text.
**************************************************************************************/
function isWhitespace(s) {
        var i;
        
        if (isEmpty(s))
                return true;
        
        for (i = 0; i < s.length; i++)
        {
                var c = s.charAt(i);
                if (whitespace.indexOf(c) == -1) 
                        return false;   
        }
        
        return true;
}
/*************************************isNum()**********************************
*tests for whitespace characters in the string
*pre: the string to be tested.
*post: Returns true if the given string contains no currency.
**************************************************************************************/
function isNum(s) {
	var valid="0123456789.";
	var ok=1; var checktemp;
	for (var i=0; i<s.length; i++)	{
		checktemp = ""+s.substring(i, i+1);
		if (valid.indexOf(checktemp) == "-1" )
			 return 0;
	}
        
        return 1;
}
/***************************************isNumeric()**********************************
*tests for a numeric string
*pre: the string to be tested.
*post:Returns True if the given string is a price.
*************************************************************************************/
function isNumeric(s)
{
        var i
                
        for (i = 0; i < s.length; i++)
        {
                if ( s.charAt(i) < "0" || s.charAt(i) > "9" )
                        return false;
        }
        
        return true;
}
/***************************************isInString()**********************************
*tests for a valid Invoice No string
*pre: the string to be tested.
*post:Returns True if the given string is a valid string.
*************************************************************************************/
function isInString(s)
{
   var valid="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	var ok=1; var checktemp;
	for (var i=0; i<s.length; i++)	{
		checktemp = ""+s.substring(i, i+1);
		if (valid.indexOf(checktemp) == "-1" )
			 return 0;
	}
        
        return 1;
}
/************************************************************************************
*This file contains various validation functions to test Email box from a form for
*correct input.
************************************************************************************/

//-----------------------------------functions--------------------------------------
 
function doupper(obj) 
{ 
obj.value=(((obj.value.substr(0,1)).toUpperCase())+obj.value.substr(1,obj.value.length)) 
} 

/*************************************isValidEmail ()**************************************
*this tests for an Email string
*pre: the string to be tested
*post: Returns true if the given string is not valid.
*************************************************************************************/

function isValidEmail (s) { 
	emailObj=s.t1;
	emailStr = s;
	if(emailStr == "") 
	{ 
	    alert ( "Please fill in the 'Email Address' box." ); 
	    return false; 
	} 
	var checkTLD=0; 
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/; 
	var emailPat=/^(.+)@(.+)$/; 
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]"; 
	var validChars="\[^\\s" + specialChars + "\]"; 
	var quotedUser="(\"[^\"]*\")"; 
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/; 
	var atom=validChars + '+'; 
	var word="(" + atom + "|" + quotedUser + ")"; 
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$"); 
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"); 
	var matchArray=emailStr.match(emailPat); 
	if (matchArray==null) { 
		alert("The Email Address Is Invalid"); 
		return false; 
	} 
	var user=matchArray[1]; 
	var domain=matchArray[2]; 
	for (i=0; i<user.length; i++) { 
		if (user.charCodeAt(i)>127) { 
			alert("The Username Contains Invalid Characters."); 
		return false; 
		} 
	} 
	for (i=0; i<domain.length; i++) { 
		if (domain.charCodeAt(i)>127) { 
			alert("This Domain Name Contains Invalid Characters."); 
		return false; 
		} 
	} 
	if (user.match(userPat)==null) { 
		alert("The Username Is Invalid."); 
	return false; 
	} 
	var IPArray=domain.match(ipDomainPat); 
	if (IPArray!=null) { 
		for (var i=1;i<=4;i++) { 
			if (IPArray[i]>255) { 
				alert("The Destination IP Address Is Invalid."); 
	return false; 
	} 
	} 
	return true; 
	} 
	var atomPat=new RegExp("^" + atom + "$"); 
	var domArr=domain.split("."); 
	var len=domArr.length; 
	for (i=0;i<len;i++) { 
		if (domArr[i].search(atomPat)==-1) { 
			alert("The Domain Name Is Invalid."); 
	return false; 
	} 
	} 
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) { 
		alert("The Domain Name Extension Is Invalid"); 
	return false; 
	} 
	if (len<2) { 
		alert("The Address Is Missing A Hostname."); 
	return false; 
	} 
	return true;
} 
var SUB_YET=0; /* this keeps it from ever submitting after the first time*/
/***************************************form_validator()**********************************
*tests for an inputed value
*pre: the string to be tested.
*post:Returns True if the inputted value is valid
*************************************************************************************/
function formbooking_validator()
{
   //test Invoice Number field for white space
       if (isWhitespace(document.frmBooking.dateIn.value) ) {
        
                alert("Required Information! \nPlease enter your check-in date");
                
                document.frmBooking.dateIn.focus();
                document.frmBooking.dateIn.select();
        }
      else if (isWhitespace(document.frmBooking.dateOut.value) ) {
        
                alert("Required Information! \nPlease enter your departure date");
                
                document.frmBooking.dateOut.focus();
                document.frmBooking.dateOut.select();
        }
        else if ( Date.parse(get_dateFormat(document.frmBooking.dateOut.value)) <= Date.parse( get_dateFormat(document.frmBooking.dateIn.value)) ) {
					alert("Check-out must be later than check-in! \nPlease enter your departure date");
                
					document.frmBooking.dateOut.focus();
					document.frmBooking.dateOut.select();
        }
        else if (isWhitespace(document.frmBooking.First_name.value) ) {
        
                alert("Required Information! \nPlease enter your First Name");
                
                document.frmBooking.First_name.focus();
                document.frmBooking.First_name.select();
        }
        else if (isWhitespace(document.frmBooking.Last_name.value)){
        
                alert("Required Information! \nPlease enter your Last Name");
                
                document.frmBooking.Last_name.focus();
                document.frmBooking.Last_name.select();
        }
        else if (isWhitespace(document.frmBooking.Phone.value)){
        
                alert("Required Information! \nPlease enter your contact phone number");
                
                document.frmBooking.Phone.focus();
                document.frmBooking.Phone.select();
        }
        else if (isWhitespace(document.frmBooking.Email.value)||
						!isValidEmail(document.frmBooking.Email.value)) {
        
                //alert("Invalid Email Address");
                
                document.frmBooking.Email.focus();
                document.frmBooking.Email.select();
       }
       else if (isWhitespace(document.frmBooking.Address_line1.value)){
        
                alert("Required Information! \nPlease enter your address");
                
                document.frmBooking.Address_line1.focus();
                document.frmBooking.Address_line1.select();
        }
        else if (isWhitespace(document.frmBooking.City.value)){
        
                alert("Required Information! \nPlease enter your City");
                
                document.frmBooking.City.focus();
                document.frmBooking.City.select();
        } 
        else if (isWhitespace(document.frmBooking.Post_code.value)){
        
                alert("Required Information! \nPlease enter your Post code");
                
                document.frmBooking.Post_code.focus();
                document.frmBooking.Post_code.select();
        }
		else
        {
            //all data entered is ok			  
			return true;
        }
        return false;

}
/***************************************get_dateFormat(str)**********************************
*pre: the string date to be converted.
*post:Returns date in yyyy/MM/dd format
*************************************************************************************/
function get_dateFormat(str) {
	var strArr = str.split("/");
	var nstr;
	nstr = strArr[2]+"/"+strArr[1]+"/"+strArr[0];
	return nstr;
}
//====================================================