﻿        
    var m_strOriginalValue = "";
    var m_firstCall = true;
    var error = false;            
    
    function colourChange(strPropertyName, strCallEnt, strManName, strManCode)   //paramters prop: the name of the property to change colour, val: the name of the inputbox holding the colour value
    {
        //function changes the colour of the colour sample next to the text box
        var elemProp = document.getElementById(strPropertyName);
        var elemValue = document.getElementById(strCallEnt);
        
        if (elemProp && elemValue) 
        {
            setValues(strManName, strManCode, strCallEnt)     //call set values to apply the change

                if(elemValue.value != "" && error != true)
                {
                    elemProp.style.background= '#' + elemValue.value; //set the bacground colour of the element to the specified value                
                }
        }
        else 
        {
            alert("Script error in colourChange()");
        }
    }
    
    //sets all of the individual values to the url string and then ressets the iframe to reflect the user changes
    function setValues(strManName, strManCode, strCallEnt) {
      
      var strheight = document.getElementById("inpHeight").value;                                        
      var strwidth =  document.getElementById("inpWidth").value;            
      
      
          if (m_strOriginalValue != document.getElementById(strCallEnt).value)    //check that the actual value has changed
              {
                    var fraDoc = document.getElementById("IFrame");            //get the iframe element
                    var strUrl;                                                // url varaibale for properties string        
                    var fraHeight = document.getElementById("inpHeight");   //holds the height value
                    var fraWidth =  document.getElementById("inpWidth");    //holds the width value
                      
                    if (fraHeight && fraWidth) 
                    { 
                      strUrl="manid="+strManName;             //set the url to hold the manufacturer name   
                      strUrl=strUrl+"&mancode="+strManCode;      //set the url to hold the manufacturer code 
                      strUrl=strUrl+returnValueList();        //set url to hold the return string from the returnValueList function
                     
                        if(check4Error() == false)  
                        {
                            fraWidth = fraWidth.value * 1;    //multiply the frame width variable by 1 to let javacript know we are dealing with numeric values 
                            fraHeight = fraHeight.value * 1;  //multiply the frame height variable by 1 to let javacript know we are dealing with numeric values 
                     
                            fraWidth = fraWidth +=15;         //increment the width by 10 so that the outer iframe is always large enough to display the inner iframe
                            fraHeight = fraHeight +=64;       //increment the height by 10 so that the outer iframe is always large enough to display the inner iframe
                      
                            var xtr = document.getElementById("inpBorder").value;   //get the border width so that a large border isnt hidden
                            //status = "xtr: " + xtr;
                            xtr = xtr * 2;                                          //multiply the value by two to account for both sides of the border
                            fraWidth = fraWidth + xtr;                             //add the border width to the frame width
                            fraHeight = fraHeight + xtr;                           //add the border height to the frame height
                      
                            fraDoc.width = fraWidth;             //set the outer Iframe to the new set width
                            
                            fraDoc.height = fraHeight;           //set the outer iframe to the new set height
                        
                            fraDoc.src = "DisplayProductListings.aspx?"+strUrl;     //set the source to the new url   
                        }
                     }
                     else
                     {
                        alert("Script error in setValues");
                     } 
                 }
      
                  m_strOriginalValue = "";   //reset the original value so that a correct test is performed on the next pass
     }
   
    
    //fucntion resets all of the values on the form to the original default values
    function resetValues(strManName, strManCode)
    {
        m_strOriginalValue = "";                                                        //remove any previous values                                                    
        
        var strheight = document.getElementById("inpHeight");
        var strwidth = document.getElementById("inpWidth");
        var strborder = document.getElementById("inpBorder");
        var strborderValue = document.getElementById("selBorderValue");
        var strborderColour = document.getElementById("inpBorderColour");
        var strcolourDisplay = document.getElementById("divColourDisplay");
        var strborderStyle = document.getElementById("selBorderStyle");
        var strbgColour =  document.getElementById("inpBGColour");
        var strbackColour = document.getElementById("divBackgroundColour"); 
        var strfontFamily = document.getElementById("selFontFamily");
        var strfontSize =  document.getElementById("inpFontSize");
        var strfontSizeValue = document.getElementById("selFontSizeValue");
        var strfontColour = document.getElementById("inpFontColour");
        var strfontColourValue = document.getElementById("divFontColour");
        var strrangeColour = document.getElementById("inpRangeColour");
        var strrangeColourValue = document.getElementById("divRangeColour");
        var strrangeHoverColour = document.getElementById("inpRangeHoverColour");
        var strrangeHoverColourValue = document.getElementById("divRangeHoverColour");
        var strlinkWeight = document.getElementById("selLinkWeight");
        var strlinkColour = document.getElementById("inpLinkColour");
        var strlinkColourValue = document.getElementById("divLinkColour");
        var strlistStyle = document.getElementById("selListStyle");
        var strscrollbar = document.getElementById("selScrollbar");
        //var strSingleValue = document.getElementId("selListingType");
        //var strSingleValueName = document.getElementId("selListOptions");
     
     if(strheight && strwidth && strborder && strborderValue && strborderColour && strcolourDisplay && strborderStyle && strbgColour && strbackColour && strfontFamily && strfontSize && strfontSizeValue && strfontColour && strfontColourValue && strrangeColour && strrangeColourValue && strrangeHoverColour && strrangeHoverColourValue && strlinkWeight && strlinkColour && strlinkColourValue && strlistStyle && strscrollbar)   
     {   
        strheight.value = "280";                                                           //set the default height value
        strwidth.value = "280";                                                            //set the default width value
        strborder.value = "1";                                                             //set the default border value        
        strborderValue.selectedIndex = 0;                                                  //set the default border unit value        
        strborderColour.value = "666666";                                                  //set the default border Colour value
        strcolourDisplay.style.backgroundColor= "666666";                                  //set the default preview Colour square        
        strborderStyle.selectedIndex = 0;                                                  //set the default border style value
        strbgColour.value = "F2F2F2";                                                      //set the default border Colour value
        strbackColour.style.backgroundColor= "f2f2f2";                                     //set the default background Colour value            
        strfontFamily.selectedIndex = 34;                                                  //set the default font family               
        strfontSize.value = "11";                                                          //set the default font size
        strfontSizeValue.selectedIndex = 0;                                                //set the default font size units
        strfontColour.value = "666666";                                                    //set the default font colour 
        strfontColourValue.style.backgroundColor= "666666";                                //set the default preview Colour square     
        strrangeColour.value = "111111";                                                    //set the default font colour 
        strrangeColourValue.style.backgroundColor= "111111";                                //set the default preview Colour square     
        strrangeHoverColour.value = "D3D3D3";                                              //set the default font colour 
        strrangeHoverColourValue.style.backgroundColor= "D3D3D3";
        strlinkWeight.selectedIndex = 0;                                                   //set the default link weight
        strlinkColour.value = "333333";                                                    //set the defualt link colour value 
        strlinkColourValue.style.backgroundColor= "333333";                                //set the default preview Colour square         
        strlistStyle.selectedIndex = 0;                                                    //set the default list style
        strscrollbar.selectedIndex = 1;                                                    //set the default scrollbar
        //strSingleValue.selectedIndex = 0;
        //strSingleValueName.Length = 0;
        //strSingleValueName.options[0] = new Option('N/A','N/A');  
       // strSingleValueName.disabled = true;
        
        setValues(strManName, strManCode, 'inpHeight');                                       //call set values to reflect the changes in the iframe
      }
      else
      {
        alert("Script error in resetValues");
      }
    }


    
    //sets the original value of the passed field before a change is made so it can be tested for later 
  function checkChange(prop)
  {   
   m_strOriginalValue = document.getElementById(prop).value;
  }
  
  
  //sets all of the dropdown boxes to there values passed either from the xml or the default settings
  function setDefault(borderUnits, borderStyle, fontFamily, fontSizeUnits, linkWeight, listStyle, scrollbar, borderColour, BGColour, fontColour, rangeColour, rangeHoverColour, singleValue, singleValueName, manName, manCode, linkColour)
  {
    
   if(m_firstCall)  //check to see if its the first call to the function from the iframe, if it is then we set the defaults passed from the xml or the standard values populated in the OM 
   {
    
    var valBorderValue = document.getElementById("selBorderValue");
    var valBorderStyle =document.getElementById("selBorderStyle");
    var valFontFamily = document.getElementById("selFontFamily");
    var valFontSizeValue = document.getElementById("selFontSizeValue");
    var valLinkWeight = document.getElementById("selLinkWeight");
    var valListStyle = document.getElementById("selListStyle");
    var valScrollbar = document.getElementById("selScrollbar");
    var valColourDisplay = document.getElementById("divColourDisplay");
    var valBackgroundColour = document.getElementById("divBackgroundColour");
    var valFontColour = document.getElementById("divFontColour");
    var valRangeColour = document.getElementById("divRangeColour");
    var valRangeHoverColour = document.getElementById("divRangeHoverColour");
    var valLinkColour = document.getElementById("divLinkColour"); 
    var valSingleValue = document.getElementById("selListingType"); 
    var valSingleValueName = document.getElementById("SelListOptions"); 
    var valRangeColourText = document.getElementById("inpRangeColour");
    var valRangeHoverColourText = document.getElementById("inpRangeHoverColour");
    
    if(valBorderValue && valBorderStyle && valFontFamily && valFontSizeValue && valLinkWeight && valListStyle && valScrollbar && valColourDisplay && valBackgroundColour && valFontColour && valRangeColour && valRangeHoverColour && valLinkColour && valSingleValue && valSingleValueName && valRangeColourText && valRangeHoverColourText)
    {
        valBorderValue.value = borderUnits;                      //set the border units dropdown
        valBorderStyle.value = borderStyle;                      //set the border style dropdown
        valFontFamily.value = fontFamily;                        //set the font family dropdown
        valFontSizeValue.value = fontSizeUnits;                  //set the font sizr units dropdown
        valLinkWeight.value = linkWeight;                        //set the link weight dropdown
        valListStyle.value = listStyle;                          //set the list style dropdown
        valScrollbar.value = scrollbar;                          //set the scrollbar dropdown
        valColourDisplay.style.background = '#' + borderColour;   
        valBackgroundColour.style.background = '#' + BGColour;
        valFontColour.style.background = '#' + fontColour;
        valRangeColour.style.background = '#' + rangeColour;
        valRangeHoverColour.style.background = '#' + rangeHoverColour;
        valLinkColour.style.background = '#' + linkColour;
        valSingleValue.value = singleValue;
        valSingleValueName.value = singleValueName;
    
        // CC 17/02/2009
        // For manufacturers without any range information
        // we remove the range option and disable the range related information
        if(m_arrRangeLabels == null || m_arrRangeLabels.length <= 0)
        {
            valSingleValue.options.remove(2);
            valRangeColourText.disabled = true;
            valRangeHoverColourText.disabled = true;
            valRangeColour.innerHTML = "&nbsp;";
            valRangeColour.style.border = "1px solid #FFFFFF";
            valRangeColour.style.background = "#FFFFFF";
            valRangeHoverColour.innerHTML = "&nbsp;";
            valRangeHoverColour.style.border = "1px solid #FFFFFF";
            valRangeHoverColour.style.background = "#FFFFFF";
        }
    
        this.CheckSelection(m_arrProductValues, m_arrLabelValues, manName, manCode);
        
        m_firstCall = false;    
    }   
   }
  }
  
 function CheckSelection(ArrayValue, LabelValue, strManName, strManCode) {

    var selection = document.getElementById("selListingType");  //Get an instance of the listings type select box
    var listings = document.getElementById("SelListOptions");   //Get an instance of the listings options select box
    var scrollbar = document.getElementById("selScrollbar");
    

   switch (selection.value)
   {
        case 'single':                                          //test if the user has selected single product
            listings.options.length = 0;                        //set the length of the options select to 0
            listings.disabled = false; 
            scrollbar.disabled = true;
            scrollbar.selectedIndex = 0;
            for(i=0; i<ArrayValue.length; i++)
            {
                // CC 21/1/09
                // Unescape the single quotes so the filenames will work as links
                listings.options[i] = new Option(ArrayValue[i], LabelValue[i].replace(/:/g, "'"));    //fill the select box with all products
            }                         
        break;
        case 'all':
            listings.options[0] = new Option('N/A','N/A');      //set the select to not applicable
            listings.disabled = true;
            scrollbar.disabled = false;                           //disable the select box
        break;
        case 'range': 
            listings.options.length = 0;                        //set the length of the options select to 0
            listings.disabled = false; 
            scrollbar.disabled = false;
            scrollbar.selectedIndex = 1;
            for(i=0; i<m_arrRangeLabels.length; i++)
            {
                listings.options[i] = new Option(m_arrRangeLabels[i], m_arrRangeLabels[i]);    //fill the select box with all products
            }  
        break;
    }
          if(m_firstCall!=true){setValues(strManName, strManCode, "selListingType");}
  }
  
  function generateHTML(strManName, strManCode)
  {
      var strUrl;
              
      strUrl="manufacturer="+strManName;
      strUrl=strUrl+"&mancode="+strManCode;
      strUrl=strUrl+returnValueList();
      window.location = "NBSPlusManufacturerListingsCode.aspx?" + strUrl;
  }
  
  function returnValueList()
  {
   //function sets all of the values of the Iframes based on the user specified properties
         
          var strheight = document.getElementById("inpHeight").value;                                            //Height Value
          var strwidth =  document.getElementById("inpWidth").value;                                          //width value
          var strborderdropdownIndex = document.getElementById("selBorderValue").selectedIndex;                
          var strborderdropdownValue = document.getElementById("selBorderValue")[strborderdropdownIndex].value;   
          var strborderWidthUnits = strborderdropdownValue;                                                        //border units value
          var strborderWidth = document.getElementById("inpBorder").value;                                     //border width value              
          var strborderColour = document.getElementById("inpBorderColour").value;                               //border colour value
          var strborderStyle = document.getElementById("selBorderStyle").value;                                 //border style value 
          var strBGColour = document.getElementById("inpBGColour").value;                                       //background colour value
          var strffdropdownIndex = document.getElementById("selFontFamily").selectedIndex;                      
          var strffdropdownValue = document.getElementById("selFontFamily")[strffdropdownIndex].value;
          var strfontFamily = strffdropdownValue;                                                                  //font family value
          var strdropdownIndex = document.getElementById("selFontSizeValue").selectedIndex;
          var strdropdownValue = document.getElementById("selFontSizeValue")[strdropdownIndex].value;
          var strfontSizeUnits = strdropdownValue;                                                                 //font size units value
          var strfontSize = document.getElementById("inpFontSize").value; 
          var strfontColour = document.getElementById("inpFontColour").value;                                   //font colour value
          var strrangeColour = document.getElementById("inpRangeColour").value;
          var strrangeHoverColour = document.getElementById("inpRangeHoverColour").value;
          var strLinkWdropdownIndex = document.getElementById("selLinkWeight").selectedIndex;                  
          var strLinkWdropdownValue = document.getElementById("selLinkWeight")[strLinkWdropdownIndex].value;      
          var strlinkWeight = strLinkWdropdownValue;                                                               //link weight value
          var strlinkColour = document.getElementById("inpLinkColour").value;                                   //link colour value
          var strlistStyle = document.getElementById("selListStyle").value;                                     //list style value
          var strscrollbar = document.getElementById("selScrollbar").value;  
          var strSingleValueIndex = document.getElementById("selListingType").selectedIndex;                
          var strSingleValuedropdownValue = document.getElementById("selListingType")[strSingleValueIndex].value;   
          var strSingleValue = strSingleValuedropdownValue;
          
          if(strSingleValue == "single" || strSingleValue == "range")
          {
              var strSingleValueNameIndex = document.getElementById("SelListOptions").selectedIndex;   
              var strSingleValueNamedropdownValue = document.getElementById("SelListOptions")[strSingleValueNameIndex].value;   
              var strSingleValueName = strSingleValueNamedropdownValue;
          }
          else
          {
              var strSingleValueName = "file.xml"
          }
              
            
            //build string of value names and values
        if(strheight && strwidth && strborderWidth && strborderWidthUnits && strborderColour && strborderStyle && strBGColour && strfontFamily && strfontSize && strfontSizeUnits && strfontColour && strrangeColour && strrangeHoverColour && strlinkWeight && strlinkColour && strlistStyle && strscrollbar && strSingleValue && strSingleValueName)    
        {    
            var strUrl;
            strUrl="&height="+strheight;
            strUrl=strUrl+"&width="+strwidth; 
            strUrl=strUrl+"&borderWidth="+strborderWidth; 
            strUrl=strUrl+"&borderWidthUnits="+strborderWidthUnits;  
            strUrl=strUrl+"&borderColour="+strborderColour; 
            strUrl=strUrl+"&borderStyle="+strborderStyle;  
            strUrl=strUrl+"&BGColour="+strBGColour; 
            strUrl=strUrl+"&fontFamily="+strfontFamily;  
            strUrl=strUrl+"&fontSize="+strfontSize; 
            strUrl=strUrl+"&fontSizeUnits="+strfontSizeUnits;
            strUrl=strUrl+"&fontColour="+strfontColour;  
            strUrl=strUrl+"&rangeColour="+strrangeColour; 
            strUrl=strUrl+"&rangeHoverColour="+strrangeHoverColour; 
            strUrl=strUrl+"&linkWeight="+strlinkWeight;  
            strUrl=strUrl+"&linkColour="+strlinkColour;     
            strUrl=strUrl+"&listStyle="+strlistStyle;  
            strUrl=strUrl+"&scrollbar="+strscrollbar; 
            strUrl=strUrl+"&singleValue="+strSingleValue;
            strUrl=strUrl+"&singleValueName="+strSingleValueName;
            
        }
    return strUrl;     //return the string of all the current values on the form
   }

    //copies the current text area to the clipboard
    function CopyHTMLListingsToClipboard(text2copy) 
    {
        var strval = document.getElementById(text2copy).innerText  //get the innertext of the text area (IE only)
        
        if (window.clipboardData) {                                     
            window.clipboardData.setData("Text",strval);   //set the clipboard to the text information passed  
        } 

    }
    
    //fucntion checks browser and displays copy to clipboard button dependant on whether the user is browsing in IE
    //firefox does not allow clipboard manipulation.
    function testBrowser(btnName)
    {
        var btnCopy = document.getElementById(btnName);
        
       if(btnCopy)
       { 
        if (navigator.appName == "Microsoft Internet Explorer")             //test the application name
            btnCopy.style.display = 'block';     //show the button if the application is IE
        else
            btnCopy.style.display = 'none';      //otherwise hide the button
       }
       else
       {
        alert("Script error in testing browser");
       }
    }
    
    //selects all of the text in thr text area when the user clicks on it (stops partial copies)
    function selectAll(Select)
    {
       var ctrTextArea = document.getElementById(Select)
        
        if (ctrTextArea) 
        {
            ctrTextArea.focus();
            ctrTextArea.select();
        }
    }


    function generateHTMLCode(strManName, strManCode)
    {
        if(check4Error() == false)
        {
         generateHTML(strManName, strManCode);
        }
         else
        {
         alert("Some information specified in the designer is incorrect or missing, please make the relevant amendments and try again.");    
        }
    
    }

    function check4Error()
    {
        error = false;
                 
        if((document.getElementById("inpHeight").value == "") || IsNumeric(document.getElementById("inpHeight").value)== false || document.getElementById("inpHeight").value > 100000)
        {
         document.getElementById("divErrorHeight").style.display = 'block';
         document.getElementById("divErrorHeight").innerHTML = "Error: Invalid Value";
         error = true;
         }
         else
         {  
          var strSingleValueIndex = document.getElementById("selListingType").selectedIndex;                
          var strSingleValuedropdownValue = document.getElementById("selListingType")[strSingleValueIndex].value;   
          var strSingleValue = strSingleValuedropdownValue;
          var height = document.getElementById("inpHeight").value;  
          var intThresHold = 120;

          //alert(strSingleValue);
          //alert(intThresHold);
          //alert(height);
          
          if(strSingleValue == "single"){intThresHold = 30;}
          if(height < intThresHold)
          {
            document.getElementById("divErrorHeight").style.display = 'block';
            document.getElementById("divErrorHeight").innerHTML = "Error: Minimum height -" + intThresHold + "px";
            error = true;
          }
          else
          {             
            document.getElementById("divErrorHeight").style.display = 'none';
          } 
        }
       
        
        if((document.getElementById("inpWidth").value == "") || IsNumeric(document.getElementById("inpWidth").value)== false || document.getElementById("inpWidth").value > 100000)
        {
         document.getElementById("divErrorWidth").style.display = 'block';
         document.getElementById("divErrorWidth").innerHTML = "Error: Invalid Value";
         error = true;
        }
         else
        {
           if(document.getElementById("inpWidth").value < 280)
            {      
                document.getElementById("divErrorWidth").style.display = 'block';
                document.getElementById("divErrorWidth").innerHTML = "Error: Minimum width - 280px";
                error = true;    
            }
            else
            {
                document.getElementById("divErrorWidth").style.display = 'none';
            }
        } 
        
        if((document.getElementById("inpBorder").value == "") || IsNumeric(document.getElementById("inpBorder").value)== false)
        {
         document.getElementById("divErrorBorder").style.display = 'block';
         document.getElementById("divErrorBorder").innerHTML = "Error: Invalid Value";
         error = true;
        }
         else
        {
         document.getElementById("divErrorBorder").style.display = 'none';
        } 
        
        if(document.getElementById("inpBorderColour").value == "" || IsHex(document.getElementById("inpBorderColour").value)== false)
        {
         document.getElementById("divErrorBorderColour").style.display = 'block';
         document.getElementById("divErrorBorderColour").innerHTML = "Error: Invalid Value";
         error = true;
        }
         else
        {
         document.getElementById("divErrorBorderColour").style.display = 'none';
        }
        
        if(document.getElementById("inpBGColour").value == "" || IsHex(document.getElementById("inpBGColour").value)== false)
        {
         document.getElementById("divErrorBGColour").style.display = 'block';
         document.getElementById("divErrorBGColour").innerHTML = "Error: Invalid Value";
         error = true;
        }
         else
        {
         document.getElementById("divErrorBGColour").style.display = 'none';
        } 
        
        if(document.getElementById("inpFontColour").value == "" || IsHex(document.getElementById("inpFontColour").value)== false)
        {
         document.getElementById("divErrorFontColour").style.display = 'block';
         document.getElementById("divErrorFontColour").innerHTML = "Error: Invalid Value";
         error = true;
        }
         else
        {
         document.getElementById("divErrorFontColour").style.display = 'none';
        } 
        
        if(document.getElementById("inpFontSize").value == "" || IsNumeric(document.getElementById("inpFontSize").value)== false)
        {
         document.getElementById("divErrorFontSize").style.display = 'block';
         document.getElementById("divErrorFontSize").innerHTML = "Error: Invalid Value";
         error = true;
        }
         else
        {
         document.getElementById("divErrorFontSize").style.display = 'none';
        } 
        
        if(document.getElementById("inpLinkColour").value == "" || IsHex(document.getElementById("inpLinkColour").value)== false)
        {
         document.getElementById("divErrorLinkColour").style.display = 'block';
         document.getElementById("divErrorLinkColour").innerHTML = "Error: Invalid Value";
         error = true;
        }
         else
        {
         document.getElementById("divErrorLinkColour").style.display = 'none';
        } 

        return error;
     
     }
    
    function IsNumeric(strString){
        var strValidChars = "0123456789.-";
        var strChar;
        var blnResult = true;

       if (strString.length == 0) return false;

       //  test strString consists of valid characters listed above
       for (i = 0; i < strString.length && blnResult == true; i++)
          {
          strChar = strString.charAt(i);
          if (strValidChars.indexOf(strChar) == -1)
             {
             blnResult = false;
             }
          }
         //alert(blnResult);
       return blnResult;
   }
   
   
    function IsHex(strString){
       var strValidChars = "0123456789ABCDEFabcdef";
       var strChar;
       var blnResult = true;
       
       if (strString.length !=6) return false;

       //  test strString consists of valid characters listed above
       for (i = 0; i < strString.length && blnResult == true; i++)
          {
          strChar = strString.charAt(i);
          if (strValidChars.indexOf(strChar) == -1)
             {
             blnResult = false;
             }
          }
         return blnResult;
   }
   
   function checkReturnPress(e, strManName, strManCode, strPropertyName, strCallEnt, blnColour){
        if(e.keyCode == 13)
        {
            if(blnColour == false)
                setValues(strManName, strManCode, strCallEnt);
            else
                colourChange(strPropertyName, strCallEnt, strManName, strManCode);
        }
    }
