/*window.onload = setScreenClass; 
window.onresize = setScreenClass;

function setScreenClass(){
        var fmt = document.documentElement.clientWidth;
        var cls = (fmt>320&&fmt<=640)?'main_ultralow':
                  (fmt>640&&fmt<=800)?'main_low':
                  (fmt>800&&fmt<=1024)?'main_med':
                  (fmt>1024&&fmt<=1280)?'main_high':'scaddBookmarkreen_wide';
        
        //document.getElementById('count').innerHTML=fmt+'px -> '+cls;
        document.getElementById('main').className=cls;
        document.getElementById('main_1').className=cls;
        document.getElementById('main_2').className=cls;
   
    };
*/
    
    if(!document.getElementById){
    if(document.all)
        document.getElementById=function(){
            if(typeof document.all[arguments[0]]!="undefined")
                return document.all[arguments[0]]
            else
                return null
        }
    else if(document.layers)
        document.getElementById=function(){
            if(typeof document[arguments[0]]!="undefined")
                return document[arguments[0]]
            else
                return null
        }
}

if(!document.getElementByName){
    if(document.all)
        document.getElementByName=function(){
            if(typeof document.all[arguments[0]]!="undefined")
                return document.all[arguments[0]]
            else
                return null
        }
    else if(document.layers)
        document.getElementByName=function(){
            if(typeof document[arguments[0]]!="undefined")
                return document[arguments[0]]
            else
                return null
        }
}

String.prototype.toInt = function(){
  return parseInt(this.replace(/^[^1-9]*|[^0-9]*$/, ""));
}

String.prototype.trim = function(){
  return this.replace(/^\s*|\s*$/g,"");
}

var num_giorni_mesi = new Array();
num_giorni_mesi[0] = 31;
num_giorni_mesi[1] = 28;
num_giorni_mesi[2] = 31;
num_giorni_mesi[3] = 30;
num_giorni_mesi[4] = 31;
num_giorni_mesi[5] = 30;
num_giorni_mesi[6] = 31;
num_giorni_mesi[7] = 31;
num_giorni_mesi[8] = 30;
num_giorni_mesi[9] = 31;
num_giorni_mesi[10] = 30;
num_giorni_mesi[11] = 31;

function popola_giorni(nome_select, mese, anno){
    oggetto_select = document.getElementById(nome_select);
    
    
    mese--;

    // controllo se l'anno è bisestile e se il mese è febbraio
    if((mese == 1) && (anno % 4 == 0))
        num_giorni_mesi[mese]++;
        
    if(oggetto_select.options.length >= num_giorni_mesi[mese]){
       
        while(oggetto_select.options.length > num_giorni_mesi[mese]){
            if(oggetto_select.selectedIndex == oggetto_select.options.length-1)
                oggetto_select.selectedIndex--;
            oggetto_select.options[oggetto_select.options.length-1] = null;
        }
    }
    else{

        for(i=oggetto_select.options.length; i<num_giorni_mesi[mese]; i++){
            opt_value = (i+1).toString()
            if(opt_value.length < 2)
                opt_value = "0"+opt_value;
            oggetto_select.options[i] = new myOption(i+1, opt_value);
        
        }
    }
}

/*
function controlla_date(){

    // allineo i mesi
    if(document.getElementById('mese_p').value < document.getElementById('mese_a').value){
        document.getElementById('mese_p').selectedIndex = document.getElementById('mese_a').selectedIndex;
        popola_giorni_ricerca_p();
        document.getElementById('giorno_p').selectedIndex = 0;
    }
    
    // allineo i giorni
    if(document.getElementById('mese_p').value <= document.getElementById('mese_a').value){
        
        if(document.getElementById('giorno_p').value <= document.getElementById('giorno_a').value){
            
            if(document.getElementById('giorno_p').options[document.getElementById('giorno_a').selectedIndex+1] == null){
                if(document.getElementById('mese_p').options[document.getElementById('mese_p').selectedIndex+1] == null){
                    document.getElementById('giorno_a').selectedIndex--;
                    document.getElementById('giorno_p').selectedIndex = document.getElementById('giorno_a').selectedIndex+1;
                }else{
                    document.getElementById('mese_p').selectedIndex++;
                    document.getElementById('giorno_p').selectedIndex = 0;
                    popola_giorni_ricerca_p();
                }
            }else{
                document.getElementById('giorno_p').selectedIndex = document.getElementById('giorno_a').selectedIndex+1;
            }
            
        }
        
    }
}
*/

