﻿var ffz_IntervalObj=null;
var ValidateError_AlfaNSPText="";
var ValidateError_AlfaNText="";
var ValidateError_AlfaNWSPText="";
var ValidateError_SimpleText="";
var ValidateError_NoHTMLText="";
var ValidateError_URLText="";
var ValidateError_MaxLength="";

var ffz_WinObjRef=new Array();
var WinObjRef_InnerEdit=0;

function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}
function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isBoolean(a) {
    return typeof a == 'boolean';
}
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isNull(a) {
    return a === null;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
} 

function ffz_GetInt(str,defaultval)
{
    try{
        var val = parseInt(str);
        if(isNaN(val)) return defaultval;
        return val;
    }
    catch(ex) {
        alert(ex);
        return defaultval;
    }
}
function ffz_GetIntGrDigitStr(str,defaultval,sep)
{
    try{
        if(sep==".") sep="\\"+sep;
        var rxp = new RegExp(sep, "gi");
        str=str.replace(rxp,'');
        return ffz_GetInt(str,defaultval);
    }
    catch(ex) {
        alert(ex);
        return defaultval;
    }
}
function ffz_GetStrGrDigitInt(val,sep)
{
    try{
        var str=""+val;
        var absval=(val<0)?0-val:val;
        if(absval>=1000)
        {
            var end=str.length;
            var strout=str.substring(end-3,end);
            if(absval>=1000000)
            {
                strout=str.substring(end-6,end-3)+sep+strout;
                if(absval>=1000000000)
                {
                    strout=str.substring(end-9,end-6)+sep+strout;
                    strout=str.substring(0,end-9)+sep+strout;
                }
                else
                    strout=str.substring(0,end-6)+sep+strout;
            }
            else
                strout=str.substring(0,end-3)+sep+strout;
            return strout;    
        }
        return str;
    }
    catch(ex) {
        alert(ex);
        return val;
    }
}

// DHTML micro API from http://www.quirksmode.org/js/dhtmloptions.html
function getObjAPI(name) {
 if (document.getElementById) { // For Level 1 DOM (Netscape 6, Explorer 5)
  return document.getElementById(name);
 }
 else if (document.all) { // For IE4 DOM
  return document.all[name];
 }
 else if (document.layers) { // For NS4 DOM
  var obj = getObjNN4API(document,name);
  obj.style = obj;
  return obj;
 }
 return null;
}

function getObjNN4API(obj,name) {
 var x = obj.layers;
 var foundLayer;
 for (i=0;i<x.length;i++) {
  if (x[i].id == name)
   foundLayer = x[i];
  else if (x[i].layers.length)
   var tmp = getObjNN4(x[i],name);
  if (tmp) foundLayer = tmp;
 }
 return foundLayer;
}


function ffz_GetObj(objid)
{
    return getObjAPI(objid);
}
function ffz_StopPropagation()
{
    if(window.event) window.event.returnValue= false;
    if(typeof ev == 'undefined') return false;
	else if(ev && ev.preventDefault) ev.preventDefault();
	else if(ev && ev.stopPropagation) ev.stopPropagation();
	else return false;
}
function ffz_AlertConferma(msg)
{
    if((""+msg).indexOf("/")>=0) 
        msg=(""+msg).replace(new RegExp("/","g"),"");
        
    var x=window.confirm(msg);
 	if(x==false)
    { 
        ffz_StopPropagation();
        return false;
	}
	return true;
}
function ffz_SetInnerText(labelid,msg)
{
    var obj=ffz_GetObj(labelid);
    
    if(obj!=null)
    {
        msg=(""+msg).replace(new RegExp("/","g"),"");
        while (obj.hasChildNodes()) {
		        obj.removeChild(obj.firstChild);
	    }
	    if(msg!=null && msg.length>0)
	    {
	        //obj.appendChild(document.createTextNode( msg ));
	        obj.innerHTML=msg;
	        obj.style.visibility="visible";
	    }
	    else
	    {
	        obj.style.visibility="hidden";
	    }
	}
    
}
function ffz_SetLabel(labelvar,msg)
{
    eval(labelvar+"=\""+msg+"\"");
}
function ffz_FormatCifre(valore,cifre)
{
    var v=""+valore;
    var x="";
    for (var i=v.length;i<cifre;i++)
    {
        x+="0";
    }
    return x+valore;
}
function ffz_DeFormatCifre(valore)
{
    return valore.replace(/^0*/g,"");
}

