/*
var winUrl = window.location;
var topUrl = top.location;
if (winUrl != topUrl) {
  top.location.href = winUrl;
}
*/

function enableSuspendValues() {
  // servizi-suspend.htm
  var el = window.document.frmMain;
  el.suspend_beg.disabled = el.eraseSuspend.checked;
  el.suspend_end.disabled = el.eraseSuspend.checked;
}

function setIbsBooksAction() {
  var el = window.document.frmIbsBooks;
  var defaultAction = "http://www.internetbookshop.it/ser/serpge.asp?";
  
  if (el.ENGBOOK.checked) {
    el.action = "http://www.internetbookshop.it/ame/ser/serpge.asp?";
    el.x.value = el.S.value; // Argomento;
    if (el.x.value == '') {
      el.x.value = el.T.value; // Titolo;
      if (el.x.value == '')
        el.x.value = el.A.value; // Autore;
    }
    alert (el.x.value);
  }
  else
    el.action = defaultAction;
}

function categoriaOrderBy(id) {
  var ctg=window.document.frmCategoria.c.value;
  var url="categoria.htm";
  if (ctg!="") {
    url+="?c="+ctg+"&";
  } else {
    url+="?";
  }
  url +="ob=" + window.document.frmCategoria.ob[id].value;
  window.location.href=url;
}

function indexOrderBy(id) {
  var letter=window.document.frmIndice.l.value;
  var url="indice.htm";
  if (letter!="") {
    url+="?letter="+letter;
    if (id==1)
      url+="&ob=n";
  } else {
    if (id==1) url+="?ob=n";
  }
  window.document.frmIndice.ob[id].checked=true;
  window.location.href=url;
}

function setNl(i){
  window.document.frmNewsletter.ubag[i].checked = !(window.document.frmNewsletter.ubag[i].checked);
}

function search() {
  /*window.document.frmBoxRicerca.submit();*/
  var q = window.document.frmBoxRicerca.termine.value;
  top.location.href = 'risultati.htm?q=' + decodeURI(q);
}

function checkBoxDate() {
	if (window.document.frmBoxRicerca.selD) {
	  var m=window.document.frmBoxRicerca.selM.options[document.frmBoxRicerca.selM.selectedIndex].value;
	  var d=window.document.frmBoxRicerca.selD.options[document.frmBoxRicerca.selD.selectedIndex].value;
	  if (d!="" && m!="") {
	    if (d.length==1) d="0"+d;
	    if (m.length==1) m="0"+m;
	    window.document.frmBoxRicerca.d.value=m+d;
	    //alert(window.document.frmBoxRicerca.d.value);
	    /*window.document.frmBoxRicerca.submit();*/
	    top.location.href = 'risultati.htm?d=' + window.document.frmBoxRicerca.d.value;
	  }
	}
}

function cambiaCalendarioRicorrenze() {
	var mm = document.getElementById('selm').value;
	if (mm.length == 1) mm = '0' + mm;
	top.location.href = 'ricorrenze.htm?d=' + document.getElementById('sely').value + mm;
}

function showMonth(id) {
  var url=window.location.href;
  var newUrl=url;
  var pos1, pos2, end, oldVal;
  if (url.indexOf("?")<0)
    newUrl=url+"?m="+id;
  else {
    pos1 = url.indexOf("?m=");
    if (pos1<0) {
      pos1 = url.indexOf("&m=");
      if (pos1<0) {
        newUrl=url+"&m="+id;
      } else {
        pos2 = pos1+3;
        end  = url.indexOf("&", pos2);
        if (end<0) end=url.length;
        oldVal=url.substring(pos2,end);
        newUrl=url.replace("m="+oldVal,"m="+id);
      }
    } else {
      pos2 = pos1+3;
      end  = url.indexOf("&", pos2);
      if (end<0) end=url.length;
      oldVal=url.substring(pos2,end);
      newUrl=url.replace("m="+oldVal,"m="+id);
    }
  }
  window.location.href=newUrl;
}

function checkEmail() {
  if(validaEmail(window.document.frmNewsletter.email.value))
    return true;
  else {
    alert("Indirizzo email non valido");
    return false;
  }
}

