﻿/***************************************************/
//AUTOTAB
//Begin
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e, strTipo) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
	//visto che il controllo è per le date
	//faccio i controlli sul tipo
	switch(strTipo)
	{
		case 'gg':
			if(input.value > 31)
			{
				input.value = '31';
			}
			if(input.value == '00')
			{
				input.value = '01';
			}
			if(input.value == '0')
			{
				input.value = '01';
			}
			if(input.value == '0 ')
			{
				input.value = '01';
			}
			break;
		case 'mm':
			if(input.value > 12)
			{
				input.value = '12';
			}
			if(input.value == 0)
			{
				input.value = '01';
			}
			break;
		case 'yyyy':
			if(input.value > 2100)
			{
				input.value = '2000';
			}
			if(input.value < 1901)
			{
				input.value = '1901';
			}
			if(input.value == 0)
			{
				input.value = '01';
			}
			break;
	}
}
function containsElement(arr, ele) {
	var found = false, index = 0;
	while(!found && index < arr.length)
	if(arr[index] == ele)
	found = true;
	else
	index++;
	return found;
}
function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
	if (input.form[i] == input)index = i;
	else i++;
	return index;
}
return true;
}
//End

/***************************************************/
//funzioni per gestire l'uscita
g_blnCheckUnload = true;     // di default fa la domanda
function RunOnBeforeUnload(){
	if (g_blnCheckUnload) {
		window.event.returnValue = ('ATTENZIONE ABBANDONANDO LA FINESTRA NON VERRA SALVATO NESSUN DATO');
	}
}
function bypassCheck(){
	g_blnCheckUnload  = false;
}

function exitForm()
{
	history.go(document.getElementById("txtRedirect").value);
}

function backUrl()
{
	location.href = document.getElementById("txtBackUrl").value;
}



var allGetter = function () {
   var a = this.getElementsByTagName("*");
   var node = this;
   a.tags = function (sTagName) {
      return node.getElementsByTagName(sTagName);
   };
   return a;
};

function confirmExit(){
	return(confirm("Are you sure that you want to leave without checking something?"))
}

function goback(strUrl){
	//history.go(intNumber);
	location.href = strUrl;
}

function callOnClickOfLinkByName(linkName){
    if(document.links){
        var lnks = document.links;
        for(var c = 0; c < lnks.length;c++){
            if(lnks[c].name == linkName){
                lnks[c].onclick();
                return;
            }
        }
    }
}

function callOnClickOfLinkByNameTop(linkName){
	if(parent.frames[0].document.links){
        var lnks = parent.frames[0].document.links;
		lnks.namedItem(linkName).click();     
    }
}

function printPage(){
	window.print();  
}


if(navigator.appVersion.indexOf('MSIE') == -1){
	HTMLDocument.prototype.__defineGetter__("all", allGetter);
	HTMLElement.prototype.__defineGetter__("all", allGetter);
}

function openWindowFeatures(sUrl, sName, sFeatures){
	newwindow=window.open(sUrl,sName,sFeatures);
}

function openLocation(sUrl){
	location.href = sUrl;
}

function openWindow(sUrl, width, height){
	// *************************************************************
	//  Apre una finestra senza lacun pulsante
	// *************************************************************
	newwindow=window.open(sUrl,"newWindow","scrollbars=yes,menubar=no,resizable=no, width=" + width + ", height="+ height);
	newwindow.focus();
}

function openWindowNoBar(sUrl, width, height){
	// *************************************************************
	//  Apre una finestra senza lacun pulsante
	// *************************************************************
	newwindow=window.open(sUrl,"newWindow","scrollbars=no,menubar=no,resizable=no, width=" + width + ", height="+ height);
	newwindow.focus();
}

function openWindowNoBarName(sUrl, sName, width, height){
	// *************************************************************
	//  Apre una finestra senza lacun pulsante
	// *************************************************************
	newwindow=window.open(sUrl,sName,"scrollbars=no,menubar=no,resizable=no, width=" + width + ", height="+ height);
	newwindow.focus();
}

function openWindowName(sUrl, sName, width, height){
	// *************************************************************
	//  Apre una finestra senza lacun pulsante, con il nome centrata
	// *************************************************************
	leftVal = (screen.width / 4);
    topVal = (screen.height / 4);
	newwindow=window.open(sUrl,sName,"status=yes,scrollbars=yes,menubar=no,resizable=no, width=" + width + ", height="+ height);
	newwindow.focus();
}

function cmsConfirmAct(strDESC, strPage) {
//Chiede la conferma prima di eseguire un'azione, se confermato redirect su pagina strPAGE
	if (confirm(strDESC)) {
		window.location.href = strPage;		
		//window.location.href= strPage;
	}
}

function cmsConfirmActBool(strDESC) {
	return confirm(strDESC)
}

