var first = 0;

function add_option(listname,showtxt,passvalue) {
	listname.length++; 
	listname.options[listname.length-1].value = passvalue; 
	listname.options[listname.length-1].text = showtxt; 
}

function setDefaultYear(){
 	aForm = document.getElementById("search");
 	bForm = document.getElementById("activities");
 	if(aForm){
 		var now = new Date();
 		now.setDate(now.getDate()+3);
 		aForm.aday.selectedIndex = now.getDate();
 		aForm.amonth.selectedIndex = now.getMonth()+1;
 		aForm.ayear.selectedIndex = 1;
	}
 	if (bForm){
 		var now = new Date();
 		//now.setDate(now.getDate()+2);
 		bForm.aday.selectedIndex = now.getDate();
 		bForm.amonth.selectedIndex = now.getMonth()+1;
 		bForm.ayear.selectedIndex = 1;
 	}
}

function isEnter(e){
 	var keynum;
 	if(window.event) // IE
 		keynum = e.keyCode;
 	else if(e.which) // Netscape/Firefox/Opera
	 	keynum = e.which;
 	if(keynum == 13)
 		return true;
 	return false;
}

function act_getStay() {
 	aForm = document.getElementById("activities");
 	if(aForm){
	 	if((aForm.aday.value != "Select") && (aForm.amonth.value != "Select") && (aForm.ayear.value != "Select")) {		
			var m, d, y, tmp, t, ma, da,mplusone;
			d = new Number(aForm.aday.value);
			m = new Number(aForm.amonth.value);
			y = new Number(aForm.ayear.value);
			if(d < 10){
				aForm.act_date.value = "0" + d + "/";
			}else {
				aForm.act_date.value = d + "/";
			}
			if(m < 9) {
			 	aForm.act_date.value += "0" + (m+1) + "/" + y;
			}else {
				aForm.act_date.value += (m+1) + "/" + y;
			}
			ok = isNaN(y);		
			if (ok) {
				alert("Date must be in format dd/mm/yyyy");
				aForm.act_date.value = "";
			}else {
				newDate = new Date(y, m , d);
				newDate.setDate((newDate.getDate()+num));
				var mt = newDate.getMonth()+1;
				var dt = newDate.getDate();
				var yr = newDate.getFullYear();
				if(mt < 10) mt = "0" + mt;
				if(dt < 10) dt = "0" + dt;
				aForm.aday.selectedIndex=dt-1;
				aForm.amonth.selectedIndex=mt-1;
				for(i=0;i<aForm.dyear.length;i++){
					if(aForm.ayear.options[i].value==yr){
						aForm.ayear.selectedIndex=i;
						break;
					}
				}			
	 		}
 		}
 	}
}

function getStay() {
 	aForm = document.getElementById("search");
 	if(aForm){
 		if ((aForm.stay.value == "nights") || (aForm.stay.value == "Select")) {
  			aForm.ddate.value == "";
		} else{
 			if((aForm.aday.value != "Select") && (aForm.amonth.value != "Select") && (aForm.ayear.value != "Select")) {
  				var num = new Number(aForm.stay.value);
				var m, d, y, tmp, t, ma, da,mplusone;
  				d = new Number(aForm.aday.value);
  				m = new Number(aForm.amonth.value);
  				y = new Number(aForm.ayear.value);
  				if(d < 10) {
  					aForm.adate.value = "0" + d + "/";
				}else {
					aForm.adate.value = d + "/";
				}
  				if(m < 9) {
  					aForm.adate.value += "0" + (m+1) + "/" + y;
				}else {
					aForm.adate.value += (m+1) + "/" + y;
				}
  				ok = isNaN(y);
  				if (ok) {
  					alert("Date must be in format dd/mm/yyyy");
  					aForm.adate.value = "";
  				} else {
  					if (num !== "nights") {
  						newDate = new Date(y, m , d);
  						newDate.setDate((newDate.getDate()+num));
  						var mt = newDate.getMonth()+1;
						var dt = newDate.getDate();
						var yr = newDate.getFullYear();
  						if(mt < 10) mt = "0" + mt;
  						if(dt < 10) dt = "0" + dt;
  						aForm.ddate.value = dt + "/" + mt + "/" + yr;
  						if(aForm.dday && aForm.dmonth && aForm.dyear){
  							aForm.dday.selectedIndex=dt-1;
							aForm.dmonth.selectedIndex=mt-1;
							for(i=0;i<aForm.dyear.length;i++){
   								if(aForm.dyear.options[i].value==yr){
  									aForm.dyear.selectedIndex=i;
  									break;
   								}
							}
						}
  					}
 				}
			}
		}
 	}
}

