// JavaScript Document

function Criterion() {
	this.aValues = new Array;
	if (typeof Criterion._initialized == "undefined"){
		Criterion.prototype.addValue = function (sValue){
			this.aValues.push(sValue);
		};
		Criterion.prototype.removeValue = function (sValue){
			for(i=0; i<this.aValues.length; i++){
				if (this.aValues[i]==sValue){
					this.aValues.splice(i, 1);
					break;
				}
			}
		};
		Criterion._initialized = true;
	}
}

function DataBox() {
	 this.shape = new Criterion;
	 this.color = new Criterion;
	 this.clarity = new Criterion;
	 this.cut = new Criterion;
	 this.polish = new Criterion;
	 this.symmetry = new Criterion;
	 this.fluorescence = new Criterion;
	 this.intensity = new Criterion;
	if (typeof DataBox._initialized == "undefined"){
		
		DataBox._initialized = true;
	}
}
  var myData = new DataBox;
  
  
function Slider(iLower, iUpper, iMinValue, iMaxValue, iPresMin, iPresMax, dec, oDiv) {
	this.lower = iLower; // The left pixel value of the min slider
	this.upper = iUpper; // The left pixel value of the max slider
	this.decimal = dec; // Tells the slider how many decimal place to round values to
	if(this.decimal == 0){
		this.minValue = stripCurrency(iMinValue); // The absolute minimum value posible in the database
	    this.maxValue = stripCurrency(iMaxValue); // The absolute maximum value posible in the database
	    this.presMin = stripCurrency(iPresMin); // The presently selected minimum value for a search
	    this.presMax = stripCurrency(iPresMax); // The presently selected maxmum value for a search
	}else {
	    this.minValue = iMinValue; // The absolute minimum value posible in the database
	    this.maxValue = iMaxValue; // The absolute maximum value posible in the database
	    this.presMin = iPresMin; // The presently selected minimum value for a search
	    this.presMax = iPresMax; // The presently selected maxmum value for a search
	}
	this.decimal = dec; // Tells the slider how many decimal place to round values to
	this.currentMin = 0;
	this.currentMax = 215;
	this.obj = oDiv;
	
	for (i=0; i<oDiv.childNodes.length; i++){
		 
		if (oDiv.childNodes[i].className == "inputSliderMin"){
			this.minBox = oDiv.childNodes[i];
			if(this.decimal==0){
			  this.minBox.value = formatCurrency(this.minValue); // the textbox that shows the minimum value
			} else {
				 this.minBox.value = this.minValue;
			}
		}
		if (oDiv.childNodes[i].className == "inputSliderMax"){
			this.maxBox = oDiv.childNodes[i];
			if(this.decimal==0){
			  this.maxBox.value = formatCurrency(this.maxValue); // the textbox that shows the maximum value
			} else {
				 this.maxBox.value = this.maxValue;
			}
		}
		if (oDiv.childNodes[i].className == "slider"){
			
			var mySlide = oDiv.childNodes[i];
			
			for(j=0;j<mySlide.childNodes.length; j++){
				
				if (mySlide.childNodes[j].className == "slideMin"){
			      this.minSlide = mySlide.childNodes[j];
				}
				if (mySlide.childNodes[j].className == "slideMax"){
			      this.maxSlide = mySlide.childNodes[j];
				}
			}
		}
		
	}
	
	//calculate logarithmic scale for price
	if(this.decimal == 0){
		this.priceScale = new Array();
			for(i=0; i<=193; i++){
				var  myPrice; 
				if(i==0){
					myPrice = this.minValue;
				} else if(i==193){
					myPrice = this.maxValue;
				} else {
					myPrice = Math.floor(Math.pow((this.maxValue/this.minValue),(i/193)) * this.minValue);
				}
				
			  myPrice = formatCurrency(myPrice);	
			  this.priceScale[i] = myPrice;
			}
	}else if(this.decimal == 1){
		this.priceScale = new Array();
			for(i=0; i<=193; i++){
				var  myPrice; 
				if(i==0){
					myPrice = this.minValue;
				} else if(i==193){
					myPrice = this.maxValue;
				} else {
					var multiplier=(this.maxValue-this.minValue)/193;
					var val1 = i*multiplier;
					var val2 = Math.round(val1*10)/10;
					var myPrice = this.minValue + val2;
					myPrice = Math.min(this.presMax,myPrice);
					myPrice = Math.max(this.minValue,myPrice);
				}
				
			  	
			  this.priceScale[i] = myPrice;
			}
	} else if(this.decimal==2){
		this.priceScale = new Array();
			for(i=0; i<=193; i++){
				//var  myPrice; 
				//if(i==0){
				//	myPrice = this.minValue;
				//} else if(i==193){
				//	myPrice = this.maxValue;
				//} else {
				//	var multiplier=(this.maxValue-this.minValue)/193;
					//var val1 = i*multiplier;
					//var val2 = Math.round(val1*100)/100;
					//var myPrice = Math.round((this.minValue + val2)*100)/100;  
					//myPrice = Math.min(this.presMax,myPrice);
					//myPrice = Math.max(this.minValue,myPrice);
				//}
				var  myPrice; 
				if(i==0){
					myPrice = this.minValue;
				} else if(i==193){
					myPrice = this.maxValue;
				} else {
					myPrice = Math.pow((this.maxValue/this.minValue),(i/193)) * this.minValue;
				}
			  myPrice =	Math.round(myPrice*100)/100;
			  this.priceScale[i] = myPrice;
			}
	}
	if (typeof Slider._initialized == "undefined"){
		// *************************************************************************
		//method to reset sliders to previously selected minimum and maximum
		Slider.prototype.resetSliders = function (myPresMin,myPresMax){
			this.presMin = myPresMin;
			this.presMax = myPresMax;
			//if rounded to 0 decimals
			if(this.decimal == 0){				
				for(i=0;i<=193;i++){
					if(parseInt(this.presMin) <= parseInt(stripCurrency(this.priceScale[i]))){
					this.minBox.value = formatCurrency(this.presMin);
					this.minSlide.style.left = i + "px";
					this.lower = i;
				    break;
					}
				}
				for(i=193;i>=0;i--){
					if(parseInt(this.presMax) >= parseInt(stripCurrency(this.priceScale[i]))){
				    maxIndex=i + 22;
					this.maxBox.value = formatCurrency(this.presMax);
					this.maxSlide.style.left = maxIndex + "px";
					this.upper = maxIndex;
				    break;
					}
					
				}
			// if rounded to 1 decimal
			} else if(this.decimal == 1){
				for(i=0;i<=193;i++){
					if(parseFloat(this.presMin) <= parseFloat(this.priceScale[i])){
					this.minBox.value = this.presMin;
					this.minSlide.style.left = i + "px";
					this.lower = i;
				    break;
					}
				}
				for(i=193;i>=0;i--){
					if(parseFloat(this.presMax) >= parseFloat(this.priceScale[i])){
				    maxIndex=i + 22;
					this.maxBox.value = this.presMax;
					this.maxSlide.style.left = maxIndex + "px";
					this.upper = maxIndex;
				    break;
					}
				}
				
			// if rounded to 2 decimals	
			} else if(this.decimal == 2){
				var minIndex = 0;
				var maxIndex = 0;
				
				for(i=0;i<=193;i++){
					if(parseFloat(this.presMin) <= parseFloat(this.priceScale[i])){
					this.minBox.value = this.presMin;
					this.minSlide.style.left = i + "px";
					this.lower = i;
				    break;
					}
				}
				for(i=193;i>=0;i--){
					if(parseFloat(this.presMax) >= parseFloat(this.priceScale[i])){
				    maxIndex=i + 22;
					this.maxBox.value = this.presMax;
					this.maxSlide.style.left = maxIndex + "px";
					this.upper = maxIndex;
				    break;
					}
			    }    
			}
		};
		
		//**********************************************************************************
		//method to reset absolute minimum and absolute maximum value of a slider
		Slider.prototype.setPriceScale = function (newMin,newMax){
			this.minValue = newMin;
			this.maxValue = newMax;
		   // var minSlide = document.getElementById("caratMinSlide");
		    //var maxSlide = document.getElementById("caratMaxSlide");
			if(this.decimal==0){
			  for(i=0; i<=193; i++){
				var  myPrice; 
				if(i==0){
					myPrice = this.minValue;
					if(price.currentMin==0){
						//this.minBox.value = formatCurrency(this.minValue);
						this.minBox.value = this.minValue;
					}
				}
				if(i==193){
					myPrice = this.maxValue;
					if(price.currentMax==215){
						//this.maxBox.value = formatCurrency(this.maxValue);
						this.maxBox.value = this.maxValue;
					}
				} else {
					myPrice = Math.floor(Math.pow((this.maxValue/this.minValue),(i/193)) * this.minValue);
				}
				
			  myPrice = formatCurrency(myPrice);	
			  this.priceScale[i] = myPrice;
			  }
			} else if(this.decimal==1){
				for(i=0; i<=193; i++){
				var  myPrice; 
				if(i==0){
					myPrice = this.minValue;
					if(price.currentMin==0){
						//this.minBox.value = formatCurrency(this.minValue);
						this.minBox.value = this.minValue;
					}
				}
				if(i==193){
					myPrice = this.maxValue;
					if(price.currentMax==215){
						//this.maxBox.value = formatCurrency(this.maxValue);
						this.maxBox.value = this.maxValue;
					}
				} else {
					 var multiplier=(this.maxValue-this.minValue)/193;
			         var val1 = i*multiplier;
			         var val2 = Math.round(val1*10)/10;
	                 var myPrice = this.minValue + val2;
	                 myPrice = Math.min(this.presMax,myPrice);
	                 myPrice = Math.max(this.minValue,myPrice);
				}	
			  this.priceScale[i] = myPrice;
			  }
			} else if(this.decimal==2){
				for(i=0; i<=193; i++){
				var  myPrice; 
				if(i==0){
					myPrice = this.minValue;
					if(price.currentMin==0){
						//this.minBox.value = formatCurrency(this.minValue);
						this.minBox.value = this.minValue;
					}
				}
				if(i==193){
					myPrice = this.maxValue;
					if(price.currentMax==215){
						//this.maxBox.value = formatCurrency(this.maxValue);
						this.maxBox.value = this.maxValue;
					}
				} else {
					 var multiplier=(this.maxValue-this.minValue)/193;
					 var val1 = i*multiplier;
					 var val2 = Math.round(val1*100)/100;
					 var myPrice = Math.round((this.minValue + val2)*100)/100;  
					 myPrice = Math.min(this.presMax,clacValue);
					 myPrice = Math.max(this.minValue,clacValue);
				 }	
			  this.priceScale[i] = myPrice;
			  }
			}
		};
		//*********************************************************************************
	    //method to figure values for the min slider
		Slider.prototype.setSlideValueMin = function (iValue){ 
		switch (this.decimal) {
		  case 0: 
		    clacValue = this.priceScale[iValue];
			if (iValue == 0){ clacValue = this.priceScale[0]};
		  break;
		  case 1: 
           clacValue = this.priceScale[iValue];
			if (iValue == 0){ clacValue = this.priceScale[0]};
		  break;
		  case 2: 
            clacValue = this.priceScale[iValue];
			if (iValue == 0){ clacValue = this.priceScale[0]};
		  break;
		}
		  return clacValue;
		};
		
		//method to figure values for the max slider
		Slider.prototype.setSlideValueMax = function (iValue){
		  switch (this.decimal) {
		    case 0: 	
		      iValue = iValue-22;
	          clacValue = this.priceScale[iValue];
			  if (iValue == 193){ clacValue = this.priceScale[193]};
		    break;
			case 1:  
			  iValue = iValue-22;
	          clacValue = this.priceScale[iValue];
			  if (iValue == 193){ clacValue = this.priceScale[193]};
		    break;
		    case 2: 
			  iValue = iValue-22;
	          clacValue = this.priceScale[iValue];
			  if (iValue == 193){ clacValue = this.priceScale[193]};
		    break;
		  }
		  
		  return clacValue;
		  
		 // return this.maxValue - (((iValue-21)/193)*(this.maxValue-this.minValue));
		};
	  
	  
		
		Slider._initialized = true;
	}
}
// End Slider Class



