// widget_V6a.js Widget Functions
//
// JSLint "helper"
/*global alert, confirm, document, eval, image, navigator, parent, screen, window */

function MM_jumpMenu(targ, selObj, restore)
{
	//v3.0
	eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
	if (restore)
	{
		selObj.selectedIndex=0;
	}
}

function MM_jumpMenuGo(selName, targ, restore)
{
	//v3.0
	var selObj;
	selObj = MM_findObj(selName);
	if (selObj)
	{
		MM_jumpMenu(targ, selObj, restore);
	}
}

function MM_preloadImages()
{
	// v3.0
	var d=document;
	if (d.images)
	{
		if(! d.MM_p)
		{
			d.MM_p = new Array();
		}
		var i, j=d.MM_p.length, a=MM_preloadImages.arguments;
		for(i=0; i < a.length; i++)
		{
			if (a[i].indexOf("#") !== 0)
			{
				d.MM_p[j] = new Image();
				d.MM_p[j++].src = a[i];
			}
		}
	}
}

function MM_swapImgRestore()
{
	// v3.0
	var i, x, a=document.MM_sr;

	for(i=0; a && i < a.length && (x = a[i]) && x.oSrc; i++)
	{
		x.src = x.oSrc;
	}
}

function MM_findObj(n, d)
{
	// v4.01
	var p, i, x;

	if (!d)
	{
		d=document;
	}
	if ((p = n.indexOf("?")) > 0 && parent.frames.length)
	{
		d = parent.frames[n.substring(p+1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all)
	{
		x=d.all[n];
	}
	for(i=0; !x && i < d.forms.length; i++)
	{
		x = d.forms[i][n];
	}
	for(i=0; !x && d.layers && i<d.layers.length; i++)
	{
		x = MM_findObj(n, d.layers[i].document);
	}
	if (!x && d.getElementById)
	{
		x = d.getElementById(n);
	}
	return x;
}

function MM_showHideLayers()
{
	//v6.0
	var i, p, v, obj, args;

	args = MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3)
	{
		if ((obj = MM_findObj(args[i])) !== null)
		{
			v = args[i+2];
			if (obj.style)
			{
				obj = obj.style;
				v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v;
			}
			obj.visibility = v;
		}
	}
}

/*
function MM_showHideLayers()
{
	//v6.0
	var i, p, v, obj, args;

	args = MM_showHideLayers.arguments;
	for (i=0; i < (args.length-2); i+=3)
	{
		if ((obj = MM_findObj(args[i])) !== null)
		{
			v = args[i+2];
			if (obj.style)
			{
				obj=obj.style;

// Visibility method is NOT used otherwise the DIV will be drawn, but with no content
//				v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v;
//			}
//			obj.visibility=v;

				v = (v == 'show') ? 'block' : (v == 'hide') ? 'none' : v;
			}
			obj.display=v;

		}
	}
}
*/

function RK_showHideLayers()
{
	// NOTE: Only uses TWO parameters per item, NOT THREE as per MM standard
	var i, p, v, obj, args;

	args = RK_showHideLayers.arguments;
	for (i=0; i < (args.length-1); i+=2)
	{
		if ((obj = MM_findObj(args[i])) !== null)
		{
			v = args[i+1];
			if (obj.style)
			{
				obj = obj.style;
// Display method is used instead of MM's Visibility method otherwise the DIV will be drawn, but with no content

				v = (v == 'show') ? 'block' : (v == 'hide') ? 'none' : v;
			}
			obj.display = v;
		}
	}
}

function MM_swapImage()
{
// v3.0
	var i, j=0, x, a=MM_swapImage.arguments;

	document.MM_sr = new Array();
	for(i=0; i < (a.length-2); i+=3)
	{
		if ((x=MM_findObj(a[i])) !== null)
		{
			document.MM_sr[j++] = x;
			if (! x.oSrc)
			{
				x.oSrc=x.src;
			}
			x.src=a[i+2];
		}
	}
}


function fnCenterDiv(oDiv)
{
    return;
/** UNREACHABLE
    //alert();
    var nheight = oDiv.height;
    var nwidth = oDiv.width;
    var iy = screen.height;
    var ix = screen.width;
    //alert("["+nheight+","+nwidth+"]["+ix+","+iy+"]");
    iy = ((iy-nheight)/2);//+document.body.scrollTop;
    ix = ((ix-nwidth)/2);//+document.body.scrollLeft;
    oDiv.style.left=ix+"px";
    oDiv.style.top=iy+"px";
UNREACHABLE **/
}

// modifies the action of the named form to include ACT=LIST XPG_MINID=  XPG_MAXID=
// Modified from code that manipulated
// hidden fields that caused problems with Opera7 and select lists
// RRH 16 June 2003
// Opera 7.11 now correctly supports  form handling mods removed
function fnListSubmit(strFormName)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
        for(var i=0;i<oForm.length;i++)
		{
            switch (oForm[i].name)
			{
            case "XPG_MINID":
                oForm[i].value="";
				break;
            case "XPG_MAXID":
                oForm[i].value="";
				break;
            case "ACT":
                oForm[i].value="LIST";
				break;
            }
        }
        oForm.submit();
    }
}

