﻿// JScript File
function impostaLink(user){
    var sep='@';
    var dom='sicilianpropertyshop';
    var dot='.';
    var ext='com';
    var fullInd=user + sep + dom + dot + ext;
    var refh='mai';
    refh=refh+'lto';
    refh=refh+':'+fullInd;
    document.write("<a href='" + refh + "'>" + fullInd + "</a>"); 
}


function isEmail(str) {
  var supported = 0;
  if(window.RegExp) {
	var tempStr = "a";
	var tempReg = new RegExp(tempStr);
	if (tempReg.test(tempStr)) supported = 1;
  }
  //if(!supported)
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
			
  //var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  //var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3,4}|[0-9]{1,3})(\\]?)$");
  //return (!r1.test(str) && r2.test(str));
}


//verifica se una data è corretta o meno
function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?
    if (matchArray == null) {
        return false;
    }
    month = matchArray[3];
    day = matchArray[1];
    year = matchArray[5];
    if (month < 1 || month > 12) {
        return false;
    }
    if (day < 1 || day > 31) {
        return false;
    }
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        return false;
    }
    if (month == 2) {
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            return false;
        }
    }
    return true;
}


function validaEnquire(nameCont, theForm)
{
    var errore = false;
    var msg = "Following error(s) occurred:";
    
    var theFld = document.getElementById(nameCont + "_ddlFirstDateDay");
    var theDay = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlFirstDateMonth");
    var theMonth = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlFirstDateYear");
    var theYear = theFld.value;        
    var fullYear = String(theDay + "/" + theMonth + "/" + theYear)
    if(!isDate(fullYear)){
        errore = true;
        msg = msg + "\n - 1st Preferred Date is not valid";
    }   

    theFld = document.getElementById(nameCont + "_txtNumberPeople");
    if(theFld.value=='' || isNaN(theFld.value)){
        errore = true;
        msg = msg + "\n - Number of people entered is not valid";
    }   


    theFld = document.getElementById(nameCont + "_txtNumberChildren");
    if(isNaN(theFld.value)){
        errore = true;
        msg = msg + "\n - Number of children entered is not valid";
    }   

    theFld = document.getElementById(nameCont + "_txtSurname");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Contact Surname is missing";
    }   

    theFld = document.getElementById(nameCont + "_txtEmail");
    var theConfMail = document.getElementById(nameCont + "_txtEmailConf").value;

    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Contact Email is missing";
    }else if(!isEmail(theFld.value)){
        errore = true;
        msg = msg + "\n - Contact Email is bad typed";
    }else if(theFld.value!=theConfMail){
        errore = true;
        msg = msg + "\n - Confirm Email doesn't match Contact Email";
    }
    
    theFld = document.getElementById(nameCont + "_txtPhone");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Preferred Phone Number is missing";
    }   

    //dati brochure
    theFld = document.getElementById(nameCont + "_chkBrochure");
    if(theFld.checked){
        theFld = document.getElementById(nameCont + "_txtAddress");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - Postal address is required to send you our brochure";
        }   

        theFld = document.getElementById(nameCont + "_txtTown");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - Town/City name is required to send you our brochure";
        }   

        theFld = document.getElementById(nameCont + "_txtState");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - County/State name is required to send you our brochure";
        }   
    
        theFld = document.getElementById(nameCont + "_txtZip");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - Postcode/Zip name is required to send you our brochure";
        }   
    
    }


    if (errore){
        msg = msg + "\n Please fix these errors and submit again the form."; 
        alert(msg);
        return false;
    }else{
        return true;
    }    
        
}


