var groupArray              = new Array()
var productArray            = new Array()
var productSizesArray       = new Array()
var quoteItemArray          = new Array()
var productSizeCoEArray     = new Array()
var productSizePackageArray = new Array()

var ceArray      = new Array();
var packageArray = new Array();

var coefficientIndex        = 2
var packageIndex            = 3
var LBSCF                   = 4
var NSFindex                = 5

//retrieve browser version
var IE  = (document.all)    ? true : false;
var NS4 = (document.layers) ? true : false;
var NS6 = (!document.all && document.getElementById) ? true : false; 
var blnVersionWarningWritten = false;

//alert("IE = " + IE + "\nNS4 = " + NS4 + "\nNS6 = " + NS6)

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function sortBySize(a, b) {
  if(parseFloat(a) < parseFloat(b)) return -1;
  if(parseFloat(a) > parseFloat(b)) return  1;
  return 0;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function sortByName(a, b) {
  if(a.toLowerCase() < b.toLowerCase()) return -1;
  if(a.toLowerCase() > b.toLowerCase()) return  1;
  return 0;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function getProductUCEs(productID) {
  var uceArray = new Array()
  
  sizeLoop:
  
  for(var i=0; i < productSizesArray.length; i++) {
  	  if(productSizesArray[i] == null) continue sizeLoop;
      if(productSizesArray[i][0] != productID) continue sizeLoop;
      for(var j=0; j < productSizesArray[i][coefficientIndex].length; j++) {
	  	uceArray = addToList(productSizesArray[i][coefficientIndex][j], uceArray)
	  }
  }
  uceArray.sort(sortBySize);
  return uceArray

/*  alert(productID)
  for(i=0;i < uceArray.length; i++) {
  	alert(uceArray[i])
  }
  alert("done")
*/
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function getProductPackages(productID) {
  var packageArray = new Array()
  
  sizeLoop:
  
  for(var i=0; i < productSizesArray.length; i++) {
      if(productSizesArray[i] == null) continue sizeLoop;
      if(productSizesArray[i][0] != productID) continue sizeLoop;
      for(var j=0; j < productSizesArray[i][packageIndex].length; j++) {
	  	packageArray = addToList(productSizesArray[i][packageIndex][j], packageArray)
	  }
  }
  packageArray.sort(sortByName);
  return packageArray

/*  alert(productID)
  for(i=0;i < packageArray.length; i++) {
  	alert(packageArray[i])
  }
  alert("done")
*/
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function makeCEarray() {
  for(var i=0; i < productSizesArray.length; i++) {
  	 sizeLoop:
     for(var j=0; j < productSizesArray[i][coefficientIndex].length; j++) {
	 	 if(productSizesArray[i] == null) continue sizeLoop;
	     ceArray = addToList(productSizesArray[i][coefficientIndex][j], ceArray)
	 }
  }
  //now sort the values in the array by size
  ceArray.sort(sortBySize);
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function makePackageArray() {
  sizeLoop:
  for(var i=0; i < productSizesArray.length; i++) {
  	 if(productSizesArray[i] == null) continue sizeLoop;
     for(var j=0; j < productSizesArray[i][packageIndex].length; j++) {
	     packageArray = addToList(productSizesArray[i][packageIndex][j], packageArray)
	 }
  }
  //now sort the array by name
  packageArray.sort(sortByName);
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function addToList(val, vArray) {
   for(var i=0; i < vArray.length; i++) {
     if(val.toLowerCase() == vArray[i].toLowerCase()) {
	   return vArray;
	 }
   }
   //made it through without returning it is not in the array yet
   var arrayLength = vArray.length;
   vArray[arrayLength] = val
   return vArray;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++
function oldAddToList(val, vArray) {
   for(var i=0; i < vArray.length; i++) {
     if(val.toLowerCase() == vArray[i].toLowerCase()) {
	   return;
	 }
   }
   //made it through without returning it is not in the array yet

   var arrayLength = vArray.length;
   vArray[arrayLength] = val
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function getProducts(groupIndex) {
   var productList = ""
   for(var i=0; i < productArray.length; i++) {
     if(productArray[i][1] == groupIndex) {
	    productList += "\n" + productArray[i][0]
	 }
   }
   alert("Products under the group: " + groupArray[groupIndex] + "\n" + productList)
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function getProductSizes(productIndex) {
   var sizeList = ""
   for(var i=0; i < productSizesArray.length; i++) {
      if(productSizesArray[i][0] == productIndex) {
	    sizeList += "\n" + productSizesArray[i][1]
	  }
   }
   alert("Sizes for product: " + productArray[productIndex][0] + "\n" + sizeList)
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function showValidPackages(productSizeIndex) {
   var packageList = ""
      for(var i=0; i < productSizesArray[productSizeIndex][packageIndex].length; i++) {
		    packageList += "\n" + productSizesArray[productSizeIndex][packageIndex][i]
	  }

   alert("Packaging for product: " + productArray[productSizesArray[productSizeIndex][0]][0] + " : Size : " + productSizesArray[productSizeIndex][1] + "\n" + packageList)
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function showValidCoefficients(productSizeIndex) {
   var ceList = ""
      for(var i=0; i < productSizesArray[productSizeIndex][coefficientIndex].length; i++) {
		    ceList += "\n" + productSizesArray[productSizeIndex][coefficientIndex][i]
	  }

   alert("Coefficients for product: " + productArray[productSizesArray[productSizeIndex][0]][0] + " : Size : " + productSizesArray[productSizeIndex][1] + "\n" + ceList)
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function loadUCEs(productSizeID, selectObj) {
   var currentValue = ""
   var j = 0
      
      //reset list 
      selectObj.options.length = 0;
	  j = 0
	  
	  //add header Item
	  selectObj.options[j] = new Option("Uniformity Coefficient ...", "");
	  selectObj.options[j].selected = true;   
      j++
	  
	  //build select list with values that match   
      for(var i=0; i < productSizesArray[productSizeID][coefficientIndex].length; i++) {
	    currentValue = productSizesArray[productSizeID][coefficientIndex][i]
		selectObj.options[j] = new Option(currentValue, currentValue);	        
		selectObj.options[j].selected = false;
		j++
	  }
	  
	  //if there are only 2 options - the header and 1 record
	  if(selectObj.options.length == 2) {
	     selectObj.options[1].selected = true;
	  }	  
	  
	  //select list has been built -- now show list
      showElement(selectObj)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function loadPackages(productSizeID, selectObj) {
   var currentValue = ""
   var j = 0
      
      //reset list 
      selectObj.options.length = 0;
	  j = 0
	  
	  //add header Item
	  selectObj.options[j] = new Option("Packages ...", "");
	  selectObj.options[j].selected = true;   
      j++
	  
	  //build select list with values that match   
      for(var i=0; i < productSizesArray[productSizeID][packageIndex].length; i++) {
	    currentValue = productSizesArray[productSizeID][packageIndex][i]
		selectObj.options[j] = new Option(currentValue, currentValue);	        
		selectObj.options[j].selected = false;
		j++
	  }
	  
	  //if there are only 2 options - the header and 1 record
	  if(selectObj.options.length == 2) {
	     selectObj.options[1].selected = true;
	  }
	  //select list has been built -- now show list
      showElement(selectObj)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function loadGroups(selectObj) {
   var currentGroup = ""
   var j = 0
      
      //reset list 
      selectObj.options.length = 0;
	  j = 0
	  
	  //add header Item
	  selectObj.options[j] = new Option("Product Groups ...", "");
	  selectObj.options[j].selected = true;   
      j++
	  
	  //build select list with values that match   
      for(var i=0; i < groupArray.length; i++) {
	    currentGroup = groupArray[i]
		selectObj.options[j] = new Option(currentGroup, i);	        
		selectObj.options[j].selected = false;
		j++
	  }
	  //select list has been built -- now show list
      showElement(selectObj)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function loadProducts(groupID, selectObj) {
   var currentProduct = ""
   var j = 0
      
      //reset list 
      selectObj.options.length = 0;
	  j = 0
	  
	  //add header Item
	  selectObj.options[j] = new Option("Products ...", "");
	  selectObj.options[j].selected = true;   
      j++
	  
	  //build select list with values that match   
      for(var i=0; i < productArray.length; i++) {
	    if(productArray[i][1] == groupID) {
	       currentProduct = productArray[i][0]
		   selectObj.options[j] = new Option(currentProduct, i);	        
		   selectObj.options[j].selected = false;
		   j++		
		}
	  }

	  //select list has been built -- now show list
      showElement(selectObj)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function loadProductSizes(productID, selectObj) {
   var currentValue = ""
   var j = 0
      
      //reset list 
      selectObj.options.length = 0;
	  j = 0
	  
	  //add header Item
	  selectObj.options[j] = new Option("Sizes ...", "");
	  selectObj.options[j].selected = true;   
      j++
	  
	  //build select list with values that match   
	  sizeLoop:
      for(var i=0; i < productSizesArray.length; i++) {
	  	if(productSizesArray[i] == null) continue sizeLoop;
	    if(productSizesArray[i][0] == productID) {
	       currentValue = productSizesArray[i][1]
		   selectObj.options[j] = new Option(currentValue, i);	        
		   selectObj.options[j].selected = false;
		   j++		
		}
	  }

	  //select list has been built -- now show list
	  showElement(selectObj)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function group_onChange(selectObj) {
     //hide elements
     hideElement(document.quoteBuilder.product)
     hideElement(document.quoteBuilder.size)
     hideElement(document.quoteBuilder.uce)
     hideElement(document.quoteBuilder.packages)
     hideElement(document.quoteBuilder.lbscf)	 

   if(selectObj.options[selectObj.selectedIndex].value == "") {return;}
   
   //build product list
   loadProducts(selectObj.options[selectObj.selectedIndex].value, document.quoteBuilder.product)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function product_onChange(selectObj) {
     //hide elements
     hideElement(document.quoteBuilder.size)
     hideElement(document.quoteBuilder.uce)
     hideElement(document.quoteBuilder.packages)
     hideElement(document.quoteBuilder.lbscf)	 

   if(selectObj.options[selectObj.selectedIndex].value == "") {return;}
   
   //build product list
   loadProductSizes(selectObj.options[selectObj.selectedIndex].value, document.quoteBuilder.size)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function size_onChange(selectObj) {
     //hide elements
     hideElement(document.quoteBuilder.uce)
     hideElement(document.quoteBuilder.packages)
     hideElement(document.quoteBuilder.lbscf)	 
	 disable(document.quoteBuilder.add)
	 
   if(selectObj.options[selectObj.selectedIndex].value == "") {return;}
   
   //enable add button
   enable(document.quoteBuilder.add) 
   
   //build lists for  uce, package
   loadUCEs(selectObj.options[selectObj.selectedIndex].value, document.quoteBuilder.uce)
   loadPackages(selectObj.options[selectObj.selectedIndex].value, document.quoteBuilder.packages)
   
   document.quoteBuilder.lbscf.value = productSizesArray[selectObj.options[selectObj.selectedIndex].value][LBSCF]
   showElement(document.quoteBuilder.lbscf)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function add_click() {
  var frm = document.quoteBuilder
  //assume that if able to click on add that they have at least selected a size
  
  if(frm.uce.options.length > 1 && frm.uce.selectedIndex == 0) {
    alert("A Uniformity Coefficient must be selected to add quote line")
	return;
  }
  
  if(frm.packages.options.length > 1 && frm.packages.selectedIndex == 0) {
    alert("A Package must be selected to add quote line")
	return;
  }  
  addQuoteLine()
  init()
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function deleteLine(lineIndex) {
  quoteItemArray[lineIndex].deleted = true;
  showQuoteLines()
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function addQuoteLine() {
   var lineIndex = quoteItemArray.length
   var frm = document.quoteBuilder
   var lineHTML = ""
   
   quoteItemArray[lineIndex]          = new quoteLine()
   quoteItemArray[lineIndex].product  = frm.product.options[frm.product.selectedIndex].text
   quoteItemArray[lineIndex].size     = frm.size.options[frm.size.selectedIndex].text
   quoteItemArray[lineIndex].uce      = frm.uce.options[frm.uce.selectedIndex].value
   quoteItemArray[lineIndex].packages = frm.packages.options[frm.packages.selectedIndex].value
   quoteItemArray[lineIndex].lbscf    = frm.lbscf.value
   
   showQuoteLines()
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function showQuoteLines() {
   var lineHTML = ""
   var lineCount = 0
   
   var divObj
   if(IE) {
   		divObj = document.all.quoteLines
   }
   if(NS6) {
   		divObj = document.getElementById("quoteLines");
   }
   if(NS4) {
   		//divObj = document.layers["quoteLines"];
		//do not support NS4 due to dhtml form not being able to submit from within layer/div
		if(blnVersionWarningWritten) {return;}
		document.write("Netscape Versions 4.x are not supported by this application.<BR>Netscape 6.x is supported")
		blnVersionWarningWritten = true;
		return;		
   }
   
//   alert("writing quoteLines")
   
   lineHTML = '<form name="quoteLinesForm" id="quoteLinesForm" method="POST" action="contactInfo.php">' +
              '<table width="80%" border=0>' +
              '<tr>' + 
			  '<td nowrap></td>' + 
			  '<td nowrap class="tableCOLHeader" align=center valign=bottom>Quantity</td>' + 
			  '<td nowrap class="tableCOLHeader" valign=bottom>Product</td>' +
			  '<td nowrap class="tableCOLHeader" valign=bottom>Size</td>' +
			  '<td nowrap class="tableCOLHeader" valign=bottom>Uniformity<BR> Coefficient</td>' +
			  '<td nowrap class="tableCOLHeader" valign=bottom>Package</td>' +
			  '<td nowrap class="tableCOLHeader" valign=bottom>Pounds/c.f.</td>' +
			  '</tr>'
              
			  //loop through quoteItemArray
			  for(var i=0; i < quoteItemArray.length; i++) {
			    if(!quoteItemArray[i].deleted) {
				   lineCount++
				   lineHTML += '<tr>' +
                     '<td nowrap align=right><a href="javascript: void deleteLine(' + i + ');" title="Delete Quote Line" alt="Delete Quote Line"><img src="deleteIcon.gif" border="0"></a>' +
                     '<input type="hidden" name="product_' + i + '" value="' + quoteItemArray[i].product  + '">' + 
					 '<input type="hidden" name="size_'    + i + '" value="' + quoteItemArray[i].size     + '">' + 
					 '<input type="hidden" name="uce_'     + i + '" value="' + quoteItemArray[i].uce      + '">' + 
					 '<input type="hidden" name="package_' + i + '" value="' + quoteItemArray[i].packages + '">' + 
					 '<input type="hidden" name="lbscf_'   + i + '" value="' + quoteItemArray[i].lbscf    + '">' + 					 
					 '</td>' +
			         '<td nowrap class="tableData" align=center><input class="qty" type="text" name="qty_' + i + '" size="4" ' + 
				     ' value="' + quoteItemArray[i].qty + '" onChange="qty_onChange(this, ' + i + ')">' +
					 '<select name="qtyType_' + i + '" size=1 onChange="qtyType_onChange(this, ' + i + ')">' +
					 	'<option value="' + quoteItemArray[i].qtyType + '" SELECTED>' + quoteItemArray[i].qtyType + '</option>'
						if(quoteItemArray[i].qtyType.toLowerCase() == "c.f") {
						   lineHTML += '<option value="Lbs.">Lbs.</option>'
						} else {
						   lineHTML += '<option value="c.f">c.f</option>'
						}
					 lineHTML += '</select>' +
					 '</td>' +
                     '<td nowrap class="tableData">' + quoteItemArray[i].product  + '</td>' +
	                 '<td nowrap class="tableData">' + quoteItemArray[i].size     + '</td>' +
	                 '<td nowrap class="tableData">' + quoteItemArray[i].uce      + '</td>' +
	                 '<td nowrap class="tableData">' + quoteItemArray[i].packages + '</td>' +
	                 '<td nowrap class="tableData">' + quoteItemArray[i].lbscf    + '</td>' +
                     '</tr>'				  
				}
			  }
   lineHTML += "</table>"
   lineHTML += "<BR clear=all>"
 
   if(lineCount > 0) {
     lineHTML += '<input type="submit" value="SEND QUOTE REQUEST" name="requestButton" class="inputButton">'
   }
   lineHTML += '</form>'
   
//   alert(lineHTML)
   
   if(IE || NS6) {
   		divObj.innerHTML = lineHTML
   }
   
   if(NS4) {
   		divObj.document.open();

		divObj.document.write(lineHTML);
		divObj.document.close();
   }
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function submitHiddenForm() {
    //create new objects in this form from saved data
	var html = ""
	var formObj, formLayer

    for(var i=0; i < quoteItemArray.length; i++) {
	    if(!quoteItemArray[i].deleted) {
			html += '<input type="hidden" name="product_' + i + '" value="' + quoteItemArray[i].product  + '">' + 
					'<input type="hidden" name="size_'    + i + '" value="' + quoteItemArray[i].size     + '">' + 
					'<input type="hidden" name="uce_'     + i + '" value="' + quoteItemArray[i].uce      + '">' + 
					'<input type="hidden" name="package_' + i + '" value="' + quoteItemArray[i].packages + '">' + 
					'<input type="hidden" name="lbscf_'   + i + '" value="' + quoteItemArray[i].lbscf    + '">' + 
					'<input type="hidden" name="qty_'     + i + '" value="' + quoteItemArray[i].qty      + '" class="qty" size=4>' 
		}
	}
   
//   html = '<form name="hiddenForm" method="POST" action="contactInfo.php">' + html + '</form>';
   html = '<form name="hiddenForm" method="GET" action="http://www.yahoo.com">' + html + '</form>';
   
   if(IE || NS6) {
   		formLayer = document.getElementById("hiddenFormDIV");
		formLayer.innerHTML = html
		formObj = document.hiddenForm
   }
   if(NS4) {
   		formLayer = window.document.layers["hiddenFormDIV"];
		formLayer.document.open();
		formLayer.document.write(html);
		formLayer.document.close();
		
		formObj = formLayer.document.forms["hiddenForm"]
   }	
	formObj.submit();
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function writeSpaces(numSpaces) {
   var returnStr = ""
   for(var i=0; i < numSpaces; i++) {
     returnStr += "&nbsp;"
   }
   return returnStr;
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function quoteLine() {
  this.product  = ""
  this.size     = ""
  this.uce      = ""
  this.packages = ""
  this.lbscf    = ""
  this.qty      = 1
  this.qtyType  = "c.f"
  this.deleted  = false
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function qtyType_onChange(obj, lineIndex) {
  //obj is selectbox
/*  alert("in qtyType_onChange" +
        "\n quoteItemArray.qtyType before = " + quoteItemArray[lineIndex].qtyType +
		"\n obj.options.value = " + obj.options[obj.selectedIndex].value
       )
*/
  quoteItemArray[lineIndex].qtyType = obj.options[obj.selectedIndex].value
//  alert(" quoteItemArray after = " + quoteItemArray[lineIndex].qtyType)
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function qty_onChange(obj, lineIndex) {
  if(obj.value == "" || isNaN(obj.value)) {obj.value = 1}
  quoteItemArray[lineIndex].qty = obj.value
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function init() {
   //load groups into list
   hideElement(document.quoteBuilder.group)
   loadGroups(document.quoteBuilder.group)
   
   //hide other lists
   hideElement(document.quoteBuilder.product)
   hideElement(document.quoteBuilder.size)
   hideElement(document.quoteBuilder.uce)
   hideElement(document.quoteBuilder.packages)
   hideElement(document.quoteBuilder.lbscf)

   //disable addButton
   disable(document.quoteBuilder.add)
   
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function hideElement(el) {
  if(IE || NS6) {
  	el.style.visibility = "hidden";
  }
  if(NS4) {
    el.visibility = "hide";
  }
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function showElement(el) {
  if(IE || NS6) {
     el.style.visibility = "visible";
  }
  if(NS4) {
     el.visibility = "show";
  }
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function enable(el) {
  if(IE || NS6) {
     el.disabled = false;
  }
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++

function disable(el) {
  if(IE || NS6) {
  	el.disabled = true;
  }
}

//this must go at the end of the page
var FUNCTIONS_LOADED = true