function actionConfirm(strDESC) {
	bypassCheck();
	return confirm(strDESC)
}

function cmsConfirmActForm(strDESC, strPage, objForm) {
//Chiede la conferma prima di eseguire un'azione, se confermato redirect su pagina strPAGE
	if (confirm(strDESC)) {
		objForm.action = strPage;
		objForm.submit();
		//window.location.href = strPage;		
		//window.location.href= strPage;
	}
}


function changeRowClass(objRow){
		switch(objRow.className){
			case 'tableRow':
				objRow.className ='tableRowSelected';
				break;
			case 'tableRowSelected':
				objRow.className ='tableRow';
				break;
		}
}

function changeVisibleClass(objRow){
		switch(objRow.className){
			case 'inputVisible':
				objRow.className ='inputHidden';
				break;
			case 'inputHidden':
				objRow.className ='inputVisible';
				break;
		}
}

function selectAllCheckBox(objCheckboxFather, strNameCheckbox){
	objCheck = document.all.tags("input");
	for (i=0; i<objCheck.length; i++) {
		if(objCheck[i].getAttribute('type') == 'checkbox' && objCheck[i].getAttribute('id') == strNameCheckbox){
			switch(objCheckboxFather.checked){
				case true:
					objCheck[i].checked = true;
					break;
				case false:
					objCheck[i].checked = false;
					break;
			}
		}
	}
}

function hrmChangeChildTab(objRow){
	switch(objRow.className){
		case 'tableTab':
			objRow.className = 'inputHidden';
			break;
		case 'inputHidden':
			objRow.className = 'tableTab';
			break;
	}
}

function hrmChangeFatherTab(objRow){
	switch(objRow.className){
		case 'top-tab-active':
			objRow.className = 'top-tab-inactive';
			break;
		case 'top-tab-inactive':
			objRow.className = 'top-tab-active';
			break;
	}
}

function setTabLebelOff(objRow){
	objRow.className = 'top-tab-inactive';
}

function setTabLebelOn(objRow){
	objRow.className = 'top-tab-active';
}

function setTabSchedaOff(objRow){
	objRow.className = 'inputHiddenTab';
}

function setTabSchedaOn(objRow){
	objRow.className = 'tableTab';
}


function changeTabLabel(objID, objRow){
        //prima imposto tutti i label non attivi
		for (i=0; i<divColl.length; i++) {
			if (divColl[i].className == "top-tab-active") {
			    alert(divColl[i].className);
			    setTabLebelOff(divColl[i])
			}	
		}
		//imposto questa accesa
		setTabLebelOn(objRow)
		
		//cerco il figlio e lo attivo
		divColl=document.all.tags("TD");
		for (i=0; i<divColl.length; i++) {
			if (divColl[i].className == "tableTab") {
				setTabSchedaOff(divColl[i])
			}	
		}
		setTabSchedaOn(document.getElementById(objID + 'Child'));
}


function checkOnSelect(objSelect, strValue){
	for(i=0;i<objSelect.length;i++){
		if(objSelect.options[i].value == strValue){
			objSelect.options[i].selected = true;
		}
	}
}


function controllaEMAIL(indirizzo) {
  if (window.RegExp) {
    var nonvalido = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var valido = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    var regnv = new RegExp(nonvalido);
    var regv = new RegExp(valido);
    if (!regnv.test(indirizzo) && regv.test(indirizzo))
      return true;
    return false;
	}
  else {
    if(indirizzo.indexOf("@") >= 0)
      return true;
    return false;
  	}
}


/* Altro controllo delle date */
function y2k(number)
{ 
	return (number < 1000) ? number + 1900 : number;
}

function isDateSimple(day,month,year) {
// checks if date passed is valid
// will accept dates in following format:
// isDate(dd,mm,ccyy), or
// isDate(dd,mm) - which defaults to the current year, or
// isDate(dd) - which defaults to the current month and year.
// Note, if passed the month must be between 1 and 12, and the
// year in ccyy format.
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
	month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
        return true;
    else
        return false
}

/* Altro controllo delle date */


/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
if(dtStr != ""){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Inserire una data in formato corretto : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Inserire mese corretto")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Inserire giorno corretto")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Inserire un anno corretto")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Inserire una data in formato corretto : dd/mm/yyyy")
		return false
	}
}
return true
}

/**************************************************/

// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function URLEncode(strText)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = strText;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

// Gestione Tab

