var IS_GLOBAL_VALID=true;

function formatErrors(a) {
	var s="";
	for (var i=0;i<a.length;i++) {
		s = s + a[i] + "\n";
	}
	return s;
}

function processStartFormValidation(button) {

	var s=_F("check_in_date").value;
	
	var b=true;
	
	for (var i=1;i<4;i++) {
		if (_F("ChildAge" + i)) {
			if (_F("ChildAge" + i).selectedIndex==0) {
				b=false;
			};
		} else {
			break;
		};
	};	
	
	if (b) {
		if (validateDate(s)) {

			

			if (makeDate(s,true)>=calendar_boundary_date) {
			
				if (!isDateUnavailable(makeDate(s,true))) {
					
					if (button==1) 
						doSubmit('start',null);
					
					if (button==2) 
						doSubmit('start','packages');	
					
				} else {
					
					alert("The check-in date is unavailable. ");
					
				}
				
			} else {
			
				alert("The check-in date should be greater than or equal to '" + formatDate(calendar_boundary_date) + "'.");
				
			};
		} else {
		
			alert("The date format is not valid, please enter in the format mm/dd/yy.");
			
		};
	}
	else {	
	
		alert("Child age is not provided.");
		
	};
};

function processPackageFormValidation() {
	var errs=new Array();
	
	var s=_F("check_in_date").value;
	
	if (validateDate(s)) {
		if (makeDate(s,true)<calendar_boundary_date) {		
			errs.push("The check-in date should be greater than or equal to '" + formatDate(calendar_boundary_date) + "'.");
		} else {
			if (isDateUnavailable(makeDate(s,true))) {
				errs.push("The check-in date is unavailable for the package. ");
			}
		};
	} else {
		alert("The date format is not valid.  Please enter in the format mm/dd/yy.");
		return;
	};
	
	el=_F("NumberOfNights")
	if (el.selectedIndex==0) {
		errs.push("Number of Nights is not selected.");
	};
	
	el=_F("NumberOfChildren")
	if (el.selectedIndex==0) {
		errs.push("Number of Children is not selected.");
	} else {
	
		for (var i=1;i<4;i++) {
			if (_F("ChildAge" + i)) {
				if (_F("ChildAge" + i).selectedIndex==0) {
					errs.push("Child age is not provided.");
					break;		
				};
			} else {
				break;
			};

		}	
	}
	
	//el=_F("NumberOfAdults")
	//if (el.selectedIndex==0) {
	//	errs.push("Number of Adults is not selected.");
	//};
	
	if (errs.length>0) {		
		err="Please fix the following errors:\n\n" + formatErrors(errs);
		alert(err);
	} else {  
		doSubmit('package',null);
	};
};

function processEmailFormValidation() {
//alert("In processEmailFormValidation");
	var errs=new Array();

//alert("errs ok,");
//var bb=_F("last_name");
//alert("last_name = " + bb.value);

	if (document.getElementById("last_name")!=null && !isValidNames("last_name")) {
		errs.push("'Customer Last Name' is empty or incorrect.");
	}
	if (document.getElementById("first_name")!=null && !isValidNames("first_name")) {
		errs.push("'Customer First Name' is empty or incorrect.");
	}
	if (document.getElementById("last_name_agentpanel")!=null && !isValidNames("last_name_agentpanel")) {
		errs.push("'Customer Last Name' is empty or incorrect");
	}
	if (document.getElementById("first_name_agentpanel")!=null && !isValidNames("first_name_agentpanel")) {
		errs.push("'Customer First Name' is empty or incorrect");
	}
	
	//if (document.getElementById("ta_first_name")!=null && !isValidNames("ta_first_name")) {
	//	errs.push("'Your First Name' is empty or incorrect.");
	//}
	//if (document.getElementById("ta_last_name")!=null && !isValidNames("ta_last_name")) {
	//	errs.push("'Your Last Name' is empty or incorrect.");
	//}
	if (document.getElementById("email")!=null && !isValidEmail("email")) {
		errs.push("'Email' is empty or incorrect.");
	}
	if (document.getElementById("email_agentpanel")!=null && !isValidEmail("email_agentpanel")) {
		errs.push("'Email' is empty or incorrect.");
	}
	if (errs.length>0) {		
		err="Please fix the following errors:\n\n" + formatErrors(errs);
		alert(err);
	} else {  
		doSubmit('email',null);
	};	
	
};

