//new location
var rating = "Rating:";
var usr_review = "User Rating";
var reviews = "Reviews";
var total_price = "Total price from:";
var actLink1 = "";
var actLink2 = "";
var err_checkin_checkout = 'Please specify check-in and check-out date';
var err_room_number = 'Please specify the number of rooms';
var err_person_number = 'Please specify the number of persons';
var err_room_le_person = 'Number of persons must be greater or equal of number of rooms';
var err_max_quadruple = 'Only from single to quadruple rooms are allowed!';
var err_wrong_ci_90max = 'Please note that the reservation cannot be longer than 90 days';
var go_submit = 0;


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function setMultipleValues3(y,m,d) {
     document.forms[0].date11_year.value=y;
     document.forms[0].date11_month.selectedIndex=m;
     document.forms[0].date11_date.selectedIndex=d;
}



function set_hidden()
{
     gd= window.document.vSearch.cb_sd.value;
     window.document.vSearch.sd.value=gd;
     gx= window.document.vSearch.cb_smy.value;
     gm=gx.substring(0,2);  // il mese di ci
     gy=gx.substring(3,7);  // anno di ci
     window.document.vSearch.sm.value=gm;
     window.document.vSearch.sy.value=gy;
     gd1= window.document.vSearch.cb_ed.value;
     window.document.vSearch.ed.value=gd1;
     gx1= window.document.vSearch.cb_emy.value;
     gm1=gx1.substring(0,2);  // il mese di ci
     gy1=gx1.substring(3,7);  // anno di ci
     window.document.vSearch.em.value=gm1;
     window.document.vSearch.ey.value=gy1;
}


/*Jumpy combo box credit:
Website Abstraction (www.wsabstract.com)
Over 200+ free JavaScripts here!
*/

function gone(select_field)
{
	window.location=select_field.value;
}

function verifyRequired() 
{
			if (document.icpsignup.fields_email.value == "") 
	{
 				alert("The Email field is required.");
 				return false;
			}
	return true;
}

 


function currentCity() 
{
	populateCities(document.getElementById('country-select'));
}

function populateCities(select) {
	var current_city_id = false;
	if(current_city_id) {
		var country = select.value;
		var city_select = document.getElementById('city-select'); 
		city_select.options.length = 0; 
		for(index in c[country]) { 
			var code = c[country][index][0];
			var city_name = c[country][index][1];
			if(code == current_city_id) {
				city_select.options[city_select.options.length] = new Option(city_name,code,false,true);
			} else {
				city_select.options[city_select.options.length] = new Option(city_name,code,false,false);
			}
		}
	} else {
		return false;
	}
}