/* Stefano Laficara 13 luglio 2006 - sostituita con interfaccia funzione generalizzata */
function popola_giorni_ricerca_a(){
/*  reg_popola_giorni = /([0-9]{4})([0-9]{2})/;
    
    temp = reg_popola_giorni.exec(document.getElementById('mese_a').value);
    popola_giorni('giorno_a', temp[2], temp[1]); */
    
    popola_giorni_ricerca( 'mese_a', 'giorno_a');
}
/* Stefano Laficara 13 luglio 2006 - sostituita con interfaccia funzione generalizzata */
function popola_giorni_ricerca_p(){
/*    reg_popola_giorni = /([0-9]{4})([0-9]{2})/;
    
    temp = reg_popola_giorni.exec(document.getElementById('mese_p').value);
    popola_giorni('giorno_p', temp[2], temp[1]);  */
    
    popola_giorni_ricerca( 'mese_p', 'giorno_p');
}

/* Stefano Laficara 13 luglio 2006 - generalizzata funzione */
function popola_giorni_ricerca( mese, giorno){
    reg_popola_giorni = /([0-9]{4})([0-9]{2})/;
    
    temp = reg_popola_giorni.exec(document.getElementById(mese).value);
     popola_giorni(giorno, temp[2], temp[1]);
}

function seleziona_valore(id_lista, valore){
    obj_lista = document.getElementById(id_lista);
    
    if(obj_lista == null)
        return false;
    
    obj_lista.selectedIndex = 0;
    
    for (i=0; i<obj_lista.options.length; i++)
        if (obj_lista.options[i].value.toInt() == valore){
            obj_lista.selectedIndex = i;
            return;            
        }
}

function is_email(email){
/*
    var regExpEmail = new RegExp(/^[a-zA-Z0-9_\-\.\+]+@[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,}$/);
    return regExpEmail.test(email);
*/

    // Recupero la posizione della @
    chicciola = email.indexOf('@');
    // non trovato
    if(chicciola == -1)
        return false;
    // primo carattere
    if(chicciola == 0)
        return false;
        
        
    // Recupero la posizione del . dopo @
    punto = email.substr(chicciola).indexOf('.');
    // non trovato
    if(punto == -1)
        return false;
    // primo carattere dopo @
    if(punto == 0)
        return false;
    // ultimo carattere
    if(punto == (email.substr(chicciola).length-1))
        return false;

    return true;
}

function obj_position(obj){
    var l=0; var t=0;
    
    aTag = obj;
    do {
        aTag = aTag.offsetParent;
        l += aTag.offsetLeft;
        t += aTag.offsetTop;
    } while (aTag.offsetParent && aTag.tagName != 'BODY');
    
    var left = obj.offsetLeft + l;
    var top = obj.offsetTop + t;
    
    return new Array(left, top);
}

function controlla_form(){
    mese = new Date().getFullYear().toString() + (parseInt(new Date().getMonth()) + 1).toString();

/*    
    if((parseInt(document.getElementById('mese_a').value) <= parseInt(mese)) && (parseInt(document.getElementById('giorno_a').value) < parseInt(new Date().getDate()))){
        alert("<%js_data_arrivo_errata%>");
        return false;
    }
 */  
    document.getElementById('checkin').value = document.getElementById('mese_a').value.substr(0,4)+'-'+document.getElementById('mese_a').value.substr(4,2)+'-'+document.getElementById('giorno_a').value;
    document.getElementById('checkout').value = document.getElementById('mese_p').value.substr(0,4)+'-'+document.getElementById('mese_p').value.substr(4,2)+'-'+document.getElementById('giorno_p').value;
    return true;
}


function controlla_data_partenza(){

    // Se il giorno di partenza è precedente a quello di arrivo
    // sposto il mese di partenza a quello successivo di arrivo
    
    giorno_a_valore = document.getElementById('giorno_a').value;
    giorno_p_valore = document.getElementById('giorno_p').value;

    if( (giorno_p_valore <= giorno_a_valore) && 
        (document.getElementById('mese_p').value <= document.getElementById('mese_a').value)){
        mese_successivo = document.getElementById('mese_a').selectedIndex+1;
        if(document.getElementById('mese_p').options[mese_successivo])
            document.getElementById('mese_p').selectedIndex = mese_successivo;
        
    }
    
}

