// JavaScript Document

function show_more(sDivIndex) {
		//alert(sDivIndex)
	if (document.getElementById(sDivIndex).style.display == "none") {
		document.getElementById(sDivIndex).style.display = "block";
	} else {
		document.getElementById(sDivIndex).style.display = "none";
	}
}

function switchImage (image, events) {
		//alert(sDivIndex)
	if (events == "over" ) {
		document.getElementById(image).src = image+"_on.gif";
	} else if (events == "out") {
		document.getElementById(image).src = image+"_off.gif";
	}
}

function send_form() {
	if (document.getElementById("loading").style.display == "none") {
		//simple_form_init();
		document.getElementById("loading").style.display = "block";
		document.getElementById("login").style.display = "none";
	} else {
		document.getElementById("loading").style.display = "none";
		document.getElementById("login").style.display = "block";
	}
	return false;
}
//FORM DI LOGIN - INIZIO
$(document).ready(simple_form_init);

function simple_form_init () {
	//$('#msgbox').toggle();
	$('#form_login').submit(
							 function () {
								 //alert("stai inviando " + $("#form_login :input").serialize());
								 $.ajax({
										type: 'POST',
										url: '../XMLauth.php',
										data: $("#form_login :input").serialize(),
										dataType: 'xml',
										success: onSuccess
						              });
						             return false;
							 }
				);
	}
	
	function onSuccess(xmlObj){
		//	$('#risposta').html(msg.getElementsByTagName('messaggio')[0].firstChild.nodeValue);
		//	$('#risposta').html(msg.getElementsByTagName('messaggio')[0].childNodes.lenght);
		//users = xmlObj.getElementsByTagName('users')[0];
		//outHtml = users.getElementsByTagName('utente');
		//$('#risposta').html(outHtml);
		var outHtml = "";
		var lista = xmlObj.getElementsByTagName('property');
			for (var i = 0; i < lista.length; i++) {
					//alert(lista.length);
					property = lista[i];
					user = property.getElementsByTagName('user')[0].firstChild.nodeValue;
					gruppo = property.getElementsByTagName('gruppo')[0].firstChild.nodeValue;
					document.getElementById("loading").style.display = "none";
					document.getElementById("inner_message").style.display = "block";
						if (user == "nofound") {
								document.getElementById("login").style.display = "block";
								$('#inner_message').html("<img src='../images/bad.gif' alt='Denied'>&nbsp;Dati non corretti");
							} else {
								//window.open('management/index.php');
								
								$('#inner_message').html("<div><img src='../images/good.gif' alt='Success'>&nbsp;Login effettuato. <a href='/logout.php'>Esci</a></div><div><a href='/management/'>Entra nell\'area riservata</a></div>");
								$('#header_message').html("<a href='/management/'>Clicca qui per accedere ai servizi online</a>");
							}
				}
		}
//FORM DI LOGIN - FINE

function check_email(target_value) {
	if (target_value.length == 0) return true;
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < target_value.length; i++){
		if(ok.indexOf(target_value.charAt(i))<0) { 
			return (false);
		}	
	} 
	re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
	re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!target_value.match(re) && target_value.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 checkDate (target_value) {
  var checkOK = "0123456789";
  var allValid = true;
  for (i = 0;  i < target_value.length;  i++) {
    ch = target_value.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
		if (j == checkOK.length) {
		  allValid = false;
		  break;
		}
  	}
}

function checkSublipointForm () {
	print_errors = '';
	if (document.getElementById('nome').value == '') {
		print_errors += '- Il campo NOME e\' abbligatorio\n';
	}
	if (document.getElementById('cognome').value == '') {
		print_errors += '- Il campo COGNOME e\' abbligatorio\n';
	}
	if (document.getElementById('localita').value == '') {
		print_errors += '- Il campo LOCALITA\' e\' abbligatorio\n';
	}
	if (document.getElementById('cellulare').value == '') {
		print_errors += '- Il campo CELLULARE e\' abbligatorio\n';
	}
	if (document.getElementById('email').value == '') {
		print_errors += '- Il campo EMAIL e\' abbligatorio\n';
	}
	if (!check_email(document.getElementById('email').value)) {
		print_errors += '- Inserire una email valida\n';
	}
	if (document.getElementById('telefono').value == '') {
		print_errors += '- Il campo TELEFONO e\' abbligatorio\n';
	}
	if (document.getElementById('privacy').checked == false) {
		print_errors += '- E\' necessario accettare i termini privacy \n';
	}
	
	if (print_errors != '') {
		alert(print_errors);
		return false;
	} else {
		return true;
	}
}

function checkAnagrForm () {
	print_errors = '';
	if (document.getElementById('nome').value == '') {
		print_errors += '- Il campo NOME è abbligatorio\n';
	}
	if (document.getElementById('cognome').value == '') {
		print_errors += '- Il campo COGNOME è abbligatorio\n';
	}
	if (document.getElementById('indirizzo').value == '') {
		print_errors += '- Il campo INDIRIZZO è abbligatorio\n';
	}
	if (document.getElementById('citta').value == '') {
		print_errors += '- Il campo CITTA è abbligatorio\n';
	}
	if (document.getElementById('cap').value == '') {
		print_errors += '- Il campo CAP è abbligatorio\n';
	}
	if (document.getElementById('telefono').value == '') {
		print_errors += '- Il campo TELEFONO è abbligatorio\n';
	}
	if (!check_cf(document.getElementById('codice_fiscale').value)) {
		print_errors += '- Inserisci un codice fiscale valido\n';
	}
	if (document.getElementById('data_nascita').value == 'gg/mm/aaaa' || document.getElementById('data_nascita').value.length < 10) {
		print_errors += '- Inserisci una data di nascita valida\n';
	}
	if (document.getElementById('luogo').value == '') {
		print_errors += '- Il campo LUOGO è abbligatorio\n';
	}
	
	if (print_errors != '') {
		alert(print_errors);
		return false;
	} else {
		return true;
	}
}