daysInMonth = new Array("31", "29", "31", "30", "31", "30", "31", "31", "30", "31", "30", "31");
today = new Date();
function alertAndFocus(element, alertText) {
	alert(alertText);
	element.focus();	
}
function isEmpty(value) { 
	if (value == "" ){ return true; }
	return false;  
}
function isNumerical(stringInput, allowedChars) {  
	var decPoints = 0;
	var decCheck = false;    
	
	if (allowedChars.indexOf(".") != -1) {     
		decCheck = true;
	} 	               
	for (i = 0;  i < stringInput.length;  i++)  {
		ch = stringInput.charAt(i);            
		if (allowedChars.indexOf(ch) == -1) {    
			return false;
		}             
		if ( (decCheck == true) && (ch == ".") ) {
			decPoints++;
			if (decPoints > 1) {
				return false;
			}
		}         
	}	 
	return true;
}
function isSelectedIndex(element, errorAlert, indexValue) {     
	if (element.selectedIndex == indexValue) {  
		alert(errorAlert);
		element.focus();
		return true;        
	}  
	return false;
}
function isLeapYear(y) {
  	leap = false;
	if (y%100 == 0) {                  
		if (y%400 == 0)   	
			leap=true;
	} else {
   		if (y%4 == 0)
   			leap=true;
	}
	if (leap == true) {
		daysInMonth[1]=29;
	} else {
		daysInMonth[1]=28;
	}
	//return leap;
}
function isDate(d, m, y) {
	isLeapYear(y);	
	if (parseInt(d) > daysInMonth[m-1]) {     					
   		return false;	
	}	
	return true;
}
function isPastDate(d, m ,y) {
	newDate = createDate(d,m,y);				
	isLeapYear(y);
	if ( newDate < today ) {		
		return false;
	}
	return true;
}
function createDate(d,m,y) {	
	//alert("d/m/y:" + d+"/"+m+"/"+y);	
	newDate = new Date();				
	//alert("new date:" + newDate);	
			
	tempMonth = m.replace(/^0+/,'');
	//alert(tempMonth);
	newDate.setFullYear(y,(parseInt(tempMonth)-1),d);


	/*
	newDate.setYear(y);				
	alert("year set:" + newDate)
		
	newDate.setMonth(parseInt(m)-1);
	alert("month set:" + newDate)
	
	newDate.setDate(d);	
	alert("day set:" + newDate)
	*/
		
	//alert("updated date:" + newDate)
	return newDate;
}
function createDateFromArray(dateArray){
	d = dateArray[0];
	m = dateArray[1];
	if (dateArray[2].length == 2) {
		dateArray[2] = "20" + dateArray[2];			
	}
	y = dateArray[2];
	return createDate(d,m,y);
}
function addDay(date, increment) {
	date.setDate(date.getDate()+increment);	
	return date;
}
function addMonth(date, increment) {
	date.setMonth(date.getMonth()+increment);	
	return date;
}
function addYear(date, increment) {
	date.setYear(date.getFullYear()+increment);	
	return date; 
}
function subtractDates(date1, date2) {
	oneDay = 1000*60*60*24;
	if (date1 < date2 ) {
		dateDiff = Math.ceil( (date2.getTime()-date1.getTime() )/(oneDay));		
	} else {
		dateDiff = Math.ceil( (date1.getTime()-date2.getTime() )/(oneDay));		
	}
	return dateDiff;
}
function padNumber(numberToPad, paddingSize) {
	stringNumber = String(numberToPad);	
	for (count = stringNumber.length; count < paddingSize; count++){
		stringNumber = "0" + stringNumber;
	}
	return stringNumber;	
}
function getShortDate(date) {		
	padNumber(date.getDate(), 2);
	dateString = padNumber(date.getDate(), 2) + "/" + padNumber((date.getMonth()+1),2) + "/" + padNumber(date.getFullYear(),4) ;	
	return dateString;
}

