// 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;");

		/*

		document.getElementById(elenco[i]).value=tmp.replace(/à/g, "&agrave;");

		document.getElementById(elenco[i]).value=tmp.replace(/è/g, "&egrave;");

		document.getElementById(elenco[i]).value=tmp.replace(/ì/g, "&igrave;");

		document.getElementById(elenco[i]).value=tmp.replace(/ò/g, "&ograve;");

		document.getElementById(elenco[i]).value=tmp.replace(/ù/g, "&ugrave;");

		*/

	}

}



// 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;
}

// controlla bontà dell'accesso
function checkLogin() {

  var accediusername = document.getElementById('accediusername').value;
  var accedipassword = document.getElementById('accedipassword').value;
  var theLink = document.getElementById('fromlink').value;
  
  var criptata = Javacrypt.displayPassword();
  criptata=criptata[0];
  theLink = theLink + '';
  var risposta = new Ajax.Request('ajax_checklogin.php',
    {
      parameters: {
        email: accediusername,
        password: criptata
      },
      onSuccess: function(transport){
        var response = transport.responseText || "no response text";
        if(response > 0) {
          theLink = theLink + '&action=register&ID_reg=' + response;
          location.href = theLink;
          return false;
        }
        else {
          return false;
        }
      },
      onFailure: function(){ alert('Something went wrong...') }
    }
  );
}

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 theLink = document.getElementById('fromlink').value;
  
  var profilo_nome = document.getElementById('profilo_nome').value;
  var profilo_cognome = document.getElementById('profilo_cognome').value;
  var profilo_indirizzo1 = document.getElementById('profilo_indirizzo1').value;
  var profilo_indirizzo2 = document.getElementById('profilo_indirizzo2').value;
  var profilo_cap = document.getElementById('profilo_cap').value;
  var profilo_citta = document.getElementById('profilo_citta').value;
  var profilo_provincia = document.getElementById('profilo_provincia').value;
  var profilo_stato = document.getElementById('profilo_stato').value;
  var profilo_telefono = document.getElementById('profilo_telefono').value;
  var profilo_email = document.getElementById('profilo_email').value;
  var profilo_password = document.getElementById('profilo_password').value;
  var profilo_passwordconf = document.getElementById('profilo_passwordconf').value;

  if(profilo_nome == '' || profilo_nome == null || profilo_nome == undefined) {
    coloraCampo('profilo_nome');
    return false;
  }
  else if(profilo_cognome == '' || profilo_cognome == null || profilo_cognome == undefined) {
    coloraCampo('profilo_cognome');
    return false;
  }
  else if(profilo_indirizzo1 == '' || profilo_indirizzo1 == null || profilo_indirizzo1 == undefined) {
    coloraCampo('profilo_indirizzo1');
    return false;
  }
  else if(profilo_cap == '' || profilo_cap == null || profilo_cap == undefined) {
    coloraCampo('profilo_cap');
    return false;
  }
  else if(profilo_citta == '' || profilo_citta == null || profilo_citta == undefined) {
    coloraCampo('profilo_citta');
    return false;
  }
  else if(profilo_provincia == '' || profilo_provincia == null || profilo_provincia == undefined) {
    coloraCampo('profilo_provincia');
    return false;
  }
  else if(profilo_stato == '' || profilo_stato == null || profilo_stato == undefined) {
    coloraCampo('profilo_stato');
    return false;
  }
  else if(profilo_telefono == '' || profilo_telefono == null || profilo_telefono == undefined) {
    coloraCampo('profilo_telefono');
    return false;
  }
  else if(!controllaEmail(profilo_email)) {
    coloraCampo('profilo_email');
    return false;
  }
  else if(profilo_password == '' || profilo_password == null || profilo_password == undefined || profilo_password.length<5) {
    coloraCampo('profilo_password');
    return false;
  }
  else if(profilo_passwordconf == '' || profilo_passwordconf == null || profilo_passwordconf == undefined || profilo_passwordconf.length<5) {
    coloraCampo('profilo_passwordconf');
    return false;
  }
  else if(profilo_passwordconf != profilo_password) {
    coloraCampo('profilo_passwordconf');
    return false;
  }
  else {
    
    var criptata = Javacrypt.displayPassword(profilo_password);
    criptata=criptata[0];
    
    var risposta = new Ajax.Request('ajax_register.php',
      {
        parameters: {
          profilo_nome: profilo_nome,
          profilo_cognome: profilo_cognome,
          profilo_indirizzo1: profilo_indirizzo1,
          profilo_indirizzo2: profilo_indirizzo2,
          profilo_cap: profilo_cap,
          profilo_citta: profilo_citta,
          profilo_provincia: profilo_provincia,
          profilo_stato: profilo_stato,
          profilo_telefono: profilo_telefono,
          profilo_email: profilo_email,
          profilo_password: criptata
        },
        onSuccess: function(transport){
          var response = transport.responseText || "no response text";
          if(response > 0) {
            theLink = theLink + '&action=firstregister&ID_ver=' + response;
            alert(theLink);
            location.href = theLink;
            return false;
          }
          else {
            return false;
          }
        },
        onFailure: function(){ alert('Something went wrong...') }
      }
    );
  }
}


function galleryFoto(link,width) {
	document.getElementById('galleria_foto').src='imageresample.php?src='+link+'&typ=w&siz='+width;
	//document.getElementById('galleria_didascalia').innerHTML=dida;
}

function checkContatti() {
  var rag_soc = document.getElementById('rag_soc');
  var email = document.getElementById('email');
  var telefono = document.getElementById('telefono');
  var libero = document.getElementById('libero');
  var contmessaggio = document.getElementById('contmessaggio');
  
  if(rag_soc.value.length==0) {
    coloraCampo(rag_soc);
    rag_soc.focus();
    document.getElementById('contmessaggio1').style.display='';
  	return false;  
  }
  else if(email.value.length==0 && telefono.value.length==0) {
    coloraCampo(email);
    coloraCampo(telefono);
    email.focus();
    document.getElementById('contmessaggio2').style.display='';
    return false;
  }
  else if(email.value.length>0 && controllaEmail(email.value) == false ) {
    coloraCampo(email);
    email.focus();
    document.getElementById('contmessaggio4').style.display='';
    return false;
  }
  else if(libero.value.length==0) {
    coloraCampo(libero);
    libero.focus();
    document.getElementById('contmessaggio3').style.display='';
    return false;
  }
  else {
    return true;
  }
}

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;
}

function coloraCampo(campo) {
  campo.style.backgroundColor='#ffdd44';
  return true;
}

function filterCat() {
	var select = document.getElementById("ID_cat");
	var select2 = document.getElementById("ID_mat");
	var ID_cat = select.options[select.selectedIndex].value;
	var ID_mat = select2.options[select2.selectedIndex].value;
	location.href = "index.php?mode=referenze&ID_cat="+ID_cat+"&ID_mat="+ID_mat;
}

/* 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='';
	}
	
}
