var MINUTE = 60 * 1000;
var HOUR = MINUTE * 60;
var DAY = HOUR * 24;
var WEEK = DAY * 7;
var imgFile=new Array();

imgFile.opened=document.getElementById("opened").src;

imgFile.closed=document.getElementById("closed").src;
imgFile.leaf=document.getElementById("leaf").src;

imgFile.first=document.getElementById("first").src;
imgFile.previous=document.getElementById("previous").src;
imgFile.backward=document.getElementById("backward").src;
imgFile.forward=document.getElementById("forward").src;
imgFile.next=document.getElementById("next").src;
imgFile.last=document.getElementById("last").src;

imgFile.first_gray=document.getElementById("first_gray").src;
imgFile.previous_gray=document.getElementById("previous_gray").src;
imgFile.backward_gray=document.getElementById("backward_gray").src;
imgFile.forward_gray=document.getElementById("forward_gray").src;
imgFile.next_gray=document.getElementById("next_gray").src;
imgFile.last_gray=document.getElementById("last_gray").src;


function browserClient() {
	var client = {'width':0,'height':0};
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    client.width = window.innerWidth;
    client.height = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    client.width = document.documentElement.clientWidth;
    client.height = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    client.width = document.body.clientWidth;
    client.height = document.body.clientHeight;
  }
	return(client);
}



function setstyle(e,sty,val){
  e=document.getElementById(e);
  e.style[sty]=val;
	}
function getRealLeft(el) {
    if (arguments.length==0) el = this;
    var xPos = el.offsetLeft;
    var tempEl = el.offsetParent;
    while (tempEl != null && typeof(tempEl.tagName)!="undefined") {
        xPos += tempEl.offsetLeft;
        tempEl = tempEl.offsetParent;
        }
    return xPos;
    }

function getRealTop(el) {
    if (arguments.length==0) el = this;
    var yPos = el.offsetTop;
    var tempEl = el.offsetParent;
    while (tempEl != null && typeof(tempEl.tagName)!="undefined") {
				yPos += tempEl.offsetTop;
        tempEl = tempEl.offsetParent;
        }
    return yPos;
    }
		
function setstyle(e,sty,val){
  e=document.getElementById(e);
  e.style[sty]=val;
	}
function window_meter(w,s,r){
    s=parseFloat(s);
		r=parseFloat(r);
		var t=((s/r)*100.0).toFixed(0) + "% Loaded";
	  var meter=w.document.getElementById("meter");
		if (meter.firstChild != null) {
			 if (meter.firstChild.data==t) return; 
			 else meter.removeChild(meter.firstChild);
			 }
	  meter.appendChild(w.document.createTextNode(t));
  }
function meter(s,r){
    r=14;
    window_meter(window,s,r);
  }
function range_ok(field,_min,_max){
  var v=field.value;
	var ok= (_min<=v && _max>=v);
	if (!ok) {
		 alert("Please enter a number between "+_min+" and "+_max+".");
		 }
	return ok;
  }

function setCookie(_name, _value, expires, path, domain, secure) {
  if (!expires) expires=new Date( (new Date()).getTime()+(365*DAY));
  var curCookie = _name + "=" + escape(_value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "" ) +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
};
function getCookie(_name) {
  var dc = document.cookie;
  var prefix = _name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
};
function getCookieById(){
  var e;
	for (e=0;e<arguments.length;e++) {
		var id=arguments[e];
    var el=document.getElementById(id);
	  if (el!=null) {
	    var v=getCookie(id);
		  if (v!=null ) {
		    el.value=v;
			  }
	    }
    }
	}
function setCookieById(){
  var e;
	for (e=0;e<arguments.length;e++) {
		var id=arguments[e];
    var el=document.getElementById(id);
	  if (el!=null) var v=setCookie(id,el.value);
    }
  }
function deleteCookie(_name, path, domain) {
  if (getCookie(_name)) {
    document.cookie = _name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
};

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
};

