/*
 * rk_widget_v7a.js
 *
 * (c) 2007 Arcaysis Limited - All Rights Reserved
 *
 *	Widget Functions
 */
// JSLint "helper"
/*global alert, confirm, document, eval, image, navigator, parent, screen, window */



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/i/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;

	objTmp = document.getElementById("skinPop");

	if (objTmp === null)
	{
		alert("The DomElement skinPop Cannot Be Found");
	}
	return objTmp;
}

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;
var oHideID;

	oIframe =  fnFindSkinPop();
	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;
}



