// browser detection

function isIE() {
  return isBrowserIE();
}

// there seems to be a javascript problem in isIE function
// will be using this instead
function isBrowserIE() {
  if (navigator.appName.indexOf("Microsoft")!=-1) {
    return true;
  }
  return false;
}

function isBrowserSafari() {
  if (navigator.userAgent.toLowerCase().indexOf("safari")!=-1) {    
    return true;
  }
  return false;
}


function runAnim() {
  var anim = document.getElementById('anim');
  if (anim && isBrowserIE()) anim.src = anim.src;
}

//popup windows
function popup(url, name, w, h) {
	
  if ( !w>0 ) {
	  w = screen.availWidth;
  }
  if ( !h>0 ) {
	  h = screen.availHeight;
  }
  window.open(url, name, 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width='+w+',height='+h);
}

/* toggle tabs */
function toggleTab(prefix, id) {
  var maxTabs = 4;
  for (var i=1; i <= maxTabs; i++) {
    var currTab = document.getElementById("tabId_" + prefix + "_" + i);
    var currContent = document.getElementById("contentId_" + prefix + "_" + i);
    if (currContent) hideDiv(currContent);
    if (currTab) removeClassName(currTab, "here");
  }
  var currTab = document.getElementById("tabId_" + prefix + "_" + id);
  var currContent = document.getElementById("contentId_" + prefix + "_" + id);
  if (currTab) addClassName(currTab, "here");
  if (currContent) showDiv(currContent);
}


/* click radio button, given the id */
function selectRadioById(el) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    el.click();
  }
}

/* focus on another element */
function focusOn(el) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    el.focus();
  }
}

/* copy value of a form field into another */
function copyValue(from, to) {
  if (typeof(from) == "string") {
    from = document.getElementById(from);
  }
  if (typeof(to) == "string") {
    to = document.getElementById(to);
  }
  if (from && to) {
    to.value = from.value;
  }
}

/* sets a field value */
function setValue(field, to) {
  if (typeof(field) == "string") {
    field = document.getElementById(field);
  }
  if (field && to) {
    field.value = to;
  }
}

/* gets the inner HTML content */
function getInnerHTML(field) {
  if (typeof(field) == "string") {
    field = document.getElementById(field);
  }
  if (field) {
    return field.innerHTML;
  }
}

/* sets the inner HTML content */
function setInnerHTML(field, to) {
  if (typeof(field) == "string") {
    field = document.getElementById(field);
  }
  if (field && to) {
    field.innerHTML = to;
  }
}

/* clears the inner HTML content */
function clearInnerHTML(field) {
  if (typeof(field) == "string") {
    field = document.getElementById(field);
  }
  if (field) {
    field.innerHTML = "";
  }
}

/* copies the inner HTML content to a destination */
function copyInnerHTML(destination, origin) {
  if (typeof(destination) == "string") {
    destination = document.getElementById(destination);
  }
  if (typeof(origin) == "string") {
    origin = document.getElementById(origin);
  }  
  if (destination && origin) {
    destination.innerHTML = origin.innerHTML;
  }
}

/* gets a field value */
function getValue(field) {
  if (typeof(field) == "string") {
    field = document.getElementById(field);
  }
  if (field && field.value) {
    return field.value;
  } else {
    return null;
  }
}

/* copies the value of one field into another */
function copyValue(from, to) {
  if (typeof(from) == "string") {
    from = document.getElementById(from);
  }
  if (typeof(to) == "string") {
    to = document.getElementById(to);
  }
  if (from && to) {
    from.value = to.value;
  }
}

/*
generate a new array from the old one
*/
function newArray(arr) {
  return [].concat(arr);
}

/*
given an array of radio buttons, return reference to the radio button
that is checked / selected. return -1 if none is selected
*/
function getSelectedRadio(radios) {
  if(typeof radios.length != "number"){
    radios = [radios];
  }
  for (var i=0; i<radios.length; i++) {
    var thisRadio = radios[i];
    if (thisRadio.checked) {
      return thisRadio;
    }
  }
  return -1;
}

/* create <option value="$optionValue">$optionText</option> */
function createOption(selectId, optionIndex, optionValue, optionText) {
  var el = document.getElementById(selectId);
  if (el) {
    el.options[optionIndex] = new Option();
    el.options[optionIndex].value = optionValue;
    el.options[optionIndex].innerHTML = optionText;
  }
}

// hide DIV
function hideDiv(el) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    el.style.display = "none";
  }
}

// show DIV
function showDiv(el) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    el.style.display = "block";
  }
}

// hide DIV
function hideTbody(el) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    el.style.display = "none";
  }
}

// show DIV
function showTbody(el) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    el.style.display = "";
  }
}

// toggle DIV hide | show
function toggleDiv(el) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    if (el.style.display == "") {
      el.style.display = "none";
    } else {
      el.style.display = "";
    }
  }
}

function containsClassName(el, name) {
  if(typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    var classnames = el.className.split(" ");
    for(var c in classnames) {
      if (classnames[c] == name) {
        return true;
      }
    }
  }
  return false;
}

// add classname to element
function addClassName(el, name) {
  if(typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    var newclassname = el.className;
    if(newclassname.indexOf(name) < 0) {
      el.className = newclassname + " " + name;
    }
  }
}