function controlla_date(){

    
    
    // allineo i mesi
    if(document.getElementById('mese_p').value < document.getElementById('mese_a').value){
        document.getElementById('mese_p').selectedIndex = document.getElementById('mese_a').selectedIndex;
        popola_giorni_ricerca_p();
        document.getElementById('giorno_p').selectedIndex = 0;
    }
    
    // allineo i giorni
    if(document.getElementById('mese_p').value <= document.getElementById('mese_a').value){
        
        if(document.getElementById('giorno_p').value <= document.getElementById('giorno_a').value){
            
            if(document.getElementById('giorno_p').options[document.getElementById('giorno_a').selectedIndex+1] == null){
                if(document.getElementById('mese_p').options[document.getElementById('mese_p').selectedIndex+1] == null){
                    document.getElementById('giorno_a').selectedIndex--;
                    document.getElementById('giorno_p').selectedIndex = document.getElementById('giorno_a').selectedIndex+1;
                }else{
                    document.getElementById('mese_p').selectedIndex++;
                    document.getElementById('giorno_p').selectedIndex = 0;
                    popola_giorni_ricerca_p();
                }
            }else{
                document.getElementById('giorno_p').selectedIndex = document.getElementById('giorno_a').selectedIndex+1;
            }
            
        }
    }
    
    giorno_a_valore = document.getElementById('giorno_a').value;
    if(giorno_a_valore.length < 2)
        giorno_a_valore = "0"+giorno_a_valore;
    
    giorno_p_valore = document.getElementById('giorno_p').value;
    if(giorno_p_valore.length < 2)
        giorno_p_valore = "0"+giorno_p_valore;
    
    // traduco le date
//   document.ricerca_date.checkin.value = document.getElementById('mese_a').value.substr(0,4)+'-'+document.getElementById('mese_a').value.substr(4,2)+'-'+giorno_a_valore;
//   document.ricerca_date.checkout.value = document.getElementById('mese_p').value.substr(0,4)+'-'+document.getElementById('mese_p').value.substr(4,2)+'-'+giorno_p_valore;
    
}

function check_verifica(){
    document.getElementById('visualizza_disponibili').checked = true;
    document.getElementById('available').value = 1;
}

function popola_giorni_ricerca_a(){
    reg_popola_giorni = /([0-9]{4})([0-9]{2})/;
    
    temp = reg_popola_giorni.exec(document.getElementById('mese_a').value);
    //alert("-->" + temp[2] + "-" + temp[1]);
    popola_giorni('giorno_a', temp[2], temp[1]);
}

function popola_giorni_ricerca_p(){
    reg_popola_giorni = /([0-9]{4})([0-9]{2})/;
    
    temp = reg_popola_giorni.exec(document.getElementById('mese_p').value);
    popola_giorni('giorno_p', temp[2], temp[1]);
}

function accetta_solo_numeri(evento){
    if(evento.keyCode)
        keyCode = evento.keyCode;
    else if(evento.which)
        keyCode = evento.which;
    else
        return true;

    switch(keyCode){
        case null:
        case 0:
        case 8:
        case 9:
        case 13:
        case 27:
        case 37:
        case 39: 
        case 46:
            return true; 
            break;
    }
                   
    keyChar = String.fromCharCode(keyCode);
        
    if ((("0123456789").indexOf(keyChar) > -1))
        return true;
    else
        return false;
}

    //  Following transition classes will be declared:
    //
    //    classname          screenwidth
    //    ------------------------------------------
    //    pda_v              240px            
    //    pda_h              320px            
    //    ultralow          320px -  640px    
    //    screen_lo          640px -  800px    
    //    screen_med          800px - 1024px    
    //    screen_hi         1024px - 1280px    
    //    screen_wide                > 1280px            

//-----------------ToolTip mappa---------------------------   

