//<!--
var thisUrl = location.href;
servername = thisUrl.substr(thisUrl.indexOf('//')+2,thisUrl.indexOf('/',thisUrl.indexOf('//')+2)-(thisUrl.indexOf('//')+2));

if (servername == '') servername = "localhost";


var App_VirtualRoot = "/cportal"
var App_ServerPath = thisUrl.substr(0,thisUrl.indexOf('/',thisUrl.indexOf('//')+2));
var App_WebRoot = App_ServerPath+App_VirtualRoot
//derive webroot from the url
var App_FullWebRoot = thisUrl.substr(0, thisUrl.indexOf('?'));
var App_ImageRoot = App_WebRoot+"/images/";

/* This Set of functions are used to simplify tab scrolling in cPortal PortalHeader.jsp */
function resetTabBand(tabType) {   
    if(document.getElementById("cp_cs_"+tabType+"_band")){              
        document.getElementById("cp_cs_"+tabType+"_band").value = 0;
    }
}
function scrollTabToLeft(tabType) {                 
    
    var currentBand = parseInt(document.getElementById("cp_cs_"+tabType+"_band").value,10);
    var maxBand = parseInt(document.getElementById("cpMaximum"+tabType+"Band").value,10);
    
    
    if(currentBand != 0){               
        if((currentBand-1) == 0){
            document.getElementById(tabType+"LeftScroll").style.display = "none"; 
        }
        if(currentBand < maxBand ){
            document.getElementById(tabType+"RightScroll").style.display = ""; 
            document.getElementById(tabType+"RightScroll2").style.display = "none";
        }                
        document.getElementById("cp_cs_"+tabType+"_band").value=currentBand-1;                
        document.getElementById(tabType+"Band"+(currentBand-1)).style.display = ""; 
        document.getElementById(tabType+"Band"+currentBand).style.display = "none";
        
    }
    
}

function scrollTabToRight(tabType) {
    var currentBand = parseInt(document.getElementById("cp_cs_"+tabType+"_band").value,10);
    var maxBand = parseInt(document.getElementById("cpMaximum"+tabType+"Band").value,10);
    
    if(currentBand < maxBand){               
        if((currentBand+2) == maxBand){				 
            document.getElementById(tabType+"RightScroll").style.display = 'none'; 				
            document.getElementById(tabType+"RightScroll2").style.display = ""; 
            
        }
        if(currentBand < maxBand){
            document.getElementById(tabType+"LeftScroll").style.display = ""; 
        }
        document.getElementById("cp_cs_"+tabType+"_band").value=currentBand+1;                
        document.getElementById(tabType+"Band"+(currentBand+1)).style.display = ""; 
        document.getElementById(tabType+"Band"+currentBand).style.display = "none";
        
    }    
    
}

function doSubmitTab(tabname) {
    var theform = document.getElementById("defaultForm");
    if(theform){
        theform.TabName.value = tabname;    
        theform.submit();
    }else{
        theform = document.defaultForm;
        theform.TabName.value = tabname;    
        theform.submit();
    }
}

/* End of Tab specific functions */


function App_NewWindow(pUrl,pWidth,pHeight,pTop,pLeft,pEtc) {
    win_New = window.open(pUrl,"win_New","width="+pWidth+",height="+pHeight+",top="+pTop+",left="+pLeft+pEtc);
    win_New.focus();
}

function fullScreen(pUrl) {
    App_NewWindow(pUrl,window.screen.width,window.screen.height,0,0,", toolbar=yes, menubar=yes");
}

function openWindow(pUrl) {
    App_NewWindow(pUrl,window.screen.width-100,window.screen.height-100,10,10,", toolbar=yes, menubar=yes, scrollbars=yes, status=yes, resizable=yes");
    
}
function windowClose(part,moduleWindowState) {
    var thisPart = document.all[part];
    if (thisPart.style.display == '') {
        thisPart.style.display = "none";		
    } else {
        thisPart.style.display = "";		
    }
    document.all[moduleWindowState].value=document.all["ClosedWindowValue"].value;	
    //document.getElementById(moduleWindowState).value=document.getElementById("ClosedWindowValue").value;
    
}

