function in_array(string, array){
	for (i = 0; i < array.length; i++){
		if(array[i] == string){
			return true;
		}
	}
	return false;
}

function isInteger (s){
      var i;

      if (isEmpty(s))
      if (isInteger.arguments.length == 1) return 0;
      else return (isInteger.arguments[1] == true);

      for (i = 0; i < s.length; i++)
      {
         var c = s.charAt(i);

         if (!isDigit(c)) return false;
      }

      return true;
}

function isEmpty(s){
      return ((s == null) || (s.length == 0))
}

function isDigit (c){
      return ((c >= "0") && (c <= "9"))
}


function ck_string(str,regexp) {
    var patternvalido = regexp;
    var reg = new RegExp(patternvalido);
    return str.match(reg);
}

// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var debugMode = true;
// hide loading data

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
	// will store the reference to the XMLHttpRequest object
	var xmlHttp;
	// this should work for all browsers except IE6 and older
	try{
		// try to create XMLHttpRequest object
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		// assume IE6 or older
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                        "MSXML2.XMLHTTP.5.0",
                                        "MSXML2.XMLHTTP.4.0",
                                        "MSXML2.XMLHTTP.3.0",
                                        "MSXML2.XMLHTTP",
                                        "Microsoft.XMLHTTP");
		// try every prog id until one works
		for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
			try { 
				// try to create XMLHttpRequest object
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			} 
			catch (e) {
			}
		}
	}
	// return the created object or display an error message
	if (!xmlHttp){
		//	var ld = document.getElementById("loadingdata");
		//	ld.style.display="";
		alert("Error creating the XMLHttpRequest object.");
	}
	else {
		return xmlHttp;
	}
}

// function called when the state of the HTTP request changes
function handleRequestStateChange() {
	// when readyState is 4, we are ready to read the server response
	if (xmlHttp.readyState == 4) {
		// continue only if HTTP status is "OK"
		if (xmlHttp.status == 200) {
			try{
				// do something with the response from the server
				handleServerResponse();
			}
			catch(e){
				// display error message
				//alert("Error reading the response: " + e.toString());
				alert("rg58 - Errore nel leggere la risposta! Può causare questo errore: " + e.toString());
				// 	var ld = document.getElementById("loadingdata");
				//	ld.style.display="";
			}
		} 
		else{
			// display status message
			//alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
			alert("rg66 - C'è stato un problema nella ricezione dei dati:\n" + xmlHttp.statusText);
			//	var ld = document.getElementById("loadingdata");
			//	ld.style.display="";
		}
	}
}

function handleServerResponse(){
	// retrieve the server's response packaged as an XML DOM object
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("rg77 - Struttura dati non valida:\n" + xmlHttp.responseText);
 
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") 
		throw("rg82 - Struttura dati non valida:\n" + xmlHttp.responseText);
	// getting the root element (the document element)
	xmlRoot = xmlResponse.documentElement;

//	numero = xmlRoot.firstChild.childNodes.length;
	numero = xmlRoot.childNodes.length;
	
	for(var xnum=0; xnum < numero; xnum++){
//		alert(xmlRoot.childNodes[xnum].firstChild.nodeValue);
//		alert(xmlRoot.childNodes[xnum].getAttribute("targ"));
		// display the user message
		myDiv = document.getElementById(xmlRoot.childNodes[xnum].getAttribute("targ"));
		myDiv.innerHTML = xmlRoot.childNodes[xnum].firstChild.nodeValue;
	}

/*
    
	var testoX = xmlRoot.firstChild.firstChild.nodeValue;
	var targ = xmlRoot.firstChild.getAttribute("targ");
 
	responseText = testoX;

	// display the user message
	myDiv = document.getElementById(targ);
	myDiv.innerHTML = responseText;

*/


}

