/**************************************************************
Project:       ARCAWEB JS LIBRARY
Version:       16.08.2011 11:58:13
**************************************************************/

function gd(el){
	return document.getElementById(el);
}

function toggle(elid){
	if (gd(elid) && gd(elid).style.display != 'none'){
		gd(elid).style.display = 'none';
	} else {
		gd(elid).style.display = 'block';
	}
}

function setaudio(mp3, srcel){
	//gd('audioplayer').innerHTML = '';
	var player = '<!--[if !IE]> --><object type="application/x-shockwave-flash" data="swf/player.swf?audiourl=swf/soundtrack.mp3" width="1" height="1"><!-- <![endif]--><!--[if IE]><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="1" height="1"><param name="movie" value="swf/player.swf?audiourl=swf/soundtrack.mp3" /><!--><!--dgx--><param name="menu" value="false" /></object><!-- <![endif]-->';
	gd('audioplayer').innerHTML = player;
	gd('player').innerHTML = '<img src="img/sound_on.png" onclick="stopaudio(\'soundtrack.mp3\',this);" style="cursor:pointer;position:absolute;right:0px;top:3px;" alt="" />';
}

function stopaudio(srcel){
	gd('audioplayer').innerHTML = '';
	gd('player').innerHTML = '<img src="img/sound_off.png" onclick="setaudio(\'soundtrack.mp3\',this);" style="cursor:pointer;position:absolute;right:0px;top:3px;" alt="" />';
}

show = function(objid, srcobj){
	gd(objid).style.display = 'block';
	if (srcobj){
		gd(objid).style.left = fpos(srcobj)[0]-fpos(srcobj.parentNode)[0]+'px';
	}
}
	
hide = function(objid){
	gd(objid).style.display = 'none';
}


function wopen(wdt,hgt,durl){
	var left = (screen.availWidth/2) - (wdt/2);
	var top = (screen.availHeight/2) - (hgt/2);
	var dwin = window.open(durl,'Insert','width='+wdt+',height='+hgt+', left='+left+', top='+top);
	dwin.focus();
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function fpos(obj) {
	if (obj){
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
			return [curleft,curtop];
		}
	}
	return false;
}

function check_email(eml){
	return (eml.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) != -1) ? true : false;
}

function xget(x_url,target_id) {

	if (x_url){
		this.req = new XMLHttpRequest();
		req.open('GET', x_url, true);
		if (target_id && gd(target_id)){
			req.onreadystatechange = function (aEvt) {
				if (req.readyState == XMLHttpRequest.DONE) {
					if (gd(target_id).tagName.toLowerCase() == 'input'){
						gd(target_id).value = req.responseText;
					} else {
						gd(target_id).innerHTML = req.responseText;
					}
				}
			}
		}
		req.send(null);
	}
}

function disableSelection(target){
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return false}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="none"
	else //All other route (ie: Opera)
		target.onmousedown=function(){return false}
	target.style.cursor = "default"
}

function getParentByTagName(obj, tagName) {
	tagName = tagName.toLowerCase();
	while (obj!= null && obj.tagName!=null && obj.tagName.toLowerCase() != tagName) {
		obj=obj.parentNode;
	}
	return obj;
}


function supportsBoxShadow() {
  var s = document.body.style;
  return s.WebkitBoxShadow !== undefined || s.MozBoxShadow !== undefined;
}

gotourl = function(targeturl, new_window){
	if (targeturl){
		if (new_window){
			window.open(targeturl,'','');
		} else {
			window.location.href = targeturl;
		}
	}
	return false;
}

reloadpage = function(){
	window.location.reload();
	return false;
}

goback = function(targeturl){
	window.history.back();
	return false;
}

urlconfirm = function(targeturl, conf_message){
	if (confirm(conf_message)){
		gotourl(targeturl);	
	}
}

