// initialize ourselves when the page is
// finished loading
window.onload = initialize;

function MM_jumpMenu_aj(targ,selObj,restore){ //v3.0
	ahahsubmit(selObj.options[selObj.selectedIndex].value, 'GET', 'content', 'spin');
}

/** Credits: http://www.babailiica.com/js/reorderizator.php */
function selectall(obj) {
	for (i=0; i<obj.length; i++) {
		obj[i].selected = true;
	}
}

function up(obj) {
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var sel = new Array();
	for (i=0; i<obj.length; i++) {
		if (obj[i].selected == true) {
			sel[sel.length] = i;
		}
	}
	var tmptxt = "";
	var tmpval = "";
	for (i in sel) {
		if (sel[i] != 0 && !obj[sel[i]-1].selected) {
			tmptxt = obj[sel[i]-1].text;
			tmpval = obj[sel[i]-1].value;
			obj[sel[i]-1].text = obj[sel[i]].text;
			obj[sel[i]-1].value = obj[sel[i]].value;
			obj[sel[i]].text = tmptxt;
			obj[sel[i]].value = tmpval;
			obj[sel[i]-1].selected = true;
			obj[sel[i]].selected = false;
		}
	}
}

function down(obj) {
	if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
		return false;
	var sel = new Array();
	for (i=obj.length-1; i>-1; i--) {
		if (obj[i].selected == true) {
			sel[sel.length] = i;
		}
	}
	var tmptxt = "";
	var tmpval = "";
	for (i in sel) {
		if (sel[i] != obj.length-1 && !obj[sel[i]+1].selected) {
			tmptxt = obj[sel[i]+1].text;
			tmpval = obj[sel[i]+1].value;
			obj[sel[i]+1].text = obj[sel[i]].text;
			obj[sel[i]+1].value = obj[sel[i]].value;
			obj[sel[i]].text = tmptxt;
			obj[sel[i]].value = tmpval;
			obj[sel[i]+1].selected = true;
			obj[sel[i]].selected = false;
		}
	}
}

function inarray(v,a) {
	for (i in a) {
		if (a[i] == v) {
			return true;
		}
	}
return false;
}


/** iframe dropdown JS funtions. */

var smooth_timer;

