
var no_delivery_methods_found = document.getElementById("no_delivery_methods_found");
if(no_delivery_methods_found) {
    no_delivery_methods_found.style.display = "block";
}

var delivery_method_data = document.getElementById("delivery_method_data");
if(delivery_method_data) {
    delivery_method_data.style.display = "none";
}

// retrieves the XMLHttprequest object
	function createXmlHttpRequestObject()
	{
	    
	    // will store the reference to the XMLHttpRequest object
	    var xmlHttp;
	    // if running Internet Explorer
	    if(window.ActiveXObject)
	    {
		try {
		    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
		    xmlHttp = false;
		}
	    }
	    
	    // if running mozilla or other browsers
	    
	    else {
		
		try {
		    
		    xmlHttp = new XMLHttpRequest();
		    
		} catch (e) {
		    
		    xmlHttp = false;
		    
		}
		
	    }
	    
	    // return the created object or display an error message
	    if(!xmlHttp) {
		alert("Error creating the XMLHttpRequest object.");
		return false;
	    } else {
		return xmlHttp;
	    }
	}


	// stores the reference to the XMLHttpRequest object
	var xmlHttp = createXmlHttpRequestObject();

	var country_select = document.getElementById('country_select');
	var delivery_method_select = document.getElementById('delivery_method_select');
	
	var country_value = '';
	
	function updateCountryValue() {
	    if(country_select) {
		country_value = country_select.value;
	    }
	}
	
	function resetDeliveryList() {
	    if(delivery_method_select) {
		while(delivery_method_select.firstChild) {
			delivery_method_select.removeChild(delivery_method_select.firstChild);
		}
	    }
	    if(delivery_method_data) {
		delivery_method_data.style.display = 'none';
	    }
	    if(no_delivery_methods_found) {
		no_delivery_methods_found.style.display = 'block';
	    }
	    stop_auto_submit = true;
	}
		
	
	function getDeliveryMethodList(stop_auto_submit) {
		updateCountryValue();
		
		if(country_value == '0') {
			resetDeliveryList();
			return false;
		} else {
			// proceed only if the xmlHttp object isn't busy
			if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0) {
			    xmlHttp.open("GET", "_load_delivery_methods.php?iso="+country_value, true);
			    //define the method to handle server response
			    if(stop_auto_submit) {
				xmlHttp.onreadystatechange = handleDeliveryMethodList_NoSubmit;
			    } else {
				xmlHttp.onreadystatechange = handleDeliveryMethodList_Submit;
			    }
			    // post variables
			    xmlHttp.send(null);
			} else {
			    alert("Sorry, the list of delivery methods could not be loaded.  You can proceed to checkout and select your delivery method later.");
			}
			return false;
		}
	}
	
	function handleDeliveryMethodList_Submit() {
	    handleDeliveryMethodList(false);
	}
	function handleDeliveryMethodList_NoSubmit() {
	    handleDeliveryMethodList(true);
	}

	// executed automatically when a message is receieved from the server
	function handleDeliveryMethodList(stop_auto_submit) {
		// move forward only if the transaction has completed
		if(xmlHttp.readyState == 4) {
			// status of 200 indicates the transaction completed successfully
			if(xmlHttp.status == 200) {
				// extract the XML retrieved from the server
				var xmlResponse = xmlHttp.responseXML;
				// obtain the document element (the root element) of the XML structure
				var xmlDocumentElement = xmlResponse.documentElement;
				var methods = xmlDocumentElement.getElementsByTagName('method');
				
				resetDeliveryList();
				
				new_option = document.createElement('option');
				new_option.value = '';
				new_text_node = document.createTextNode("-- Please choose --");
				new_option.appendChild(new_text_node);
				if(delivery_method_select) {
				    delivery_method_select.appendChild(new_option);
				}
				
				for (j=0;j<methods.length;j++) {
				    new_option = document.createElement('option');
				    new_option.value = methods[j].getAttribute('id');
				    new_text_node = document.createTextNode(methods[j].firstChild.nodeValue);
				    new_option.appendChild(new_text_node);
				    if(delivery_method_select) {
					delivery_method_select.appendChild(new_option);
				    }
				}
				
				if(methods.length > 0) {
				    if(no_delivery_methods_found) {
					no_delivery_methods_found.style.display = "none";
				    }
				    if(delivery_method_data) {
					delivery_method_data.style.display = "";
				    }
				    /* If there is only one method, submit the form automatically for the user */
				    if(methods.length == 1) {
                                        if(stop_auto_submit === false) {
                                            var last_index = parseInt(delivery_method_select.childNodes.length) - 1;
                                            delivery_method_select.childNodes.item(last_index).selected = "selected";
					
					    submitBasketForm();
					}
				    }
				} else {
				    resetDeliveryList();
				    if(no_delivery_methods_found) {
					no_delivery_methods_found.style.display = "";
				    }
				}

			} else {
				alert('Sorry, the list of delivery methods could not be loaded.  You can proceed to checkout and select your delivery method later.');
			}
		}
	}


	function submitBasketForm() {
		for(k=0;k<document.forms.length;k++) {
			if(document.forms[k].id == "basket-form") {
				frm = document.forms.item(k);
				frm.submit();
			}
		}
	}
	
	function getDeliveryMethodList_NoSubmit() {
	    getDeliveryMethodList(true);
	}
	
	function getDeliveryMethodList_Submit() {
	    getDeliveryMethodList(false);
	}
	
	window.onload = function() { getDeliveryMethodList_NoSubmit(); }
	
	if(country_select) {
	    country_select.onchange = getDeliveryMethodList_Submit;
	}
	
	if(delivery_method_select) {
	    delivery_method_select.onchange = submitBasketForm;
	}
	