function windowMaximize(moduleWindowState) {
    //document.getElementById(moduleWindowState).value=document.getElementById("MaximizedWindowValue").value;
    document.all[moduleWindowState].value=document.all["MaximizedWindowValue"].value;
}
function windowRestoreMinimize(part,alt,moduleWindowState) {
    var thisPart = document.all['part'+part];
    var thisImg = document.all['min'+part];
    if (thisPart.style.display == '') {
        thisPart.style.display = "none";
        thisImg.src = App_ImageRoot+"restore"+alt+".gif";
        document.getElementById(moduleWindowState).value=document.getElementById("MinimizedWindowValue").value;
        //document.all[moduleWindowState].value=document.all["MinimizedWindowValue"].value;
    } else {
        thisPart.style.display = "";
        thisImg.src = App_ImageRoot+"min"+alt+".gif";
        document.getElementById(moduleWindowState).value=document.getElementById("NormalizedWindowValue").value;
        //document.all[moduleWindowState].value=document.all["NormalizedWindowValue"].value;
    }
}
function windowRestoreMaximize(moduleWindowState) {
    if(document.getElementById(moduleWindowState)){
        document.getElementById(moduleWindowState).value=document.getElementById("NormalizedWindowValue").value;
    }else{
        document.all[moduleWindowState].value=document.all["NormalizedWindowValue"].value;
    }
}

function App_MinMax(part,alt) {
    var thisPart = document.all['part'+part];
    var thisImg = document.all['min'+part];
    if (thisPart.style.display == '') {
        thisPart.style.display = "none";
        thisImg.src = App_ImageRoot+"plus"+alt+".gif";
    } else {
        thisPart.style.display = "";
        thisImg.src = App_ImageRoot+"minus"+alt+".gif";
    }
}
function App_MinMax(part,alt,moduleWindowState) {
    var thisPart = document.all['part'+part];
    var thisImg = document.all['min'+part];
    if (thisPart.style.display == '') {
        thisPart.style.display = "none";
        thisImg.src = App_ImageRoot+"plus"+alt+".gif";
        document.getElementById(moduleWindowState).value=document.getElementById("MinimizedWindowValue").value;
    } else {
        thisPart.style.display = "";
        thisImg.src = App_ImageRoot+"minus"+alt+".gif";
        document.getElementById(moduleWindowState).value=document.getElementById("NormalizedWindowValue").value;
    }
}
function disableEnterKey(){
    if(event.keyCode == 13) event.returnValue = false;
    event.cancelBubble = true
}

//still leave for now, study more with time
function App_FindMatch(param) {
    var sURL =  App_VirtualRoot+"/services/peopleFinder.aspx?"+param;
    App_NewWindow(sURL,700,400,100,50,'",scrollbars=1"');
}

function Footer_Link(pagename) {
    var sURL = App_VirtualRoot + "/services/footerLinks.aspx?id=" + pagename;
    App_NewWindow(sURL,400,300,100,50,'",scrollbars=1"');
}


function doOnload(redirect, newUrl) {
    if (redirect) {
        //move to specified page
        window.location.href = newUrl;
    } else {
        document.getElementById('splashScreen').style.visibility='hidden';
        document.getElementById('contentScreen').style.display='';
    }
}

function loadHelp() {
    window.location.href = App_FullWebRoot + "?Event=default&TabName=Help";
}

function doSubmit(target, action) {
    doSubmitAllModules(target, action);	
    //doSubmitPage(target, action);	
}
function doSubmitPage(target, action) {
    var theform = document.defaultForm;
    theform.Target.value = target;
    theform.Action.value = action;
    theform.submit();
}


function doSubmitState(target, action,windowState) {		
    //fn_AjaxSubmit(target, action, windowState,"","RefreshContent", true);
    doSubmitState2(target, action,windowState);
}
function doSubmitState2(target, action,windowState) {
    var theform = document.defaultForm;
    theform.Target.value = target;
    theform.Action.value = action;
    theform.WindowState.value = windowState;
    theform.submit();
}



