// JavaScript Document

//   ----------- AJAX FUNCTION ------------- //
function getXMLHTTPRequest() {
	var req = false;
	
	try {
			req = new XMLHttpRequest(); /*Firefox e alri*/
		}
	catch(err1){
		
		try {
			
			req = new ActiveXObject("Msxm12.XMLHTTP"); /*alcune versioni di IE*/
			
			}
			
		catch(err2){
			
			try {
				
				req = new ActiveXObject("Microsoft.XMLHTTP"); /*alcune versioni di IE*/
				
				}
			
			catch(err3) {
				
				req = false;
				
				}	
			}
		}
	
	return req;
	
	}



function getAjax(valore,idValore,fileRisposta,idElemento) { //funzione che esegue la chiamata AJAX 
	
	
	var elemento = document.getElementById(idElemento);
	elemento.options.length = 0;
	
	var indirizzoValore = document.getElementById(idValore);
	var variabile = indirizzoValore.options[valore].value; //valore della select 
	
	var myRequest = getXMLHTTPRequest();
	
	
	var file = fileRisposta; 
	var myRand = + new Date().getTime();
 	var url = file + "?valore=" + variabile + "&rand=" + myRand ;
	
	//Chiedo all'oggetto XMLHTTPRequest di aprire una connessione al server
	
	myRequest.open("get", url, true);
	
	//preparo funzine da eseguire quando arriva la risposta
	
	myRequest.onreadystatechange = responseAjax;

	//invio la richiesta
	
	myRequest.send(null);
	
	
		function responseAjax(){

			if ( myRequest.readyState == 4 ) {
				
				
				if ( myRequest.status == 200 ) {
					
					//Richiesta andata a buon fine, elaboro i dati
					eval(myRequest.responseText);
					
								
					}
				else {
					alert("Errore nella chiamata HTTP: " + myRequest.statusText);
					}
				}
				
			}
	
	
	
	}
//   ----------- END AJAX FUNCTION ------------- //


//   ----------- CECK FORM FUNCTION ------------- //
function ceckInput(indirizzo,errore){
	
	var valore = indirizzo.value;
	if (valore == "" || valore == 0){
		
			alert(errore);
			return indirizzo.focus();
		}
	else {
		return true;
		}
	
	}
function ceckSelect(indirizzo,opzione,errore){
	
	var valore = opzione.value;
	if (valore == "" || valore == 0){
		
			alert(errore);
			return indirizzo.focus();
		}
	else {
		return true;
		}
	
	}

function controllomail(indirizzo,errore){
	var mail = indirizzo.value;
	var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
	
	if (!espressione.test(mail))
	{
	    alert(errore);
		return indirizzo.focus();
	}
	else{
		return true;
		}

}

function controllopass(indirizzo){
	
	var passLength = indirizzo.value.length;
	var passespressione = /^[0-9A-Za-z]*$/;
	var passValu = indirizzo.value;
	
	if (!passespressione.test(passValu)) {
		
		alert ("PASSWORD può contenere solo lettere dalla a alla z e numeri");
		return indirizzo.focus();
		
		}
	else if (passLength <= 5 || passLength == 0 ) {
		
		alert ("La PASSWORD deve essere almeno 6 caratteri");
		return indirizzo.focus();
		
		}
	else if (passLength >= 17){
		
		alert ("La PASSWORD può essere massimo 16 caratteri");
		return indirizzo.focus();
		
		}
	else{
		
		return true;
		
		}
	
	}
	
function controllopiva(indirizzo){

	var pivaVal = indirizzo.value;
	var pivaLength = indirizzo.value.length;
	var stringaNumerica = /^[0-9]+$/;
	
	if(!stringaNumerica.test(pivaVal)){
		
		alert ("La PARTITA IVA è composta da soli numeri");
		return indirizzo.focus();
		
		}
	else if ( pivaLength != 11 ) {
		
		alert ("La PARTITA IVA non è corretta");
		return indirizzo.focus();
		
		}
	else{
		
		return true;
		
		}
	
	}
	
function ceckChecked(indirizzo,errore){
	
	if ( !indirizzo.checked == true){
		alert(errore);
		return indirizzo.focus();
	}
	else{
		return true;
		}
	
	}
//   ----------- END CECK FORM FUNCTION ------------- //


//   ----------- SEND FORM FUNCTION ------------- //
function SimoneFirst() {
	var comuns = document.ricerca.selectComune.value;
	var categor = document.ricerca.categoria.value;
	var sottocategor = document.ricerca.sottocategoria.value;
		if (comuns == 0 || comuns == ""){
				if (categor == 0 || categor == ""){
					alert("Seleziona una categoria!!!");
					document.ricerca.categoria.focus();
					return false;
				}
				else {
						if (sottocategor == 0 || sottocategor == ""){
						document.ricerca.action = "http://www.azienderoma.it/"+categor+"/index.html";
						document.ricerca.submit();
						}
						else {
						document.ricerca.action = "http://www.azienderoma.it/"+categor+"/"+sottocategor+"/";
						document.ricerca.submit();
						}
					}
		}
		else{
				if (categor == 0 || categor == ""){
					alert("Seleziona una categoria!!!");
					document.ricerca.categoria.focus();
					return false;
				}
				else {
						if (sottocategor == 0 || sottocategor == ""){
						alert("Seleziona una sottocategoria!!!");
						document.ricerca.sottocategoria.focus();
						return false;
						
						}
						else {
						document.ricerca.action = "http://www.azienderoma.it/"+categor+"/"+sottocategor+"/"+comuns+"/index.html";
						document.ricerca.submit();
						}
					}
			}
	}

