var MINUTE		= 60 * 1000;
var HOUR		= 60 * MINUTE;
var DAY			= 24 * HOUR;
var WEEK		= 7 * DAY;

var Hoy			= new Date();
var EnDosDias	= new Date(Hoy.getTime() + 2*DAY);

function ControlValidation($strId,$strMensaje){
	var objVar;
	objVar = document.getElementById($strId);

	if(trim(objVar.value)==""){
		alert($strMensaje);
		objVar.focus();
		return false;
	}

	return true;
}

function ValidateForm(){
	var i,flag;


	if(!ControlValidation("lstTourType","Please insert the Tour Type...")){
		return false;
	}

	if(!ControlValidation("txtTourDate","Please insert the Tour Date...")){
		return false;
	}

	//date validation
	if(!isTwoDaysAfter(document.getElementById("txtTourDate").value)){
		return false;
	}


	flag = false;
	for(i=1;i<4;i++){
		objVar = document.getElementById("txtPassengerName"+i);
		if(objVar.value!=""){
			flag = true;

			if(!ControlValidation("lstPassengerAge"+i,"Please insert the Age of Passenger...")){
				return false;
			}else{
				objVar = document.getElementById("lstPassengerAge"+i);
				if(isNaN(objVar.value)){
					alert("Please insert a number in passenger's age.");
					objVar.focus();
					return false;
				}
			}

			if(!ControlValidation("lstPassengerWeight"+i,"Please insert the Weight of Passenger...")){
				return false;
			}else{
				objVar = document.getElementById("lstPassengerWeight"+i);
				if(isNaN(objVar.value)){
					alert("Please insert a number in passenger's weight.");
					objVar.focus();
					return false;
				}
			}

		}
	}

	if(flag==false){

		alert("Please insert the data from one passenger at least...");

		objVar.focus();

		return false;

	}


	if(!ControlValidation("txtContactName","Please insert the Contact Name...")){

		return false;

	}

	if(!ControlValidation("txtAddress","Please insert your Address...")){

		return false;

	}

	if(!ControlValidation("txtCity","Please insert the City...")){

		return false;

	}

	if(!ControlValidation("txtState","Please insert the State...")){

		return false;

	}

	if(!ControlValidation("txtCountry","Please insert the Country...")){

		return false;

	}

	if(!ControlValidation("txtPhoneNumber","Please insert the Phone Number...")){

		return false;

	}

	/*
	if(!ControlValidation("txtFax","Please insert the Fax...")){

		return false;

	}
	*/

	objVar = document.getElementById("txtEmail");

	if(!valida_email(objVar, true)){

		return false;

	}

	return true;

}



function valida_email(objEmail, alerta)

{

	p_email = objEmail.value.toLowerCase();

	reg_email = /^[\w\-\.]+@[\w\-\.]+\.[a-z]{2,4}$/;

	if (! reg_email.test(p_email)){

		if (alerta){

			alert('Invalid E-mail.');

			objEmail.focus();

			objEmail.select();

		}

		return false;

	}

	return true;

}



function trim(texto){

	var inicio	=	/^[ ]+/;

	var fin		=	/[ ]+$/;

	var texto	=	texto.replace (inicio,"");

	texto		=	texto.replace (fin,"");

	return texto;

}

//we receive a date in mm/dd/yyyy format
function isTwoDaysAfter(strDate){
	var strMes,strDia;

	var Today		= new Date();
	var PlusTwoDays	= new Date(Today.getTime() + 2*24*60*60*1000);
	strMes = (PlusTwoDays.getMonth()+1);
	strDia = (PlusTwoDays.getDate());

	if(strMes<10){
		strMes = "0"+strMes;
	}

	if(strDia<10){
		strDia = "0"+strDia;
	}

	var inTwoDays	= (PlusTwoDays.getFullYear())+""+strMes+""+strDia;

	var custDate	= strDate.substr(6,4)+""+strDate.substr(0,2)+""+strDate.substr(3,2);

	//alert("Selected day: "+custDate);
	//alert("In two days: "+inTwoDays);
	if(custDate<inTwoDays){

		alert("You can only make reservations since two days after today.");

		return false;
	}

	return true;
}

function disallowDate(date) {

	if (date.getFullYear() < EnDosDias.getFullYear()){

		return true;

	}

	if (date.getFullYear() == EnDosDias.getFullYear()){

		if(date.getMonth() < EnDosDias.getMonth()){

			return true;

		}

		/*
		//if we want to see the day of today
		if(date.getDate()==Hoy.getDate()){
			return false;
		}
		*/

		if((date.getMonth() == EnDosDias.getMonth()) && (date.getDate() < EnDosDias.getDate())){

			return true;

		}

	}

	return false; // enable other dates
}