
function apriTemplate() {
	apriPopUp('/boatonsale/template.php',true,500,500);
}
function apriBarcheOperatore(oper,tipo) {
    apriPopUp('/boatonsale/operatore.php/operatore_'+oper+'/'+tipo,true);
}

function apriAltro(sez,elemento) {
	if(document.getElementById(elemento).selectedIndex<2) {
		apriPopUp('/boatonsale/altro.php?elemento='+elemento+'&sez='+sez,false,200,200);
	}
}
function inserisciAltro(sez) {
	if(document.getElementById("altro").value.length>0) {
		dati = document.createElement('option');
		dati.text=document.getElementById("altro").value;
		dati.value=document.getElementById("altro").value;
		albero = self.opener.document.getElementById(sez);
		albero.add(dati,null);
		albero.selectedIndex=albero.length-1;
	}
	self.close();
}

function visualizzaElementiForm() {
    if(getElement('oggetto').selectedIndex==0) {
        showElement('frm_venditore');
    }
    else {
        hideElement('frm_venditore');
    }
    selectModello();
    selectCostruttore();
}
function selectModello() {
    i = loadJSONDoc("/lib/getModelli.php/categoria_"+getElement('oggetto').options[getElement('oggetto').selectedIndex].value);
    i.addCallback(riempiSelectModello);
}
function riempiSelectModello(dati) {
    sel = SELECT({"name":"modello","id":"modello","style":"width:300px","class":"forminput"},map(rigaSelect,dati));
    replaceChildNodes("cont_modello", sel);
}

function selectCostruttore() {
    i = loadJSONDoc("/lib/getCostruttori.php/categoria_"+getElement('oggetto').options[getElement('oggetto').selectedIndex].value);
    i.addCallback(riempiSelectCostruttore);
}
function riempiSelectCostruttore(dati) {
    sel = SELECT({"name":"costruttore","id":"costruttore","style":"width:300px","class":"forminput"},map(rigaSelect,dati));
    replaceChildNodes("cont_costruttore", sel);
}

function selezionaBarca(barca,finestra,aziende) {
    i = loadJSONDoc("/lib/getBarca.php?"+queryString(['barca','aziende'],[barca,aziende]));
    i.addCallback(riempiBarca,finestra);
}


function riempiBarca(finestra,dati) {
    //log(dati);
    finestra.document.getElementById("modello").value=dati.modello;
    finestra.document.getElementById("aziendeBarche_id").value=dati.ID;
    //finestra.document.getElementById("barca_extra").style.display = 'none';
    if(dati.lunghezza && finestra.document.getElementById("lunghezza_ft"))
        finestra.document.getElementById("lunghezza_ft").value=dati.lunghezza;
    //log(dati.omologati);
    //log(finestra.document.getElementById("omologati"));
    //if(dati.omologati && finestra.document.getElementById("omologati"))
    //    finestra.document.getElementById("omologati").value=dati.omologati;
    //if((!dati.omologati || dati.omologati==0) && dati.postiletto && finestra.document.getElementById("omologati"))
    if(dati.postiletto && finestra.document.getElementById("omologati"))
        finestra.document.getElementById("omologati").value=dati.postiletto;
    if(dati.numcabine && finestra.document.getElementById("numcabine"))
        finestra.document.getElementById("numcabine").value=dati.numcabine;
    //self.close();
}

function selezionaTemplate(barca,finestra) {
    i = loadJSONDoc("/lib/getTemplate.php?"+queryString(['barca'],[barca]));
    i.addCallback(riempiTemplate,finestra);
}

function riempiTemplate(finestra,dati) {
	finestra.document.getElementById("modello").value=dati.modello;
	if(dati.lunghezza && finestra.document.getElementById("lunghezza_ft"))
	    finestra.document.getElementById("lunghezza_ft").value=dati.lunghezza;
	if(dati.tipo_id && finestra.document.getElementById("tipo_id"))
	    finestra.document.getElementById("tipo_id").selectedIndex=dati.tipo_id;
	if(dati.costruttore && finestra.document.getElementById("costruttore"))
		finestra.document.getElementById("costruttore").value=dati.costruttore;
	if(dati.progettista && finestra.document.getElementById("progettista"))
		finestra.document.getElementById("progettista").value=dati.progettista;
	if(dati.lunghezza_gall && finestra.document.getElementById("lunghezza_gall"))
		finestra.document.getElementById("lunghezza_gall").value=dati.lunghezza_gall;
	if(dati.larghezza && finestra.document.getElementById("larghezza"))
		finestra.document.getElementById("larghezza").value=dati.larghezza;
	if(dati.pescaggio && finestra.document.getElementById("pescaggio"))
		finestra.document.getElementById("pescaggio").value=dati.pescaggio;
	if(dati.dislocamento && finestra.document.getElementById("dislocamento"))
		finestra.document.getElementById("dislocamento").value=dati.dislocamento;
	if(dati.omologati && finestra.document.getElementById("omologati"))
		finestra.document.getElementById("omologati").value=dati.omologati;
    if(dati.numcabine && finestra.document.getElementById("numcabine"))
        finestra.document.getElementById("numcabine").value=dati.numcabine;
    if(dati.numcabine && finestra.document.getElementById("postiletto"))
        finestra.document.getElementById("postiletto").value=dati.postiletto;
    if(dati.numbagni && finestra.document.getElementById("numbagni"))
        finestra.document.getElementById("numbagni").value=dati.numbagni;
	self.close();
}


