/*		Author:				Andrea Fonsati
*		Create Date:	Thu Jun  5 16:38:10 CEST 2008
*		Last Modify:	
*
*		Notes:
*		This module contains the functions for User
*/

function UserCreation(fieldList)
{
	fieldToChek = new Array();
	fieldToChek = fieldList.split(',');
	msg='';
	checkIVA = false;
	doSubmit = true;
	
	for (field in fieldToChek){
		if (msg.length == 0){
			switch(fieldToChek[field])
			{
			case 'addusername':
				addusername = document.getElementById('addusername').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addusername==0) {
					msg = 'Inserire uno username!';
					document.getElementById('addusername').focus();
				}
				break;
			case 'addemail':
				addemail = document.getElementById('addemail').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				addemail2 = document.getElementById('addemail2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addemail2==0) {
					msg = 'Inserire un\'e-mail di conferma valida!';
					document.getElementById('addemail2').focus();
				}
				if (addemail==0) {
					msg = 'Inserire un\'e-mail valida!';
					document.getElementById('addemail').focus();
				}else{
					if (document.getElementById('addemail').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
						msg = 'Inserire un\'e-mail valida!';
						document.getElementById('addemail').focus();
					}else{
						addmail = document.getElementById('addemail').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
						addmail2 = document.getElementById('addemail2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
						if (addmail != addmail2) {
							msg = 'L\'e-mail inserita non coincide con il campo "conferma e-mail"!';
							document.getElementById('addemail').focus();
						}
					}
				}
				break;
			case 'addcognome':
				addcognome = document.getElementById('addcognome').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addcognome==0) {
					msg = 'Inserire il cognome!';
					document.getElementById('addcognome').focus();
				}
				break;
			case 'addnome':
				addnome = document.getElementById('addnome').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addnome==0) {
					msg = 'Inserire il nome!';
					document.getElementById('addnome').focus();
				}
				break;
			case 'addcomune':
				addcomune = document.getElementById('addcomune').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addcomune==0) {
					msg = 'Inserire il comune di residenza!';
					document.getElementById('addcomune').focus();
				}
				break;
			case 'addcap':
				addcap = document.getElementById('addcap').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addcap==0) {
					msg = 'Inserire il cap!';
					document.getElementById('addcap').focus();
				}
				break;
			case 'addpiva':
				checkIVA = true;
				addpiva = document.getElementById('addpiva').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				err='';
				if( addpiva==0 ) {
					msg = "Inserire il codice fiscale/partita iva valido!";
					document.getElementById('addpiva').focus();
				} else if( document.getElementById('addpiva').value.length == 16 ) {
					err = ControllaCF(document.getElementById('addpiva').value);
				} else if( document.getElementById('addpiva').value.length == 11 ) {
					err = ControllaPIVA(document.getElementById('addpiva').value);
				} else {
					err = "Il codice introdotto non è valido:\n" +
						"  - un codice fiscale deve essere lungo 16 caratteri;\n" +
						"  - una partita IVA deve essere lunga 11 caratteri.";
				}
				if( err > '' ) {
					msg = "Il Codice Fiscale o Partita Iva introdotto non è valido:\n\n" + err;
					document.getElementById('addpiva').focus();
				}
				break;
			case 'addphone':
				addphone = document.getElementById('addphone').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addphone==0) {
					msg = 'Inserire il numero di telefono!';
					document.getElementById('addphone').focus();
				}
				break;
			case 'addpasswd':
				addpasswd = document.getElementById('addpasswd').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				addpasswd2 = document.getElementById('addpasswd2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addpasswd2==0) {
					msg = 'Inserire conferma password!';
					document.getElementById('addpasswd2').focus();
				}
				if (addpasswd==0) {
					msg = 'Inserire una password!';
					document.getElementById('addpasswd').focus();
				}else{
					addpasswd = document.getElementById('addpasswd').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
					addpasswd2 = document.getElementById('addpasswd2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
					if (addpasswd != addpasswd2) {
						msg = 'La password inserita non coincide con il campo "conferma password"!';
						document.getElementById('addpasswd').focus();
					}
				}
				break;
/*			case 'privacycheck':
				if (document.getElementById('privacycheck').checked==false) {
					msg = 'È necessario accettare le condizioni d\'uso!';
					document.getElementById('privacycheck').focus();
				}
				break;
*/			default:
				msg='';
			}		
		}
	}

	if (msg.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length != 0){
		alert(msg);
		return;
	}else if (!checkIVA){

		addpiva = document.getElementById('addpiva').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
		err='';
		if( addpiva==0 ) {
			err = "Inserire il codice fiscale/partita iva valido!";
			document.getElementById('addpiva').focus();
		} else if( document.getElementById('addpiva').value.length == 16 ) {
			err = ControllaCF(document.getElementById('addpiva').value);
		} else if( document.getElementById('addpiva').value.length == 11 ) {
			err = ControllaPIVA(document.getElementById('addpiva').value);
		} else {
			err = "Il codice introdotto non è valido:\n" +
				"  - un codice fiscale deve essere lungo 16 caratteri;\n" +
				"  - una partita IVA deve essere lunga 11 caratteri.";
		}
		if( err > '' ) {
			msg = "Il Codice Fiscale o Partita Iva introdotto non è valido:\n\n" + err;
			document.getElementById('addpiva').focus();
			if(confirm(msg + '\n\nTi è comunque permesso registrarti.\nSei sicuro di continuare?')) {
				document.getElementById('main').action = '?page=panel&func=usercreate&seqid=0&act=create&underid=1';
				document.getElementById('main').submit();
				return;
			}else{
				doSubmit = false;
			}
		}
	}
	if (doSubmit){
		document.getElementById('main').action = '?page=panel&func=usercreate&seqid=0&act=create&underid=1';
		document.getElementById('main').submit();
	}
}