function processRegistrationFormValidation(f1,f2,f3,f4,f5) {

	var errs=new Array();
	
	if (!isValidEmail(f1)) {
		errs.push("'Email' is empty or incorrect.");
	}
	
	if (!isValidNames(f4)) {
		errs.push("'First Name' is empty or incorrect.");
	}
	if (!isValidNames(f5)) {
		errs.push("'Last' is empty or incorrect.");
	}
	if (_F(f2)!=null && _F(f2).value=="") {
		errs.push("'Your Password' is empty or incorrect.");
	}
	if (_F(f3)!=null && _F(f3).value=="") {
		errs.push("'Your Repeated Password' is empty or incorrect.");
	}
	if (_F(f2)!=null && _F(f3)!=null && _F(f2).value!=_F(f3).value) {
		errs.push("Passwords do not match.");
	}
	
	if (errs.length>0) {		
		err="Please fix the following errors:\n\n" + formatErrors(errs);
		alert(err);
		return false;
	} else {  
		return true;
	};

}

function processConfigurationFormValidation() {	
	var errs=new Array();
	
	var a=null;
	
	a=getElementsNamesStartWith("first_traveler_");	
	for (var i=0;i<a.length;i++) {
		if (!isValidNames(a[i])) {
			errs.push("'" + a[i].getAttribute("label") + "' is empty or incorrect.");
		}
	} 
		
	a=getElementsNamesStartWith("last_traveler_");
	for (var i=0;i<a.length;i++) {
		if (!isValidNames(a[i])) {
			errs.push("'" + a[i].getAttribute("label") + "' is empty or incorrect.");
		}
	}
	
	a=_N("SELECT");
	for (var i=0;i<a.length;i++) {
		if (a[i].selectedIndex==0) {

		    //errs.push("Date & Time for '" + a[i].getAttribute("label"));


		    var x = a[i].getAttribute("label");
		    var y = x.indexOf("'<b>");
		    var z = x.indexOf("</b>");
		    var w = x.substring(y + 4, z);

			errs.push(w + " requires a date and/or time selection");

		}
	}
	
	if (errs.length>0) {
	    err = "Please fix the following errors:\n\n" + formatErrors(errs);

		alert(err);
	} else {  
		doSubmit('config',null);
	};	
};

function processCheckoutFormValidation() {
	
	var msg="";
	var x=_F("firstNameTxt");
	if (x) {
	    //alert("x = " + x.value);
	    if (x.value=="") {
	        msg="You must enter the customer first name";
	    }
	} else {
	
	};
	
	if (msg="") {
	
	} else {
        doSubmit('placing',null);
        
    };

//	return true;
};

function contactMeOnClick(d) {

	var x=_F("CCNumber");
	if (x) {
		if (d) x.value="";
		x.disabled=d;
	}	
	
	x=_F("CCSecurityCode");
	if (x) {
		if (d) x.value="";
		x.disabled=d;
	}	
	
	x=_F("CCExpMonth");
	if (x) {
		if (d) x.selectedIndex=0;
		x.disabled=d;
	}	
	
	x=_F("CCExpYear");
	if (x) {
		if (d) x.selectedIndex=0;
		x.disabled=d;
	}	

}

function processTermsFormValidation() {
	var errs=new Array();
	
	//if (isEmpty("initials1") || isEmpty("initials2")) {
		//errs.push("'First Name' or 'Last Name' is empty or incorrect.");
	//}
	
	var el=_F("agreement1");
	if (!el.checked) {
		errs.push("You have to agree to the Terms and Conditions in order to continue.");
	}
	
	if (errs.length>0) {		
		err="Please fix the following errors:\n\n" + formatErrors(errs);
		alert(err);
	} else {  
		doSubmit('terms',null);
	};	
};

function processMainFormValidation(e) {
	if (!e) var e = window.event
	
	var x=(e.target) ? e.target : e.srcElement;
	
	if (x) {
		if (!isPositiveNumber(x.value)) {
			IS_GLOBAL_VALID=false;
			alert("Quantity field requires positive integer value.");
			x.focus();
			return false;
		};
	};	
};

function getElementsNamesStartWith(x) {
	var els=document.getElementsByTagName("INPUT");
	var a=new Array();
	for (var i=0,rc=els.length;i<rc;i++) {
		if (els[i].name.substring(0,x.length)==x) {
			a.push(els[i]);		
		}		
	};
	return a;
};