// modifies the action of the named form to include ACT=LIST
// Modified from code that manipulated
// hidden fields that caused problems with Opera7 and select lists
// RRH 16 June 2003
// Opera 7.11 now correctly supports  form handling mods removed

function fnBackToListSubmit(strFormName)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
        /*
        var strAction = oForm.action;
        strAction+=((strAction.indexOf("?") != -1)?"&":"?")+"ACT=LIST";
        oForm.action=strAction;
        */
        oForm.ACT.value = "LIST";
        oForm.submit();
    }
}

// modifies the action of the named form to include ACT=LIST XPG_MINID=  XPG_MAXID=sID
// Modified from code that manipulated
// hidden fields that caused problems with Opera7 and select lists
// RRH 16 June 2003
// Opera 7.11 now correctly supports  form handling mods removed

function fnPageUp(strFormName,sID)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
        oForm.ACT.value = "LIST";
        oForm.XPG_MAXID.value = sID;
        oForm.XPG_MINID.value = "";
        oForm.submit();
    }
}

function fnPageDown(strFormName,sID)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
        oForm.ACT.value = "LIST";
        oForm.XPG_MAXID.value = "";
        oForm.XPG_MINID.value = sID;
        oForm.submit();
    }
}

function fnDeleteRecord(strFormName)
{
    if (fnCheckFormName(strFormName))
	{
        if (!confirm("Are you sure you want to delete this record"))
		{
            return;
        }
        var oForm=document.forms[strFormName];
        oForm.ACT.value = "DELETE";
        oForm.submit();
    }
}

function fnUpdateRecord(strFormName)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];

		if (oForm.onsubmit() !== false)
		{
            oForm.ACT.value = "UPDATE";
            oForm.submit();
        }
    }
}

function fnAddRecord(strFormName)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
        oForm.ACT.value = "ADD";
        oForm.submit();
    }
}


function fnEditSelect(strFormName,strNames,strVals)
{
    var aNameList = fnDelimSplit(strNames);
    var aValList = fnDelimSplit(strVals);
    if (aNameList.length!=aValList.length)
	{
        alert("Error in Name:Value Arrays");
        return;
    }
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
        oForm.ACT.value="EDIT";
        for (var err=0;err<aNameList.length;err++)
        {
            if (aNameList[err] != "ACT")
			{
                eval('document.' + strFormName + '.'+aNameList[err]+'.value="'+aValList[err]+'";');
            }

        }
        oForm.submit();
    }
}

