// Javascipt for page generated by the SelectSku servlet
//
// globals go here
var products = new Array();
var skunotfound;
var wireframeDirURL;

// detect Internet explorer
var isIE=navigator.userAgent.indexOf("MSIE") != -1;

// sku data from Scout, kept in an Array var called products
function Product(catId, sku, desc, size, color, group, key)
{
	this.catId = catId;
	this.sku = sku;
	this.desc = desc;
  this.size = size;
  this.color = color;
  this.group = group;
  this.key = key;
}

// ssform element reference:
//   input box is sku
//   category drop list is sscategorylist
//   product list is ssproductlist

function ssFormSubmit()
{
	if (document.ssform.ssproductlist.selectedIndex == -1)
	{
		alert(skunotfound);
		return false;		// don't submit
	}
	else
	{
		ssDblClickList();
	}
}

var splashing = true;

function ssRemoveSplash()
{
  if (splashing)
  {
    document.getElementById("splash").style.display = "none";
    document.getElementById("results").className = "show";
    document.getElementById("wireframe").className = "show";
    splashing = false;
  }
}

// Same as ssFormSubmit, except that it doesn't return false on skunotfound
// The return false cause a page to display that said "false"!
function ssNext()
{
	if (document.ssform.ssproductlist.selectedIndex == -1)
	{
		alert(skunotfound);
	}
	else
	{
		ssDblClickList();
	}
}

function ssChangeCategory()
{
  ssRemoveSplash();
  
	var catIndex = document.ssform.sscategorylist.value;
	
	document.ssform.ssproductlist.options.length = 0;
	var optionIndex = 0;
  var lastKey = "foo";
  
	for (n = 0; n < products.length; n++)
	{
		if (catIndex == 0 || catIndex == products[n].catId)
		{ 
			var thisSku = products[n].sku;
      var thisKey = products[n].key;
      
			if (thisKey != lastKey)  // prevent duplicates in list
			{
				document.ssform.ssproductlist.options[optionIndex] = new Option(thisSku + " - " + products[n].desc, n);
				++optionIndex;
				lastKey = thisKey;
			}
		}
	}
	document.ssform.ssproductlist.selectedIndex = 0;
	document.ssform.sku.value = "";
  ssClickList();
}

function ssClickList()
{
	var selectedIndex = document.ssform.ssproductlist.selectedIndex;
	var productIndex = document.ssform.ssproductlist.options[selectedIndex].value;
	if (productIndex < 0)
	{ // unselect
		document.ssform.ssproductlist.selectedIndex = -1;
    clearProductInfoDiv();
    clearWireframe();
	}
	else
	{
    document.ssform.productname.value = products[productIndex].sku;
    document.ssform.productgroup.value = products[productIndex].group;
    updateProductInfoDiv(productIndex);
    updateWireframe(products[productIndex].group);
	}
}

function ssDblClickList()
{
	ssClickList();
	if (document.ssform.ssproductlist.selectedIndex != -1)
	{
		document.ssform.submit();
	}
}

function updateWireframe(group)
{
  var firstGrEnd = group.indexOf(',');
  var imgElement = document.getElementById("wireframeImg");
  if(firstGrEnd == -1)
  {
    imgElement.src = wireframeDirURL + "/" + group + ".jpg";
  }
  else
  {
    imgElement.src = wireframeDirURL + "/" + group.substring(0,firstGrEnd) + ".jpg";
  }
}

function clearWireframe()
{
  var imgElement = document.getElementById("wireframeImg");
  imgElement.src = "/pixel.gif";
}

function updateProductInfoDiv(productIndex)
{
  document.getElementById("productSKU").innerHTML = products[productIndex].sku;
  document.getElementById("productDesc").innerHTML = products[productIndex].desc;
  document.getElementById("productSize").innerHTML = products[productIndex].size;
  document.getElementById("productColor").innerHTML = products[productIndex].color;
}

function clearProductInfoDiv()
{
  document.getElementById("productSKU").innerHTML = "";
  document.getElementById("productDesc").innerHTML = "";
  document.getElementById("productSize").innerHTML = "";
  document.getElementById("productColor").innerHTML = "";
}


