
//Define calendar(s): addCalendar ("Unique Calendar Name", "Window title", "Form element's name", Form name")
addCalendar("Calendar1", "Select Date", "fromDate", "hazmana");
addCalendar("Calendar2", "Select Date", "toDate", "hazmana");

// default settings for English
// Uncomment desired lines and modify its values
// setFont("verdana", 9);
 setWidth(90, 1, 15, 1);
// setColor("#cccccc", "#cccccc", "#ffffff", "#ffffff", "#333333", "#cccccc", "#333333");
// setFontColor("#333333", "#333333", "#333333", "#ffffff", "#333333");
// setFormat("yyyy/mm/dd");
// setSize(200, 200, -200, 16);

// setWeekDay(0);
// setMonthNames("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
// setDayNames("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
// setLinkNames("[Close]", "[Clear]");

if(!Dictionary){
	var Dictionary = {
		'fillFields': "אנא מלאו את השדות המסומנים בכוכבית",
		'insertPhone': "אנא הכנס מספר טלפון תקין לשדה הטלפון",
		'insertEmail': "אנא הכנס כתובת איי-מייל תקינה",
		'insertPeople': "אנא מלאו מספר אנשים",
		'invalidArrival': "על תאריך ההגעה להיות היום או מאוחר יותר. אנא וודא שהוא תקין ונסה שוב",
		'invalidDeparture': "על תאריך העזיבה להיות מאוחר יותר מתאריך ההגעה. אנא וודא שהתאריכים תקינים ונסה שוב",
		'fillArrivalFirst': "אנא מלא את תאריך היציאה קודם"
	};
	if(eng == true){
		Dictionary = {
			'fillFields': "Please fill the fields marked with an asterisk",
			'insertPhone': "Please insert a valid phone number",
			'insertEmail': "Please insert a valid email address",
			'insertPeople': "Please insert the number of people",
			'invalidArrival': "The arrival date must be today or later. Please check it is valid and try again",
			'invalidDeparture': "The departure date must come after the arrival date. Please check they are both valid and try again",
			'fillArrivalFirst': "Please fill the arrival date first."
		};
	}
}

function validation(form){
	if(isEmpty(form.name) || isEmpty(form.fromDate) || isEmpty(form.toDate) 
			|| isEmpty(form.phone) || isEmpty(form.email) || isEmpty(form.adults)){
		alert(Dictionary.fillFields);
		return false;
	}
 	if(!isPhone(form.phone)){
		alert(Dictionary.insertPhone);
 	 	return false;
 	}
	if(!isMail(form.email)){
		alert(Dictionary.insertEmail);
		return false;
	}
	if(!isNumerical(form.adults)){
		alert(Dictionary.insertPeople);
		return false;
	}
	return true;
}

function isEmpty(elem){
	var str = elem.value;
	if(str.length > 0)
		return false;
	else{
		return true;
	}
}

function isPhone(elem){
	var phone = elem.value;
	if(!isEmpty(elem) && isNum(phone))
		return true;
	return false;
}

function isMail(elem){
	var at;
	var mail = elem.value;
	if(!isEmpty(elem)){
 		at=mail.indexOf('@');
		if(!at){
			return false;
		}
		return true;
	}
	return false; 
}

function isNum(str){
	for(var i=0; i<str.length; i++){
		var digit = str.charAt(i);
		switch(digit){
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case ' ':
		case '-':
			break;
		default:
			return false;
		}
 	}
	return true; 
}

function isNumerical(str){
	var adlts = str.value;
	if(adlts > 0)
		return true;
	else{
		return false;
	}
}

function showToCalendar(name){
	if(!document.getElementsByName('fromDate')[0].value)
		return alert(Dictionary.fillArrivalFirst);
	if(!document.getElementsByName('toDate')[0].value)
		document.getElementsByName('toDate')[0].value = document.getElementsByName('fromDate')[0].value;
	showCal(name);
}

function getDateFromString(s){
	s = s.split('/');
	var d = new Date();
	d.setFullYear(s[2]);
	d.setMonth(s[1]-1);
	d.setDate(s[0]);
	return d;
}

function fromCalValidate(elm){
	var from = getDateFromString(elm.value);
	var now = new Date();
	if(from.getTime() < now.getTime()){
		elm.value = '';
		alert(Dictionary.invalidArrival);
	}
}

function toCalValidate(efrom,eto){
	var from = getDateFromString(efrom.value);
	var to = getDateFromString(eto.value);
	if(to.getTime() < from.getTime()){
		eto.value = '';
		alert(Dictionary.invalidDeparture);
	}
}
