var winUrl = window.location;
var topUrl = top.location;
if (winUrl != topUrl) {
  top.location.href = winUrl;
}

function checkFormContatti() {
  var valida = true;
  var err = "";

  if(!validaEmail(window.document.frmContatti.email.value)) {
    valida=false;
    err="Indirizzo email non valido";
  }

  if(!valida && validaEmail(window.document.frmContatti.nome.value)) {
    valida=true;
    window.document.frmContatti.email.value=window.document.frmContatti.nome.value;
    window.document.frmContatti.nome.value="";
    err="";
  }

  var txtMsg=window.document.frmContatti.msg.value;
  if(valida && txtMsg=="") {
    valida=false;
    err="Inserire il testo del messaggio";
  }
  if(valida && txtMsg.length>30000) {
    valida=false;
    err="Testo del messaggio troppo lungo";
  }
  
  var optElements = window.document.frmContatti.optSubj.length;
  var atLeastOne = false;
  if (valida) {
    for (var i=0;i<optElements;i++) {
      atLeastOne = window.document.frmContatti.optSubj[i].checked;
      if (atLeastOne) break;
    }
    
    if (window.document.frmContatti.optSubj[optElements-1].checked) {
      if (window.document.frmContatti.txtSubj.value=="")
        atLeastOne=false;
    }
    
    if (!atLeastOne) {
      valida=false;
      err="Indicare l\'oggetto del messaggio";
    }
  }
  
  if (!valida) alert(err);
  return valida;
}

function validaEmail(stringa) {
	var valida = false;
	var c;
	var found_chiocciola = 0;
	var found_punto = false;
	var found_spazio = false;
	var found_virgola = false;
	  	
	// CONTROLLO solo 1 "@" e almeno 1 "." e nessuno " " e nessuna ","
	if (stringa.length > 6) {
  	for (i=0;i<stringa.length;i++){
  		c = stringa.charAt(i);
  		if (c==".") found_punto = true;
  		if (c=="@") found_chiocciola++;
  		if (c==" ") found_spazio = true;
  		if (c==",") found_virgola = true;
  	}
  }
	if (found_chiocciola==1 && found_punto) valida = true;
	if (found_spazio) valida = false;
	if (found_virgola) valida = false;
	  	
	// CONTROLLO "@" e "." all'inizio oppure alla fine
	if (valida) {
  	c = stringa.charAt(0);
  	if (c=="@" || c==".") {
  		valida = false;
  	}
  	c = stringa.charAt(stringa.length-1);
  	if (c=="@" || c==".") {
  		valida = false;
    }
  }
		
	// CONTROLLO "@." e ".@"
	if (valida) {
		var aParts = stringa.split("@");
	  	var err=false;
	  	for (i=0;i<aParts.length;i++) {
	  		if (aParts[i].charAt(aParts[i].length-1)==".") {
	  			valida=false;
	  			break;
	  		}
	  		if (aParts[i].charAt(0)==".") {
	  			valida=false;
	  			break;
	  		}
	  	}
	}
	return valida;
}

function validaChars(s) {
	var valida = true;
	var c;
	  	
	for (var i=0; i< s.length; i++){
		c = s.charCodeAt(i);
		
		// 49-57  (Numeri)
		// 65-90  (Maiuscole)
		// 97-122 (Minuscole)
		
		if (!((c >= 49 && c <= 57) ||
		      (c >= 65 && c <= 90) ||
		        (c >= 97 && c <= 122))) {
		  valida = false;
		  break;
		}
	}
  
	return valida;
}

function Trim(MyString) {
	var NoExit;
	var x;
	var NoExit;
	var MyChar;

	MyString = MyString + "";
	NoExit = true;
	x = MyString.length-1;

	do 
	{
		MyChar = MyString.charCodeAt(x);
		
		if (MyChar != 32)
			NoExit = false
		else
			x = x -1;
	}
	while (NoExit)

	MyString = MyString.substring(0,x+1);
	return (MyString);
}