function toggleButtonOn(btn,dat){

	  dat.addValue(btn.lastChild.nodeValue);
	  btn.style.backgroundPosition = "bottom left";
	  btn.onclick=function(){toggleButtonOff(this,dat);return false;};
	  
	  var idCheck = btn.parentNode.id;
	  var idFlag = false;
	  
	  if(btn.parentNode.id=="liShape01"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape02"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape03"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape04"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape05"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape06"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape07"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape08"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape09"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape10"){
		  idFlag = true;
	  } 
	  
	  
	  if(idFlag==true){
	  var shapeVal = 0;
	  for(i=0;i<myData.shape.aValues.length;i++){
		 if(myData.shape.aValues[i]=="Round" || myData.shape.aValues[i]=="Princess" ){
			 shapeVal++;
		 } 
	  }
	  if (shapeVal==0 && myData.shape.aValues.length>0){
		  //alert("Fade");
		  //alert("ShapeVal is " + shapeVal);
		  //alert("myData.shape.aValues.length is " + myData.shape.aValues.length);
		  
		  objCut = document.getElementById("liCut01");
		  if(objCut !=null){
		    aObj = objCut.getElementsByTagName("a");
		    aObj[0].onclick=null;
		    aObj[0].onclick=function(){return false;};
		    objCut.id="liCut01Temp";
		  }
		  for(i=0;i<myData.cut.aValues.length;i++){
			  
			  if(myData.cut.aValues[i]=="Ideal"){
				  myData.cut.aValues.splice(i, 1);
			  }
			  
		  }
	     
			  

		  
	  } else if(shapeVal>0 || myData.shape.aValues.length==0){
		  //alert("Show");
		  //alert("ShapeVal is " + shapeVal);
		  //alert("myData.shape.aValues.length is " + myData.shape.aValues.length);
		 objCut = document.getElementById("liCut01Temp");
		 if(objCut != null){
		   aObj = objCut.getElementsByTagName("a");
		   aObj[0].onclick=null;
		   aObj[0].onclick=function(){toggleButtonOn(this,dat);return false;};
		   aObj[0].style.backgroundPosition = "";
		   objCut.id="liCut01"; 
		 }
	  }
	  objCut=null;
	  shapeVal=null;
	  idFlag =null;
	  }
	  
	  if (do_ajax > 0) {
	  	pagenbr=1;
	  	ajaxCall();
	  }
}
function toggleButtonOff(btn,dat){
	  dat.removeValue(btn.lastChild.nodeValue);
	  btn.style.backgroundPosition = "";
	  btn.onclick=function(){toggleButtonOn(this,dat);return false;};
	  
	 var idCheck = btn.parentNode.id;
	  var idFlag = false;
	  
	  if(btn.parentNode.id=="liShape01"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape02"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape03"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape04"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape05"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape06"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape07"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape08"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape09"){
		  idFlag = true;
	  } else if(btn.parentNode.id=="liShape10"){
		  idFlag = true;
	  } 
	  
	  
	  if(idFlag==true){
	  var shapeVal = 0;
	  for(i=0;i<myData.shape.aValues.length;i++){
		 if(myData.shape.aValues[i]=="Round" || myData.shape.aValues[i]=="Princess" ){
			 shapeVal++;
		 } 
	  }
	  if (shapeVal==0 && myData.shape.aValues.length>0){
		  //alert("Fade");
		  //alert("ShapeVal is " + shapeVal);
		  //alert("myData.shape.aValues.length is " + myData.shape.aValues.length);
		  
		  objCut = document.getElementById("liCut01");
		  if(objCut !=null){
		    aObj = objCut.getElementsByTagName("a");
		    aObj[0].onclick=null;
		    aObj[0].onclick=function(){return false;};
		    objCut.id="liCut01Temp";
		  }
		  for(i=0;i<myData.cut.aValues.length;i++){
			  
			  if(myData.cut.aValues[i]=="Ideal"){
				  myData.cut.aValues.splice(i, 1);
			  }
			  
		  }
		 
	  } else if(shapeVal>0 || myData.shape.aValues.length==0){
		  //alert("Show");
		  //alert("ShapeVal is " + shapeVal);
		  //alert("myData.shape.aValues.length is " + myData.shape.aValues.length);
		 objCut = document.getElementById("liCut01Temp");
		 if(objCut != null){
		   aObj = objCut.getElementsByTagName("a");
		   aObj[0].onclick=null;
		   aObj[0].onclick=function(){toggleButtonOn(this,dat);return false;};
		   aObj[0].style.backgroundPosition = "";
		   objCut.id="liCut01"; 
		 }
	  }
	  objCut=null;
	  shapeVal=null;
	  idFlag =null;
	  }
	  
	  if (do_ajax > 0) {
	  	pagenbr=1;
	  	ajaxCall();
	  }
	  
}

