// ---- Begin - set base url for accessing the MivaScript file that builds the XML (make sure to include the store code)
var url = location.protocol + '//' + location.hostname + '/miva/4.23/mctajax.mvc?Store_Code=MCT&prodcode=';
// ---- End - set base url for accessing the MivaScript file that builds the XML

      function handleHttpResponse() {
        if (http.readyState == 4) {
          if (http.responseText.indexOf('invalid') == -1) {

            // Use the XML DOM to unpack the city and state data 
            var xmlDocument = http.responseXML; 
            var basketcount = getInnerText(xmlDocument.getElementsByTagName('basketcount').item(0));
            var stocklevel = getInnerText(xmlDocument.getElementsByTagName('stocklevel').item(0));
            var invshort = serializeNode(xmlDocument.getElementsByTagName('invshort').item(0));
            var invlong = serializeNode(xmlDocument.getElementsByTagName('invlong').item(0));
            document.getElementById('basketcount').innerHTML = ": "+basketcount;
	          var prodcodeValue = document.getElementsByName("Product_Code")[0].value;
						if (prodcodeValue != 'GC10') {
	            if (document.getElementById('invmsg')) {
  	            if (stocklevel) {
    	            document.getElementById('invmsg').innerHTML = invlong;
      	        }
        	    }
						}
            isWorking = false;
          }
        }
      }

      var isWorking = false;
      function checkCart() {
        if (document.getElementsByName("Product_Code")[0])
          var prodcodeValue = document.getElementsByName("Product_Code")[0].value;
        else
          var prodcodeValue = 'none';
        if (!isWorking && http) {
          fullurl = url + escape(prodcodeValue);
          http.open("GET", fullurl, true);
          http.onreadystatechange = handleHttpResponse;
          isWorking = true;
          http.send(null);
        }
      }

      function getHTTPObject() {
        var xmlhttp;
        /*@cc_on
        @if (@_jscript_version >= 5)
          try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (e) {
            try {
              xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
              xmlhttp = false;
            }
          }
        @else
        xmlhttp = false;
        @end @*/
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
          try {
            xmlhttp = new XMLHttpRequest();
          } catch (e) {
            xmlhttp = false;
          }
        }
        return xmlhttp;
      }

// Helper function to return text (no tags) contained within a node
function getInnerText(node) { 
	if (typeof node.textContent != 'undefined') { 
		return node.textContent; 
	} 
	else if (typeof node.innerText != 'undefined') { 
		return node.innerText; 
	} 
	else if (typeof node.text != 'undefined') { 
		return node.text; 
	} 
	else { 
		switch (node.nodeType) { 
			case 3: 
			case 4: 
				return node.nodeValue; 
				break; 
			case 1: 
			case 11: 
				var innerText = ''; 
				for (var i = 0; i < node.childNodes.length; i++) { 
					innerText += getInnerText(node.childNodes[i]); 
				} 
				return innerText; 
				break; 
			default: 
				return ''; 
		} 
	} 
} 

// Helper function to return XML contained within a node
function serializeNode(node) { 
	if (typeof XMLSerializer != 'undefined') {
		return new XMLSerializer().serializeToString(node);
	}
	if (typeof node.xml != 'undefined') { 
		return node.xml; 
	} 
} 

var http = getHTTPObject(); // We create the HTTP Object