function validaContatti(nameCont, theForm)
{
    var errore = false;
    var msg = "Following error(s) occurred:";
    
    var theFld


    theFld = document.getElementById(nameCont + "_txtSurname");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Contact Surname is missing";
    }   

    theFld = document.getElementById(nameCont + "_txtEmail");
    var theConfMail = document.getElementById(nameCont + "_txtEmailConf").value;

    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Contact Email is missing";
    }else if(!isEmail(theFld.value)){
        errore = true;
        msg = msg + "\n - Contact Email is bad typed";
    }else if(theFld.value!=theConfMail){
        errore = true;
        msg = msg + "\n - Confirm Email doesn't match Contact Email";
    }
    
    theFld = document.getElementById(nameCont + "_txtPhone");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Preferred Phone Number is missing";
    }   

    //dati brochure
    theFld = document.getElementById(nameCont + "_chkBrochure");
    if(theFld.checked){
        theFld = document.getElementById(nameCont + "_txtAddress");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - Postal address is required to send you our brochure";
        }   

        theFld = document.getElementById(nameCont + "_txtTown");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - Town/City name is required to send you our brochure";
        }   

        theFld = document.getElementById(nameCont + "_txtState");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - County/State name is required to send you our brochure";
        }   
    
        theFld = document.getElementById(nameCont + "_txtZip");
        if(theFld.value==''){
            errore = true;
            msg = msg + "\n - Postcode/Zip name is required to send you our brochure";
        }   
    
    }

    if (errore){
        msg = msg + "\n Please fix these errors and submit again the form."; 
        alert(msg);
        return false;
    }else{
        return true;
    }    
        
}

function validaNuovoProprietario(nameCont, theForm)
{
    var errore = false;
    var msg = "Sono stati riscontrati i seguenti errori:";
    

    var theFld = document.getElementById(nameCont + "_txtNome");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Non hai inserito il tuo nome";
    }   


    theFld = document.getElementById(nameCont + "_txtCognome");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Non hai inserito il tuo cognome";
    }   

    /*
    theFld = document.getElementById(nameCont + "_txtIndirizzo");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Non hai inserito il tuo indirizzo";
    }   
    */


    theFld = document.getElementById(nameCont + "_txtCitta");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Non hai inserito la tua città";
    }   


    theFld = document.getElementById(nameCont + "_txtProvincia");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Non hai inserito la tua provincia";
    }   

/*
    theFld = document.getElementById(nameCont + "_txtTelefono");
    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Non hai inserito il tuo numero di telefono";
    }   
*/



    theFld = document.getElementById(nameCont + "_txtEmail");
    var theConfMail = document.getElementById(nameCont + "_txtEmailConf").value;

    if(theFld.value==''){
        errore = true;
        msg = msg + "\n - Non hai inserito il tuo indirizzo e-mail";
    }else if(!isEmail(theFld.value)){
        errore = true;
        msg = msg + "\n - Hai inserito un indirizzo e-mail non corretto";
    }else if(theFld.value!=theConfMail){
        errore = true;
        msg = msg + "\n - L\'indirizzo e-mail e l\'e-mail di conferma non coincidono";
    }

    if (errore){
        msg = msg + "\n Correggi questi errori prima di inviare il modulo."; 
        alert(msg);
        return false;
    }else{
        return true;
    }    
        
}

function verificaFormPeriodo(nameCont){
    var errore = false;
    var msg = "Attenzione sono stati riscontrati i seguenti errori:";
    
    var theFld = document.getElementById(nameCont + "_ddlGiornoInizio");
    var theDay = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlMeseInizio");
    var theMonth = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlAnnoInizio");
    var theYear = theFld.value;        

    var dataPartenza = String(theDay + "/" + theMonth + "/" + theYear)
    var myDate1 = new Date()
    myDate1.setFullYear (theYear,theMonth-1,theDay)

    if(!isDate(dataPartenza)){
        errore = true;
        msg = msg + "\n - La data di inizio periodo non è valido";
    }   

    theFld = document.getElementById(nameCont + "_ddlGiornoFine");
    theDay = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlMeseFine");
    theMonth = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlAnnoFine");
    theYear = theFld.value;        
    var dataFine = String(theDay + "/" + theMonth + "/" + theYear)
    var myDate2 = new Date()
    myDate2.setFullYear (theYear,theMonth-1,theDay)

    if(!isDate(dataFine)){
        errore = true;
        msg = msg + "\n - La data di fine periodo non è valido";
    }   
    
    if(!errore)
    {
        if(myDate2.valueOf () < myDate1.valueOf()){
            errore = true;
            msg = msg + "\n - La data di fine periodo è precedente a quella di inzio";
        }
    }

    if (errore){
        msg = msg + "\n Correggi gli errori prima di procedere."; 
        alert(msg);
        return false;
    }else{
        return true;
    }    
}


