/****
 *  Common
 *  	Contains JavaScript functions common to many pages
 */


/****
 * $() - Cheap and easy way to getElementById
 * @param {String} anId - id of element to be referenced
 */
function $(anId){
	return document.getElementById(anId);
}

/****
 * equalColumns - Sets two CSS columns to have an equal CSS style height
 * @param {String} lCol - Left column
 * @param {String} rCol - Right column
 */
function equalColumns(lCol,rCol){
    l = document.getElementById(lCol);
    r = document.getElementById(rCol);
    
    r.offsetHeight >= l.offsetHeight ? l.style.height = r.offsetHeight + "px" : r.style.height = l.offsetHeight + "px";
}  

  
/****
 * For element el, appends "_on" to el's className for a mouseover effect
 * Appropriate classes must be defined in a selector for el
 * 
 * @param {Object} el Object to roll over
 */
function bgImg_on(el){
    if(el.className.indexOf("_on") == -1){
        el.className += "_on";  
    } 
}


/****
 * For element el, removes "_on" from el's className for a mouseout effect
 * Appropriate classes must be defined in a selector for el 
 * 
 * @param {Object} el Object to roll out
 */
function bgImg_off(el){
    var i = el.className.indexOf("_on");
    if(i != -1){
        el.className = el.className.substring(0,i); 
    }    
    i = null;             
}

/****
 * For registration pages. Determines whether or not chkBox (not US and Canadian)
 * is checked. If it is -> disables zip code box, shows and enables country box.
 * If not -> disable and hide country, enable zip.
 * 
 * @param {Object} chkBox Reference to checkbox object
 * Europe:location
 */
function evalCheck(chkBox){
    var zip = document.getElementById('zipCode');
    var country = document.getElementById('country');
    var countryRow = document.getElementById('countryRow');
    
    if(chkBox.checked){
      zip.value = "";
      zip.disabled = true;
      countryRow.style.display = "block";
      country.disabled = false;
      country.focus();  
    }
    else if (!chkBox.checked){
      zip.disabled = false;
      countryRow.style.display = "none";
      country.value = "";
      country.disabled = true;
    }
}

/****
 * Run onsubmit() for registration forms. Determines if zipCode is a disabled field
 * (the user has checked "not US and CAN box"). If so, enable zip box and set value to 
 * "00000" so that we don't submit a null value. 
 * Europe:location
 */  
function testZipCode(){
    var zip = document.getElementById('zipCode')
    
    if(zip.disabled){
      zip.value = "00000";
      zip.disabled = false;
    }
}

/****
 * For registration pages. Can be wired to onload to allow testing whether or not US/CAN
 * checkbox is checked on form completion errors. 
 * Europe:location 
 */  
function __load_testZipCode(){
    evalCheck(document.getElementById('notUSandCN'));
}

function toggleDiv(id) {
  var currDisplay = $(id).style.display;
  if (currDisplay == '') {
    $(id).style.display = 'none';
  }
  else {
    $(id).style.display = '';
  }
}

/****
 * Returns x coordinate of a mouse event
 * @param {Event} e Mouse event
 */  
function retX(e){
if(!e) var e = window.event;
if(e.pageX) return e.pageX 
else if (e.clientX) return e.clientX + document.documentElement.scrollLeft;
else return "40%";
}


/****
 * Returns y coordinate of a mouse event
 * @param {Event} e Mouse event
 */ 
function retY(e){
if(!e) var e = window.event;
if(e.pageY) return e.pageY;
else if (e.clientY) return e.clientY + document.documentElement.scrollTop;
else return "40%";
}

/****
 * Removes leading whitespaces
 */
function lTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

/****
 * Removes ending whitespaces
 */
function rTrim(value){
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

/****
 * Removes leading and ending whitespaces
 */
function trim(value){
	
	return lTrim(rTrim(value));
	
}

/****
 * Hides all select elements on a page
 */
function hideAllSelects(){
	var s = document.getElementsByTagName('select');
	for(var i = 0; i < s.length; i++){
		s.style.visibility = 'hidden';
	}
}

/****
 * Shows all hidden select elements on a page
 */
function showAllSelects(){
	var s = document.getElementsByTagName('select');
	for(var i = 0; i < s.length; i++){
		s.style.visibility = 'visible';
	}	
}

function stripTags(str)
{
  return str.replace(/<\/?[^>]+>/gi, '');
}


function escapeHTML(str)
{
  var div = document.createElement('div');
  var text = document.createTextNode(str);
  div.appendChild(text);
  return div.innerHTML;
}

function unescapeHTML(str)
{
  var div = document.createElement('div');
  div.innerHTML = stripTags(str);
  return div.childNodes[0] ? (div.childNodes.length > 1 ?
    $A(div.childNodes).inject('',function(memo,node){ return memo+node.nodeValue }) :
    div.childNodes[0].nodeValue) : '';
}

/* todo remove this hack when ManageJobs return the right data */
function fixLocation(locationStr)
{
  if (locationStr == null)
  {
    return "";
  }
  if (locationStr[0] == ",")
  {
    return trim(locationStr.substring(1));
  }
  return locationStr;  	
}

function getAllElementsById(tagName, id)
{
  var arr = new Array();
  var elementsForTag = document.getElementsByTagName(tagName);
  if (elementsForTag)
  {
    for (var i=0; i < elementsForTag.length; i++)
    {
      if (elementsForTag[i].id == id)
      {
        arr[arr.length] = elementsForTag[i];
      }
    }
  }
  return arr;
}

function getParentForm(obj)
{
   var theParent = obj.parentNode;
   while (theParent)
   {
       if (theParent == null)
       {
           break;
       }
       if (isForm(theParent))
       {
           break;
       }
       theParent = theParent.parentNode;
   }
   return theParent;
}

function isForm(obj)
{
   var bool = false;
   if (obj)
   {
       if (obj.nodeName.toLowerCase().indexOf("text") < 0)
       {
          if (obj.tagName) {
               bool = obj.tagName.toLowerCase().indexOf("form") > -1;
           }
       }
   }
   return bool;
}

function submitParentForm(obj)
{
    var formObj = getParentForm(obj);
    if (isForm(formObj))
    {
        formObj.submit();
    }
}

function getValueFromOptionList(theForm, optionListName)
{
    var checkedValue = null;
    if (theForm)
    {
        for (var i=0; i < theForm.elements.length; i++)
        {
            if (theForm.elements[i].name == optionListName)
            {
                if (theForm.elements[i].checked)
                {
                    checkedValue = theForm.elements[i].value;
                    break;
                }
            }
        }
    }
    return checkedValue;
}
