/*	CSJS function: debugLocations()
 *	Alert box showing window (self), parent & top location URLs
 */
function debugLocations()
{
    var locs = "Locations:" + "\nwindow - " + window.location + "\nparent - " + parent.location + "\ntop - " + top.location;
    alert(locs);
}

/*	CSJS function: gotoSelectedOption(Select, [String])
 * 	Go to the URL that is the selected option in a dropdown listbox.
 *	Resets selectedIndex to 0.
 * 	Params	- objSelect:	SELECT object (CSJS)
 *			- contextPath:	app URL String (e.g. "/myApp") to prepend to the URL
 *	Returns - true
 */
function gotoSelectedOption(objSelect, targetName)
{
    var idx = objSelect.selectedIndex;
    var gotoURL = objSelect.options[idx].value;

	// Revert selection to index 0.
    objSelect.selectedIndex = 0;
    objSelect.options[0].selected = true;

    if(gotoURL.search(/^javascript\:/i) != -1)
        // If URL is CSJS then evaluate it.
    {
        eval(gotoURL);
    }
    else if(idx != 0 && gotoURL != "")
    {
        var gotoHref = gotoURL;

        if(targetName == null || targetName == "")
            // Target not specified, default is _parent.
        {
            //alert("targetName = NULL\ngotoHref = " + gotoHref + "\nformName = " + objSelect.form.name);
            //dummyLoop(100);
            parent.location.href = gotoHref;
        }
        else if(targetName.charAt(0) == "_")
        {
            //alert("targetName = " + targetName + "\ngotoHref = " + gotoHref + "\nformName = " + objSelect.form.name);
            targetName = targetName.substring(1);
			//dummyLoop(100);
            eval(targetName + ".location.href = gotoHref;");
        }
        else
            // Target is specified
        {
            var targetFrameP = "parent." + targetName;
            var targetFrameT = "top." + targetName;
            if(eval(targetFrameP + " != null"))
                // Frame w/ specified name exists for parent window (frame).
            {
                //alert("targetNameP = "+ targetName + "\ngotoHref = " + gotoHref);
                //dummyLoop(100);
                eval(targetFrameP + ".location.href = gotoHref;");
            }
            else if(eval(targetFrameT + " != null"))
                // Frame w/ specified name exists for top window (frame).
            {
                //alert("targetNameT = "+ targetName + "\ngotoHref = " + gotoHref);
                //dummyLoop(100);
                eval(targetFrameT + ".location.href = gotoHref;");
            }
            else
                // No frame exists with specified name; open new window.
            {
                //alert("New window: "+ targetName + "\ngotoHref = " + gotoHref);
                //dummyLoop(100);
                window.open(gotoHref, targetName);
            }
        }
        gotoHref = null;
    }
	//else return false;

    //return true;
}

/*	CSJS function: getRadioValue(Radio)
 *	Get value of the selected radio button.
 *	Params	- radioObj:	Radio object (CSJS)
 *	Returns	- value as String, if exists, else null.
 */
function getRadioValue(radioObj)
{
    for(var i = 0; i < radioObj.length; i++)
    {
        if(radioObj[i].checked)
        {
            return radioObj[i].value;
        }
    }
    return null;
}

/*	CSJS function: getCheckboxValues(Checkbox, [boolean])
 *	Get the values corresponding to all checked Checkboxes
 *	Params	- chkboxObj:	Checkbox object (CSJS)
 *			- isUnique:		boolean, does the Checkbox object represents a
 *							single option (on/off)
 *	Returns	- value as String if Checkbox provides multiple options
 *			- values in a String[], in the order requested if Checkbox
 *				provides multiple options
 *			- null if no Checkbox options are checked
 */
function getCheckboxValues(chkboxObj, isUnique)
{
    if(isUnique != null && isUnique && chkboxObj.checked)
    {
        return chkboxObj.value;
    }
    else if(isUnique == null || !isUnique)
    {
        var valueArray = new Array();
        for(var i = 0; i < chkboxObj.length; i++)
        {
            if(chkboxObj[i].checked) valueArray.push(chkboxObj[i].value);
        }
        return ((valueArray.length > 0) ? valueArray.reverse() : null);
    }
    else
        return null;
}

/*	CSJS function: getMultiSelectedValues(Select)
 *	Get the selected values in a multi-select listbox
 *	Params	- selectObj:	Select object (CSJS)
 *	Returns	- values in a String[] in the requested order
 *			- null if no values selected
 */
function getMultiSelectedValues(selectObj)
{
    var valueArray = new Array();
    for(var i = 0; i < selectObj.options.length; i++)
    {
        if(selectObj.options[i].selected)
            valueArray.push(selectObj.options[i].value);
    }
    return ((valueArray.length > 0) ? valueArray.reverse() : null);
}

/*	CSJS function: getSelectedValue(Select)
*	Get the selected value in a single-select listbox
*	Params	- selectObj:	Select object (CSJS)
*	Returns	- value as a String, if one is selected, else null
*/
function getSelectedValue(selectObj)
{
    var selIdx = selectObj.selectedIndex;
    return ((selIdx != -1) ? selectObj.options[selIdx].value : null);
}

/* 	CSJS function: multiValuesToQueryString(String, String[])
 *	Params	- paramName:		String
 *			- paramValueArray:	String[]
 *	Returns	- HTTP query string (String); empty string if paramValue array is
 *				not provided or has length 0
 */
function multiValuesToQueryString(paramName, paramValueArray)
{
    if(null == paramValueArray) return "";

    var queryString = "";
    for(var i = 0; i < paramValueArray.length; i++)
    {
        queryString += ((i > 0) ? "&" : "") + paramName + "=" + paramValueArray[i];
    }
    return queryString;
}

function dummyLoop(iterations)
{
    for(var n = 0; n < iterations; n++)
    {
        if(parent == null)
            alert("Parent is NULL");
        else if(parent.location == null)
            alert("Parent location is NULL");
        else
            continue;

        return;
    }
}

function showDoc(docURI, winName, w, h, otherFeatures)
{
    if(!winName || winName == '') winName = 'DocWin';
    if(!w || w == 0) w = 600;
    if(!h || h == 0) h = 450;
    if(!otherFeatures) otherFeatures = ',toolbar=no,menubar=yes,scrollbars,';
    else otherFeatures = ',' + otherFeatures;
    var newWin = window.open(docURI, winName, 'width=' + w + ',height=' + h + ',top=75,left=75,resizable=yes' + otherFeatures);
    newWin.focus();
}

function openLinkInPrimaryScreen(linkref)
{
    parent.Main.focus();
    parent.Main.location=linkref;
}

