﻿
var rootPath=document.location.href.replace('http://','');
rootPath=rootPath.substring(rootPath.indexOf('/'));
rootPath=rootPath.substr(0,rootPath.indexOf('/',1));

function $(obj) {
	if (typeof obj=='string')
		return document.getElementById(obj);
	else
		return obj;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

//********************    VALIDATION   ************************
var datepath = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
var emailpat = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

function validateDate( sdate ) {
  if(!datepath.test(sdate))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = sdate.substring(2,3) 
    var arrayDate = sdate.split(strSeparator); 
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, 
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[0],10); 

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)
        return true; //found in lookup table, good date
    }
    
    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    var intMonth = parseInt(arrayDate[1],10);
    if (intMonth == 2) { 
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }   
       }
    }
  }  
  return false; //any other values, bad date
}

function validateMail(mail) {
   return emailpat.test(mail);
}

//**********************   DISPLAY   **************************
function jodaDisplay() {
}

	jodaDisplay.show = function (obj) {
		var o=$(obj);
		if (o) o.style.display="block";
	}

	jodaDisplay.hide = function (obj) {
		var o=$(obj);
		if (o) o.style.display="none";
	}

	jodaDisplay.displaySwitch = function (obj) {
		var o=$(obj);
		if (o) 
			if (o.style.display=="block")
				o.style.display="none";
			else
				o.style.display="block";
	}

	jodaDisplay.setHTML = function (obj,html) {
		var o=$(obj);
		if (o) 
			o.innerHTML=html;
	}

	//******************** SHIELD ***************************
	
	jodaDisplay.showShield = function (color,opacity) {
	   var shieldObject;
		if (!shieldObject) {
			shieldObject=document.createElement('DIV');
			shieldObject.className='jodashield';
			shieldObject.id='jodashield';
			document.body.appendChild(shieldObject);
			if (opacity) {
				shieldObject.style.opacity = opacity;
				shieldObject.style.filter = 'alpha(opacity='+opacity*100+')';
			}
			shieldObject.style.position='absolute';
			shieldObject.style.display='block';
			if (color) 
				shieldObject.style.backgroundColor=color;
			shieldObject.style.zIndex=100;
			shieldObject.innerHTML='';
			shieldObject.style.left=0;	
			shieldObject.style.top=0;
			shieldObject.style.height='100%'; //document.body.scrollHeight;
			shieldObject.style.width='100%'; //document.body.scrollWidth;
			shieldObject.style.overflow='auto';
		}
		return shieldObject;
	}

	jodaDisplay.hideShield = function (shield) {
		if (shield) 
			document.body.removeChild(shield);
	}

	//***********************   CENTER   **************************

	jodaDisplay.centerParent = function (obj) {
		var o=$(obj);
		if (o) {
			o.style.position="absolute";
			w=o.parentNode.clientWidth;
			h=o.parentNode.clientHeight;
			o.style.left=(w-(o.clientWidth))>0?(w-(o.clientWidth))/2:0;	
			o.style.top=(h-(o.clientHeight))>0?(h-(o.clientHeight))/2:0;
		}
	}
	
	jodaDisplay.centerClient = function (obj) {
		var o=$(obj);
		if (o) {
			o.style.position="absolute";
			w=document.body.clientWidth;
			h=document.body.clientHeight;
			o.style.left=(w-(o.clientWidth))>0?(w-(o.clientWidth))/2:0;	
			o.style.top=(h-(o.clientHeight))>0?(h-(o.clientHeight))/2:0;
		}
	}

	jodaDisplay.centerDocument = function (obj) {
		var o=$(obj);
		if (o) {
			o.style.position="absolute";
			w=document.body.scrollWidth;
			h=document.body.scrollHeight;
			o.style.left=(w-(o.scrollWidth))/2>0?(w-(o.scrollWidth))/2:0;	
			o.style.top=(h-(o.scrollHeight))/2>0?(h-(o.scrollHeight))/2:0;
		}
	}

	jodaDisplay.showToolTip = function (content) {
		var l;
		var posimgsel;
		
	posimgsel=document.getElementById("tblopec");
	l=document.getElementById("ToolTip");
	l.innerHTML='<SPAN class="solitoblu">'+contenut+'</SPAN>';
	l.style.top=(event.srcElement.offsetTop+20)+'px';
	l.style.left=(posimgsel.offsetLeft+65)+'px';
	l.style.display="block"; //"inline"; //
	}

//*************************   AJAX   **************************
// Ajax
function AjaxAutoUpdater() {
   this.timer=null;
	this.ajax=null;
	this.object=null;
	
   this.parameters='';
	this.url='';
	this.operation='';
	
	AjaxAutoUpdater.prototype.setUpdater=function (object, url, parameters) {
	}
	
	AjaxAutoUpdater.prototype.update=function () {
		
	}
		
	AjaxAutoUpdater.prototype.start=function (interval) {
	   this.timer=setInterval (update, interval);  //passare l'istanza corretta dell'updater
	}

	AjaxAutoUpdater.prototype.stop=function () {
		clearInterval(this.timer);
	}
}

function Ajax() {
	var This=this;
	
	Ajax.prototype.getAjax = function () { 
		if (window.XMLHttpRequest) {
			return new XMLHttpRequest(); 
		} else if(window.ActiveXObject) {
			return new ActiveXObject("Microsoft.XMLHTTP"); //IE
		} else {
			alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
		}
	}
	
	Ajax.prototype.event = function() {
		if (This.connector.readyState == 4) {
			if (This.objectUpdate!=null)
				if (This.objectUpdate.name!=undefined) 
					This.objectUpdate.setContent(This.connector.responseText);
				else
					This.objectUpdate.innerHTML=This.connector.responseText;
			else {
				This.response=This.connector.responseText;
				if (This.callBack)
				   This.callBack(This);
			}
      }
	}
	
	Ajax.prototype.call = function() {
	   This.connector.open('POST', This.url, This.async);
		This.connector.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		if (This.async)
			This.connector.onreadystatechange = This.event;
      This.connector.send('function='+This.operation+'&'+This.parameters);
		if (!This.async)
		   if (This.objectUpdate!=null)
				This.objectUpdate.innerHTML=This.connector.responseText;
			else
				This.response=This.connector.responseText;
	}
	
	Ajax.prototype.update = function(object,url,operation,parameters) {
		if (url)
			This.url=url;
		if (operation)
			This.operation=operation;
		if (parameters)
			This.parameters=parameters;			
		This.objectUpdate=$(object);
		This.call();
	}
	
	Ajax.prototype.request = function(url,operation,parameters) {
		if (url)
			This.url=url;
		if (operation)
			This.operation=operation;
		if (parameters)
			This.parameters=parameters;	
		This.call();
		if (!This.async)
			return This.response;
	}
		
	This.callBack=null;
	This.connector=This.getAjax();
   This.parameters='';
	This.url='';
	This.async=true;
	This.operation='';
	This.response='';
	
	This.objectUpdate=null;
}

//logwin=window.open('','logwin','')
//function writelog(m) {
//	logwin.document.write(m+"\n")
//}

