String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function addmessage(str,item,del)
{
    if(str == '')
        return item;
    return str + del + item;
}

function checkdate(y,m,d)
{
    m = m-1;
    dt = new Date(y,m,d);
    return(y == dt.getFullYear() && m == dt.getMonth() && d == dt.getDate());
}

function checkemail(mail)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(mail);
}

function checkprice(n)
{
	var filter = /^([1-9])(\d{0,2}(\.\d{3})*|\d+)$/;
	return filter.test(n);
}

function checkphone(phone)
{
	var filter  = /^([0-9])+$/;
	return filter.test(phone);
}

function checkcf(cf){

    var filter  = /^[a-zA-Z]{6}[0-9]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9]{2}([a-zA-Z]{1}[0-9]{3})[a-zA-Z]{1}$/
    return filter.test(cf);
}

function checkformdata(theform, check_occupation)
{
    var tmp,message,email,importo;

    message = "";
    tmp = "";

    if(theform.elements['d_name'].value.trim() == '')
        tmp = "nome";
    if(theform.elements['d_surname'].value.trim() == '')
        tmp = addmessage(tmp,"cognome",", ");
    if(theform.elements['d_birthprovince'].value == '')
        tmp = addmessage(tmp,"residenza",", ");

	if(typeof check_occupation  != 'undefined')
	{
	    if(theform.elements['d_occupation'].value == '')
	        tmp = addmessage(tmp,"professione",", ");
	}

    var prefixph = theform.elements['d_prefixphone1'].value.trim();
    if(prefixph == '')
        tmp = addmessage(tmp,"prefisso",", ");
    var ph = theform.elements['d_phone1'].value.trim();
    if(ph == '')
        tmp = addmessage(tmp,"telefono",", ");
    if(theform.elements['d_cf']){
        var cf = theform.elements['d_cf'].value.trim();
        if(cf == '')
            tmp = addmessage(tmp,"codice fiscale",", ");
    }
    if(theform.elements['d_contacttime'].value == '')
        tmp = addmessage(tmp,"ore di contatto",", ");
    var email = theform.elements['d_email'].value.trim();
    if(email == '')
        tmp = addmessage(tmp,"e-mail",", ");
	var importo = theform.elements['d_importo'];
	if(importo && importo.value == '')
        tmp = addmessage(tmp,"importo",", ");
    if(tmp)
        message = "Per cortesia indicare "+tmp+".";
    if(! checkdate(theform.elements['d_birthyear'].value,theform.elements['d_birthmonth'].value,theform.elements['d_birthday'].value))
        message = addmessage(message,"La data non è valida.","\n");
    if(ph != '' && ! checkphone(ph))
        message = addmessage(message,"Il numero di telefono non è valido.","\n");
    if(theform.elements['d_cf']){
        if(cf != '' && ! checkcf(cf))
            message = addmessage(message,"Attenzione, è necessario completare il campo codice fiscale in modo corretto.","\n");
    }
    if(email != '' && ! checkemail(email))
        message = addmessage(message,"L'indirizzo e-mail non è valido.","\n");
	if(importo && importo.value != '' && ! checkprice(importo.value))
        message = addmessage(message,"L'importo non è valido.","\n");

    // check d_accept1, d_accept2,  d_accept3, d_accept4
    if(theform.elements['d_accept1']){
      if(! theform.elements['d_accept1'].checked)
          message = addmessage(message,"Spiacente. Plusvalore può concedere credito solo a persone maggiorenni.","\n");
      if(! theform.elements['d_accept2'].checked)
          message = addmessage(message,"Spiacente. Plusvalore può concedere credito solo a persone intestatarie di un Conto Corrente.","\n");
      if(! theform.elements['d_accept3'].checked)
          message = addmessage(message,"Spiacente. Plusvalore può concedere prestito solo a persone con reddito dimostrabile.","\n");
      if(! theform.elements['d_accept4'].checked)
          message = addmessage(message,"Spiacente. Plusvalore non può concedere prestito a persone protestate o che hanno subito fallimenti.","\n");
    }

    if(! theform.elements['d_accept'].checked)
        message = addmessage(message,"Per proseguire è necessario indicare il consenso al trattamento dati.","\n");        

    if(message)
    {
        alert(message);
        return false;
    }
    return true;
}
