﻿// XpertHR.js
// Generic functionality for XpertHR site, starting ...


// Control window size and move to (50,50) from top left
function MoveTopLeftAndTrim(x,y,scroll) 
{
	window.moveTo(50,50);
	
	if(scroll)
	{
	    window.location.hash=window.location.href.match(/#(\w.+)/)[1]; 
	}
}

function popupNoNav(url)
{
	window.open(url, 'NoNavWindow', 'resizable=yes, scrollbars=yes, status=yes');
}


// (25/06/2007) GF: Method to get an object given its ID, works cross browser 
function getObjectById(id) {                          
 var obj;                                             
 
 if (document.getElementById)
 {                        
    obj = document.getElementById(id);                  
 }
 else if(document.layers)
 { 
    obj = document.layers[id];
 }
 else
 {                                               
    obj = document.all.item(id);                        
 } 
 
 return obj;                                        
}


// Method to hide an accessible button by ID passed
function hideAccessibleButton(clientID)
{
    var button = getObjectById(clientID);
    if (button) {
        button.style.display = 'none';
    }
}


// switch syle sheets (see http://www.alistapart.com/stories/alternate/)
function setActiveStyleSheet(title) 
{
    var i, a, main;
    for(i=0; (a = document.getElementsByTagName("link")[i]); i++) 
    {
        if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) 
        {
            a.disabled = true;
            if(a.getAttribute("title") == title) a.disabled = false;
        }
    }
}

// - 21/8/07 -MG: cookie functions (http://www.quirksmode.org/js/cookies.html)
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	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(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


// Stephan Heitmeyer 05-NOV-2007
// Called from /employmentlaw/legaladviceemails/default.aspx
function ToggleDisplay(idDiv, idImg) {
    eDiv = document.getElementById(idDiv);
    eImg = document.getElementById(idImg);
    if (eDiv!=null && eImg!=null) {
        if (eDiv.style.display=='none') {
            eDiv.style.display = 'block';
            eImg.src = '/App_Themes/default/Images/minus.gif'; 
            eImg.alt = 'collapse';
        } else {
            eDiv.style.display = 'none';
            eImg.src = '/App_Themes/default/Images/plus.gif'; 
            eImg.alt = 'expand';
        }
    }
}
  
  
/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}



// Stephan Heitmeyer 10-MAR-2008
// Onclick handler for match-highlighted spans created in ArticleUtility.HighlightHtml()
// Status: experimental (currently works in FF2.0, not in IE6 or Opera 9)
function JumpToNeighbourMatch(aEvent, name)
{
	var event = window.event ? window.event : aEvent;
	var forward = !event.altKey;
	var callerElement = event.srcElement ? event.srcElement : event.target;

    // get candidate elements by given name attribute (works for FF)
	var matches = document.getElementsByName(name);
	// work-around for IE and Opera
	if (matches.length==0) {
	    var matches = new Array();
	    var tagMatches = document.getElementsByTagName('span');
	    for (var i=0; i<tagMatches.length; i++ ) {
	        var e = tagMatches[i];
            if (e.attributes["name"])
	            eleName = e.attributes["name"].value;               // Opera requires it spelled out like this
            else
	            eleName = e.name;
    if (eleName == name) matches.push(e);
	    }
	}
	
    // nothing found: return
	if (matches.length==0) return;

    // identify current (clicked) element in list, next and previous
	var i=0;
	while (matches[i] != callerElement && i < matches.length) {
		i++;
	}
	if (forward)
		i++;
	else
		i--;
    // circularity
	if (i<0) i = matches.length-1;
	if (i>=matches.length) i = 0;

	// scroll to and 'select' the new current: next - near bottom, previous - near top of window
	matches[i].scrollIntoView(!forward);
	matches[i].className = "highlightCurrent";

	// reset all the other match classes to "highlight"
	for (j=0; j<matches.length; j++)
		if (j!=i) matches[j].className = "highlight";

    // scroll it 30px away from the edge
    // (That's not going to work ok for FF where scrollIntoView() behaves differently; so leave it)
//	if (forward) 
//	    window.scrollBy(0, 30);
//	else
//	    window.scrollBy(0, -30);
}


// CLIENT-SIDE HIGHLIGHT TOGGLING

// If id identifies an <A> element, then change its href to the specified link.
// This is typically called from a javascript event handler on an element (like onfocus) 
// to apply the javascript implementation of some functionality where javascript is enabled.
function JS_ChangeLink(id, link)
{
    var ele = document.getElementById(id); 
    if (ele!=null && ele.nodeName.toUpperCase()=='A') ele.href = link;
}

