// JavaScript Document

function execJS(t) {
    var p1 = 0, p2 = 0, p3 = 0, p4 = 0;
    p1 = t.indexOf("<" + "script", 0);
    if(p1 == -1) return t;

    p2 = t.indexOf(">", p1 + 7) + 1;
    p3 = t.indexOf("<" + "/script>", p2);
    p4 = p3 + 9;

    var c = t.substring(p2, p3);
    var s = document.createElement("script");
    s.type = "text/javascript";
    s.text = c;
    document.getElementsByTagName("head")[0].appendChild(s);  

    t = t.substring(0, p1) + t.substr(p4);
    return execJS(t);
}


function createRequestObject() {
	var req = false;
	if (typeof XMLHttpRequest != "undefined")
		req = new XMLHttpRequest();
	if (!req && typeof ActiveXObject != "undefined") {
		try {
			req=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				req=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				try {
					req=new ActiveXObject("Msxml2.XMLHTTP.4.0");
				} catch (e3) {
					req=null;
				}
			}
		}
	}

	if(!req && window.createRequest)
		req = window.createRequest();

	if (!req) alert("Il browser non supporta AJAX");

	return req;
}

// JavaScript Document

var http2 = createRequestObject();

function handleResponse2() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();

		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
			//document.getElementById(update[0]).style.display="block";
			outputupdate=execJS(update[1]);
			document.getElementById(update[0]).innerHTML = update[1];
			if (document.getElementById(update[0]+'minus')) document.getElementById(update[0]+'minus').style.display='block';
		}
	} else {
		isBusy = false;
	}
}

function handleResponse3() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();
		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
			document.getElementById(update[0]).style.border="1px solid #7F9DB9;";
			document.getElementById(update[0]).style.padding="2px 2px 3px 3px";
			if (update[1]==1) {
				document.getElementById(update[0]).style.background = '#bbffbb';
			} else {
				document.getElementById(update[0]).style.background = '#ffaaaa';
			}
			if (document.getElementById(update[0]+'minus')) document.getElementById(update[0]+'minus').style.display='block';
		} 
	} else {
		isBusy = false;
	}
}

function handleResponse4() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();
		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
		}
	} else {
		isBusy = false;
	}
}

function handleResponse5() {
	if(http2.readyState == 4){
		var response = http2.responseText;
		var update = new Array();

		if(response.indexOf(']|[' != -1)) {
			update = response.split(']|[');
			outputupdate=execJS(update[1]);
		}
	} else {
		isBusy = false;
	}
}

var isBusy ='';
function sndReq2(action,idoutput,operation) {
	if (isBusy == true)
	{
		http2.onreadystatechange = null;
		http2.abort();
	}
	if (operation!="sendrich") document.getElementById(idoutput).innerHTML='<div id="boxmotore"><img style="margin-left:230px;" src="/img/loader.gif" /></div>';
	http2.open('get', '/include/ajax.php?'+action+'&idoutput='+idoutput+'&operation='+operation);
	isBusy = true;
	if (operation=="checkform") {
		http2.onreadystatechange = handleResponse3;
	} else if (operation=="sendrich") {
		http2.onreadystatechange = handleResponse4;
	} else {
		http2.onreadystatechange = handleResponse2;
	}
	http2.send(null);
}

function sndReq2b(action,idoutput,operation) {
	if (isBusy == true)
	{
		http2.onreadystatechange = null;
		http2.abort();
	}
	http2.open('get', '/include/ajax.php?'+action+'&idoutput='+idoutput+'&operation='+operation);
	isBusy = true;
	if (operation=="checkform") {
		http2.onreadystatechange = handleResponse3;
	} else {
		http2.onreadystatechange = handleResponse2;
	}
	http2.send(null);
}

function sndReqpost2(action,idoutput,operation,vars) {
	document.getElementById(idoutput).innerHTML='<img style="padding:2px; padding-top:4px;" src="/img/n_loader.gif" />';
	http2.open('post', '/include/ajax_php.inc.php?'+action+'&idoutput='+idoutput+'&operation='+operation, true);
	var variabili=vars;
	http2.onreadystatechange = handleResponse2;
	http2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http2.send(variabili);
}

