function check(form, message) {
	if (form.value == "") {
		alert(message);
		form.focus();
		return false;
	} else { return true; }

}

var testresults;
function checkmail(form){
	var str=form.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		testresults=true;
	else{
		alert("Inserire un indirizzo email valido!");
		form.focus();
		testresults=false;
	}
	return (testresults);
}

function checkpassword(form) {
	var testresults = "";
    var rpass = form.value;
	var pass = document.reg.password.value;
    
	if  (rpass == pass) {
		//alert("pass controllata!" + pass);
		testresults=true;
	      } else {
		alert("la password ripetuta non è corretta!");
		form.focus();
		testresults=false;
	}
        return (testresults);
}

function checkprivacy(form) {
	if(document.reg.privacy[1].checked==true)
	{
		alert("Prima di procedere con la registrazione, devi 'Leggere ed Autorizzare' le condizioni di privacy.");
		document.reg.privacy[1].focus();
		return false;
	} else { return true; }	
}

function checkform() {
	stat = true;
	if (stat == true) stat = check(document.reg.nome, 'Campo nome obbligatorio!');
	if (stat == true) stat = check(document.reg.cognome, 'Campo cognome obbligatorio!');
	if (stat == true) stat = check(document.reg.societa, 'Campo società obbligatorio!');
	if (stat == true) stat = check(document.reg.citta, 'Campo città obbligatorio!');
	if (stat == true) stat = check(document.reg.cod_fisc_iva, 'Campo P.IVA o C.F obbligatorio!');
	if (stat == true) stat = checkmail(document.reg.email, 'Campo email obbligatorio!');
	if (stat == true) stat = check(document.reg.telefono, 'Campo telefono obbligatorio!');
	//if (stat == true) stat = check(document.reg.username, 'Campo username obbligatorio!');
	//if (stat == true) stat = checkpassword(document.reg.rpassword, 'Ripettere la password!');
	//if (stat == true) stat = check(document.reg.password, 'Campo password obbligatorio!');
	if (stat == true) stat = checkprivacy(document.reg.privacy);
	return stat;
}
//Sessione registrazione area riservata
function checkform2() {
	stat = true;
	if (stat == true) stat = check(document.reg.nome, 'Campo nome obbligatorio!');
	if (stat == true) stat = check(document.reg.cognome, 'Campo cognome obbligatorio!');
	//if (stat == true) stat = check(document.reg.societa, 'Campo società obbligatorio!');
	//if (stat == true) stat = check(document.reg.citta, 'Campo città obbligatorio!');
	//if (stat == true) stat = check(document.reg.cod_fisc_iva, 'Campo P.IVA o C.F obbligatorio!');
	if (stat == true) stat = checkmail(document.reg.email, 'Campo email obbligatorio!');
	//if (stat == true) stat = check(document.reg.telefono, 'Campo telefono obbligatorio!');
	//if (stat == true) stat = check(document.reg.username, 'Campo username obbligatorio!');
	//if (stat == true) stat = checkpassword(document.reg.rpassword, 'Ripettere la password!');
	//if (stat == true) stat = check(document.reg.password, 'Campo password obbligatorio!');
	if (stat == true) stat = checkprivacy(document.reg.privacy);
	return stat;
}

function http_function_focus(){
	if(document.reg.homepage.value==''){
		document.reg.homepage.value='http://';
	}
}
function http_function_blur(){
	if(document.reg.homepage.value=='http://'){
		document.reg.homepage.value='';
	}
}
function colorate(field){
	field.style.backgroundColor="#E9FEDA";
}
function decolorate(field){
	//field.style.backgroundColor="#EFEFEF";
	field.style.backgroundColor="";
}

function colorateErrore(field){
	field.style.backgroundColor="#D9F2FF";
}


function loading_image(divid){
var img ='<p align="left" class="testo"><img src="immagini/ajax-loader.gif" alt="Caricamento in corso...."/>Attendere....</p>';
	document.getElementById(divid).innerHTML = img;
}
function preloadimg(){
	pic1= new Image();
	pic1.src="../img/loading2.gif";
}

//************************************************************************
//FUNZIONE PER INVIARE CON IL METODO GET
//************************************************************************
function ajaxFunction(divId, pagina, variabile, valore, variabile2, valore2){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e){
// Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){

alert("Tuo browser non supporta AJAX!");
return false;
}
}
}

xmlHttp.onreadystatechange=function(){


	//0 uninitialized
	//1 loading
	//2 loaded
	//3 interactive
	//4 complete

if (xmlHttp.readyState==1) {
loading_image(divId);
alert('readystate=inloading');
}
if(xmlHttp.readyState==4){
if(xmlHttp.status  == 200){
document.getElementById(divId).innerHTML=xmlHttp.responseText;
}
else{
alert(xhr.status);
}
}
}
	var r = Math.random();
	var url = pagina + '?' +  variabile + '=' + valore + '&' +  variabile2 + '=' + valore2 ;
//alert(url);

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
//*********************FINE DEL GET***************************************



//************************************************************************
//FUNZIONE PER INVIARE DATI CON IL METODO POST FaiRequestLinksPOST
//************************************************************************
var http_request = false;

function FaiRequestLinksPOST(url, parameters) {
	
var id_div = "x4";
//id_div = passo id della div con la qualle visualizzera' risposta del server
//parameters = passo tutti i valori delle form, text, select combobox ecc...
//url = passa la pagina che elaborera le informazioni es. risultato.php


  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 http_request = new XMLHttpRequest();
	 if (http_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		http_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!http_request) {
	alert('Cannot create XMLHTTP instance');
	 return false;
  }

  http_request.onreadystatechange = alertContents;
	//alert(url);
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}


function alertContents() {

//0 uninitialized
//1 loading
//2 loaded
//3 interactive
//4 complete

		if (http_request.readyState==1) 
		{
		loading_image('x4');
		//alert('readystate=inloading');
		}
		
		if (http_request.readyState == 4) 
		{

	//alert(http_request.responseTexWEt);
	
	result = http_request.responseText;
	//alert(result);
	document.getElementById('x4').innerHTML = result;
	var ob = document.getElementsByTagName('x4');
	for(var i=0; i<ob.length-1; i++){
		if(ob[i+1].text!=null) eval(ob[i+1].text);
	}
  }
}

function RequestSessioneMarchio(Parametro) {
	
	//RequestSessioneLinks("generapagina.php?","");
	var r = Math.random();
  var RequestPagina ="generapagina.php";
  var valori = "marchio="+Parametro+"&r="+r; 
				//alert(poststr);
  FaiRequestLinksPOST(RequestPagina, valori);
}
//**********************************FINE DEL POST*************************************