function ToggleHiliteStyle(linkId, action)
{
    var callerElement = document.getElementById(linkId); 
    var browser='IE';
    if (callerElement.textContent) browser = 'FF';
    var label = browser=='IE' ? callerElement.innerText : callerElement.textContent;
    // If action does not specify whether On or Off, look at link text to determine
    if (action==0) {
        if (label.toLowerCase().indexOf('highlighting off') >= 0) action = -1;
        if (label.toLowerCase().indexOf('highlighting on') >= 0) action = 1;
    }
    // Delete or insert hilite rule, whatever action specifies, if still 0: probe.
    // Then adapt the link text for the opposite action (2*2*2: on-->off | off-->on, lowercase | uppercase, textContent | innerText)
    var toInsert = false;
    if (action<=0)
        toInsert = !DeleteHiliteRule('.highlight');
                  //================
    if (action==1 || (action==0 && toInsert)) {
        InsertHiliteRule('rbi.css', '.highlight', ' {background-color:#FEE902}');
      //================
        if (callerElement.textContent) {
            callerElement.textContent = callerElement.textContent.replace(/ on/,' off');
            callerElement.textContent = callerElement.textContent.replace(/ On/,' Off');
        } else {
            callerElement.innerText = callerElement.innerText.replace(/ on/,' off');
            callerElement.innerText = callerElement.innerText.replace(/ On/,' Off');
        }
    } else {
        if (callerElement.textContent) {
            callerElement.textContent = callerElement.textContent.replace(/ off/,' on');
            callerElement.textContent = callerElement.textContent.replace(/ Off/,' On');
        } else {
            callerElement.innerText = callerElement.innerText.replace(/ off/,' on');
            callerElement.innerText = callerElement.innerText.replace(/ Off/,' On');
        }
    }
}

function DeleteHiliteRule(classSelector)
{
    var deleted = false;                // highlight rule found and deleted?
    for (var i=0; i < document.styleSheets.length; i++) {
        var ss = document.styleSheets[i];
        var cssRules = (ss.cssRules) ? ss.cssRules : ss.rules;      // browser switch
        var j=0;
        while (j < cssRules.length) {
            if (cssRules[j].selectorText==classSelector) {
                try {
                    ss.deleteRule(j);
                } catch (ex) {
                    ss.removeRule(j);
                }                             
                deleted = true;
            }
            else
                j++;
        }
    }
    return deleted;
}
function InsertHiliteRule(stylesheetName, selector, cssproperty)
{
    // Find the stylesheet to insert into
    var ss = '';
    for (var i=0; i < document.styleSheets.length; i++) {
        if (document.styleSheets[i].href.toLowerCase().indexOf(stylesheetName.toLowerCase()) >= 0)
            ss = document.styleSheets[i];
    }
    // Insert the rule
    if (ss!='') 
        try {
            ss.insertRule(selector + cssproperty, 0);   
        } catch (ex) {
            ss.addRule(selector, cssproperty); 
        }
}

// jp-related

function lbxZoomIn(lbxId, nRows, key, prompt) {
    var lbx = document.getElementById(lbxId);
    var nRowsMax = 7;
    lbx.size = nRows;
    if (lbx.options.length < lbx.size) {
        lbx.size = lbx.options.length;
    }
    
    if (lbx.options.length > nRowsMax) {
        var btnApplyId = lbx.parentNode.parentNode.getElementsByTagName("input")[0].id;
        w = window.open('', 'SelectorPopup', 'location=no,menubar=no,toolbar=no,status=no,height=200,width=200,resizable=yes');
        
        w.document.write('<html><head><title>' + key + '</title></head>');
        // generate some js for ok / cancel
        w.document.write('<script>');
        w.document.write('function ResizeToFit() {');
        w.document.write('  var wrp = document.getElementById("wrapper");');
        w.document.write('  var sel = document.getElementById("popupSelector");');
        w.document.write('  var deltaX = window.outerWidth - window.innerWidth;');
        w.document.write('  var deltaY = window.outerHeight - window.innerHeight;');
        w.document.write('  window.resizeTo(sel.clientWidth+deltaX+40, wrp.clientHeight+deltaY+40);');
        w.document.write('}');
        w.document.write('function CancelPopup(btnApplyId) {');
        w.document.write('  var btn = opener.document.getElementById(btnApplyId);');
        w.document.write('  btn.focus();');   // to avoid onfocus of the SELECT firing again when popup closes
        w.document.write('  self.close()');   // now go, silently
        w.document.write('}');
        w.document.write('function CancelIfESC(e, btnApplyId) {');
        w.document.write('  if(e.which==0) CancelPopup(btnApplyId);');
        w.document.write('}');
        w.document.write('function FinalizePopup(btnApplyId, lbxId) {');
        w.document.write('  var btn = opener.document.getElementById(btnApplyId);');
        w.document.write('  var lbx = opener.document.getElementById(lbxId);');
        w.document.write('  var localSelect = document.getElementById("popupSelector");');
        w.document.write('  var nSelections = 0;');
        w.document.write('  for (var j=0; j<localSelect.options.length; j++) {');
        w.document.write('    lbx.options[j].selected = localSelect.options[j].selected;');   // transfer selections to orig page
        w.document.write('    if (localSelect.options[j].selected) nSelections++;');
        w.document.write('  }');
        w.document.write('  if (nSelections > 0) btn.click();');   // submitting the popup is as good as clicking apply
        w.document.write('  btn.focus();');   // to avoid onfocus of the SELECT firing again when popup closes
        w.document.write('  self.close()');   // now go, silently
        w.document.write('}');
        w.document.write('</script>');
        w.document.write('<style type="text/css">.redText { color:#FF0000; }</style>');
        w.document.write('</head>');
        // create the popup content on the fly
        w.document.write('<body style="font-family:Verdana; text-align:center;" onkeypress="CancelIfESC(event, \'' + btnApplyId + '\')" onunload="CancelPopup(\'' + btnApplyId + '\')">');
        w.document.write('<div id="wrapper">');
        w.document.write('<h4 style="text-align:center">' + prompt + '</h4>');
        w.document.write('<div style="font-size: 9px">Ctrl+Click to multi-select</div>');
        w.document.write('<select id="popupSelector" size="' + Math.min(lbx.options.length, 50) + '" multiple="multiple">');
        for (var i=0; i<lbx.options.length; i++)
            w.document.write('<option value="' + lbx.options[i].value + '" class="' + lbx.options[i].className + '" style="font-size: 9px">' + lbx.options[i].text + '</option>');
        w.document.write('</select><br/>');
        w.document.write('<div><button onclick="CancelPopup(\'' + btnApplyId + '\')" style="font-size: 9px">cancel</button>&nbsp;&nbsp;');
        w.document.write('<button onclick="FinalizePopup(\'' + btnApplyId + '\', \'' + lbxId + '\')" style="font-size: 9px">ok</button></div>');
        w.document.write('</div></body><script>ResizeToFit()</script></html>');
    }

}
function lbxZoomOut(lbxId) {
    var lbx = document.getElementById(lbxId);
    lbx.size = 1;
    //updateCsv(id);
}