function showTab(tab,label) { // mostra Tab e rispettiva label
        hideAllTab();
        document.getElementById(tab).style.display='block';
        document.getElementById(label).className='TabLabel_active';
    }
    
    function hideAllTab() { // nasconde tutti i tab e tutte le label
    
    var myObjColl = getElementsByClassName('Tab');
    for (var i = 0, j = myObjColl.length; i < j; i++) {
       myObjColl[i].style.display='none';
    }
    
    var myObjLabelColl = getElementsByClassName('TabLabel_active|TabLabel');
    for (var i = 0, j = myObjLabelColl.length; i < j; i++) {
       myObjLabelColl[i].className='TabLabel';
    }
    }
    
    //trova un elemento partendo dal nome della classe
    function getElementsByClassName(strClass, strTag, objContElm) {
      strTag = strTag || "*";
      objContElm = objContElm || document;
      var objColl = objContElm.getElementsByTagName(strTag);
      if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
      var arr = new Array();
      var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
      var arrClass = strClass.split(delim);
      for (var i = 0, j = objColl.length; i < j; i++) {
        var arrObjClass = objColl[i].className.split(' ');
        if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
        var c = 0;
        comparisonLoop:
        for (var k = 0, l = arrObjClass.length; k < l; k++) {
          for (var m = 0, n = arrClass.length; m < n; m++) {
            if (arrClass[m] == arrObjClass[k]) c++;
            if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
              arr.push(objColl[i]);
              break comparisonLoop;
            }
          }
        }
      }
      return arr;
    }
    
    
 
function ClickTopTab(id,dfl,url_main,url_menu)
{
    //disattivo tutti i tab
    var myObjColl = getElementsByClassName('toptab_active');
    for (var i = 0, j = myObjColl.length; i < j; i++) {
        myObjColl[i].className='toptab_inactive';
    }

    //attivo quello cliccato
    document.getElementById('top' + id).className='toptab_active';

    //nascondo tutti i menu di secondo livello
    var myObjLabelColl = getElementsByClassName('undertab');
    for (var i = 0, j = myObjLabelColl.length; i < j; i++) {
       myObjLabelColl[i].style.display='none';
    }
    
    //attivo quello corrispondente  
    document.getElementById('tab' + id).style.display='block';
    
    //attivo il menu di secondo livello di default
    //nascondo tutti gli elementi del menu di secondo livello
    var myObjLabelColl = getElementsByClassName('undertab_active');
    for (var i = 0, j = myObjLabelColl.length; i < j; i++) {
       myObjLabelColl[i].className='undertab_inactive'; 
    }
    
    //attivo quello corrispondente  
    LoadUnderTab(dfl,url_main,url_menu);  
}  

function hideUnderTab()
{
    //nascondo tutti i menu di secondo livello
    var myObjLabelColl = getElementsByClassName('undertab');
    for (var i = 0, j = myObjLabelColl.length; i < j; i++) {
       myObjLabelColl[i].style.display='none';
    }
}  

function LoadUnderTab(id_under,url_main,url_menu)
{

    //nascondo tutti gli elementi del menu di secondo livello
    var myObjLabelColl = getElementsByClassName('undertab_active');
    for (var i = 0, j = myObjLabelColl.length; i < j; i++) {
       myObjLabelColl[i].className='undertab_inactive'; 
    }
    
    //attivo quello corrispondente  
    if (id_under!='') {
    parent.frames['topFrame'].document.getElementById('under' + id_under).className='undertab_active'; 
    }
        
    //carico il menu corrispondente
    parent.frames['mainFrameMenu'].location.href=url_menu;
    
    //carico l'index corrispondente  
    parent.frames.mainFrame.location.href=url_main; 
    
    
}    

function LoadUnderTabFromMain(id_under,url_main,url_menu)
{

    //nascondo tutti gli elementi del menu di secondo livello
    var myObjLabelColl = getElementsByClassName('undertab_active','*',parent.frames['topFrame'].document);
    for (var i = 0, j = myObjLabelColl.length; i < j; i++) {
       myObjLabelColl[i].className='undertab_inactive'; 
    }
    
    //attivo quello corrispondente  
    if (id_under!='') {
    parent.frames['topFrame'].document.getElementById('under' + id_under).className='undertab_active'; 
    }
        
    //carico il menu corrispondente
    parent.frames['mainFrameMenu'].location.href=url_menu;
    
    //carico l'index corrispondente  
    parent.frames.mainFrame.location.href=url_main; 
    
    
}   

function closeInfoBox()
{
    document.getElementById('pnlInfoBox').style.display='none';
}

//Nella console di amministrazione verifico quale tab e' selezionato, se non presente seleziono il primo
function checkTab() {
    var t = parseInt(Request.QueryString("TAB"));
    if (isNaN(t)) t = 1; //Di default apro il primo tab
    showTab('tab' + t,'tablabel' + t);
}

function OpenPopUpGallery(sUrl){
	// *************************************************************
	//  Apre una finestra senza lacun pulsante, con il nome centrata
	// *************************************************************
	newwindow=window.open(sUrl,'PopUpGallery','status=no,scrollbars=no,menubar=no,resizable=no, width=0, height=0,top=0,left=0' );
	newwindow.focus();
}