var unloadHandlers=new Array();
var loadHandlers=new Array();

var numPopups = 0 ;
var winPopups = new Array();

function runUnloadHandlers() {
	for(f=0; f<unloadHandlers.length;f++) {
		unloadHandlers[f] () ;
	}
	for(n=0;n<winPopups.length;n++) {
		winPopups[n].close();
	}

	if (window.onPageUnload)
		window.onPageUnload();
}

function addUnloadHandler(f) {
	unloadHandlers[unloadHandlers.length] = f;
}

function runLoadHandlers() {
	for(f=0; f<loadHandlers.length;f++) {
		loadHandlers[f] () ;
	}

	if ((nf = document.forms.length)>0) {
		for(f = 0 ; f < nf ; f ++) {
			if (document.forms[f].onsubmit)
				alert('Old style onSubmit has been disabled in this form. Please contact LibertyLine and report this message') ;
			document.forms[f].onsubmit = function() {
				if (window.onFormSubmit) {
					if (!window.onFormSubmit(this)) {
						return false ;
					}
				}
				disableForms();
				return true ;
			}
		}
	}

	if (window.onPageLoad)
		window.onPageLoad();
}

function addLoadHandler(f) {
	loadHandlers[loadHandlers.length] = f;
}

function popup(file,w,h) {
	nmpopup(file,'',w,h) ;
}

function nmpopup(file,win,w,h) {
	tw = window.open(file,win,"height="+h+",width="+w+", toolbar=no,location=no,directories=no,scrollbars=yes,status=no,resizable=yes,copyhistory=no") ;
	if (!tw) {
		alert('Warning: Popup blocked enabled. This site needs popups enabled to operate correctly.')
	} else {
		winPopups[winPopups.length] = tw ;
	}
}

var _form_has_been_submitted = false ;

function disableForms() {
	if (_form_has_been_submitted) {
		window.status='please wait...';
		return false;
	}
	_form_has_been_submitted = true ;

	num = document.forms.length ;
	for(a = 0; a<num ; a++ )  {
		with(document.forms[a]) {
			for(b=0;b<elements.length;b++) {
				elname = elements[b].type.toLowerCase();
				if (elname=='submit' || elname=='button') {
					elements[b].style.visibility = 'hidden';
				}
			}
		}
	}
	return true ;
}

function jsGo(loc) {
	disableForms();
	document.location = loc ;
}

function trim(str) {
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

function setContent(id,content) {
	if (document.all) document.all[id].innerHTML=content ;
	else if (document.getElementById) {
		rng = document.createRange();
		el = document.getElementById(id);
		rng.setStartBefore(el);
		htmlFrag = rng.createContextualFragment(content);
		while (el.hasChildNodes())
			el.removeChild(el.lastChild);
		el.appendChild(htmlFrag);
	}
}

function getElement(elname) {
	if (document.all) {
		return document.all[elname];
	}
	if (document.getElementById) {
		return document.getElementById(elname) ;
	}

	return null ;
}

function hideElement(name) {
	if ((el = getElement(name))!=null) {
		if (el.style) {
			el.style.display = 'none';
		} else {
			if (el.display) {
			 	el.display='none';
			}
		}
	}
}

function showElement(name) {
	if ((el = getElement(name))!=null) {
		if (el.style) {
			el.style.display = '';
			//el.style.visibility = 'visible';
		} else {
			if (el.display) el.display='';
			// if (el.visibility)  el.visibility = 'visible';
		}
	}
}

function checkField(fld,ml,err) {
	if (fld.value.length<ml) {
		if (err!='') {
			alert(err) ;
			fld.focus();
		}
		return false ;
	}
	return true ;
}

function checkSelect(fld,err) {
	if (fld.selectedIndex==0) {
		alert(err) ;
		return false;
	}
	return true ;
}

function checkDate(fld,fmt,err) {
	if (!internalCheckDate(fld.value,fmt)) {
		alert(err) ;
		return false;
	}
	return true ;
}

function checkEmail(field,msg) {
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if (emailPattern.test(field.value))
		return true ;
	alert(msg) ;
	field.focus();
	return false; 
}

function notNull(str) {
	if (str.length == 0 )
		return false ;
	else
		return true;
}

function notBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true;
	}
	return false;
}

	function isSize(str, size) {
		if (str.length == size)
			return true;
		else
			return false;
	}