function messages(mess) {
 	if (mess == "x") message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
 	else message.innerText = mess;
}

function messages(mess) {
 	if (mess == "x") message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
 	else message.innerText = mess;
}

function checkField(evt) {
 	if(evt.value == "") {
 		message.innerText = "!! You cannot leave this field blank !!";
 		evt.style.backgroundColor = "mistyrose";
	} else {
 		message.innerText = ".:Studios92.com Ltd Secure Online Booking Form:.";
 		evt.style.backgroundColor = "aliceblue";
 	}
}

function validate() {
 	aForm = document.getElementById("search");
 	if (aForm.acctype && aForm.acctype.value == "Select") {
 		alert("You did not select an accommodation type!");
 	} else if (aForm.location && aForm.location.value == "Location") {
	 	alert("You did not select a location");
 	} else if (aForm.ddate.value == "") {
 		alert("Your departure date has unselected values, please check it carefully");
	} else if (!validateDate()){
 		alert("Please select at least 3 days ahead for your arrival date!")
 	} else if (!checkDateExist()) {
 		alert("Arrival date does not exist !");
 	} else if (aForm.stay.value == "nights") {
 		alert("You did not select your length of stay!");
 	} else if (aForm.people.value == "Select") {
 		alert("You did not select the number of people");
	} else {
 		var xMax = screen.width, yMax = screen.height;
 		var xOffset = (xMax - 270)/2, yOffset = (yMax - 70)/2;
 		//window.open('popups/finding.htm','finding','width=270,height=70,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
 		aForm.submit();
 		//finding.focus();
 	}
}

function condition() {
		aForm = document.getElementById("search");
		if ((aForm.stay.value < 3) && (aForm.amonth.value == 11) && ((aForm.aday.value == 29) || (aForm.aday.value == 30) || (aForm.aday.value == 31))){
			alert("You have to stay at least 3 nights if you are planning to arrive on 29th, 30th or 31st of December.");
		}else {
			validate();
		}
}

function validateAct(){
	aForm = document.getElementById("activities");
	if (!validateActDate()){
 		alert("Please don't selecting a date in the past!")
 	}else aForm.submit();
}

function fillstay() {
	var iMinStay;
	//CHANGE 7 IF YOU WANT TO CHANGE THE NUMBER MINIMUM OF NIGHTS TO BOOK A FLAT
	iMinStay = 7;
 	aForm = document.getElementById("search"); 
 	if(aForm){
  		if(aForm.stay){
  			var it = aForm.stay.selectedIndex;
  			aForm.ddate.value = "";
  			for(i=aForm.stay.options.length; i >= 0; i--) {
  				aForm.stay.options[i] = null;
  			}
  		}
  		for(i=1; i< 151; i++) {
  			add_option(aForm.stay,i,i);
  			if (aForm.acctype){ if (i < iMinStay && aForm.acctype.value == "Studios"){
  				aForm.stay.options[aForm.stay.length - 1].disabled = "disabled";
  			}}
  		}
  		if(aForm.acctype){
  			if (aForm.acctype.value == "Studios") {
  				getLoc("fs");
  				if(it > iMinStay) {
  					aForm.stay.selectedIndex = it;
				}else{
					aForm.stay.selectedIndex = iMinStay -1;
				}
  			} else {
				if (aForm.acctype.value == "All") getLoc("all");
  				if (aForm.acctype.value == "Hotel / Bed and Breakfast / Guest House") getLoc("hb");
  				if (aForm.acctype.value == "Luxury Hotels") getLoc("lh");
  				if (aForm.acctype.value == "Youth Hostel") getLoc("h");
  				if (aForm.acctype.value == "Apartments") getLoc("a");
  				if (aForm.acctype.value == "Campsites / Caravans / Resorts") getLoc("c");
  				if (aForm.acctype.value == "YHA / Youth hostels Association") getLoc("yh");
  				aForm.stay.selectedIndex = it;
  			}
  		}
 	}
 }

