function setResourceSetId(setId)
{
    var id = setId;
    var pos = id.indexOf("_");
    if (pos > 0) id = id.substring(pos+1, id.length);
    document.edit_res_prefs.set.value = id;
}
function setResourceId(resourceId)
{
    var id = resourceId;
    var pos = id.indexOf("_");
    if (pos > 0) id = id.substring(pos+1, id.length);
    document.edit_res_prefs.resource.value = id;
}
function loadResource(htmlRef, template, resourceSet, resourceId)
{
    var resourceForm = document.forms["form_" + htmlRef];
    resourceForm.template.value = template;
    resourceForm.set.value = resourceSet;
    resourceForm.resource.value = resourceId;

    var params = new Array();
    params["formName"] = "form_" + htmlRef;
    params["divId"] = "div_" + htmlRef;

    submitAjaxReq(resourceForm, resourceForm.action, renderPortlet, params, 'process_portlet_content');
}
function linkToResource(pageId, selectedSet, selectedResource)
{
    var pageUrl;
    if (pageId.indexOf("/") > -1) {
        pageUrl = pageId + '?p_p_process=1&p_p_globalparams=1';
    }
    else {
        pageUrl = '/main/portal/layout?p_l_id=' + pageId + '&p_p_process=1&p_p_globalparams=1';
    }

    if (selectedSet) {
        pageUrl += "&sSet=" + selectedSet;
    }
    if (selectedResource) {
        pageUrl += "&sResource=" + selectedResource;
    }
    window.location=pageUrl;
}
function openResourceWindow(url)
{
    var windowOptions = 'directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no';
    window.open(url, windowOptions);
}//Formatting
function highlight(id, color, lastHighlighted)
{
    var el = document.getElementById(id);
    if (document.styleSheets)
    {
        if (rs_lastHighlighted) {
            rs_lastHighlighted.style.backgroundColor='white';
        }
        el.style.backgroundColor='lightblue';
        rs_lastHighlighted = el;
    }
}
function getURLParam(strParamName)
{
  var strReturn = "";
  var strHref = window.location.href;
   if ( strHref.indexOf("?") > -1 )
  {
    var strQueryString =  strHref.substr(strHref.indexOf("?")).toLowerCase();
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
     {
      if (aQueryString[iParam].indexOf(strParamName + "=") > -1 )
       {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return strReturn;
}//Ajax
function renderPortlet(xmlHttpReq, params)
{
    var html = xmlHttpReq.responseText;

    if (typeof params == "string") {
            var portletDivId = params;
    } else {
            var portletDivId = params["divId"];
            var portletFormName = params["formName"];
            html = html.replace("form_false",portletFormName);
            html = html.replace("div_false",portletDivId);
    }

    document.getElementById(portletDivId).innerHTML = html;
    document.body.style.cursor = "default";
}
function submitAjaxReq(form, actionUrl, returnFunction, returnArgs, strutsAction)
{
    var action = actionUrl;
    var pos = action.indexOf("?");
    var path = action;
    var queryString = "";

    if (strutsAction == undefined) {
        var strutsAction = 'process_portlet';
    }

    if (pos != -1)
    {
        path = action.substring(0, pos);
        queryString = action.substring(pos + 1, action.length);
    }

    if (!endsWith(queryString, "&")) {
        queryString += "&";
    }
    for (var i = 0; i < form.elements.length; i++)
    {
        var e = form.elements[i];
        if ((e.name != null) && (e.value != null)) {
            queryString += e.name + "=" + e.value + "&";
        }
    }

    document.body.style.cursor = "wait";
    pos = path.indexOf("/portal/layout");
    if (pos == -1) {
        path = "/main/portal/" + strutsAction;
    } else {
        path = path.substring(0, pos) + "/portal/" + strutsAction;
    }
    if (queryString == null) {
        queryString = "";
    }
    if (!endsWith(queryString, "&")) {
        queryString += "&";
    }
    queryString += "r=" + random();

    if (returnFunction == null) {
        returnFunction = function () {};
    }

    var xmlHttpReq;
    try {
        xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttpReq = new XMLHttpRequest();
            }
            catch (e) {
                document.body.style.cursor = "default";
            }
        }
    }

    try {
        xmlHttpReq.open("POST", path, true);
        xmlHttpReq.setRequestHeader("Method", "POST " + path + " HTTP/1.1");
        xmlHttpReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpReq.send(queryString);
        xmlHttpReq.onreadystatechange =
            function() {
                if (xmlHttpReq.readyState == 4) {
                    if (xmlHttpReq.status == 200) {
                        document.body.style.cursor = "default";
                        returnFunction(xmlHttpReq, returnArgs);
                    }
                }
            };
    }
    catch (e) {
        document.body.style.cursor = "default";
    }
    document.body.style.cursor = "default";
}