function ffz_Open(strUrl, strWindowName, WinObjIndex)
{
    if(ffz_WinObjRef[WinObjIndex] == null || ffz_WinObjRef[WinObjIndex].closed)
    {
        ffz_WinObjRef[WinObjIndex] = window.open(strUrl, strWindowName,
           "resizable=yes,menubar=yes,toolbar=yes,location=yes,scrollbars=yes,status=yes", true);
    }
    else
    {
        ffz_WinObjRef[WinObjIndex].focus();
    }
}

function ffz_OpenX(strUrl, strWindowName)
{
    var ref = window.open(strUrl, strWindowName,
           "resizable=yes,menubar=yes,toolbar=yes,location=yes,scrollbars=yes,status=yes", true);
    ref.focus();
    return ref;
}

// CONTROLS -----------------------------------------------

function NUD_Tool(fun,objid,value,minval,maxval,defaultval,dotted,sep,f)
{
    var obj=ffz_GetObj(objid);
    NUD_ToolObj(fun,obj,value,minval,maxval,defaultval,dotted,sep,f);
}
function NUD_ToolObj(fun,obj,value,minval,maxval,defaultval,dotted,sep,f)
{
    try
    {
        var val=0;
        if(fun=='add')
        {
            if(dotted) val=ffz_GetIntGrDigitStr(obj.value,defaultval,sep)+value;
            else val=ffz_GetInt(obj.value,defaultval)+value;
        }
        if(fun=='sub')
        {
            if(dotted) val=ffz_GetIntGrDigitStr(obj.value,defaultval,sep)-value;
            else val=ffz_GetInt(obj.value,defaultval)-value;
        }
        if(fun=='set')
        {
            val=value;
        }
        if(fun=='validate')
        {
            if(dotted) val=ffz_GetIntGrDigitStr(obj.value,defaultval,sep);
            else val=ffz_GetInt(obj.value,defaultval);
        }
        NUD_SetValidatingObj(obj,val,minval,maxval,defaultval,dotted,sep);
        
        if(f==1)
        {
            if(ffz_IntervalObj!=null) 
                clearInterval(ffz_IntervalObj);
            ffz_IntervalObj = setInterval("NUD_Tool('"+fun+"','"+obj.id+"',"+value+","+minval+","+maxval+","+defaultval+","+dotted+",'"+sep+"',2);",100);
        }
        if(f==2)
        {
            if(ffz_IntervalObj!=null) 
                clearInterval(ffz_IntervalObj);
            ffz_IntervalObj = setInterval("NUD_Tool('"+fun+"','"+obj.id+"',"+value+","+minval+","+maxval+","+defaultval+","+dotted+",'"+sep+"',0);",10);
        }
    }
    catch(ex)
    {
        alert(ex);
    }
}

function NUD_SetValidatingObj(obj,value,minval,maxval,defaultval,dotted,sep)
{
    if(obj.disabled) return;
    if(value<minval) value=minval;
    if(value>maxval) value=maxval;
    if(dotted) obj.value=ffz_GetStrGrDigitInt(value,sep);
    else obj.value=value;
}
function NUD_Enabled(disabled,obj1,obj2,obj3)
{
    obj1.disabled=disabled;
    obj2.disabled=disabled;
    obj3.disabled=disabled;
}

function AUP_Enabled(obj,obj1,obj2,obj3,obj4)
{
    obj4.disabled=!obj.checked;
    NUD_Enabled(!obj.checked,obj1,obj2,obj3);    
}

function CHK1_Enabled(obj,obj1)
{
    obj1.disabled=!obj.checked;
}

function CHK2_Enabled(obj,obj1,obj2)
{
    obj1.disabled=!obj.checked;
    if(obj2)
    if(obj2!=null)
    {
        obj2.disabled=!obj.checked;
    }
}

function CHK1_Hide(obj)
{
    if(obj!=null)
        obj.style.visibility="hidden";
}

function CHK1_Show(obj)
{
    if(obj!=null)
        obj.style.visibility="visible";
}

function PWD_Enabled(obj,obj1)
{
    obj1.disabled=!obj.checked;
    if (obj1.disabled) obj1.value="";
}

