
/* this is the code taken from profile.tpl which should be the same as register.tpl code */

            var xmlHttp = false;
            var xmlHttp2 = false;
            var shiftdown = true;
            var ready = true;
            var postback = true;
            var chosenDealer = null;

            /*@cc_on @*/
            /*@if (@_jscript_version >= 5)
            try {
              xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
              try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e2) {
                xmlHttp = false;
              }
            }
            @end @*/

            if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
                xmlHttp = new XMLHttpRequest();
              }
            
            if (!xmlHttp2 && typeof XMLHttpRequest != 'undefined') {
            	xmlHttp2 = new XMLHttpRequest();
              }

        function toProperCase(s)
        {
          return s.toLowerCase().replace(/^(.)|\s(.)/g,
                  function($1) { return $1.toUpperCase(); });
        }

        function isBlank(val){
            if(val==null){return true;}
            for(var i=0;i<val.length;i++) {
                if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
                }
            return true;
          }
        
       

        function getPostcode(zPostback, zChosenDealer) {
           
            if ( ready==false ) { alert('A lookup is in progress. Please wait.'); return true; }

            postback = zPostback;
            chosenDealer = zChosenDealer; // for keeping form state, usedin in popDealerBox

            shiftdown = true;
            var streetnum = null;
            var lookup_houseno = document.getElementById("lookup_houseno").value;
            var regex = /(\d+).*/;
            var matches = regex.exec(lookup_houseno);
            if ( isBlank(document.getElementById("lookup_houseno").value) ) { shiftdown = false; }
            if ( matches != null ) {
                streetnum = matches[1];
                shiftdown = false;
            }
            /* make a call to postcode lookup program */
            var lookup_postcode = document.getElementById("lookup_postcode").value;
            if (lookup_postcode.length>0) {
                var url = "postcode_lookup.pl?";
                if ( streetnum != null ) { url += "h=" + escape(streetnum) + "&"; }
                url += "p=" + escape(lookup_postcode);
                xmlHttp.open("GET", url, true);
                xmlHttp.onreadystatechange = updatePage;
                ready = false;
                xmlHttp.send(null);
            }
        }

        function updatePage() {

            if (xmlHttp.readyState == 4) {

                  if ( xmlHttp.status == 200 ) {

                	  var responseText = xmlHttp.responseText;                              
                      var addressDetails = responseText.split('|');
                      
                	  if (postback==false) {
                          if ( addressDetails.length == 4 ) {
                                if (shiftdown==true) {
                                    document.getElementById('cust_address2').value = addressDetails[0];
                                    document.getElementById('cust_address3').value = addressDetails[3];
                                    document.getElementById('cust_address4').value = '';
                                    document.getElementById('cust_address5').value = addressDetails[1];
                                    document.getElementById('cust_county').value = addressDetails[2];
                                    document.getElementById('cust_postcode').value = document.getElementById("lookup_postcode").value.toUpperCase();
                                } else {
                              	  document.getElementById('cust_address1').value = addressDetails[0];
                                    document.getElementById('cust_address2').value = addressDetails[3];
                                    document.getElementById('cust_address3').value = '';
                                    document.getElementById('cust_address4').value = '';
                                    document.getElementById('cust_address5').value = addressDetails[1];
                                    document.getElementById('cust_county').value = addressDetails[2];
                                    document.getElementById('cust_postcode').value = document.getElementById("lookup_postcode").value.toUpperCase();
                               } // shiftdown

                          } else {
                                  alert('No address details found. Please enter manually');
                          }
                      }
                	  
                        

                    /* pcode res back, call nrst main dealers */
                        var lookup_postcode = document.getElementById("lookup_postcode").value;
                        if (lookup_postcode.length>0) {
                            var url = "dealer_nrst.php?";
                            url += "dpcode=" + escape(lookup_postcode) +"&"+"dmain=on";
                            xmlHttp2.open("GET", url, true);
                            xmlHttp2.onreadystatechange = popDealerBox;
                            xmlHttp2.send(null);
                        }
                        /*var lookup_postcode = document.getElementById("lookup_postcode").value;
                        if (lookup_postcode.length>0) {
                            var url = "dealer_nrst.php?";
                            url += "dpcode=" + escape(lookup_postcode) +"&"+"dmain=on";
                            xmlHttp.open("GET", url, true);
                            xmlHttp.onreadystatechange = popDealerBox;
                            xmlHttp.send(null);
                        }*/

                        
                  
                  } // xmlHttp.readyState

                }
            	ready = true;
            }

            function popDealerBox() {
                if (xmlHttp2.readyState == 4) {

                      if ( xmlHttp2.status == 200 ) {

                        var responseText = xmlHttp2.responseText;
                        var dlrs = responseText.split('#');
                        if ( dlrs.length>0 ) {
                            var cdlrbox = document.getElementById("cust_dealer");
                            cdlrbox.options.length = 0;
                            cdlrbox.options[0] = new Option('Select nearest dealer here OR mailer order below.', 0);
                            for( c=0; c<dlrs.length-1; c++){
                                var detail = dlrs[c].split('|');
                                var dtext   = detail[1];
                                var distance = detail[3];
                                if ( detail[2].length>0 ) { dtext += ", "+detail[2]; }
                                dtext += ' (' + distance + ' miles)';
                                cdlrbox.options[c+1] = new Option(toProperCase(dtext),detail[0]);
                                if ( chosenDealer!='undefined' && chosenDealer!='') {
                                    if ( chosenDealer==detail[0] ) {
                                        cdlrbox.options.selectedIndex = c+1;
                                        document.getElementById('cust_dealermord').options.selectedIndex = 0;
                                    }
                                }
                            }
                        }
                        /* let the user use the form again when our request finishes */
                        document.getElementById('cust_firstname').focus();
                        ready = true;
                      }
                }
            }

            function checkAddress() {
                //var cust_address1 = document.getElementById("cust_address1").value;
                //var regex = /(\d+).*/;
                //var matches = regex.exec(cust_address1);
                //if ( matches == null ) {
                //    return confirm('We need a house number to match online accounts to any existing offline account you may hold with us.  Please ensure you enter your house number on Address Line 1 if appropriate.');
                //}
                return true;
            }