function updateDate() 
{
	var thirtyones = [1,3,5,7,8,10,12]; //months with 31 days...
	var thirties = [4,6,9,11];
	var checkout_my_array = [];
	var mod_num = 0;

	//-- Stuff from the form --
	var ci_day = document.getElementById('checkin-day').value;
	var ci_monthyear = document.getElementById('checkin-monthyear').value;
	var co_day = document.getElementById('checkout-day').value;
	var co_monthyear = document.getElementById('checkout-monthyear').value;
	//---- Split the Month_Year variables
	var checkin_my_array = ci_monthyear.split('-');
	var checkin_month = parseInt('1' + checkin_my_array[0]) - 100;
	var checkin_year = parseInt('1' + checkin_my_array[1]) - 100;
	var checkout_my_array = co_monthyear.split('/');
	var checkout_month = parseInt('1' + checkout_my_array[0]) - 100;
	var checkout_year = parseInt('1' + checkout_my_array[1]) - 100;
	//-----Make the strings numbers...
	co_day = parseInt(co_day);
	ci_day = parseInt(ci_day);		
	
	for(var i = 0; i < thirtyones.length; i++){
		if(checkin_month == thirtyones[i])
			var is_thirtyones = true;
		if((checkin_month + 1) == thirtyones[i])
			mod_num = 31;
	}
	for(var i = 0; i < thirties.length; i++){
		if(checkin_month == thirties[i])
			var is_thirties = true;
		
		if((checkin_month + 1) == thirties[i])
			mod_num = 30;
	}
	if((checkin_month == 9) || (checkin_month == 4) || (checkin_month == 6) || (checkin_month == 11))
		mod_num = 30;
	if((checkin_month == 2))
		mod_num = 28;
	if((checkin_month == 12) || (checkin_month == 1) || (checkin_month == 3) || (checkin_month == 5) || (checkin_month == 7) || (checkin_month == 8) || (checkin_month == 10))
		mod_num = 31;
	if(ci_day + 2 == mod_num)
		co_day = mod_num
	else
	{
		co_day = (ci_day + 2) % mod_num;
	}
	
	if((checkin_month == 12) && ((ci_day + 2) > 31))
		checkout_month = 1;
	else if((checkin_month == 2) && ((ci_day + 2) > 28))
		checkout_month = 3;
	else if(is_thirties && ((ci_day + 2) > 30))
		checkout_month = checkin_month + 1;
	else if(is_thirtyones && ((ci_day + 2) > 31))
		checkout_month = checkin_month + 1;
	else
		checkout_month = checkin_month;

	
	var ci_day_select = document.getElementById('checkin-day');
	// Rpopulate the checkin day based on what month is selected.
	if(checkin_month == 2){
		ci_day_select.options.length = 0;
		for(var i = 1; i < 29; i ++) 
		{ 
			if(i == ci_day)
				ci_day_select.options[ci_day_select.options.length] = new Option(i, i, false, true);
			else
				ci_day_select.options[ci_day_select.options.length] = new Option(i,i, false, false);
		}
	}
	else if(is_thirtyones){
		ci_day_select.options.length = 0;
		for(var i = 1; i < 32; i ++) 
		{ 
			if(i == ci_day)
				ci_day_select.options[ci_day_select.options.length] = new Option(i, i, false, true);
			else
				ci_day_select.options[ci_day_select.options.length] = new Option(i,i, false, false);
		}
	}else{
		ci_day_select.options.length = 0;
		for(var i = 1; i < 31; i ++) 
		{ 
			if(i == ci_day)
				ci_day_select.options[ci_day_select.options.length] = new Option(i, i, false, true);
			else
				ci_day_select.options[ci_day_select.options.length] = new Option(i,i, false, false);

		}
	}


	var co_my_select = document.getElementById('checkout-monthyear');
	var temp_month = 0;
	for(var i = 0; i < co_my_select.options.length; i++)
	{
		var temp_my_array 	= co_my_select.options[i].value.split('/');
		var temp_month 		= parseInt('1' + temp_my_array[0]) - 100;

		if(checkout_month == temp_month)
			co_my_select.options[i].selected = true;

		my_alert_output = "temp_month:" + temp_month + "\n checkout_month:" + checkout_month;
		//alert(my_alert_output);
	}
	
	//alert(checkout_month - checkin_month);
	my_alert_output = "ci_day:" + ci_day + "\n" + "co_day:" + co_day + "\nmod_num:" + mod_num + "\n (checkin_month + 1):" + (checkin_month + 1) + "\n" + " checkout_month:" + checkout_month;
	my_alert_output = "checkin_month:" + checkin_month + "\n checkout_month:" + checkout_month;
	
	my_alert_output = "temp_month:" + temp_month + "\n i:" + i;		


	//my_alert_output = "checkin_month:" + checkin_month + "\n" + "is_thirties:" + is_thirties + "\nis_thirtyones:" + is_thirtyones;		
	//alert(my_alert_output);
	
	for(var i = 0; i < thirtyones.length; i++){
		if(checkin_month == thirtyones[i])
			var co_is_thirtyones = true;
	}
	for(var i = 0; i < thirties.length; i++){
		if(checkin_month == thirties[i])
			var co_is_thirties = true;
	}
	
	var co_day_select = document.getElementById('checkout-day');
	if(checkout_month == 2){
		co_day_select.options.length = 0;
		for(var i = 1; i < 29; i ++) 
		{ 
			if(i == co_day)
				co_day_select.options[co_day_select.options.length] = new Option(i, i, false, true);
			else
				co_day_select.options[co_day_select.options.length] = new Option(i,i, false, false);
		}
	}
	else if(co_is_thirtyones){
		co_day_select.options.length = 0;
		for(var i = 1; i < 32; i ++) 
		{ 
			if(i == co_day)
				co_day_select.options[co_day_select.options.length] = new Option(i, i, false, true);
			else
				co_day_select.options[co_day_select.options.length] = new Option(i,i, false, false);
		}
	}else{
		co_day_select.options.length = 0;
		for(var i = 1; i < 31; i ++)
		{ 
			if(i == co_day)
				co_day_select.options[co_day_select.options.length] = new Option(i, i, false, true);
			else
				co_day_select.options[co_day_select.options.length] = new Option(i,i, false, false);

		}
	}
}