function compare(date_1, date_2){
	diff = date_1.getTime()-date_2.getTime();
 	return (diff==0?diff:diff/Math.abs(diff));
}

function validateDate(){
 	aForm = document.getElementById("search");
 	if((aForm.aday.value != "Select") && (aForm.amonth.value != "Select") && (aForm.ayear.value != "Select")) {
 		var day = aForm.aday.value;
 		var month = aForm.amonth.value;
 		var year = aForm.ayear.value;
 		var arrivalDate = new Date(year,month,day);
 		arrivalDate.setDate(arrivalDate.getDate()-1);
 		if (compare(arrivalDate,new Date()) < 0) return false;
 		else return true;
 	}else
 	return false;
}

function validateActDate(){
	aForm = document.getElementById("activities");
	if((aForm.aday.value != "Select") && (aForm.amonth.value != "Select") && (aForm.ayear.value != "Select")) {
		var day = aForm.aday.value;
 		var month = aForm.amonth.value;
 		var year = aForm.ayear.value;
 		var arrivalDate = new Date(year,month,day);
 		arrivalDate.setDate(arrivalDate.getDate()+1);
 		if (compare(arrivalDate,new Date()) < 0){
 			return false;
		}else return true;
 	}else
 	return false;
}

function checkDateExist() {
	aForm = document.getElementById("search");
	//specific check if february is selected (number 1)
	if (aForm.amonth.value == 1) {
		if ( ((aForm.ayear.value % 4) == 0) && (aForm.aday.value > 29) ) {  //if a year where february has 29 days
			return false;
		} else if (aForm.aday.value > 28) {   //normal year
			return false;
		} else {	//other days of the month
			return true;
		}
	}
	//check if month between January and July
	if (aForm.amonth.value <= 6) {
		if ((aForm.aday.value == 31) && ((aForm.amonth.value % 2) != 0)) {  //months which have even have 30 days
			return false;
		} else {
			return true;
		}
	} else if (aForm.amonth.value > 6) {
		if ((aForm.aday.value == 31) && ((aForm.amonth.value % 2) == 0) ) {	//months which have odd have 30 days
			if (aForm.amonth.value != 7) { //specific case of august
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}
}

function openWin(url,name){
 	window.open(url,name,"");
}

function popUp(url,name,width,height){
 	if(width!=true) width=680;
 	if(height!=true) height=650;
 	window.open(url,name,'resizable=yes,scrollbars=yes,width='+width+',height='+height);
}

function sel(ths, at,stay) {
 	var xMax = screen.width, yMax = screen.height;
 	var xOffset = (xMax - 270)/2, yOffset = (yMax - 70)/2;
 	if (((at == "92Z2") || (at == "BU3") || (at == "GA141") || (at == "HA7") || (at == "MA29")|| (at == "MH2") || (at == "MI107") || (at == "NO98") || (at == "SH55") || (at == "SH57") || (at == "PR104")) && (stay < 7)) {
 		alert("This option has a minimum stay of 7 nights. \nPlease click back and change your number of nights!");//memo giuliano
	} else {
 		document.getElementById('booking').aut.value=at;
 		document.getElementById('booking').name.value=ths;
 		//window.open("viewDetails.asp?aut=" + at + "&name=" + ths + "&pax=<%=pax%>&a=<%=a%>&d=<%=d%>&stay=<%=stay%>&search=yes&refered=<%=fr%>", "_self");
 		document.getElementById('booking').submit();
 		window.open('popups/waiting.asp?field=2','waiting','width=270,height=70,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
 	}
}

window.onload = function(){ setDefaultYear();fillstay();getStay();act_getStay();};