function setButtons(dat,obj){
  if(dat.aValues.length>0){
    for (i=0; i<obj.childNodes.length;i++){
	  if (obj.childNodes[i].tagName){
	    if (obj.childNodes[i].tagName=="LI"){
		  for (j=0;j<obj.childNodes[i].childNodes.length;j++){
		    if(obj.childNodes[i].childNodes[j].tagName){
			  if(obj.childNodes[i].childNodes[j].tagName == "A"){
			    for(k=0;k<dat.aValues.length;k++){
				  if(dat.aValues[k]==obj.childNodes[i].childNodes[j].lastChild.nodeValue){
					btn=obj.childNodes[i].childNodes[j];
					btn.style.backgroundPosition = "bottom left";
	  				btn.onclick=function(){toggleButtonOff(this,dat);return false;};
				  }
                }
			  }
			}
		  }
		}
      }		
	}
  }
}

function prevPage(curpage) {
	pagenbr--;
	ajaxCall();
	return false;
}

function mynbrfmt(expr, decplaces) {
	var mystr= "" + Math.round(eval(expr) * Math.pow(10,decplaces))
	while (mystr.length <= decplaces) {
		mystr = "0" + mystr
	}
	var decpoint= mystr.length - decplaces
	return mystr.substring(0,decpoint) + "." + mystr.substring(decpoint,mystr.length)
}
function nextPage(curpage) {
	pagenbr++;
	ajaxCall();
	return false;
}
function gotoPage(newpage) {
//alert(newpage);
	pagenbr=newpage;
	ajaxCall();
	return false;
}

