// JavaScript Document

// **********************************************************
// Gestione webservices date e hotel
// **********************************************************
function ClearOptions(OptionList) {
	//Always clear an option list from the last entry to the first
	for (x = OptionList.length; x >= 0; x = x - 1) {
		OptionList[x] = null;
	}
}

function AddToOptionList(OptionList, OptionValue, OptionText) {
	OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}

function CallbackDate(req)	{
	obj_form = document.getElementById("form"+req.userData.form);
	arr_date = req.xhRequest.responseText.split(',')
	if (req.userData.tipoData == 'a') {
		ClearOptions(obj_form.arrivo);
		AddToOptionList(obj_form.arrivo, "", str_moduloveloce_seleziona);
		ClearOptions(obj_form.partenza);
		for (i=0;i<arr_date.length;i+=2)	{
			AddToOptionList(obj_form.arrivo, arr_date[i], arr_date[i]+' '+arr_moduloveloce_giornisett[arr_date[i+1]]);
			if (arr_date[i+1] == 7) {
				obj_form.arrivo[obj_form.arrivo.length-1].style.backgroundColor = arr_moduloveloce_coloresabato;
			}
		}
	} else {
		ClearOptions(obj_form.partenza);
		AddToOptionList(obj_form.partenza, "", str_moduloveloce_seleziona);
		for (i=0;i<arr_date.length;i+=2)	{
			AddToOptionList(obj_form.partenza, arr_date[i], arr_date[i]+' '+arr_moduloveloce_giornisett[arr_date[i+1]]);
			if (arr_date[i+1] == 7) {
				obj_form.partenza[obj_form.partenza.length-1].style.backgroundColor = arr_moduloveloce_coloresabato;
			}
		}
	}
};

function CallbackHotel(req)	{
	obj_form = document.getElementById("form"+req.userData.form);
	arr_hotel = req.xhRequest.responseText.split(',')
		ClearOptions(obj_form.id_hotel);
		AddToOptionList(obj_form.id_hotel, "0", str_moduloveloce_seleziona_struttura);
		for (i=0;i<arr_hotel.length;i+=2)	{
			AddToOptionList(obj_form.id_hotel, arr_hotel[i], arr_hotel[i+1]);
	}
};
function popolaDate(form,tipoData) {
	obj_form = document.getElementById("form"+form);
	if (tipoData=='a') {
		ClearOptions(obj_form.arrivo);
		ClearOptions(obj_form.partenza);
		if (obj_form.id_hotel.value != '0') {
			AddToOptionList(obj_form.arrivo, "", str_moduloveloce_ricerca);
			AddToOptionList(obj_form.partenza, "", "");
		}
	} else {
		ClearOptions(obj_form.partenza);
		if (obj_form.id_hotel.value != '0') {
			AddToOptionList(obj_form.partenza, "", str_moduloveloce_ricerca);
		}
	}
	var obj_parametri = new Object;
	obj_parametri.tipoData = tipoData;
	obj_parametri.form = form;
//		alert("/ws_leggidate.asp?id_hotel=" + document.getElementById("hotel").value + "&id_data="+tipoData+"&data="+obj_form.arrivo.value)
	if (online) {
		var req = Spry.Utils.loadURL("GET", "/offerte.trentinoresidences.it/prg10/webservices/ws_leggidate.asp?id_hotel=" + obj_form.id_hotel.value + "&id_data="+tipoData+"&data="+obj_form.arrivo.value, true, CallbackDate, {userData: obj_parametri});
	} else {
		var req = Spry.Utils.loadURL("GET", "/offerte.trentinoresidences.it/prg10/webservices/ws_leggidate.asp?id_hotel=" + obj_form.id_hotel.value + "&id_data="+tipoData+"&data="+obj_form.arrivo.value, true, CallbackDate, {userData: obj_parametri});
	}
}

// **********************************************************
// Gestione selezione grandezza caratteri
// **********************************************************
function setActiveStyleSheet(title, quale) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"+quale)) {
      a.disabled = true;
      if(a.getAttribute("title"+quale) == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet(quale) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"+quale) && !a.disabled) return a.getAttribute("title"+quale);
  }
  return null;
}

function getPreferredStyleSheet(quale) {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title"+quale)
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet("");
  setActiveStyleSheet(title, "");
}

window.onunload = function(e) {
  var title = getActiveStyleSheet("");
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet("");
setActiveStyleSheet(title, "");

// **********************************************************
// VARIE
// **********************************************************
function newsletter_submit(lingua) { 
	var ret = Spry.Widget.Form.validate(document.getElementById('newsletter_form')); 
	if (ret) {
		var radioObj = document.getElementsByName('newsletter_azione');
		var radioLength = radioObj.length;
		for(var i = 0; i < radioLength; i++) {
			if(radioObj[i].checked) {
				var radioValue = radioObj[i].value;
			}
		};
		Spry.Utils.updateContent('div_newsletter', '/aspScripts/newsletter_azione.asp?newsletter_email=' + document.getElementById('newsletter_email').value + '&newsletter_azione=' + radioValue + '&lingua=' + lingua);
	} ;
	return false; 
};

// APT FIEMME BOOKING
var sr1 = "";

function fill_arrival() {
    str = document.forms["research"].arrivaldate.value;
    valori = str.split("_");
    if (valori[0].length < 2) {
        valori[0] = "0" + valori[0];
    }
    document.forms["research"].sd_D.value = valori[0];
    document.forms["research"].sd_M.value = valori[1];
    document.forms["research"].sd_Y.value = valori[2];
    document.forms["research"].f_date1.value = valori[0] + '/' + valori[1] + '/' + valori[2];
}

function fill_departure() {
    str = document.forms["research"].departure_date.value;
    valori = str.split("_");
    document.forms["research"].ed_D.value = valori[0];
    valori[1] = valori[1] - 1
    document.forms["research"].ed_M.value = valori[1] + "_" + valori[2];
}

function hide_accommodation() {
    var at = document.getElementById("accommodation-type");
    at.style.visibility = "hidden";
    sr1 = "package";
}

function show_accommodation() {
    var at = document.getElementById("accommodation-type");
    at.style.visibility = "visible";
    sr1 = "accommodation";
}

function onSubmit() {
    if (sr1 == "package") {
        var selInd = document.forms["research"].sr0.selectedIndex;
        var myOption = document.forms["research"].sr0[selInd];
        myOption.value = "package";
        //document.forms["research"].sr0.value = "package";
    }
    if (document.forms["research"].arrivaldate.value == "") {
        alert("Please select the arrival date")
    } else {
        document.forms["research"].submit();
    }
}


