/*
    Version Control
    $URL: http://svn.quru/bonhams/Progress/Release/www/pubjs/public.js $
    Last Changed: $Date: 2011-07-13 11:13:03 +0100 (Wed, 13 Jul 2011) $ $Rev: 7021 $ by $Author: trevor $
*/

// Bug fix allowing MooTools 1.3 to live with other JS libs (MooTools.Core lighthouse ticket #1070).
var Window;

function lookupValueXML(sScreen, sSourceKeys, sSourceNames, sDestNames, sType)
{
var sURL;
var sURLOrig;
var sKeyValue, sKeyValues = "";
var msKeys;
var msKeyValues = new Array();
var msSource, msDest;
var iKey;
var iPos;

   if (sScreen == "") {
      alert("Invalid screen name");
      return;
   }
   if (!sSourceKeys)
      return;

   msKeys = sSourceKeys.split(",");
   msSource = sSourceNames.split(",");
   msDest = sDestNames.split(",");
   
   // Get the key values (what to search by)
   for (iKey = 0; iKey < msKeys.length; iKey++) {
      iPos = msKeys[iKey].indexOf("=");
      if (iPos == -1)
      {
         sKeyValue = getValue("window", msKeys[iKey]);
         
         if (sKeyValue > "" && sKeyValue.substr(0, 1) == "<")
            return;
      }
      else
      {
         sKeyValue = getValue("window", msKeys[iKey].substr(iPos + 1));
      }
      sKeyValues += sKeyValue;

      if ((iKey == 1) && ((sKeyValue == "") || (sKeyValue == "0")))
      {
         // Clear values
         for (iPos = 0; iPos < msDest.length; iPos++) {
            setValue("window", msDest[iPos], "");
         }
         return;
      }
      msKeyValues.push(sKeyValue);
   }

   // Don't search if there's nothing to search by
   if (sKeyValues == "")
      return;

   // Build up the search url
   sURL = document.location.href.substr(0, document.location.href.lastIndexOf("/"));
   sURL = document.location.protocol + "//" + document.location.host + "/cgi-bin/public.sh/pubweb";
   sURL += "/pubxml.r?screen=" + sScreen + "&outputtype=xml2&saction=search&maxrows=10&fieldnames=yes";
   sURLOrig = sURL;
   for (iKey = 0; iKey < msKeys.length; iKey++) {
      // check if field is an integer but value is a character, or if value is blank
      if ((msKeys[iKey].substring(0,1) == "i" && isNaN(msKeyValues[iKey])) || (msKeyValues[iKey] == ""))
          sURL == sURL;
      else 
      {
      	  if (msKeys[iKey].indexOf("=") == -1)
         	  sURL += "&" + msKeys[iKey] + "=" + msKeyValues[iKey];
		  else
         	  sURL += "&" + msKeys[iKey].substr(0, msKeys[iKey].indexOf("=")) + "=" + msKeyValues[iKey];
      }
   }
      
   // if it was not built with any values then return
   if (sURL == sURLOrig)
       return;
   
   // Perform the XML request
   var xmlrequest = new XMLRequest();
   xmlrequest.loadXMLDoc(sURL, msSource, msDest, sType);
}