function UserAutoCreation(fieldList)
{
	fieldToChek = new Array();
	fieldToChek = fieldList.split(',');
	msg='';
	checkIVA = false;
	doSubmit = true;
	
	for (field in fieldToChek){
		if (msg.length == 0){
			switch(fieldToChek[field])
			{
			case 'addusername':
				addusername = document.getElementById('addusername').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addusername==0) {
					msg = 'Inserire uno username!';
					document.getElementById('addusername').focus();
				}
				break;
			case 'addemail':
				addemail = document.getElementById('addemail').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				addemail2 = document.getElementById('addemail2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addemail2==0) {
					msg = 'Inserire un\'e-mail di conferma valida!';
					document.getElementById('addemail2').focus();
				}
				if (addemail==0) {
					msg = 'Inserire un\'e-mail valida!';
					document.getElementById('addemail').focus();
				}else{
					if (document.getElementById('addemail').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) {
						msg = 'Inserire un\'e-mail valida!';
						document.getElementById('addemail').focus();
					}else{
						addmail = document.getElementById('addemail').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
						addmail2 = document.getElementById('addemail2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
						if (addmail != addmail2) {
							msg = 'L\'e-mail inserita non coincide con il campo "conferma e-mail"!';
							document.getElementById('addemail').focus();
						}
					}
				}
				break;
			case 'addcognome':
				addcognome = document.getElementById('addcognome').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addcognome==0) {
					msg = 'Inserire il cognome!';
					document.getElementById('addcognome').focus();
				}
				break;
			case 'addnome':
				addnome = document.getElementById('addnome').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addnome==0) {
					msg = 'Inserire il nome!';
					document.getElementById('addnome').focus();
				}
				break;
			case 'addcomune':
				addcomune = document.getElementById('addcomune').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addcomune==0) {
					msg = 'Inserire il comune di residenza!';
					document.getElementById('addcomune').focus();
				}
				break;
			case 'addcap':
				addcap = document.getElementById('addcap').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addcap==0) {
					msg = 'Inserire il cap!';
					document.getElementById('addcap').focus();
				}
				break;
			case 'addpiva':
				checkIVA = true;
				addpiva = document.getElementById('addpiva').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				err='';
				if( addpiva==0 ) {
					msg = "Inserire il codice fiscale/partita iva valido!";
					document.getElementById('addpiva').focus();
				} else if( document.getElementById('addpiva').value.length == 16 ) {
					err = ControllaCF(document.getElementById('addpiva').value);
				} else if( document.getElementById('addpiva').value.length == 11 ) {
					err = ControllaPIVA(document.getElementById('addpiva').value);
				} else {
					err = "Il codice introdotto non è valido:\n" +
						"  - un codice fiscale deve essere lungo 16 caratteri;\n" +
						"  - una partita IVA deve essere lunga 11 caratteri.";
				}
				if( err > '' ) {
					msg = "Il Codice Fiscale o Partita Iva introdotto non è valido:\n\n" + err;
					document.getElementById('addpiva').focus();
				}
				break;
			case 'addphone':
				addphone = document.getElementById('addphone').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addphone==0) {
					msg = 'Inserire il numero di telefono!';
					document.getElementById('addphone').focus();
				}
				break;
			case 'addpasswd':
				addpasswd = document.getElementById('addpasswd').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				addpasswd2 = document.getElementById('addpasswd2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
				if (addpasswd2==0) {
					msg = 'Inserire conferma password!';
					document.getElementById('addpasswd2').focus();
				}
				if (addpasswd==0) {
					msg = 'Inserire una password!';
					document.getElementById('addpasswd').focus();
				}else{
					addpasswd = document.getElementById('addpasswd').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
					addpasswd2 = document.getElementById('addpasswd2').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').toUpperCase();
					if (addpasswd != addpasswd2) {
						msg = 'La password inserita non coincide con il campo "conferma password"!';
						document.getElementById('addpasswd').focus();
					}
				}
				break;
			case 'privacycheck':
				if (document.getElementById('privacycheck').checked==false) {
					msg = 'È necessario accettare le condizioni d\'uso!';
					document.getElementById('privacycheck').focus();
				}
				break;
			default:
				msg='';
			}		
		}
	}

	if (msg.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length != 0){
		alert(msg);
		return;
	}else if (!checkIVA){

		addpiva = document.getElementById('addpiva').value.replace(/^\s\s*/, '').replace(/\s\s*$/, '').length;
		err='';
		if( addpiva==0 ) {
			err = "Inserire il codice fiscale/partita iva valido!";
			document.getElementById('addpiva').focus();
		} else if( document.getElementById('addpiva').value.length == 16 ) {
			err = ControllaCF(document.getElementById('addpiva').value);
		} else if( document.getElementById('addpiva').value.length == 11 ) {
			err = ControllaPIVA(document.getElementById('addpiva').value);
		} else {
			err = "Il codice introdotto non è valido:\n" +
				"  - un codice fiscale deve essere lungo 16 caratteri;\n" +
				"  - una partita IVA deve essere lunga 11 caratteri.";
		}
		if( err > '' ) {
			msg = "Il Codice Fiscale o Partita Iva introdotto non è valido:\n\n" + err;
			document.getElementById('addpiva').focus();
			if(confirm(msg + '\n\nTi è comunque permesso registrarti.\nSei sicuro di continuare?')) {
				document.getElementById('adduser').action = '?act=okcaptiveadduser&seqid=1';
				document.getElementById('adduser').submit();
				return;
			}else{
				doSubmit = false;
			}
		}
	}
	if (doSubmit){
		document.getElementById('adduser').action = '?act=okcaptiveadduser&seqid=1';
		document.getElementById('adduser').submit();
	}
}

