﻿function replaceAll(str, from, to) 
{
    var idx = str.indexOf( from );
    while ( idx > -1 ) 
    {
        str = str.replace( from, to ); 
        idx = str.indexOf( from );
    }
    return str;
}
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("L\'indirizzo email digitato non e\' valido.")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		alert("L\'indirizzo email digitato non e\' valido.")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("L\'indirizzo email digitato non e\' valido.")
		
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		 alert("L\'indirizzo email digitato non e\' valido.")
		 
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		 alert("L\'indirizzo email digitato non e\' valido.")
		 
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		 alert("L\'indirizzo email digitato non e\' valido.")
		 
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		 alert("L\'indirizzo email digitato non e\' valido.")
		 
		return false
	 }

	 return true					
}
function Trim(StrToTrim)
{
    // CONTROLLA CHE IL VALORE IN INPUT SIA DI TIPO STRING
    if (typeof StrToTrim != "string")
    {
        return StrToTrim;
    }

    // CATTURA IL PRIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
    var StrBlank = StrToTrim.substring(0, 1);

    // ELIMINA LO SPAZIO VUOTO DALLA PRIMA POSIZIONE DELLA STRINGA
    while (StrBlank == " ")
    {
        StrToTrim = StrToTrim.substring(1, StrToTrim.length);
        StrBlank = StrToTrim.substring(0, 1);
    }

    // CATTURA L'ULTIMO CARATTERE DELLA STRINGA PER CONTROLLARE CHE NON SIA UNO SPAZIO VUOTO
    StrBlank = StrToTrim.substring(StrToTrim.length - 1, StrToTrim.length);

    // ELIMINA LO SPAZIO VUOTO DALL'ULTIMA POSIZIONE DELLA STRINGA
    while (StrBlank == " ")
    {
        StrToTrim = StrToTrim.substring(0, StrToTrim.length-1);
        StrBlank = StrToTrim.substring(StrToTrim.length-1, StrToTrim.length);
    }

    // ELIMINA POTENZIALI SPAZI VUOTI MULTIPLI ALL'INIZIO ED ALLA FINE DI UNA STRINGA
    while (StrToTrim.indexOf("  ") != -1)
    {
        StrToTrim = StrToTrim.substring(0, StrToTrim.indexOf("  "));
        StrToTrim += StrToTrim.substring(StrToTrim.indexOf("  ") + 1, StrToTrim.length);
    }

    // RESTITUISCE IL VALORE FINALE SENZA SPAZI VUOTI DI CONTORNO
    return StrToTrim;
}
function suggCheckMandatory()
{
	strEmail = Trim(document.getElementById("txtEmail").value);
	strSuggerimento = Trim(document.getElementById("txtSuggerimento").value);
	strCodice = Trim(document.getElementById("txtCode").value);
	if ((strEmail == "") || (strSuggerimento == "") || (strCodice == ""))
	{
		alert("Devi riempire i campi obbligatori e digitare il codice.");
	}
	else
	{
		if (echeck(strEmail))
		{
			document.getElementById("frmSuggerimento").submit()
		}
	}
}
function showGiuridico()
{
    objGiuridico = document.getElementById("tblStatoGiuridico");
    if (objGiuridico.style.display != "block")
    {
        objGiuridico.style.display = "block";
        document.getElementById("giuridicoLink").innerHTML = "nascondi stato giuridico";
    }
    else
    {
        objGiuridico.style.display = "none";
        document.getElementById("giuridicoLink").innerHTML = "mostra stato giuridico";
    }
    objGiuridico = null;
}

function showMap()
{
    objMap = document.getElementById("mappa");
    if (objMap.style.display != "block")
    {
        objMap.style.display = "block";
        document.getElementById("mapLink").innerHTML = "nascondi mappa";
    }
    else
    {
        objMap.style.display = "none";
        document.getElementById("mapLink").innerHTML = "mostra mappa";
    }
    objMap = null;
}

function openPop(){
    strCodice = document.getElementById("controllo").value
    if (Trim(strCodice) == "")
    {
        alert("Partita IVA o Codice Fiscale non inserito");
        document.getElementById("controllo").focus();
    }
    else
    {
	    window.open("../gestionale/login.php","_popfptp","width=800, height=600");
	    document.getElementById("loginForm").submit();
    }
}