function ClassTreeCheckbox() {
   
	 this.binary_group_i_list=new Array(); 
	 this.binary_group_i_list.address=new Array(); 
	 this.binary_group_i_list.preference=new Array(); 
	 this.binary_group_result=new Array();
	 this.binary_group_result.address=new Array();
	 this.binary_group_result.preference=new Array();
	 this.binary_result=new Array();
	 
	 this.results_start=0;
	 this.results_total=0;
	 this.results_person_indices=null;
	 this.results_company_indices=null;
	 this.amount_min=0;
	 this.amount_max=0;
	 this.form=null;
	 this.selectedCompanyNode=null;
	 this.preference_bool=null;
	 this.window_web=null;
	 this.allowRefresh=true;
	 this.lastInfoTab=null;
	 this.lastTabId=null;
	 this.results_index=0;
	 
	 
	 this.unload=function(){
		  setCookie("checkbox_setting",this.checkbox_setting());
			setCookie("lastTabId",this.lastTabId);
			if (this.selectedCompanyNode!=null) setCookie("company",this.company.company[this.selectedCompanyNode.id]);
			setCookie("results_start",this.results_start);
			setCookieById("per_set","amount_min","amount_max");
	    };
			

			
	 this.RestoreChecks=function(){
			var e;
	    this.allowRefresh=false;
			var company=getCookie("company");
		  var _span=document.getElementById("results");	
	 	  checkbox_setting=getCookie("checkbox_setting");
	 	  if (checkbox_setting!=null) {
			  checkbox_setting=checkbox_setting.split(',');
			  for (e in checkbox_setting){
					var n = document.getElementById(checkbox_setting[e]);
					if (null!=n){
						 n.checked=true;
						 this.checkbox_click(n);
						 EnsureVisible(n);
						 }
			    }
				}
			getCookieById("per_set","amount_min","amount_max");
			this.millions(this.form.amount_min);
			this.millions(this.form.amount_max);
	    this.allowRefresh=true;
			
			this.RefreshCompanies(getCookie("results_start"));
			
			var n=_span.firstChild;
			while (n!=null){
				if (this.company.company[n.id]==company) {
					this.company_click(n,true);
					break;
					}
				n=n.nextSibling;
				}

			{
			 var lastTab = getCookie("lastTabId");
			 if (lastTab!=null)	lastTab = document.getElementById(getCookie("lastTabId"));
			 this.click_tab(lastTab);
			 }
	    };
		
 this.MakeReverseIndexes=function(){
   if (!this.Local()) return;
   var e;
	 this.index_from.company=new Array();
	 this.index_from.company.address=new Array(this.company.company.length);
	 for (e=0;e<this.index_from.company.address.length;e++) this.index_from.company.address[e]=new Array();
	 for (e=0;e<this.index_from.address.company.length;e++) this.index_from.company.address[this.index_from.address.company[e]].push(e);
	 
	 this.index_from.address.person=new Array(this.address.address.length);
	 for (e=0;e<this.index_from.address.person.length;e++) this.index_from.address.person[e]=new Array();
	 for (e=0;e<this.index_from.person.address.length;e++) this.index_from.address.person[this.index_from.person.address[e]].push(e);
   };			

	 
this.Local=function(){
  result=typeof(this.person.name_last)!="undefined";
  return result;
  };	 
	

this.Export=function(){
  var local=this.Local();
	if (local){
	   var w=window.open();
		 this.TableCSV(w);
		 }
	return local;
};




	 this.sizeFrame=function(e){
	 		var client = browserClient();
			var width=client.width-(getRealLeft(e)+10); 	
			var height=client.height-(getRealTop(e)+10); 
			if (e.width != width)  e.width = width;
			if (e.height != height)  e.height = height;		
			this.DisplayCompanies("resize");
	   };
	 
 this.click_tab=function(ths){
  var e;
  if (ths==null) ths=document.getElementById("tab_Help");
	if (this.lastTab!=ths){
	    this.lastTab = ths;
 	    c=ths.parentNode.childNodes;
	    for (e=0;e<c.length;e++) if (c[e]!=ths && c[e].id!="") c[e].className="tab_deselected";
	    ths.className="tab_selected";
	    this.lastTabId=ths.id;
	
      tabText=trim(ths.innerHTML.replace(/&nbsp;/g,''));
      if (tabText=="") return;
	    var n = document.getElementById(tabText);
	    if (n==null) return;
	    var c=n.parentNode.childNodes;
	    for (e=0;e<parseInt(c.length);e++) if (typeof(c[e].style)!="undefined" && c[e].id!="") if (c[e]!=n) {
			   c[e].style.display="none";
			   }
	    n.style.display="";
	    }

	e=document.getElementById("IFrame " + tabText);
	if (this.selectedCompanyNode!=null && e!=null) {
		 e.style.display="";						
		 switch (tabText){
		    case "Help":
				  this.sizeFrame(e);
					break;
		    case "Contacts":
					this.sizeFrame(e);
			    this.lastInfoTab=ths;
					if (this.Local()) this.DisplayContacts(e); 
					else e.src = this.URL_Contacts();
					break;
		    case "Google":
					this.sizeFrame(e);
			    this.lastInfoTab=ths;
					var google=this.URL_Google();
          if (e.src != google) e.src=google;
			    break;
		    case "Website":
					this.sizeFrame(e);
			    this.lastInfoTab=ths;
					var web ="http://" + this.company.web[this.selectedCompanyNode.id];
          if (e.src != web) e.src = web;
		      break;
				}
			}			
  };
	
	
	

	this.SetWhere=function(f){
		var i=new Array();
		var e;
	  if (this.results_person_indices.length>0) {
			for (e in this.results_person_indices) i.push(this.person.id_person[this.results_person_indices[e]]);
		  f.value="person.id_person in (" + i.join(",") + ")";
			}
	  else if (this.results_address_indices.length>0) {
			for (e in this.results_address_indices) i.push(this.address.id_address[this.results_address_indices[e]]);
		  f.value="address.id_address in (" + i.join(",") + ")";
			}
		else {
			for (e in this.results_company_indices) i.push(this.company.id_company[this.results_company_indices[e]]);
		  f.value="company.id_company in (" + i.join(",") + ")";
	    }
		};	
		
	this.URL_Contacts=function(){
	  var preference="";
		var e;
	  return "contacts.php?"+preference+"c="+this.company.id_company[this.selectedCompanyNode.id];
		};
	this.URL_Google=function(){
	  return "http://www.google.com/search?q="+escape(this.company.company[this.selectedCompanyNode.id]);
		};
	
	 this.formcheck=function(f){
		var nf = new NumberFormat();
		nf.setNumber(f.amount_min.value);
		var amount_min=nf.toUnformatted();
		nf.setNumber(f.amount_max.value);
		var amount_max=nf.toUnformatted();
		if(amount_max<amount_min) {
				alert("Please make the minimum amount less than the maximum amount.");
				return false;
				}
      return true;
		};


	 this.millions=function(ths){ 
 		var nf = new NumberFormat();
		nf.setNumber(ths.value);
		var n = nf.toUnformatted();
		var other_name=(ths.name=="amount_min"?"amount_max":"amount_min");
		nf.setNumber(document.f[other_name].value);
		var other_value=nf.toUnformatted();
		if (n<5000) n*=1000000;
		nf.setNumber(n);
    nf.setPlaces(0);
		ths.value=nf.toFormatted();	
		if ((other_name=="amount_min" && n<other_value) || (other_name=="amount_max" && n>other_value)) {
			document.f[other_name].value=ths.value;
			this[other_name]=n;
			};
	  this[ths.name]=n;
		this.RecalcAmountBinary();
		this.RefreshCompanies();
    };
	 
	 function PreviousSiblingWithTagName(node,tagName) {
	      var n=node.previousSibling;
	 			while (n!=null && n.tagName != tagName && n.tagName != node.tagName) n=n.previousSibling;
	 			if (n==null || n.tagName!=tagName) return null;
	 			return(n);
 	 			};
	 function NextSiblingWithTagName(node,tagName) {
	 			var n=node.nextSibling;
	      while (n!=null && n.tagName != tagName && n.tagName != node.tagName) n=n.nextSibling;
	      if (n==null || n.tagName!=tagName) return null;
	      return(n);
 	      };
	 function EnsureVisible(n){
	 			if (n!=null){
	 			  if (n.tagName=="SPAN") n.style.display = "block";
					if (n.id!="results") EnsureVisible(n.parentNode);
					}
				};
   function NodeOpen(n,yes) {
	      if (n.tagName!="SPAN") n=NextSiblingWithTagName(n,"SPAN");
	 			if (n!=null) {
						var _img=PreviousSiblingWithTagName(n,"IMG");
	 					if (n != null && _img != null) {
	 				  	 n.style.display = (yes ? "block":"none");
	 				  	 _img.src= (yes ? imgFile.opened : imgFile.closed);
				    	 }
						}
	      };
				
   function IsNodeOpen(n) {
	      if (n.tagName!="SPAN") n=NextSiblingWithTagName(n,"SPAN");
	      if (n!=null) {
					 var _img=PreviousSiblingWithTagName(n,"IMG");
	      	 if (n != null && _img != null) return(n.style.display == "block");
					 }
	      return false;
	      };
   function CheckAndDisableChildren(n){
   		 if (n==null) return;
	     if (n.tagName!="SPAN") n=NextSiblingWithTagName(n,"SPAN");
	     for (n=n.firstChild;n!=null;n=n.nextSibling){
			     switch (n.tagName) {
			         case "INPUT":
							     if (n.type=="checkbox") {
										   n.checked=true;
										   n.disabled=true;
										   }
								   break;
						   case "FONT":
								   n.className="disabled";			
								   break;
						   case "SPAN":
								   CheckAndDisableChildren(n);
								   break;
							 }
			     }
	     };
			 
   this.UncheckAndEnableChildren=function(n){
	     if (n.tagName!="SPAN") n=NextSiblingWithTagName(n,"SPAN");
	     for (n=n.firstChild;n!=null;n=n.nextSibling){
			     switch (n.tagName) {
			         case "INPUT":
							     if (n.type=="checkbox") {
										   n.checked=false;
										   n.disabled=false;
											 }
								   break;
						   case "FONT":
								   n.className="enabled";			
								   break;
						   case "SPAN":
								   this.UncheckAndEnableChildren(n);
								   break;
							 }
			     }
	     };	 
   this.UncheckChildren=function(n){
	     for (n=n.firstChild;n!=null;n=n.nextSibling){
			     switch (n.tagName) {
			         case "INPUT":
							     if (n.type=="checkbox") n.checked=false;
								   break;
						   case "SPAN":
								   this.UncheckChildren(n);
								   break;
							 }
			     }
	     };	 
		
	 this.checkbox_click=function(n){
			 var id=n.id.split('_');
			 var seg=id[0];
			 var group=id[1];
			 var id=id[2];													
	     var _span=NextSiblingWithTagName(n,"SPAN");
	     if (_span!=null) {
	 		     if (n.checked) {
				       NodeOpen(n,false);
				       CheckAndDisableChildren(_span);
				       EnsureVisible(n);
							 }
			     else {	
				       this.UncheckAndEnableChildren(_span);
				       }
			     }
			 CheckBinary(seg,this,group,id,n.checked);
			 this.RefreshCompanies();
	     };
	
	 this.folder_click=function(n){
	      NodeOpen(n,!IsNodeOpen(n));
	      };
	
   function cloneArray(what) {
	   var c=what.length;
	   var a=new Array(c);
		 var i;
		 for (i=0;i<c;i++) a[i]=what[i];
		 return(a);
     };
		 
	 function ORGroup(seg,ths,group,i){			
	 		 if (i==null) {
	 	     if (typeof(ths.binary_group_i_list[seg][group])!="undefined")	{
					 delete ths.binary_group_result[seg][group];
					 var i;
					 for (i in ths.binary_group_i_list[seg][group]) {
						 var bi = ths.binary[seg][i];
					   if (typeof(ths.binary_group_result[seg][group])=="undefined") ths.binary_group_result[seg][group]=cloneArray(bi);
						 else {
							 var e;
							 var l=bi.length;
							 var bgr=ths.binary_group_result[seg][group];
							 for (e=0;e<l;e++) bgr[e] |= bi[e];
							 }  
						 }
					 }
				 ANDGroups(seg,ths,null);
			   }
			 else {
					 var bi=ths.binary[seg][i];
					 if (typeof(ths.binary_group_i_list[seg][group])=="undefined") ths.binary_group_i_list[seg][group]=new Array();
					 ths.binary_group_i_list[seg][group][i]=null;
				   if (typeof(ths.binary_group_result[seg])=="undefined")  ths.binary_group_result[seg]=new Array();
				   if (typeof(ths.binary_group_result[seg][group])=="undefined")  {
					 		ths.binary_group_result[seg][group]=cloneArray(bi);
						  ANDGroups(seg,ths,group); // AND this group into the result
							}
					 else {
							var e;
							var l=bi.length;
						  var bgr=ths.binary_group_result[seg][group];
							for (e=0;e<l;e++) bgr[e] |= bi[e];
					 		ANDGroups(seg,ths,null);
							}  
					 }
			 };
	 function ANDGroups(seg,ths,gr){
	 		if (gr==null) {
			   delete ths.binary_result[seg];
				 var gr;
				 for (gr in ths.binary_group_result[seg]) {
				 		 if (typeof(ths.binary_result[seg])=="undefined") {
						 	 ths.binary_result[seg]=cloneArray(ths.binary_group_result[seg][gr]);
							 }
						 else {
						   var bi= ths.binary_group_result[seg][gr];
							 var e;
							 var l=bi.length;
							 var br=ths.binary_result[seg];
							 for (e=0;e<l;e++) br[e] &= bi[e];
							 }
						 }
				 }
			else {
				 if (typeof(ths.binary_result[seg])=="undefined") {
				 		ths.binary_result[seg]=cloneArray(ths.binary_group_result[seg][gr]);
						}
				 else {
						 var bi= ths.binary_group_result[seg][gr];
						 var l=bi.length;
						 var e;
						 var br=ths.binary_result[seg];
						 for (e=0;e<l;e++) br[e] &= bi[e];
						 }
				 }
			};
		
	 function array_keys(a){
	 		var e;
			var c=0;
			var r = new Array();
			for (e in a) r[c++]=e;
			return r;
			};
			
	 this.checkbox_setting=function(){
	 		var e;
			var gr;
			var pr=new Array();
			var pc=0;
			var seg="";
			for (seg in this.binary_group_i_list) for (gr in this.binary_group_i_list[seg]) for (e in this.binary_group_i_list[seg][gr]) pr[pc++]=seg+"_"+gr+"_"+e;
			return(pr.join()+"");
	 };
	 this.RecalcAmountBinary=function(){
	    if((0==this.amount_min) && (0==this.amount_max)) {
			  if (typeof(this.binary_group_result.preference["Amount"])!="undefined") delete this.binary_group_result.preference["Amount"];
				}
			else {
	      var l =this.preference.amount_min.length;
	      var a =new Array();
			  var e;
			  for (e=0;e<=(l>>4);e++) a.push(0);
        if (this.amount_min==0) {
					 for (e=0;e<l;e++) if (this.amount_max >= this.preference.amount_min[e]) a[e>>4] |= (1<<(e & 15));	
					 }
        else if (this.amount_max==0) {
						 for (e=0;e<l;e++) if (this.amount_min <= this.preference.amount_min[e] || this.preference.amount_max[e]==0) a[e>>4] |= (1<<(e & 15));	
						 }
				else {
						 for (e=0;e<l;e++) if (!(
						                        (this.amount_min > this.preference.amount_max[e] && this.preference.amount_max[e] > 0) 
		                                ||(this.preference.amount_min[e] > this.amount_max)
                                    )) a[e>>4] |= (1<<(e & 15));
						 }
			  this.binary_group_result.preference.Amount=a;	
 			  }	
			ANDGroups('preference',this,null);
		  };
			
	 this.RefreshCompanies = function(start){
	 		
	    if (!this.allowRefresh) return;
			if (typeof(start)=="undefined") start="refresh";
			if (start==null) start="refresh";
		  this.results_preference_indices=null;
	 		this.results_address_indices=null;
			var a;
			var p;
		  this.results_company_indices=new Array(); 
		  this.results_address_indices=new Array(); 
		  this.results_person_indices=new Array(); 
			this.results_preference_indices=new Array();
			var ifpp=this.index_from.preference.person; 
			var ifpa=this.index_from.person.address; 
			var ifac=this.index_from.address.company;
			var cl=this.company.company.length;
			var previous_c=null;
			var previous_a=null;
			var previous_person=null;
			 		
			if ((typeof(this.binary_result.preference)!="undefined") && (typeof(this.binary_result.address)!="undefined")){
			  var br_p=this.binary_result.preference; 
			  var br_a=this.binary_result.address; 
			  var br_p_bits = br_p.length<<4;
			  var br_a_bits = br_a.length<<4;
				for (p=0;p<br_p_bits;p++) {
					if (br_p[p>>4] & (1<<(p & 15))) {
				    var person=ifpp[p];
						var a=ifpa[person];
					  if (br_a[a>>4] & (1<<(a & 15))) {
						  this.results_preference_indices.push(p);
							if (person!=previous_person) {
							  this.results_person_indices.push(person);
								pervious_person=person;
						    if (a!=previous_a) {
							    this.results_address_indices.push(a); 
							    previous_a=a;
						      var c=ifac[a];
						      if (c != previous_c) {
							      this.results_company_indices.push(c); 
							      previous_c=c;
							      }
									}
							  }
							}
						}
					}
				}
				
			if ((typeof(this.binary_result.preference)!="undefined") && (typeof(this.binary_result.address)=="undefined")){
			  var br_p=this.binary_result.preference; 
			  var br_p_bits = br_p.length<<4;
				for (p=0;p<br_p_bits;p++) {
					if (br_p[p>>4] & (1<<(p & 15))) {
						this.results_preference_indices.push(p);
						var person=ifpp[p];
					  if (person!=previous_person) {
							this.results_person_indices.push(person);
							previous_person=person;
							var a=ifpa[person];
						  if (a!=previous_a) {
							  this.results_address_indices.push(a); 
							  previous_a=a;
						    var c=ifac[a];
						    if (c != previous_c) {
							    this.results_company_indices.push(c); 
							    previous_c=c;
							    }
								}
							}
						}
					}
				}
			if ((typeof(this.binary_result.preference)=="undefined") && (typeof(this.binary_result.address)!="undefined")){
			  var br_a=this.binary_result.address; 
			  var br_a_bits = br_a.length<<4;
				for (a=0;a<br_a_bits;a++) {
					if (br_a[a>>4] & (1<<(a & 15))) {
						this.results_address_indices.push(a);
						var c=ifac[a];
						if (c != previous_c) {
							this.results_company_indices.push(c); 
							previous_c=c;
							}
						}
					}
				}
				
			if ((typeof(this.binary_result.preference)=="undefined") && (typeof(this.binary_result.address)=="undefined")){
				for (c=0;c<cl;c++) this.results_company_indices.push(c);
			  }		
			this.DisplayCompanies(start);
		};
		
		function clearspan(_span){
			while (_span.lastChild != null) {
				clearspan(_span.lastChild);
				_span.removeChild(_span.lastChild);
				var n =_span.lastChild;
				delete n;
				}
			};
	function trim(s){
   	s = s.replace(/^(\s*)/, ''); 
   	s = s.replace(/(\s*)$/, ''); 
	 	return(s);
	 	};
  this.find_company_indice=function(company){
			  company=trim(company);
	      if (company=="") return 0;
				var e = (this.results_company_indices.length+1)>>1;
				var increment=(e+1)>>1;
				while (increment>0){
				    if (this.company.company[this.results_company_indices[e]].toUpperCase()==company.toUpperCase()) return(e);
						if (this.company.company[this.results_company_indices[e]].toUpperCase()>company.toUpperCase()) e-=increment; else e+=increment;
						if (increment==1) increment=0;
						increment=(increment+1)>>1;
						}
				if (this.company.company[this.results_company_indices[e]].toUpperCase()<company.toUpperCase()) e++;
				return(e);
				};		
	this.search=function(n){
	      this.DisplayCompanies(this.find_company_indice(n.value));
	      };
	
	var timer=null;
	this.resizer = function(){
		if (timer != null) clearTimeout(timer);
    timer=setTimeout('t.click_tab(t.lastTab)',250); 
	  }
	
	this.DisplayCompanies = function(button){
	  var originalSelectedCompanyNode=this.selectedCompanyNode;
		if (icon_isgray(button)) return;
		var start=this.results_start; /* results = display list */
		var column=document.getElementById("left_column");
		var span_position=document.getElementById("position");	
		var _span=document.getElementById("results");	
		if (this.results_company_indices.length==0) {
		   this.selectedCompanyNode=null;
			 _span.innerHTML="No matches.";
			 span_position.innerHTML="";
			 adjust_buttons(this);	
			 return;
			 }
		var r;
		var i;
		var rows=Math.min(rows_max,this.results_company_indices.length-start);
		var rows_max;
		var rowHeight=0;
		var selected=null;
		clearspan(_span);
		_span.innerHTML="";
		rowHeight=column.clientHeight;
		_span.innerHTML += "<BR>\n";
		rowHeight=column.clientHeight-rowHeight;
		_span.innerHTML="";

    rows_max=parseInt(((browserClient().height-getRealTop(_span))/rowHeight));
		if (this.selectedCompanyNode!=null) selected=this.selectedCompanyNode.id;
		this.selectedCompanyNode=null;
		var increment=0;
		switch (button){
			case "refresh":
				//alert("ELIMINATE REFRESH");
				//return;
			case "first":
			  start=0;
				break;
			case "resize":
				//alert("ELIMINATE RESIZE");
				//return;
			  increment=0;
				break;
			case "previous":
			  increment=-1;
				break;
			case "next":
			  increment=1;
				break;
			case "backward":
			  increment=-rows_max;
				break;
			case "forward":
			  increment=rows_max;
				break;
			case "last":
			  start=this.results_company_indices.length-rows_max;
				break;
			default:
				start=parseInt(button);
				break;
		  }
			
			
		start+=increment;


		start=Math.max(0,Math.min(this.results_company_indices.length-rows_max,start));
		var rows=Math.max(0,Math.min(rows_max,this.results_company_indices.length-start));
		var stop=start+rows; 
		var s=new Array();
		var selected_index=-1;
				for (r=start;r<stop;r++) {
		    i=this.results_company_indices[r];
				if (i==selected) {
					 selected_index=r-start;
					 }
				s.push("<font style='font-family:sans-serif;vertical-align:top;white-space: nowrap;cursor:hand;cursor:pointer' id="+i+" onselectstart='return false;' onmousedown='t.mouse_company_click(this);return false;'>"+Ellipsis(this.company.company[i])+"<br></font>");
				}
		_span.innerHTML=s.join("");
			

		this.results_start=start;
		span_position.innerHTML=(start+1) + "-" + (stop) + " of " + this.results_company_indices.length;


		if (start==0 && (button=="first" || button=="backward")) {
			 selected_index=0;
			 }
		if (stop==this.results_company_indices.length && (button=="last" || button=="forward")) selected_index=(_span.childNodes.length-1);
		if (selected_index>=0) {
			 this.selectedCompanyNode =_span.childNodes[selected_index];
			 if (increment==1 && this.selectedCompanyNode.nextSibling!=null) this.selectedCompanyNode=this.selectedCompanyNode.nextSibling;
			 if (increment==-1 && this.selectedCompanyNode.previousSibling!=null) this.selectedCompanyNode=this.selectedCompanyNode.previousSibling;
			 }
		else {
		  if (button=="last") this.selectedCompanyNode=_span.lastChild;
			else  {
					 if (button=="previous") this.selectedCompanyNode=_span.lastChild;
					 else { 
					 	   this.selectedCompanyNode=_span.firstChild;
							 }
					 }
			}
		
		this.company_click(this.selectedCompanyNode,button!="resize" && button!="refresh");

	  };			
		
	function adjust_buttons(ths){
			  var _span=document.getElementById("results");	
				var rows_shown=(_span.firstChild!=null?_span.childNodes.length:0);
				if (ths.selectedCompanyNode!=null && rows_shown>0){
				    icons_enable(ths.results_start>0 || ths.selectedCompanyNode.previousSibling!=null,"first","backward","previous");
				    icons_enable(ths.results_start<(ths.results_company_indices.length-rows_shown) || (ths.selectedCompanyNode.nextSibling!=null),"next","forward","last");
						}
				else{
				    icons_enable(false,"first","backward","previous","next","forward","last");
						};
			};		
	function icons_enable(){
	    var e;
			for (e=1;e<arguments.length;e++) {
					var n=document.getElementById("icon_" + arguments[e] );
					n.src=imgFile[arguments[e]+(arguments[0]?"":"_gray") ];
					}
			};
	function icon_isgray(i){
			var n=document.getElementById("icon_" + i );
			if (n==null) return false;
			return(/_gray\.gif/.test(n.src));
			};
	function Ellipsis(text){
	    var maxlen=20;
			if (typeof(text)=="undefined") return("BUG!");
	    return (text.length>maxlen ? text.substr(0,maxlen) + "..." : text);
	    };
  function http(s){ 
     if (s.substr(0,7)!=("http:/" + "/")) s="http:/"+"/"+s;
	   return(s);
    };
  this.mouse_company_click=function(n){
	    if (n==null) return;
			this.company_click(n,true);
			};
   this.company_click=function(n,bymouse){
	    if (n==null) return;
		  if (this.selectedCompanyNode!=null && this.selectedCompanyNode!=n) this.selectedCompanyNode.className="deselected";
			n.className="selected";
			this.selectedCompanyNode=n;
			adjust_buttons(this);
			if (bymouse) this.click_tab(this.lastInfoTab);
	    };
/*			
	this.keyboard_company_click=function(key){
	    if (this.selectedCompanyNode==null) 
	    };
*/		
			
			
	 function CheckBinary(seg,ths,group,i,checked){
	 		if (checked) ORGroup(seg,ths,group,i);
			else {
				BinaryClear(seg,ths,group,i);
				ORGroup(seg,ths,group,null); // with no index ORs what we have again
				}
			ths.results_start=0;
			ths.results_total=0;
			};
	 function BinaryClear(seg,ths,group,i){
		 if (typeof(ths.binary_group_i_list[seg][group][i]!="undefined")) delete ths.binary_group_i_list[seg][group][i] ;
			};
	 this.ClearAll=function(){
	    this.allowRefresh=false;
	    var e;
	    var ids = this.checkbox_setting();
			if (ids!="") {
	      ids = ids.split(",");
			  for (e=0;e<ids.length;e++) document.getElementById(ids[e]).checked=false;
				}
			document.getElementById('amount_min').value="";
			document.getElementById('amount_max').value="";
	    this.binary_group_i_list=new Array(); 
	    this.binary_group_i_list.address=new Array(); 
	    this.binary_group_i_list.preference=new Array(); 
	    this.binary_group_result=new Array();
	    this.binary_group_result.address=new Array();
	    this.binary_group_result.preference=new Array();
	    this.binary_result=new Array();
      this.allowRefresh=true;
			this.RefreshCompanies();
	    };

	};

