function openPrivacy(){
	window.open('privacy.cfm','priv','width=300,height=300,scrollbars=1');
	}
function charCalc(){
	currLen = document.getElementById('testo').value.length;
	rim = 160 - currLen;
	document.getElementById('rChar').innerHTML = rim;
}


//================== invia la mail ==================
function sendMail(a){
	if(a == "sub"){
		if(!document.getElementById('privacy').checked){
			alert("Devi dichiarare di aver letto la privacy");
			return false;
		}
	}
	if(ver_form(1,'email','formNewsletter')){
		if(chkEmail(document.getElementById('email').value)){
			document.getElementById('ty').value = a;
			sendForm(1,'email','resMail','formNewsletter','sMail')
		}
		else
		{
			return false;
		}
		
	}
	else
	{
		return false;
	}	
	
}

//================== invia Il commento ==================
function sendComm(a){
	if(!document.getElementById('privacyComm').checked){
		alert("Devi dichiarare di aver letto la privacy");
		return false;
	}
	if(document.getElementById('cpck').value != a){
		alert("Il testo del captcha non è corretto");
		return false;		
	}
	if(ver_form(2,'nick,emailComm','formComm')){
		if(chkEmail(document.getElementById('emailComm').value)){
			sendForm(2,'nick, emailComm','resComm','formComm','sComm')
		}
		else
		{
			return false;
		}
		
	}
	else
	{
		return false;
	}	
	
}


//=================== controllo email =================================
function chkEmail(a){		
	EmailAddr = a;
	Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (!Filtro.test(EmailAddr)){
		alert("Controlla l'indirizzo di e-mail inserito");
		return false;
	}else{
		return true;
	}
}


//===========================      collapse DIV       =====================
//=======================================================================================

function collThis(idDiv){
	myDivEl = document.getElementById(idDiv);
	if(myDivEl.style.display=="none"){
		setDisplayBlock(idDiv);
	}else {
		//myDivEl.style.display='none';
		setDisplayNone(idDiv);
	}
}

//mi forzo il div dei commenti a display:none
function setDisplayNone(a){
myDivEl = document.getElementById(a);
myDivEl.style.display='none';
}
function setDisplayBlock(a){
myDivEl = document.getElementById(a);
myDivEl.style.display='block';
}
	
//========================================================
//VAlidazione stringhe
//========================================================

function validateIt(a,b){
	regExpress = a;
	stringa = b;
	if(!regExpress.test(stringa)){
		return false;  
	}else{
		return true;
	}
}

//======funzione generica di controllo campi obbligatori =======================
//======a=N. campi obbligatori, b=array con nome campi, c= nome form =========================
function ver_form(a,b,c){	
	elementi = b.split(",");
	control = 0;
	for (var i = 0; i < a; i++) {			
		stringa='document.'+c+'.'+elementi[i]+'.value';
		nome='document.'+c+'.'+elementi[i]+'.name';
		if (eval(stringa)==''){
			alert("Attenzione, compilare i campi obbligatori");
			control=1;
			return false;
		}
	}
	return true;
}

//=======================================================================================
//================================FUNZIONI AJAX =========================================
//=======================================================================================

//============================ PAGINE E FORM =========================================
var xmlHttp;
function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
//a = pagina, b = GET-POST  c=queryString d= div(non obbligatorio)
function startRequest(a,b,c,d) {
	createXMLHttpRequest();
	if(!d){
		myDiv = "pagina";	
	}else{
		myDiv = d;	
	}
	document.getElementById(myDiv).innerHTML ="<div>attendere...</div>";
	mya = a;
    xmlHttp.onreadystatechange = handleStateChange;
	var queryStr = null;
	
	//imposto  l'invio del form se la richiesta arriva come post
	if(b == 'POST'){
		var p_name = "./inc/" + a + ".cfm?timeS=" + new Date().getTime();
		xmlHttp.open(b, p_name, true);
		if(c){			
			queryStr = c;
		}
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.send(queryStr);
	}else{
	//imposto la chiamata alla pagina se arriva come GET
		var p_name = "./inc/" + a + ".cfm?timeS=" + new Date().getTime();
		if(c){			
			var p_name = p_name+"&"+c;
		}
		//alert(c);
		xmlHttp.open(b, p_name, true);
		xmlHttp.send(null);
	}  
}    
function handleStateChange() {
	/*visualizaz div*/ //alert(myDiv);
    if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			document.getElementById(myDiv).innerHTML = xmlHttp.responseText;
			afterResp(mya,myDiv);
		}		
	}
}

//=======================================================================================
//======================Funzioni da chiamare dopo il caricamento della pagina============
//=======================================================================================
function afterResp(myPage,myDiv){
	switch (myPage){
		case "sMail":
		document.getElementById('email').value = "";
		document.getElementById('privacy').checked = false;
		break;
		case "sComm":
		document.getElementById('emailComm').value = "";
		document.getElementById('privacyComm').checked = false;
		document.getElementById('nick').value = "";
		document.getElementById('msg').value = "";
		currId = document.getElementById("IdPost").value;
		qstr = "IdP="+currId;
		startRequest('comm','GET',qstr,'cont_comm');
		break;
	}
}


//=======================================================================================
//=====================================Invio link =======================================
//=======================================================================================
function sendLink(a,b,c,d){	
	startRequest(a,b,c,d);
}


//=======================================================================================
//invio dati FORM	a=nelementi obbligatori; b= nomi elementi obbligatori; c= div target, d= nome form, e= pagina //=======================================================================================
function sendForm(a,b,c,d,e){
	if(ver_form(a,b,d)){
		//alert("vai");
		if(document.getElementById("testoID")){
			mceTesto = document.getElementById("testoID").value;
			//alert(mceTesto);
		}
		formName ='document.'+d+'.elements.length' ;
		Nelementi =eval(formName);
		//alert(Nelementi);
		//alert("ok");
		qryStrn = "";
		for (var i = 0; i < Nelementi; i++) {
			tipo = 'document.'+d+'.elements['+i+'].type';
			nome = 'document.'+d+'.elements['+i+'].name';
			if(eval(tipo)=='radio' || eval(tipo)=='checkbox'){
				chk = 'document.'+d+'.elements['+i+'].checked';
				
				if(eval(chk)){
					valore =  'document.'+d+'.elements['+i+'].value';
					nome = eval(nome);
					valore = encodeURIComponent(eval(valore));	
					qryStrn = qryStrn+nome+"="+valore+"&";
				}
			}else{
				valore =  'document.'+d+'.elements['+i+'].value';
				nome = eval(nome);
				valore = encodeURIComponent(eval(valore));	
				qryStrn = qryStrn+nome+"="+valore+"&";
			}
		}
		//alert(qryStrn);
		startRequest(e,'POST',qryStrn,c);
	}
}

