var ajax = [];
function pjx(args,fname,method) { 
	this.target=args[1];
	this.args=args[0];
	method=(method)?method:'GET';
	if(method=='post'){method='POST';}
	this.method = method;
	this.r=ghr();
	this.url = this.getURL(fname);
}

function formDump(){ var all = [];
	var fL = document.forms.length;
	for(var f = 0;
	f<fL;
	f++){ var els = document.forms[f].elements;
		for(var e in els){ var tmp = (els[e].id != undefined)? els[e].id : els[e].name;
			if(typeof tmp != 'string'){continue;
			}
			if(tmp){ all[all.length]=tmp}
		}
	}
	return all;
}

function getVal(id) {
	if (id.constructor == Function ) {return id();}
	if (typeof(id)!= 'string') { return id;	}
	var element = document.getElementById(id);
	if( !element ) {
		for( var i=0;		i<document.forms.length;		i++ ){
			element = document.forms[i].elements[id];
			if( element ) break;
		}
		if( element && !element.type ) element = element[0];
	}
	if(!element){
		alert('ERROR: Cant find HTML element with id or name: ' + id+'. Check that an element with name or id='+id+' exists');
		return 0;
	}
	if(element.type == 'select-one') {
		if(element.selectedIndex == -1) return;
		var item = element[element.selectedIndex];
		return item.value || item.text;
	}
	if(element.type == 'select-multiple') {
		var ans = [];
		var k =0;
		for (var i=0;		i<element.length;		i++) {
			if (element[i].selected || element[i].checked ) {
				ans[k++]= element[i].value || element[i].text;
			}
		}
		return ans;
	}
	if(element.type == 'radio' || element.type == 'checkbox'){
		var ans =[];
		var elms = document.getElementsByTagName('input');
		var endk = elms.length ;
		var i =0;
		for(var k=0;	k<endk;	k++){
			if(elms[k].type== element.type && elms[k].checked && (elms[k].id==id||elms[k].name==id)){ ans[i++]=elms[k].value;	}
		}
		return ans;
	}
	if( element.value == undefined ){ return element.innerHTML;	}
	else{ return element.value;	}
}

function fnsplit(arg) {
	var url="";
	if(arg=='NO_CACHE'){
		var s = Math.random();
		return '&args='+s+'&pjxrand='+s
	}
	if((typeof(arg)).toLowerCase() == 'object'){
		for(var k in arg){ url += '&' + k + '=' + arg[k];	}
	}
	else if (arg.indexOf('__') != -1) { arga = arg.split(/__/);
		url += '&' + arga[0] +'='+ escape(arga[1]);
	}
	else { var res = getVal(arg) || '';
		if(res.constructor != Array){ res = [res] }
		for(var i=0;
		i<res.length;
		i++) { url += '&args=' + escape(res[i]) + '&' + arg + '=' + escape(res[i]);
		}
	}
	return url;
}

