//var ck_businessname = /^[A-Za-z0-9\s!@#$%^&*()_]{3,60}$/;
//var ck_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
//var ck_username = /^[A-Za-z0-9_]{3,20}$/;
//var ck_password = /^[A-Za-z0-9!@#$%^&*()_]{6,20}$/;
//var ck_phone = /^[0-9-()\s]{10,17}$/;
//var ck_address = /^[A-Za-z0-9\-\'\s!@#$%^&*()_]{3,55}$/;
//var ck_city = /^[A-Za-z-\'\s!@#$%^&*()_]{3,35}$/;
//var ck_state = /^[A-Za-z-\s]{2,30}$/;
//var ck_zipcode = /^[0-9-\s]{5,11}$/;
//var ck_url = /^http:\/\/www\.[a-z]+\.(com)|(org)|(edu)|(net)$/;
//var ck_reviewlink = /^http:\/\/[a-z]+\.[a-z]+\.(com)|(org)|(edu)|(net)$/;
//var ck_names = /^[A-Za-z\']{3,20}$/;
//var ck_title_tagline = /^[0-9A-Za-z\-\'\s!@#$%^&*()_]{2,65}$/;
//var ck_thankyoumessage = /^[0-9A-Za-z\'\s!@#$%^&*()_]{7,80}$/;
//var ck_contactus_piwik = /^[0-9]{1,4}$/;
//var ck_keyword = /^[0-9A-Za-z\s!@#$%^&*()_]{3,65}$/;
//var ck_weekday = /^[0-9A-Za-z\s\!@#$%^&*()_]{4,35}$/;