function viewdmd(lotnum) {
	//alert(lotnum);
	location.href="http://www.firenzejewels.com/loose-certified-diamonds/diamond-detail.html?lot=" + lotnum;
	return true;
}

function ajaxCall(){
	
	str = 
	"Page number      " + pagenbr + "\n" +
	"Advflag:         " + advflag + "\n" +
	"Order:           " + order   + "\n" +
	"Order_dir:           " + order_dir   + "\n" +
	"Shape values:    " + myData.shape.aValues.join(',') + "\n" +
	"Carat minimum:   " + carat.presMin + "\n" + 
	"Carat maximum:   " + carat.presMax + "\n" +
	"Color Grade:     " + myData.color.aValues.join(',') + "\n" +
	"Clarity:         " + myData.clarity.aValues.join(',') + "\n" +
	"Cut Grade:       " + myData.cut.aValues.join(',') + "\n" +
	"Price minimum:   " + price.presMin + "\n" ;
	if (dmdtype=='W') {
	str = str +
	"Price maximum:   " + price.presMax + "\n" +
	"Polish:          " + myData.polish.aValues.join(',') + "\n" +
	"Symmetry:        " + myData.symmetry.aValues.join(',') + "\n" +
	"Fluorescence:    " + myData.fluorescence.aValues.join(',') + "\n" +
	"Depth minimum:   " + depth.presMin + "\n" + 
	"Depth maximum:   " + depth.presMin + "\n" +
	"Table minimum:   " + table.presMin + "\n" + 
	"Table maximum:   "+ table.presMin + "\n";
	} else {
	str = str + "Intensity:       " + myData.intensity.aValues.join(',') + "\n" +
	"Price maximum:   " + price.presMax + "\n" ;
	}
	//alert(str);
	var srch_parms= new create_parms();
	//alert(srch_parms);
	start_progressbar();
	var formvars=new Array();
	formvars['dmdSearch']=JSON.stringify(srch_parms);
	url="http://www.firenzejewels.com/diamondsearchAjax.cfm";
	HTTP.post(url,formvars,parseResponse,handleError);
	return true;
}

