var ajaxObjects = new Array();

function risultato(index,dove)
{
	document.getElementById(dove).innerHTML = "";
	document.getElementById(dove).innerHTML = ajaxObjects[index].response;
}

function inviaForm(tiposcheda,idarea)
{
	var theForm = document.myform
	var campi = "";
	for(i=0; i<theForm.elements.length; i++)
		
	{
	
	//recupero i valori del form
	if (theForm.elements[i].type!="undefined"&&theForm.elements[i].value!="-")
	{	
		  if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "hidden"){
			  campi +=  theForm.elements[i].name +"|@"+theForm.elements[i].value + "||";
			  }
			  else if(theForm.elements[i].type == "checkbox"){
			  campi +=  theForm.elements[i].name +"|@"+theForm.elements[i].checked + "||";
			  }
			  else if(theForm.elements[i].type == "select-one"){
			  campi +=  theForm.elements[i].name +"|@"+theForm.elements[i].options[theForm.elements[i].selectedIndex].value + "||";
			  }	
			  else if (theForm.elements[i].type == "radio")
			  {
  				campi +=  theForm.elements[i].name +"|@"+theForm.elements[i].checked + "||"	;			  
			  }
		}
   }
	
    //validazione lato server con AJAX
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	var url = 'validazione.asp?tiposcheda='+tiposcheda+'&idarea='+idarea+'&campi='+campi;
	//document.getElementById("errori").innerHTML = url;
	ajaxObjects[ajaxIndex].requestFile = url;
	ajaxObjects[ajaxIndex].onCompletion = function() { risultatoInviaForm(ajaxIndex); } ;
	ajaxObjects[ajaxIndex].onError = function() { risultatoInviaForm(ajaxIndex); }
	ajaxObjects[ajaxIndex].runAJAX();

}

function risultatoInviaForm(index)
{
	var errore = ajaxObjects[index].response;

	if (errore!="")
	{
		document.getElementById("errori").innerHTML = "";
		document.getElementById("errori").innerHTML = '<div id="msg">' + ajaxObjects[index].response + '</div>';
	}
	if (errore=="")
	{
		document.myform.submit();
	}
}

function risultato_aggiungi(index,dove)
{

	var div_dove = document.getElementById(dove);

	//div risultato
	var r_divID = 'r' + dove;

	//cancello se c'è div per risultato
	if (document.getElementById(r_divID))
	{
		//lo cancello
		document.getElementById(dove).removeChild(document.getElementById(r_divID));
	}
	//lo ricreo
	var r_div = document.createElement('span');
	r_div.setAttribute('id',r_divID);
	//r_div.setAttribute('class','campi');
	//r_div.className = 'campi';

	div_dove.appendChild(r_div);

	document.getElementById(r_divID).innerHTML = ajaxObjects[index].response;

}

function apri_pop(pagina,x,y)
{
	//apro la popup
	UploadWinFile = window.open(pagina, "NuovaFinestra", "scrollbars=yes,status=no,menubar=no,toolbar=no,location=no,resizable=Yes,width="+x+",height="+y+""); 
	UploadWinFile.focus();

}

function edita(id,idesp,cosa) {

    //alert(document.getElementById(id).innerHTML);
    var valore = document.getElementById(id).innerHTML

    document.getElementById('td_' + id).innerHTML = "";

    var nuovo = document.createElement("input");
    nuovo.setAttribute('type', 'text');
    nuovo.setAttribute('value', valore);
    nuovo.setAttribute('id', id);
    nuovo.setAttribute('onblur', 'edita_conferma(\'' + id + '\',\'' + cosa + '\',\'' + idesp + '\');');

    var div = document.getElementById('td_' + id);
    div.appendChild(nuovo);
    
    document.getElementById(id).focus();


}

function edita_conferma(id,campo,idesp) {

    var valore = document.getElementById(id).value;

    var riga = 'td_' + id;

    document.getElementById(riga).innerHTML = ""

    var str_campo = id.indexOf('_');

    var nome_campo = id.substr(str_campo);
    nome_campo = nome_campo.replace('_', '');
    
    var nuovo = document.createElement('a');
    nuovo.setAttribute('href', '#');
    nuovo.setAttribute('onclick', 'edita(\'' + id + '\');');
    nuovo.setAttribute('id', id);

    var txt = document.createTextNode(valore);
    nuovo.appendChild(txt)

    var div = document.getElementById(riga);
    div.appendChild(nuovo);

    //validazione lato server con AJAX
    var ajaxIndex = ajaxObjects.length;
    ajaxObjects[ajaxIndex] = new sack();
    var url = 'dati_professionale_2.asp?id=' + idesp + '&' + nome_campo + '=' + valore;
    ajaxObjects[ajaxIndex].requestFile = url;
    /*ajaxObjects*[ajaxIndex].onCompletion = function() { risultato(ajaxIndex); };
    ajaxObjects[ajaxIndex].onError = function() { risultatoInviaForm(ajaxIndex); }*/
    ajaxObjects[ajaxIndex].runAJAX();

}