function ControllaCF(cf)
{
   var validi, i, s, set1, set2, setpari, setdisp;
   if( cf == '' )  return '';
   cf = cf.toUpperCase();
   if( cf.length != 16 )
      return "La lunghezza del codice fiscale non è\n"
      +"corretta: il codice fiscale dovrebbe essere lungo\n"
      +"esattamente 16 caratteri.\n";
   validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
   for( i = 0; i < 16; i++ ){
      if( validi.indexOf( cf.charAt(i) ) == -1 )
         return "Il codice fiscale contiene un carattere non valido `" +
            cf.charAt(i) +
            "'.\nI caratteri validi sono le lettere e le cifre.\n";
   }
   set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   s = 0;
   for( i = 1; i <= 13; i += 2 )
      s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   for( i = 0; i <= 14; i += 2 )
      s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
   if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
      return "Il codice fiscale non è corretto:\n"+
         "il codice di controllo non corrisponde.\n";
   return "";
}

function ControllaPIVA(pi)
{
   if( pi == '' )  return '';
   if( pi.length != 11 )
      return "La lunghezza della partita IVA non è\n" +
         "corretta: la partita IVA dovrebbe essere lunga\n" +
         "esattamente 11 caratteri.\n";
   validi = "0123456789";
   for( i = 0; i < 11; i++ ){
      if( validi.indexOf( pi.charAt(i) ) == -1 )
         return "La partita IVA contiene un carattere non valido `" +
            pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
   }
   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 "La partita IVA non è valida:\n" +
         "il codice di controllo non corrisponde.\n";
   return '';
}

function GoBack()
{
	history.back();
}

function GoToContractTerms()
{
	document.getElementById('adduser').action = '?page=contractterms&seqid=1';
	document.getElementById('adduser').submit();
}

function ContactRequest()
{
	document.getElementById('connect_request').action = '?page=buyuser&seqid=1';
	document.getElementById('connect_request').submit();
}

function bannerManage(func)
{
	url = 'banner';
	if (func != '') { url = url + '_doorman'}
	document.getElementById('main').action = '?page=panel&func=manage&seqid=1&act='+url;
	document.getElementById('main').submit();
}