function showAlert(msg) {
    alert(msg);
    
}

// Ajax stuff

function doAjaxCall(target, method, paramArray, callBack, asynch){
    outerCallback = callBack;
    var params = "Event=ajax&Target="+target+"&CallType=SingleValue&CallFunction="+method;
    for(i=0;i < paramArray.length;i++){
        params+="&CallParameter="+paramArray[i];
    }
    return sendAjaxRequest(App_FullWebRoot+"?"+params, asynch, callSubmitCallback);
}
function showLoadDiv(target){
    
    //var moduleDiv = document.getElementById("modulePage"+target);
    var procDiv = document.getElementById("processingPage"+target);
    if(procDiv){
        //moduleDiv.style.display = "none";
        procDiv.style.display = "";
    }
}
function hideLoadDiv(target){
    
    var actualTarget = target.substring(target.indexOf("Pane")+4,target.length);
    if(actualTarget=="Div"){
        //this is hard coded to be equal to the value of 
        //com.sw.portal.bean.DefaultBean.PORTAL_MODULE_BEAN = -1000
        actualTarget=-1000;
    }
    var procDiv = document.getElementById("processingPage"+actualTarget);
    if(procDiv){
        //moduleDiv.style.display = "none";
        procDiv.style.display = "none";
    }
}
var req;
var outerCallback;
var _sFormData;
/*
function fn_AjaxSubmit(target, action, windowState,extraParam,callType, asynch){
 
    showLoadDiv(target);
    //alert("CALL 2");
    var divs = document.getElementsByTagName("div");
    var params = "Event=ajax&Target="+target+"&Action="+action+"&WindowState="+windowState+"&CallType="+callType+extraParam;
    for(i=0;i < divs.length;i++){
        if(divs[i].id==null) continue;
        if(divs[i].id.indexOf(target) >= 0) break;
    }
    if(i < divs.length){
        var elements = divs[i].all;
        for(j=0;j<elements.length;j++){
            if(elements[j].name==null) continue;
 
            if(elements[j].type=="checkbox"){
                if(elements[j].checked){
                    params+="&"+elements[j].name+"="+elements[j].value;
                }
            }else{
                params+="&"+elements[j].name+"="+elements[j].value;
            }
 
        }
    }
    //alert(params.length);
    if(params.length<2000){
        //alert("using get");
        if(!sendAjaxRequest(App_FullWebRoot+"?"+params, asynch, ajaxSubmitCallback)){
            doSubmitState2(target, action,windowState);
        }
    }else{
        //alert("using post");
        if(!sendAjaxPostRequest(App_FullWebRoot,params, asynch, ajaxSubmitCallback)){
            doSubmitState2(target, action,windowState);
        }
    }
 
}
 */
