//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;
}

