﻿
function GetXMLHTTP()
{
  var xmlHttp;

  try {    // Firefox, Opera 8.0+, Safari    
    xmlHttp=new XMLHttpRequest();
  }
  catch(e)
  {    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!");
        return false;
      }
    } 
  }
  
  return xmlHttp;
}

function GetLabelName(pControlID)
{
  var labels = document.getElementsByTagName("label");

  for (var i = 0; i < labels.length; i++)
  {
    if(labels[i].htmlFor == pControlID)
    {
      if(labels[i].innerText != undefined) {
        return labels[i].innerText;
	    }
	    else {
        return labels[i].innerHTML;
      }
    }
  }
  
  return "";
}

function SetRequired(pCtrl, pImg, pRequired)
{
  var ctrl = document.getElementById(pCtrl);
  var img = document.getElementById(pImg);
  
  if(pRequired == true)
  {
    if(ctrl) {ctrl.className = "required";}
    if(img) {img.style.display = "inline";}
  }
  else
  {
    if(ctrl) {ctrl.className = "";}
    if(img) {img.style.display = "none";}
  }
}

function SetDisabled(pID, pDisabled)
{
  var o = document.getElementById(pID);
  if(o) {
    o.disabled = pDisabled;
  }
}

function SetVisible(pID, pDisabled)
{
  var o = document.getElementById(pID);
  if(o)
  {
      if (pDisabled) {
          try {
              o.style.display = "table-row";
          }
          catch (e) {
              o.style.display = "block";
          }
    }
    else {
      o.style.display = "none";
    }
  }
}

/* VALIDATION FUNCTIONS */

      function ValidatePostCode(el)
      {
        if(!el) {return;}

        var pattern = /^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$/;
        var value = el.value;
        
        if(pattern.test(value) == false)
        {
          alert("Please check the postcode.");
          el.focus();
          return false;
        }
        else {
          return true;
        }
      }

      function ValidateEmailAddress(el) {
          if (!el) { return; }

          var pattern = /^[a-zA-Z0-9._-]*[a-zA-Z0-9._-]@[a-zA-Z0-9._-]*[a-zA-Z0-9._-]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;

          var value = el.value;

          if (pattern.test(value) == false) {
              alert("Please check the email address.");
              el.focus();
              return false;
          }
          else {
              return true;
          }
      }



/* POST CODE SEARCH FUNCTIONS */
	    
	          
      function ShowAddress(pShow)
      {
        SetVisible("address1Row", pShow);
        SetVisible("address2Row", pShow);
        SetVisible("address3Row", pShow);
        SetVisible("townRow", pShow);
        SetVisible("countyRow", pShow); 
      }
            
      function ShowPostcode(pShow)
      {
        SetVisible("postcodeRow", pShow);
      }
      
      function cmdAddressNotPresent_Click()
      {     
        ShowAddress(true);
        SetVisible("postcodePopup", false)
      }
      
      function cmdCancelPostcodeSearch_Click()
      {
        SetVisible("postcodePopup", false)
      }
      
      function CentreObject(o, width, height)
      {
        var scrolledX, scrolledY;
        if( self.pageYOffset ) {
          scrolledX = self.pageXOffset;
          scrolledY = self.pageYOffset;
        } else if( document.documentElement && document.documentElement.scrollTop ) {
          scrolledX = document.documentElement.scrollLeft;
          scrolledY = document.documentElement.scrollTop;
        } else if( document.body ) {
          scrolledX = document.body.scrollLeft;
          scrolledY = document.body.scrollTop;
        }

        var centerX, centerY;
        if( self.innerHeight ) {
          centerX = self.innerWidth;
          centerY = self.innerHeight;
        } else if( document.documentElement && document.documentElement.clientHeight ) {
          centerX = document.documentElement.clientWidth;
          centerY = document.documentElement.clientHeight;
        } else if( document.body ) {
          centerX = document.body.clientWidth;
          centerY = document.body.clientHeight;
        }

        var leftOffset = scrolledX + (centerX - width) / 2;
        var topOffset = scrolledY + (centerY - height) / 2;
        
        o.style.display = "block";
        o.style.position = "absolute";
        o.style.left = leftOffset + "px";
        o.style.top = topOffset + "px";
      }

      function cmdSearch_Click()
      {
        var ctrlPostcode = document.getElementById("txtPostcode");
        if(ctrlPostcode) {
          if(ctrlPostcode.value.length == 0) {
            alert("Please enter a postcode.");
            ctrlPostcode.focus();
            return false;
          }
        }
      
        var xmlHttp = GetXMLHTTP();

        xmlHttp.onreadystatechange = function()
        {
          if(xmlHttp.readyState==4)
          {         
            var addresses = xmlHttp.responseText.split("|");
            
            switch(addresses[0])
            {
              case "OK":
              {
                var addressList = document.getElementById("lstAddress");
                
                addressList.options.length = 0;

                for (var i = 1; i < addresses.length; i++)
                {
                  var address = addresses[i].split(",");
                  
                  addressList.options[addressList.length] = new Option(address[1], address[0]);
                }
                
                var o = document.getElementById("postcodePopup");
                
                CentreObject(o, 530, 280);
                
                break;
              }
              case "BX":
              {
                alert("Sorry, we are unable to handle any post code requests from this computer as the service has been abused. If you believe this to be incorrect please email development@kyoceramita.co.uk.");
                break;
              }
              case "BC":
              {
                alert("Sorry, the number of post code requests from this computer has exceeded the daily limit.");
                break;
              }
              default:
              {
                alert("Sorry, there has been a problem accessing the postcode database.");
                break;
              }
            }
          }
        }
        
        var postcode = document.getElementById("txtPostcode").value;
        
        document.getElementById("hidPostcode").value = postcode;
                   
        xmlHttp.open("GET", "postcodelookup.aspx?postcode=" + postcode, true);
        xmlHttp.send(null);  
      }
      
	    function lstAddress_Click()
	    {
	      var xmlHttp = GetXMLHTTP();

        xmlHttp.onreadystatechange = function()
        {
          if(xmlHttp.readyState == 4)
          {         
            var addressArray = xmlHttp.responseText.split("|");

            switch(addressArray[0])
            {
              case "OK":
              {
                document.getElementById("txtAddressLine1").value = addressArray[1];    
                document.getElementById("txtAddressLine2").value = addressArray[2];    
                document.getElementById("txtAddressLine3").value = addressArray[3];    
                document.getElementById("txtTown").value = addressArray[4];   
                document.getElementById("txtCounty").value = addressArray[5];   
                document.getElementById("txtPostcode").value = addressArray[6];
                ShowAddress(true);
                break;
              }
              case "BX":
              {
                alert("Sorry, we are unable to handle any post code requests from this computer as the service has been abused. If you believe this to be incorrect please email development@kyoceramita.co.uk.");
                break;
              }
              case "BC":
              {
                alert("Sorry, the number of post code requests from this computer has exceeded the daily limit.");
                break;
              }
              default:
              {
                alert("Sorry, there has been a problem accessing the postcode database.");
                break;
              }
            }
          }
        }
        
        var postcode = document.getElementById("hidPostcode").value;
        var addressID = this.options[this.selectedIndex].value;
        
        xmlHttp.open("GET", "postcodelookup.aspx?postcode=" + postcode + "&addressid=" + addressID, true);
        xmlHttp.send(null);
        
        document.getElementById("postcodePopup").style.display = "none";
	    }