function create_parms(srchtype) {
	this.act='init';
	this.shape=myData.shape.aValues.join(',');
	this.cutgrade=myData.cut.aValues.join(',');
	this.clarity=myData.clarity.aValues.join(',');
	this.caratMin=carat.presMin;
	this.caratMax=carat.presMax;
	this.priceMin=stripCurrency(price.presMin);
	this.priceMax=stripCurrency(price.presMax);
	this.advflag=advflag;
	if (dmdtype =='W') {
		this.color=myData.color.aValues.join(',');
		this.depthMin=depth.presMin;
		this.depthMax=depth.presMax;
		this.tableMin=table.presMin;
		this.tableMax=table.presMax;
		this.polish=myData.polish.aValues.join(',');
		this.symmetry=myData.symmetry.aValues.join(',');
		this.fluoresence=myData.fluorescence.aValues.join(',');
		this.intensity='';
	} else {
		tcp=document.getElementById("selectFancyColor");
		this.color=tcp[tcp.selectedIndex].value;
		this.depthMin=1;
		this.depthMax=99;
		this.tableMin=1;
		this.tableMax=99;
		this.polish='';
		this.symmetry='';
		this.fluoresence='';
		this.intensity=myData.intensity.aValues.join(',');
	}
	this.pagenbr=pagenbr;
	this.order=order;
	this.order_dir=order_dir;
	this.dmdtype=dmdtype;
}