function Ora_UpDown(cmd,objid,valore,f)
{
    var AM=0;
    var PM=0;
    try
    {
        var obj=ffz_GetObj(objid);
        var value=obj.value;
        if(value.indexOf('AM')>=0)
        {
            value=value.replace(/AM/gi,"");
            value=value.replace(/ /g,"");
            AM=1;
        }
        if(value.indexOf('PM')>=0)
        {
            value=value.replace(/PM/gi,"");
            value=value.replace(/ /g,"");
            PM=1;
        }
        
        var a=obj.value.split(":");
        var ora = ffz_GetInt(ffz_DeFormatCifre(a[0]),0);
        var minuti = ffz_GetInt(ffz_DeFormatCifre(a[1]),0);
        if (cmd=="add") minuti+=15;
        if (cmd=="sub") minuti-=15;
        
        if (minuti >= 60)
        {
            minuti -= 60;
            ora++;
        }
        if (ora >= 24)
        {
            ora -= 24;
        }
        if (minuti < 0)
        {
            minuti += 60;
            ora--;
        }
        if (ora <0)
        {
            ora += 24;
        }
        if (minuti < 0 || minuti >= 60) minuti = 0;
        if (ora < 0 || ora >= 24) ora = 0;
        
        if(AM==0 && PM==0)
        {
            obj.value=ffz_FormatCifre(ora,2)+":"+ffz_FormatCifre(minuti,2);
        }
        else
        {
            if(AM==1)
            {
                if (ora > 12)
                {
                    ora -= 12;
                    PM=1;
                    AM=0;
                }
                if (ora <1)
                {
                    ora += 12;
                    PM=1;
                    AM=0;
                }
            }
            else
            {
                if (ora > 12)
                {
                    ora -= 12;
                    PM=0;
                    AM=1;
                }
                if (ora <1)
                {
                    ora += 12;
                    PM=0;
                    AM=1;
                }
            }
            
            if (minuti < 0 || minuti >= 60) minuti = 0;
            if (ora < 1 || ora > 12) ora = 1;
            
            value=ora+":"+ffz_FormatCifre(minuti,2);
            if(AM==1)
               value=value+' AM'; 
            if(PM==1)
               value=value+' PM'; 
               
            obj.value=value;
        }
        
        if(f==1)
        {
            if(ffz_IntervalObj!=null) 
                clearInterval(ffz_IntervalObj);
            ffz_IntervalObj = setInterval("Ora_UpDown('"+cmd+"','"+obj.id+"',"+valore+",2);",100);
        }
        if(f==2)
        {
            if(ffz_IntervalObj!=null) 
                clearInterval(ffz_IntervalObj);
            ffz_IntervalObj = setInterval("Ora_UpDown('"+cmd+"','"+obj.id+"',"+valore+",0);",10);
        }
    }
    catch(ex)
    {
        var obj=ffz_GetObj(objid);
        if(obj!=null)
        {
            obj.value="00:00";
        }
    }
    return ffz_StopPropagation();
}

function PO_Enabled(obj,obj1,obj2,obj3,obj4,obj5,obj6,obj7)
{
    var disabled=!obj.checked;
    obj1.disabled=disabled;
    obj2.disabled=disabled;
    obj3.disabled=disabled;
    obj4.disabled=disabled;
    obj5.disabled=disabled;
    obj6.disabled=disabled;
    obj7.disabled=disabled;
}

