/*
Written by Burt Petrescue 2007. May have copyrighted material but used with permission.
*/
function mt(x){
 if (x==null || x=='') return true;
 return false;
}
function denull(x,def){
 if (mt(x)){
  if (mt(def))
   return '';
  else
   return def;
 }
 return x;
}
function qfix(xu){
 if (mt(xu)) return 0;
 var x=xu.replace(/[^0123456789\.\-]/g,'');
 if (mt(x)) return 0;
 return x*1;
}
function isonlynumeric(x){//empty does not count as numeric
 if (qfix(x)+''==x+'') return true;
 return false;
}
function limitstring(x,maxlen){
 if (mt(x)) return '';
 if (x.length>maxlen){
  return x.substring(0,maxlen-3)+'...';
 }
 return x;
}
function trim(x){
 return x.replace(/^\s+|\s+$/g,'')
}

function isobject(x){
 return (typeof x == 'object' && !!x);
}
function getobj(objname){
 return document.getElementById?document.getElementById(objname):document.all?document.all[objname]:document.layers[objname];
}

function changetext(objectid,text) {
 var obj;
 if (isobject(objectid))
  obj=objectid;
 else
  obj=getobj(objectid);
 if (obj!=null){
  obj.innerHTML=text;
 }
}

function prepjs(x){
  return x.replace(/\'/g,'\\\'');
}

function prepinput(x){
  return x.replace(/\"/g,'\&amp\;');
}

function gettext(objectid) {
 var obj;
 if (isobject(objectid))
  obj=objectid;
 else
  obj=getobj(objectid);
 return obj.innerHTML;
}

var windowWidth = 0, windowHeight = 0;
function getWindowSize() {
 if( typeof( window.innerWidth ) == 'number' ) {
  windowWidth = window.innerWidth;
  windowHeight = window.innerHeight;
 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
  windowWidth = document.documentElement.clientWidth;
  windowHeight = document.documentElement.clientHeight;
 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  windowWidth = document.body.clientWidth;
  windowHeight = document.body.clientHeight;
 }
}
getWindowSize();
var scrollOffsetX = 0, scrollOffsetY = 0;
function getScrollXY() {
 if( typeof( window.pageYOffset ) == 'number' ) {
  scrollOffsetY = window.pageYOffset;
  scrollOffsetX = window.pageXOffset;
 } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  scrollOffsetY = document.body.scrollTop;
  scrollOffsetX = document.body.scrollLeft;
 } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
  scrollOffsetY = document.documentElement.scrollTop;
  scrollOffsetX = document.documentElement.scrollLeft;
 }
}
getScrollXY();
function getpagewidth() {
 return document.body.scrollWidth;
}
function getpageheight() {
 return document.body.scrollHeight;
}
function getwidth(obj){
 if (document.all) return obj.offsetWidth;
 return obj.offsetWidth;
}
function getheight(obj){
 if (typeof(obj).type=='string') return obj.offsetHeight;
 if (document.all) return obj.scrollHeight;
 return obj.scrollHeight;
}
function moveit(obj,thisx,thisy){
 obj.style.x=thisx;
 obj.style.y=thisy;
 obj.style.left=thisx;
 obj.style.top=thisy;
}
function findpos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function findrect(obj) {//returns Left, Top, Width, Height
  var coords=findpos(obj);
  return [coords[0],coords[1],getwidth(obj),getheight(obj)];
}
function setrect(obj, rect) {
  moveit(obj,rect[0],rect[1]);
  obj.style.width=rect[2]+'px';
  obj.style.height=rect[3]+'px';
}
function setopacity(obj,perc){//perc - 0 to 100
 if (document.getElementById && document.all){
  if (perc==100)
   obj.style.filter='';
  else{
   obj.style.filter='alpha(opacity='+perc+')';}
 } else {
  obj.style.MozOpacity=perc/100;
 }
}
function getopacity(obj){
 var op=0;
 if (document.getElementById && document.all){
  var x=obj.style.filter;
  var pos1=x.indexOf('=');
  var pos2=x.indexOf(')');
  if(pos1<0 || pos2<0) return 0;
  op=x.substring(pos1+1,pos2);
 } else {
  op=obj.style.MozOpacity*100;
 }
 return op*1;
}
function showobj(obj) {
  obj.style.display='block';
}
function hideobj(obj) {
  obj.style.display='none';
}