function isDigits(str) {
	var i
		for (i = 0; i < str.length; i++) {
			mychar = str.charAt(i);
			if (mychar < "0" || mychar > "9")
				return false;
		}
	return true;
}

function isNumber(str) {
	str = trim(str) ;
	numdecs = 0;
	if (str.length==0)
		return false ;

	for (i = 0; i < str.length; i++) {
		mychar = str.charAt(i);
		if ((mychar >= "0" && mychar <= "9") || mychar == ".") {
			if (mychar == ".")
				numdecs++;
		}
		else
			return false;
	}
	if (numdecs > 1)
		return false;
	return true;
}

function isInRange(str, num1, num2) {
	var i = parseInt(str);
	return((i >= num1) && (i <= num2));
}

function stripNonDigits(str) {
	var i;
	var newstring = "";
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i);
		if (isDigits(mychar))
			newstring += mychar;
	}
	return newstring;
}

function stripChars(str, chars) {
	var i;
	var newstring = "";
	for (i = 0;  i < str.length; i++) {
		mychar = str.charAt(i);
		if (chars.indexOf(mychar) == -1)
			newstring += mychar;
	}
	return newstring;
}

function getYear(d) {
	return (d < 1000) ? d + 1900 : d;
}
function internalCheckDate(dat,format)
{
	if (dat.length != 10) return false ;

	switch(format) {
		case 0 :
			var day=dat.substring(0,2);
			var mon=dat.substring(3,5) -1;
			var year=dat.substring(6,10);
			if (dat.substring(2,3)!='/' || dat.substring(5,6)!='/') {
				return false;
			}
			break ;
		case 1 :
			var mon=dat.substring(0,2)-1;
			var day=dat.substring(3,5)
				var year=dat.substring(6,10);
			if (dat.substring(2,3)!='/' || dat.substring(5,6)!='/') {
				return false;
			}
			break ;
		case 2 :
			var year=dat.substring(0,4);
			var mon=dat.substring(6,7) -1;
			var day=dat.substring(8,10);
			if (dat.substring(4,5)!='/' || dat.substring(7,8)!='/') {
				return false;
			}
			break ;
	}
	var test = new Date(year,mon,day);
	if (getYear(test.getYear())==year && test.getMonth()==mon && test.getDate()==day) return true ;
	return false ;
}

function resizeWindow(w,h) {
	if (!window.innerWidth) {
		window.resizeTo(w+10,h+10);
	} else {
		window.innerWidth = w ;
		window.innerHeight = h ;
	}
}

function getAbsoluteTop(objectId) {
	o = getElement(objectId) ;

	if (o) {
		oTop = o.offsetTop ;           // Get top position from the parent object
		while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent ; // Get parent object reference
				oTop += oParent.offsetTop ;// Add parent top position
				o = oParent ;
		}
		return oTop ;
	}
	return oTop
}

function getAbsoluteBottom(objectId) {
	o = getElement(objectId) ;
	if (o) {
		oHeight = o.offsetHeight ;
		oTop = o.offsetTop            // Get top position from the parent object
		while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent  // Get parent object reference
				oTop += oParent.offsetTop // Add parent top position
				o = oParent
		}
		return oTop + oHeight ;
	}
	return 0 ;
}

function setCopyright() {
	if (cpDiv = getElement('copyright')) {
		minY = getAbsoluteBottom('content') ;
		if (xx = getElement('mainLayout')) {
			posY = xx.offsetHeight - 24;
			//posY = document.body.offsetHeight - 24;
			if (posY < minY) posY = minY - 20;
			cpDiv.style.left = 48 ;
			cpDiv.style.top =  posY;
			cpDiv.style.display = '';
			//alert('posY: ' + posY + ' , minY: ' + minY) ;
		}
	}
}


// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) +
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);

   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}


/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];

  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}

function cancelEvent(e) {
	if (window.event) {
		window.event.cancelBubble = true ;
		window.event.returnValue = false ;
	}
	if (e && e.stopPropagation && e.preventDefault) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function stripHTML(str) {
	return str.replace(/(<([^>]+)>)/ig,"");
}

window.onunload = runUnloadHandlers ;

addEvent(window,'resize',setCopyright,false);
addEvent(window,'load',runLoadHandlers,false);
//window.onresize = setCopyright ;
//window.onload = runLoadHandlers ;
addLoadHandler(setCopyright) ;