function updateRatesDate() {
	var thirtyones = [1,3,5,7,8,10,12]; //months with 31 days...
	var thirties = [4,6,9,11];
	var checkout_my_array = [];

	//-- Stuff from the form --
	var ci_day = document.getElementById('rates-checkin-day').value;
	var ci_monthyear = document.getElementById('rates-checkin-monthyear').value;
	var co_day = document.getElementById('rates-checkout-day').value;
	var co_monthyear = document.getElementById('rates-checkout-monthyear').value;
		
	//---- Split the Month_Year variables
	var checkin_my_array = ci_monthyear.split('-');
	var checkin_month = parseInt('1' + checkin_my_array[1]) - 100;
	var checkin_year = parseInt(checkin_my_array[0]);
	var checkout_my_array = co_monthyear.split('-');
	var checkout_month = parseInt('1' + checkout_my_array[1]) - 100;
	var checkout_year = parseInt(checkout_my_array[0]);
	
	for(var i = 0; i < thirtyones.length; i++){
		if(checkin_month == thirtyones[i])
			var ci_is_thirtyones = true;
	}
	for(var i = 0; i < thirties.length; i++){
		if(checkin_month == thirties[i])
			var ci_is_thirties = true;
	}

	var ci_day_select = document.getElementById('rates-checkin-day');
	var ci_my_select = document.getElementById('rates-checkin-monthyear');

	// Rpopulate the checkin day based on what month is selected.
	var padded_value;
	if(checkin_month == 2) {
		ci_day_select.options.length = 0;
		for(var i = 1; i < 29; i ++) {
			padded_value = (i < 10 ? '0' + i : i);	
			if(i == ci_day)
				ci_day_select.options[ci_day_select.options.length] = new Option(i, padded_value, false, true);
			else
				ci_day_select.options[ci_day_select.options.length] = new Option(i, padded_value, false, false);
		}
	} else if(ci_is_thirtyones) {
		ci_day_select.options.length = 0;
		for(var i = 1; i < 32; i ++) {
			padded_value = (i < 10 ? '0' + i : i);
			if(i == ci_day) {
				ci_day_select.options[ci_day_select.options.length] = new Option(i, padded_value, false, true);
			} else {
				ci_day_select.options[ci_day_select.options.length] = new Option(i, padded_value, false, false);
			}
		}
	} else {
		ci_day_select.options.length = 0;
		for(var i = 1; i < 31; i ++) {
			padded_value = (i < 10 ? '0' + i : i);
			if(i == ci_day)
				ci_day_select.options[ci_day_select.options.length] = new Option(i, padded_value, false, true);
			else
				ci_day_select.options[ci_day_select.options.length] = new Option(i, padded_value, false, false);
		}
	}

	var now = new Date();
	var ci_date = new Date(checkin_year,checkin_month - 1,ci_day,3,3,3,3);
	var two_days_later = new Date(ci_date.getFullYear(),(ci_date.getMonth()),(ci_date.getDate() + 2),3,3,3,3);
	//var two_days_later = new Date();
	//two_days_later.setDate(ci_date.getDate()+14);
	//alert(two_days_later);
	var month_names = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
	var co_my_select = document.getElementById('rates-checkout-monthyear');
	//alert("check in day: " + ci_day + " two_days_later:" + two_days_later.getDate());
	co_my_select.options.length = 0;
	for(var i = 0; i < 12; i++){
		var temp_date = new Date(now.getFullYear(),(now.getMonth() + i),1,3,3,3,3);
		//var temp_date = new Date();
		//temp_date.setDate(temp_date.getDate()+14);
		var display_text 	= month_names[temp_date.getMonth()] + " " + temp_date.getFullYear();
		var temp_month_val = temp_date.getMonth() + 1; // js months start at 0.
		
		if(temp_month_val < 10)
			var month_str = "0" + temp_month_val;
		else
			var month_str = temp_month_val;
		
		
		
		var display_year = temp_date.getFullYear();
		
		if(display_year > now.getFullYear())
			display_year = now.getFullYear() + 1;
		
		
		var option_val 	= display_year + "-" + month_str;
		
		//alert("display_year" + display_year + "\nnow.getYear()" + now.getFullYear());
		
		//alert("Temp date month:" + temp_date.getMonth() + "\nTwo days month:" + two_days_later.getMonth() + "\n i: " + i);
		
		if(temp_date.getMonth() == two_days_later.getMonth())
			co_my_select.options[co_my_select.options.length] = new Option(display_text, option_val, false, true);
		else
			co_my_select.options[co_my_select.options.length] = new Option(display_text, option_val, false, false);
		
	}

	for(var i = 0; i < thirtyones.length; i++){
		if((two_days_later.getMonth() + 1) == thirtyones[i])
			var co_is_thirtyones = true;
	}
	for(var i = 0; i < thirties.length; i++){
		if((two_days_later.getMonth() + 1) == thirties[i])
			var co_is_thirties = true;
	}
	
	var co_day_select = document.getElementById('rates-checkout-day');
	// Rpopulate the checkin day based on what month is selected.
	if((two_days_later.getMonth() + 1) == 2) {
		co_day_select.options.length = 0;
		padded_value = (i < 10 ? '0' + i : i);
		for(var i = 1; i < 29; i ++) {
			if(i == two_days_later.getDate())
				co_day_select.options[co_day_select.options.length] = new Option(i, padded_value, false, true);
			else
				co_day_select.options[co_day_select.options.length] = new Option(i, padded_value, false, false);
		}
	} else if(co_is_thirtyones) {
		co_day_select.options.length = 0;
		for(var i = 1; i < 32; i ++) {
			padded_value = (i < 10 ? '0' + i : i);
			if(i == two_days_later.getDate())
				co_day_select.options[co_day_select.options.length] = new Option(i, padded_value, false, true);
			else
				co_day_select.options[co_day_select.options.length] = new Option(i, padded_value, false, false);
		}
	} else {
		co_day_select.options.length = 0;
		for(var i = 1; i < 31; i ++) {
			padded_value = (i < 10 ? '0' + i : i);
			if(i == two_days_later.getDate())
				co_day_select.options[co_day_select.options.length] = new Option(i, padded_value, false, true);
			else
				co_day_select.options[co_day_select.options.length] = new Option(i, padded_value, false, false);
		}
	}

	//update the calendars
	var coMonthYear = co_my_select.options[co_my_select.selectedIndex].value;
	var coDay = co_day_select.options[co_day_select.selectedIndex].value;

	var ciMonthYear = ci_my_select.options[ci_my_select.selectedIndex].value;
	var ciDay = ci_day_select.options[ci_day_select.selectedIndex].value;


	setCalendarDates(YAHOO.getrates.calendar.cal1, ciMonthYear, ciDay);
	setCalendarDates(YAHOO.getrates.calendar.cal2, coMonthYear, coDay);
	return true;
}