function verificaFormPeriodoIns(nameCont){
    var errore = false;
    var msg = "Attenzione sono stati riscontrati i seguenti errori:";
    
    var theFld = document.getElementById(nameCont + "_ddlGiornoInizioIns");
    var theDay = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlMeseInizioIns");
    var theMonth = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlAnnoInizioIns");
    var theYear = theFld.value;        

    var dataPartenza = String(theDay + "/" + theMonth + "/" + theYear)
    var myDate1 = new Date()
    myDate1.setFullYear (theYear,theMonth-1,theDay)

    if(!isDate(dataPartenza)){
        errore = true;
        msg = msg + "\n - La data di inizio periodo non è valido";
    }   

    theFld = document.getElementById(nameCont + "_ddlGiornoFineIns");
    theDay = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlMeseFineIns");
    theMonth = theFld.value;        
    theFld = document.getElementById(nameCont + "_ddlAnnoFineIns");
    theYear = theFld.value;        
    var dataFine = String(theDay + "/" + theMonth + "/" + theYear)
    var myDate2 = new Date()
    myDate2.setFullYear (theYear,theMonth-1,theDay)

    if(!isDate(dataFine)){
        errore = true;
        msg = msg + "\n - La data di fine periodo non è valido";
    }   
    
    if(!errore)
    {
        if(myDate2.valueOf () < myDate1.valueOf()){
            errore = true;
            msg = msg + "\n - La data di fine periodo è precedente a quella di inzio";
        }
    }

    if (errore){
        msg = msg + "\n Correggi gli errori prima di procedere."; 
        alert(msg);
        return false;
    }else{
        return true;
    }    
}


function getId(nameCont, fieldName)
{
    var theObj;
    theObj = document.getElementById(nameCont + "_" + fieldName);
    return theObj;

}