function floatdiv(_id, _x, _y, _w, _h, _z, _a){
	
	this._x = _x;
	this._y = _y;
	this._w = _w;
	this._h = _h;
	this._z = _z ? _z : 99;
	this._a = _a;
	this._obj = false;	
	this._id = _id;
	
	this.create = function(){
		this._obj = document.createElement('div');
		this._obj.id = this._id;
		with (this._obj.style){
			position = 'absolute';
			left = this._x + 'px';
			top = this._y + 'px';
			width = this._w + 'px';
			height = this._h + 'px';
			zIndex = this._z;
		}
		document.body.appendChild(this._obj);
	}
	
	this.destroy = function(){
		document.body.removeChild(this._obj);
	}
	
	this.setbg = function(bg){
		this._obj.style.background = bg;
	}
	
	this.move = function(mx, my){
		this._x = mx;
		this._y = my;
		this.updatepos();
	}
	
	this.resize = function(mw, mh){
		this._w = mw;
		this._h = mh;
		this.updatepos();
	}
	
	this.updatepos = function(){
		with (this._obj.style){
			left = Math.round(this._x) + 'px';
			top = Math.round(this._y) + 'px';
			width = Math.round(this._w) + 'px';
			height = Math.round(this._h) + 'px';
			zIndex = this._z;
		}
	}
	
	this.show = function(){
		this._obj.style.display = 'block';
	}
	
	this.hide = function(){
		this._obj.style.display = 'none';
	}
	
}

function is_numeric(sText){
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++){ 
	  Char = sText.charAt(i); 
	  if (ValidChars.indexOf(Char) == -1) {
	    IsNumber = false;
	  }
	}
	return IsNumber;
}

function postForm(form_id, target_url, callback){

	if (!target_url) {
		target_url = '/';
	}

  var x_form = document.getElementById(form_id);
  if (!x_form){
  	alert('Form inesistente: '+form_id);
  	return;
  }
  
  var x_form_len = x_form.elements.length;
  var x_encoded_data = "";
  var endchar;
  for(var i = 0; i < x_form_len; i++){
    if(i < x_form_len-1) {
    	endchar = '&';
    } else {
    	endchar = '';
    }
    if (x_form.elements[i].name != ''){
    	if (x_form.elements[i].type == 'checkbox'){
	     	x_encoded_data += x_form.elements[i].name+"="+encodeURIComponent(x_form.elements[i].checked)+endchar;
	    } else {
    		x_encoded_data += x_form.elements[i].name+"="+encodeURIComponent(x_form.elements[i].value)+endchar;
    	}
    }
  }
	
  this.req = new XMLHttpRequest();
	req.open('POST', target_url, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", x_encoded_data.length);
	req.setRequestHeader("Connection", "close");
	req.onreadystatechange = function (aEvt) {
		if (req.readyState == 4) {
		  if(req.status != 200){
				alert('Errore '+ req.status +', salvataggio non riuscito');
			} else {
				var resp = req.responseText;
				if (callback){
					setTimeout(callback, 200);	
				}
			}
		}
	}
	req.send(x_encoded_data);
	
}



window.onload = function(){

	try {
  		document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
	
	
	var formelements = new Array();
	
	var allinput = document.getElementsByTagName('input');
	for(var i = 0; i < allinput.length; i++)
		formelements.push(allinput[i]);
	var alltextareas = document.getElementsByTagName('textarea');
	for(var i = 0; i < alltextareas.length; i++)
		formelements.push(alltextareas[i]);
	var allselects = document.getElementsByTagName('select');
	for(var i = 0; i < allselects.length; i++)
		formelements.push(allselects[i]);
	
	//var formelements = allinput.concat(alltextareas, allselects)
	for(var i = 0; i < formelements.length; i++){
		//alert(formelements[i].type);
		if (formelements[i].type == 'textarea' || formelements[i].type == 'text' || formelements[i].type == 'password' || formelements[i].type == 'select-one'){
			formelements[i].onfocus = function(){
				this.style.borderColor = '#999999';
			}
			formelements[i].onblur = function(){
				this.style.borderColor = '';
			}
			if (formelements[i].id.indexOf('autofocus') >= 0){
				formelements[i].focus();
				try {
					formelements[i].select();
				} catch(err) {
					// none
				}
			}
		}
		if (formelements[i].className == 'button' ){
			/*formelements[i].onfocus = function(){
				this.blur();
			}*/
			formelements[i].onmouseover = function(){
				this.className = 'buttonhover';
			}
			formelements[i].onmousedown = function(){
				this.className = 'buttondown';
			}
			formelements[i].onmouseup = function(){
				this.className = 'button';
			}
			formelements[i].onmouseout = function(){
				this.className = 'button';
			}
		}
		
	}
	
}