function stampaBarca(id,tipo) {
    if(tipo=='') {
        apriPopUp('/boatonsale/stampa.php/barca_'+id,true,750,500);
    }
    else if(tipo=='template') {
        apriPopUp('/boatonsale/stampa.php/template_1/tipo_'+tipo+'/barca_'+id,true,750,500);
    }
    else {
        apriPopUp('/boatonsale/stampa.php/aziende_1/tipo_'+tipo+'/barca_'+id,true,750,500);
    }
}

function barcheModPeriodi(id) {
	apriPopUp('/boatonsale/modifica.php/aziende_1/modnoleggi_1/barca_'+id,true,750,500);
}

function controllaInsBarca() {
	errori = 0
	messaggio = "";
	if(document.getElementById("stato") && document.getElementById("stato").value=="0") {
		if(document.getElementById("email").value.length==0) {
			errori +=1;
			messaggio += "Non è stata inserita l'email\n";
		}
		else if(!verificaEmail(document.getElementById("email").value)) {
			errori +=1;
			messaggio += "L'indirizzo email non è valido\n";
		}
		/*
		if(document.getElementById("password").value.length==0) {
			errori +=1;
			messaggio += "Non è stata inserita la password\n";
		}*/
	}
	if(document.getElementById("captcha") && document.getElementById("captcha").value.length==0) {
		errori +=1;
		messaggio += "Non è stato inserito il codice di validazione\n";
	}
	if(document.getElementById("telefono") && document.getElementById("telefono").value.length==0) {
		errori +=1;
		messaggio += "Non è stato inserito il telefono\n";
	}
	if(document.getElementById("postiletto").className.indexOf("obbligatorio")>-1 && document.getElementById("postiletto").value==0) {
		errori +=1;
		messaggio += "Non è stato inserito il numero di posti letto disponibili\n";
	}
	if(document.getElementById("modello").value.length==0) {
		errori +=1;
		messaggio += "Non è stato inserito il modello\n";
	}
	if(document.getElementById("prezzo") && document.getElementById("prezzo").value.length==0) {
            if(!document.getElementById("nuove") || document.getElementById("nuove").value!=1) {
		errori +=1;
		messaggio += "Non è stato inserito il prezzo\n";
            }
	}
	if(document.getElementById("lunghezza_ft").value.length==0) {
		errori +=1;
		messaggio += "Non è stato inserita la lunghezza ft\n";
	}
	if(document.getElementById("tipo_id").value==0) {
		errori +=1;
		messaggio += "Non è stato inserito il tipo di barca\n";
	}
	if(document.getElementById("barche_luoghi_1") && !verificaElemento('barche_','luoghi')) {
		errori +=1;
		messaggio += "Non è stato indicato dove è visibile la barca\n";
	}
	if(document.getElementById("contratto") && document.getElementById("contratto").selectedIndex==0) {
        errori +=1;
        messaggio += "Non è stato indicato il tipo di contratto\n";
	}
	if(errori>0)
		alert(messaggio);
	else {
        document.getElementById("velanet").style.cursor="wait";
        mostraProgress();
        document.getElementById("barcheForm").submit();
    }
}
function controllaModNoleggioBarca() {
	errori = 0
	messaggio = "";
	if(document.getElementById("contratto") && document.getElementById("contratto").selectedIndex==0) {
        errori +=1;
        messaggio += "Non è stato indicato il tipo di contratto\n";
	}
	if(errori>0)
		alert(messaggio);
	else {
        document.getElementById("velanet").style.cursor="wait";
        mostraProgress();
        document.getElementById("barcheForm").submit();
    }
}


function confermaCancellaBarca() {
	conf = confirm('Sei sicuro di voler cancellare la barca selezionata?');
	if(conf) {
        document.getElementById("velanet").style.cursor="wait";
		document.getElementById("azione").value = 'cancella';
        mostraProgress();
		document.getElementById("barcheForm").submit();
	}
}

function clonaBarca() {
	if(
            (getElement('tipo_nuove') && getElement('tipo_nuove').checked) ||
                (getElement('tipo_usate') && getElement('tipo_usate').checked) ||
                (getElement('tipo_scuole') && getElement('tipo_scuole').checked) ||
                (getElement('tipo_noleggio') && getElement('tipo_noleggio').checked)
               )
            {
            document.getElementById("velanet").style.cursor="wait";
            mostraProgress();
            getElement('barcheClonaForm').submit();
        }
        else {
            alert("E' necessario selezionare il tipo di barca che verrà creato");
        }
}
function clonaBarcaApri() {
	showElement('controlloClona');
}
function clonaBarcaChiudi() {
    hideElement('controlloClona');
    //setTimeout(hideElement, 5000, 'controlloClona');
}

function controllaRanda() {
    if(document.getElementById("randa_randa")) {
        if(document.getElementById("randa_randa").checked || document.getElementById("randa_randaavv").checked  || document.getElementById("randa_randastecc").checked ) {
            document.getElementById("randamq").disabled=false;
        }
        else {
            document.getElementById("randamq").disabled="disabled";
        }
    }
    
}
function controllaGenoa() {
    if(document.getElementById("genoa_genoa")) {
        if(document.getElementById("genoa_genoa").checked || document.getElementById("genoa_genoaavv").checked ) {
            document.getElementById("genoamq").disabled=false;
        }
        else {
            document.getElementById("genoamq").disabled="disabled";
        }
    }
    
}