function ShowTip(fArg) {
  var tooltipOBJ = (document.getElementById) ? document.getElementById('ih' + fArg) : eval("document.all['ih" + fArg + "']");
  if (tooltipOBJ != null) {
    var tooltipLft = (document.body.offsetWidth?document.body.offsetWidth:document.body.style.pixelWidth) - (tooltipOBJ.offsetWidth?tooltipOBJ.offsetWidth:(tooltipOBJ.style.pixelWidth?tooltipOBJ.style.pixelWidth:$TOOLTIPWIDTH)) - 5;
    var tooltipTop = 10;
    if (navigator.appName == 'Netscape') {
      if (parseFloat(navigator.appVersion) >= 5) tooltipTop = (document.body.scrollTop>=0?document.body.scrollTop+10:event.clientY+10);
      tooltipOBJ.style.left = tooltipLft; tooltipOBJ.style.top = tooltipTop;
    }
    else {
      tooltipLft -= 30;
      tooltipTop = (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + event.clientY - (tooltipOBJ.scrollHeight?tooltipOBJ.scrollHeight:tooltipOBJ.style.pixelHeight) - 30;
      if (tooltipTop < (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + 10) {
        if (event.clientX > tooltipLft) tooltipTop = (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + event.clientY + 30;
        else tooltipTop = (document.body.scrollTop?document.body.scrollTop:document.body.offsetTop) + 10;
      } 
      tooltipOBJ.style.pixelLeft = tooltipLft; tooltipOBJ.style.pixelTop = tooltipTop;
    }
    tooltipOBJ.style.visibility = "visible";
  }
}
function HideTip(fArg) {
  var tooltipOBJ = (document.getElementById) ? document.getElementById('ih' + fArg) : eval("document.all['ih" + fArg + "']");
  if (tooltipOBJ != null) tooltipOBJ.style.visibility = "hidden";
}
function smoothHeight(id, curH, targetH, stepH, mode) {
  diff = targetH - curH;
  if (diff != 0) {
    newH = (diff > 0) ? curH + stepH : curH - stepH;
    ((document.getElementById) ? document.getElementById(id) : eval("document.all['" + id + "']")).style.height = newH + "px";
    if (smooth_timer) window.clearTimeout(smooth_timer);
    smooth_timer = window.setTimeout( "smoothHeight('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 16 );
  }
  else if (mode != "o") ((document.getElementById) ? document.getElementById(mode) : eval("document.all['" + mode + "']")).style.display="none";
}
function servOC(i, href, nColor) {
  var trObj = (document.getElementById) ? document.getElementById('ihtr' + i) : eval("document.all['ihtr" + i + "']");
  var nameObj = (document.getElementById) ? document.getElementById('name' + i) : eval("document.all['name" + i + "']");
  var ifObj = (document.getElementById) ? document.getElementById('ihif' + i) : eval("document.all['ihif" + i + "']");
  if (trObj != null) {
    if (trObj.style.display=="none") {
      ifObj.style.height = "0px";
      trObj.style.display="";
      nameObj.style.background="#ccffcc";
      if (!ifObj.src) ifObj.src = href;
      smoothHeight('ihif' + i, 0, 210, 42, 'o');
    }
    else {
      nameObj.style.background=nColor;
      smoothHeight('ihif' + i, 210, 0, 42, 'ihtr' + i);
    }
  }
}


/** Misc public JS funtions. */

function Choice(){
	Create.URL.value = Create.choices.value;
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location=\'"+selObj.options[selObj.selectedIndex].value+"\'");
	if (restore) selObj.selectedIndex=0;
}

function hideshowlayer(LayerName) {
	var VState = document.getElementById(LayerName).style.display;
	document.title = LayerName +" "+document.getElementById(LayerName).style.display;

	if (VState=="inline") {
		document.getElementById(LayerName).style.display = "none";
	}
	if (VState=="none") {
		document.getElementById(LayerName).style.display = "inline";
	}
}

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	while (theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
		theForm[z].checked = theElement.checked;
		z++;
	}
}

//DHTML Window script- Copyright Dynamic Drive (http://www.dynamicdrive.com)
//For full source code, documentation, and terms of usage,
//Modified by Arie Vandenberg - Vandenberg Media Inc.

var dragapproved=false
var minrestore=0
var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all

function iecompattest(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function drag_drop(e){
if (ie5&&dragapproved&&event.button==1){
document.getElementById(winid2).style.right=tempx-event.clientX+offsetx+"px"
document.getElementById(winid2).style.top=tempy+event.clientY-offsety+"px"
}
else if (ns6&&dragapproved){
document.getElementById(winid2).style.right=tempx-e.clientX+offsetx+"px"
document.getElementById(winid2).style.top=tempy+e.clientY-offsety+"px"
}
}

function initializedrag(e,winid){
offsetx=ie5? event.clientX : e.clientX
offsety=ie5? event.clientY : e.clientY
document.getElementById("dwindowcontent").style.display="none" //extra
tempx=parseInt(document.getElementById(winid).style.right)
tempy=parseInt(document.getElementById(winid).style.top)

dragapproved=true
document.getElementById(winid).onmousemove=drag_drop
winid2=winid
}

function loadwindow(url,winid,width,height){

document.getElementById("dwindowcontent").style.height=height-14+"px"

if (!ie5&&!ns6)
window.open(url,"","width=width,height=height,scrollbars=1")
else{
cframe=winid+"_frame"
document.getElementById(winid).style.display=''
document.getElementById(winid).style.width=initialwidth=width+"px"
document.getElementById(winid).style.height=initialheight=height+"px"
document.getElementById(winid).style.right="0px"
document.getElementById(winid).style.top=ns6? window.pageYOffset*1+60+"px" : iecompattest().scrollTop*1+60+"px"
document.getElementById(cframe).src=url
}
}

function maximize(winid){
if (minrestore==0){
minrestore=1 //maximize window
document.getElementById("maxname").setAttribute("src","restore.gif")
document.getElementById(winid).style.width=ns6? window.innerWidth-20+"px" : iecompattest().clientWidth+"px"
document.getElementById(winid).style.height=ns6? window.innerHeight-20+"px" : iecompattest().clientHeight+"px"
}
else{
minrestore=0 //restore window
document.getElementById("maxname").setAttribute("src","max.gif")
document.getElementById(winid).style.width=initialwidth
document.getElementById(winid).style.height=initialheight
}
document.getElementById(winid).style.left=ns6? window.pageXOffset+"px" : iecompattest().scrollLeft+"px"
document.getElementById(winid).style.top=ns6? window.pageYOffset+"px" : iecompattest().scrollTop+"px"
}

function closeit(winid){
document.getElementById(winid).style.display="none"
}

function stopdrag(winid){
dragapproved=false;
document.getElementById(winid).onmousemove=null;
document.getElementById("dwindowcontent").style.display="" //extra
}

/***********************************************
* Overlapping Content link- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
function getposOffset(overlay, offsettype){
	var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
	var parentEl=overlay.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}

function overlay(curobj, subobj,left,top){
	if (document.getElementById){
		var subobj=document.getElementById(subobj)
		subobj.style.left=left
		subobj.style.top=top
		subobj.style.display="block"
		return false
	}else{
		return true
	}
}

function overlayclose(subobj){
	document.getElementById(subobj).style.display="none"
}

/***********************************************
* START ahah/history
***********************************************/

/** Our function that initializes when the page
    is finished loading. */
function initialize() {
   // initialize the DHTML History framework
   dhtmlHistory.initialize();

   // add ourselves as a DHTML History listener
   dhtmlHistory.addListener(handleHistoryChange);

/**
   // subscribe to mouse clicks on the menu
   var menu = document.getElementById("menu");
   if (typeof window.attachEvent != "undefined") {
      // Internet Explorer
      menu.attachEvent("onclick", handleMouseClick);
   } else { // W3C standards
      menu.addEventListener("click", handleMouseClick, false);
   }
*/
   
   // determine what our initial location is
   // by retrieving it from the browser's
   // location after the hash
   var currentLocation = dhtmlHistory.getCurrentLocation();
      
   // if there is no location then display
   // the default, which is the inbox
   if (currentLocation == "") {
      return false;
      exit;
      var target = "";
      // currentLocation = "";
   }else{
      var target = "content";
   }

 
   // causes browser loading state to HANG
   // extract the section to display from
   // the initial location 
   currentLocation = currentLocation.replace(/section\:/, "");

   ahah(currentLocation, target);
}

/** Handles history change events. */
function handleHistoryChange(newLocation, historyData) {
   // if there is no location then display
   // the default, which is the inbox

   if (newLocation == "") {
      return false;
      exit;
      var target = "";
      // currentLocation = "";
   }else{
      var target = "content";
   }
   
   // extract the section to display from
   // the location change; newLocation will
   // begin with the word "section:" 
   newLocation = newLocation.replace(/section\:/, "");
   
   // update the browser to respond to this
   // DHTML history change

   ahah(newLocation, target);
}


/********************************
Funzione che sostituisce il SUBMIT, utile al passaggio di tutti i parametri
alla pagina che poi restituirą i valori a seconda dei parametri inseriti
esempio utilizzo: <a href="javascript:submit('nomefile.php', 'GET')">link</a>
********************************/
function ahahsubmit(FILE, METHOD, TARGET, SPIN){

	var num = document.forms[0].elements.length;
	var url = "";

	//radio button 
	var j = 0;
	var a = 0;
	var radio_buttons = new Array();
	var nome_buttons = new Array();
	var the_form = window.document.forms[0];
	for(var i=0; i<the_form.length; i++){
		var temp = the_form.elements[i].type;
		if ( (temp == "radio") && ( the_form.elements[i].checked) ) { 
			nome_buttons[a] = the_form.elements[i].name;
			radio_buttons[j] = the_form.elements[i].value; 
			j++; 
			a++;
		}
	}
	for(var k = 0; k < radio_buttons.length; k++) {
		url += nome_buttons[k] + "=" + radio_buttons[k] + "&";
	}
	//checkbox
	var j = 0;
	var a = 0;
	var check_buttons = new Array();
	var nome_buttons = new Array();
	var the_form = window.document.forms[0];
	for(var i=0; i<the_form.length; i++){
		var temp = the_form.elements[i].type;
		if ( (temp == "checkbox") && ( the_form.elements[i].checked) ) { 
			nome_buttons[a] = the_form.elements[i].name;
			check_buttons[j] = the_form.elements[i].value; 
			j++; 
			a++;
		}
	}
	for(var k = 0; k < check_buttons.length; k++) {
		url += nome_buttons[k] + "=" + check_buttons[k] + "&";
	}
	for (var i = 0; i < num; i++){
		
		var chiave = Url.encode(document.forms[0].elements[i].name);
		var valore = Url.encode(document.forms[0].elements[i].value);
		var tipo = Url.encode(document.forms[0].elements[i].type);
		
		if ( (tipo == "submit") || (tipo == "radio") || (tipo == "checkbox") ){}
		else {
			url += chiave + "=" + valore + "&";
		}
	}
	var parameters = url;
	url = FILE + "?" + url;
	if (METHOD == undefined) { METHOD = "GET"; 	}
	if (METHOD == "GET") { ahah(url, TARGET, '', METHOD, '', SPIN); }
	else { ahah(FILE, TARGET, '', METHOD, parameters, SPIN); }
}

function ahah(url, target, delay, method, parameters, spin) {

  if (method == undefined) { 
	if (spin == "spin") { 
	  document.getElementById(target).innerHTML = '<p align="center">&nbsp;</p> <p align="center">&nbsp;</p> <p align="center"><img style="padding-bottom:375px;" src="/images/working2.gif" width="32" height="32">';
	}
	  if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  if (req) {

if (target == "content" && url != "/go.php") {
	// dhtmlHistory.js routines
	// get the location the user clicked on
	var newLocation = target.id;
	var modifiedLocation = "section:" + url;
	var historyData = "";
	dhtmlHistory.add(modifiedLocation, historyData);
}

		req.onreadystatechange = function() {
			ahahDone(url, target, delay, method, parameters);
		};
		req.open("GET", url, true);
		req.send("");
	  }  
  }
  if ( (method == "GET") || (method == "get") ){
	if (spin == "spin") { 
	  document.getElementById(target).innerHTML = '<p align="center">&nbsp;</p> <p align="center">&nbsp;</p> <p align="center"><img style="padding-bottom:375px;" src="/images/working2.gif" width="32" height="32">';
	}
	  if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  if (req) {

if (target == "content" && url != "/go.php") {
	// dhtmlHistory.js routines
	// get the location the user clicked on
	var newLocation = target.id;
	var modifiedLocation = "section:" + url;
	var historyData = "";
	dhtmlHistory.add(modifiedLocation, historyData);
}

		req.onreadystatechange = function() {
			ahahDone(url, target, delay, method, parameters);
		};
		req.open(method, url, true);
		req.send("");
	  }
  }

  if ( (method == "POST") || (method == "post") ){
	if (spin == "spin") { 
	  document.getElementById(target).innerHTML = '<p align="center">&nbsp;</p> <p align="center">&nbsp;</p> <p align="center"><img style="padding-bottom:375px;" src="/images/working2.gif" width="32" height="32">';
	}
	  if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	  } else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  if (req) {

if (target == "content" && url != "/go.php") {
	// dhtmlHistory.js routines
	// get the location the user clicked on
	var newLocation = target.id;
	var modifiedLocation = "section:" + url;
	var historyData = "";
	dhtmlHistory.add(modifiedLocation, historyData);
}

		req.onreadystatechange = function() {
			ahahDone(url, target, delay, method, parameters);
		};
		req.open(method, url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.send(parameters);
	  }
  }
}  

function ahahDone(url, target, delay, method, parameters) {
  if (req.readyState == 4) { 
    if (req.status == 200) { 
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML="ahah error:\n"+req.statusText;
    }
  }
}

var bSaf = (navigator.userAgent.indexOf('Safari') != -1);
var bOpera = (navigator.userAgent.indexOf('Opera') != -1);
var bMoz = (navigator.appName == 'Netscape');
function execJS(node) {
  var st = node.getElementsByTagName('SCRIPT');
  var strExec;
  for(var i=0;i<st.length; i++) {     
    if (bSaf) {
      strExec = st[i].innerHTML;
    }
    else if (bOpera) {
      strExec = st[i].text;
    }
    else if (bMoz) {
      strExec = st[i].textContent;
    }
    else {
      strExec = st[i].text;
    }
    try {
      eval(strExec.split("<!--").join("").split("-->").join(""));
    } catch(e) {
      alert(e);
    }
  }
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
}