function CheckFields(CtrlName,idc,oper,table,divout){
      var reqcell="";                  //string for required field's corresponding notification cell
      var msg="";                        //debug message
	  var altro="";
      var reqlist=document.getElementById(CtrlName).value.split(",");
	  //document.write(reqlist);
      for (var x in reqlist){
		  //document.write(reqlist[x]);
           // reqcell="Required_"+reqlist[x];
            msg+="&"+reqlist[x];
            msg+="="+document.getElementById(reqlist[x]).value;
      }
     //reqcell="CreationDate";
	 if (idc!='') {
	 	altro="&id="+idc;
	 }
	 msg=msg.replace(/\+/g,'#43#');
	 msg=unescape(msg);
	 if (divout==null) divout='form';
	 sndReqpost2('table='+table+altro,divout,oper,msg)
}


function rollhot(sel,base,foto) {
	//document.getElementById(sel).style.backgroundColor='#ffffff';
	document.getElementById(base).src=foto;
	document.getElementById(base).style.display='block';
}

function rollhotout(sel,base,foto) {
	//document.getElementById(sel).style.backgroundColor='#eeeeee';
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function jumpcerca(key,sel,base) {
	if (base!='') {
		var arr=base.split("|");
		var arr2 = new Array();
		var arr3 = new Array();
		i=0;
		esiste=0;
		while (arr.length > 0) {
			controlla=arr.shift();
			//alert(controlla);
			if (controlla==sel) {
				arr2[i]=controlla;
				esiste=1;
			} else {
				arr3[i]=controlla;
			}
			i++;
		}
		if (esiste==0) arr3[i]=sel;
		sel=arr3.join('|');
		var espressione=new RegExp('||',"gi");
		var ris=sel.replace(espressione,'');
		sel=ris;
	}
	document.location.href='/search/?key='+key+'&fcat='+sel;
}

function digitsOnly(e)
{

    var charCode = (e.which) ? e.which : event.keyCode
    
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {

        return false

    }

    return true
    
}

function ShowHideLayer(divID,divID2) {
	var box = document.getElementById(divID);	
		
	if(box.style.display == "none" || box.style.display=="") {
		box.style.display = "block"; 		
	}
	else {
		box.style.display = "none";		
	}
	if (divID2!='') ShowHideLayer2(divID2);
}

function ShowHideLayer2(divID) {
	var box = document.getElementById(divID);	
	if(box.style.display == "none" || box.style.display=="") {
		box.style.display = "block"; 		
	}
	else {
		box.style.display = "none";		
	}
}

function createDiv(nome,idvid) {
	var divTag = document.createElement("div");
	divTag.id = 'ex'+nome;
	divTag.setAttribute("align","center");
	divTag.style.margin = "0px auto";
	divTag.className ="dynamicDiv";
	divTag.innerHTML = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/'+idvid+'&hl=it&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+idvid+'&hl=it&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>';
	document. getElementById('b'+nome).appendChild(divTag);
	document.getElementById('c'+nome).style.display='none';
	document.getElementById('extv'+nome).style.display='block';
} 

 

function URLEncode(valore)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = valore;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
	  //document.URLForm.F2.select();
	//return false;
}

function CheckLen(nomecampo,lunghezza,check) {
         StrLen = document.getElementById(nomecampo).value.length;
         if (StrLen >= lunghezza ) {
                 document.getElementById(nomecampo).value = document.getElementById(nomecampo).value.substring(0,lunghezza);
                 CharsLeft = 0;
         } else {
                 CharsLeft = StrLen;
         }
		 if (check=='') check='charsleft';
         document.getElementById(check).innerHTML = CharsLeft+'/750';       
}

function redirectjs(idformbt) {
	document.getElementById('form'+idformbt).submit();
}