function elimina_azienda(id, idstato) {
    var conferma = confirm('Vuoi eliminare lo stato?');

    if (conferma) {
        //document.location.href = "elimina_stato.asp?idoggetto=" + id + "&idstato=" + idstato;
    }

}

function vedi() {
    document.getElementById('nuovo').style.display = 'block';
    document.getElementById('c_azienda').style.display = 'block';
    document.getElementById('a_azienda').style.display = 'none';
}

function nascondi() {
    
    document.getElementById('nuovo').style.display = 'none';
    document.getElementById('a_azienda').style.display = 'block';
    document.getElementById('c_azienda').style.display = 'none';
}

function controllologin(str) {

    if (str.length > 0) {

        //validazione lato server con AJAX
        var ajaxIndex = ajaxObjects.length;
        ajaxObjects[ajaxIndex] = new sack();
        var url = 'controllalogin.asp?str=' + str;
        ajaxObjects[ajaxIndex].requestFile = url;
        ajaxObjects[ajaxIndex].onCompletion = function() { risultatoC(ajaxIndex, 'errori'); };
        ajaxObjects[ajaxIndex].onError = function() { risultatoC(ajaxIndex, 'errori'); };
        ajaxObjects[ajaxIndex].runAJAX();
    }

}

function controllomail(str) {

    if (str.length > 0) {

        //validazione lato server con AJAX
        var ajaxIndex = ajaxObjects.length;
        ajaxObjects[ajaxIndex] = new sack();
        var url = 'controllamail.asp?str=' + str;
        ajaxObjects[ajaxIndex].requestFile = url;
        ajaxObjects[ajaxIndex].onCompletion = function() { risultatoC(ajaxIndex, 'errori'); };
        ajaxObjects[ajaxIndex].onError = function() { risultatoC(ajaxIndex, 'errori'); };
        ajaxObjects[ajaxIndex].runAJAX();

    }
}

function risultatoC(index, dove) {

    var esito = ajaxObjects[index].response;

    if (esito.length > 0) {
        document.myform.btn_invia.disabled = true;
        document.getElementById(dove).innerHTML = "";
        document.getElementById(dove).innerHTML = ajaxObjects[index].response;
    }
    else {
        document.myform.btn_invia.disabled = false;
        document.getElementById(dove).innerHTML = "";
    }
    
}

function verifyDateFormat(DateString, DateFormat) {
    var match;
    var formato = "gg/mm/aaaa";
    var tmpDate;
    var validFormat = false

    try {
        match = DateString.match(/^(\d?\d)\D(\d?\d)\D(\d{4}|\d{2})$/)

        controlla = DateString.replace(/\//gi, '');

        if (controlla.length != 8) {
            //alert(controlla)
            validFormat = false;
        }
        if (match != null) {
            if (DateFormat == "en") {
                formato = "aaaa/mm/gg";
                tmpDate = new Date(match[3], match[1] - 1, match[2])
                validFormat = ((tmpDate.getMonth() == match[1] - 1) && (tmpDate.getDate() == match[2]))
            } else if (DateFormat == "it") {
                formato = "gg/mm/aaaa";
                tmpDate = new Date(match[3], match[2] - 1, match[1])
                validFormat = ((tmpDate.getMonth() == match[2] - 1) && (tmpDate.getDate() == match[1]))
            }
        }
    }
    catch (e) {
        alert(e.message)
    }
    finally {
        if (validFormat == false && DateString.length != 0) {
            //visualizzo errore
            alert('Inserire la data nel formato ' + formato)

            if (document.getElementById("submitForm")) {
                //disattivo il submit
                document.getElementById("submitForm").disabled = true;

                if (document.getElementById("errori")) {
                    document.getElementById("errori").value = 1;
                }
            }
        }
        else {
            if (document.getElementById("submitForm")) {
                document.getElementById("submitForm").disabled = false;

                if (document.getElementById("errori")) {
                    document.getElementById("errori").value = 0;
                }

            }
        }
    }
}

function getindirizzi(valore) {

    if (valore.length > 0) {

        //validazione lato server con AJAX
        var ajaxIndex = ajaxObjects.length;
        ajaxObjects[ajaxIndex] = new sack();
        var url = 'getindirizzi.asp?v=' + valore;
        ajaxObjects[ajaxIndex].requestFile = url;
        ajaxObjects[ajaxIndex].onCompletion = function() { risultato_aggiungi(ajaxIndex, 'd_universita'); };
        ajaxObjects[ajaxIndex].onError = function() { risultato_aggiungi(ajaxIndex, 'd_universita'); };
        ajaxObjects[ajaxIndex].runAJAX();
        document.getElementById('d_universita').innerHTML += '<br/>';
    }

}