function setInfoSingleRoom (id){
              
                            var infoRoom = [];
                            var  policieRoom  = []; 
                            var contain = document.getElementById('info_single_room_' + id);        
                            var info_room = document.getElementById('info_room_' + id);        
                          
                           if(info_room.style.display != 'block'){
                                
                                var listDiv = []; 
                                listDiv = contain.childNodes;
                                if(listDiv[0].nodeName == '#text' )
                                    contain.removeChild(listDiv[0]);
                                        
                                for (var i =0; i<listDiv.length; i++){
                                 
                                   
                                    if(typeof listDiv[i].className != "undefined")  {
                                        infoRoom [i]=   listDiv[i].className;
                                        listDiv[i].id =  infoRoom [i] +'_'+id ;
                                    }
                                    
                                    
                                    containList=listDiv[i];
         
                                    if(containList.hasChildNodes){
                                        childListDiv = containList.childNodes;
                                      
                                        for (var y = 0; y<childListDiv.length; y++){
    
                                            if(typeof(childListDiv[y].className != "undefined") || childListDiv[y].className!='label' ){

                                                    policieRoom[y] =   childListDiv[y].className;
                                                    
                                                    if( typeof policieRoom[y] != 'undefined'){   
                                                        childListDiv[y].id =  policieRoom[y] + '_' + id;
                                                    }
                                                    
                                                    var div = document.getElementById(childListDiv[y].id)
                                                    var divParent = document.getElementById('policies_'+id);
                                                    if(childListDiv[y].id == (('POLICY_TITLE_'+id)))
                                                        divParent.removeChild(div);

                                            }
                                        
                                        }
                                    
                                    }
                                
                       
                                
                                }
                               
                       
                                writeLabel (infoRoom, id);
                                writeLabel (policieRoom, id);
                          
                           }
                       
                       
                                      
                                if(info_room.style.display == 'block'){
                                    info_room.style.display = 'none';
                                    document.getElementById('info_hotel_'+id).src='/images/piu.gif'; 
                                }
                                else{
                                    info_room.style.display = 'block';
                                    document.getElementById('info_hotel_'+id).src='/images/meno.gif';
                                
                                }
                           
                 }
                 
                  function writeLabel (infoRoom , id){
                       
                       var info_room = document.getElementById('info_room_' + id); 
                       var checkLabel = document.getElementById('label_policy_'+id);
                       var ParentRoom = document.getElementById('info_single_room_' + id); 
                                            
                       /*var removeTheNode =  ParentRoom.childNodes;                      
                       alert('ll ' + removeTheNode.lenght);
                       for (y=0; y<removeTheNode.lenght; y++){
                        alert( 'className ' +  removeTheNode.className);
                       }  */
                    
                         if (checkLabel == null){
                     
                                for (i=0; i<infoRoom.length; i++){
                                    if(typeof infoRoom[i] != "undefined" ){
                                        var contain = document.getElementById(infoRoom[i] +'_'+ id );        
                                        label = getLabel();                                         
                                        
                                        if(typeof label[infoRoom[i]] != "undefined" ){
                                            
                                            /*if(contain.innerHTML == '' ){
                                                var divContain =  document.getElementById(contain.id).parentNode
                                                var toRemove = document.getElementById(contain.id);
                                                divContain.removeChild(toRemove);
                                            
                                            }*/
                                            
                                            if(contain.innerHTML != '' ){
                                                textPolicy = contain.innerHTML;
                                                textPolicy.toString();
                                                
                                                /////////////////////////////////////////////////////////////////////
                                                //  Posso inviare un alert da javascript nel caso sia aggiunta 
                                                //  Una nuova mailsend
                                                 
                                                //  if(label[infoRoom[i]] == null || label[infoRoom[i]] == '') 
                                                /////////////////////////////////////////////////////////////////////
                                                
                                                if(textPolicy.length > 1) {
                                                    if(!infoRoom[i].match('POLICY')){
                                                        text = "<span class='label' id='label_" + id + "'>"+ label[infoRoom[i]] + '</span>' + textPolicy ;
                                                    }
                                                    else{
                                                        text = "<span class='label' id='label_policy_" + id + "'>"+ label[infoRoom[i]] + '</span>' + textPolicy ;
                                                    }    
                                                        
                                                    contain.innerHTML =   text ;
                                                }
                                            }
                                        }
                                       
                                    }
                                } 
                                
                                /*if(i>9)
                                    sendJmail ();*/
                                                
                           
                           }   
                    }
