function resetBorder(obj) {
	for(var n=0; n<obj.elements.length; n++){
	  if(obj.elements[n].name!=""){
    	obj.elements[n].style.border = "1px solid #999";
  		//obj.elements[n].style.borderBottom = "1px solid #AACD82";
  		//obj.elements[n].style.borderRight = "1px solid #AACD82";
	  }
	}
}

function normalizeCampo(obj){
  obj.style.border = "1px solid #999";
  //obj.style.borderBottom = "1px solid #AACD82";
  //obj.style.borderRight = "1px solid #AACD82";
}

function buildUri(obj){
  var strUri = "";
  for(var n=0; n<obj.elements.length; n++){
    if(obj.elements[n].name!=""){
			strUri += "&"+obj.elements[n].name+"="+escape(obj.elements[n].value);
    }
  }
  return strUri;
}

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 controlField(nome,id,id2,tipo){
	color = "darkred";
  switch(tipo){
    case "_":       // controlla se il campo e' vuoto
    if(document.getElementById(id).value == false){
    	alert(nome+": campo mancante");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+color;
    	return false;
    }else{
    	return true;
    }
    break;
    case "__":      // controlla se il campo e' vuoto con ""
    if(document.getElementById(id).value == ""){
    	alert(nome+": campo mancante");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+color;
    	return false;
    }else{
      return true;
    }
    break;
    case "@":       // controlla la validita' del campo mail
    if(!indirizzoEmailValido(document.getElementById(id).value)){
      alert(nome+": campo non valido");
      document.getElementById(id).style.border = "1px solid "+color;
      document.getElementById(id).focus();
      return false;
    }else{
      return true;
    }
    break;
    case "=":       // confronta due campi
    if(document.getElementById(id).value != document.getElementById(id2).value){
      alert("Le Password non Coincidono");
      document.getElementById(id).focus();
      document.getElementById(id).style.border = "1px solid "+color;
      document.getElementById(id2).value = "";
      return false;
    }else{
      return true;
    }
    break;
    case "h":       // controlla se il campo e' vuoto
    if(document.getElementById(id2).value == false){
    	alert(nome+": campo mancante");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+color;
    	return false;
    }else{
    	return true;
    }
    break;
    case "c":       // controlla se il campo pi lungo di un tot
    if(document.getElementById(id).value.length > id2){
    	alert(nome+": il campo deve contenere massimo "+id2+" caratteri");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+color;
    	return false;
    }else{
    	return true;
    }
    break;
    case "c-":       // controlla se il campo pi corto di un tot
    if(document.getElementById(id).value.length < id2){
    	alert(nome+": il campo deve contenere minimo "+id2+" caratteri");
    	document.getElementById(id).focus();
    	document.getElementById(id).style.border = "1px solid "+color;
    	return false;
    }else{
    	return true;
    }
    break;
    case "n":       // controlla se il campo  e' numerico
	   if(isNaN(document.getElementById(id).value)){
	       alert(nome+": il campo deve contenere solo numeri");
	       document.getElementById(id).focus();
	       document.getElementById(id).style.border = "1px solid "+color;
	       return false;
	   }else{
	       return true;
	   }
	   break;
    default:
    alert("tipo non riconosciuto");
    return false;
  }
}

function viewMex(mex){
	if(document.body){
		dTop = document.body.scrollTop+420;
	}else{
		dTop = document.documentElement.scrollTop+420;
	}
	document.getElementById('loader').innerHTML = "<img src=\"img/loading.gif\" style=\"vertical-align: -18px\">&nbsp;&nbsp;"+mex;
	document.getElementById('loader').style.top = dTop+"px";
	document.getElementById('loader').style.display = "block";
}

function hideMex(cont,dbg){
	if(dbg){
		document.getElementById('loader').innerHTML = cont;
	}else{
		document.getElementById('loader').style.display = "none";
		document.getElementById('loader').innerHTML = "";
	}	
}

