/*
 * rk_util_v8a.js
 *
 * (c) 2008 Arcaysis Limited - All Rights Reserved
 *
 *	Arcaysis Utility Functions
 */
//
// JSLint "helper"
/* extern alert, confirm, document, eval, location, navigator, parent, window, ActiveXObject */

function rkfnUtilVersion()
{
	return "8a";
}

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_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_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 rkfnStyleDisplay(strDiv, strAttribute){var objDiv = MM_findObj(strDiv);if (objDiv){objDiv.style.display = strAttribute;}}

function rkfnStyleToggle(strDiv, strAttribute1, strAttribute2)
{
	var objDiv = MM_findObj(strDiv);
	if (objDiv)
	{
		if (objDiv.style.display === strAttribute1)
		{
			objDiv.style.display = strAttribute2;
		} else
		if (objDiv.style.display === strAttribute2)
		{
			objDiv.style.display = strAttribute1;
		}
	}
}

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 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 rkfnCheckboxStateSet(objControl, objTarget)
{
// Set all objTarget checkboxes to match objControl

var i;

	if (typeof(objTarget.length) === "undefined")
	{
		objTarget.checked = objControl.checked;
    } else
	{
		for (i=0; i < objTarget.length; i++)
		{
			objTarget[i].checked = objControl.checked;
		}
	}
}

function rkfnSetCheckboxState(objControl, objTarget)
{
	alert("USE: rkfnCheckboxStateSet");
	rkfnCheckboxStateSet(objControl, objTarget);
}

function rkfnCheckboxToggle(objCheckbox)
{
	if (objCheckbox)
	{
		if (objCheckbox.checked)
		{
			objCheckbox.checked = false;
		}
		else
		{
			objCheckbox.checked = true;
		}
	}
}

function rkfnToggleValue(objCheckbox, strInput, strChecked, strUnchecked)
{
	var objInput;
	
	objInput = MM_findObj(strInput);
	if (objCheckbox && objInput)
	{
		if (objCheckbox.checked)
		{
			objInput.value = strChecked;
		}
		else
		{
			objInput.value = strUnchecked;
		}
	}
}

function rkfnCookieGet(strName)
{
	var objDc = document.cookie;
	var strPrefix = strName + "=";
	var intBegin = objDc.indexOf("; " + strPrefix);
	var intEnd;
	
	if (intBegin === -1)
	{
		intBegin = objDc.indexOf(strPrefix);

		if (intBegin !== 0)
		{
			return "";	// null;
		}
	} else
	{
		intBegin += 2;
	}
	intEnd = document.cookie.indexOf(";", intBegin);
	if (intEnd === -1)
	{
		intEnd = objDc.length;
	}
	return unescape(objDc.substring(intBegin + strPrefix.length, intEnd));
}


function rkfnCopyHTML(strSrcContainer, strDstContainer)
{
// THIS SHOULD COPY - **NOT** MOVE !!
	var blnRetValue;
	var objSrc = MM_findObj(strSrcContainer);
	var objDst = MM_findObj(strDstContainer);
	if (objSrc && objDst)
	{
		objDst.innerHTML = objSrc.innerHTML;
		objSrc.innerHTML = '';
		blnRetValue = true;
	} else
	{
		alert('rkfnCopyHTML: Invalid parameters, check source/destination');
		blnRetValue = false;
	}
	return blnRetValue;
}


function rkfnHideShow(strHideDiv,strShowDiv)
{
/*
 *	Hide and Show DIVs respectively.  Scrolls page to top
 */
	RK_showHideLayers(strHideDiv,'hide',strShowDiv,'show');
	self.scrollTo(0,0);
}

function rkfnLinkHide(strURL, strHideDiv, strShowDiv)
{
/*
 *	Hide the "main" layer, and folow the URL link
 */
	if (! strHideDiv)
	{
		strHideDiv = 'listing';
	}
	if (! strShowDiv)
	{
		strShowDiv = 'loading';
	}
	RK_showHideLayers(strHideDiv,'hide',strShowDiv,'show');
	window.location = "strURL";
}