function XMLRequest ()
{
   var _self = this;
   
   // Track number of current requests across all object instances
   if (typeof XMLRequest.activeRequests == 'undefined')
     XMLRequest.activeRequests = 0;

   XMLRequest.prototype.processReqChange = function (req, msSourceNames, msDestNames, sType)
   {
      var result = null;
      
      // only if req shows "loaded" and status "OK"
      if ((req.readyState == 4) && (req.status == 200))
      {
         var response = req.responseXML.documentElement;
         if (response)
            result = response.getElementsByTagName('Result');
         if (result && result.length > 0)
         {
	        var iResultsLength;
	        if (!sType) 
	        	iResultsLength = 1;
	       	else 
	       		iResultsLength = result.length;
	       	
	       	var sDestValues = new Array();	
	        for (var iResults = 0; iResults < iResultsLength; iResults++)
	        { 	
	         	for (var iField = 0; iField < msSourceNames.length; iField++) 
	            {
	               var msFields = msSourceNames[iField].split(" ");
	               var sValue = "";
	               for (var iField2 = 0; iField2 < msFields.length; iField2++) 
	               {
	               	  var sValue2 = result[iResults].getAttribute(msFields[iField2]);
	                  if (!sValue2) 
	                  {
	                     var field = result[iResults].getElementsByTagName(msFields[iField2]);
	                     if (field && field.length > 0)
	                     {
	                        if (field[0].firstChild)
	                           sValue2 = field[0].firstChild.nodeValue;
	                        else
	                           sValue2 = "";
	                     }
	                     else
	                        sValue2 = "<Not found>";
	                  }
	                  if (iField2 > 0 && sValue > "")
	                     sValue += " ";
	                  sValue += sValue2;
	               }
	               if (sDestValues[iField])
	                   sDestValues[iField] += '||' + sValue;
	               else 
	               	   sDestValues[iField] = sValue;
	               setValue("window", msDestNames[iField], sDestValues[iField]);
	            }
	        }
         }
         else
         {
	         if (response)
	     	 	var message = response.getElementsByTagName("Message");
	  		 if ((message && message.length > 0) && (message[0].firstChild && message[0].firstChild.nodeValue > ""))
	 			setValue("window", msDestNames[0], message[0].firstChild.nodeValue);
	         else
	         {
	            // No 'Result' element
	            for (iField = 0; iField < msDestNames.length; iField++) 
	            {
	               setValue("window", msDestNames[iField], "<Not found>");
                }
             }
         }
      }
      
      if (req.readyState == 4)
        XMLRequest.activeRequests--;
   }
   
   XMLRequest.prototype.loadXMLDoc = function (sURL, msSourceNames, msDestNames, sType) 
   {  
      var req = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
      if (!req)
         return;
      
      XMLRequest.activeRequests++;
      req.onreadystatechange = function() { _self.processReqChange(req, msSourceNames, msDestNames, sType); };
      req.open("GET", sURL, true);
      req.send(null);
   }
}

function getValue(sWindow, sElement) {
var	oElement;

	oElement = getElement(sWindow, sElement);
    if (!oElement)
    	return ""
    
    if (oElement.tagName == "SPAN")
	    return oElement.innerHTML;
	else
		return oElement.value;
}

function getElement(sWindow, sElement)
{
	var el = null;
	
	// Look for cloned pop-up form in preference to standard form
	var popForm = eval(sWindow + ".document.forms['popupForm']");
	if (popForm)
	{
		try       { el = eval(sWindow + ".document.forms['popupForm']." + sElement); }
		catch (e) { el = null; }
		
		if (!el)
			el = findFromId(popForm, sElement);
	}
	// But fall-back to the standard form in either case
	if (!el)
	{
		try       { el = eval(sWindow + ".document.forms[0]." + sElement); }
		catch (e) { el = null; }
		
		if (!el && document.forms[0])
			el = findFromId(eval(sWindow + ".document.forms[0]"), sElement);
	}
	return(el);
}

// Search the children of parentObj for the element with ID id
function findFromId(parentObj, id)
{
	var curnode = null, foundnode = null;
	for (var i = 0; (i < parentObj.childNodes.length) && (foundnode == null); i++)
	{
		curnode = parentObj.childNodes[i];
		if (curnode.id == id)
		{
			foundnode = curnode;
		}
		if ((foundnode == null) && (curnode.childNodes.length > 0))
			foundnode = findFromId(curnode, id);
	}
	return(foundnode);
}