function encodeURIComponent(param){
    return param;
}
function fn_AjaxSubmit(target, action, windowState,extraParam,callType, asynch){
    
    showLoadDiv(target);
    //alert("CALL 2");
    var oForm = document.getElementById("defaultForm");
    var oElement, oName, oValue, oDisabled;
    var hasSubmit = false;
    _sFormData="";
    // Iterate over the form elements collection to construct the
    // label-value pairs.
    //alert("FormData Length = " +oForm.elements.length );
    for (var i=0; i<oForm.elements.length; i++){
        oDisabled = oForm.elements[i].disabled;
        
        // If the name attribute is not populated, the form field's
        // value will not be submitted.
        oElement = oForm.elements[i];
        oName = oForm.elements[i].name;
        oValue = oForm.elements[i].value;
        //alert( oName + oValue);
        // Do not submit fields that are disabled or
        // do not have a name attribute value.
        if(!oDisabled && oName && oName!="Action" && oName!="Target" && oName!="WindowState" ) {
            switch (oElement.type) {
                case 'select-one':
                case 'select-multiple':
                    for(var j=0; j<oElement.options.length; j++){
                        if(oElement.options[j].selected){
                            _sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oElement.options[j].value || oElement.options[j].text) + '&';
                        }
                    }
                    break;
                case 'radio':
                case 'checkbox':
                    if(oElement.checked){
                        _sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
                    }
                    break;
                case 'file':
                    // stub case as XMLHttpRequest will only send the file path as a string.
                case undefined:
                    // stub case for fieldset element which returns undefined.
                case 'reset':
                    // stub case for input type reset button.
                case 'button':
                    // stub case for input type button elements.
                    break;
                case 'submit':
                    if(hasSubmit == false){
                        _sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
                        hasSubmit = true;
                    }
                    break;
                default:
                    _sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
                    break;
            }
        }
    }
    
    
    _sFormData+="Event=ajax&Target="+target+"&Action="+action+"&WindowState="+windowState+"&CallType="+callType+extraParam;
    
    //alert(_sFormData);
    if(_sFormData.length<2000){
        //alert("using get");
        if(!sendAjaxRequest(App_FullWebRoot+"?"+_sFormData, asynch, ajaxSubmitCallback)){
            doSubmitState2(target, action,windowState);
        }
    }else{
        //alert("using post");
        if(!sendAjaxPostRequest(App_FullWebRoot,_sFormData, asynch, ajaxSubmitCallback)){
            doSubmitState2(target, action,windowState);
        }
    }
    
}




function fn_AjaxSubmitNoTargetParams(target, action, windowState,extraParam,callType, asynch){
    
    showLoadDiv(target);
    //alert("CALL 2");
    
    var params = "Event=ajax&Target="+target+"&Action="+action+"&WindowState="+windowState+"&CallType="+callType+extraParam;
    
    //alert(params.length);
    if(params.length<2000){
        //alert("using get");
        if(!sendAjaxRequest(App_FullWebRoot+"?"+params, asynch, ajaxSubmitCallback)){
            doSubmitState2(target, action,windowState);
        }
    }else{
        //alert("using post");
        if(!sendAjaxPostRequest(App_FullWebRoot,params, asynch, ajaxSubmitCallback)){
            doSubmitState2(target, action,windowState);
        }
    }
    
}


function doSubmitAjax(target, action){
    fn_AjaxSubmit(target, action, "","", "RefreshCaller", true);
}
function doRefreshAjax(target, action){
    fn_AjaxSubmit(target, action, "","", "RefreshAll", true);
}
function doSubmitModule(target, action){
    //fn_AjaxSubmit(target, action, "","", "RefreshCaller", true);
    doSubmitPage(target,action);
}
function doSubmitPane(target, action){
    fn_AjaxSubmit(target, action, "","", "RefreshPane", true);
}

function doSubmitAllModules(target, action){
    //fn_AjaxSubmit(target, action, "","", "RefreshAll", true);
    doSubmitPage(target,action);
}
/* Used by framework modules */
function ajax_doSubmit(target, action){
    fn_AjaxSubmit(target, action, "","", "RefreshAll", true);
    
}
function ajax_doSubmitAllModules(target, action){
    fn_AjaxSubmit(target, action, "","", "RefreshAll", true);
    
}
function ajax_doSubmitModule(target, action){
    fn_AjaxSubmit(target, action, "","", "RefreshCaller", true);
    
}
function ajax_doLinkSubmit(target, action){
    fn_AjaxSubmitNoTargetParams(target, action, "","", "RefreshAll", true);
    
}
function ajax_doLinkSubmitAllModules(target, action){
    fn_AjaxSubmitNoTargetParams(target, action, "","", "RefreshAll", true);
    
}
function ajax_doLinkSubmitModule(target, action){
    fn_AjaxSubmitNoTargetParams(target, action, "","", "RefreshCaller", true);
    
}

function doSubmitAllModules_NoTargetParams(target, action){
    fn_AjaxSubmitNoTargetParams(target, action, "","", "RefreshAll", true);
    //doSubmitPage(target,action);
}