pjx.prototype = {
	send2perl : function(){
		var r = this.r;
		var dt = this.target;
		this.pjxInitialized(dt);
		var url=this.url;
		var postdata;
		if(this.method=="POST"){
			var idx=url.indexOf('?');
			postdata = url.substr(idx+1);
			url = url.substr(0,idx);
		}
		
		r.open(this.method,url,true);
		if(this.method=="POST"){
			r.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			r.setRequestHeader("Cache-control", "no-cache");
			r.send(postdata);
		}
		if(this.method=="GET"){ r.send(null);		}
		r.onreadystatechange = handleReturn;
	}
	, pjxInitialized : function(){Mask.show();}
	, pjxCompleted : function(){
			try  {grp = document.getElementById('gruppo').innerHTML}
			catch(err) {grp=0;}
			if (!grp.length) grp=0;
			if (document.getElementById('procedi').innerHTML == '1' && grp==0) {
				continua(cercarisultato(0))
				Mask.hide();
			}
			else Mask.hide();
		}
	, readyState4 : function(){
		var rsp = unescape(this.r.responseText);
		/* the response from perl */
		var splitval = '__pjx__';
		/* to split text */ /* fix IE problems with undef values in an Array getting squashed*/
		rsp = rsp.replace(splitval+splitval+'g',splitval+" "+splitval);
		var data = rsp.split(splitval);
		dt = this.target;
		if (dt.constructor != Array) { dt=[dt];		}
		if (data.constructor != Array) { data=[data];		}
		if (typeof(dt[0])!='function') { 
			for ( var i=0;	i<dt.length;	i++ ) { 
				var div = document.getElementById(dt[i]);
				if (div.type =='text' || div.type=='textarea' || div.type=='hidden' ) { div.value=data[i];	}
				else{ div.innerHTML = data[i];		}
			}
		}
		else if (typeof(dt[0])=='function') { dt[0].apply(this,data);		}
		this.pjxCompleted(dt);
	}
	, getURL : function(fname) { 
		var args = this.args;
		var url= 'fname=' + fname;
		for (var i=0;
		i<args.length;
		i++) { url=url + args[i];
		}
		return url;
	}
};
handleReturn = function() {
	for( var k=0;	k<ajax.length;	k++ ) {
		if (ajax[k].r==null) {
			ajax.splice(k--,1);
			continue;
		}
		if ( ajax[k].r.readyState== 4) {
			ajax[k].readyState4();
			ajax.splice(k--,1);
			continue;
		}
	}
};
var i = 0;
function cercarisultato(n)  {
	var result = "";
	try  {
		result = document.getElementById('risultato').innerHTML
	}
	catch(err) {result = ""}
	i++;
	if (result == "") {
		var com = "cercarisultato("+i+")"
		xx = setTimeout(com,500); result = "";
	}
	else {
		document.getElementById('procedi').innerHTML = "";
		return result;
	}
}
var ghr=getghr();
function getghr(){
	if(typeof XMLHttpRequest != "undefined") {
		return function(){return new XMLHttpRequest();}
	}
	var msv= ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
	for(var j=0;	j<=msv.length;	j++){
		try {
			A = new ActiveXObject(msv[j]);
			if(A){ return function(){return new ActiveXObject(msv[j]);			}
			}
		}
		catch(e) { }
	}
	return false;
}

function jsdebug(){ 
	var tmp = document.getElementById('pjxdebugrequest').innerHTML = "<br><pre>";
	for( var i=0;	i < ajax.length;	i++ ) { 
		tmp += '<a href= '+ ajax[i].url +' target=_blank>' + decodeURI(ajax[i].url) + ' </a><br>';
	}
	document.getElementById('pjxdebugrequest').innerHTML = tmp + "</pre>";
}

/**
 Inspired by the Veil component by Igor Vaynberg in wicketstuff-minis
 http://wicketstuff.org/confluence/display/STUFFWIKI/wicketstuff-minis
 wicketstuff-minis is released under the Apache 2 License
 http://apache.org/licenses/LICENSE-2.0.html 
*/
Mask = { };
 
/**
 Shows a mask and a spinner over the element with the specified id
*/
Mask.show = function(targetId)
{
  var target=document.getElementById(targetId);
  var mask=document.createElement("div");
  mask.innerHTML="&nbsp;";
  mask.className="wicket-mask";
  mask.style.cursor="not-allowed";
  mask.style.zIndex="5000";
  mask.id="wicket_mask_"+targetId;
  document.body.appendChild(mask);
  Mask.offsetMask(mask);

  var spinner=document.createElement("div");
  spinner.innerHTML="&nbsp;";
  spinner.className="wicket-spinner";
  spinner.style.cursor="not-allowed";
  spinner.style.zIndex="6000";
  spinner.id="wicket_spinner_"+targetId;
  document.body.appendChild(spinner);
  Mask.centerSpinner(spinner);
}

/**
 Hides the mask and spinner
*/
Mask.hide = function(targetId)
{
  var mask=document.getElementById("wicket_mask_"+targetId);
  if (mask!=null) {
   mask.style.display="none";
   document.body.removeChild(mask);
  }
  var spinner=document.getElementById("wicket_spinner_"+targetId);
  if (spinner!=null) {
   spinner.style.display="none";
   document.body.removeChild(spinner);
  }
}

/**
 * Places the spinner at the center of the viewport.
 */ 
