LIB_PATH = document.getElementsByTagName("script")[document.getElementsByTagName("script").length -1].src.replace(/common.js/, "")


function addStyle(style){
	document.write("<style>"+style+"<\/style>")
}



document.getElementsByClassName = function(className){
	var els
	var collection = new Array()
	if (document.all)
	{
		els = document.all
	}
	else if (document.getElementsByTagName)
	{
		els = document.getElementsByTagName("*")
	}

	for (var i=0; i<els.length; i++)
	{
		if (els[i].className == className) collection[collection.length] = els[i]
	}
	return collection
}

//import a js file from the lib
function includelib(src)
{
	return include(LIB_PATH + src)
}

//include (import) another js file if it has not already been loadeded
function include(src)
{
	//this little trick resolves the src url to be relative to the document
	var a = document.createElement("a")
	a.href = src
	src = a.href 
	
	var included = false
	for (var i=0; i<document.getElementsByTagName("script").length; i++){
		if (document.getElementsByTagName("script")[i].src == src){
			included = true
			break
		}
	}
	if (!included){
		//scrap this until it works in safari
		/*
		script = document.createElement("script");
		script.src = src
		document.getElementsByTagName("head")[0].appendChild(script)
		*/
		
		//use the hacky method instead 
		document.write("<scr" + "ipt src='" + src + "'></scr" + "ipt>");
	}
	return included
}




//http://simon.incutio.com/archive/2004/05/26/addLoadEvent
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			if (func) func(); //itomic modification
		}
	}
}

function get(variable) {
  //from http://www.activsoftware.com/code_samples/code.cfm/CodeID/59/JavaScript/Get_Query_String_variables_in_JavaScript
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function zap(el){
	if (el && el.style) el.style.display = "none"
}

function unzap(el){
	if (el && el.style) el.style.display = ""
}

function workzap(el){
	element = document.getElementById(el);
	
	if(element.style.display == 'none'){
		unzap(element);
	}else{
		zap(element);
	}
}

function zaplist(elname,num){
	for(var i=0;i<num;i++){
		zap(document.getElementById(elname+i));
	}
}


//http://www.scottandrew.com/weblog/articles/cbs-events
function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
  	}
	return r;
}


function killInputs(check, el){
	el = document.getElementById(el);
	if(!el){
		return;
	}
	
	var inputs = el.getElementsByTagName('INPUT');
	for(var i = 0; i < inputs.length; i++){
		inputs[i].disabled = check.checked;
	}
}

function startIeFix(){
  if(isIE()){
    document.write('<div id="bo_ns_id_' + bo_ns_id + '"><!-- ');
  }
}

function endIeFix(){
  if(isIE()){
    document.write('</div>');
    var theObject = document.getElementById("bo_ns_id_" + bo_ns_id++);
    var theCode = theObject.innerHTML;
    theCode = theCode.substring(4 ,9+theCode.indexOf("</object>"))
    document.write(theCode);
  }
}

function isIE(){
  var strBrowser = navigator.userAgent.toLowerCase();
  if(strBrowser.indexOf("msie") > -1 && strBrowser.indexOf("mac") < 0){
    return true;
  }else{
    return false;
  }
}

var bo_ns_id = 0;


function blurText(text){
	
	if(trim(text.value) == ''){
		text.value = text.defaultValue;
	}
}

function focusText(text){
	if(text.value == text.defaultValue){
		text.value='';
	}
}

function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}

function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
		return true;
	}else{
		return false;
	}
}

function numeric(string) {
	if (string.search(/^[0-9]*$/) != -1){
		return true;
	}else{
		return false;
	}
}

function delEl(el){
	var ele = document.getElementById(el);
	if(!ele){
		return;
	}
	
	ele.parentNode.removeChild(ele);
}

function openExternal(theURL, width, height){
	var external=window.open(theURL,'external','width='+ width +',height='+ height +'');
	external.focus();
}

function formToggleAdv(form){
	var search = 'formAdvth' + form;
	var tform = document.getElementById(form);
	
	var tf_TH = tform.getElementsByTagName('TH');
	var tf_TD = tform.getElementsByTagName('TD');
	var tf_DIV = tform.getElementsByTagName('DIV');
	
	for(var i = 0; i < tf_TH.length; i++){
		if(tf_TH[i].id.substring(0,search.length) == search){
			tf_TH[i].style.display = tf_TH[i].style.display == 'none'?'':'none';
		}
	}
	search = 'formAdvtd' + form;
	for(var i = 0; i < tf_TD.length; i++){
		if(tf_TD[i].id.substring(0,search.length) == search){
			tf_TD[i].style.display = tf_TD[i].style.display == 'none'?'':'none';
		}
	}
	search = 'formAdvdiv' + form;
	for(var i = 0; i < tf_DIV.length; i++){
		if(tf_DIV[i].id.substring(0,search.length) == search){
			tf_DIV[i].style.display = tf_DIV[i].style.display == 'none'?'':'none';
		}
	}
}

function checkNum(id){
	var inp = document.getElementById(id);
	
	if(!numeric(inp.value) || !inp.value) {
		alert('Error: Please enter a quantity amount');
		inp.focus();
		return false;
	}
	return true;
}

function setCookie(c_name,value,expiredays) {
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1) { 
			c_start=c_start + c_name.length+1 
			c_end=document.cookie.indexOf(";",c_start)
			if (c_end==-1) c_end=document.cookie.length
			return unescape(document.cookie.substring(c_start,c_end))
			} 
		}
	return ""
}