function rkfnOnLoadAppend(strOnload)
{
    var strNewOnLoad;
	var strOnLoad;

    if (document.body.onload !== null && document.body.onload !== "undefined")
	{
        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 rkfnPagePrint()
{
// Print current page

	window.print();
}


function rkfnPrintPage()
{
// Print current page

	alert("USE: rkfnPagePrint");
	rkfnPagePrint();
}


function rkfnSelectedSet(strSelectName, strOption){var i;var objSel = document.getElementById(strSelectName);if (strOption !== ""){for(i=0; i < objSel.length; i++){if (objSel.options[i].value === strOption){objSel.options[i].selected = true;}}}}

function rkfnStyleDisplay(strDiv, strAttribute){var objDiv = MM_findObj(strDiv);if (objDiv){objDiv.style.display = strAttribute;}}

function rkfnWindowDisplay(param1, param2, param3, param4, param5, param6)
{
// Alternative 2-parameter calling syntax: rkfnWindowDisplay(this(), blnSetFocus)
// Alternative 3-parameter calling syntax: rkfnWindowDisplay(strURL, strWinName, blnSetFocus)
// Alternative 6-parameter calling syntax: rkfnWindowDisplay(strURL, strWinName, intWidth, intHeight, strFeatures, blnSetFocus)
// v3.1
	var pLink;
	var newfeatures;
	var blnSetFocus;
	var strURL;
	var hWin;
	var strWinName;
	var window_height;
	var window_top;
	var window_left;
	var window_width;

//DEBUG alert("ArgsLen=" + arguments.length);

	if (arguments.length < 2)
	{
		alert("rkfnWindowDisplay:Too few arguments");
	} else
	if (arguments.length === 2)
	{
		pLink = param1;
		blnSetFocus = param2;
//DEBUG alert("a2:" + pLink.href + ", " + pLink.target + ", " + blnSetFocus);
		hWin = window.open(pLink.href, pLink.target, "", false);
	
	} else
	if (arguments.length === 3)
	{
		strURL = param1;
		strWinName = param2;
		blnSetFocus = param3;
//DEBUG alert("a3:" + strURL + ", " + strWinName + ", " + blnSetFocus);
		hWin = window.open(strURL, strWinName, "", false);
	} else
	{
//DEBUG alert("a4+:" + strURL + ", " + strWinName + ", " + blnSetFocus);
		strURL = param1;
		strWinName = param2;
		window_width = param3;
		window_height = param4;
		newfeatures = param5;
		blnSetFocus = param6;
		window_top = (screen.height - window_height)/2;
		window_left = (screen.width - window_width)/2;
		hWin = window.open(strURL, strWinName, 'width=' + window_width + ', height=' + window_height + ', top=' + window_top + ', left=' + window_left + ',' + newfeatures, false);
	}
	if (blnSetFocus)
	{
		hWin.focus();
	}
	return false;	// Allows "return rkfnWindowDisplay(this, true);" to be used in OnClick for brevity
}


function rkfnDisplayWindow(strURL, strWinName, intWidth, intHeight, strFeatures, blnSetFocus)
{
	alert("USE: rkfnWindowDisplay");
	rkfnWindowDisplay(strURL, strWinName, intWidth, intHeight, strFeatures, blnSetFocus);
}






function rkfnStripe(id)
// make a table stripy, but only on rows that are VISIBLE (display != none)
// id	Table's ID
// Even Style, default ksrsTREven
// Odd Style , default ksrsTROdd
{
	var blnEven;
	var evenColor;
	var oddColor;
	var objTable;
	var aryTBody;
	var aryTR;
	var h;
	var i;

	blnEven = false;
	evenColor = arguments[1] ? arguments[1] : "ksrsTREven";
	oddColor = arguments[2] ? arguments[2] : "ksrsTROdd";

	objTable = MM_findObj(id);
	if (! objTable)
	{
		alert("rkfnStripe:Could not find table [" + id + "]");
		return;
	}

	aryTBody = objTable.getElementsByTagName("tbody");

	for (h = 0; h < aryTBody.length; h++)
	{
		aryTR = aryTBody[h].getElementsByTagName("tr");
		for (i = 0; i < aryTR.length; i++)
		{
			// skip hiddens
			if (aryTR[i].style.display != 'none') 
			{
//DEL			aryTR[i].className = blnEven ? evenColor : '';
				aryTR[i].className .replace(blnEven ? /TROdd/ : /TREven/, blnEven ? "TREven" : "TROdd");
				// Only switch even/odd for rows we actually colour!
				blnEven =  ! blnEven;
			}
		}
	}
}


function rkfnRowHilite(strTable, strSearchID, strHiClass, strHiHiClass)
// highlight rows in strTable where row ID contains strSearchID
// where the row has a class, replace with strHiHiclass
// otherwise, where matched, set class to strHiClass.
{
	var objTable;
	var aryTBody;
	var aryTR;
	var h;
	var i;

	// Firstly reset table
	rkfnRowReset(strTable);
	
	// Get a handle on our objTable table
	// if can't find then abort
	objTable = MM_findObj(strTable);
	if (! objTable)
	{
		alert("rkfnRowHilite:Could not find table [" + strTable + "]");
		return;
	}
	
	// Loop through <tbody> tags
	// tables can have more than one.
	aryTBody = objTable.getElementsByTagName('tbody');
	for (h = 0; h < aryTBody.length; h++)
	{
		// loop through <tr>'s
		aryTR = aryTBody[h].getElementsByTagName('tr');
		for (i = 0; i < aryTR.length; i++)
		{
			// Save original Class to custom oldclassname attribute.
			// TODO: Only create if doean't exist
			aryTR[i].setAttribute('oldclassname', aryTR[i].className);
			
			// Is this the ID we are looking for?
			if (aryTR[i].id.indexOf(strSearchID) > -1)
			{
				if (aryTR[i].className)
				{
					aryTR[i].className = aryTR[i].className + ', ' + strHiHiClass;
				} else
				{
					aryTR[i].className = strHiClass;
				}
			}
		}
	}
}


function rkfnRowShow(strTable, strSearchID, blnReStripe)
// Only show rows in table (strTable) where row ID's contain strSearchID
{
	var objTable;
	var aryTBody;
	var aryTR;
	var h;
	var i;

//DEBUG alert("rkfnRowShow:" + strTable);
	// Firstly reset table
	rkfnRowReset(strTable);
	
	if (strSearchID === "_RESET_")
	{
		return;
	}
	// Get a handle on our objTable table
	// if can't find then abort
	objTable = MM_findObj(strTable);
	if (! objTable)
	{
		alert("rkfnRowShow:Could not find table [" + strTable + "]");
		return;
	}
	
	// Loop through <tbody> tags
	// tables can have more than one.
	aryTBody = objTable.getElementsByTagName('tbody');
	for (h = 0; h < aryTBody.length; h++)
	{
		// loop through <tr>'s
		aryTR = aryTBody[h].getElementsByTagName('tr');
		for (i = 0; i < aryTR.length; i++)
		{
			// Is this the ID we are looking for?
			if (! (aryTR[i].id.indexOf(strSearchID) > -1))
			{
				if (aryTR[i].style.display != 'none')
				{
					aryTR[i].style.display = 'none';
				}
			} else
			{
				aryTR[i].style.display = '';
			}
		}
	}
	rkfnStripe(strTable);
}


function rkfnRowReset(strTable, blnReStripe)
// If rows are set to hidden, unhide them
// make the class = oldclassname, if they have an oldclassname attribute
// otherwise remove the class
{
	var objTable;
	var aryTBody;
	var aryTR;
	var h;
	var i;

	// Get a handle on our objTable table
	// if can't find then abort
	objTable = MM_findObj(strTable);
	if (! objTable)
	{
		alert("rkfnRowReset:Could not find table [" + strTable + "]");
		return;
	}
	
	// Loop through <tbody> tags
	// tables can have more than one.
	aryTBody = objTable.getElementsByTagName('tbody');
	for (h = 0; h < aryTBody.length; h++)
	{
		// loop through <tr>'s
		aryTR = aryTBody[h].getElementsByTagName('tr');
		for (i = 0; i < aryTR.length; i++)
		{
			// Unhide if hidden
			if (aryTR[i].style.display == 'none')
			{
//				aryTR[i].style.display = 'block';
				aryTR[i].style.display = '';
			}
			
			if (aryTR[i].getAttribute('oldclassname') !== undefined)
			{
				if (aryTR[i].getAttribute('oldclassname') !== ''
					&& aryTR[i].getAttribute('oldclassname') !== null)
				{
					aryTR[i].className = aryTR[i].getAttribute('oldclassname');
					aryTR[i].removeAttribute('oldclassname');
				} else
				{
//DEL					aryTR[i].className = '';
					aryTR[i].removeAttribute('oldclassname');
				}
			}
		}
	}
	rkfnStripe(strTable);
}

function rkfnTrackClick(param1, param2, param3)
// Track click-through
// 2-parameter calling syntax: rkfnTrackClick(this(), yppl_ID) - Use for HREF's Link
// 3-parameter calling syntax: rkfnTrackClick(dummy, yppl_ID, strTD) - Use for specific strTD
{
	var	pLink;
	var	strURL;
	var	strTD;
	var yppl_ID;
	
	if (arguments.length < 2)
	{
		alert("rkfnTrackClick:Too few arguments");
	} else
	if (arguments.length === 2)
	{
		pLink = param1;
		yppl_ID = param2;
		strTD = pLink.href;
	
	} else
	if (arguments.length >= 3)
	{
//		strURL = param1;
		yppl_ID = param2;
		strTD = param3;
	}

	if (document.images)
	{
		(new Image()).src="rk_GetImage_V7b.asp?yppl_ID=" + yppl_ID + "&td=" + td;
		(new Image()).src="/rkdom/rk_GetImage_V7b.asp?yppl_ID=" + yppl_ID + "&td=" + td;
	}
	return true;
}


function rkfnNextSibling(pObj)
//
//	Get Next Object, ignoring any white space
//
{
	do
	{
		pObj = pObj.nextSibling;
	} while (pObj && pObj.nodeType != 1);
	return pObj;
}
 
function rkfnPreviousSibling(pObj)
{
	do
	{
		pObj = pObj.previousSibling;
	} while (pObj && pObj.nodeType != 1);
	return pObj;
}

function rkfnTabOn(objTab, strTabObj)
//
//	Turn on objTab, using strTabObj as the Prefix to find all the Tabs in this Tab-set
//
{
	var leftClassName;
	var rightClassName;

	var pPreviousSibling;
	var pNextSibling;

	rkfnTabAllOff(strTabObj);

	pPreviousSibling = rkfnPreviousSibling(objTab);
	if (pPreviousSibling.id.indexOf('l_end') != -1)
	{
		leftClassName = 'rkADlhonTD';
	}
	else
	{
		leftClassName = 'rkADoffonTD';
	}

	pNextSibling = rkfnNextSibling(objTab);
	if (pNextSibling.id.indexOf('r_end') != -1)
	{
		rightClassName = 'rkADrhonTD';
	}
	else
	{
		rightClassName = 'rkADonoffTD';
	}

	pPreviousSibling.className = leftClassName;
	objTab.className = 'rkADlinkonTD';
	pNextSibling.className = rightClassName;

	RK_showHideLayers(objTab.id + "Content", "show");
	
	return true;
}

function rkfnTabAllOff(strTabObj)
//	Turn all tabs to the OFF state where strTabObj is the prefix for all the Tab IDs in this Tab-set
{
	var i;
	var pObj;

	for (i = 1; i < 50; i++)	// 50=Max number of TABs (just to prevent endless looping)
	{
		if ((pObj = document.getElementById(strTabObj + i)))
		{
			pObj.className = "rkADlinkoffTD";
			RK_showHideLayers(strTabObj + i + "Content", "hide");
		} else
		{
			break;
		}

		if ((pObj = document.getElementById(strTabObj + i + "_l"))
			&& pObj.className !== "rkADoffoffTD")
		{
			pObj.className = "rkADoffoffTD";
		} else
		if ((pObj = document.getElementById(strTabObj + i + "_l_end"))
			&& pObj.className !== "rkADlhoffTD")
		{
			pObj.className = "rkADlhoffTD";
		}

		if ((pObj = document.getElementById(strTabObj + i + "_r_end"))
			&& pObj.className !== "rkADrhoffTD")
		{
			pObj.className = "rkADrhoffTD";
		}

//		if ((pObj = document.getElementById(strTabObj + i + "_r")))
//		{
//			pObj.className = "rkADoffoffTD";
//		}

	}
}