function parseResponse(rsp) {
	if (rsp) {
		if (rsp.AJAXDEBUG != '') showDebug('<h3>AJAX Debug:</h3>' + rsp.AJAXDEBUG,1,1,300,'divAJAXDebug');
		if (rsp.ERROR == '') {
			stop_progressbar();
			setcol(order,order_dir);
			var tblptr = $('tabDiamondSearch');
			var tbodyptr = $('tabDSBody');
			if (tblptr) {
				var tblBody = document.createElement("tbody");
				tblBody.setAttribute("id", "tabDSBody");
				var tblrows = rsp.RESULTROWS;
				if (tblrows > 0) {
		        // creating all cells
			        for (var j = 0; j < tblrows; j++) {
			            // creates a table row
			            var row = document.createElement("tr");
			
						var rowdata=rsp.HTML[j];
						row.onclick=new Function("viewdmd("+rowdata.LOT_NUMBER+")");
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.SHAPE);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(mynbrfmt(rowdata.WEIGHT,2));
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.CUT);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.COLOR);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						if (rsp.SRCHTYPE != 'W') {
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.INTENSITY);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						}
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.CLARITY);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.POLISH);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.SYMMETRY);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						var tcell=document.createElement("td");
						var tcellText=document.createTextNode(rowdata.LAB);
						tcell.appendChild(tcellText);
						row.appendChild(tcell);
						//alert(rsp.SRCHTYPE);
						if (rsp.SRCHTYPE == 'W') {
							var tcell=document.createElement("td");
							var tcellText=document.createTextNode(rowdata.DEPTH);
							tcell.appendChild(tcellText);
							row.appendChild(tcell);
							var tcell=document.createElement("td");
							var tcellText=document.createTextNode(rowdata.TABLE);
							tcell.appendChild(tcellText);
							row.appendChild(tcell);
							var tcell=document.createElement("td");
							var tcellText=document.createTextNode(rowdata.FLUORESENCE);
							tcell.appendChild(tcellText);
							row.appendChild(tcell);
						}
							var tcell=document.createElement("td");
							var tcellText=document.createTextNode(rowdata.PRICE);
							tcell.appendChild(tcellText);
						row.appendChild(tcell);
			
			            // add the row to the end of the table body
			            tblBody.appendChild(row);
			        }
				} else {
					 var row = document.createElement("tr");
					 var tcell=document.createElement("td");
					 var tcellText=document.createTextNode("No matches found");
					 tcell.appendChild(tcellText);
					 tcell.setAttribute("colspan",12)>
					 row.appendChild(tcell);
			         tblBody.appendChild(row);
				}
				//replace the existing tbody
				tblptr.replaceChild(tblBody,tbodyptr);

				stripe();
			}
			// now handle paging- first rip out existing stuff
			var pgTop = $('topPgBar');
			if (!pgTop) return false;
			removeChildren(pgTop);
			var pgBot = $('botPgBar');
			if (!pgBot) return false;
			removeChildren(pgBot);
			if (tblrows >0) {
				// put in Back link?
				if (pagenbr > 1) {
					//alert('add Previous');
					tnode=document.createElement("li");
					tnode2=document.createElement("a");
					tnode3=document.createTextNode("Previous");
					tnode2.setAttribute('href','#');
					tnode2.onclick= function() { prevPage(pagenbr); return false; };
					tnode2.appendChild(tnode3);
					tnode.appendChild(tnode2);
					tnode0=tnode.cloneNode(true);
					pgTop.appendChild(tnode);
					tnode0.lastChild.onclick=function() { prevPage(pagenbr); return false; };
					pgBot.appendChild(tnode0);
				}
				//put in number 1?
				if (pagenbr >= 4) {
					//alert('add 1');
					tnode=document.createElement("li");
					tnode2=document.createElement("a");
					tnode3=document.createTextNode("1");
					tnode2.setAttribute('href','#');
					tnode2.onclick=function(){ gotoPage(1); return false; };
					tnode2.appendChild(tnode3);
					tnode.appendChild(tnode2);
					tnode0=tnode.cloneNode(true);
					pgTop.appendChild(tnode);
					tnode0.lastChild.onclick=function(){ gotoPage(1); return false; };
					pgBot.appendChild(tnode0);
				}
				//put in ellipsis?
				if (pagenbr > 4) {
					//alert('add ellipse');
					tnode=document.createElement("li");
					tnode3=document.createTextNode("\u2026 ");
					tnode.appendChild(tnode3);
					tnode0=tnode.cloneNode(true);
					pgTop.appendChild(tnode);
					pgBot.appendChild(tnode0);
				}
				startpg=pagenbr-2;
				stoppg=pagenbr+2;
				if (advflag ==1) rowsperpage=30; else rowsperpage=20;
				//alert('rsp.recordcount is:' + rsp.RECORDCOUNT);
				totalpgs= rsp.RECORDCOUNT / rowsperpage;
				//alert('totalpgs is:' + totalpgs);
				totalpgs=Math.floor(totalpgs);
				if ((rsp.RECORDCOUNT % rowsperpage) > 0) totalpgs++;
				for (var ctr=startpg; ctr <=stoppg; ctr++) {
					//alert('top of loop: ctr= ' + ctr);
					if (ctr == pagenbr) {
						//alert('is page');
						tnode=document.createElement("li");
						//tnode2=document.createElement("a");
						//tnode2.setAttribute('href','#');
						tnode3=document.createTextNode(ctr);
//						tnode.setAttribute('class','liSelected'); 
						tnode.className='liSelected'; 
						//tnode2.appendChild(tnode3);
						//tnode.appendChild(tnode2);
						tnode.appendChild(tnode3);
						tnode0=tnode.cloneNode(true);
						pgTop.appendChild(tnode);
						pgBot.appendChild(tnode0);
					} else {
						if ((ctr > 0) && (ctr <= totalpgs)) {
						//alert('add to list');
							tnode=document.createElement("li");
							tnode2=document.createElement("a");
							tnode3=document.createTextNode(ctr);
							tnode2.setAttribute('href','#');
							//alert(ctr);
							tnode2.onclick=function(){ gotoPage(parseInt(this.lastChild.nodeValue)); return false; };
							tnode2.appendChild(tnode3);
							tnode.appendChild(tnode2);
							tnode0=tnode.cloneNode(true);
							pgBot.appendChild(tnode);
							tnode0.lastChild.onclick=function(){ gotoPage(parseInt(this.lastChild.nodeValue)); return false; };
							pgTop.appendChild(tnode0);
						}
					}
				}
				// Ellipsis again?
				tnbr=totalpgs-3;
				if (tnbr > pagenbr) {
					//alert('another ellipsis');
					tnode=document.createElement("li");
					tnode3=document.createTextNode("\u2026 ");
					tnode.appendChild(tnode3);
					tnode0=tnode.cloneNode(true);
					pgTop.appendChild(tnode);
					pgBot.appendChild(tnode0);
				}
				// Max number?
				tnbr++;
				if (tnbr > pagenbr) {
					tnode=document.createElement("li");
					tnode2=document.createElement("a");
					tnode3=document.createTextNode(totalpgs);
					tnode2.setAttribute('href','#');
					tnode2.onclick=function(){ gotoPage(totalpgs); return false; }
					tnode2.appendChild(tnode3);
					tnode.appendChild(tnode2);
					tnode0=tnode.cloneNode(true);
					pgTop.appendChild(tnode);
					tnode0.lastChild.onclick=function(){ gotoPage(totalpgs); return false; }
					pgBot.appendChild(tnode0);
				}
				//Next?
				if (pagenbr < totalpgs) {
					tnode=document.createElement("li");
					tnode2=document.createElement("a");
					tnode3=document.createTextNode("Next");
					tnode2.setAttribute('href','#');
					tnode2.onclick=function(){ nextPage(pagenbr); return false; }
					tnode2.appendChild(tnode3);
					tnode.appendChild(tnode2);
					tnode0=tnode.cloneNode(true);
					pgTop.appendChild(tnode);
					//tnode0.lastChild.onclick=function(){ gotoPage(totalpgs); return false; }
					tnode0.onclick=function(){ nextPage(pagenbr); return false; }
					pgBot.appendChild(tnode0);
				}
			} else {
					tnode=document.createElement("li");
					tnode3=document.createTextNode("\u00A0 ");
					tnode.appendChild(tnode3);
					tnode0=tnode.cloneNode(true);
					pgTop.appendChild(tnode);
					pgBot.appendChild(tnode0);
			}
		} else {
			if (rsp.error) alert('/' + rsp.error + '/');
			else alert('|' + rsp.ERROR + '|');
		}
	} else {
		alert('no response received');
	}
}