function inarray(ar,value){ //returns array index. returns -1 if not found. keep data type in mind
  for (var i=0;i<ar.length;i++){
    if (ar[i]==value) return i;
  }
  return -1;
}

function dropdown_empty(obj){
 while (obj.options.length>0)
  obj.options[0] = null; 
}
function dropdown_populate(obj,data,rdelim,fdelim){
 var ltildapos=-1,tildapos;
 var optsarray=data.split(rdelim);
 for (var i=0;i<optsarray.length;i++) {
  var opt=optsarray[i].split(fdelim);
  dropdown_add(obj,opt[0],opt[1]);
 }
}
function dropdown_add(obj,value,title){
 var opt=new Option(title,value,false,false);
 obj.options[obj.length]=opt;
}

function findoptionindex(obj,matchvalue){
 for (var i=0;i<=obj.length-1;i++){
  if (obj.options[i].value==matchvalue){
   return i;
  }
 }
 return -1;
}
function autoselectlist(obj,matchvalue){
 if (document.all) {
  var seli=findoptionindex(obj,matchvalue);
  if (seli>=0)
   obj.selectedIndex=seli;
 } else {
   obj.value=matchvalue;
 }
}
function selectedvalue(x){
 if (!x)
  return '';
 if (document.all) {
  return x.options[x.selectedIndex].value;
 } else {
  return x.value;
 }
}
function selectedtext(x){
 return x.options[x.selectedIndex].text;
}
function getselectradio(obj){
 if (!obj)
  return '';
 for (var i=0; i<obj.length; i++){
  if (obj[i].checked)
   return obj[i].value;
 }
 return '';
}

function getformfieldvalue(obj){
 if (!obj) return '';
 var type=obj.type;
 if (type=='text' || type=='textarea' || type=='hidden' || type=='password') {
  return obj.value;
 }
 if (type=='radio') {
  return getselectradio(obj);
 }
 if (type=='checkbox') {
  if (obj.checked) return obj.value;
  return 0;
 }
 if (type=='select-one' || type=='select-multiple') {
  return selectedvalue(obj);
 }
}

var objcache_name=Array();
var objcache_obj=Array();
function getobj_cache(objname){
 for (var i=0;i<objcache_name.length;i++){
  if (objcache_name[i]==objname)
   return objcache_obj[i];
 }
 var obj=getobj(objname);
 objcache_name[objcache_name.length]=objname;
 objcache_obj[objcache_obj.length]=obj;
 return obj;
}

//getrendered function warnings and notes:
//  target div must have a parent div with id="p_"+targetdivid.
//  target div must have overflow:hidden;
//  target div must not have padding style or mozilla will add this to the height each iteration. Put padding in child div
//  ignores the first occurrances of display: and height:
grhhtml='<div id="grhdiv" style="display:none;position:absolute;"></div>';
document.write(grhhtml);
function getrenderedheight(objname){
 var grhobj=getobj_cache('grhdiv');
 var obj=getobj_cache('p_'+objname);
 changetext(grhobj,obj.innerHTML.replace(/display\:/i,'z:').replace(/height\:/i,''));
 setopacity(grhobj,0);
 showobj('grhdiv');
 var h=getheight(grhobj);
 hideobj('grhdiv');
 changetext(grhobj,'');
 return h;
}
function getrenderedwidth(objname){
 var grhobj=getobj_cache('grhdiv');
 var obj=getobj_cache('p_'+objname);
 changetext(grhobj,obj.innerHTML.replace(/display\:/i,'z:').replace(/width\:/i,''));
 setopacity(grhobj,0);
 showobj('grhdiv');
 var h=getwidth(grhobj);
 hideobj('grhdiv');
 changetext(grhobj,'');
 return h;
}

var onloadcom = new Array();
function addloadcommand(functtext){
 onloadcom[onloadcom.length]=functtext;
}
function bodyonload(){
 for (var i=0;i<onloadcom.length;i++){
  eval(onloadcom[i]);
 }
}
if (window.addEventListener)
 window.addEventListener("load", bodyonload, false);
else if (window.attachEvent)
 window.attachEvent("onload", bodyonload);
else if (document.getElementById)
 window.onload=bodyonload;