function setValue(sWindow, sElement, sValue, bSuppressEvents) {
var	oElement;

    // Get the element
    oElement = getElement(sWindow, sElement);
    if (!oElement)
    	return false;
    	
   	if (oElement.tagName.toUpperCase() == "SPAN")
    {
	    oElement.innerHTML = sValue;
	    return false;
    }

    // Special case of Index fields populated by the artist details
    if ((sElement.substr(4, 11) == "Class100Seq") && (sValue > "") && (oElement.value > ""))
        return;

    // Set the value (if different)
    if (oElement.type == "checkbox")
    {
        if (oElement.checked && sValue == "yes") //may need to use oElement.value in the future
            return;
        if (!oElement.checked && sValue != "yes")
            return;
        oElement.checked = (sValue == "yes");
     
        //processCheckbox(sWindow, sElement, oElement);
    }
    else
    {
        if (oElement.value == sValue)
            return;
        oElement.setAttribute('value', sValue); // Required by FF, Safari
        oElement.value = sValue;
    }
   
    // Fire any onChange or onBlur (lost focus) actions 
    if (!bSuppressEvents)
	{
	    if (oElement.onchange)
			oElement.onchange();
			
		if (oElement.onblur)
			oElement.onblur();
	}

	return false;
}

function openWindowSize(sName, sURL, sOptions, iLeft, iTop,	iWidth,	iHeight)
{
	if (sURL.indexOf("?") <	0)
		sURL +=	"?";
	else
		sURL +=	"&";

	if (sName != "login")
		sURL +=	"MJ=" +	document.forms[0].MJ.value;

	if (sOptions != "")
		sOptions += ",";

	hWindow	= window.open(sURL,	sName, sOptions	+ 'top=' + iTop	+ ',left=' + iLeft + ',screenX=' + iLeft + ',screenY=' + iTop +	',width=' +	iWidth + ',height='	+ iHeight);
	hWindow.focus();
}

function openWindow(sName, sURL, sOptions)
{
var	iDefWidth  = 800;
var	iDefHeight = 600;

	openWindowSize(sName, sURL, sOptions ? sOptions : 'scrollbars,status,resizable', ((screen.availWidth - iDefWidth) / 2), ((screen.availHeight - iDefHeight) / 3), iDefWidth, iDefHeight);
}

function reloadWindow()
{
}

function gotoScreen(sScreen)
{
	document.forms[0].screen.value = sScreen;
	document.forms[0].submit();
	return;
}

function reloadWindow()
{
	performSearch(document.forms[0].screen.value, 'search');
}

function performSearch(sScreen, sAction)
{
	/*this part is to check if the lot search has suffix and cut the suffix - KL*/
	//works only on live as we are not using forms[0].isalelotnumber on dev.
//	var temp_char;
//	var check;
//	temp_char = document.forms[0].iSalelotnumber.value.charAt(document.forms[0].Crit20.value.length-1);
//	check=temp_char.match(/[a-zA-Z]/);
//	if (check != null)
//		document.forms[0].iSalelotnumber.value=document.forms[0].iSalelotnumber.value.slice(0,document.forms[0].iSalelotnumber.value.length-1);
	//end of update - KL
	document.forms[0].saction.value = sAction;
	if (document.forms[0].screen)
		document.forms[0].screen.value = sScreen;
	document.forms[0].submit();
	return;
}

function submitPage(sAction)
{
	document.forms[0].saction.value = sAction;
	document.forms[0].submit();
	return;
}

function checkKey(o, e)
{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
	if ( charCode == 13 )
	{
		submitPage('search');
	}
	return true;
}

function lotFind(e)
{
var sLotFind;
var sURL;

	if (e)
	{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
		if ( charCode != 13 )
			return true;
	}

	sLotFind = document.forms[0].LotFind.value;
	if (sLotFind == "")
		return true;

	sURL = "http://" + document.location.hostname + document.forms[0].SelfURL.value + "?screen=MySearchResults&saction=search&sFreeText=" + sLotFind;
	if (document.forms[0].MJ.value != "")
		sURL += "&MJ=document.forms[0].MJ.value";
	document.location.href = sURL;
	return false;
}