function handleError(rsp) {
	if (rsp.AJAXDebug) showDebug('<h3>AJAX Debug:</h3>' + rsp.AJAXDebug,1,1,300,'divAJAXDebug');
	alert(rsp);
}

function start_progressbar() {

}

function stop_progressbar() {

}

function do_sort(colname) {
	var useid=colname + 'col';
	//alert(useid);
	var tptr=document.getElementById(useid);
	//alert(tptr.className);
	if ((tptr.className == "selectedUp") || (tptr.className == "selectedDown")) {
	//alert('change sort direction only');
		order_dir= 1 - order_dir;
	} else { //alert('//reset sort, new column');
		order=colname;
		order_dir=1;
		pagenbr=1;
	}
	ajaxCall();
}


function setcol(sortcol,sortdir) {
	var tptr=document.getElementById('reshdrrow');
	
	var tchild=tptr.firstChild;
	while (tchild) {
		tchild.className=null;
		tchild=tchild.nextSibling;
	}
	if (sortdir == 1) var useclass="selectedUp"; else useclass="selectedDown";
	var useid=sortcol + 'col';
	//alert(useid);
	var tptr2=document.getElementById(useid);
	tptr2.className=useclass;
}

function showDebug(txt,xcoord,ycoord,ht,divName) {
	var divObj = $(divName);
	if (divObj) {
		removeChildren(divObj);
		document.body.removeChild(divObj);
	}
	divNew = document.createElement('div');
	divNew.setAttribute('id',divName); 
	divNew.style.visibility = "visible";
	divNew.style.width = '300px';
	divNew.style.height = ht + 'px';
	divNew.style.position = 'absolute';
	divNew.style.fontSize = '10px';
	divNew.style.borderWidth = "1px";
	divNew.style.backgroundColor = "#ffffcc";
	divNew.style.left  = xcoord + "px";
	divNew.style.top   = ycoord + "px";
	divNew.style.overflow = 'scroll';
	divNew.innerHTML = txt;
	document.body.appendChild(divNew);
}

