﻿var callbackMap = new Object();

//  Do a synchronous callback to the server with the given context and a 
//  variable number of parameters.
function DoSyncCallback(context, postCall) {
    if (postCall != null && postCall.length > 0)
        callbackMap[context] = postCall;

    var callbackArgs = context;
    
    for (i = 2; i < arguments.length; i++) {
        callbackArgs += "|";
        if (arguments[i] != null) callbackArgs += arguments[i];
    }

    var callbackSync = "WebForm_DoCallback('__Page',callbackArgs,CallbackReturn,context,null,false)";
    eval(callbackSync);
}

//  Route server-side callback results.
function CallbackReturn(rvalue, context) {
    if (callbackMap[context] != null)
        eval(callbackMap[context] + "(rvalue);");
}

//  Create an return a delimited string with the given delimiter and a 
//  variable number of parameters.
function CreateDelimitedString(delimiter) {
    var retVal = "";
    
    for (i = 1; i < arguments.length; i++) {
        if (i > 1) retVal += delimiter;
        if (arguments[i] != null) retVal += arguments[i];
    }
    
    return retVal;
}

//  Return a "true" or "false" string value based on a boolean.
function BoolStr(boolVal) {
    return (boolVal) ? "true" : "false";
}

//  Trim all leading and trailing spaces and tabs from the given string.
function Trim(strStringIn) {
    var i;
    var strRetVal;
    var strOneChar;
    
    strRetVal = strStringIn;
    
    //  Do an RTrim() first...
    while (strRetVal.length > 0) {
        strOneChar = strRetVal.substr(strRetVal.length - 1, 1);
    
        if (strOneChar == " " || strOneChar == "\t")
            strRetVal = strRetVal.substr(0, strRetVal.length - 1);
        else
            break;
    }    
        
    while (strRetVal.length > 0) {
        strOneChar = strRetVal.substr(0, 1);
    
        if (strOneChar == " " || strOneChar == "\t")
            strRetVal = strRetVal.substr(1, strRetVal.length - 1);
        else
            break;
    }    
        
    return strRetVal;    
}

//  Integer divide.
function div(op1, op2) {
    return Math.floor(op1 / op2);
}

//  Generic callback result handler
function StandardEvalReturn(rValue) {
    eval(rValue);
}

//  Return a delimited string of the check items from an stwc:KMCheckBoxList
function GetCheckedListValues(chkboxList) {
    var selectedCheckBoxValues = "";                                            // string used for selected checkbox values
    var inputlist = chkboxList.getElementsByTagName("input");       // get only the input objects from the table

    for (var i = 0; i < inputlist.length; i++) {
        if ( inputlist[i].getAttribute("type") == 'checkbox' ) {                // look only at input elements that are checkboxes
  		    if (inputlist[i].checked) {
  		        if (selectedCheckBoxValues.length == 0)
                    selectedCheckBoxValues = selectedCheckBoxValues + inputlist[i].value;
                else
                    selectedCheckBoxValues = selectedCheckBoxValues + "|" + inputlist[i].value;
            }
        }
    }
    
    return selectedCheckBoxValues;
}

// Set CheckBoxList Checked to value of status
function SetCheckBoxListCheckedStatus(cbl, status) {
    var inputlist = cbl.getElementsByTagName("input");      // get only the input objects from the table

    for (var i = 0; i < inputlist.length; i++) {
        if ( inputlist[i].getAttribute("type") == 'checkbox' ) {   // look only at input elements that are checkboxes
            inputlist[i].checked = status;
        }
    }
}

//  Set the currently select radio button in a radio list.
function SetRadioListValue(ctrl, newValue) {
    var rdoList = ctrl.getElementsByTagName("input");
    
    for (var i = 0; i < rdoList.length; i++) {
        if ( rdoList[i].getAttribute("type") == 'radio' && rdoList[i].getAttribute("value") == newValue) {
            rdoList[i].checked = true;
            return;
        }
    }
}

var saveLoginURL = "";

//  Save off navigation information for Login.aspx
function SetLoginMap(loginURL, authenticatedURL, cancelURL) {
    saveLoginURL = loginURL;
    
    if(cancelURL.length == 0)
        cancelURL = authenticatedURL;
    
    DoSyncCallback("SetLoginMap", "PostSetLoginMap", authenticatedURL, cancelURL);
}

//  Callback result function.
function PostSetLoginMap(ralue) {
    window.location = saveLoginURL;
}

//  Useful cookie-handling functions taken and modified (slightly) from
//  http://www.quirksmode.org/js/cookies.html

//  Store a cookie name-value pair.
function SaveCookie(name,value,days) {
	var expires = "";

	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}

	document.cookie = name+"="+value+expires+"; path=/";
}

//  Return a cookie value by name.
function GetCookie(name) {
	var ca = document.cookie.split(';');

	name += "=";
	
	for(var i = 0; i < ca.length; i++) {
		var c = ca[i];
		
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(name) == 0) 
		        return c.substring(name.length,c.length);
	}
	
	return null;
}

//  Remove a named cookie name-value pair.
function DeleteCookie(name) {
	SaveCookie(name,"",-1);
}