var ck_zipcode = /^[0-9-\s]{5,11}$/;
var ck_sname = /^[A-Za-z\'\-\s.]{3,50}$/;
var ck_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
var ck_TEL = /^[0-9]{10,10}$/;
var ck_EXT = /^[0-9]{0,6}$/;

function validate_form(theForm){
var SZIP = theForm.SZIP.value;
var RZIP = theForm.RZIP.value;
var SNAME = theForm.SNAME.value;
var EMAIL = trim(theForm.EMAIL.value);

//var STELH = trim(form.STELH.value);
//var stelh_area = trim(theForm.area_code.value);
//var stelh_first3 = trim(theForm.xxx.value);
//var stelh_last4 = trim(theForm.xxxx.value);

var STELH = theForm.STELH.value;

var STELHEXT = trim(theForm.ext.value);

//var STELO = trim(form.STELO.value);
//var add_stelh_area = trim(theForm.area_code_2.value);
//var add_stelh_first3 = trim(theForm.xxx_2.value);
//var add_stelh_last4 = trim(theForm.xxxx_2.value);

var STELO = theForm.RTELH.value;

var DD = theForm.DD.value;
var MM = theForm.MM.value;
var YY = theForm.YY.value;
var ROOMS = theForm.ROOMS.value;
//var CMT = form.CMT.value;
var errors = [];




if (!ck_zipcode.test(SZIP)) {
errors[errors.length] = "Valid pickup zipcode";
}

if (!ck_zipcode.test(RZIP)) {
errors[errors.length] = "Valid delivery zipcode";
}

if (!ck_sname.test(SNAME)) {
errors[errors.length] = "Valid full name";
}

if (!ck_email.test(EMAIL)) {
errors[errors.length] = "Valid e-mail address";
}

/*
if (!ck_TEL.test(STELH)) {
errors[errors.length] = "valid 10-digit telephone number";
}
*/

if (STELH.length != 0) {
	var new_STELH=/\d/g;
	var new_STELH_1 = STELH.match(new_STELH);
	var new_STELH_2 = new_STELH_1.join('');

	/*
	var new_STELH_3 = new_STELH_2.substr(new_STELH_2.length-10,10);

	if(new_STELH_2.length > 10){
		errors[errors.length] = new_STELH_3;
	*/
	if(new_STELH_2.length == 10){
		//errors[errors.length] = new_STELH_2;
	}else{
		errors[errors.length] = 'Your phone number must be 10 digits';
	}
}else{
	errors[errors.length] = 'Please provide your contact number';
}

if(STELO != ''){
	var new_STELO=/\d/g;
	var new_STELO_1 = STELO.match(new_STELO);
	var new_STELO_2 = new_STELO_1.join('');

	/*
	var new_STELH_3 = new_STELH_2.substr(new_STELH_2.length-10,10);
	
	if(new_STELH_2.length > 10){
		errors[errors.length] = new_STELH_3;
	*/
	if(new_STELO_2.length == 10){
		//errors[errors.length] = new_STELH_2;
	}else{
		errors[errors.length] = 'Your additional phone number must be 10 digits';
	}
}

if (!STELHEXT.length == 0) {
	if (!ck_EXT.test(STELHEXT)) {
	errors[errors.length] = "Valid primary phone extention";
	}
}

/*
if (STELO.length != 0) {
	var new_STELO=/\d/g;
	var new_STELO_1 = STELO.match(new_STELO);
	var new_STELO_2 = new_STELO_1.join('');
*/
	/*
	var new_STELO_3 = new_STELO_2.substr(new_STELO_2.length-10,10);

	if(new_STELO_2.length > 10){
		errors[errors.length] = new_STELO_3;
	*/
/*
	if(new_STELO_2.length == 10){
		errors[errors.length] = new_STELO_2;
	}else{
		errors[errors.length] = new_STELO_2 + 'must be 10 digits';
	}
}else{
	errors[errors.length] = 'you may post an additional contact number';
}
*/
/*
if (!STELO.length == 0) {
	if (!ck_TEL.test(STELO)) {
	errors[errors.length] = "valid 10-digit additional telephone number";
	}
}
*/



//date check
if( MM == "" || YY == "" || DD == ""){
    errors[errors.length] = "Please enter valid date";
}else if( checkDate( DD, MM, YY ) === false ){
    errors[errors.length] = "That is not a valid date";
}
else{
    var InputDate = new Date();
    var CurrentDate = new Date();

    InputDate.setDate(DD);
    InputDate.setMonth(MM-1);
    InputDate.setFullYear(YY);
	
	//alert(InputDate);

    if(InputDate < CurrentDate){
        errors[errors.length] = "Date entered has already passed"
    }
    else{
    var SixMonthsAheadDateTime = new Date();
    SixMonthsAheadDateTime.setDate(SixMonthsAheadDateTime.getDate()+180);

    if(SixMonthsAheadDateTime < InputDate){
        errors[errors.length] = "We only accept dates that are within 6 months"
    }else{
        //errors[errors.length] = InputDate;
    }
	}
}
//end of date check


if (ROOMS == "") {
errors[errors.length] = "Please select number of rooms";
}


/*
if(CMT.length > 140){
errors[errors.length] = "Too much data in the comment box! Please remove "+
    (CMT.length - 140)+ " characters";
}
*/



if (errors.length > 0) {
reportErrors(errors);
return false;
}
return true;
}


function fixDate(MM){
if(MM == "January"){MM = 0;}
if(MM == "February"){MM = 1;}
if(MM == "March"){MM = 2;}
if(MM == "April"){MM = 3;}
if(MM == "May"){MM = 4;}
if(MM == "June"){MM = 5;}
if(MM == "July"){MM = 6;}
if(MM == "August"){MM = 7;}
if(MM == "September"){MM = 8;}
if(MM == "October"){MM = 9;}
if(MM == "November"){MM = 10;}
if(MM == "December"){MM = 11;}

return MM;
}


function reportErrors(errors){
 var msg = "Please Enter Valid Data for:\n";
 for (var i = 0; i<errors.length; i++) {
  var numError = i + 1;
  msg += "\n" + numError + ". " + errors[i];
 }
 alert(msg);
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function checkDate(day,month,year){
	days_in_month = new Date(year, month, 0).getDate();
	
	if(day > 0 && day <= days_in_month){
		return true;
	}else{
		return false;
	}
}
