// JavaScript Document

// deve stare nella head della pagina per funzionare seguita da window.onresize=resize_iframe('idiframe); 
function resize_iframe(idframe) {
	var height=window.innerWidth; //Firefox
	if (document.body.clientHeight) {
		height=document.body.clientHeight; //IE
	}
	//resize iframe in base al size della window (tutto sulla stessa linea)
	document.getElementById(idframe).style.height=parseInt(height-document.getElementById(idframe).offsetTop)+"px";
}

function stripHTML(){
var re = /(<([^>]+)>)/gi;
for (i=0; i < arguments.length; i++)
arguments[i].value=arguments[i].value.replace(re, "")
}

/* THUMBNAIL */
var img = new Image();
function loadImage(fileName)
{
  img.src = fileName;
  document.preview.src = img.src; 
}

// browser detection
var d, dom, ie, ie4, ie5x, moz, mac, win, lin, old, ie5mac, ie5xwin, op;
d = document;
n = navigator;
na = n.appVersion;
nua = n.userAgent;
win = ( na.indexOf( 'Win' ) != -1 );
mac = ( na.indexOf( 'Mac' ) != -1 );
lin = ( nua.indexOf( 'Linux' ) != -1 );

if ( !d.layers ){
	dom = ( d.getElementById );
	op = ( nua.indexOf( 'Opera' ) != -1 );
	konq = ( nua.indexOf( 'Konqueror' ) != -1 );
	saf = ( nua.indexOf( 'Safari' ) != -1 );
	moz = ( nua.indexOf( 'Gecko' ) != -1 && !saf && !konq);
	ie = ( d.all && !op );
	ie4 = ( ie && !dom );

	/*
	ie5x tests only for functionality. ( dom||ie5x ) would be default settings. 
	Opera will register true in this test if set to identify as IE 5
	*/

	ie5x = ( d.all && dom );
	ie5mac = ( mac && ie5x );
	ie5xwin = ( win && ie5x );
}

function ShowDiv(divname) {
	boxGMap = document.getElementById(divname);
	if (boxGMap.style.display == 'block')
		boxGMap.style.display = 'none';
	else
		boxGMap.style.display = 'block';
	}

// calendar
function selected(cal, date) {
  cal.sel.value = date; 
  if (cal.dateClicked && false) 
    cal.callCloseHandler();
}

function closeHandler(cal) {
	cal.hide();
	_dynarch_popupCalendar = null;
}

function showCalendar(id, format, showsTime, showsOtherMonths) {
 
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    _dynarch_popupCalendar.hide();
  } else {
 
    var cal = new Calendar(1, null, selected, closeHandler);
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;
    cal.setRange(2009, 2015);
    cal.create();
  }
  
  _dynarch_popupCalendar.setDateFormat(format);
  _dynarch_popupCalendar.parseDate(el.value);
  _dynarch_popupCalendar.sel = el;
  _dynarch_popupCalendar.showAtElement(document.getElementById(id), 0);
  //cal.setDisabledHandler(disallowDate);
  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

function disallowDate(date) {
 var today = new Date();
 var d = 0 ;
 var dNOW = 0 ;
 
 dNOW = today.getFullYear()& today.getMonth()&today.getDate();
 d = date.getFullYear()&date.getMonth()&date.getDate() ;
  if (  d <dnow)  {
    return true; 
  }

  return true;
};
function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");
  var cal = new Calendar(0, null, flatSelected);
  cal.weekNumbers = false;
  cal.setDateFormat("%A, %B %e");
  cal.create(parent);
}
	function hide1(id){
		document.getElementById(id).style.visibility="hidden";
		}
	function Show1(id){
		document.getElementById(id).style.visibility="visible";
		}
		
function checkAll(id){
   var f = document.getElementById(id);
   var inputs = f.getElementsByTagName("input");
   for(var t = 0;t < inputs.length;t++){
     if(inputs[t].type == "checkbox")
       inputs[t].checked = true;
   }
 }
 function uncheckAll(id){
   var f = document.getElementById(id);
   var inputs = f.getElementsByTagName("input");
   for(var t = 0;t < inputs.length;t++){
     if(inputs[t].type == "checkbox")
       inputs[t].checked = false;
   }
 }
 function invertAll(id){
   var f = document.getElementById(id);
   var inputs = f.getElementsByTagName("input");
   for(var t = 0;t < inputs.length;t++){
     if(inputs[t].type == "checkbox")
       inputs[t].checked = !inputs[t].checked;
   }
 }	

