function normalizeCampo(campo){
  document.getElementById(campo).style.border = "1px solid #ccc";
}
function indirizzoEmailValido(indirizzo) { 
	if (window.RegExp) { 
		var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"; 
		var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$"; 
		var regnv = new RegExp(nonvalido); 
		var regv = new RegExp(valido); 
		if (!regnv.test(indirizzo) && regv.test(indirizzo)) return true; 
		return false; 
	}else{ 
		if(indirizzo.indexOf("@") >= 0) return true; 
		return false; 
	} 
}

function controllaLogin(){
  document.getElementById('user').style.border = "1px solid #ccc";
  document.getElementById('pass').style.border = "1px solid #ccc";
  if(document.getElementById('user').value == false){
    alert("USER: campo mancante");
    document.getElementById('user').focus();
    document.getElementById('user').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('pass').value == false){
    alert("PASSWORD: campo mancante");
    document.getElementById('pass').focus();
    document.getElementById('pass').style.border = "1px solid #ff6600";
    return false;
  }else {
    //document.getElementById('login').submit();
    //return true;
    inviaLogin(document.getElementById('user').value,document.getElementById('pass').value);
  }
}

function inviaLogin(user,pass) {
  ajaxGetRand('?cmd=validation&user='+escape(user)+'&pass='+escape(pass),myHandler);
  function myHandler(aa){
    document.getElementById('carica').style.visibility='hidden';
    if(aa=="no"){
      document.getElementById('pass').value = "";
      alert("Account non valido");
    }else if(aa=="oki"){
      location.href="?cmd=areaRis";
    }
    //location.href = "view.html";
    /*document.getElementById('titolo').value = "";
    document.getElementById('posizione').value = "";
    frames['rte1'].document.body.innerHTML = "";*/
  }
}

function openAddDoc(){
	xx = getPosX(document.getElementById('pippo'));
	yy = getPosY(document.getElementById('pippo')) + 15;
	ajaxGetRand('?cmd=addDoc',myHandler);
  function myHandler(aa){
    document.getElementById('carica').style.visibility='hidden';
    document.getElementById('obj').style.width="400px";
    document.getElementById('obj').style.display="block";
    document.getElementById('obj').style.top=yy+"px";
    document.getElementById('obj').style.left=xx+"px";
    document.getElementById('obj').innerHTML = aa;
  }
}

function getPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}else if (obj.x)
	curleft += obj.x;
	//alert(curleft);
	return curleft;
}

function getPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}else if (obj.y)
	curtop += obj.y;
	//alert(curtop);
	return curtop;
}function controllaDoc(){	document.getElementById('nome').style.border = "1px solid #ccc";
  document.getElementById('filex').style.border = "1px solid #ccc";	document.getElementById('categoria').style.border = "1px solid #ccc";
  if(document.getElementById('nome').value == false){
    alert("NOME: campo mancante");
    document.getElementById('nome').focus();
    document.getElementById('nome').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('filex').value == false){
    alert("FILE: campo mancante");
    document.getElementById('filex').focus();
    document.getElementById('filex').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('categoria').value == false){
    alert("CATEGORIA: campo mancante");
    document.getElementById('categoria').focus();
    document.getElementById('categoria').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('categoria').value == "fattura" && document.getElementById('userDoc').value == false){
    alert("USER: campo mancante");
    document.getElementById('userDoc').focus();
    document.getElementById('userDoc').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('categoria').value == "analisi" && document.getElementById('userDoc').value == false){
    alert("USER: campo mancante");
    document.getElementById('userDoc').focus();
    document.getElementById('userDoc').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('categoria').value == "haccpFlowChart" && document.getElementById('userDoc').value == false){
    alert("USER: campo mancante");
    document.getElementById('userDoc').focus();
    document.getElementById('userDoc').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('categoria').value == "specificheProdotti" && document.getElementById('userDoc').value == false){
    alert("USER: campo mancante");
    document.getElementById('userDoc').focus();
    document.getElementById('userDoc').style.border = "1px solid #ff6600";
    return false;
  }else{
    document.getElementById('addDoc').submit();
    //return true;
  }}

function multiSel333(idInput,idHidden,valueInput,valueHidden,obj){
	alert(valueHidden);
}