function lbxSummarizeSelections(lbxId, lblId) {
    var lbx = document.getElementById(lbxId);
    var lbl = document.getElementById(lblId);
    var s='';
    for (var i=0; i<lbx.options.length; i++)
        if (lbx.options[i].selected) {
            if (s=='')
                s = lbx.options[i].text;
            else
                s += ',' + lbx.options[i].text;
        }
    lbl.innerHTML = s;
}

// Specialized implementation of ToggleDisplay
// Toggles the display style between block and none for the DIV given by idDiv and an additional element given by idExtra.
// In parallel toggles the icon image (given as idImg) between minus.gif [collapse] and plus.gif [expand]
function ToggleFilterItemDisplay(idDiv, idExtra, idImg, toggle) {
    eDiv = document.getElementById(idDiv);
    eImg = document.getElementById(idImg);
    eExtra = document.getElementById(idExtra);
    if (eDiv!=null && eImg!=null) {
        if (toggle!='on' && toggle!='off') {
            if (eDiv.style.display=='none') 
                toggle='on';
            else
                toggle='off';
        }
        if (toggle=='on') {
            eDiv.style.display = 'block';
            eExtra.style.display = 'block';
            eImg.src = '/App_Themes/default/Images/minus.gif'; 
            eImg.alt = 'collapse';
        } else {
            eDiv.style.display = 'none';
            eExtra.style.display = 'none';
            eImg.src = '/App_Themes/default/Images/plus.gif'; 
            eImg.alt = 'expand';
        }
    }
}

function TickAll(cblId, csvId) {
    var cblRoot = document.getElementById(cblId);
    var options = cblRoot.getElementsByTagName("input");
    for (var i=0; i < options.length; i++)
        options[i].checked = true;
    cblSummarizeSelections(cblId,csvId);
}
function UntickAll(cblId, csvId) {
    var cblRoot = document.getElementById(cblId);
    var options = cblRoot.getElementsByTagName("input");
    for (var i=0; i < options.length; i++)
        options[i].checked = false;
    cblSummarizeSelections(cblId,csvId);
}
function UntickItem(objItemId) {
    var objItem = document.getElementById(objItemId);
    if(objItem!=null)
    {
        objItem.checked = false;   
    }        
}

function InverseTicks(cblId, csvId) {
    var cblRoot = document.getElementById(cblId);
    var options = cblRoot.getElementsByTagName("input");
    for (var i=0; i < options.length; i++)
        options[i].checked = !options[i].checked;
    cblSummarizeSelections(cblId,csvId);
}


function cblSummarizeSelections(cblId, lblId) {
    var cblRoot = document.getElementById(cblId);
    var options = cblRoot.getElementsByTagName("input");
    var texts = cblRoot.getElementsByTagName("label");
    var lbl = document.getElementById(lblId);
    var s='';
    for (var i=0; i<options.length; i++)
        if (options[i].checked) {
            if (s=='')
                s = texts[i].innerHTML;
            else
                s += '; ' + texts[i].innerHTML;
        }
    if (s=='') s='(unspecified)';
    lbl.innerHTML = s;
    // after any selection changes reflect that new selection is not applied yet
    lbl.className = "greyText smallText";
    var btn = cblRoot.nextSibling.nextSibling.getElementsByTagName("input")[0];   // this is not very robust...
    btn.removeAttribute("disabled");
}