function setCalendarDates(cal, monthYear, day) {
	var parts = monthYear.split("-");
	var selectedDate = parseInt(parts[1]) + '/' + parseInt(day) + '/' + parseInt(parts[0]);
	var pageDate = parseInt(parts[1]) + '/' + parseInt(parts[0]);

	cal.cfg.setProperty("selected", selectedDate, false);//what date will be highlighted
	cal.cfg.setProperty("pagedate", pageDate, false);//what page will show
	cal.render();
	return true;
}

function hideCals() {
	YAHOO.getrates.calendar.cal1.hide();
	YAHOO.getrates.calendar.cal2.hide();
}


//get rates calendars
//handle popup calendar
YAHOO.namespace("getrates.calendar");
function getrates_calendars() {
	//hack to get this working only on getrates pages - JE
	if(document.getElementById('getrates_checkincal')) {
		YAHOO.getrates.calendar.cal1 = new YAHOO.widget.CalendarGroup("checkincal","getrates_checkincal", { pages:2, title:'Check in date', close:true } );
		YAHOO.getrates.calendar.cal1.render();
		YAHOO.getrates.calendar.cal2 = new YAHOO.widget.CalendarGroup("checkoutcal","getrates_checkoutcal", { pages:2, title:'Check out date', close:true } );
		YAHOO.getrates.calendar.cal2.render();

		// click listeners
		//checkin calendar
		YAHOO.util.Event.addListener("getrates_showcheckin", "click", YAHOO.getrates.calendar.cal2.hide, YAHOO.getrates.calendar.cal2, true);
		YAHOO.util.Event.addListener("getrates_showcheckin", "click", YAHOO.getrates.calendar.cal1.show, YAHOO.getrates.calendar.cal1, true);

		//checkout calendar
		YAHOO.util.Event.addListener("getrates_showcheckout", "click", YAHOO.getrates.calendar.cal1.hide, YAHOO.getrates.calendar.cal1, true);
		YAHOO.util.Event.addListener("getrates_showcheckout", "click", YAHOO.getrates.calendar.cal2.show, YAHOO.getrates.calendar.cal2, true);

		//handle updating of selectors
		YAHOO.getrates.calendar.cal1.selectEvent.subscribe(getrates_calendars_selector, YAHOO.getrates.calendar.cal1, true);
		YAHOO.getrates.calendar.cal2.selectEvent.subscribe(getrates_calendars_selector, YAHOO.getrates.calendar.cal2, true);
	}
}
YAHOO.util.Event.addListener(window, "load", getrates_calendars);