function verificaFormVillaNew(nameCont,lng)
{
    var errore = false;
    
    var msg='';
    if(lng=='it'){
        msg = "Sono stati riscontrati degli errori: ";
    }else{
        msg = "Following error(s) occurred: ";
    } 
    
    currFld = getId(nameCont,"ddl_tipostruttura").value;
    if(currFld == 0 ){
        errore = true;
        
        if(lng=='it'){
           msg = msg + "\n - seleziona un [Tipo Immobile] dalla lista";
        }else{
            msg = msg + "\n - please choose a [PROPERTY TYPE] from the list";
        } 

    }

    var tipocontratto;
    currFld = getId(nameCont,"ddl_tipocontratto").value;
    tipocontratto=currFld;
    if(currFld == 0 ){
        errore = true;
        if(lng=='it'){
           msg = msg + "\n - seleziona un [Tipo Contratto] dalla lista";
        }else{
            msg = msg + "\n - please choose a [CONTRACT TYPE] from the list";
        } 
    }
    
    currFld = getId(nameCont,"villa_nome").value;
    if(currFld == ""){
        errore = true;
        if(lng=='it'){
           msg = msg + "\n - inserisci la [descrizione breve]";
        }else{
            msg = msg + "\n - please enter [SHORT DESCRIPTION]";
        } 
        
    }
    

    if(getId(nameCont,"ddl_localita").selectedIndex<=0){
        currFld = getId(nameCont,"localita_altro").value;
        if(currFld == ""){
            errore = true;
            if(lng=='it'){
              msg = msg + "\n - seleziona una [località] dall'elenco o inseriscine una nuova";
            }else{
               msg = msg + "\n - please enter at least one [LOCATION]";
            } 
            
        }
    }

  var fckId=nameCont + "_villa_descrizione";
  var oEditor = FCKeditorAPI.GetInstance( fckId ) ; // Get the editor contents as XHTML. 
  if(oEditor.GetXHTML( true )==''){
    errore = true;
        if(lng=='it'){
           msg = msg + "\n - inserisci la [descrizione completa]";
        }else{
            msg = msg + "\n - please enter [FULL DESCRIPTION]";
        } 

    } 

    currFld = getId(nameCont,"villa_superficie_mq").value;
    if(currFld == "" || isNaN(currFld)){
        errore = true;
        if(lng=='it'){
            msg = msg + "\n - inserisci un valore di [superficie] valido";
        }else{
            msg = msg + "\n - please enter a valid [SQM] value";
        } 

    }else if(currFld < 0){
        errore = true;
        if(lng=='it'){
            msg = msg + "\n - inserisci un valore di [superficie] valido";
        }else{
            msg = msg + "\n - please enter a valid [SQM] value";
        } 
    }


    if (tipocontratto==20){ //vendita
        currFld = getId(nameCont,"villa_prezzo").value;
        if(currFld.indexOf(".")>=0){
            errore = true;
            if(lng=='it'){
                msg = msg + "\n - usa la virgola [,] come separatore decimale per il [prezzo di vendita]";
            }else{
                msg = msg + "\n - please use comma [,] as decimal separator for [PRICE]";
            } 

        }else{
            currFld = currFld.replace(",","."); 
            if((currFld == "" || isNaN(currFld))){
                errore = true;
                if(lng=='it'){
                    msg = msg + "\n - inserisci un [prezzo di vendita] valido";
                }else{
                    msg = msg + "\n - please enter a valid [PRICE] value";
                } 
                
             }else if(currFld <= 0){
               errore = true;
               if(lng=='it'){
                    msg = msg + "\n - inserisci un [prezzo di vendita] valido";
               }else{
                    msg = msg + "\n - please enter a valid [PRICE] value";
               } 
            }
        }

    }else{//affitto
        fckId=nameCont + "_villa_prezzoRent";
        oEditor = FCKeditorAPI.GetInstance( fckId ) ; // Get the editor contents as XHTML. 
        //getId(nameCont,"villa_prezzo").value=0;
        if(oEditor.GetXHTML( true )==''){
            errore = true;
            if(lng=='it'){
                msg = msg + "\n - inserisci la tabella con il [prezzo di affitto]";
            }else{
                msg = msg + "\n - please enter [RENT RATES TABLE]";
            } 
        } 
    }
    
    
          
    if(errore){
        alert(msg);
        return false;
    }else{
        return true;
    }         
}


function verificaFormTipologieImmaginiVille(theForm)
{
    var errore = false;
    var msg = "Sono presenti degli errori nel modulo: ";
    
    currFld = getControl(theForm,"tipologiaimmagine_tipo").value;
    if(currFld == ""){
        errore = true;
        msg = msg + "\n - il campo TIPO IMMAGINE è vuoto";
        
    }else if(currFld.length>200){
        errore = true;
        msg = msg + "\n - il campo TIPO IMMAGINE può contenere max 200 caratteri ";
    }
    
    if(errore){
        alert(msg);
        return false;
    }else{
        return true;
    }         
}


function verificaFormImmaginiVille(theForm,lingua)
{
    var errore = false;
    var msg = '';    
    if(lingua=='it'){
        msg = "Sono stati riscontrati degli errori: ";
    }else{
        msg = "Following error(s) occurred: ";
    } 
    
    currFld= document.getElementById ("ctl00_ContentPlaceHolder1_manageEstates_DetailsView1_immagineProprietario_DetailsView1_ddl_tipoimg").selectedIndex;
    //var tipoimmagine=currFld;
    //msg = msg + "\n - Tipo Img " + tipoimmagine;
   
    if(currFld <= 0 ){
        errore = true;
        if (lingua=='it'){
                msg = msg + "\n - E' necessario selezionare un TIPO IMMAGINE";
        }else{
                msg = msg + "\n - Please choose a [TYPE]";
        }
    }
    
      
    currFld = document.getElementById ("ctl00_ContentPlaceHolder1_manageEstates_DetailsView1_immagineProprietario_DetailsView1_immaginevilla_descr").value;
    if(currFld == ""){
        errore = true;
        if (lingua=='it'){
            msg = msg + "\n - il campo DESCRIZIONE è vuoto";
        }else{
            msg = msg + "\n - photo [DESCRIPTION] is missing";
        }


        
    }else if(currFld.length>200){
        errore = true;
        if (lingua=='it'){
            msg = msg + "\n - il campo DESCRIZIONE può contenere max 200 caratteri ";
        }else{
            msg = msg + "\n - photo [DESCRIPTION] is too big, max 200 characters allowed";
            msg = msg + "\n - il campo DESCRIZIONE può contenere max 200 caratteri ";
        }

    }


    
    currFld1 = document.getElementById ("ctl00_ContentPlaceHolder1_manageEstates_DetailsView1_immagineProprietario_DetailsView1_ddl_immaginevilla_nome").value;
    if((currFld1 == "") || (currFld1 == undefined)){
        
        currFld = document.getElementById ("ctl00_ContentPlaceHolder1_manageEstates_DetailsView1_immagineProprietario_DetailsView1_FU_img").value;
        if(currFld == ""){
             errore = true;
             if (lingua=='it'){
                msg = msg + "\n - è necessario selezionare un'immagine";
            }else{
                msg = msg + "\n - image file is missing";
            }
         }
               
    }
            
    if(errore){
        alert(msg);
        return false;
    }else{
        document.getElementById ("loading").style.display = "block";
        return true;
    }         
}



