function isValidEmail(a)
{
        var at = a.indexOf("@");
        var name = a.substring(0, at);
        var isp = a.substring(at + 1, a.length);
        var dot = a.lastIndexOf(".");
        if (at == -1 || at == 0 || name == "" || isp == "" || dot == -1 || dot== (a.length - 1))
        {
                return false;
        }
        return true;
}

function clearFields(fields) {
    for (i = 0; i < fields.length; i++) {
        eval("document.form1."+fields[i]+".className=''");
    }
}

function toggleDiv(dd){
	if(document.getElementById(dd).style.visibility=='hidden'){
		document.getElementById(dd).style.visibility='visible';
		document.getElementById(dd).style.display='block';
	}else{
		document.getElementById(dd).style.visibility='hidden';
		document.getElementById(dd).style.display='none';
	}
}

function loading(){
	toggleDiv('loadDiv');
	toggleDiv('formSubmit');
}



function submitForm(){
	loading();
	//alert('ok'); //debug
	//return false; //debug
	document.form1.submit();
}

function warnField(fieldName)
{
	//loading();
	if (fieldName!=null)
	{
		eval("document.form1."+fieldName+".className='form_control_alert'");
		eval("document.form1."+fieldName+".focus();");
	}
    
}


function generalAjaxHandler(field,message,onErrorFunction,onSuccessFunction) {
    if (req.readyState == 4 && req.status == 200) {
        var res = req.responseText;
        if (res.indexOf("OK") != -1) {
            onSuccessFunction();
        } else {
            if (onErrorFunction == null) {
				warnField(field);
				alert(message);
            } else {
                onErrorFunction();
				warnField(field);
				if(message != null) alert(message);
            }
        }
    }
}

function checkLogin() {
    var myfunc = function() {
        generalAjaxHandler("email","Attenzione: esiste gią un account con questa email", null, submitForm);
    }
    req = CreateXmlHttpReq(myfunc);
    req.open("GET", "_include/ajax.php?act=logindbcheck&email="+escape(document.form1.email.value)+"&rand="+escape(Math.random()));
    req.send(null);
}

function controllaForm(){
	var a=document.form1;
	//loading();
 	clearFields(new Array("url", "nome", "via", "n_civico", "cap", "provincia", "citta", "telefono", "email", "password", "password2"), new Array(""));


	if (a.url.value=="") {
		alert("Inserire un valore per \"URL del sito Web\"");
		warnField("url");
		return false;
	}

	if (a.nome.value=="") {
		alert("Inserire un valore per il campo \"Nome e Cognome e/o ragione sociale\".");
		warnField("nome");
		return false;
	}

	if (a.via.value=="") {
		alert("Inserire un valore per il campo \"Via\".");
		warnField("via");
		return false;
	}

	if (a.n_civico.value=="") {
		alert("Inserire un valore per il campo \"Num. civico\".");
		warnField("n_civico");
		return false;
	}

	if (a.cap.value=="") {
		alert("Inserire un valore per il campo \"CAP\".");
		warnField("cap");
		return false;
	}

	if (a.provincia.value=="") {
		alert("Inserire un valore per il campo \"Provincia\".");
		warnField("provincia");
		return false;
	}

	if (a.citta.value=="") {
		alert("Inserire un valore per il campo \"Cittą\".");
		warnField("citta");
		return false;
	}

	if (a.telefono.value=="") {
		alert("Inserire un valore per il campo \"Telefono\".");
		warnField("telefono");
		return false;
	}

	if (a.email.value=="") {
		alert("Inserire un valore per il campo \"Email\".");
		warnField("email");
		return false;
	}
	
	if (!isValidEmail(a.email.value)) {
 		warnField("email");
 		alert("Email non valida!");
 		return false;
 	}

	if (a.password.value=="") {
		alert("Inserire un valore per \"Password\"");
		warnField("password");
		return false;
	}

	if (a.password2.value=="") {
		alert("Inserire un valore per \"Riscrivi la password\"");
		warnField("password2");
		return false;
	}

	if (a.password.value!=a.password2.value) {
		alert("\"Password\" e \"Riscrivi la password\" non coincidono");
		warnField(null);
		return false;
	}
	
	checkLogin();

	return false;
}