function multiSel(idInput,idHidden,valueInput,valueHidden,obj){
	var i=0;
	valsAr = document.getElementById(idHidden).value;
	valsAr = valsAr.split(";");
	flag = false;
	for(var i=0;i<valsAr.length;i++){
		if(valueHidden == valsAr[i]) flag = true;		
	}
	inPut = document.getElementById(idInput);
	hiPut = document.getElementById(idHidden);
	if(flag){
		// toglie item
		obj.style.fontWeight='normal';
		arText = inPut.value;
		dummy = arText.split(" - ");
		arText = dummy;
		newText = new Array();
		for(var i=0; i<arText.length; i++){
			if(arText[i] != valueInput) newText.push(arText[i]);
		}
		inPut.value = newText.join(" - ");
		// hidden
		arText = hiPut.value;
		dummy = arText.split(";");
		arText = dummy;
		newText = new Array();
		for(var i=0; i<arText.length; i++){
			if(arText[i] != valueHidden) newText.push(arText[i]);
		}
		hiPut.value = newText.join(";");
	}else {
		// aggiunge item
		obj.style.fontWeight='bolder';
		if(inPut.value != "") inPut.value += " - ";
		inPut.value += valueInput;
		// hidden
		if(hiPut.value != "") hiPut.value += ";";
		hiPut.value += valueHidden;
	}
}

function mostraUser(){
	cat = document.getElementById("categoria").value;
	if(cat == "fattura" || cat == "analisi" || cat == "specificheProdotti" || cat == "haccpFlowChart"){
		document.getElementById("hidUser").style.display = "block";
	}else{
		document.getElementById("hidUser").style.display = "none";
	}
}

function divUser(){
	xx = getPosX(document.getElementById('pippo2')) + 35;
	yy = getPosY(document.getElementById('pippo2')) - 100;
	ajaxGetRand('?cmd=addUser',myHandler);
  function myHandler(aa){
    document.getElementById('carica').style.visibility='hidden';
    document.getElementById('obj2').style.width="auto";
    document.getElementById('obj2').style.height="auto";
    document.getElementById('obj2').style.display="block";
    document.getElementById('obj2').style.top=yy+"px";
    document.getElementById('obj2').style.left=xx+"px";
    document.getElementById('obj2').innerHTML = aa;
  }
}

function openSelect(w,h,idName,idValue,urlAj,corrX,corrY,multi,any){
	this.win = document.getElementById('obj2');
	selIds = "";
	if(!multi){
		document.onmouseup=function(){
			document.getElementById('select').style.display='none';
			document.onmouseup='';
		}
	}else {
		selIds = document.getElementById(idValue).value;
	}
	
	xx = getPosX(document.getElementById(idName))+corrX;
	yy = getPosY(document.getElementById(idName))+corrY;
	win.style.width = w+"px";
	win.style.height = h+"px";
	win.style.overflow = "auto";
	//viewMex("Caricamento in corso...");
	ajaxGetRand(urlAj+'&idName='+idName+'&idValue='+escape(idValue)+'&multi='+multi+'&any='+any+'&selIds='+selIds,myHandler);
  function myHandler(aa){
    //hideMex(aa,0);
    win.style.top=yy+"px";
    win.style.left=xx+"px";
    win.innerHTML = aa;
    win.style.display="block";
     //Drag.init(document.getElementById("handle"),document.getElementById('win'));
  }
}

function insertUser(nome,id){
	document.getElementById('userDoc').value = nome;
	document.getElementById('idUserDoc').value = id;
	document.getElementById('obj2').style.display = "none";
}

function delFile(id){
	if(confirm("conferma eliminazione file")){
		location.href="?cmd=exDelFile&id="+id;
	}else{
		return false;
	}
}

function openAddUt(){
	xx = getPosX(document.getElementById('pippo'));
	yy = getPosY(document.getElementById('pippo')) + 15;
	ajaxGetRand('?cmd=addUt',myHandler);
  function myHandler(aa){
    document.getElementById('carica').style.visibility='hidden';
		document.getElementById('obj').style.width="400px";
    document.getElementById('obj').style.display="block";
    document.getElementById('obj').style.top=yy+"px";
    document.getElementById('obj').style.left=xx+"px";
    document.getElementById('obj').innerHTML = aa;
  }
}

