//+----------------------------------------+//
//+ Javascript Library Functions           +//
//+----------------------------------------+//

var ns6up = (document.getElementById&&!document.all) ? 1 : 0;
var ns4up = (document.layers) ? 1 : 0;  // browser sniffer
var ie4up = (document.all) ? 1 : 0;

/*
Takes a value and tests to see if it is an object
*/
function isObject(o){return (o && typeof o == 'object');}

/*
Takes any object and checks if it is null
*/
function isNull(o){return typeof o == 'object' && !o;}

/*
Takes an object and tries to determine if it is a valid numeric object
Attempts to emulate the results of visual basic function isNumeric
Valid strings believed to be formatted like:
   ($x) or -$x or +$x or $x or $-x or $+x
   where x can be 1+ numbers as xx.xx or x,xxx.xx etc.,
Returns Boolean value
Note: There is probably a more condensed way to write the regular expression; however, this seems to work.
*/
function isNumeric(s){
   if(s==null){return false;}
   var r=(/^(([-\+]?[\$]?)|([\$]?[-\+]?))?\d*(\.?|,?)\d+(?:[eE][-\+]?\d+)?$/);
   var re=(/^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$/);
   return (r.test(s.toString())||re.test(s.toString()));
}

/*
Takes a string value to format
Returns a String with no leading/trailing whitespace
*/
function Trim(s){if(s==null){return "";}var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);return(m == null)?"":m[1];}

/*
Takes a numeric value to format
Returns a String value formatted as currency
Padds a single leading zero and trailing zeros
Converts negatives to () enclosed format
Drops -/+ signs
*/
function FormatCurrency(val){
   if(val==null||!isNumeric(val)){return 'NaN';}
   var s=val.toString().replace('$','').replace('+');
   var Negative=false;
   if(s.indexOf('-')!=-1){s=s.replace('-','');Negative=true;}
   if(s.indexOf('(')!=-1&&s.indexOf(')')!=-1){s=s.replace('(','').replace(')','');Negative=true;}
   if(s.indexOf('.')==-1){s+='.00';}
   if(s.indexOf('.')==0){s='0'+s;}
   if(s.indexOf('.')==s.length-1){s+='00';}
   if(s.indexOf('.')==s.length-2){s+='0';}

   return (Negative)?'($'+s+')':'$'+s;
}

/*
Takes a string object and Returns the last character or null
*/
function chop(s){return (!isNull(s))?s.charAt(s.length-1):null;}

/*
Takes a string object and Returns all but the last character or null
*/
function chomp(s){return (!isNull(s))?s.substr(0,s.length-1):null;}

/*
Takes a string object and a character to match and Returns a boolean value
to indicate if/when the string ends with that character or not
*/
function EndsWith(s,ch){return (!isNull(s))?((s.charAt(s.length-1)==ch)?true:false):false;}

/*
Takes a string object and Returns a valid CheckBoxList id without
the index e.g. myId_xx where xx is the removed index
*/
function GetValidCBId(s){
   var vid=s;
   while(!EndsWith(vid,'_')&&!isNull(vid)){vid=chomp(vid);}
   return vid;
}

/*
Takes an object and tests for like objects differing only by the final character
assumed to be an incremental integer as .net checkboxlists
default to listid_x where x is equal to some integer begining with zero
if more than one object is found, the list acts as a radiobuttonlist
*/
/*
function fnCBtoRB(obj){
   var id=obj.id;
   var len=0;
   while(isObject(document.forms[0].elements[GetValidCBId(id)+len.toString()])){++len;}

   for(var i=0;i<len;++i){
      if(document.forms[0].elements[GetValidCBId(id)+i.toString()].checked){
         if(GetValidCBId(id)+i.toString()!=id){
            document.forms[0].elements[GetValidCBId(id)+i.toString()].checked=false;
         }
      }
   }
   if(typeof(iePricingHack)=='function'){iePricingHack();}
}
*/
/*
Takes the id string of a checkbox(list) to attach click events
in order to force the checkbox(list) to act like a radiobuttonlist
*/
/*
function SetCBtoRB(id){
   var len=0;
   while(isObject(document.forms[0].elements[id+'_'+len.toString()])){++len;}
   for(var i=0;i<len;++i){
      if(ie4up){
         document.forms[0].elements[id+'_'+i].attachEvent('onclick',fnHandler);
      }else{
         var attrib=document.forms[0].elements[id+'_'+i].getAttribute('onclick');
         if(null!=attrib){attrib='fnCBtoRB(this);'+attrib;}
         //document.forms[0].elements[id+'_'+i].setAttribute('onclick','fnCBtoRB(this);');
         document.forms[0].elements[id+'_'+i].setAttribute('onclick',attrib);
      }
   }
}
*/
/*
IE requires an event handler rather than using the common setAttribute method
*/
function fnHandler(){
   fnCBtoRB(event.srcElement);
}
/*
function ValidateNumericValue(elemid){
   try{
	   var obj=document.forms[0].elements[elemid];
		if(!isNumeric(obj.value)){
		   obj.value='';
		}
	}catch(e){
		return;
	}
}
*/
function CheckKeyCode(e){
   if(!e) var e=window.event;
   var key = e.keyCode ? e.keyCode : e.which;
	if( (key == 189 || key == 109) ||
       (key >= 48 && key <= 57) ||
       (key >= 96 && key <= 105) ||
       (key == 8 || key == 46) ||
       (key >= 112 && key <= 123) ||
       (key == 37 || key == 39) ||
       (key == 190 || key == 110) ||
       (key == 9) ) {
      return true;
	} else {
      return false;
   }
}
function sht(id,OnOff){
   try{
      var obj=document.getElementById(id);
      if(OnOff){
         /*if(obj.style.display=='none'){
            obj.style.display='block';
            obj.style.width=100+'%';
         }*/
         obj.style.visibility='visible';
      }else{
         /*if(obj.style.display=='block'||obj.style.display==''){
            obj.style.display='none';
         }*/
         obj.style.visibility='hidden';
      }
   }catch(e){
      return;
   }
}
function np(tid){
   try{
		var ok=false;
      if(uc()>0){ok=true;}
		/*always show*/
		ok=true;
		if(ok){sht(tid,true);sp();}else{sht(tid,false);sp();}
   }catch(e){
      return;
   }
}
function DisplayText(id){
   try{
      var obj=document.getElementById(id);
      if(obj){
         obj.style.display=(obj.style.display=='block'||obj.style.display=='')?'none':'block';
      }
   }catch(e){
      return;
   }
}
function ClosePage(cp){
   try{
      cp.close();
   }catch(e){
      alert(e);
   }
   return false;
}
function showPage(ttl,pg,vert,horiz){
   try{
      var win=window.open(pg,'','resizable=no,scrollbars=yes,height='+vert.toString()+',width='+horiz.toString());
      return false;
   }catch(e){
      return false;
   }
}
function CenterWindow(){
   try{
      var iWidth;
      var iHeight;
      //half the screen width minus half the new window width (plus 5 pixel borders).
      //half the screen height minus half the new window height (plus title and status bars).
      var _width;
      var _height;
      _width = f_clientWidth() / 2 + 5;
      _height = f_clientHeight() / 2 + 50;
      self.moveTo(_width,_height);
      return;
   }catch(e){
      return;
   }
}

function f_clientWidth() {
	return f_filterResults (
      window.innerWidth ? window.innerWidth : 0,
   	document.documentElement ? document.documentElement.clientWidth : 0,
   	document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