function LGU_EditClick(c,ddlobj)
{
    var id = ffz_GetInt(ddlobj.options[ddlobj.selectedIndex].value);
    var strUrl="adming.aspx?c="+c;
    if(id<0)
    {
        id=id*(-1);
        strUrl="editsg.aspx?c="+c+"&idsg="+id;
    }
    else if(id>0)
    {
        strUrl="editgu.aspx?c="+c+"&idgu="+id;
    }
    ffz_Open(strUrl, ""/*, WinObjRef_InnerEdit*/);
    return ffz_StopPropagation();   
}
function DDL_EditClick(c,ddlid,filename,idname,altfilename)
{
    var ddlobj=ffz_GetObj(ddlid);
    var id = ffz_GetInt(ddlobj.options[ddlobj.selectedIndex].value, 0);
    if(id>0 && c>0)
    {
        var strUrl=""+filename+"?c="+c+"&"+idname+"="+id+"&t=";
        ffz_OpenX(strUrl, "");
        return ffz_StopPropagation(); 
    }
    if(c>0)
    {
        var strUrl=""+altfilename+"?c="+c+"&"+idname+"="+id+"&t=";
        ffz_OpenX(strUrl, "");
        return ffz_StopPropagation();
    }
}
function MsGV_CheckAll(me, meStop, prefixStop)
{
    var index = me.id.indexOf(meStop);  
    var prefix = me.id.substr(0,index); 
    for(i=0; i<document.forms[0].length; i++) 
    { 
        var o = document.forms[0][i]; 
        if (o.type == 'checkbox') 
        { 
            if (me.id != o.id) 
            {
                if (o.id.substring(0, prefix.length) == prefix) 
                {
                    if(o.id.indexOf(prefixStop)>0)
                    {
                        o.checked = !me.checked; 
                        o.click(); 
                    }
                }
            }
        } 
    } 
}
function MsGV_ApplyStyle(me, selectedForeColor, selectedBackColor, foreColor, backColor, bold, checkBoxHeaderId) 
{ 
    var td = me.parentNode; 
    if (td == null) 
        return; 
        
    var tr = td.parentNode;
    if (me.checked)
    { 
       tr.style.fontWeight = 700; // bold
       tr.style.color = selectedForeColor; 
       tr.style.backgroundColor = selectedBackColor; 
    } 
    else 
    { 
       document.getElementById(checkBoxHeaderId).checked = false;
       tr.style.fontWeight = bold; 
       tr.style.color = foreColor; 
       tr.style.backgroundColor = backColor; 
    } 
}
function Grid_UC(obj, opt)
{
    var idpanel="";
    if(opt==1) 
        idpanel = (""+obj.id).replace("LBFilter","OptionsPanelOn");
    else
        idpanel = (""+obj.id).replace("LBClose","OptionsPanelOn");
    var obj = ffz_GetObj(idpanel);
    if(obj.style.visibility=="visible")
    {
        obj.style.visibility="hidden";
    }
    else 
    {
        obj.style.visibility="visible";
    }
    return ffz_StopPropagation();
}
function Grid_UCx(obj, opt) {
    var idpanel = "";
    if (opt == 1)
        idpanel = ("" + obj.id).replace("LBFilter", "OptionsPanelOn");
    else
        idpanel = ("" + obj.id).replace("LBClose", "OptionsPanelOn");
    var obj = ffz_GetObj(idpanel);
    if (obj.style.visibility == "visible") {
        obj.style.visibility = "hidden";
        if (obj.style.display != "none")
            obj.style.display = "none";
    }
    else {
        obj.style.visibility = "visible";
        if (obj.style.display == "none")
            obj.style.display = "block";
    }
    return ffz_StopPropagation();
}


function CHKT_backWhite(obj)
{
    obj.className="CheckBoxTristateOff";
}

function ImgAutoResize(imgobj, maxw, maxh)
{
    var out=false;
    if(imgobj.width>maxw)
    {
        imgobj.style.width=maxw;
        imgobj.style.maxWidth=maxw;
        //imgobj.width=maxw;
        out=true;
    }
    else
    {
        imgobj.style.width='';
        imgobj.style.maxWidth='';
        //imgobj.width='';
    }
    if(imgobj.height>maxh)
    {
        imgobj.style.height=maxh;
        imgobj.style.maxHeight=maxh;
        //imgobj.height=maxh;
        out=true;
    }
    else
    {
        imgobj.style.height='';
        imgobj.style.maxHeight='';
        //imgobj.height='';
    }   
    return out;
}

function ImgResize(imgobj, maxw, maxh)
{
    var w = ffz_GetInt(imgobj.width, 0);
    var h = ffz_GetInt(imgobj.height, 0);
    
    var out=false;
    if(w>maxw)
    {
        imgobj.style.width=maxw;
        imgobj.style.maxWidth=maxw;
        imgobj.width=maxw;
        out=true;
    }
    if(h>maxh)
    {
        imgobj.style.height=maxh;
        imgobj.style.maxHeight=maxh;
        imgobj.height=maxh;
        out=true;
    }
    return out;
}

function RBListCheck(obj, objidx, objidA, objidB, objidB1, objidB2, objidB3, objidB2Hidden)
{
    var objidb=obj.id.replace(objidx,"");
    var objA=ffz_GetObj(objidb+objidA);
    var objB=ffz_GetObj(objidb+objidB);
    var objB1=ffz_GetObj(objidb+objidB1);
    var objB2=ffz_GetObj(objidb+objidB2);
    var objB3=ffz_GetObj(objidb+objidB3);
    var objB2Hidden=ffz_GetObj(objidb+objidB2Hidden);
    
    if(obj.id==objA.id)
    {
        objB1.checked=false;
        objB2.checked=false;
        objB3.checked=false;
        objB2Hidden.style.visibility="hidden";
    }
    else if(obj.id==objB.id)
    {
        objB1.checked=true;
        objB2.checked=false;
        objB3.checked=false;
        objB2Hidden.style.visibility="hidden";
    }
    else 
    {
        objA.checked=false;
        objB.checked=true;
        
        if(obj.id==objB2.id)
        {
            objB2Hidden.style.visibility="visible";
        }
        else
        {
            objB2Hidden.style.visibility="hidden";
        }
    }
    
    
}