function SaleQuickLink(e)
{
	var Salenum;
	var sURL;

	if (e)
	{
	var charCode = (navigator.appName == "Netscape") ? e.which : e.keyCode;
		if ( charCode != 13 )
			return true;
	}

	Salenum=document.forms[0].SaleQLink.value;
	if (Salenum == "")
		return true;

	sURL = "http://" + document.location.hostname + document.forms[0].SelfURL.value + "?screen=catalogue&iSaleNo=" + Salenum;
	if (document.forms[0].MJ.value != "")
		sURL += "&MJ=document.forms[0].MJ.value";
	document.location.href = sURL;
	return false;
}


currentDate = new Date();
switch (currentDate.getMonth()) {
 case 0:
  currentMonth="January";
  break;
 case 1:
  currentMonth="February";
  break;
 case 2:
  currentMonth="March";
  break;
 case 3:
  currentMonth="April";
  break;
 case 4:
  currentMonth="May";
  break;
 case 5:
  currentMonth="June";
  break;
 case 6:
  currentMonth="July";
  break;
 case 7:
  currentMonth="August";
  break;
 case 8:
  currentMonth="September";
  break;
 case 9:
  currentMonth="October";
  break;
 case 10:
  currentMonth="November";
  break;
 case 11:
  currentMonth="December";
}
 
currentDateString = currentDate.getDate()+" "+currentMonth+" "+currentDate.getFullYear();
	var isMac = (navigator.userAgent.indexOf("Mac") != -1);
	var isSaf = (navigator.userAgent.indexOf("Safari") != -1);

	isIE=document.all; //for the next function of the image swapping
	/* img1= new Image();   //caching the images to be swapped
	img1.src="/images/salesb.gif";
	img2= new Image(); 
	img2.src="/images/departmentsb.gif";
	img3= new Image(); 
	img3.src="/images/servicesb.gif"; 
	img4= new Image(); 
	img4.src="/images/locationsb.gif"; */

function swap_img(which,to) //simple functions for swapping images -KL
{
	if (isIE) //for IE
		document.all[which].src = to;
	else //for NN
		document.getElementById(which).src = to;
}
function swap_back(which,to)
{
	if (isIE) //for IE
		document.all[which].src = to;
	else //for NN
		document.getElementById(which).src = to;
}

function LogOff() //website logoff
{
var ckyDate = new Date;

	ckyDate.setDate(ckyDate.getDate() -1);
	var path = document.location.pathname.substr(0,document.location.pathname.length-20);
	document.cookie = "MJ=; path=" + path + "; expires=" + ckyDate.toGMTString() + ';';
	document.cookie = "MJ=; path=/; expires=" + ckyDate.toGMTString() + ';';
	window.location.href="http://" + window.location.host + window.location.pathname + "?screen=logoff";
}

function dispalayEmail(address,PosLeft,PosTop)
{
	trimRight=address.indexOf('.com');
    address=address.slice(7,trimRight+4);
	document.getElementById("EmailAddress").innerHTML = address;
    document.getElementById("EmailAddress").style.right=12;
    document.getElementById("EmailAddress").style.top=PosTop+16;
	document.getElementById("EmailAddress").style.visibility = "visible";
}
function hideEmail()
{
	document.getElementById("EmailAddress").style.visibility = "hidden";
}

function getTop(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetTop = 0;

    while (offsetTrail) {
		
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return offsetTop;
}

function getLeft(elemID) {
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return offsetLeft;
}

function goBack()
{
var vi = -1;

   if (document.forms[0].back)
      vi = parseInt(document.forms[0].back.value, 10);

   window.history.go(vi); 
}

//Now we're checking whether the browser has Flash for the banner, UK map and Erez images
	var MM_contentVersion = 5;
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
	if ( plugin ) 
	{
		var words = navigator.plugins["Shockwave Flash"].description.split(" ");
	    for (var i = 0; i < words.length; ++i)
		{
			if (isNaN(parseInt(words[i])))
				continue;
			var MM_PluginVersion = words[i]; 
	    }
		var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
	}
	else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.indexOf("Win") != -1)) 
		{
			document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
			document.write('on error resume next \n');
			document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
			document.write('</SCR' + 'IPT\> \n');
		}