function fnDelimSplit(strVal)
{
    if (strVal.lastIndexOf(",") == strVal.length-1)
	{
        strVal = strVal.substring(0,strVal.length-1);
    }
    return strVal.split(",");
}

function fnCheckFormName(strFormName)
{
    if (typeof document.forms[strFormName] == "undefined")
	{
        alert("["+strFormName+"] is not a valid form");
        return false;
    }
    return true;
}


function displayWindow(theURL, winName, width, height, features)
{
// v3.1
    var window_width = width;
    var window_height = height;
    var newfeatures= features;
    var window_top = (screen.height-window_height)/2;
    var window_left = (screen.width-window_width)/2;
	var newWindow;

    newWindow=window.open(theURL,winName,'width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ',' + newfeatures,false);
    newWindow.focus();
}

function fnPop800(strURL)
{
    displayWindow(strURL,'pop800','760','570','scrollbars=yes,resizable=yes');
}


function fnUrlPopUpWin(strURL)
{
    displayWindow(strURL,'','760','500','scrollbars=yes,resizable=yes');

}

function fnSetMSHChanged()
{
    var field = document.forms[0];
    var i;
    for (i = 0; i < field.length; i++)
    {
        document.forms[0].elements[i].onchange=fnMSHchanged;
    }
}
function fnMSHchanged()
{
	document.changedwarn.src = "/rkDom/unsaved.gif";
}


function fnXPageNo(strFormName,selObj)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
//		oForm.ACT.value = "LIST";
        oForm.XPAGENO.value = selObj.options[selObj.selectedIndex].value;
        oForm.submit();
    }
}

function fnXPageSize(strFormName,selObj)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
//		oForm.ACT.value = "LIST";
        oForm.XPAGENO.value = "1";
        oForm.XPAGESIZE.value = selObj.options[selObj.selectedIndex].value;
        oForm.submit();
    }
}

function fnXPageLink(strFormName,nPage)
{
    if (fnCheckFormName(strFormName))
	{
        var oForm=document.forms[strFormName];
//		oForm.ACT.value = "LIST";
        oForm.XPAGENO.value = nPage;
        oForm.submit();
    }
}

function fnHideErrorSaving()
{
document.getElementById("ErrorSaving").style.display="none";
}

function fnAppendToOnLoad(strOnload)
{
    var strNewOnLoad;

    if (document.body.onload !== null && document.body.onload !== "undefined")
	{
        var strOnLoad = document.body.onload.toString();
        strNewOnLoad = strOnLoad.substring(strOnLoad.indexOf('{') +1, strOnLoad.length -2);
    } else
	{
        strNewOnLoad = "";
    }
    strNewOnLoad += strOnload;
    document.body.onload = new Function(strNewOnLoad);
}