function controllaUt(){	document.getElementById('nome').style.border = "1px solid #ccc";
  document.getElementById('cognome').style.border = "1px solid #ccc";	document.getElementById('azienda').style.border = "1px solid #ccc";
	document.getElementById('paese').style.border = "1px solid #ccc";
	document.getElementById('email').style.border = "1px solid #ccc";
	document.getElementById('settore').style.border = "1px solid #ccc";
	document.getElementById('user').style.border = "1px solid #ccc";
	document.getElementById('password').style.border = "1px solid #ccc";
	
  if(document.getElementById('nome').value == false){
    alert("NOME: campo mancante");
    document.getElementById('nome').focus();
    document.getElementById('nome').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('cognome').value == false){
    alert("COGNOME: campo mancante");
    document.getElementById('cognome').focus();
    document.getElementById('cognome').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('azienda').value == false){
    alert("AZIENDA: campo mancante");
    document.getElementById('azienda').focus();
    document.getElementById('azienda').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('settore').value == ""){
    alert("SETTORE: campo mancante");
    document.getElementById('settore').focus();
    document.getElementById('settore').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('paese').value == false){
    alert("PAESE: campo mancante");
    document.getElementById('paese').focus();
    document.getElementById('paese').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('email').value == false){
    alert("EMAIL: campo mancante");
    document.getElementById('email').focus();
    document.getElementById('email').style.border = "1px solid #ff6600";
    return false;
  }else if(!indirizzoEmailValido(document.getElementById('email').value)){ 
		alert("Indirizzo Email non valido"); 
		document.getElementById('email').focus();
    document.getElementById('email').style.border = "1px solid #ff6600";
    return false;
	}else if(document.getElementById('user').value == false){
    alert("USER: campo mancante");
    document.getElementById('user').focus();
    document.getElementById('user').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('livello').value == ""){
    alert("LIVELLO: campo mancante");
    document.getElementById('livello').focus();
    document.getElementById('livello').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('password').value == false){
    alert("PASSWORD: campo mancante");
    document.getElementById('password').focus();
    document.getElementById('password').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('password').value != document.getElementById('password2').value){
    alert("Le Password non coincidono");
    document.getElementById('password').focus();
		document.getElementById('password2').value = "";
    document.getElementById('password').style.border = "1px solid #ff6600";
    return false;
  }else{
		document.getElementById('addUt').submit();
  }}

function delUser(id){
	if(confirm("conferma eliminazione utente")){
		location.href="?cmd=exDelUser&id="+id;
	}else{
		return false;
	}
}

function openAddNews(){
	xx = getPosX(document.getElementById('pippo'));
	yy = getPosY(document.getElementById('pippo')) + 15;
	//ajaxGetRand('?cmd=addNews',myHandler);
  //function myHandler(aa){
    //document.getElementById('carica').style.visibility='hidden';
		document.getElementById('obj3').style.width="400px";
    document.getElementById('obj3').style.display="block";
    document.getElementById('obj3').style.top=yy+"px";
    document.getElementById('obj3').style.left=xx+"px";
    //document.getElementById('obj').innerHTML = aa;
  //}
}

function controllaNews(){	document.getElementById('titolo').style.border = "1px solid #ccc";
  document.getElementById('corpoIt').style.border = "1px solid #ccc";
	document.getElementById('corpoEn').style.border = "1px solid #ccc";	document.getElementById('tipo').style.border = "1px solid #ccc";
	document.getElementById('riservato').style.border = "1px solid #ccc";
  if(document.getElementById('titolo').value == false){
    alert("TITOLO: campo mancante");
    document.getElementById('titolo').focus();
    document.getElementById('titolo').style.border = "1px solid #ff6600";
    return false;
  }/*else if(document.getElementsByName('corpo').value == false){
    alert("CORPO: campo mancante");
    document.getElementById('corpo').focus();
    document.getElementById('corpo').style.border = "1px solid #ff6600";
    return false;
  }*/else if(document.getElementById('tipo').value == ""){
    alert("TIPO: campo mancante");
    document.getElementById('tipo').focus();
    document.getElementById('tipo').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('riservato').value == ""){
    alert("RISERVATO: campo mancante");
    document.getElementById('riservato').focus();
    document.getElementById('riservato').style.border = "1px solid #ff6600";
    return false;
  }else{
		document.getElementById('addNews').submit();
  }}

