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

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

*/


function WriteYearOptions(Ystring, YearsAhead, lang) {
	Now = new Date();
	NowYear = Now.getFullYear();
	if (lang=='gr') yearHead = "????:";
	else if (lang=='de') yearHead = "Jahr:";
	else if (lang=='fr') yearHead = "Ann&eacute;e:";
	else yearHead = "Year:";
	line = "<select name='"+Ystring+"'>";
	line += "<option value='-'>"+yearHead+"</option>";
	if (YearsAhead>0) {
		for (i=0; i<YearsAhead; i++) {
			year = NowYear + i;
			line += "<option value='"+year+"'>"+year+"</option>";
		}
	} else {
		for (i=NowYear+YearsAhead; i<NowYear; i++) {
			year = i;
			line += "<option value='"+year+"'>"+year+"</option>";
		}
	}

	line += "</select>";
	return line;
}
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;
	vmonth_start=month_start;
	if (year_start=='this') vyear_start=vyear_cur; else vyear_start=year_start;

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

	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;
}
/*
function isEmailAddr(email) {
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0) {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}
*/

function validRequired(formField,fieldLabel) {
	var result = true;
	if (formField.value == "") {
		alert('Please Enter ' + fieldLabel +'.');
		formField.focus();
		result = false;
	}

	return result;
}

function isEmailAddr(str) {
	if (str.search(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})/)==-1) return false;
	//original: if (str.search(/^[A-Za-z0-9_]+((-[A-Za-z0-9_]+)|(\.[A-Za-z0-9_]+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)==-1) return false;
	return true;
}

function validEmail(formField,fieldLabel,required) {
	var result = true;
	if (required && !validRequired(formField,fieldLabel)) result = false;
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) ) {
		alert("Please Enter a Correct E-mail Address");
		formField.focus();
		result = false;
	}

  return result;

}

function isChecked(formField, fieldLabel) {
	var result = false;
	for (i = 0; i<formField.length; i++) {
		if (formField[i].checked) {
			result=true;
			break;
		}

	}

	if (result==false) {
		alert('Please Enter ' + fieldLabel +'.');
		formField[0].focus();
	}

	return result;

}


function isChecked2(formField, fieldLabel) {
	var result = false;
	if (formField.checked) result=true;
	if (result==false) {
		alert('Please Enter ' + fieldLabel +'.');
		formField.focus();
	}

	return result;

}

function isSelected(formField, fieldLabel) {
	var result = true;
	var sel_ind = formField.selectedIndex;
	if (sel_ind == 0) {
		alert('Please Enter ' + fieldLabel +'.');
		formField.focus();
		result = false;
	}

	return result;
}

function isSelected2(formField) {
	var sel_ind = formField.selectedIndex;
	if (sel_ind == 0) return false;
	return true;
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
		var radioLength = radioObj.length;
		if(radioLength == undefined)
		if(radioObj.checked)
		return radioObj.value;
		else
		return "";
		for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
		return radioObj[i].value;
		}
	}
	return "";
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
	return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
	radioObj.checked = (radioObj.value == newValue.toString());
	return;
	}
	for(var i = 0; i < radioLength; i++) {
	radioObj[i].checked = false;
	if(radioObj[i].value == newValue.toString()) {
	radioObj[i].checked = true;
	}
	}
}

function CheckTravExp(){
	val=getCheckedValue(document.forms['theForm'].elements['TravelExperience']);
	if(val){
		setCheckedValue(document.forms['theForm'].elements['TravelExperience'], '');
	}
}
function EmVal(){
	vl=document.getElementById("otherExperience").value;
	if(vl || vl==''){
		document.getElementById("otherExperience").value="";
	}
}

function validateForm(theForm) {

	if (!validRequired(theForm.fname,"First Name",true)) return false;
	if (!validRequired(theForm.lname,"Last Name",true)) return false;
	if (!validEmail(theForm.from,"your E-mail Address",true)) return false;
	if (!validRequired(theForm.phone,"Home/Work Phone Number",true)) return false;
	if (!validRequired(theForm.street,"Building Name and Number",true)) return false;
	if (!validRequired(theForm.city,"Street",true)) return false;
	if (!validRequired(theForm.code,"Post Code",true)) return false;
	if (!validRequired(theForm.country,"Country",true)) return false;
	if (!validRequired(theForm.npeople,"Total Number of People traveling",true)) return false;
	if (!validRequired(theForm.nchild,"Number of children under the age of 12",true)) return false;


	var a=new Array(7);
	for (var i=0; i<a.length; i++) { a[i]=theForm.elements['age['+i+']']; }
	if (!isChecked(a, "Age")) return false;
	if (!validRequired(theForm.ndaysgreece,"Preferred Number of Days in Greece",true)) return false;

	f=document.theForm;
        datefrom=f.elements['datefrom'].value;
	if(datefrom==""){
		alert("Departure Date is required");
		return false;
	}
	dateto=f.elements['dateto'].value;
	if(dateto==""){
		alert("Return Date is required");
		return false;
	}
						
	datef = datefrom.split("/");
	doa=datef[0];
	moa=datef[1];
	yoa=datef[2];
        datet = dateto.split("/");
        dod=datet[0];
        mod=datet[1];
        yod=datet[2];
											
        afield = f.elements['msg[arrival][text]'];
        dfield = f.elements['msg[depart][text]'];

        txt=checkConsecutiveDatesMsg(doa, moa, yoa, dod, mod, yod, true, true);
        if (txt!='') {
                alert(txt);
                return false;
        }

        afield.value = datefrom;
        dfield.value = dateto;
			  

//	val=getCheckedValue(document.forms['theForm'].elements['TravelExperience']);
//	alert(val);
	if (!isSelected(theForm.nislands,"Select Number of islands",true)) return false;
  	if (!isSelected(theForm.ndaysathens,"Select Number of days",true)) return false;

//	if (!validRequired(theForm.departure,"Suggested Departure Date from the US",true))return false;
//	if (!validRequired(theForm.returngr,"Suggested Return Date from Greece",true)) return false;


	var b=new Array(4);
	for (var i=0; i<b.length; i++) { b[i]=theForm.elements['accommodation['+i+']']; }
	if (!isChecked(b, "Preferred Type of Accommodation")) return false;
	if (!isSelected(theForm.quality,"Quality accommodations",true)) return false;
  	if (!isSelected(theForm.roomtype,"Type of room",true)) return false;

	var c=new Array(4);
	for (var i=0; i<c.length; i++) { c[i]=theForm.elements['rooms['+i+']']; }
	if (!isChecked(c, "Number and type of rooms")) return false;
	if (!isChecked(theForm.typeEx,"Type of Experience",false)) return false;

	if (!isSelected2(theForm.heardOfSelect)) {
		if (theForm.heardOfOther.value == "") {
			alert ("Tell us how you heard about TrueGreece");
			theForm.heardOfOther.focus();
			return false;

		}

	} else {

		if (theForm.heardOfOther.value != "") {
			alert ("Do NOT fill BOTH fields on how you heard about TrueGreece");
			theForm.heardOfOther.focus();
			return false;
		}
	}
	
	
	if (!isChecked2(theForm.accept,"Accept the TrueGreece Legal Disclaimer")) return false;
	
	afield = theForm.elements['msg[place][text]'];
	var optionList =document.getElementById("selectedOptions").options;
	var dta = '';
	var len = optionList.length;
	for(i=0; i<len; i++){
		dta += ',';
		dta += optionList.item(i).value;
	}

	afield.value=dta;
	

	return true;

}

