//Hire.com utility functions
// createPopup - open a window with the given url
// container - wrapper around array
// v_onLoad - functions to invoke with body onLoad event
// v_onSubmit - functions to invoke with form onsubmit event
//
//

// Look through all the forms on the page for the 
// one that contains the ERFormAction.  That is what
// we now use to determine which form on the page is 
// the one that submits the main form and does the work.
function findERMainForm(){
    var form = null;
    for (var i = 0;i < document.forms.length; i++){
        if (typeof document.forms[i].ERFormAction != 'undefined'){
            form = document.forms[i];
        }
    }
    if (form == null){
        alert("FindERMainForm is null.  Please contact customer support");
    }
    return form;
}


function createPopup( url, windowName, options ) {
    var w;
    w = window.open(url,windowName,options);
    if (w != null) {
        w.focus();
    }
    return false;
}

//Utility functions so components can
//have various methods invoked at onLoad and/or
//onSubmit events

var submitFunctions = new Array();
var loadFunctions   = new Array();

function addOnSubmitFunction(aFunction, anObject) {
	var aCallback = new callback(aFunction, anObject);
	submitFunctions[submitFunctions.length] = aCallback;
}
function addOnLoadFunction(aFunction, anObject) {
	var aCallback = new callback(aFunction, anObject);
	loadFunctions[loadFunctions.length] = aCallback;
}
function doOnLoad() {
	for (var i=0;i<loadFunctions.length;i++) {
	     var func = loadFunctions[i];
	     func.execute();
  	}
}
function doOnSubmit() {
	var rtn = true;
	for (var i=0;i<submitFunctions.length;i++) {
	     	var func = submitFunctions[i];
	     	rtn = func.execute();
	 	if (!rtn)
			return rtn;
  	}
	return rtn;

}
//callback
//calls the desired functionn with Object as parameter
function callback(aFunction, aObject) {
	this.f = aFunction;
	this.params = aObject;
	this.execute = execute;

	function execute() {
	   return this.f(this.params);
  }
}

function confirmAction(form, comp, msg) {
    if(msg==null || confirm(msg)) {
	form.ERFormAction.value=comp;
	doOnSubmit();
	form.submit();
    }
    return false;
}

function stripUnicode(str) {
    var newStr = '';
    var breakChar = "";
    var ASCIIregexp = /[\x00-\xFF]/
    for(var i=0;i<str.length;i++) {
        breakChar = str.charAt(i);
        if( ASCIIregexp.test(breakChar) ) {
            newStr = newStr + breakChar;
        }
    }

    return newStr ;
}
// Look through all the forms on the page for the 
// one that matches the name
function findForm(formname){
    var form = null;
    for (var i = 0;i < document.forms.length; i++){
        if (document.forms[i].name == formname){
            form = document.forms[i];
        }
    }
    if (form == null){
        alert("FindForm returns null");
    }
    return form;
}

function select(c){
    c.checked = true;
    return false;
}

function unSelect(c){
    c.checked = false;
}	

function xxselectAll(c, name){
	if(c.checked){
           checkAll(name);
        }
	else{
	   unCheckAll(name);
 	}

}

function checkAll(name){
   var erform = findERMainForm();
   var i = erform.elements.length;
   for(var j = 0;j<i;j++){
	var cb = erform.elements[j];
        if(cb.name == name){
            select(cb);
	}
   }
}

function unCheckAll(name){
   var erform = findERMainForm();
   var i = erform.elements.length;
   for(var j = 0;j<i;j++){
	var cb = erform.elements[j];
        if(cb.name == name){
            unSelect(cb);
	}
   }
}

// set current session flavor
// used only by flavornavbar component!
function setFlavor() {
    var url = window.location + "";
    url = url.replace(/lang=\w+&?/g,"");
    var dest = "lang=" + document.flavorselect.flavors.options[document.flavorselect.flavors.selectedIndex].value;
    var qindex = url.indexOf("?");
    if (qindex < 0) { 
        dest = url + "?" + dest; 
    } else { 
	if (qindex == (url.length-1)) {
	    // have a trailing "?", no need to add intervening "&"
            dest = url + dest;
	} else {
	    dest = url + "&" + dest;
        }
    }
    window.top.location = dest;
}

// returns the XMLHttpRequest object so that ajax calls can be made to the server
// if XMLHttp is not supported by browser reurn null
function getXmlHttp() {
	var xmlHttp;
	try {  
		// Firefox, Opera 8.0+, Safari  xmlHttp=new XMLHttpRequest();  
		xmlHttp=new XMLHttpRequest();
	} catch (e) {  
		// Internet Explorer  try
    	try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (e) {
    		try {
    			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    		} catch (e) {}; // browser does not support ajax
    	}  
    }
    return xmlHttp;
}

var sessionTimeoutMs=0; // the session time out for the current page
var timerShowTimeoutWarning=null; // timer used to showTimeoutWarning
var sessiontimoutmsg=null; // Message to display when about to timeout

// displays the warning that session is about to time out
// only if XmlHttp is supported by browser
function showTimeoutWarning()
{
	var xmlHttp = getXmlHttp();
	if (xmlHttp) {
		alert( sessiontimoutmsg );
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState==4) {
				//alert( xmlHttp.responseText );
				if ( -1 == xmlHttp.responseText.indexOf("SESSION_EXTENDED")) {
					// session not extended so go back to index
					window.location.href = "index.html";
				} else {
					// session extended so reset the timer
					setSessionTimers(); 
				}
			}
		}
		xmlHttp.open("GET", "hire.com/jsp/extendsess.jsp", true);
		xmlHttp.send(null);
	} 
}        

// start the timer for timeout warning
function setSessionTimers() {
	var warnBeforeTimeoutMs = 120000 // display warning alert 2 minutes before end of session
	clearSessTimeout();
	if ( sessionTimeoutMs >= warnBeforeTimeoutMs ) {
		timerShowTimeoutWarning = setTimeout( "showTimeoutWarning()", sessionTimeoutMs - warnBeforeTimeoutMs );
	}
}

// clear timeout warning timer when page unloads
function clearSessTimeout() {
	if ( timerShowTimeoutWarning ) {
		clearTimeout(timerShowTimeoutWarning);
		timerShowTimeoutWarning=null;
	}
}

// sets the seesion timeout and warning message
// this is called from the page from generated 
// code in StatMsg.java
function setSessionTimeOut( timeout, timeoutmsg ) {
	sessiontimoutmsg=timeoutmsg;
	sessionTimeoutMs = timeout * 1000;
	addOnLoadFunction( setSessionTimers, null);
	window.onUnLoad = clearSessTimeout;
}