var req;
var fld;

function loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
         response  = req.responseXML.documentElement;
         method    = response.getElementsByTagName('method')[0].firstChild.data;
         result    = response.getElementsByTagName('result')[0].firstChild.data;
         eval(method + '(\'\', result)');
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}
function hkasse_lookup() {  
  var pid = document.getElementById("hkasse_productnu");
  if(pid) {
    var pnu = pid.value; 
    var txt = document.getElementById("hkasse_txt");
    if(pnu == '') {
      if(txt) {
        txt.innerHTML = '';
      }
    }
    else {
      var url = "/wsp/shgsve/ajax.cgi?func=catalog.hkasse_lookup&JSON=&productnu="+pnu;
      jQuery.get(url,
        function(data){
          isBusy = false;
          if(txt) {
            txt.innerHTML = data;
          }
        }
      );
    }
  }
}

function city_lookup_new(object, event) {
  var city = document.getElementById('ORDERS.D_CITY');
  var city_input = document.getElementById('city_input');
  var errormessage = '<span style=\'color:red;\'>Ingen postort hitades</span>';
  if(event=='onblur' && object.value.length==5 && city_input.value) {
     return true;
  }
  else if(object.value.length==5) {
    jQuery.get(
      '/wsp/shgsve/ajax.cgi?func=order.zipcode&JSON=0&o_zipcode='+object.value,
      function(data) {
        if(data){
          var cty = data.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
          city.innerHTML = cty;
          city_input.value = cty;
        }
        else {
          city.innerHTML = errormessage;
          city_input.value = '';
        }
      }
    )
  }
  else {
    if(event!='onblur') {
      errormessage = "";
    }
    city.innerHTML = errormessage;
    city_input.value = '';    
  }
}

function city_lookup() {  
  var zipcode = document.getElementById("ORDERS.D_ZIPCODE");
  var city = document.getElementById("ORDERS.D_CITY");
  var city_input = document.getElementById("city_input");
  if(zipcode && city && city_input) {
    var code = zipcode.value; 
    if(code != '') {
      var url = "/wsp/shgsve/ajax.cgi?func=order.zipcode&JSON=&o_zipcode="+code;
      jQuery.get(url,
        function(data){
          var cty = mytrim(data);
          city.innerHTML = cty;
          city_input.value = cty; 
        }
      );
    }
  }
}

function mytrim(str) {
  return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
