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

/****
 * 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 {objCheck} chkBox Reference to checkbox object
 * @param {objZip} input to be disabled and prepop with 00000 as zip code
 */
function evalCheckNoCountry(objCheck, objZip)
{
  var zipBox = null;
  zipBox = document.getElementById(objZip);
  if (objCheck.checked)
  {
    zipBox.disabled = true;
    zipBox.value = "00000";
  }
  else
  {
    zipBox.value = "";
    zipBox.disabled = false;
    zipBox.focus();
  }
}

/****
 * 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. 
 * 
 */  
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. 
 * 
 */  
function __load_testZipCode(){
    evalCheck(document.getElementById('notUSandCN'));
}

/****
  *  Function that returns an array of elements with an specified class name
  *  @param className String representing the class of the elements to be found
  *  @param aNode A valid DOM node (i.e. document) if null, document will be set to default
  *  @param aTag A valid String representation of a HTML tag (i.e. input, div) if null, it will be set to '*' which
  *         means all tags
  */
function getElementsByClassName(className, aNode, aTag){
  var returnEls = new Array();
  if(!aNode)
    aNode = document;
  if(!aTag) //aTag is not specified
    aTag = '*';
  var tempEls = aNode.getElementsByTagName(aTag);
  var regEx = new RegExp('\\b'+className+'\\b');
  for(i = 0, j = 0; i < tempEls.length; i++){
    if(regEx.test(tempEls[i].className)){
      returnEls[j] = tempEls[i];
      j++;
    }
  }
  return returnEls;
}

function evalCheckNoCountryInitial(objCheck, objZip)
{
  var zipBox = null;
  zipBox = document.getElementById(objZip);
  if (objCheck.checked)
  {
    zipBox.disabled = true;
    zipBox.value = "00000";
  }
}