function fnrkWinSize()
{
//	returns: ["W"] = width, ["H"] = height, ["T"] = ScrollTop, ["L"] = ScrollLeft
	var ArrPageSize = new Array();
	var intWidth = 0, intHeight = 0;
	var  intTop=0, intLeft =0;
	if ( typeof( window.innerWidth ) == 'number' )
	{
		// Non-IE
		intWidth = window.innerWidth;
		intHeight = window.innerHeight;
	} else
	{
		if ( document.documentElement &&
			 ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
			 {
			// IE 6+ in 'standards compliant mode'
			intWidth = document.documentElement.clientWidth;
			intHeight = document.documentElement.clientHeight;
		} else
		{
			if ( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
			{
				// IE 4 compatible
				intWidth = document.body.clientWidth;
				intHeight = document.body.clientHeight;
			}
		}
	}

	if (typeof( document.body.scrollTop ) == 'number')
	{
		intTop = document.body.scrollTop + document.documentElement.scrollTop;
		intLeft = document.body.scrollLeft + document.documentElement.scrollLeft;
	} else
	{
		intTop = window.pageYOffset;
		intLeft = window.pageXOffset;
	}

	ArrPageSize["W"]=intWidth;
	ArrPageSize["H"]=intHeight;
	ArrPageSize["T"]=intTop;
	ArrPageSize["L"]=intLeft;
	return ArrPageSize;
}

function fnCompressHTML(strForm,strFld)
{
    var o=document.forms[strForm][strFld];
    var strContent = o.value;
    var rExp = /\n/gi;
    strContent = strContent.replace(rExp,'');
    rExp = /\r/gi;
    strContent = strContent.replace(rExp,'');
    rExp = /\t/gi;
    strContent = strContent.replace(rExp,'');
    rExp = /\s+/gi;
    strContent = strContent.replace(rExp,' ');
    o.value = strContent;
}

function fnHideValError()
{
    var o=MM_findObj("ValError");
    o.style.display="none";
}

function fnGetCookie(strName)
{
	var objDc = document.cookie;
	var strPrefix = strName + "=";
	var intBegin = objDc.indexOf("; " + strPrefix);

	if (intBegin == -1)
	{
		intBegin = objDc.indexOf(strPrefix);

		if (intBegin !== 0)
		{
			return "";	// null;
		}
	} else
	{
		intBegin += 2;
	}
	var intEnd = document.cookie.indexOf(";", intBegin);
	if (intEnd == -1)
	{
		intEnd = objDc.length;
	}
	return unescape(objDc.substring(intBegin + strPrefix.length, intEnd));
}

function fnHideID(strID)
{
    var o=MM_findObj(strID);

	if (o)
	{
		o.style.display = "none";
	}
}

/*
function fnCleanACEHTML(strContent)
{
		// alter paths to relative
        var strDomain = new RegExp( document.location.protocol + '\/\/' + document.location.hostname + '\/','gi');
        strContent = strContent.replace(strDomain, "");

        // remove common artifacts
        strContent = strContent.replace(/<P><\/P>/gi, "");
        strContent = strContent.replace(/<P><\/P>/gi, "");
        strContent = strContent.replace(/<P>\&nbsp\;<\/P>/gi, "");
        strContent = strContent.replace(/<DIV><\/DIV>/gi, "");
        strContent = strContent.replace(/<DIV>\&nbsp\;<\/DIV>/gi, "");
        strContent = strContent.replace(/STRONG>/gi, "B>");
        strContent = strContent.replace(/EM>/gi, "I>");

        // copyright © UKPound £ Registered ®
        strContent = strContent.replace(/£/gi, "&pound;");
        strContent = strContent.replace(/®/gi, "&reg;");
        strContent = strContent.replace(/©/gi, "&copy;");
        strContent = strContent.replace(/€/gi, "&euro;");

		return  strContent;
}
*/
function fnFindSkinPop()
{
var objTmp = document.getElementById("skinPop");

	if (objTmp !== null)
	{
		return objTmp;
	} else
	{
		alert("The DomElement skinPop Cannot Be Found");
		return null;
	}
}

function fnShowSkinPop(strURL,intWidth,intHeight,intTop,intLeft)
{
var divtop;
var divleft;
var oIframe =  fnFindSkinPop();
var oHideID =  document.getElementById("MainContent");

	if (oIframe)
	{
		if (oHideID){
			oHideID.style.display = "none";
		}

		if (!intTop || !intLeft)
		{
			divtop = ((document.documentElement.clientHeight-intHeight)/2)+(document.body.scrollTop + document.documentElement.scrollTop);
			divleft = ((document.body.clientWidth-intWidth)/2)+(document.body.scrollLeft + document.documentElement.scrollLeft);
		}
		else
		{
			divtop = intTop;
			divleft = intLeft;
		}

		oIframe.src = strURL;
		oIframe.style.left=divleft + 'px';
		oIframe.style.top=divtop + 'px';
		oIframe.style.width=intWidth + 'px';
		oIframe.style.height=intHeight + 'px';
		oIframe.style.display = "block";
	}
}

function fnShowSkinPop2(strURL)
{
var divtop;
var divleft;
var intWidth;
var intHeight;
var oIframe =  fnFindSkinPop();
var oHideID =  document.getElementById("MainContent");

	if (oIframe)
	{
		if (oHideID)
		{
			//alert(oHideID.style.display);
			oHideID.style.display = "none";
		}

        intHeight = document.documentElement.clientHeight;// + document.body.scrollTop + document.documentElement.scrollTop;
		intWidth =  document.body.clientWidth;// + document.body.scrollLeft + document.documentElement.scrollLeft;
		intHeight = intHeight * 0.9;
		intWidth = intWidth * 0.9;
        divtop = 10;//((document.documentElement.clientHeight-intHeight)/2)+(document.body.scrollTop + document.documentElement.scrollTop);
		divleft = 10;//((document.body.clientWidth-intWidth)/2)+(document.body.scrollLeft + document.documentElement.scrollLeft);
		
		oIframe.src = strURL;
		oIframe.style.left=divleft + 'px';
		oIframe.style.top=divtop + 'px';
		oIframe.style.width=intWidth + 'px';
		oIframe.style.height=intHeight + 'px';
		oIframe.style.display = "block";
	}
}

function fnHideSkinPop()
{
var oIframe =  fnFindSkinPop();
var oHideID =  document.getElementById("MainContent");

	if (oIframe)
	{
		oIframe.style.display = "none";
		oIframe.style.left='0px';
		oIframe.style.top='0px';
		oIframe.style.width='10px';
		oIframe.style.height='10px';
		oIframe.src = "about:blank";

		if (oHideID)
		{
			oHideID.style.display = "block";
		}
	}
}

function fnParamHelp(intItem, intPage, intIdentity)
{
var oOBJ = MM_findObj("VALUE_LIST");
//TODO RRH Note&Delete: " + "&ITEM_NO=" + intItem" is NOT present on FROGS, not sure if it is now correct for the "master"
var strURL = "?PAGEID=" + intPage + "&PGD_IDENTITY=" + intIdentity + "&ITEM_NO=" + intItem;

	if (oOBJ)
	{
//TODO RRH Note&Delete: Variations with FROGS box
		if (oOBJ.length > 0)
		{
			 g_EditValue =  oOBJ[intItem];
			 oOBJ = MM_findObj("NAME_LIST");
			 g_EditName = oOBJ[intItem].value;
		}
		else
		{
			g_EditValue =  oOBJ;
			oOBJ = MM_findObj("NAME_LIST");
			g_EditName = oOBJ.value;

		}

		fnShowSkinPop(strURL, 400, 350, 100, 100);
	}
}

function fnGenParamPut(strValue, strFormName)
{
	fnHideSkinPop();
	if (g_EditValue)
	{
		if (g_EditName == "PGID")
		{
			if (!confirm("Are you sure you want to change the setting for PGID"))
			{
				return;
			}
			g_EditValue.value = strValue;
			fnUpdateRecord(strFormName);
		} else
		{
			if (g_EditValue.value != strValue)
			{
				g_EditValue.value = strValue;
				fnMSHchanged();
			}
		}
	}
}

function fnFileExt(strfileName)
{
    //alert(strfileName);
	
	var intDot = strfileName.lastIndexOf(".", strfileName.length);
	var strExt;

	//alert(intDot);

	if (intDot != -1)
	{
		strExt = strfileName.substring(intDot,(strfileName.length));
	}
	else
	{
		strExt = "";
	}
	//alert(fnFileExt);

	return strExt;
}

function rkfnSetCheckboxState(objControl, objTarget)
{
// Set all objTarget checkboxes to match objControl

var i;

	for (i=0; i< objTarget.length; i++)
	{
		objTarget[i].checked = objControl.checked;
	}
}

function rkfnPrintPage()
{
// Print current page

	window.print();
}