Mask.centerSpinner = function(spinner)
{
  var width = document.body.clientWidth;
  var height = document.body.clientHeight;
  
  var offsetX =   document.body.scrollLeft;
  var offsetY =  document.body.scrollTop;
  
  var left = (width / 2) - 24 + offsetX;
  var top = (height / 2) - 24 + offsetY;
  
  spinner.style.left = left + "px";
  spinner.style.top = top + "px";
}
 
/**
 * Offsets the mask to the scroll position.
 */ 
Mask.offsetMask = function(mask)
{
  var offsetX =   document.body.scrollLeft;
  var offsetY =  document.body.scrollTop;
  
  mask.style.left = offsetX + "px";
  mask.style.top = offsetY + "px";
}
function NumberFormat(num, inputDecimal) {
	this.VERSION = 'Number Format v1.5.4';
	this.COMMA = ',';
	this.PERIOD = '.';
	this.DASH = '-';
	this.LEFT_PAREN = '(';
	this.RIGHT_PAREN = ')';
	this.LEFT_OUTSIDE = 0;
	this.LEFT_INSIDE = 1;
	this.RIGHT_INSIDE = 2;
	this.RIGHT_OUTSIDE = 3;
	this.LEFT_DASH = 0;
	this.RIGHT_DASH = 1;
	this.PARENTHESIS = 2;
	this.NO_ROUNDING = -1
	this.num;
	this.numOriginal;
	this.hasSeparators = false;
	this.separatorValue;
	this.inputDecimalValue;
	this.decimalValue;
	this.negativeFormat;
	this.negativeRed;
	this.hasCurrency;
	this.currencyPosition;
	this.currencyValue;
	this.places;
	this.roundToPlaces;
	this.truncate;
	this.setNumber = setNumberNF;
	this.toUnformatted = toUnformattedNF;
	this.setInputDecimal = setInputDecimalNF;
	this.setSeparators = setSeparatorsNF;
	this.setCommas = setCommasNF;
	this.setNegativeFormat = setNegativeFormatNF;
	this.setNegativeRed = setNegativeRedNF;
	this.setCurrency = setCurrencyNF;
	this.setCurrencyPrefix = setCurrencyPrefixNF;
	this.setCurrencyValue = setCurrencyValueNF;
	this.setCurrencyPosition = setCurrencyPositionNF;
	this.setPlaces = setPlacesNF;
	this.toFormatted = toFormattedNF;
	this.toPercentage = toPercentageNF;
	this.getOriginal = getOriginalNF;
	this.moveDecimalRight = moveDecimalRightNF;
	this.moveDecimalLeft = moveDecimalLeftNF;
	this.getRounded = getRoundedNF;
	this.preserveZeros = preserveZerosNF;
	this.justNumber = justNumberNF;
	this.expandExponential = expandExponentialNF;
	this.getZeros = getZerosNF;
	this.moveDecimalAsString = moveDecimalAsStringNF;
	this.moveDecimal = moveDecimalNF;
	this.addSeparators = addSeparatorsNF;
	if (inputDecimal == null) {
		this.setNumber(num, this.PERIOD);
	} else {
		this.setNumber(num, inputDecimal);
	}
	this.setCommas(true);
	this.setNegativeFormat(this.LEFT_DASH);
	this.setNegativeRed(false);
	this.setCurrency(false);
	this.setCurrencyPrefix('$');
	this.setPlaces(2);
}
function setInputDecimalNF(val){
	this.inputDecimalValue = val;
}
function setNumberNF(num, inputDecimal){
	if (inputDecimal != null) {
		this.setInputDecimal(inputDecimal);
	}
	this.numOriginal = num;
	this.num = this.justNumber(num);
}
function toUnformattedNF(){
	return (this.num);
}
function getOriginalNF(){
	return (this.numOriginal);
}
function setNegativeFormatNF(format){
	this.negativeFormat = format;
}
function setNegativeRedNF(isRed){
	this.negativeRed = isRed;
}
function setSeparatorsNF(isC, separator, decimal){
	this.hasSeparators = isC;
	if (separator == null) separator = this.COMMA;
	if (decimal == null) decimal = this.PERIOD;
	if (separator == decimal) {
		this.decimalValue = (decimal == this.PERIOD) ? this.COMMA : this.PERIOD;
	} else {
		this.decimalValue = decimal;
	}
	this.separatorValue = separator;
}
function setCommasNF(isC){
	this.setSeparators(isC, this.COMMA, this.PERIOD);
}
function setCurrencyNF(isC){
	this.hasCurrency = isC;
}
function setCurrencyValueNF(val){
	this.currencyValue = val;
}
function setCurrencyPrefixNF(cp){
	this.setCurrencyValue(cp);
	this.setCurrencyPosition(this.LEFT_OUTSIDE);
}
function setCurrencyPositionNF(cp){
	this.currencyPosition = cp
}
function setPlacesNF(p, tr){
	this.roundToPlaces = !(p == this.NO_ROUNDING);
	this.truncate = (tr != null && tr);
	this.places = (p < 0) ? 0 : p;
}
function addSeparatorsNF(nStr, inD, outD, sep){
	nStr += '';
	var dpos = nStr.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + nStr.substring(dpos + 1, nStr.length);
		nStr = nStr.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(nStr)) {
		nStr = nStr.replace(rgx, '$1' + sep + '$2');
	}
	return nStr + nStrEnd;
}
function toFormattedNF(){
	var pos;
	var nNum = this.num;
	var nStr;
	var splitString = new Array(2);
	if (this.roundToPlaces) {
		nNum = this.getRounded(nNum);
		nStr = this.preserveZeros(Math.abs(nNum));
	} else {
		nStr = this.expandExponential(Math.abs(nNum));
	}
	if (this.hasSeparators) {
		nStr = this.addSeparators(nStr, this.PERIOD, this.decimalValue, this.separatorValue);
	} else {
		nStr = nStr.replace(new RegExp('\\' + this.PERIOD), this.decimalValue);
	}
	var c0 = '';
	var n0 = '';
	var c1 = '';
	var n1 = '';
	var n2 = '';
	var c2 = '';
	var n3 = '';
	var c3 = '';
	var negSignL = (this.negativeFormat == this.PARENTHESIS) ? this.LEFT_PAREN : this.DASH;
	var negSignR = (this.negativeFormat == this.PARENTHESIS) ? this.RIGHT_PAREN : this.DASH;
	if (this.currencyPosition == this.LEFT_OUTSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
		}
		if (this.hasCurrency) c0 = this.currencyValue;
	} else if (this.currencyPosition == this.LEFT_INSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
		}
		if (this.hasCurrency) c1 = this.currencyValue;
	}
	else if (this.currencyPosition == this.RIGHT_INSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n0 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n3 = negSignR;
		}
		if (this.hasCurrency) c2 = this.currencyValue;
	}
	else if (this.currencyPosition == this.RIGHT_OUTSIDE) {
		if (nNum < 0) {
			if (this.negativeFormat == this.LEFT_DASH || this.negativeFormat == this.PARENTHESIS) n1 = negSignL;
			if (this.negativeFormat == this.RIGHT_DASH || this.negativeFormat == this.PARENTHESIS) n2 = negSignR;
		}
		if (this.hasCurrency) c3 = this.currencyValue;
	}
	nStr = c0 + n0 + c1 + n1 + nStr + n2 + c2 + n3 + c3;
	if (this.negativeRed && nNum < 0) {
		nStr = '<font color="red">' + nStr + '</font>';
	}
	return (nStr);
}
function toPercentageNF(){
	nNum = this.num * 100;
	nNum = this.getRounded(nNum);
	return nNum + '%';
}
function getZerosNF(places){
	var extraZ = '';
	var i;
	for (i=0; i<places; i++) {
		extraZ += '0';
	}
	return extraZ;
}
function expandExponentialNF(origVal){
	if (isNaN(origVal)) return origVal;
	var newVal = parseFloat(origVal) + '';
	var eLoc = newVal.toLowerCase().indexOf('e');
	if (eLoc != -1) {
		var plusLoc = newVal.toLowerCase().indexOf('+');
		var negLoc = newVal.toLowerCase().indexOf('-', eLoc);
		var justNumber = newVal.substring(0, eLoc);
		if (negLoc != -1) {
			var places = newVal.substring(negLoc + 1, newVal.length);
			justNumber = this.moveDecimalAsString(justNumber, true, parseInt(places));
		} else {
			if (plusLoc == -1) plusLoc = eLoc;
			var places = newVal.substring(plusLoc + 1, newVal.length);
			justNumber = this.moveDecimalAsString(justNumber, false, parseInt(places));
		}
		newVal = justNumber;
	}
	return newVal;
}
function moveDecimalRightNF(val, places){
	var newVal = '';
	if (places == null) {
		newVal = this.moveDecimal(val, false);
	} else {
		newVal = this.moveDecimal(val, false, places);
	}
	return newVal;
}
function moveDecimalLeftNF(val, places){
	var newVal = '';
	if (places == null) {
		newVal = this.moveDecimal(val, true);
	} else {
		newVal = this.moveDecimal(val, true, places);
	}
	return newVal;
}
function moveDecimalAsStringNF(val, left, places){
	var spaces = (arguments.length < 3) ? this.places : places;
	if (spaces <= 0) return val;
	var newVal = val + '';
	var extraZ = this.getZeros(spaces);
	var re1 = new RegExp('([0-9.]+)');
	if (left) {
		newVal = newVal.replace(re1, extraZ + '$1');
		var re2 = new RegExp('(-?)([0-9]*)([0-9]{' + spaces + '})(\\.?)');
		newVal = newVal.replace(re2, '$1$2.$3');
	} else {
		var reArray = re1.exec(newVal);
		if (reArray != null) {
			newVal = newVal.substring(0,reArray.index) + reArray[1] + extraZ + newVal.substring(reArray.index + reArray[0].length);
		}
		var re2 = new RegExp('(-?)([0-9]*)(\\.?)([0-9]{' + spaces + '})');
		newVal = newVal.replace(re2, '$1$2$4.');
	}
	newVal = newVal.replace(/\.$/, '');
	return newVal;
}
function moveDecimalNF(val, left, places){
	var newVal = '';
	if (places == null) {
		newVal = this.moveDecimalAsString(val, left);
	} else {
		newVal = this.moveDecimalAsString(val, left, places);
	}
	return parseFloat(newVal);
}
function getRoundedNF(val){
	val = this.moveDecimalRight(val);
	if (this.truncate) {
		val = val >= 0 ? Math.floor(val) : Math.ceil(val);
	} else {
		val = Math.round(val);
	}
	val = this.moveDecimalLeft(val);
	return val;
}
function preserveZerosNF(val){
	var i;
	val = this.expandExponential(val);
	if (this.places <= 0) return val;
	var decimalPos = val.indexOf('.');
	if (decimalPos == -1) {
		val += '.';
		for (i=0; i<this.places; i++) {
			val += '0';
		}
	} else {
		var actualDecimals = (val.length - 1) - decimalPos;
		var difference = this.places - actualDecimals;
		for (i=0; i<difference; i++) {
			val += '0';
		}
	}
	return val;
}
function justNumberNF(val){
	newVal = val + '';
	var isPercentage = false;
	if (newVal.indexOf('%') != -1) {
		newVal = newVal.replace(/\%/g, '');
		isPercentage = true;
	}
	var re = new RegExp('[^\\' + this.inputDecimalValue + '\\d\\-\\+\\(\\)eE]', 'g');
	newVal = newVal.replace(re, '');
	var tempRe = new RegExp('[' + this.inputDecimalValue + ']', 'g');
	var treArray = tempRe.exec(newVal);
	if (treArray != null) {
		var tempRight = newVal.substring(treArray.index + treArray[0].length);
		newVal = newVal.substring(0,treArray.index) + this.PERIOD + tempRight.replace(tempRe, '');
	}
	if (newVal.charAt(newVal.length - 1) == this.DASH ) {
		newVal = newVal.substring(0, newVal.length - 1);
		newVal = '-' + newVal;
	}
	else if (newVal.charAt(0) == this.LEFT_PAREN && newVal.charAt(newVal.length - 1) == this.RIGHT_PAREN) {
		newVal = newVal.substring(1, newVal.length - 1);
		newVal = '-' + newVal;
	}
	newVal = parseFloat(newVal);
	if (!isFinite(newVal)) {
		newVal = 0;
	}
	if (isPercentage) {
		newVal = this.moveDecimalLeft(newVal, 2);
	}
	return newVal;
}

