/*
$Id: checkdate.js,v 1.2 2005/11/08 11:12:22 nmargar Exp $

United Online S.A., (c) 2005
Author: Nikolaos Margaritis

*/

function isDisecton (y) {

	q1 = y%4;
	q2 = y%100;
	q3 = y%400;

	if (q3==0) return true;
	if (q2==0) return false;
	if (q1==0) return true;
	return false;
}

function dateDoesNotExist(d,m,y) {

	if (d>30 && ( m==4 || m==6 || m==9 || m==11 )) return true;
	if (d>31) return true;

	if (isDisecton(y)) {
		if (m==2 && d>29) return true;
	} else {
		if (m==2 && d>28) return true;
	}
	return false;

}

function checkConsecutiveHours(hour_start, hour_end) {

	current = new Date(); // a new instance
	vhour_cur=current.getHours();
	vhour_start=hour_start.value;
	vhour_end=hour_end.value;

	f1 = hourConsec(vhour_cur, vhour_start);
	f2 = hourConsec(vhour_cur, vhour_end);

	if ( f1 >= 0 && f2 >= 0 ) {
		return hourConsec(vhour_start, vhour_end);
	} else {
		return -1;
	}
}


function checkConsecutiveDatesMsg(day_start, month_start, year_start, day_end, month_end, year_end, checkCurrent, checkEmpty) {
	f=checkConsecutiveDates(day_start, month_start, year_start, day_end, month_end, year_end, checkCurrent, checkEmpty);

	if (f==0)	return '';
	if (f>=32)	return 'Unknown Exception!!!\n';

	msg='';

	if (checkEmpty) {
		if (f>=16) {
			f-=16; msg+='One of the dates has an empty field. \n';
		}
	} else {
		if (f>=16) {
			f-=16; msg+='Either all of the date fields must be set or none. \n';
		}
	}

	if (f>=8) {f-=8; msg+='One of the dates is NOT appropriate \n';}
	if (f>=4) {f-=4; msg+='None of the dates can be set before the present. \n';}
	if (f>=2) {f-=2; msg+='The departure date cannot be before the arrival date. \n';}
	if (f==1) msg+='The dates cannot be the same. \n';

	return msg;
}

// select '' if no value for paramaters...
function checkConsecutiveDates(day_start, month_start, year_start, day_end, month_end, year_end, checkCurrent, checkEmpty) {

	/* Return Values are:
		 0	- Dates are the same.	(D2 = D1)
		 1	- Dates are consecutive (D2 > D1)
		 2	- Dates are inconsecutive (D2 < D1)
		 4	- Either D1 or D2 is inconsecutive with Dpresent.
		 8	- either D1 or D2 is not a real date (eg, 29 Feb 2005).
		16	- Either D1 or D2 has an empty field
	*/

	current = new Date(); // a new instance
	vday_cur=current.getDate();
	vmonth_cur=current.getMonth() + 1;
	vyear_cur=current.getFullYear();

	vday_start=day_start.value;
	vmonth_start=month_start.value;
	if (year_start=='this') vyear_start=vyear_cur; else vyear_start=year_start.value;

	vday_end=day_end.value;
	vmonth_end=month_end.value;
	vyear_end=year_end.value;
	if (year_end=='this') vyear_end=vyear_cur; else vyear_end=year_end.value;

	test=0;
	//check if they didnt select any dates

	empty='-';
	atLeastOneEmpty=false;
	if ( (vmonth_start==empty) || (vday_start==empty) || (vyear_start==empty) || (vmonth_end==empty) || (vday_end==empty) || (vyear_end==empty) )
		atLeastOneEmpty=true;

	if (checkEmpty) {
		if (atLeastOneEmpty) return 16;
	} else {
		atLeastOneNotEmpty = ( (vmonth_start!=empty) || (vday_start!=empty) || (vyear_start!=empty) || (vmonth_end!=empty) || (vday_end!=empty) || (vyear_end!=empty) );
		if (atLeastOneEmpty && atLeastOneNotEmpty) {
			return 16;
		}
	}

	if ( dateDoesNotExist(vday_start, vmonth_start, vyear_start) ||
		 dateDoesNotExist(vday_end, vmonth_end, vyear_end) ) test+=8;


	if (checkCurrent) {

		f1 = DateConsec(vday_cur, vmonth_cur, vyear_cur, vday_end, vmonth_end, vyear_end);
		f2 = DateConsec(vday_cur, vmonth_cur, vyear_cur, vday_start, vmonth_start, vyear_start);
		if ( f1<0 || f2<0 ) test+=4;

	}

	v=DateConsec(vday_start, vmonth_start, vyear_start, vday_end, vmonth_end, vyear_end);
	if (v==-1) test+=2;
	if (v==0) test+=1;

	return test;
}

function hourConsec(h1, h2) {

	ih1=parseInt(h1);
	ih2=parseInt(h2);

	if (ih1 < ih2) return 1;
	if (ih2 == ih1) return 0;
	if (ih1 > ih2) return -1;

}

function DateConsec(d1, m1, y1, d2, m2, y2) {

	id1=parseInt(d1);
	im1=parseInt(m1);
	iy1=parseInt(y1);

	id2=parseInt(d2);
	im2=parseInt(m2);
	iy2=parseInt(y2);

	if (iy2<iy1) return -1 ;

	if (iy2==iy1) {
		if (im2<im1) return -1 ;
		if (im2==im1){
			if (id2<id1) return -1 ;
			else if (id2==id1) return 0;
		}
	}
	return 1;
}