function inviaReg() {
	
	var dove = document.getElementById("formReg");
	var eail = dove.email.value;
	var ceail = dove.cemail.value;
	
	/*if( ome == "" || ome == 0 ){
		alert('campo NOME non è stato compilato correttamete');
		dove.nome.focus();
		return false;
		}*/
		
	if	( !ceckInput(dove.nome,"campo NOME non è stato compilato corretamente") ){
		
		return false;
		
		}
		
	else if ( !ceckInput(dove.cognome,"campo COGNOME non è stato compilato corretamente") ){
		
		return false;
		
		}
	else if( !controllomail(dove.email,"La mail inserita non è valida!") ){
		
		return false;
		
		}
	else if ( eail != ceail ){
		alert('Inserire correttamente la email di conferma');
		dove.cemail.focus();
		return false;
		}
	else if ( !controllopass(dove.pass) ){
		
		return false;
		
		}
	else if ( !ceckInput(dove.ragione_sociale,"campo RAGIONE SOCIALE non è stato compilato corretamente") ){
		
		return false;
		
		}
	else if ( !controllopiva(dove.piva) ){
		
		return false;
		
		}
	else if ( !ceckSelect(dove.categoria,dove.categoria.options[dove.categoria.selectedIndex],"Seleziona una CATEGORIA per l' azienda") ){
		
		return false;
		
		}
	else if ( !ceckSelect(dove.sottocategoria,dove.sottocategoria.options[dove.sottocategoria.selectedIndex],"Seleziona una SOTTOCATEGORIA per l'azienda") ){
		
		return false;
		
		}
	else if ( !ceckSelect(dove.comune,dove.comune.options[dove.comune.selectedIndex],"Seleziona il COMUNE") ){
		
		return false;
		
		}
	else if ( !ceckInput(dove.indirizzo,"campo INDIRIZZO non è stato compilato") ){
		
		return false;
		
		}
	else if ( !ceckInput(dove.tel,"campo TELEFONO non è stato compilato") ){
		
		return false;
		
		}
		else if ( !controllomail(dove.email_azienda,"EMAIL AZIENDA non è corretta") ){
		
		return false;
		
		}
		else if ( !ceckInput(dove.descrizione,"campo DESCRIZIONE non è stato compilato") ){
		
		return false;
		
		}
		else if ( !ceckInput(dove.captcha,"Inserisci il codice dell'immagine") ){
		
		return false;
		
		}
		else if ( !ceckChecked(dove.accetto,"Devi accettare quanto riportato nell'informativa") ){
		
		return false;
		
		}
	else{
		
		dove.action = "http://www.azienderoma.it/iscrizione_azienda.html";
		dove.submit();
		
		}/*
	else if ( pasLenght <= 5 ) {
		alert('Password inferiore a 6 caratteri');
		dove.pass.focus();
		return false;
		}
	else if ( pasLenght >= 15 ) {
		alert('Password superiore a 14 caratteri');
		dove.pass.focus();
		return false;
		}*/
	
	
	 
	}
	
function FormContatti(){
	var dove = document.getElementById('contatti');
	var nom = document.contatti.nome.value;
	var emai = document.contatti.mail.value;
	var domand = document.contatti.domanda.value;
		
		if	( !ceckInput(dove.nome,"campo NOME non è stato compilato corretamente") ){
		
		return false;
		
		}
		else if ( !controllomail(dove.mail,"EMAIL non è corretta") ){
		
		return false;
		
		}
		else if	( !ceckInput(dove.domanda,"Scrivi corretamente la tua domanda") ){
		
		return false;
		
		}
		else if ( !ceckInput(dove.captcha,"Inserisci il codice dell'immagine") ){
		
		return false;
		
		}
			else {
					document.contatti.action = "http://www.azienderoma.it/contatti.html";
					document.contatti.submit();
					
				}
				
		}
		
function FormElimina(idAziend){
	var dove = document.getElementById('contatti');
	var nom = document.contatti.nome.value;
	var emai = document.contatti.mail.value;
	var domand = document.contatti.domanda.value;
		
		if	( !ceckInput(dove.nome,"campo NOME non è stato compilato corretamente") ){
		
		return false;
		
		}
		else if ( !controllomail(dove.mail,"EMAIL non è corretta") ){
		
		return false;
		
		}
		else if	( !ceckInput(dove.domanda,"Scrivi corretamente la tua motivazione") ){
		
		return false;
		
		}
		else if ( !ceckInput(dove.captcha,"Inserisci il codice dell'immagine") ){
		
		return false;
		
		}
			else {
					document.contatti.action = "http://www.azienderoma.it/eliminazione_azienda_"+idAziend+".html";
					document.contatti.submit();
					
				}
				
		}

//   ----------- END SEND FORM FUNCTION ------------- //