function delNews(id){
	if(confirm("conferma eliminazione news")){
		location.href="?cmd=exDelNews&id="+id;
	}else{
		return false;
	}
}

function delEvento(id){
	if(confirm("conferma eliminazione evento")){
		location.href="?cmd=exDelEvento&id="+id;
	}else{
		return false;
	}
}

function controllaNewsLetter(){	document.getElementById('oggetto').style.border = "1px solid #ccc";
  document.getElementById('corpo').style.border = "1px solid #ccc";	document.getElementById('settore').style.border = "1px solid #ccc";
  if(document.getElementById('oggetto').value == false){
    alert("OGGETTO: campo mancante");
    document.getElementById('oggetto').focus();
    document.getElementById('oggetto').style.border = "1px solid #ff6600";
    return false;
  }/*else if(document.getElementById('corpo').value == false){
    alert("CORPO: campo mancante");
    document.getElementById('corpo').focus();
    document.getElementById('corpo').style.border = "1px solid #ff6600";
    return false;
  }*/else if(document.getElementById('settore').value == ""){
    alert("SETTORE: campo mancante");
    document.getElementById('settore').focus();
    document.getElementById('settore').style.border = "1px solid #ff6600";
    return false;
  }else{
		document.getElementById('addNewsLetter').submit();
  }}

function controllaModPass(){	document.getElementById('pass').style.border = "1px solid #ccc";
  document.getElementById('pass2').style.border = "1px solid #ccc";
  if(document.getElementById('pass').value == false){
    alert("PASSWORD: campo mancante");
    document.getElementById('pass').focus();
    document.getElementById('pass').style.border = "1px solid #ff6600";
    return false;
  }/*else if(document.getElementsByName('corpo').value == false){
    alert("CORPO: campo mancante");
    document.getElementById('corpo').focus();
    document.getElementById('corpo').style.border = "1px solid #ff6600";
    return false;
  }*/else if(document.getElementById('pass').value != document.getElementById('pass2').value){
    alert("LE PASSWORD NON COINCIDONO: campo mancante");
    document.getElementById('pass').focus();
    document.getElementById('pass').style.border = "1px solid #ff6600";
		document.getElementById('pass2').value = "";
    return false;
  }else{
		document.getElementById('modPass').submit();
  }}

function modRis(val,id){
	ajaxGetRand('?cmd=modRis&val='+val+'&id='+id,myHandler);
  function myHandler(aa){
    if(aa=="ok"){
			alert("Modifica Effettuata");
		}	
  }
}

function controllaUtPub(){	document.getElementById('nome').style.border = "1px solid #ccc";
  document.getElementById('cognome').style.border = "1px solid #ccc";	document.getElementById('azienda').style.border = "1px solid #ccc";
	document.getElementById('paese').style.border = "1px solid #ccc";
	document.getElementById('email').style.border = "1px solid #ccc";
	document.getElementById('userR').style.border = "1px solid #ccc";
	document.getElementById('password').style.border = "1px solid #ccc";
	
  if(document.getElementById('nome').value == false){
    alert("NOME: campo mancante");
    document.getElementById('nome').focus();
    document.getElementById('nome').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('cognome').value == false){
    alert("COGNOME: campo mancante");
    document.getElementById('cognome').focus();
    document.getElementById('cognome').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('azienda').value == false){
    alert("AZIENDA: campo mancante");
    document.getElementById('azienda').focus();
    document.getElementById('azienda').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('paese').value == false){
    alert("PAESE: campo mancante");
    document.getElementById('paese').focus();
    document.getElementById('paese').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('email').value == false){
    alert("EMAIL: campo mancante");
    document.getElementById('email').focus();
    document.getElementById('email').style.border = "1px solid #ff6600";
    return false;
  }else if(!indirizzoEmailValido(document.getElementById('email').value)){ 
		alert("Indirizzo Email non valido"); 
		document.getElementById('email').focus();
    document.getElementById('email').style.border = "1px solid #ff6600";
    return false;
	}else if(document.getElementById('userR').value == false){
    alert("USER: campo mancante");
    document.getElementById('userR').focus();
    document.getElementById('userR').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('password').value == false){
    alert("PASSWORD: campo mancante");
    document.getElementById('password').focus();
    document.getElementById('password').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('password').value != document.getElementById('password2').value){
    alert("Le Password non coincidono");
    document.getElementById('password').focus();
		document.getElementById('password2').value = "";
    document.getElementById('password').style.border = "1px solid #ff6600";
    return false;
  }else{
		document.getElementById('regUt').submit();
  }}

