// A Javascript widget table used for displaying query results.


//GLOBAL VARIABLES
var printTable
var records
var newaltrow
var newitemrow
var firstrow

var elem = {};

//Constructor
elem.Table = function(arrTable,rows,spanName,headers,colSize,colType,ref,tableclass,headerrow,firstrow,itemrow,altrow){
	

this.pages = 0;
this.rem =0;
this.numPages=0;
this.status=null;
this.arrTable= arrTable;
this.spanName = spanName;
this.rows = parseInt(rows);
this.currentpage =0;
this.strResults="";
this.fin=0;
this.init=0;
this.rowstyle="";
this.rem = 0;
this.numPages = 0;
this.sel=null;
this.headers=headers;
this.colSize =colSize;
this.ref=ref;
this.tableclass = tableclass;
this.firstrow = firstrow;
this.itemrow = itemrow;
this.altrow=altrow;
this.headerrow = headerrow;
this.selnum=null;
this.sortColumn=null;
this.sortDirection="ASC";
this.sortarrow="";
this.colType=colType;

}
// Function to build the table
elem.Table.prototype.buildTable = function(page){

	this.rem = this.arrTable.length%this.rows;
	this.numPages = this.rem>0 ? Math.floor(this.arrTable.length/this.rows)+1 : Math.floor(this.arrTable.length/this.rows);
	this.currentpage=page;
	
	//get the current set
	var setnum = this.currentpage%5==0 ? parseInt((this.currentpage/5)-1) : parseInt((this.currentpage/5));
	//get the current set start number
	this.currentsetstart = setnum*5 +1;
	//alert(this.currentpage +" | " +setnum+" | " + this.currentsetstart);
	this.strResults="";
	this.init = (this.currentpage-1)*this.rows;  // number for table loop
	if(this.currentpage == this.numPages&&this.rem>0){
		this.fin = this.init + this.rem;
	}else{
		this.fin=this.init + this.rows;
	}
	
	if(this.arrTable.length==0){
		this.strResults="No Images Found Matching Your Request";
	}
	else
	{
		
	
	var clname="";
	//check to see if we are in the last set or not
	var iter = this.numPages > this.currentsetstart+5 ? this.currentsetstart+5 : this.numPages+1;
	if(this.currentsetstart> 1)
	    {
	    		this.strResults+= "<a class='bluemenu'  href='javascript:"+ this.ref+".buildTable(1)'><< </a>"
	    		this.strResults+= "<a class='bluemenu'  href='javascript:"+ this.ref+".buildTable("+(this.currentsetstart-1)+")'>... </a>"
        }
        
        
	for(j=this.currentsetstart;j<iter;j++){
		if(j==this.currentpage){
		clname="currentPage";
		}else{
		clname="bluemenu";
		}
		this.strResults+= "<a class='"+clname+"'  href='javascript:"+ this.ref+".buildTable("+j+")'> "+j+" </a>"
	}
	
	if(this.currentsetstart+5 < this.numPages)
	    {
	    		this.strResults+= "<a class='bluemenu'  href='javascript:"+ this.ref+".buildTable("+(this.currentsetstart+5)+")'> ...</a>"
	    		this.strResults+= "<a class='bluemenu'  href='javascript:"+ this.ref+".buildTable("+this.numPages+")'> >></a>"
        }
        
	this.strResults+="<br>";
	 
	this.sel = document.getElementById(this.spanName);
	this.strResults+="<table class='"+this.tableclass+"' cellspacing='0' cellpadding='0'><tr class='"+this.headerrow+"'>"
	
	for(i=0;i<this.headers.length;i++){
		
		this.strResults+="<td width='"+this.colSize[i]+"' onclick='"+this.ref+".checkSort("+i+")'>"+this.headers[i];
		
		if(this.sortColumn == i&& this.sortDirection=="ASC"){
			this.strResults+=" <img src='images/ASC.gif'>";
		}else 
		{
			if(this.sortColumn == i&& this.sortDirection=="DESC")
			this.strResults+=" <img src='images/DSC.gif'>"
		}
		this.strResults+="</td>";
	}
	this.strResults+="</tr>";
	var counter=0;
		for (i=this.init;i<this.fin;i++){
			
   			
   			 if(counter%2 !=0){
   				 this.rowstyle = this.altrow;
   			 }else{
   				 this.rowstyle = this.itemrow;
   			 }
   			 if(counter==0){
   			 this.rowstyle = this.firstrow;
   			 }
			
   			 this.strResults += "<tr class='"+ this.rowstyle+"'>"
			 	for(j=0;j<this.arrTable[i].length;j++){
					
					if(this.colType[j]=="link")
					{
						
						var col = this.arrTable[i][j].split("|");
						
						this.strResults +="<td style='text-decoration:underline; cursor:hand;' onClick=\""+col[1]+"\">"+col[0]+"</td>"
						
					}
					
					else if(this.colType[j]=="Date")
					{
						var vdate = new Date();
					
						if(this.arrTable[i][j]!="")
						{
						vdate.setTime( this.arrTable[i][j]);
						var newDate = "<td>"+(vdate.getMonth()+1)+"/"+ vdate.getDate()+"/"+ vdate.getYear()+"</td>"
						}
						
						if (newDate == "<td>1/11/1111</td>") {
							newDate = "<td>&nbsp;</td>"
						
						}
						this.strResults += newDate
						 
					}
					else if(this.colType[j]=="Currency")
					{
						if(this.arrTable[i][j]!=0)
						{
						 this.strResults +="<td>$"+CommaFormatted(this.arrTable[i][j])+ "</td>"
						}
						else
						{
						 this.strResults +="<td>&nbsp;</td>" ;
						}
						 
					}
					else{
					 this.strResults +="<td>"+this.arrTable[i][j]+ "</td>"
					}
				}
				
			 this.strResults +="</tr>"; 
		
   		 	counter++
		 }
   	 
   	this.strResults += "</table>";
	}

    this.sel.innerHTML = this.strResults
	this.sel.style.display ="block";
	document.getElementById('numResults').innerHTML = this.arrTable.length
    
   
   //pass table to print page
    printTable = this.arrTable
    newaltrow = this.altrow
    newitemrow = this.itemrow
    firstrow = this.firstrow
    records = this.arrTable.length
	
    
}