function HtmlAreaOn(HA, DivHeight)
{
    HA.style.visibility="visible";
    HA.style.display="inline";
    
    if(DivHeight && DivHeight!=null)
     HA.parentNode.parentNode.style.height=DivHeight;
    else
     HA.parentNode.parentNode.style.height="560px";
}

function HtmlAreaOff(HA)
{
    HA.style.visibility="hidden";
    HA.style.display="none";
    
    HA.parentNode.parentNode.style.height="";
}

// CONTROL VALIDATORS -----------------------------------------------

function ValidateControlRegexp(regx,args,ValidateError)
{
    var rg = new RegExp(regx,"gi");
    if (args.Value.match(rg))
    {
        args.IsValid = false;
        ffz_SetInnerText("ffz_label_error",ValidateError);
    }
    else
    {
        ffz_SetInnerText("ffz_label_error","");
    }
    
}

function ValidateAlfaNText(sender,args)
{
    ValidateControlRegexp("[^\\w]",args,ValidateError_AlfaNText);
}

function ValidateAlfaNWSPText(sender,args)
{
    ValidateControlRegexp("[^\\w\\s]",args,ValidateError_AlfaNWSPText);
}

function ValidateAlfaNSPText(sender,args)
{
    ValidateControlRegexp("[^\\w ]",args,ValidateError_AlfaNSPText);
}

function ValidateSimpleText(sender,args)
{
    ValidateControlRegexp("[^\\w .,;:!\\?\\-\\+\\*%]",args,ValidateError_SimpleText);
}

function ValidateNoHTMLText(sender,args)
{
    
}

function ValidateNoHTMLText_Replace(sender, args)
{
    var obj=ffz_GetObj(sender.controltovalidate);
    var str=args.Value;
    str=str.replace(/</gi, "&lt;");
    str=str.replace(/>/gi, "&gt;");
    str=str.replace(/&#/gi, "&amp;#");
    if(obj.value)
    {
        obj.value=str;
    }
}

function ValidateHTMLText(sender,args)
{
    ValidateHTMLText_Replace(sender,args);
}

function ValidateHTMLText_Replace(sender, args)
{

}

function ValidateURLText(sender,args)
{
    ValidateURLText_Replace(sender,args);
}

function ValidateURLText_Replace(sender, args)
{

}

function ValidateMaxLength(sender,args)
{

}

// ----------------------------------------------------------------

var IsIE6=false;
var IsIE=(navigator.userAgent.toLowerCase().indexOf('msie')>0);
if(IsIE)
{
    var IEArrVersion = navigator.appVersion.split("MSIE");
    var IEVersion = parseFloat(IEArrVersion[1]);
    if(IEVersion<7)
        IsIE6=true;
}

function PNGRollAll()
{
	if(IsIE && IsIE6)
	{		
		var is = document.getElementsByTagName('IMG'); // get all images
		for(var x=0; x<is.length; x++){ // cycle through those images
			if(is[x].src.indexOf('.png')){ // only do this to png files
	
				var ww = is[x].offsetWidth; if(ww==0) is[x].width;
				var hh = is[x].offsetHeight; if(hh==0) is[x].eeight;
				if(ww==0 || hh==0) continue;
				if(is[x].alphasrc && is[x].alphasrc!=null) continue;
				if(is[x].style.filter && is[x].style.filter!=null) continue;
			    
		        var src=is[x].src;
		        is[x].src = "http://im0.freeforumzone.it/v3/img/z.gif";
		        is[x].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"', sizingMethod='scale')"; 
				is[x].width = ww;
				is[x].height = hh;
			}
		}
	}
}

function ImgSwapPNG(obj, newSrc)
{
	if(IsIE && IsIE6)
	{
		var ww = obj.offsetWidth; if(ww==0) obj.width;
		var hh = obj.offsetHeight; if(hh==0) obj.eeight;
		obj.src = "http://im0.freeforumzone.it/v3/img/z.gif";
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+newSrc+"', sizingMethod='scale')"; 
		if(ww>0) obj.width = ww;
		if(hh>0) obj.height = hh;
	}
	else
	{
		obj.src=newSrc;
	}
}

function SwapVisibility(objid, def)
{
    var obj=ffz_GetObj(objid);
    if(!obj || obj==null || !obj.style || obj.style==null) return;
    
    var op=1;
    
    if(obj.style.display=="")    
        op=def;
    else if(obj.style.display=="none")
        op=1;
    else 
        op=2;
        
    if(op==1)
    {
        obj.style.visibility="visible";
        obj.style.display="block";
    }
    else
    {
        obj.style.visibility="hidden";
        obj.style.display="none";
    }
}