﻿// JScript File

   
    function ClearValue(field, strCheck)
        {
	        if(field.value == strCheck)
	        field.value = "";				
        }
    function Repopulate(field, strCheck)
        {
	        if(field.value == "")
	        field.value = strCheck;
        }	

    function doClick(buttonName,e)
    {

        var key;

         if(window.event)
              key = window.event.keyCode;     //IE
         else
              key = e.which;     //firefox
    
        if (key == 13)
        {
            //Get the button the user wants to have clicked
            var btn = document.getElementById(buttonName);
            if (btn != null)
            { //If we find the button click it
                btn.click();
                event.keyCode = 0
            }
        }
   }


    function handleError() {
	return true;    
    }

    window.onerror = handleError;

    function CurrencyFormatted(amount)
    {
        var i = parseFloat(amount);
        if(isNaN(i)) { i = 0.00; }
        var minus = '';
        if(i < 0) { minus = '-'; }
        i = Math.abs(i);
        i = parseInt((i + .005) * 100);
        i = i / 100;
        s = new String(i);
        if(s.indexOf('.') < 0) { s += '.00'; }
        if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
        s = minus + s;
        return s;
    }
    // end of function CurrencyFormatted()

    // Google Tracking loader script
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

    function switchMenu(obj, linkText) {
        var el = document.getElementById(obj);
        var filterstore = document.getElementById('filter_FilterDrops');
        var switchlink = document.getElementById(obj + 'link');
        
        if ( el.style.display != "none" ) {
            // close it
	        el.style.display = 'none';
	        filterstore.value += obj + 'c|';
	        switchlink.innerHTML = '» Show' + linkText;
        }
        else {
            // open it
	        el.style.display = '';
	        filterstore.value += obj + 'o|';
	        switchlink.innerHTML = '» Hide' + linkText;
        }
    }


    function closeMenu(obj) {
        var el = document.getElementById(obj);
	    el.style.display = 'none';
    }

    function openMenu(obj) {
        var el = document.getElementById(obj);
	    el.style.display = '';
    }

    function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
      var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
      return newnumber; // Output the result to the form field (change for your purposes)
    }

    function checkPostCode (toCheck) {
    
      // blank?
      if (toCheck == 'Enter Keyword')
        {
            toCheck = '';
        }

      // Permitted letters depend upon their position in the postcode.
      var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
      var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
      var alpha3 = "[abcdefghjkstuw]";                                // Character 3
      var alpha4 = "[abehmnprvwxy]";                                  // Character 4
      var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
      
      // Array holds the regular expressions for the valid postcodes
      var pcexp = new Array ();

      // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
      pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
      
      // Expression for postcodes: ANA NAA
      pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

      // Expression for postcodes: AANA  NAA
      pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
      
      // Exception for the special postcode GIR 0AA
      pcexp.push (/^(GIR)(\s*)(0AA)$/i);
      
      // Standard BFPO numbers
      pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
      
      // c/o BFPO numbers
      pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
      
      // Overseas Territories
      pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

      // Load up the string to check
      var postCode = toCheck;

      // Assume we're not going to find a valid postcode
      var valid = false;
      
      // Check the string against the types of post codes
      for ( var i=0; i<pcexp.length; i++) {
        if (pcexp[i].test(postCode)) {
        
          // The post code is valid - split the post code into component parts
          pcexp[i].exec(postCode);
          
          // Copy it back into the original string, converting it to uppercase and
          // inserting a space between the inward and outward codes
          postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
          
          // If it is a BFPO c/o type postcode, tidy up the "c/o" part
          postCode = postCode.replace (/C\/O\s*/,"c/o ");
          
          // Load new postcode back into the form element
          valid = true;
          
          // Remember that we have found that the code is valid and break from loop
          break;
        }
      }
      
      // Return with either the reformatted valid postcode or the original invalid 
      // postcode
      if (valid)
        {
            window.location = 'findprice.aspx?s=' + postCode;
        }
    else
        {   
            window.location = 'process.aspx?mode=search&val=' + postCode;
        }
       
    }

    function checkEnter(e)
    
    { 
        var characterCode
    
        characterCode = e.keyCode 
        
        if (characterCode == 13)
            {
                alert('eee');
            }
    
    
    }
    
    function getXMLHTTP() {
        var XMLHTTP = null;
        if (window.ActiveXObject) {
            try {
                XMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                }
            }
        } else if (window.XMLHttpRequest) {
            try {
                XMLHTTP = new XMLHttpRequest();
            } catch (e) {
            }
        }
        return XMLHTTP;
    }
    
    function stateChanged() {
        if (XMLHTTP.readyState == 4 &&
            XMLHTTP.status ==200) {
            if (XMLHTTP.responseText == 'diff') {
                window.alert("When choosing products to compare, they must be of the same type. For example, you can only compare laminate products with other laminate products. To compare a different type of product you must first remove the other products from your comparison range. To view your current compared products, please click on the 'Compare Selected' link.");
            }
        }
    }
    
	var XMLHTTP = getXMLHTTP();

    function addToCompareBasket(productID) {
        
        if (XMLHTTP != null) {
            
            XMLHTTP.open("GET", "process.aspx?sendData=ok&mode=ADDCOMPARE&productid=" + productID, true);
            XMLHTTP.onreadystatechange = stateChanged;
            XMLHTTP.send(null);
        }
       
    }
