//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(form){
var SZIP = form.pzip.value;
var RZIP = form.dzip.value;
var SNAME = form.name.value;
var EMAIL = trim(form.email.value);

//var STELH = trim(form.STELH.value);
var stelh_area = trim(form.area_code.value);
var stelh_first3 = trim(form.xxx.value);
var stelh_last4 = trim(form.xxxx.value);

var STELH = stelh_area + stelh_first3 + stelh_last4;

var STELHEXT = trim(form.ext.value);

//var STELO = trim(form.STELO.value);
var add_stelh_area = trim(form.area_code_2.value);
var add_stelh_first3 = trim(form.xxx_2.value);
var add_stelh_last4 = trim(form.xxxx_2.value);

var STELO = add_stelh_area + add_stelh_first3 + add_stelh_last4;

var DD = form.day.value;
var MM = form.month.value;
var YY = form.year.value;
var ROOMS = form.number_of_rooms.value;
//var CMT = form.CMT.value;
var errors = [];

var ajax_url = "http://www.fivemovingquotes.com/tasks/p_validator.php";
var ajax_name = encodeURIComponent(SNAME);
var ajax_email = encodeURIComponent(EMAIL);
var ajax_phone_number = encodeURIComponent(STELH);
var ajax_phone_number2 = encodeURIComponent(STELO);
var ajax_from_zip = encodeURIComponent(SZIP);
var ajax_to_zip = encodeURIComponent(RZIP);
var ajax_move_date = encodeURIComponent(YY + '-' + MM + '-' + DD);
var ajax_rooms = encodeURIComponent(ROOMS);
var ajax_sid = encodeURIComponent(form.sid.value);
var request = createXMLHTTPObject();

request.open("POST",ajax_url,true);

request.onreadystatechange = function() {

if(request.readyState == 4) {

    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";
    }
    var response = eval("("+request.responseText+")");


    /*
    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){
                    if(!response.isValid) {
	    	            errors[errors.length] = "Valid phone number";
                    }
		    //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){
  	            if(!response.isValid2) {
                            errors[errors.length] = "Valid additional phone number";
                    }
		    //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);
	    
        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 (errors.length > 0) {
   	 reportErrors(errors);
    } else {
         form.submit();
    }
/*
if(CMT.length > 140){
errors[errors.length] = "Too much data in the comment box! Please remove "+
    (CMT.length - 140)+ " characters";
}
*/

}
}

var post_string = '';

if(ajax_name.length > 0) {
	post_string += "&name=" + ajax_name;
}
if(ajax_email.length > 0) {
        post_string += "&email=" + ajax_email;
}
if(ajax_phone_number.length > 0) {
	post_string += "&phone_number=" + ajax_phone_number;
}
if(ajax_phone_number2.length > 0) {
	post_string += "&phone_number2=" + ajax_phone_number2;
}
if(ajax_from_zip.length > 0) {
	post_string += "&from_zip=" + ajax_from_zip;
}
if(ajax_to_zip.length > 0) {
	post_string += "&to_zip=" + ajax_to_zip;
}
if(ajax_move_date.length > 0) {
	post_string += "&move_date=" + ajax_move_date;
}
if(ajax_rooms.length > 0) {
	post_string += "&rooms=" + ajax_rooms;
}
if(ajax_sid.length > 0) {
	post_string += "&sid=" + ajax_sid;
}

post_string = post_string.substr(1);

request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send(post_string);
return false;
}

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;
	}
}

