/*
    Version Control
    $URL: http://svn.quru/bonhams/Progress/Release/www/pubjs/mySearch.js $
    Last Changed: $Date: 2009-09-25 16:22:34 +0100 (Fri, 25 Sep 2009) $ $Rev: 2193 $ by $Author: lee $
*/
var bNetscape = (navigator.appName == "Netscape") ? true : false;

function mySearch()
{
	// Calculate base currencies if necessary
	dRate = 1.0;
	if (document.forms[0].sCurrencyID.value != "GBP" && msCurrencyIDFrom)
	{
		for (iIndex = 0; iIndex < msCurrencyIDFrom.length; iIndex++)
		{
			if (msCurrencyIDFrom[iIndex][0] == document.forms[0].sCurrencyID.value)
			{
				dRate = parseFloat(msCurrencyIDFrom[iIndex][1]);
				break;
			}
		}
	}
   document.forms[0].dBaseEstLow.value  = parseFloat(document.forms[0].dEstimateHigh.value.replace(/,/g, ""))  * dRate;
	if (document.forms[0].dBaseEstLow.value == "0")
		document.forms[0].dBaseEstLow.value = "";
	document.forms[0].dBaseEstHigh.value = parseFloat(document.forms[0].dEstimateLow.value.replace(/,/g, "")) * dRate;
	if (document.forms[0].dBaseEstHigh.value == "0")
		document.forms[0].dBaseEstHigh.value = "";

	// Perform search
	document.forms[0].screen.value = "MySearchResults";
	document.forms[0].saction.value = "Search";
	document.forms[0].submit();
}

function saveSearch()
{
	document.forms[0].screen.value = "MySearches";
   if (document.forms[0].iClientWantsNo.value > 0)
   	document.forms[0].saction.value = "Update";
   else
      document.forms[0].saction.value = "Add";

	document.forms[0].submit();
}

function saveSearchAs()
{
	document.forms[0].iClientWantsNo.value = "0";
	saveSearch();
}

function removeSearch()
{
   document.forms[0].screen.value= "MySearches";
   document.forms[0].saction.value= "Delete";
   document.forms[0].submit();
}

function clearComboBox(oCombo)
{
var iOption;

	if (oCombo.type.indexOf("select") != 0)
		return;

	for (iOption = oCombo.options.length - 1; iOption > -1; iOption--)
	{
		if (bNetscape)
			oCombo.options[iOption] = null;
		else
			oCombo.options.remove(iOption);
	}
}

function addToCombo(oCombo, sValue, sText)
{
	if (bNetscape)
		oCombo.options[oCombo.options.length] = new Option(sText, sValue, false, false);
	else
	{
		var newElem = document.createElement("OPTION");
		newElem.value = sValue;
		newElem.text = sText;
		oCombo.options.add(newElem);
	}
}

function selectCombo(oCombo, sValue)
{
	for (iItem = 0; iItem < oCombo.options.length; iItem++)
	{
		if (oCombo.options[iItem].value == sValue)
		{
			if (bNetscape)
				window.setTimeout("document.forms[0]." + oCombo.name +  ".options[" + iItem + "].selected = true;", 1);
			else
				oCombo.options[iItem].selected = true;
			break;
		}
	}
}

function changeDeptType(sInitDept, sInitType)
{
var iIndex;
var iItem;
var sValue;
var sText;
var bSelected;
var sDepartment;

	/* Select the initial department */
	if (sInitDept)
	{
		selectCombo(document.forms[0].sDepartment, sInitDept);
		sDepartment = sInitDept;
	}
	else
		sDepartment = document.forms[0].sDepartment.value;

	clearComboBox(document.forms[0].iSaleItemType);
	addToCombo(document.forms[0].iSaleItemType, "", "(Any type)");

	for (iIndex = 0; iIndex < miSaleItemTypes.length; iIndex++)
		if (miSaleItemTypes[iIndex][0] == sDepartment)
			break;

	if (iIndex >= miSaleItemTypes.length)
		return;
	for (iItem = 2; iItem < miSaleItemTypes[iIndex].length; iItem++)
	{
		sValue = miSaleItemTypes[iIndex][iItem][0];
		sText = miSaleItemTypes[iIndex][iItem][1];

		addToCombo(document.forms[0].iSaleItemType, sValue, sText);
	}

	if (!sInitType)
		return;

	selectCombo(document.forms[0].iSaleItemType, sInitType);
}