function show_props(obj, obj_name) {
   var result = ""
   for (var i in obj)
      result += obj_name + "." + i + " = " + obj[i] + "\n"
   return result
}

function formatCurrency(num) {
  num = num.toString().replace(/\$|\,/g,'');
  if(isNaN(num))
    num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
  if(cents<10)
    cents = "0" + cents;
  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3))+','+
    num.substring(num.length-(4*i+3));
  return (((sign)?'':'-') + '$' + num);
}

function stripCurrency(myStr){
	
	myStr = myStr.toString();
	myStr = myStr.replace(/\$/g,"");
	myStr = myStr.replace(/,/g,"");
	num = parseInt(myStr);
	return num;
}

function minValueUpDate(slider){
 switch (slider.decimal){
 case 0: 
 
   var val1= stripCurrency(slider.minBox.value);
   if(val1<stripCurrency(slider.minValue)){
     val1=stripCurrency(slider.minValue);
	}
	if(val1>stripCurrency(slider.presMax)){
	 val1=stripCurrency(slider.presMax);	
	 }
   var val2= stripCurrency(slider.maxBox.value);
   if(typeof val1 != 'number' || val1==0 || isNaN(val1) == true){
     val1 = stripCurrency(slider.minValue);
   }
   slider.presMin = formatCurrency(val1);
   slider.resetSliders(parseInt(val1),parseInt(val2));
   slider.minBox.value=formatCurrency(val1);	   
 break;
 case 1: 
	var val1= parseFloat(slider.minBox.value);
	if(val1<slider.minValue){
     val1=parseFloat(slider.minValue);
	}
	if(val1>slider.presMax){
	 val1=parseFloat(slider.presMax);
	 }
    var val2= slider.maxBox.value;
	if(typeof val1 != 'number' || val1==0 || isNaN(val1) == true){
     val1 = slider.minValue;
   }
    val1 = Math.round(val1*10)/10;
	slider.presMin = val1;
	
    slider.resetSliders(val1,val2);	   
 break;
 case 2: 
	var val1= parseFloat(slider.minBox.value);
	if(val1<slider.minValue){
     val1=parseFloat(slider.minValue);
	}
	if(val1>slider.presMax){
	 val1=parseFloat(slider.presMax);
	 }
    var val2= slider.maxBox.value;
	if(typeof val1 != 'number' || val1==0 || isNaN(val1) == true){
		alert("hello");
     val1 = slider.minValue;
   }
    val1 = Math.round(val1*100)/100;
	slider.presMin = val1;
    slider.resetSliders(val1,val2);	   
 break;   
 }
 if (do_ajax > 0) {
	  	pagenbr=1;
	  	ajaxCall();
	  }
}

function maxValueUpDate(slider){
  switch (slider.decimal){
 case 0: 
   var val1= stripCurrency(slider.minBox.value); 
   var val2= stripCurrency(slider.maxBox.value);
   if(val2>stripCurrency(slider.maxValue)){
     val2=stripCurrency(slider.maxValue);
	}
	if(val2<stripCurrency(slider.presMin)){
	
	 val2=stripCurrency(slider.presMin);
	 }
  if(typeof val2 != 'number' || val2==0 || isNaN(val2) == true){
     val2 = stripCurrency(slider.maxValue);
   }
   slider.resetSliders(parseInt(val1),parseInt(val2));
   slider.maxBox.value=formatCurrency(val2);	   
 break;
 case 1: 
	var val2= parseFloat(slider.maxBox.value);
    var val1= slider.minBox.value;
	if(val2>slider.maxValue){
     val2=parseFloat(slider.maxValue);
	}
	if(val2<slider.presMin){
	 val2=parseFloat(slider.presMin);
	 }
	 if(typeof val2 != 'number' || val2==0 || isNaN(val2) == true){
     val2 = slider.maxValue;
   }
    val2 = Math.round(val2*10)/10;
	slider.presMax = val2;
    slider.resetSliders(val1,val2);	   
 break;
 case 2: 
	var val2= parseFloat(slider.maxBox.value);
    var val1= slider.minBox.value;
    
	if(val2>slider.maxValue){
     val2=parseFloat(slider.maxValue);
	}
	if(val2<slider.presMin){
	 val2=parseFloat(slider.presMin);
	 }
	 if(typeof val2 != 'number' || val2==0 || isNaN(val2) == true){
     val2 = slider.maxValue;
   }
    val2 = Math.round(val2*100)/100;
    slider.presMax = val2;
    slider.resetSliders(val1,val2);	   
 break;   
 }
 if (do_ajax > 0) {
	  	pagenbr=1;
	  	ajaxCall();
	  }
}

function searchKeyPress(e){
	var key;
         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox
        if (key == 13){				
			this.blur();
			return false;
        } 
		
}