// remove classname from element
function removeClassName(el, name) {
  if(typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    var classnames = el.className.split(" ");
    var newclassnames = "";
    for(var c in classnames) {
      if (classnames[c] !== name) {
        newclassnames = newclassnames + " " + classnames[c];
      }
    }
    el.className = newclassnames;  
  }
}

// remove classname that starts with startsWith from element
function removeClassNamesThatStartsWith(el, startsWith) {
  if(typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    var classnames = el.className.split(" ");
    var newclassnames = "";
    for(var c in classnames) {
      if (classnames[c].indexOf(startsWith) !== 0) {
        newclassnames = newclassnames + " " + classnames[c];
      }
    }
    el.className = newclassnames;  
  }
}

/* search for parent element of type $type */
function getParentElementOfType(el, type) {
  if(typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el && type!=='') {
    while(el.tagName != type) {
      el = el.parentNode;
    }
  }  
  return el;
}

/* disable a form elements */
function disableFormElement(el, disable) {
  if (typeof(el) == "string") {
    el = document.getElementById(el);
  }
  if (el) {
    el.disabled = disable;
  }
}

/* SORT OPTIONS BY TEXT */
/* usage:
  1. create new array: $arr
  2. push select dropdown options into array: $arr.push(new Array(option.value, option.text))
  3. sort that array: $arr.sort(sortOptionsByText)
  4. re-populate select dropdown with that array
*/
function sortOptionsByText(a, b)
{
  if (a[0] === "") {
     return -1;
  }
  if (b[0] === "") {
     return 1;
  }
  if (a[1] < b[1]) {
     return -1;
  }
  if (b[1] < a[1]) {
      return 1;
  }
  return 0;
}

function sortString(a, b) {
  if (a === "") {
     return -1;
  }
  if (b === "") {
     return 1;
  }
  if (a < b) {
     return -1;
  }
  if (b < a) {
      return 1;
  }
  return 0;
}

/* PAD LEFT */
/* pad string $str on the left with char $c, up to length $l)
   usage: padleft('7', '0', 3) -> '007'
*/
function padLeft(str, c, l) {
  str = ""+str;
  if (str.length < l) {
    str = c + str + '';
    return padLeft(str, c, l);
  }
  return str;
}

/* GET NUMBER */
/* get number from a string (strips characters from the left)
   usage: getNumber("AUD100.00") -> "100.00"
*/
function getNumber(str) {
  if (typeof str == "number") {
    return str;
  } else if (str && str.length > 0) {
    if (Number(str) < 0 || Number(str) > 0) {
      return Number(str);
    } else {
      return getNumber(str.substring(1, str.length));
    }
  } else {
    return 0;
  }
}

/* PRESELECT A VALUE FOR SELECT DROPDOWN */
/* sets the value in a drop down with the specified parameter value,
   sets nothing if no value matches
*/
function preselectValue(el, value)
{
  var select = el;
  if (typeof(select) == "string") {
    select = document.getElementById(select);
  }
    
  if (select)
  {
    var selectedValue = null;
    for (var i = 0; i < select.length; i++)
    {
      selectedValue = select[i].value;
      if (selectedValue == value)
      {
        select[i].selected = true;
        select.value = value;
        break;
      }
    }
  }
}

/* Time out */
/* displays an alert informing the user of a timeout, and directing it to a page
   the TIMEOUTMESSAGE and TIMEOUTURL is being set via properties file
*/
function timeOutFunction() {
  alert(TIMEOUTMESSAGE);
  location.href = TIMEOUTURL;
}

function callTimeout() {
  setTimeout('timeOutFunction()',TIMEOUTDURATION);
}

/* CURSORS */
function busyCursor() {
  var area = document.getElementById("outerframe");
  area.style.cursor="progress";
}

function normalCursor() {
  var area = document.getElementById("outerframe");
  area.style.cursor="auto";
}

/* Secured page redirection */
function secureFormSubmit(formName) {
  document.forms[formName].action=SECUREDURL;
}

function unsecureFormSubmit(formName) {
  document.forms[formName].action=BASEURL; 
}

function addOverlayWindowEvent(dialogId, windowOpenButtonId, windowOpenCallback) {
    YAHOO.namespace("jetblue.b2c.dialog");

    if(YAHOO.lang.isNull(YAHOO.util.Dom.get(windowOpenButtonId)) || typeof YAHOO.util.Dom.get(dialogId) === 'undefined') 
    {
    	return;
    }
    else 
    {
    	YAHOO.util.Dom.get(dialogId).style.display = '';
    }
    
    var panel = new YAHOO.widget.Panel(dialogId,
    {
        fixedcenter: true,
        visible: false,
        constraintoviewport : true,
        close: false,
        modal: true,
        underlay: "none"
    });

    eval( 'YAHOO.jetblue.b2c.dialog.' + dialogId + ' = panel' ); 

    panel.render();
        
    if ( windowOpenCallback == null ) {
    	eval ( 'windowOpenCallback = function (e) { YAHOO.jetblue.b2c.dialog.' + dialogId + '.show();  YAHOO.util.Event.stopEvent(e);  } ' );     		
    }

    var dlg = YAHOO.util.Dom.get(dialogId);
    var divs = YAHOO.util.Dom.getElementsByClassName('hd', 'div', dlg);
    (divs.length > 0) ? divs[0].style.display = 'none': void(0);

    YAHOO.util.Event.addListener(windowOpenButtonId, "click", windowOpenCallback);
}