///////////////////////////    
//colour picker functions//     
///////////////////////////   
 

    function clickPosition(anchorLocationName) {
        // This function will return an Object with x and y properties
        var useWindow=false;
        var coordinates=new Object();
        var x=0,y=0;
    	
        var o=document.getElementById(anchorLocationName);
    		
        x=clickPosition_getPageOffsetLeft(o);
        y=clickPosition_getPageOffsetTop(o);
        coordinates.x=x;
        coordinates.y=y;
    	
        return coordinates;
        }

    // Functions for IE to get position of an object
    function clickPosition_getPageOffsetLeft (el) {
        var ol=el.offsetLeft;
    	
        while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
        return ol;
        }

    function clickPosition_getPageOffsetTop (el) {
        var ol=el.offsetTop;
    	
        while ((el=el.offsetParent) != null) { ol += el.offsetTop; }
        return ol;
        }
    	
    // Set the position of the popup window based on the anchor
    function setLocationCoordinates(anchorLocationName) {
        var coordinates;
            coordinates = clickPosition(anchorLocationName);	
        this.x = coordinates.x;
        this.y = coordinates.y;

    	
        }
    	
    // Fill the window with contents
    function PopulateWindow(contents) {
        this.contents = contents;
        this.populated = false;
        }

    // Refresh the displayed contents of the popup
    function RefreshWindow() {
        if (this.divName != null) {
	        // refresh the DIV object
            document.getElementById(this.divName).innerHTML = this.contents;
	        }
        }
    // Position and show the popup, relative to an anchor object
    function ShowWindow(anchorLocationName) {
        this.getXYPosition(anchorLocationName);
        this.x += this.offsetX;
        this.y += this.offsetY;
    	
        if (!this.populated && (this.contents != "")) {
	        this.populated = true;
	        this.refresh();
	        }
        if (this.divName != null) {
	        // Show the DIV object
		        document.getElementById(this.divName).style.left = this.x + "px";
		        document.getElementById(this.divName).style.top = this.y + "px";
		        document.getElementById(this.divName).style.visibility = "visible";
	        }
        }
    // Hide the popup
    function HideWindow() {
        if (this.divName != null) {
		        document.getElementById(this.divName).style.visibility = "hidden";
            }
        }

    // Pass an event and return whether or not it was the popup DIV that was clicked
    function WindowIsClicked(e) {
        if (this.divName != null) {
	        if (e) {
		        var t = e.originalTarget;
		        while (t.parentNode != null) 
		        {
			        if (t.id==this.divName) 
			        {
				        return true;
			        }
			        t = t.parentNode;
		        }
		        return false;
		        }
	        return false;
	        }
        return false;
        }

    // Check an onMouseDown event to see if we should hide
    function WindowHideIfNotClicked(e) {
        if (this.autoHideEnabled && !this.isClicked(e)) {
	        this.hidePopup();
	        }
        }
    // Call this to make the DIV disable automatically when mouse is clicked outside it
    function WindowAutohide() {
        this.autoHideEnabled = true;
        }
    // This global function checks all PopupWindow objects onmouseup to see if they should be hidden
    function PopupWindow_hidePopupWindows(e) {
        for (var i=0; i<popupWindowObjects.length; i++) {
	        if (popupWindowObjects[i] != null) {
		        var p = popupWindowObjects[i];
		        p.hideIfNotClicked(e);
		        }
	        }
        }
    // Run this immediately to attach the event listener
    function PopupWindow_attachListener() {
        if (document.layers) {
	        document.captureEvents(Event.MOUSEUP);
	        }
        window.popupWindowOldEventListener = document.onmouseup;
        if (window.popupWindowOldEventListener != null) {
	        document.onmouseup = new Function("window.popupWindowOldEventListener(); PopupWindow_hidePopupWindows();");
	        }
        else {
	        document.onmouseup = PopupWindow_hidePopupWindows;
	        }
        }
    // CONSTRUCTOR for the PopupWindow object
    function PopupWindow() {
        if (!window.popupWindowIndex) { window.popupWindowIndex = 0; }
        if (!window.popupWindowObjects) { window.popupWindowObjects = new Array(); }
        if (!window.listenerAttached) {
	        window.listenerAttached = true;
	        PopupWindow_attachListener();
	        }
        this.index = popupWindowIndex++;
        popupWindowObjects[this.index] = this;
        this.Width=0;
        this.height=0;
        this.populated = false;
        this.visible = false;
        this.autoHideEnabled = false;
        this.contents = "";
        this.type="DIV";
        this.divName = arguments[0];
        this.offsetX = 0;
        this.offsetY = 0;
    	
        // Method mappings
        this.getXYPosition = setLocationCoordinates;
        this.populate = PopulateWindow;
        this.refresh = RefreshWindow;
        this.showPopup = ShowWindow;
        this.hidePopup = HideWindow;
        this.isClicked = WindowIsClicked;
        this.autoHide = WindowAutohide;
        this.hideIfNotClicked = WindowHideIfNotClicked;
        }

    ColorPicker_targetInput = null;
    function ColorPicker_writeDiv() {
        document.writeln("<div id=\"colorPickerDiv\" STYLE=\"position:absolute;visibility:hidden;\"> </div>");
        }

    function ColorPicker_show(anchorLocationName) {
        this.showPopup(anchorLocationName);
        }

    function ColorPicker_pickColor(color,obj) {
        obj.hidePopup();
        pickColor(color);
        }

    // A Default "pickColor" function to accept the color passed back from popup.
    // User can over-ride this with their own function.
    function pickColor(colour) {
        if (ColorPicker_targetInput==null) {
	        alert("Target Input is null, which means you either didn't use the 'select' function or you have no defined your own 'pickColor' function to handle the picked color!");
	        return;
	        }
        ColorPicker_targetInput.value = colour;
        colourChange(PropertyName, CallingEntity, ManufacturerName, ManufacturerCode);
    	
        }

    function ColorPicker_select(inputobj,linkname,strCallEnt,strPropName,strManName,strManCode) {
    //debugger
        if (inputobj.type!="text" && inputobj.type!="hidden" && inputobj.type!="textarea") { 
	        alert("colorpicker.select: Input object passed is not a valid form input object"); 
	        window.ColorPicker_targetInput=null;
	        return;
	        }
        window.ColorPicker_targetInput = inputobj;
        window.CallingEntity = strCallEnt;
        window.PropertyName = strPropName;	
        window.ManufacturerName = strManName;
        window.ManufacturerCode = strManCode;
    	
        this.show(linkname);
        }


    function ColorPicker() {
    //debugger

        var divname = "colorPickerDiv";
        var cp = new PopupWindow(divname);

        cp.currentValue = "#FFFFFF";
    	

        cp.writeDiv = ColorPicker_writeDiv;
        cp.show = ColorPicker_show;
        cp.select = ColorPicker_select;

        // Code to populate colour picker window
        var colors = new Array( "191970","00008B","0000CD","0000FF","1E90FF","00BFFF","87CEFA","87CEEB","ADD8E6","B0E0E6","AFEEEE","E0FFFF",
                                "8B0000","A52A2A","B22222","8B4513","D2691E","CD853F","B8860B","D2B48C","DEB887","F5DEB3","FFDEAD","FFE4C4",
                                "006400","008000","228B22","2E8B57","0D8F63","32CD32","00FF00","7CFC00","7FFF00","ADFF2F","90EE90","98FB98",
                                "4B0082","800080","8B008B","9932CC","9400D3","8A2BE2","9370DB","DA70D6","EE82EE","DDA0DD","D8BFD8", "E6E6FA",
                                "000000","333333","4C4C4C","808080","C0C0C0","D3D3D3","DCDCDC","F0F8FF","F0FFFF","F5FFFA","FFFAFA","FFFFFF"
                               );
        var intTotal = colors.length;
        var intWidth = 12;
        var strCP_contents = "";
    	
        strCP_contents+= "<table bgcolor=#000000 cellspacing=2 cellpadding=0>";
        for (var i=0; i<intTotal; i++) {
	        if ((i % intWidth) == 0) 
	        {
	            strCP_contents+= "<tr>"; 
	        }
            strCP_contents+= '<td bgcolor="#'+colors[i]+'"><font size="4"><a href="#" onclick="ColorPicker_pickColor(\''+colors[i]+'\',window.popupWindowObjects['+cp.index+']);return false;" STYLE="text-decoration:none;">&nbsp;&nbsp;&nbsp;</a></font></td>';
	        if ( ((i+1)>=intTotal) || (((i+1) % intWidth) == 0)) 
	        { 
		        strCP_contents+= "</tr>";
	        }
        }
        strCP_contents+= "</table>";

        // end populate code

        // Write the contents to the popup object
        cp.populate(strCP_contents+"\n");
        // Move the table down a bit so you can see it
        cp.offsetX = 20;
        cp.autoHide();
        return cp;
        }




