// browser detection

function isIE() {
  var browserName = navigator.appName;
  if (browserName.indexOf("Microsoft") < 0) return false;
  return true;
}


function runAnim() {
  var anim = document.getElementById('anim');
  if (anim && isIE()) anim.src = anim.src;
}

// popup windows
function popup(url, name, w, h) {
  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;
  }
}

/* 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 = "";
		}
	}
}

// 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;
}

/* 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) {
	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;
  }
}

/* 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; 
}
