function convertDate(str){

var month=str.substring(0,3);
var day = str.substring(4,6);
var year = str.substring(7);
var combined=month +" "+day+", "+year;
//alert(combined);

return combined;

}

// If the element's string matches the regular expression it is all numbers
function isNumeric(str){
	var numericExpression = /^[0-9]+$/;
	if(str.match(numericExpression)){
		return true;
	}else{
		
		return false;
	}
}

// EMail checker
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

/**
*
*  Javascript trim, ltrim, rtrim
*  http://www.webtoolkit.info/
*
*
**/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function trimAll(form){
	form.fname.value=trim(form.fname.value);
	form.lname.value=trim(form.lname.value);
	form.telno.value=trim(form.telno.value);
	form.faxno.value=trim(form.faxno.value);
	form.homeadd.value=trim(form.homeadd.value);
	form.email.value=trim(form.email.value);
	form.occupants.value=trim(form.occupants.value);
}
function validateForm(form){
	trimAll(form);

	//Validate first name
	if (trim(form.fname.value) == "") {
    		alert( "Please enter your first name." );
    		form.fname.focus();
    		return false ;
  	}

	//Validate last name
	if (trim(form.lname.value) == "") {
    		alert( "Please enter your last name." );
    		form.lname.focus();
    		return false ;
  	}

	//Validate telephone number
	if (trim(form.telno.value) == "") {
    		alert( "Please enter your phone number." );
    		form.telno.focus();
    		return false ;
  	}else{
		if(!isNumeric(form.telno.value)){
			alert("Please enter valid phone numbers only.");
			form.telno.focus();
			return false;
		}
	}
	//Validate fax no
	if(trim(form.faxno.value)!= ""){
	if(!isNumeric(form.faxno.value)){
			alert("Please enter valid fax numbers only.");
			form.faxno.focus();
			return false;
	}}
	//Validate home address
	if (trim(form.homeadd.value) == "") {
    		alert( "Please enter your home address." );
    		form.homeadd.focus();
    		return false ;
  	}

	//Validate email 
	if (trim(form.email.value) == "") {
    		alert( "Please enter your email." );
    		form.email.focus();
    		return false ;
  	}else{
		if(!isEmail(form.email.value)){
		
			alert("Please enter a valid email address.");
			form.email.focus();
			return false;
		}

	}

	//Validate roomtype
	if (trim(form.roomtype.value) == "none") {
    		alert( "Please select the type of room to reserve." );
    		form.roomtype.focus();
    		return false ;
  	}

	//Validate occupants
	if (trim(form.occupants.value) == "") {
    		alert( "Please enter the number of occupants." );
    		form.occupants.focus();
    		return false ;
  	}else{
		if(!isNumeric(form.occupants.value)){
			alert("Please enter numbers only for the occupants.");
			form.occupants.focus();
			return false;
		}
	}

	//validate date range
	//alert(Date.parse(convertDate(form.cindate.value)));
	//alert(Date.parse(convertDate(form.coutdate.value)));
	
	if(Date.parse(convertDate(form.cindate.value)) > Date.parse(convertDate(form.coutdate.value))){
 		alert("Invalid date range.");
 		return false;
	}

	//end
	return true;
}