function checkInvita1() {
  var elem = window.document.frmInvita;
  var res = false;
  var atLeastOne = false;
  var errMsg = "";
  for (var i=0;i<elem.dest.length;i++) {
    if (Trim(elem.dest[i].value)!='') {
      atLeastOne=true;
      if(validaEmail(elem.dest[i].value)) {
        res=true;
      } else {
        errMsg = elem.dest[i].value + "\nIndirizzo email non valido";
        res = false;
        break;
      }
    }
  }
  if (!atLeastOne) errMsg="Indicare almeno un indirizzo email";
  if (errMsg!='') alert(errMsg);
  return res;
}

function checkInvita2() {
  var elem = window.document.frmInvita;
  var res = false;
  var atLeastOne = false;
  var errMsg = "";
  for (var i=0;i<elem.dest.length;i++) {
    if (Trim(elem.dest[i].value)!='') {
      atLeastOne=true;
      if(validaEmail(elem.dest[i].value)) {
        res=true;
      } else {
        errMsg = elem.dest[i].value + "\nIndirizzo email non valido";
        res = false;
        break;
      }
    }
  }
  if (!atLeastOne) errMsg="Indicare almeno un indirizzo email";
  
  if (errMsg=='' && Trim(elem.subj.value)=='') {
    errMsg="Indicare l\'oggetto del messaggio";
    res = false;
  }
  
  if (errMsg=='' && Trim(elem.msg.value)=='' && elem.chkMsg.checked) {
    errMsg="Utilizzare il messaggio standard\noppure indicare un proprio messaggio personale";
    res = false;
  }
  
  if (errMsg=='' && Trim(elem.firma.value)=='') {
    errMsg="Inserisci il tuo nome per firmare il messaggio";
    res = false;
  }
  
  if (errMsg!='') alert(errMsg);
  return res;
}

function checkLogon() {
  var elem = window.document.frmLogon;
  var errMsg = '';
  var res = true;
  
  if(Trim(elem.UID.value)=='') {
    errMsg = "Inserisci il nickname scelto\nal momento della registrazione";
    res = false;
  }
  
  if (errMsg=='' && Trim(elem.pw.value)=='') {
    errMsg="Inserisci la password scelta\nal momento della registrazione";
    res = false;
  }
  
  if (errMsg!='') alert(errMsg);
  return res;
}

function checkRegistrati() {
  var elem = window.document.frmRegistrati;
  var errMsg = '';
  var res = true;
  var KPW_MIN_CHARS = 6;
  
  if(!validaEmail(elem.email.value)) {
    errMsg = "Indirizzo E-Mail non valido";
    res = false;
  }
  
  var pw0 = Trim(elem.pw[0].value);
  var pw1 = Trim(elem.pw[1].value);
  if (res && pw0=='') {
    errMsg="Scegli una password";
    res = false;
  }
  if (res && pw0.length < KPW_MIN_CHARS) {
    errMsg="Scegli una password di almeno 6 caratteri";
    res = false;
  }
  if (res && !validaChars(pw0)) {
    errMsg="Scegli una password composta\nda sole lettere o numeri";
    res = false;
  }
  if (res && pw0!=pw1) {
    errMsg="La ripetizione non conferma\nla password inserita";
    elem.pw[1].value = '';
    res = false;
  }
  
  if (res && elem.prov[elem.prov.selectedIndex].value == '') {
    errMsg= "Non è stata indicata la provincia";
    res = false;
  }
  
  if (res && elem.accetto[1].checked) {
    errMsg = "Impossibile proseguire\nsenza accettare l'informativa";
    res = false;
  }
  
  if (errMsg!='') alert(errMsg);
  return res;
}

function checkLostPwd() {
  var elem = window.document.frmLostPwd;
  var errMsg = '';
  var res = true;
  
  if(!validaEmail(elem.email.value)) {
    errMsg = "Indirizzo email non valido";
    res = false;
  }
  
  if (errMsg!='') alert(errMsg);
  return res;
}

function setStdMsg() {
  if (window.document.frmInvita.chkMsg.checked)
    window.stdMsg.className="cntdisabled";
  else
    window.stdMsg.className="cnt";
}

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);
}