// Declaring valid date character, minimum year and maximum year
var dtCh= "/";

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30;}
		if (i==2) {this[i] = 29;}
	} 
	return this;
}

var today = new Date();
today.setHours(0, 0, 0, 0);

var tomorrow = new Date();
tomorrow.setDate(today.getDate() + 1);

function lhwDateRange(d) {
	d.setHours(0, 0, 0, 0);
	return d < today;
}

function isDate(dtStr, maskControl){
	//Extract the separator character
	//Take the format that should be displayed
	var sDateFormat = $(maskControl).value;
	sDateFormat = sDateFormat.replace('%Y','');
	sDateFormat = sDateFormat.replace('%m','');
	sDateFormat = sDateFormat.replace('%d','');
	dtCh = sDateFormat.substring(1);
	sDateFormat = $(maskControl).value;
	var posY=sDateFormat.indexOf('%Y');
	var posm=sDateFormat.indexOf('%m');
	var posd=sDateFormat.indexOf('%d');
	//Position of the separator chars
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	//Check the order of the parameters
	var strMonth = "";
	var strDay = "";
	var strYear = "";
	
	//The year is the first param
	if (posY < posm && posY < posd) {
		strYear = dtStr.substring(0,pos1);
		//The second param is the month
		if (posm < posd) {
			strMonth = dtStr.substring(pos1+1,pos2);
			strDay = dtStr.substring(pos2+1);
		}
		else {
			strDay = dtStr.substring(pos1+1,pos2);
			strMonth = dtStr.substring(pos2+1);
		}
		
	}
	//The month is the first
	else if (posm < posY && posm < posd) {
		strMonth = dtStr.substring(0,pos1);
		//The second param is the year
		if (posY < posd) {
			strYear = dtStr.substring(pos1+1,pos2);
			strDay = dtStr.substring(pos2+1);
		}
		else {
			strDay = dtStr.substring(pos1+1,pos2);
			strYear = dtStr.substring(pos2+1);
		}
	}
	//Day is first
	else {
		strDay = dtStr.substring(0,pos1);
		//The second param is the year
		if (posY < posm) {
			strYear = dtStr.substring(pos1+1,pos2);
			strMonth = dtStr.substring(pos2+1);
		}
		else {
			strMonth = dtStr.substring(pos1+1,pos2);
			strYear = dtStr.substring(pos2+1);
		}
	}
	
	var daysInMonth = DaysArray(12);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		return [];
	}
	if (strMonth.length<1 || month<1 || month>12){
		return [];
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return [];
	}
	if (strYear.length != 4 || year==0){
		return [];
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return [];
	}
	
	return [day,month,year];
}

////Change the departure date based on the arrival. The departure should be two days more.
function ChangedArrivalDate(arrivalID, departureID, defaultValueID, formatID) {

	var arrival = $(arrivalID);
	var departure = $(departureID);
	var defaultValue = $(defaultValueID).value.toLowerCase();
	if (arrival.value != '' && trim(arrival.value) != '' )
	{
		//Check that the date is correct
		var date = isDate(arrival.value, formatID);
		if (date.size() > 0){
			//Check that the format is correct
			try{
				//Create the date and add two days
				var newDate = new Date(date[2],date[1]-1,date[0]);
				newDate.setDate(newDate.getDate()+2); 
				//Take the format that should be displayed
				var sDate = $(formatID).value;
				//Replace the string with real data
				sDate = sDate.replace('%Y',newDate.getFullYear().toString());
				// Months begins in 0 (0-11)
				sDate = sDate.replace('%m',(newDate.getMonth() + 1).toPaddedString(2));
				sDate = sDate.replace('%d',newDate.getDate().toPaddedString(2));
				departure.value = sDate;
			}
			catch(err){}
		}
	}
}

function ValidateDateRange(arrivalID, departureID, formatID){
    	var Arrival = $(arrivalID);
    	var ArrivalDate = isDate(Arrival.value, formatID);
    	var Departure = $(departureID);    	
    	var DepartureDate = isDate(Departure.value,formatID); 
    	

        if(ArrivalDate.size() > 0 && DepartureDate.size() > 0){

        }  	
}

/*
	Validates the state of the dates before submiting the form
*/
function ValidateDateFields(arrivalID, departureID, formatID){

    	var Arrival = $(arrivalID);
    	var ArrivalDate = isDate(Arrival.value, formatID);
   	
    	if (ArrivalDate.size() == 0){
            DisplayError($F('hdnArrivalError'));
    	    return false;
	    }
	    
    	var Departure = $(departureID);    	
    	var DepartureDate = isDate(Departure.value,formatID); 
    	
    	if (DepartureDate.size() == 0){
    	    DisplayError($F('hdnDepartureError'));
    	    return false;
        }            
        
		
       if (ArrivalDate[2] > DepartureDate[2]  ||//year
		   ArrivalDate[1] > DepartureDate[1]  ||//mont
		   ArrivalDate[0] >= DepartureDate[0]){ //day
            DisplayError($F('hdnRangeError'));
            return false;      
        }

       return true;
}

/*
	Displays the error message 
*/
function DisplayError(errorMessage){	
	var ErrorTextboxId = $F('hdnError');	
	//alert($(ErrorTextboxId).id + ' antes tenia: ' + $(ErrorTextboxId).value + ' ahora es: ' + errorMessage);
	
	$(ErrorTextboxId).update();//clears
	$(ErrorTextboxId).update(errorMessage);
	
	var ErrorContainerId = $F('hdnErrorContainer');
    $(ErrorContainerId).show();	    
}