function getrates_calendars_selector(type,args,obj) {
	var dates = args[0];
	var date = dates[0];
	var year = date[0], month = date[1], day = date[2];
	if(month < 10) month = '0' + month;
	if(day < 10) day = '0' + day;
	var monthYear = year + '-' + month;

	switch(obj.id) {
			case "checkincal":
			YAHOO.getrates.calendar.cal1.hide();//hide the check in calendar
			var selMonthYear = document.getElementById("rates-checkin-monthyear");
			var selDay = document.getElementById("rates-checkin-day");
			break;
		case "checkoutcal":
			YAHOO.getrates.calendar.cal2.hide();//hide the checkout calendar
			var selMonthYear = document.getElementById("rates-checkout-monthyear");
			var selDay = document.getElementById("rates-checkout-day");
			break;
		default:
			return false;
			break;
	}
	for (var my=0;my<selMonthYear.options.length;my++) {
		if (selMonthYear.options[my].value == monthYear) {
			selMonthYear.selectedIndex = my;
			break;
		}
	}

	for (var d=0;d<selDay.options.length;d++) {
		if (selDay.options[d].value == day) {
			selDay.selectedIndex = d;
			break;
		}
	}

	switch(obj.id) {
		case "checkincal":
			//call the existing onchange logic
			updateRatesDate();
			break;
		default:
			return false;
			break;
	}
}
//end get rates cal handlers