function ssSkuKeyUp(userString)
{
  if (userString.length == 1)
    return;
  ssRemoveSplash();
    
	document.ssform.ssproductlist.options.length = 0;
 	document.ssform.ssproductlist.selectedIndex = -1;
	var len = userString.length;
	if (len == 0)
	{
  	document.ssform.sscategorylist.selectedIndex = 0;
		ssChangeCategory();		// userString is blank - refresh to "All Products"
	}
	else	// update product list with matching products
	{
		var optionIndex = 0;
		var catIndex = 0;
		var multiCats = false;
		var lastSku = "b0b";
    var lastKey = "foo";
		
		userString = userString.toUpperCase();
		var start = ssGetStartIndex(userString.charAt(0));
		
		for (n = start; n < products.length; n++)
		{
			var thisSku = products[n].sku;
      var thisKey = products[n].key;
      
			if (userString == thisSku.substring(0, len))
			{ 
				if (thisKey == lastKey)  // duplicate in the product list
					continue;

				document.ssform.ssproductlist.options[optionIndex++] = new Option(thisSku + " - " + products[n].desc, n);
				
				if (catIndex == 0)
				{
					catIndex = products[n].catId;
				}
				else if (catIndex != products[n].catId)
				{
					multiCats = true;
				}
				lastSku = thisSku;
        lastKey = thisKey;
			}
			else if (optionIndex > 0)
				break;  // found some, and product list is alphabetical - no need to continue looping
		}
    
    if (optionIndex == 0 && userString.length > 1)
    { 
      // no sku found, search descriptions
      for (n = 0; n < products.length; n++)
      {
        // var thisSku = products[n].sku;
        var thisDesc = products[n].desc.toUpperCase();
        
        if (thisDesc.indexOf(userString) > -1)
        {  
          // if (thisSku == lastSku)  // duplicate in the product list
          //    continue;
          document.ssform.ssproductlist.options[optionIndex++] = new Option(products[n].sku + " - " + products[n].desc, n);       
          if (catIndex == 0)
          {
            catIndex = products[n].catId;
          }
          else if (catIndex != products[n].catId)
          {
            multiCats = true;
          }
          // lastSku = thisSku;
        }
      }   
    }
    
		if (optionIndex == 0 && userString.length > 1)		// no matches found!
		{
			document.ssform.sscategorylist.selectedIndex = 0;
			document.ssform.ssproductlist.options[0] = new Option(skunotfound, -1);
			document.ssform.ssproductlist.selectedIndex = -1;
      clearProductInfoDiv();
      clearWireframe();
      return;
		}
		else if (multiCats == false)		// show the common category
		{
			document.ssform.sscategorylist.selectedIndex = catIndex;
			document.ssform.ssproductlist.selectedIndex = 0;
		}
		else	// show "All Products" as category
		{
			document.ssform.sscategorylist.selectedIndex = 0;
			document.ssform.ssproductlist.selectedIndex = 0;
		}
    
    var productIndex = document.ssform.ssproductlist.options[0].value;
    if (productIndex < 0)
    { // unselect
      document.ssform.ssproductlist.selectedIndex = -1;
      clearWireframe();
    }
    else
    {
      document.ssform.productname.value = products[productIndex].sku;
      document.ssform.productgroup.value = products[productIndex].group;
      updateProductInfoDiv(productIndex);
      updateWireframe(products[productIndex].group);
    }
	}
}

// returns index of first product that starts with char ch
function ssGetStartIndex(ch)
{
	if (products[0].sku.charAt(0) == ch)
	{
		return 0;
	}
	var low = 1;
	var high = products.length - 1;
	var middle;
	while	(low < high)
	{
		middle = Math.round((low + high) / 2);
		var x = products[middle].sku.charAt(0);
		if (ch < x)
		{
			high = middle - 1;
		}
		else if (ch > x)
		{
			low = middle + 1;
		}
		else // ch == x
		{
			while (ch == x)
			{
				--middle;
				x = products[middle].sku.charAt(0);
			}
			return middle + 1;
		}
	}
	return products.length - 1;
}