function setCookie(NameOfCookie, value, expiredays) { 
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
function getCookie(NameOfCookie){
	if (document.cookie.length > 0){ 
		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1){ 
			begin += NameOfCookie.length+1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end));
		} 
	}
	return null; 
}
function unescapa(testo) {
	document.write(unescape(testo));
}

//Drag and Drop script - http://www.btinternet.com/~kurt.grigg/javascript

if  (document.getElementById){
(function(){
//Stop Opera selecting anything whilst dragging.
if (window.opera){
document.write("<input type='hidden' id='Q' value=' '>");
}

var n = 500;
var dragok = false;
var y,x,d,dy,dx;

function move(e){
if (!e) e = window.event;
 if (dragok){
  d.style.left = dx + e.clientX - x + "px";
  d.style.top  = dy + e.clientY - y + "px";
  
	setCookie(d.id, 'left:' + d.style.left + '; top:' +  d.style.top + ';', 5);
  
  return false;
 }
}

function down(e){
if (!e) e = window.event;
var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
 temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
 }
if (temp.className == "dragclass"){
 if (window.opera){
  document.getElementById("Q").focus();
 }
 dragok = true;
 temp.style.zIndex = n++;
 d = temp;
 dx = parseInt(temp.style.left+0);
 dy = parseInt(temp.style.top+0);
 x = e.clientX;
 y = e.clientY;
 document.onmousemove = move;
 return false;
 }
}

function up(){
dragok = false;
document.onmousemove = null;
}

document.onmousedown = down;
document.onmouseup = up;

})();
}

function Scambia(stringa, sost, rimpiazzo){
for (iCount = 0; iCount < sost.length; iCount++) {
	while (stringa.indexOf(sost[iCount]) != -1){
		stringa = stringa.replace(sost[iCount], rimpiazzo);
		}
	return(stringa);
	}
}

function filterInput(filterType, evt, allowDecimal, allowCustom){ 
/*
Numbers Only (No Decimal Point) onKeyPress="return filterInput(1, event)
Numbers Only (With Decimal Point)onKeyPress="return filterInput(1, event, true)
Alphabets Only onKeyPress="return filterInput(0, event)
Alphabet Numeric onKeyPress="return filterInput(2, event)
Alphabet Numeric (With Custom characters [ .]) onKeyPress="return filterInput(2, event, false, ' .')
Alphabet Numeric (With Custom characters [@ _ .])onKeyPress="return filterInput(2, event, false, '@_-.')
*/

var keyCode, Char, inputField, filter = ''; 
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 
    var num   = '0123456789'; 
    // Get the Key Code of the Key pressed if possible else - allow 
    if(window.event){ 
        keyCode = window.event.keyCode; 
        evt = window.event; 
    }else if (evt)keyCode = evt.which; 
    else return true; 
    // Setup the allowed Character Set 
    if(filterType == 0) filter = alpha; 
    else if(filterType == 1) filter = num; 
    else if(filterType == 2) filter = alpha + num; 
    if(allowCustom)filter += allowCustom; 
    if(filter == '')return true; 
    // Get the Element that triggered the Event 
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget; 
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow 
    if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27) )return true; 
    // Get the Pressed Character 
    Char = String.fromCharCode(keyCode); 
    // If the Character is a number - allow 
    if((filter.indexOf(Char) > -1)) return true; 
    // Else if Decimal Point is allowed and the Character is '.' - allow 
    else if(filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)return true; 
    else return false; 
}

function Mid(String, Start, Length)
{
    if (String == null)
        return (false);
    
    if (Start > String.length)
        return '';
    
    if (Length == null || Length.length == 0)
        return (false);
    
    return String.substr((Start - 1), Length);
}

function InStr(String1, String2)
{
    var a = 0;
	
    if (String1 == null || String2 == null)
        return (false);
    
    String1 = String1.toLowerCase();
    String2 = String2.toLowerCase();
    
    a = String1.indexOf(String2);
    if (a == -1)
        return 0;
    else
        return a + 1;
}

function Len(string)
{
    if (string == null)
        return (false);
    
    return String(string).length;
}

// colorpicker
function MakeColor(tipo,id,ThisColor) {

// <a href="#" language="javascript" onmouseover="return MakeColor(0,'colortext','#<%=Color%>');" onClick="document.getElementById('testo').style.display = 'none';">

	if (tipo == 0) {
		document.getElementById(id).value = ThisColor;
		document.getElementById(id).style.color = ThisColor;
	} else {
		document.getElementById(id).value = ThisColor;
		document.getElementById(id).style.backgroundColor  = ThisColor;
	}
}