function verificaFormImmaginiVilleGV(theForm)
{
    var errore = false;
    var msg = "Sono presenti degli errori nel modulo: ";
     
       
    currFld = getControl(theForm,"immaginevilla_descr").value;
    if(currFld == ""){
        errore = true;
        msg = msg + "\n - il campo DESCRIZIONE è vuoto";
        
    }else if(currFld.length>200){
        errore = true;
        msg = msg + "\n - il campo DESCRIZIONE può contenere max 200 caratteri ";
    }
    
    currFld = getControl(theForm,"immaginevilla_priorita").value;
    if(currFld == ""){
        errore = true;
        msg = msg + "\n - il campo PRIORITA' è vuoto";
    }else if( isNaN(currFld) ){
        errore = true;
        msg = msg + "\n - il campo PRIORITA' non è numerico o non è un numero intero ";
    }else if(currFld.indexOf(".")>=0){
        errore = true;
        msg = msg + "\n - il campo PRIORITA deve contenere un numero intero";
    }else if(currFld < 0){
        errore = true;
        msg = msg + "\n - il campo PRIORITA' è negativo ";
    }
    
                 
    if(errore){
        alert(msg);
        return false;
    }else{
        return true;
    }         
}


//restituisce un controllo del form in base all'id definita nel codice html della pagina
function getControl(theForm,theCtrName){
    var theCtr;
    var theCtrN = "$" + theCtrName;
    for(i=0;i<theForm.elements.length;i++){
        theCtr = theForm.elements[i];
        if(theCtr.name.indexOf(theCtrN)>=0)
        {
            return theCtr;
        }
    }
    return false;
}

/*

function verificaFormImmaginiVille(theForm)
{
    var errore = false;
    var msg = "Sono presenti degli errori nel modulo: ";
    
    currFld= getControl(theForm,"ddl_tipoimg").value;
    //var tipoimmagine=currFld;
    //msg = msg + "\n - Tipo Img " + tipoimmagine;
   
    if(currFld == 0 ){
        errore = true;
        msg = msg + "\n - E' necessario selezionare un TIPO IMMAGINE";
    }
    
       
      
    currFld = getControl(theForm,"immaginevilla_descr").value;
    if(currFld == ""){
        errore = true;
        msg = msg + "\n - il campo DESCRIZIONE è vuoto";
        
    }else if(currFld.length>200){
        errore = true;
        msg = msg + "\n - il campo DESCRIZIONE può contenere max 200 caratteri ";
    }
    
    currFld1 = getControl(theForm,"ddl_immaginevilla_nome").value;
    if((currFld1 == "") || (currFld1 == undefined)){
        
        currFld = getControl(theForm,"FU_img").value;
        if(currFld == ""){
             errore = true;
             msg = msg + "\n - è necessario selezionare un'immagine";
         }
               
    }
            
    if(errore){
        alert(msg);
        return false;
    }else{
        return true;
    }         
}*/