// JavaScript Document
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	  if (document.getElementById("privacy").checked == false) {
		  errors+='- E\' necessario autorizzare il trattamento dei dati personali.\n';
	  }
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' deve contenere un indirizzo email valido.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' deve contenere un numero.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- Il campo '+nm.toUpperCase()+' e\' necessario.\n'; }
    }
	if (errors) alert('Si sono verificati i seguenti errori:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

/* funzione di check e calcolo quota corretta per ogni client*/
function checkQuota(clients,quota_totale) {
	tempTotal = 0;
	for (i=0; i<clients; i++) {
		singleQuota = Number(document.getElementById("quota-"+i).value); 
		if (isNaN(singleQuota)) {
			alert("Inserire solo numeri.");
			document.getElementById("quota-"+i).value = "";
			document.getElementById("quota-"+i).focus();
			break;
		}
		tempTotal = tempTotal + singleQuota;
	}
	if (tempTotal > quota_totale) {
		outOfQuota = new Number(tempTotal-quota_totale).toFixed(2);
		document.getElementById("showit").innerHTML = "Hai superato la quota di "+outOfQuota+" Gb";
	} else {
		document.getElementById("showit").innerHTML = "Hai suddiviso "+tempTotal+" su "+quota_totale;
	}
}

/* FUNZIONE ROLLOVER MENU IN HOME PAGE */
function rollTopMenu(sDivIndex,events,active_lang) {
	
		buttons = new Array("steps_1","steps_2","steps_3","steps_4");
		for ( singleDivIndex in buttons ) {
			document.getElementById(buttons[singleDivIndex]).src = "/"+active_lang+"/images/4"+buttons[singleDivIndex]+"_down.jpg";
			//alert("images/4"+buttons[singleDivIndex]+"_down.jpg");
		}
	
	if (events == 'over') {
		//su rollOver
		//alert(sDivIndex);
		document.getElementById(sDivIndex).src = "/"+active_lang+"/images/4"+sDivIndex+"_up.jpg";
		} else {
		//su rollOut
			document.getElementById(sDivIndex).src = "/"+active_lang+"/images/4"+sDivIndex+"_down.jpg";
		}
}
/* FUNZIONE INGRANDIMENTO IMGS IN SS */
function switchOver (thisname) {
		//alert(sezione+"maxi/"+thisname)
		document.getElementById("zoom").style.display = "block";
		document.getElementById("shoMaxiImage").src = "../images/"+thisname+".jpg";
	}
function switchOver_eng (thisname) {
		//alert(sezione+"maxi/"+thisname)
		document.getElementById("zoom").style.display = "block";
		document.getElementById("shoMaxiImage").src = "../images/"+thisname+"_eng.jpg";
	}
function check_email(e) {
	if (e.length == 0) return true;
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length; i++){
		if(ok.indexOf(e.charAt(i))<0) { 
			return (false);
		}	
	} 
	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);		
		}
}
function check_cf (target_value) {
    var validi, i, s, set1, set2, setpari, setdisp;
    if( target_value == '' )  return (false);
    target_value = target_value.toUpperCase();
    if( target_value.length != 16 )
	return (false);
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
	if( validi.indexOf( target_value.charAt(i) ) == -1 )
	    return (false);
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
	s += setpari.indexOf( set2.charAt( set1.indexOf( target_value.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
	s += setdisp.indexOf( set2.charAt( set1.indexOf( target_value.charAt(i) )));
    if( s%26 != target_value.charCodeAt(15)-'A'.charCodeAt(0) )
	return (false);
    return (true);
}

function check_pi(pi) {
    if( pi == '' )  return (false);
    if( pi.length != 11 ) return (false);
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
	if( validi.indexOf( pi.charAt(i) ) == -1 )
	    return (false);
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
	s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
	c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
	if( c > 9 )  c = c - 9;
	s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
	return (false);
    return (true);
}
function check_reg_form() {
		errors = '';
		if (document.getElementById("nome").value == "") {
			errors+='- E\' necessario riempire il campo Nome.\n';
		}
		fiscal_code = document.getElementById("fiscal_code").value;
		partita_iva = document.getElementById("partita_iva").value;
		if (fiscal_code == "" && partita_iva == "" ) { 
			errors+='- E\' necessario riempire o Codice Fiscale o Partita Iva.\n';
		} else if (fiscal_code != "") {
			if (!check_cf(fiscal_code)) {
				errors+='- E\' necessario inserire un Codice Fiscale valido.\n';
			}
		} else if (partita_iva != "") {
			if (!check_pi(partita_iva)) {
				errors+='- E\' necessario inserire una Partita Iva valida.\n';
			}
		}
		if (document.getElementById("mailaddr").value == "") {
			errors+='- E\' necessario riempire il campo Email.\n';
		} else if (document.getElementById("mailaddr").value != "") {
				if (!check_email(document.getElementById("mailaddr").value)) {
					errors+='- Inserire un indirizzo email valido.\n';
				}
		}
		if (document.getElementById("phone1").value == "") {
			errors+='- E\' necessario riempire il campo Telefono.\n';
		}
		if (document.getElementById("address").value == "") {
			errors+='- E\' necessario riempire il campo Indirizzo.\n';
		}
		if (document.getElementById("town").value == "") {
			errors+='- E\' necessario riempire il campo Citta\'.\n';
		}
		if (document.getElementById("cap").value == "") {
			errors+='- E\' necessario riempire il campo CAP.\n';
		}
		if (document.getElementById("province").value == "") {
			errors+='- E\' necessario riempire il campo Provincia.\n';
		}
		if (document.getElementById("contratto").checked == false) {
		    errors+='- E\' necessario accettare il contratto Micso Partners.\n';
	    }
		if (document.getElementById("privacy").checked == false) {
		    errors+='- E\' necessario autorizzare il trattamento dei dati personali.\n';
	    }
		if (errors){
			alert('Si sono verificati i seguenti errori:\n'+errors);
			return false;
		} else {
			return true;
		}
}