function validateDateString(dateString, pastDatesAllowed) {		
	// check number sequence
	if ( ! ( isNumerical(dateString, "0/123456789") ) ) {						
		return "INVALIDFORMAT";
	}
	dateArray = dateString.split("/");	
	if (dateArray.length != 3) {
		return "INVALIDFORMAT";
	}
	if (! isDate(dateArray[0],dateArray[1],dateArray[2])) {		
		return "NOTVALID";
	}	
	// past dates?
	if (pastDatesAllowed == false ) {
		if (! isPastDate(dateArray[0],dateArray[1],dateArray[2])) {		
			return "DATEPASSED";
		} 
	}
	return true;	
}
function dateAlertAndFocus(element, dateStatus) {
	switch (dateStatus) {			
		case "INVALIDFORMAT" : 				
			alertText = "The date entered is not in the valid format. Please use the format dd/mm/yyyy.";
		break;		
		case "NOTVALID" :
			alertText = "The date entered is not valid. Please enter a date in the format dd/mm/yyyy";
		break;
		case "DATEPASSED" :
			alertText = "The date entered has already passed!. Please enter a different date."
		break;
		default :
			alertText = "";
			// do nothing
	}
	if (alertText != "") {
		alertAndFocus(element, alertText);
	}
}

function setToDate(datePartToIncrement) {				
	eFrom = document.getElementById("frmFrom");	
	tripLength = parseInt(document.getElementById("frmTripLength").value);
	
	// check for from date
	if ( isEmpty(eFrom.value) == true ) {
		alertAndFocus(eFrom, "Please select a 'From' date.")
		return false;
	}	
	dateStatus = validateDateString(eFrom.value, false);	

	if ( dateStatus != true ) {
		//output error message
		dateAlertAndFocus(eFrom, dateStatus);
	}	
		
	// create date as valid
	fromDate = eFrom.value.split("/");
	toDate = createDate(fromDate[0],fromDate[1],fromDate[2]);
	

	//alert("fromDate: " + fromDate)
	//alert("toDate: " + toDate)
	//alert("tripLength: " + tripLength)
	
	// incremenet date
	switch (datePartToIncrement) {			
		case "DAY" : 				
			toDate = addDay(toDate, tripLength-1);			
		break;
		case "MONTH" :
			toDate = addMonth(toDate, tripLength);
			toDate = addDay(toDate, -1);			
		break;
		case "YEAR" : 
			toDate = addYear(toDate, tripLength);
		break;
		default :
			toDate = addDay(toDate, tripLength);
	}						
	// update to date
	eTo = document.getElementById("frmTo");
	eTo.value = getShortDate(toDate);
	return true;
}
function setDateOld() {
	//check format	
	if ( ! ( isNumerical(eFrom.value, "0/123456789") ) ) {						
		alertAndFocus(eFrom, "The date entered is not in the valid format. Please use the format dd/mm/yyyy.");
		return false;
	} 	
	//split into array	 to check format				
	fromDate = eFrom.value.split("/");	
	if (fromDate.length != 3) {
		alertAndFocus(eFrom, "The date entered is not in the valid format. Please use the format dd/mm/yyyy.");
		return false;
	}									
	//validate date
	if (! isDate(fromDate[0],fromDate[1],fromDate[2])) {
		alertAndFocus(eFrom, "The date entered is not valid. Please use enter a date in the format dd/mm/yyyy");			
		return false;
	}	
	//check date hasnt passed
	if (! isPastDate(fromDate[0],fromDate[1],fromDate[2])) {
		alertAndFocus(eFrom, "The date entered has already passed!. Please enter a different date.");			
		return false;
	} 
}
function setTripLength(datePartToIncrement) {
	//subtract dates to find the number of days
	//alert(true)
	//alert(datePartToIncrement)
		
	eFrom = document.getElementById("frmFrom");	
	eTo = document.getElementById("frmTo");	
	
	
	// check for to and from date
	if ( (isEmpty(eFrom.value) == true) || (isEmpty(eTo.value) == true) ) {													   
	} else {
		//validate dates 
		dateStatus = validateDateString(eFrom.value, false);	
		if ( dateStatus != true ) {
			//output error message
			dateAlertAndFocus(eFrom, dateStatus);
		}		
		dateStatus = validateDateString(eTo.value, false);	
		if ( dateStatus != true ) {
			//output error message
			dateAlertAndFocus(eTo, dateStatus);
		}

		//create dates
		fromDate = eFrom.value.split("/");
		fDate = createDate(fromDate[0],fromDate[1],fromDate[2]);
	
		toDate = eTo.value.split("/");
		tDate = createDate(toDate[0],toDate[1],toDate[2]);
		
		//subtract dates		
		tripLength = subtractDates(fDate, tDate);
		switch (datePartToIncrement) {			
			case "DAY" : 				
				tripLength = tripLength + 1;	
			break;
			case "MONTH" :
				tripLength = tripLength + 1;	
				/*
				alert("tripLength "+tripLength)
				alert("days in month" + daysInMonth[fDate.getMonth()])
				tripLength = Math.ceil((tripLength)/ daysInMonth[fDate.getMonth()] );
				alert("months "+tripLength)
				*/				
				tripLength = Math.ceil((tripLength)/ 31 );
				if (tripLength < 2) {
					tripLength = 2
				}
			break;
			default :
				tripLength = tripLength;
		}		
		//update triplength		
		tripLengthElement = document.getElementById("frmTripLength")
		if (tripLength > tripLengthElement.length) {
			alertAndFocus(eTo, "You have exceeded the maximum trip length. Please review your options.")	
		} else {
			tripLengthElement.value = tripLength;			
		}		
	}	
}
function updateTravellingAs() {
	numChildren = document.getElementById("frmChildren").value;
	numAdults = document.getElementById("frmAdults").value;
	
	if (numChildren	== 0) {
		if (numAdults == 2) {
			//document.getElementById("frmCouple").checked = true;
			document.getElementById("frmIndividual").checked = true;
		} else {
			document.getElementById("frmIndividual").checked = true;
		}		
	} else {
		document.getElementById("frmFamily").checked = true;
	}
	//checkTravellers()
}
/*
---MOVED TO STEP 1
function validatestep1() {	
	// check destination is selected
	if (isSelectedIndex(document.getElementById("frmDestination"), "Please Select a destination", 0) ) {
		return false;		
	}
	
	if (!checkTravellers() ) {
		return false;
	}
	
	return true;
}
*/
function checkTravellers() {
	// check the number of travellers depending for 'travelling as' 
	numChildren = document.getElementById("frmChildren").value;
	numAdults = document.getElementById("frmAdults").value;
	
	individuals = document.getElementById("frmIndividual")
	if (individuals.checked == true ) {		
		if (numChildren != 0) {
			alertAndFocus(individuals, "If you are taking children please select Family")	
			return false;
		}
		if (numAdults == 2) {
			//alertAndFocus(individuals, "If there are only two people travelling please select Couple.")
			//return false;
		}
	}	
	couple = document.getElementById("frmCouple")
	if ( couple.checked == true) {
		if ( (numChildren ==0 )&&(numAdults == 2) ){
		} else {
			alertAndFocus(couple, "To qualify as a couple only two travellers with no children is acceptable")	
			return false;
		}
	}		
	family = document.getElementById("frmFamily")
	if ( family.checked == true ) {
		if ( (numChildren == 0 )||(numAdults == 0) ) {
			alertAndFocus(family, "To qualify as a family a minimum of one adult and one child must be selected.")	
			return false;
		} else {
			if (numAdults > 2) {
				alertAndFocus(family, "Two adults is the maximum number of adults for a family.")
				return false;
			}
		}
		
	}
	return true;
}