function NumberFormat(num)
{
	this.setNumber=function(num){
		this.numOriginal = num;
		this.num = this.justNumber(num);
		};
	this.toUnformatted=function(){
		return (this.num);
		};
	this.getOriginal=function(){
		return (this.numOriginal);
		};
	this.setCommas=function(isC){
		this.isCommas = isC;
		};
	this.setCurrency=function(isC){
		this.isCurrency = isC;
		};
	this.setCurrencyPrefix=function(cp){
		this.currencyPrefix = cp;
		};
	this.setPlaces=function(pl){
		this.places = pl;
		};
	this.toFormatted=function(){
		var pos;
		var nNum = this.num; // v1.0.1 - number as a number
		var nStr;            // v1.0.1 - number as a string
		if (!nNum) return("");
		nNum = this.getRounded(nNum);
		nStr = this.preserveZeros(Math.abs(nNum)); // this step makes nNum into a string. v1.0.1 Math.abs
		if (this.isCommas)
		{
			pos = nStr.indexOf('.');
			if (pos == -1) pos = nStr.length;
			while (pos > 0)
			{
				pos -= 3;
				if (pos <= 0) break;
				nStr = nStr.substring(0,pos) + ',' + nStr.substring(pos, nStr.length);
			}
		}	
		nStr = (nNum < 0) ? '-' + nStr : nStr; 
		if (this.isCurrency) nStr = this.currencyPrefix + nStr;
		return (nStr);
	};
	this.getRounded=function(val){
		var factor;
		var i;
		factor = 1;
		for (i=0; i<this.places; i++) factor *= 10; 
		val *= factor;
		val = Math.round(val);
	  val /= factor;
		return (val);
	};
	this.preserveZeros=function(val){
		var i;
		val = val + '';
		if (this.places <= 0) return val; // leave now. no zeros are necessary - v1.0.1 less than or equal	
		var decimalPos = val.indexOf('.');
		if (decimalPos == -1){
			val += '.';
			for (i=0; i<this.places; i++)	{
				val += '0';
			}
		}
		else	{
			var actualDecimals = (val.length - 1) - decimalPos;
			var difference = this.places - actualDecimals;
			for (i=0; i<difference; i++)val += '0';
		}	
		return val;
	};
	
	this.justNumber=function(v){
		v = ((v==null) ? 0 : v);
		if (isNaN(v))	{
			var newVal = parseFloat(v.replace(/[^\d\.\-]/g, ''));
			return (isNaN(newVal) ? 0 : newVal); 
		}
		
		else if (!isFinite(v)) return 0;
		return v;
		
	};
	
	this.num;
	this.numOriginal;
	this.isCommas;
	this.isCurrency;
	this.currencyPrefix;
	this.places;
	this.setNumber(num);
	this.setCommas(true);
	this.setCurrency(true);
	this.setCurrencyPrefix('$');
	this.setPlaces(2);
};

