// JavaScript Document

// controlla email
function controllaEmail(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);var res=1;if (str.indexOf(at)==-1){
		res=0;
	}
	else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		res=0;
	}
	else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		res=0;
	}
	else if (str.indexOf(at,(lat+1))!=-1){
		res=0;
	}
	else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		res=0;
	}
	else if (str.indexOf(dot,(lat+2))==-1){
		res=0;
	}
	else if (str.indexOf(" ")!=-1){
		res=0;
	}
	if(res==1) {
		return true;
	}
	else {
		return false;
	}					
}

function addSeparatorsNF(nStr, inD, outD, sep)
{
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}

function quoteReplace(fields) {

	var elenco=fields.split(",");

	var tmp;

	for(i in elenco) {

		var tmp=document.getElementById(elenco[i]).value;

		document.getElementById(elenco[i]).value=tmp.replace(/\'/g, "&#39;");

		document.getElementById(elenco[i]).value=tmp.replace(/\"/g, "&#34;");

	}

}



// cookie functions

function setCookie(name,value,days) {

	if (days) {

		var date = new Date();

		date.setTime(date.getTime()+(days*24*60*60*1000));

		var expires = "; expires="+date.toGMTString();

	}

	else var expires = "";

	document.cookie = name+"="+value+expires+"; path=/;";

}



function getCookie(name) {

	var nameEQ = name + "=";

	var ca = document.cookie.split(';');

	for(var i=0;i < ca.length;i++) {

		var c = ca[i];

		while (c.charAt(0)==' ') c = c.substring(1,c.length);

		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

	}

	return null;

}



function deleteCookie(name) {

	setCookie(name,"",-1);

}


function getcwd() {
  var myloc = window.location.href;
  var locarray = myloc.split("/");
  delete locarray[(locarray.length-1)];
  var arraytext = locarray.join("/");
  return arraytext;
}


function showHide(elemento) {
  var stato = document.getElementById(elemento).style.display;
  var newstato = 'none';
  if(stato == 'none') newstato = '';
  document.getElementById(elemento).style.display=newstato;
}

function coloraCampo(idCampo) {
  
  document.getElementById(idCampo).focus();
  document.getElementById(idCampo).style.backgroundColor='#ffcccc';
  /*document.getElementById('messaggioerrore').style.display='';*/
  return true;
}

function decoloraCampo(idCampo) {
    idCampo.style.backgroundColor='';
    /*document.getElementById('messaggioerrore').style.display='none';*/
}


// controlla i campi inseriti in sede di registrazione
function checkFields() {

  
  var firstname = document.getElementById('firstname').value;
  var lastname = document.getElementById('lastname').value;
  var citta = document.getElementById('citta').value;
  var email = document.getElementById('email').value;
  var request = document.getElementById('request').value;
  var accetto = document.getElementById('accetto');

  var stato = document.getElementById('stato').value;
  var telefono = document.getElementById('telefono').value;

  if (firstname == '' || firstname == null || firstname == undefined) {
  	coloraCampo('firstname');
  	return false;
  }
  else 
  	if (lastname == '' || lastname == null || lastname == undefined) {
  		coloraCampo('lastname');
  		return false;
  	}
  	else 
			/*
  		if (stato == '' || stato == null || stato == undefined) {
  			coloraCampo('stato');
  			return false;
  		}
  		else 
  		*/
  			if (citta == '' || citta == null || citta == undefined) {
  				coloraCampo('citta');
  				return false;
  			}
  			else 
  				if (!controllaEmail(email)) {
  					coloraCampo('email');
  					return false;
  				}
  				else   
						if (!accetto.checked) {
	  					coloraCampo('accetto');
	  					return false;
	  				}
						else {
  				
  					$.ajax({
  						url: "ajax/sendForm.php",
  						data: "firstname=" + firstname + "&lastname=" + lastname + "&stato=" + stato + "&citta=" + citta + "&email=" + email + "&telefono=" + telefono + "&request=" + request,
  						success: function(html){
								alert('Richiesta registrata con successo.');
  						}
  					});
  					
  				}
}


function removeColor() {
  document.getElementById('rag_soc').style.backgroundColor='';
  document.getElementById('email').style.backgroundColor='';
  document.getElementById('telefono').style.backgroundColor='';
  document.getElementById('libero').style.backgroundColor='';
  document.getElementById('contmessaggio1').style.display='none';
  document.getElementById('contmessaggio2').style.display='none';
  document.getElementById('contmessaggio3').style.display='none';
  document.getElementById('contmessaggio4').style.display='none';
  return true;
}

/* controlla estensione file */
function checkExt(whichInput,estensioni) {
	var risultato = false;
	
	if(estensioni.length>0) {
		
		var nomeFile = whichInput.value;
		var estensioneFileArray = nomeFile.split(".");
		var estensione = estensioneFileArray[estensioneFileArray.length-1];
		
		var array_valide = estensioni.split(",");
		var stringaValidi = '';
		
		for(i=0;i<array_valide.length;i++) {
			if(array_valide[i].length>0) stringaValidi = stringaValidi + ' ' + array_valide[i];
			if(risultato == false && estensione.toLowerCase() == array_valide[i]) {
				risultato = true;
			}
		}
	}
	else {
		risultato = true;
	}
	
	if(!risultato) {
		alert('Formato non valido. I formati riconosciuti sono' + stringaValidi);
		whichInput.value='';
	}
	
}

function archivio(classe) {
	$("."+classe).toggle();
}