function colorpicker(tipo,ThisColor) {
	if (tipo == 0) {
		document.getElementById('colortext').value = ThisColor;
		document.getElementById('colortext').style.color = ThisColor;
		document.getElementById('colorback').style.color = ThisColor;
	} else {
		document.getElementById('colorback').value = ThisColor;
		document.getElementById('colortext').style.backgroundColor = ThisColor;
		document.getElementById('colorback').style.backgroundColor = ThisColor;
	}
}

//End.

function ismaxlength(obj) {
	// maxlenght per le textarea
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}

// paginazione risultati ricerca
function PagePrev(valore) {
	return parseInt(valore)-1;
	}
function PageNext(valore) {
	return parseInt(valore)+1;
}

// slidepop
var browser = navigator.userAgent.toLowerCase();
var eIE6    = (browser.indexOf("msie 6") > -1) ? true : false;

var pixel = 10;
var millisec = 10;
var curpos;
var nuopos;

function hidePop(){
	if (eIE6){
        document.getElementById('slidepop').style.display = "none";
        document.getElementById('slidepop').style.visibility="hidden";
        document.getElementById('popchiuso').style.display = "block";
        document.getElementById('popchiuso').style.visibility="visible";
    }else{
        document.getElementById('slidepop').style.right = "-306px";
        document.getElementById('slidepopcont').style.visibility="hidden";
        document.getElementById('showpop').style.visibility="visible";
    }
}

function showPop(){
    var expire=new Date();
    expire.setDate(expire.getDate()+90);
    document.cookie = "ah_pod=visible;path=/;expires=" + expire.toGMTString();

    if ( eIE6 ){ // IE6
        document.getElementById('popchiuso').style.display = "none";
        document.getElementById('popchiuso').style.visibility="hidden";
        document.getElementById('slidepop').style.display = "block";
        document.getElementById('slidepop').style.visibility="visible";
    }else{
        document.getElementById('showpop').style.visibility="hidden";
        document.getElementById('slidepopcont').style.visibility="visible";
        slideIn( '-304' );
    }
}

function slideOut(pos){
    var expire=new Date();
    expire.setDate(expire.getDate()+90);
    document.cookie = "ah_pod=hidden;path=/;expires=" + expire.toGMTString();

    if ( eIE6 ){
        document.getElementById('slidepop').style.display="none";
        document.getElementById('slidepop').style.visibility="hidden";
        document.getElementById('popchiuso').style.display="block";
        document.getElementById('popchiuso').style.visibility="visible";
    }else{
        curpos = pos;
        nuopos = curpos - pixel;
        if ( nuopos > -310 ){ 
           document.getElementById('slidepop').style.right = nuopos + "px";
           setTimeout("slideOut(nuopos)", millisec);
        }else{
            document.getElementById('slidepopcont').style.visibility="hidden";
            document.getElementById('showpop').style.visibility="visible";
        }
    }
}
function slideIn(pos){
    curpos = pos;
    nuopos = eval(pixel+curpos);
    if ( nuopos  < 20 ){ 
       document.getElementById('slidepop').style.right = nuopos + "px";
       setTimeout("slideIn(nuopos)",millisec);
    }
}
// captcha
function RefreshImage(valImageId,palette) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString() + '&bmp=' + palette;
}

function disableSelection(element)
{
if (typeof(element.onselectstart)!="undefined") // IE
	element.onselectstart=function(){ return false; }
else if (typeof(element.style.MozUserSelect)!="undefined") // Firefox
	element.style.MozUserSelect="none";
else // Altri (es: Opera)
	element.onmousedown=function(){ return false; }
	element.style.cursor = "default";
}

function showFlash(video,width,height)
{
	document.write('<object type="application/x-shockwave-flash" data="'+video+'" width="'+width+'" height="'+height+'">');
	document.write('<param name="movie" value="'+video+'">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('</object>');
}

function catchError() { return true; }	// call: window.onerror = catchError;

function ShowPdf(pdf) {
	sRef = 'pdfWindow';
  wnd = window.open(pdf, 'pdfWindow', 'menubar=no,toolbars=yes,resizable=yes,status=no,location=no,width=550,height=360,');
  if (wnd) wnd.focus();
  return true;
}

function PlaySound(soundObj) {
  var sound = document.getElementById(soundObj);
  sound.Play();
  // <embed src="success.wav" autostart="false" width="0" height="0" id="sound1" enablejavascript="true">
}