function controllaUpUt(){	document.getElementById('nome').style.border = "1px solid #ccc";
  document.getElementById('cognome').style.border = "1px solid #ccc";	document.getElementById('azienda').style.border = "1px solid #ccc";
	document.getElementById('paese').style.border = "1px solid #ccc";
	document.getElementById('email').style.border = "1px solid #ccc";
	document.getElementById('settore').style.border = "1px solid #ccc";
	document.getElementById('user').style.border = "1px solid #ccc";
	
  if(document.getElementById('nome').value == false){
    alert("NOME: campo mancante");
    document.getElementById('nome').focus();
    document.getElementById('nome').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('cognome').value == false){
    alert("COGNOME: campo mancante");
    document.getElementById('cognome').focus();
    document.getElementById('cognome').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('azienda').value == false){
    alert("AZIENDA: campo mancante");
    document.getElementById('azienda').focus();
    document.getElementById('azienda').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('settore').value == ""){
    alert("SETTORE: campo mancante");
    document.getElementById('settore').focus();
    document.getElementById('settore').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('paese').value == false){
    alert("PAESE: campo mancante");
    document.getElementById('paese').focus();
    document.getElementById('paese').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('email').value == false){
    alert("EMAIL: campo mancante");
    document.getElementById('email').focus();
    document.getElementById('email').style.border = "1px solid #ff6600";
    return false;
  }else if(!indirizzoEmailValido(document.getElementById('email').value)){ 
		alert("Indirizzo Email non valido"); 
		document.getElementById('email').focus();
    document.getElementById('email').style.border = "1px solid #ff6600";
    return false;
	}else if(document.getElementById('user').value == false){
    alert("USER: campo mancante");
    document.getElementById('user').focus();
    document.getElementById('user').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('livello').value == ""){
    alert("LIVELLO: campo mancante");
    document.getElementById('livello').focus();
    document.getElementById('livello').style.border = "1px solid #ff6600";
    return false;
  }else{
		document.getElementById('addUpUt').submit();
  }}

function modNomeFile(id){
	val = document.getElementById("in"+id).value;
	ajaxGetRand('?cmd=modNomeFile&val='+val+'&id='+id,myHandler);
  function myHandler(aa){
    if(aa=="ok"){
			alert("Modifica Effettuata");
		}	
  }
}

function modNews(){
	document.getElementById('titolo').style.border = "1px solid #ccc";
  document.getElementById('corpoIt').style.border = "1px solid #ccc";
	document.getElementById('corpoEn').style.border = "1px solid #ccc";
  if(document.getElementById('titolo').value == false){
    alert("TITOLO: campo mancante");
    document.getElementById('titolo').focus();
    document.getElementById('titolo').style.border = "1px solid #ff6600";
    return false;
  }/*else if(document.getElementsByName('corpo').value == false){
    alert("CORPO: campo mancante");
    document.getElementById('corpo').focus();
    document.getElementById('corpo').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('tipo').value == ""){
    alert("TIPO: campo mancante");
    document.getElementById('tipo').focus();
    document.getElementById('tipo').style.border = "1px solid #ff6600";
    return false;
  }else if(document.getElementById('riservato').value == ""){
    alert("RISERVATO: campo mancante");
    document.getElementById('riservato').focus();
    document.getElementById('riservato').style.border = "1px solid #ff6600";
    return false;
  }*/else{
		document.getElementById('addNews').submit();
  }
}

function selCal(anno,mese,campo){
        ajaxGetRand('?cmd=selCal&mese='+mese+'&anno='+anno+'&campo='+campo,myHandler);
        function myHandler(aa){
                win = document.getElementById("select");
                xx = getPosX(document.getElementById(campo));
                yy = getPosY(document.getElementById(campo))+21;
                win.style.width = "";
                win.style.height = "";
                win.style.left = xx+"px";
                win.style.top = yy+"px";
                win.innerHTML = aa;
                win.style.display = "block";
        }
}