// JavaScript Document


function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function daysElapsed(date1,date2) {//date1 is greater
    var difference =
        Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
    return difference/1000/60/60/24;
}
function dateDiff(dep_date, arr_date)
{
	splitted_dep_date = dep_date.split("-");
	splitted_arr_date = arr_date.split("-");
	var d1_year = splitted_dep_date[2];
	var d1_mnth = splitted_dep_date[1];
	var d1_day = splitted_dep_date[0];
	var d2_year = splitted_arr_date[2];
	var d2_mnth = splitted_arr_date[1];
	var d2_day = splitted_arr_date[0];
	var date_differ = daysElapsed(new Date(d1_year,d1_mnth,d1_day),new Date(d2_year,d2_mnth,d2_day));
	
	if(date_differ < 0)
		return 0;
	else
		return date_differ;
}

/*
function isValidDate(dateStr) {

//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year
var datePat = /^([0-9]{4})(-)([0-9]{2})(-)([0-9]{2})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert(dateStr + " Date is not in a valid format.")
return false;
}

month = matchArray[3]; // parse date into variables
day = matchArray[5];
year = matchArray[1];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;
}

function dateDiff(dateform) {

date1 = new Date();
date2 = new Date();
diff  = new Date();

if (isValidDate(dateform.txt_start_date.value)) { // Validates first date 
date1temp = new Date(dateform.txt_start_date.value);
date1.setTime(date1temp.getTime());
}
else return false; // otherwise exits

if (isValidDate(dateform.txt_end_date.value)) { // Validates second date 
date2temp = new Date(dateform.txt_end_date.value);
date2.setTime(date2temp.getTime());
}
else return false; // otherwise exits

// sets difference date to difference of first date and second date

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

//weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
//timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24)); 
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60)); 
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60)); 
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000); 
timediff -= secs * 1000;

var num_nights = days;
dateform.no_nights.value = num_nights;
return false; // form should never submit, returns false
}*/
