// 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;
}
*/

/* 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;
  }
}

/*
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; 
}

function queryString(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function dateAddExtention(p_Interval, p_Number){ 
 
 
	var thing = new String(); 
	 
	 
	//in the spirt of VB we'll make this function non-case sensitive 
	//and convert the charcters for the coder. 
	p_Interval = p_Interval.toLowerCase(); 
	 
	if(isNaN(p_Number)){ 
	 
		//Only accpets numbers  
		//throws an error so that the coder can see why he effed up	 
		throw "The second parameter must be a number. \n You passed: " + p_Number; 
		return false; 
	} 
 
	p_Number = new Number(p_Number); 
	switch(p_Interval.toLowerCase()){ 
		case "yyyy": {// year 
			this.setFullYear(this.getFullYear() + p_Number); 
			break; 
		} 
		case "q": {		// quarter 
			this.setMonth(this.getMonth() + (p_Number*3)); 
			break; 
		} 
		case "m": {		// month 
			this.setMonth(this.getMonth() + p_Number); 
			break; 
		} 
		case "y":		// day of year 
		case "d":		// day 
		case "w": {		// weekday 
			this.setDate(this.getDate() + p_Number); 
			break; 
		} 
		case "ww": {	// week of year 
			this.setDate(this.getDate() + (p_Number*7)); 
			break; 
		} 
		case "h": {		// hour 
			this.setHours(this.getHours() + p_Number); 
			break; 
		} 
		case "n": {		// minute 
			this.setMinutes(this.getMinutes() + p_Number); 
			break; 
		} 
		case "s": {		// second 
			this.setSeconds(this.getSeconds() + p_Number); 
			break; 
		} 
		case "ms": {		// second 
			this.setMilliseconds(this.getMilliseconds() + p_Number); 
			break; 
		} 
		default: { 
		 
			//throws an error so that the coder can see why he effed up and 
			//a list of elegible letters. 
			throw	"The first parameter must be a string from this list: \n" + 
					"yyyy, q, m, y, d, w, ww, h, n, s, or ms.  You passed: " + p_Interval; 
			return false; 
		} 
	} 
	return this; 
} 

Date.prototype.dateAdd = dateAddExtention; 
