// JavaScript Document
function showHideCommento() {
	var myCommentSpan = document.getElementById("nuovo_commento");
	if(myCommentSpan.style.display == 'block')	myCommentSpan.style.display = "none";
	else myCommentSpan.style.display = 'block';
}



function testCommentForm() {
	var errors = '';
	var j = 0;
	var myForm = document.forms['nuovo_commento_form'];
	
	//alert("testform");
	
	var mandatoryFileds = new Array(0,1);
	var fieldsDesc = new Array("Email","Nome del mittente");
	
	for (var i = 0; i < mandatoryFileds.length; i++) {
		j = mandatoryFileds[i];
		if (myForm.elements[j].value=='') {
			errors += 'Il campo ' + fieldsDesc[i] + ' è obbligatorio.\n';
		}
	}
		
	var arr  = new Array();
	var arr2 = new Array();
	var valid = true;
	var s = myForm.elements[0].value;
	if (s.indexOf("@") != -1) {
		arr = s.split("@");
		if (arr[0].length < 1)
			valid = false;
		else {
			if (arr[1].indexOf(".") != -1) {
				arr2 = arr[1].split(".");
				if (arr2[0].length < 2)
					valid = false;
			}
			else 
				valid = false;
		}
	}
	else 
		valid = false;

	if (valid != true) {
		errors += "Il campo Email deve contenere un indirizzo di posta elettronica valido\n";
	}
	
	if(errors != '') {
		alert('Errore nella compilazione del modulo:\n' + errors);
	}
	
	//alert(errors);
	
	if (errors == '') loadXMLSendNews();
	
	return false;
}



if (window.XMLHttpRequest) {
	var xmlhttp=new XMLHttpRequest();
} else if (window.ActiveXObject) {
	var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}


function loadXMLSendNews()
{
var myForm = document.forms['nuovo_commento_form'];
	var mySendText = 'void=1&';
	
	mySendText += 'idart=' + myForm.idart.value + '&';
	mySendText += 'titolo=' + myForm.titolo.value + '&';
	mySendText += 'commento=' + encodeURI(escape(myForm.commento.value)) + '&';
	mySendText += 'email=' + myForm.email.value + '&';
	mySendText += 'nome=' + myForm.nome.value;
	
	//alert(mySendText);
	if(xmlhttp) {
		xmlhttp.onreadystatechange=xmlhttpComm;
		xmlhttp.open('GET','xmlSendNews.php?' + mySendText,true);
		xmlhttp.send(null);
	} else {
		alert('errore nella creazione dell\'oggetto');
	}
}


function xmlhttpComm()
{
//alert('here I am: xmlhttpBannerimg');
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (xmlhttp.status==200)
    {
		//alert('here I am: xmlhttp.status==200');
    	var xmldoc = xmlhttp.responseText;
		var divOp = document.getElementById("nuovo_commento");
		var myTempHtml = '<strong>' + xmldoc + '</strong>';
		divOp.innerHTML = myTempHtml;
		divOp.style.display = 'block';
    } else {
		var divOp = document.getElementById("nuovo_commento");
		var myTempHtml = '<strong>Impossibile inviare i dati</strong>';
		divOp.innerHTML = myTempHtml;
		divOp.style.display = 'block';    
	}
  }
}