//Function to step through the pages of results

 elem.Table.prototype.changeNumPage = function(num) {
	 this.rows = parseInt(num);
	 this.buildTable(1);
	 
 }
 //Function to check sort direction of  the results
 
 elem.Table.prototype.checkSort = function(element){
 
	var len = this.arrTable.length;
	
	if(len <1000)
	{
	     if (element==this.sortColumn)
	          {
	    	    if(this.sortDirection=="ASC")
		            {
			         this.sortColumn=element;
			         this.sortDirection="DESC"
			         this.Sort(element,"DESC");
		             }
		         else 
		            {
			         this.sortColumn=element;
			         this.sortDirection="ASC"
			         this.Sort(element,"ASC");
		             }
		 
	        }
	       else
	       {
			 this.sortColumn=element;
			 this.sortDirection="ASC"
			 this.Sort(element,"ASC");
	       }
	 
	 }
	else
	{
	alert("This table contains more then 1,000 entries. Sorting has been disabled")
	}
}
 
 //Function to sort the results
 
 elem.Table.prototype.Sort = function(element,direction) {
	
	var arrayName = this.arrTable;
	var len = this.arrTable.length;
	
	if(direction=="ASC"){
	
    for (var i=0; i<(len-1); i++){
        for (var j=i+1; j<len; j++){
            if (arrayName[j][element] < arrayName[i][element]) {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
            }
			
		}
	}
	}else{
		
	for (var i=0; i<(len-1); i++){
        for (var j=i+1; j<len; j++){
            if (arrayName[j][element] > arrayName[i][element]) {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
            }
			
		}
	}
	}
	
//	alert(elem.Table[0])
	this.buildTable(this.currentpage);
	
	
 }
 
// -----------------------------------------------------------------------------------------------



 function createSummaryPrint() {
 arrTable = printTable

	
	var strResults=""
	strResults+= "<p class='printtitle' align='center'>Summary List</p>"
	strResults+= "<p class='body'><strong>Search Criteria:</strong> " + searchstring +"</p>"
	strResults+= "<p class='body'><strong>Records:</strong> "+records+"</p>"
	
	strResults+="<table class='currentPageS' align='center' cellspacing='0' cellpadding='0'>"
    strResults+="<tr  class='headerrow'>"
    strResults+="<td width='80' height='23'><p align='left'><strong>Parcel ID</strong></p></td>"
    strResults+="<td width='150'><p align='left'><strong>Address</strong></p></td>"
    strResults+="<td width='80'><p align='left'><strong>Sale Date </strong></p></td>"
    strResults+="<td width='100'><p align='left'><strong>Sale Price </strong></p></td>"
    strResults+="<td width='100'><p align='left'><strong>Value</strong></p></td>"
    strResults+=" <td width='100'><p align='left'><strong>Lot Size</strong></p></td>"
    strResults+="</tr>"
	
for(var i =0;i<arrTable.length;i++) {	
	
	var myArray = new Array();
	var rowstyle = ""
			
	myArray[0] = arrTable[i]
	var list = arrTable[i].toString()
	
	var stringArray = list.split(",");
	
	 if(i%2 !=0){
   				 rowstyle = newaltrow;
   			 }else{
   				 rowstyle = newitemrow;
   			 }
   	if(counter==0){
   			 rowstyle = firstrow;
   			 }
	strResults+= "<tr class="+"'"+rowstyle+"'"+">"
   strResults+= "<td><p>&nbsp;&nbsp;&nbsp;"+stringArray[2]+"</p></td>"
   strResults+= "<td><p>"+stringArray[3]+"</p></td>"
   //formats the date
	var vdate = new Date();
	var myDate		
			  
	            if(stringArray[4]!="&nbsp;")
		        {
		        var vdate = new Date(stringArray[4]);
		       
		         myDate =(vdate.getMonth()+1)+"/"+ vdate.getDate()+"/"+ vdate.getYear()
		        }
		        else
		         {
		        myDate ="&nbsp;";
		        }
		       
			
    strResults+= "<td><p>"+myDate+"</p></td>"
    strResults+= "<td><p>"+"$"+CommaFormatted(stringArray[5])+"</p></td>"
    strResults+= "<td><p>"+"$"+CommaFormatted(stringArray[6])+"</p></td>"
    strResults+= "<td><p>"+stringArray[7]+"</p></td>"
   strResults+= " </tr>"

	}
strResults+= "</table>"



////--------------------------------

  var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
      disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
  
  
  var docprint=window.open("","",disp_setting); 
   docprint.document.open(); 
   docprint.document.write('<html><head>'); 
   docprint.document.write('<LINK media="all" href="_includes/styles.css" type="text/css" rel="stylesheet">')
   docprint.document.write('</head><body>');  
   docprint.document.write('<span id="print_content">');         
   docprint.document.write(strResults);    
   docprint.document.write('</span>');      
   docprint.document.write('</body></html>'); 
   docprint.document.close(); 
   docprint.focus(); 

}