function apriSottoMenu(obj){
	while(obj.nodeName!="DIV"){
		obj = obj.nextSibling;
		if(obj.nodeName == "DIV"){
			if(obj.style.display=="none"){
				obj.style.display = "block";
			}else{
				obj.style.display = "none";
			}
			break;
		}
	}
}

var iInt = null;
var sW = 0;
//var sH = 0;
var eW = 200;
//var eH = 200;
var active = false;
var currObj = null;

function mostraFotoOld(obj){
       ammazzaAnim();
       currObj = obj.parentNode.nextSibling.firstChild;
       obj.parentNode.nextSibling.style.display = "block";
       iInt = setInterval('allarga(currObj)', 20);
}

function mostraFoto(obj,url){
	ammazzaAnim();
	currObj = document.getElementById('win');
	//obj.parentNode.nextSibling.style.display = "block";
	xx = getPosX(obj)-3;
	yy = getPosY(obj)-3;
	currObj.innerHTML = "<img src='"+url+"' style='width: 82px'>";
	//currObj.style.width = 82+"px";
	//currObj.style.height = h+"px";
	//currObj.style.overflow = "auto";
	  
	currObj.style.top=yy+"px";
	currObj.style.left=xx+"px";
	currObj.style.display="block";
	   
	   
	   
	iInt = setInterval('allarga(currObj)', 20);
}


function allarga(obj){
	//aa = aa + (505-aa)/3;
	sW += 10;
	//sH += 10;
	//obj.style.height = sH+"px";
	obj.style.width = sW+"px";
	obj.firstChild.style.width = sW+"px";
	/*
	xx = getPosX(obj) - 5;
	yy = getPosY(obj)  - 5;
	currObj.style.top=yy+"px";
	currObj.style.left=xx+"px";
	*/
	if(sW>=eW){// || sH>=eH){
		clearInterval(iInt);
	}
}

function ammazzaAnim(){
       if(iInt) clearInterval(iInt);
       iInt = 0;
       sW = 100;
       //sH = 100;
       if(currObj){
               //currObj.style.height = sH+"px";
               currObj.firstChild.style.width = sW+"px";
               currObj.style.width = sW+"px";
               //currObj.parentNode.style.display = "none";
               currObj.style.display = "none";
       }
}

function setControl(val, obj){
  var parentObj = obj.parentNode.childNodes;
    for(i=0; i<parentObj.length; i++){
    	if(parentObj[i].nodeName == "A") parentObj[i].style.fontWeight = "normal";
	    if(parentObj[i].nodeName == "INPUT") parentObj[i].value = val;
    }
    obj.style.fontWeight = "bolder";
}

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

function inviaContact(obj){
  resetBorder(obj); 
	flag = true;
  flag = flag && controlField('NOME','nome','','_');
  flag = flag && controlField('COGNOME','cognome','','_');
	flag = flag && controlField('E-MAIL','email','','_');
	flag = flag && controlField('E-MAIL','email','','@');
	flag = flag && controlField('CITTA\'','citta','','_');
	flag = flag && controlField("TRATTAMENTO DEI DATI PERSONALI",'concedo','','_');
	if(flag){
  	strUri = buildUri(obj);
  	//alert(strUri);
    viewMex("Salvataggio in corso...");
    myajax.post("?cmd=inviaContact",buildUri(obj));
  }
  myajax.echoFunction=function(response){
    hideMex(response,0);
    if(response=="ok") alert("Invio effettuato");
  }
}

function inviaContactP(obj){
  resetBorder(obj); 
	flag = true;
  flag = flag && controlField('NOME','nome','','_');
  flag = flag && controlField('COGNOME','cognome','','_');
	flag = flag && controlField('E-MAIL','email','','_');
	flag = flag && controlField('E-MAIL','email','','@');
	flag = flag && controlField('CITTA\'','citta','','_');
	flag = flag && controlField("TRATTAMENTO DEI DATI PERSONALI",'concedo','','_');
	if(flag){
  	//strUri = buildUri(obj);
  	//alert(strUri);
    viewMex("Salvataggio in corso...");
    document.getElementById('inviaContactPiu').submit();
  }
}