//-----------------------------------------------------------------------------------------------------------------------------
//Funzione per loggare gli errori in javascript 
window.onerror = function(err, filename, line){
    
    // in genere questi sono errori di caricamento
    if (line==2) {
     if (err == "Carattere non valido" || err == "Invalid character"
      || err == "Errore di sintassi" || err == "Syntaxfehler"
      || err == "Syntax error" || err == "Caractère incorrect"
      || err == "Ungültiges Zeichen" || err == "Erreur de syntaxe"
      || err == "Erro de sintaxe" || err == "Error de sintaxis"
     )
     return;
    }

    // unknown source; probably in google analytics code
    if (line == 35 && (err.indexOf("'}'") != -1 || err.indexOf("')'") != -1) )
     	return;

   if ((err.indexOf("www.google-analytics.com/ga.js") != -1) || (filename.indexOf("www.google-analytics.com/ga.js") != -1))
        return;

    // non logghiamo gli errori di analytics
       if (err.indexOf("'_gat'")!=-1
        || err.indexOf("_gat")==0
        || err.indexOf('"_gat"')==0
        || err.indexOf("Error loading script")!=-1)
        return;

       log_error_js("["+filename+" @ linea: "+line+"] - ERRORE JS: "+err, true);
   }
//-----------------------------------------------------------------------------------------------------------------------------

   function log_error_js(errore, no_prefix){
       var request = mkXMLHttpRequest();

       if(request == null)
           return;

       if(no_prefix != true)
           errore = "ERRORE JS 1: "+errore;

       request.open("post", "../ajax/ajax.php", true);
       request.setRequestHeader("content-type", "application/x-www-form-urlencoded");
       request.setRequestHeader("connection", "close");
       request.send("type=error&errstr="+encodeURIComponent(errore));
   }

//-----------------------------------------------------------------------------------------------------------------------------
   
   
   // funzione per assegnare l'oggetto XMLHttpRequest
// compatibile con i browsers piÃ¹ recenti e diffusi
function mkXMLHttpRequest() {

    // lista delle variabili locali
    var
    // variabile di ritorno, nulla di default
    XHR = null,

    // informazioni sul nome del browser
    browserUtente = navigator.userAgent.toUpperCase();

    // browser standard con supporto nativo
    // non importa il tipo di browser
    if(typeof(XMLHttpRequest) === "function" || typeof(XMLHttpRequest) === "object")
        XHR = new XMLHttpRequest();

    // browser Internet Explorer
    // Ã¨ necessario filtrare la versione 4
    else if(
        window.ActiveXObject &&
        browserUtente.indexOf("MSIE 4") < 0
        ) {

            // la versione 6 di IE ha un nome differente
            // per il tipo di oggetto ActiveX
            if(browserUtente.indexOf("MSIE 5") < 0)
            XHR = new ActiveXObject("Msxml2.XMLHTTP");

            // le versioni 5 e 5.5 invece sfruttano lo stesso nome
        else
            XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }

    return XHR;
}

function show_room_hide(id){
    
    var isIE = false;
    if(navigator.userAgent.indexOf('Internet Explorer')){
        isIE = true;
    }
    
    if(!isIE)
        var showRoomHide = document.getElementsByName('paragrafo_room_hide_' + id); 
    else {
        //var showRoomHide = document.getElementsByName('paragrafo_room_hide_' + id); 
        var showRoomHide = getElementsByAttr('table', 'name', 'paragrafo_room_hide_' + id)
    }
    
    for(var i=0; i<showRoomHide.length; i++){
        showRoomHide[i].style.display='block';
        
    }
    
    
    
    var openLink = document.getElementById('open_link_'+ id); 
    openLink.style.display='none';

   
}

function getElementsByAttr(tag, attr, value){
   var arrayEl = new Array();
   var el = document.getElementsByTagName(tag);
   for(i=0, j=0; i<el.length; i++){
       if(el[i].getAttribute(attr) == value){
           arrayEl[j++] = el[i];
       }
   }
   return arrayEl;
} 

function myOption(par1, par2) {
	
	if(typeof Option == 'undefined') {
    	var elem = document.createElement('OPTION');
    	elem.text = par1;
    	elem.value = par2;
    	
    	return elem;
    	
	} else
    	return new Option(par1, par2);
}

function myImage() {
    if(typeof Image == 'undefined')
    	return document.createElement('IMG');
    else
    	return new Image();
}