/*
function handleServerResponse(){
	// retrieve the server's response packaged as an XML DOM object
	var xmlResponse = xmlHttp.responseXML;
	// catching potential errors with IE and Opera
	if (!xmlResponse || !xmlResponse.documentElement)
		throw("rg77 - Struttura dati non valida:\n" + xmlHttp.responseText);
 
	// catching potential errors with Firefox
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if (rootNodeName == "parsererror") 
		throw("rg82 - Struttura dati non valida:\n" + xmlHttp.responseText);
	// getting the root element (the document element)
	xmlRoot = xmlResponse.documentElement;
    
	var testoX = xmlRoot.firstChild.firstChild.nodeValue;
	var targ = xmlRoot.firstChild.getAttribute("targ");
 
	responseText = testoX;

	// display the user message
	myDiv = document.getElementById(targ);
	myDiv.innerHTML = responseText;

}
*/

function display_message_on_div(div,msg){
	document.getElementById(div).innerHTML=msg;
}


function start_control(divfield,tipo,urlpass){
	if (xmlHttp){
		// try to connect to the server
		try {
//			display_message_on_div("checknickresponse",msg04);

			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", urlpass, true);
			// xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e){
			alert("Non è stato possibile connettersi alla sorgente dati. Non cliccare il bottone ripetutamente:\n" + e.toString());
			//	var ld = document.getElementById("loadingdata");
			//	ld.style.display="";
		}
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
	 return true					
}

function managebutton(val){
	if(!val){
		document.form.bottone.disabled=true;
	}
	else{
		document.form.bottone.disabled=false;
	}
}

function checkform(){
	provincia_id = document.form.provincia_id.options[document.form.provincia_id.selectedIndex].value;

	if(isEmpty(document.form.nome.value) || isEmpty(document.form.cognome.value) || isEmpty(document.form.email.value)){
		msg="<p><strong>Attenzione:</strong> I campi nome, cognome e indirizzo email sono obbligatori. Completali per favore.</p>";
		display_message_on_div("validazione-msg",msg);
//		document.form.nome.focus();
		return false;
	}
	else if(provincia_id == "NULL"){
		msg="<p><strong>Attenzione:</strong> Devi selezionare la provincia.</p>";
		display_message_on_div("validazione-msg",msg);
//		document.form.provincia_id.focus();
		return false;
	}
	else if(!echeck(document.form.email.value)){
		msg="<p><strong>Attenzione:</strong> L'indirizzo email inserito non e' corretto. Controlla per favore.</p>";
		display_message_on_div("validazione-msg",msg);
//		document.form.email.focus();
		return false;
	}
	else if(isEmpty(document.form.codice.value)){
		msg="<p><strong>Attenzione:</strong> Devi inserire il codice antispam.</p>";
		display_message_on_div("validazione-msg",msg);
//		document.form.nome.focus();
		return false;
	}
	else{
		document.form.submit();
		/*
		codice = document.form.codice.value;
		email = document.form.email.value;
		codice_petizione = document.form.codice_petizione.value;
		check_antispam(codice,email,codice_petizione);
		*/

	}
}

/*
function check_antispam(codice,email,codice_petizione){
	var urlpass = "/ajax.php?do=check_antispam&codice=" + codice + "&email=" + email + "&codice_petizione=" + codice_petizione;
			
	if (xmlHttp){
		// try to connect to the server
		try {
			// display_message_on_div("box_wishlist",loading_msg);
			
			// initiate the asynchronous HTTP request
			xmlHttp.open("GET", urlpass, true);
			// xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.onreadystatechange = handleRequestStateChange;
			xmlHttp.send(null);
		}
		// display the error in case of failure
		catch (e){
			msg="<p><strong>Attenzione!</strong> Si e' verificato un errore nella connessione al server. Riprova tra qualche minuto.</p>";
			display_message_on_div("validazione-msg",msg);
		}
	}

}
*/
function reimposta(){
	msg1="<p><input type=\"button\" name=\"bottone\" value=\"Verifica i dati inseriti\" id=\"bottone\" onclick=\"return checkform()\"/></p>";
	msg2=" ";
	display_message_on_div("validazione-btn",msg1);
	display_message_on_div("validazione-msg",msg2);
}