function changeContinent(sInitCont)
{
var iIndex;
var bSelected;
var sContinent2 = "";
var sRegion = "";

	// If the combo box is blank populate it first
	if (document.forms[0].sContinent2.options.length == 0)
	{
		addToCombo(document.forms[0].sContinent2, "", "(Anywhere)");
		for (iIndex = 0; iIndex < msContinent.length; iIndex++)
		{
			if (sContinent2 != msContinent[iIndex][0])
			{
				sContinent2 = msContinent[iIndex][0];
				addToCombo(document.forms[0].sContinent2, msContinent[iIndex][0], msContinent[iIndex][1]);
			}
		}
	}

	// Select the initial continent
	if (sInitCont)
		selectCombo(document.forms[0].sContinent2, sInitCont);
	else
		sInitCont = document.forms[0].sContinent2.value;

	clearComboBox(document.forms[0].sRegion);
	addToCombo(document.forms[0].sRegion, "", "(Anywhere)");
	clearComboBox(document.forms[0].sDivision);
	addToCombo(document.forms[0].sDivision, "", "(Anywhere)");

	for (iIndex = 0; iIndex < msContinent.length; iIndex++)
	{
		if (msContinent[iIndex][0] == sInitCont && sRegion != msContinent[iIndex][2])
		{
			sRegion = msContinent[iIndex][2];
			addToCombo(document.forms[0].sRegion, msContinent[iIndex][2], msContinent[iIndex][3]);
		}
	}

	for (iIndex = 0; iIndex < msContinent.length; iIndex++)
	{
		if (msContinent[iIndex][0] == sInitCont)
			addToCombo(document.forms[0].sDivision, msContinent[iIndex][4], msContinent[iIndex][5]);
	}
}

function changeRegion(sInitReg, sInitDiv)
{
var iIndex;
var iIndex;
var sValue = "";
var sText = "(Anywhere)";
var bSelected;

	/* Select the initial region */
	if (sInitReg)
		selectCombo(document.forms[0].sRegion, sInitReg);
	else
		sInitReg = document.forms[0].sRegion.value;

	clearComboBox(document.forms[0].sDivision);
	addToCombo(document.forms[0].sDivision, "", "(Anywhere)");

	for (iIndex = 0; iIndex < msContinent.length; iIndex++)
	{
		if (msContinent[iIndex][2] == sInitReg)
			addToCombo(document.forms[0].sDivision, msContinent[iIndex][4], msContinent[iIndex][5]);
	}

	if (!sInitDiv)
		return;

	selectCombo(document.forms[0].sDivision, sInitDiv);
}

function changeCurrency(sCurrencyID)
{
	if (sCurrencyID > "")
		selectCombo(document.forms[0].sCurrencyID, sCurrencyID);
	else
	{
		sLanguage = (navigator.userLanguage ? navigator.userLanguage : "");
		if (sLanguage == "")
			sLanguage = (navigator.language ? navigator.language : navigator.browserLanguage);
		sLanguage = sLanguage.toLowerCase();
		if (sLanguage == "en-us")
			selectCombo(document.forms[0].sCurrencyID, "USD");
		else
			selectCombo(document.forms[0].sCurrencyID, "GBP");
	}
}

function errorMsg(name,ext,cat)
{
  alert("Netscape 6 or Mozilla is needed to install a search plugin");
  f=document.createElement("form");
  f.setAttribute("name","installform");
  f.setAttribute("method","post");
  f.setAttribute("action","http://mycroft.mozdev.org/error.html");
  fe=document.createElement("input");
  fe.setAttribute("type","hidden");
  fe.setAttribute("name","name");
  fe.setAttribute("value",name);
  f.appendChild(fe);
  fe=document.createElement("input");
  fe.setAttribute("type","hidden");
  fe.setAttribute("name","ext");
  fe.setAttribute("value",ext);
  f.appendChild(fe);
  fe=document.createElement("input");
  fe.setAttribute("type","hidden");
  fe.setAttribute("name","cat");
  fe.setAttribute("value",cat);
  f.appendChild(fe);
  document.getElementsByTagName("body")[0].appendChild(f);
  if (document.installform) { 
    document.installform.submit();
  } else {
    location.href="http://mycroft.mozdev.org/error.html"; //hack for DOM-incompatible browsers
  }
}

function addEngine()
{
  name = "bonhams";
  ext = "gif";
  cat = "Auction";

  if ((typeof window.sidebar == "object") && (typeof
  window.sidebar.addSearchEngine == "function"))
  {
    //cat="Web";
    //cat=prompt("In what category should this engine be installed?","Web")
    window.sidebar.addSearchEngine(
      "http://www.bonhams.com/search/"+name+".src",
      "http://www.bonhams.com/search/"+name+"."+ext,
      name,
      cat );
  }
  else
  {
    alert("Netscape 6 or Mozilla is needed to install a search plugin");
  }
}



