
/*	AV Soft Web Site Javascript Core Functions v2.0  (cc) AV Soft 2009 */

var defaultErrorString = 'Si è verificato un errore nel recupero delle informazioni. Ci scusiamo per il disagio.';
var hider;

function PopWinProperty(){
	this.toolbar=0;this.location=0;this.directories=0;this.status=1;this.menubar=0;this.scrollbars=0;this.resizable=1;	
	this.print = function(){
		return "toolbar="+this.toolbar+",location="+this.location+",directories="+this.directories+",status="+this.status+"," +
    			"menubar="+this.menubar+",scrollbars="+this.scrollbars+",resizable="+this.resizable+"";
	};
};
var PopWin = new Object(); //property deve essere istanza di PopWinProperty
PopWin.showPopup = function(url, target, width, height, top, left, property){
	 window.open(url, target, "width=" + width + ",height=" + 
	    height + ",top=" + top + ",left=" + left + property.print);
};

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date(); var expire = new Date();
 if (nDays===null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
};
function ReadCookie(cookieName) {
 var theCookie=""+document.cookie; var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName==="") return "";
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
};

function isEmail(value){
	var valore = value.match(/\b(^(\S+@).+((\.com)|(\.it)|(\.aero)|(\.biz)|(\.coop)|(\.info)|(\.pro)|(\.museum)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if(valore){ return true; }
	else { return false; }
};

var Server = new Object();
Server.retrive = function(id_destinazione, page){
	$.ajax({ type: "GET", url: page, cache:false, dataType: 'html', error: function(){ Gui.showMessage(defaultErrorString, 'Errore'); },
	   success: function(msg) { $("#"+id_destinazione).html(msg); }, start: Gui.showWaiter(), complete: function(){setTimeout("Gui.hideWaiter()", 500)} });
};
Server.retrivePlus = function(destination, page, actionOnComplete){
	$.ajax({  
	   type: "GET", url: page, dataType: 'html',
	   error: function(){
	   	 Gui.hideWaiter();
		 Gui.showMessage(defaultErrorString, 'Errore');
	   },
	   success: function(msg) {
	   	 setTimeout("Gui.hideWaiter()", 500);
	     $("#"+destination).html(msg);
	   },
	   start: Gui.showWaiter(), complete: actionOnComplete
	 });
};
Server.showData = function(from_id, to_id){ $("#"+to_id).html($("#"+from_id).html()); };

var Gui = new Object();
Gui.showWaiter = function(){
	$("#waitbox").html('<b>Attendere, caricamento in corso...</b>');
	document.getElementById('waitbox').style.top = ((document.body.scrollTop>0)?(document.body.scrollTop):0);
	document.getElementById('waitbox').style.left = (document.body.clientWidth/2)-(450/2);
	Gui.show('waitbox', true);
};
Gui.hideWaiter = function(){ Gui.hide('waitbox', true); };
Gui.showMessage = function(msg, title){
	$("#oscuratore").fadeIn("fast");
	document.getElementById('oscuratore').style.top = ((document.body.scrollTop>0)?(document.body.scrollTop):0);
	(!title)?title= 'avsoft.it - Messaggio utente':0;
	$("#box_text").html(msg);
	$("#box_title").html(title);
	document.getElementById('box').style.top = (document.body.clientHeight/2)-100 + ((document.body.scrollTop>0)?(document.body.scrollTop):0);
	document.getElementById('box').style.left = (document.body.clientWidth/2)-200;
	$("#box").fadeIn("fast");
};
Gui.hideMessage = function(){ Gui.hide('oscuratore');Gui.hide('box', true); };
Gui.hide = function(id, animate){ (!animate)?$("#"+id).hide():$("#"+id).fadeOut("fast"); };
Gui.show = function(id, animate){ (!animate)?$("#"+id).show():$("#"+id).fadeIn("fast"); };
Gui.toggle = function(id){ $("#"+id).toggle(); };

var Context = new Object();
Context.caller='';
Context.objCaller=null;
Context.show = function(menu_id, attached_to, e, offsetLeft, offsetTop){
	if (window.event) event.cancelBubble=true;
	else if (e.stopPropagation) e.stopPropagation();
	if(Context.caller!=menu_id){
		Context.realHide();
		Context.caller=menu_id;
		document.getElementById(menu_id).style.zIndex = 99999;
		document.getElementById(menu_id).style.top =  Math.abs((attached_to.offsetTop + ((attached_to.offsetHeight==0)?32:attached_to.offsetHeight) + ((!offsetTop)?0:offsetTop)))+"px";
		document.getElementById(menu_id).style.left = Math.abs((attached_to.offsetLeft + ((!offsetLeft)?0:offsetLeft)))+"px";
		attached_to.setAttribute('id','sel');
		Context.objCaller = attached_to;
		$('#'+menu_id).slideDown("fast");
	}
	else{
		Context.realHide();
	}
};
Context.hide = function(){ hider=setTimeout('Context.realHide()',180); };
Context.abortHiding = function(){
	if (typeof hider!="undefined")
		clearTimeout(hider);
};
Context.realHide = function(){
	$('#'+Context.caller).slideUp("fast");
	if(Context.objCaller!==null)
		Context.objCaller.setAttribute('id','');
	Context.objCaller = null;
	Context.caller='';
};

var Pref = new Object();
Pref.setPreference = function(prefName, prefValue){
	SetCookie(prefName, prefValue, 300);
	location.reload();
};
Pref.setNoReload = function(prefName, prefValue){ SetCookie(prefName, prefValue, 300); };

function expandeOrCollapsePage(){
	Gui.toggle('expander');
	Gui.toggle('collapser');
	if($("#pageTable").attr('tag')=='little'){
		$("#pageTable").attr('tag', 'big');
		$("#pageTable").animate( { width:"98%"}, 1000);
		Pref.setNoReload('avsoft_pagewidth','98%');
	}
	else{
		$("#pageTable").attr('tag', 'little');
		$("#pageTable").animate( { width:"90%"}, 1000);
		Pref.setNoReload('avsoft_pagewidth','90%');
	}
};

function getposOffset(what, offsettype){
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!==null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	totaloffset+=(offsettype=="left")? ((document.body.scrollLeft)?(document.body.scrollLeft-totaloffset):0) : ((document.body.scrollTop)?(document.body.scrollTop-totaloffset):0);
	return totaloffset;
};
function getRealPageHeight(){
	var x,y;
	if (self.innerHeight){
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight){
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body){
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return y;
};

var Post = new Object();   
	Post.Send = function(form, dest_id, dest_page){
	 var query = Post.buildQuery(form);	 
		$.ajax({ type: "POST", url: dest_page, data: query, cache:false, dataType: 'html', error: function(){ Gui.showMessage(defaultErrorString, 'Errore'); },
		success: function(msg) { $("#"+dest_id).html(msg); }, start: Gui.showWaiter(),
		complete: function(){setTimeout("Gui.hideWaiter()", 500)} });
	};
	Post.AdvancedSend = function(form, dest_id, dest_page, actionOnComplete){
		 var query = Post.buildQuery(form);
		 $.ajax({  
		   type: "POST", url: dest_page, data: query, dataType: 'html', cache: false,
		   error: function(){
		   	 Gui.hideWaiter();
			 Gui.showMessage(defaultErrorString, 'Errore');
		   },
		   success: function(msg) {
		   	 setTimeout("Gui.hideWaiter()", 500);
		     $("#"+dest_id).html(msg);
		   },
		   start: Gui.showWaiter(), complete: actionOnComplete
		 });
	};  
	Post.buildQuery = function(form){ //form must have an id
		var formElements = form.elements;
		if(form.id==''){
			alert('Form must expose an ID property');
			return '';
		}
		return $('#'+form.id+'').serialize();
	};
	Post.buildQueryFromID = function(form_id){
		return $('#'+form_id+'').serialize();
	};

function downloadFile(indirizzo, tabella, id, ti){
	PopWin.showPopup('apo_v3/getfile.php?f='+indirizzo+'&t='+tabella+'&i='+id+'&ti='+ti+'','_blank',600,459,0,0, new PopWinProperty())
};
function downloadFile3(tabella, id, blobfield, ti){
	PopWin.showPopup('apo_v3/getfile_b.php?t='+tabella+'&i='+id+'&b='+blobfield+'&ti='+ti+'','_blank',600,459,0,0, new PopWinProperty())
};

function newOptionWithUserRequest(element, requestValue){
	if(requestValue == null){ requestValue = 'Inserisci testo'; }
	var optionValue= prompt(requestValue, '');
	element.options[element.options.length]=new Option(optionValue,optionValue, true, true);
};