function doRefreshContent(target, action){
    fn_AjaxSubmit(target, action,"","", "RefreshContent", true);
}

function doRefreshContentWithParam(target, action,paramName,paramValue){
    fn_AjaxSubmit(target, action,"","&"+paramName + "="+paramValue, "RefreshContent", true);
}
function doRefreshContentWithParam_NoTargetParams(target, action,paramName,paramValue){
    fn_AjaxSubmitNoTargetParams(target, action,"","&"+paramName + "="+paramValue, "RefreshContent", true);
}

function doRefreshPane(target, action,paramName,paramValue){
    fn_AjaxSubmit(target, action,"","&"+paramName + "="+paramValue, "RefreshPane", true);
}

function doRefreshPane_NoTargetParams(target, action,paramName,paramValue){
    fn_AjaxSubmitNoTargetParams(target, action,"","&"+paramName + "="+paramValue, "RefreshPane", true);
}

function callSubmitCallback(){
    if(req.readyState == 4){
        var resp = req.responseText;
        var myRegExp = /\<result\>\s*\<\!\[CDATA\[\s*([\d|\D]*)\s*\]\]\>\s*\<\/result\>/;
        var results = myRegExp.exec(resp);
        myRegExp = /\<error_message\>\s*\<\!\[CDATA\[\s*([\d|\D]*)\s*\]\]\>\s*\<\/error_message\>/
        var error = myRegExp.exec(resp);
        if(error){
            showAlert(error[1]);
        }else	if(results){
            outerCallback(results[1]);
        }else{
            document.close();
            document.write(resp);
        }
        
    }//end if
}
function ajaxSubmitCallback(){
    if(req.readyState == 4){
        var resp = req.responseText;        
        var myRegExp = /\<module\>\s*\<id\>(\w+)\<\/id\>\s*\<content\>\s*\<\!\[CDATA\[\s*([\d|\D]*?)\s*\]\]\>\s*\<\/content\>\s*\<\/module\>/g;
        var results = myRegExp.exec(resp);
        if(results){
            while(results){
                var divId = results[1];
                hideLoadDiv(divId);
                var theDiv = document.getElementById(divId);
                if(theDiv){
                    theDiv.innerHTML=results[2];
                }
                results = myRegExp.exec(resp);
            }
        }else{
            document.close();
            document.write(resp);
        }
        myRegExp = /\<error_message\>\s*\<\!\[CDATA\[\s*([\d|\D]*)\s*\]\]\>\s*\<\/error_message\>/
        results = myRegExp.exec(resp);
        if(results){
            showAlert(results[1]);
        }
        
    }//end if
}





function sendAjaxRequest(url, asynch, callback){
    req = false;
    try{
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        
    }catch(e){
        
        req = false;
    }
    if (req) {
        
        req.onreadystatechange = callback;
        req.open("GET", url, asynch);
        req.send(null);
        
        return true;
    }else{
        return false;
    }
}


function sendAjaxPostRequest(url,params, asynch, callback){
    // return false;
    
    req = false;
    try{			
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }catch(e){
        req = false;
    }
    if (req) {		
        req.onreadystatechange = callback;
        req.open("POST", url, asynch);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(params);
        
        return true;
    }else{
        return false;
    }
    
}
function doLoginSubmit(){
    var IE = (navigator.appName.indexOf("Microsoft") >= 0);
    var loginForm = document.loginForm;
    var key = new Date().getTime();
    var password = loginForm.UserPassword1.value;
    loginForm.UserPassword.value = MD5(password + key);
    loginForm.LoginKey.value = key;
    loginForm.UserPassword1.disabled = true;
    loginForm.submit();
}
function waitPreloadPage() { //DOM
    if (document.getElementById){
        document.getElementById('prepage').style.visibility='hidden';
    }else{
        if (document.layers){ //NS4
            document.prepage.visibility = 'hidden';
        }
        else { //IE4
            document.all.prepage.style.visibility = 'hidden';
        }
    }
}
// End -->