
function triggerShippingLocation(currentLocationCode,withTrigger){
	
	//var url = '11_ajaxReturnXML.php';
	var url = 'AjaxWrapper_special.php';
	var pars = 'ajaxURL=shipping/controller/aj_shippingutil.php';	
	pars = pars + '&selectedLocationCode='+currentLocationCode;
	pars = pars + '&shippingAction=getShippingLocation';
		
	var myAjax = new Ajax.Request(
		encodeURI(url), 
		{
			method: 'get', 
			parameters: pars, 
			onCreate: Ajax_showProcessing(document.getElementById('div_ajaxProcessing')),
			onComplete: triggeredLocationAction 
		});
}


function triggeredLocationAction(originalRequest){
	//return xml
	
	Ajax_endProcessing(document.getElementById('div_ajaxProcessing'));	
	var repText = originalRequest.responseText;
	repText = repText.replace(/^\s*|\s*$/g, "");
	
	//repText="<resut><location><id>WMY</id><name>West Malaysia (Penisular)</name><isselected>0</isselected></location><location><id>EMY</id><name>East Malaysia (Sabah, Sarawak)</name><isselected>0</isselected></location><location><id>SG</id><name>Singapore</name><isselected>0</isselected></location><location><id>OTHER</id><name>Other (International)</name><isselected>0</isselected></location></resut>";
	
	var xmlDocument = "";
	try{ //Internet Explorer
	  xmlDocument=new ActiveXObject("Microsoft.XMLDOM");
	  xmlDocument.async="false";
	  xmlDocument.loadXML(repText);
  }
	catch(e){			
  	try{ //Firefox, Mozilla, Opera, etc.
    	parser=new DOMParser();
    	xmlDocument=parser.parseFromString(repText,"text/xml");
    }
  	catch(e) {
  		alert("Your Browser may not support Ajax. Please use IE and retry : "+e.message)
  	}
 	}
	
	// create select element
	var mytd=document.getElementById("td_location");
	
	myselect = document.createElement("select");
	myselect.setAttribute("name","txtShippingLocation");
	myselect.setAttribute("id","txtShippingLocation");
	
	myOnChange = new Function("e",
														"document.getElementById('tr_courier').style.display='none';"+
														"document.getElementById('tr_international').style.display='none';"+ 
														"if(myselect.selectedIndex!=''){"+
															"triggerDeliveryAgent(myselect.options[myselect.selectedIndex].value)"+
														"}");
	
	// insert empty option
	var theOption = document.createElement("OPTION");
	theText=document.createTextNode("");
	theOption.appendChild(theText);
	myselect.appendChild(theOption);
	
	var selectedindex = 0;
	
	var root = xmlDocument.getElementsByTagName('resut')[0];	
	
	if(root.getElementsByTagName("location")[0].firstChild){
		locationListObj = root.getElementsByTagName("location");
		
		for(var i=0;i<locationListObj.length;i++){
			locationObj = locationListObj[i];
			var id = locationObj.getElementsByTagName("id")[0].firstChild.nodeValue;
			var name = locationObj.getElementsByTagName("name")[0].firstChild.nodeValue;			
			var isselected = locationObj.getElementsByTagName("isselected")[0].firstChild.nodeValue;
			
			theOption = document.createElement("OPTION");
			theText=document.createTextNode(name);
			theOption.appendChild(theText);
			theOption.setAttribute("value",id);
			myselect.appendChild(theOption);
			if(isselected==1){
				selectedindex = i+1;
				selectedid = id;
			}
		}
	}
	
	mytd.appendChild(myselect);
	myselect.onchange=myOnChange;
	myselect.selectedIndex = selectedindex;
	
	if(selectedindex!=0){
		triggerDeliveryAgent(selectedid);
	}

}



function triggerDeliveryAgent(locationcode){
	var url = 'AjaxWrapper_special.php';
	var pars = 'ajaxURL=shipping/controller/aj_shippingutil.php';	
	pars = pars + '&locationcode='+locationcode;
	pars = pars + '&shippingAction=getAgent';
	
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onCreate: Ajax_showProcessing(document.getElementById('div_ajaxProcessing')),
			onComplete: triggeredAgentAction 
		});
}

function triggeredAgentAction(originalRequest){
	//return xml
	Ajax_endProcessing(document.getElementById('div_ajaxProcessing'));
	var repText = originalRequest.responseText;
	repText = repText.replace(/^\s*|\s*$/g, "");
		
	var xmlDocument = "";
	try{ //Internet Explorer			
	  xmlDocument=new ActiveXObject("Microsoft.XMLDOM");
	  xmlDocument.async="false";
	  xmlDocument.loadXML(repText);
  }
	catch(e){			
  	try{ //Firefox, Mozilla, Opera, etc.
    	parser=new DOMParser();
    	xmlDocument=parser.parseFromString(repText,"text/xml");
    }
  	catch(e) {
  		alert("Your Browser may not support Ajax. Please use IE and retry : "+e.message)
  	}
 	}
	
	var node = document.getElementById("slCourier");	
	if(node){
		node.parentNode.removeChild(node);
	}
	
	// create select element
	mytd=document.getElementById("td_courier");
	mycourierselect = document.createElement("select");
	
	mycourierselect.setAttribute("name","slCourier");
	mycourierselect.setAttribute("id","slCourier");
	
	
	myOnChange = new Function("e", 
											"shipCompanyId=mycourierselect.options[mycourierselect.selectedIndex].value;"+
											"document.getElementById('span_courier_note').innerHTML = document.getElementById('hdNote_'+shipCompanyId).value;"+
											"if(shipCompanyId!=''){"+
												"calculationMethod=document.getElementById('calculattion_'+shipCompanyId).value;"+
												"if(calculationMethod==2){showCountryOption('slCountry','td_international');}"+
											"}else{"+
													"document.getElementById('tr_international').style.display='none';"+
											"}");
	
	var theOption = document.createElement("OPTION");
	theText=document.createTextNode("");
	theOption.appendChild(theText);
	// insert empty option
	mycourierselect.appendChild(theOption);
	
	var selectedindex = 0;
	
	var root = xmlDocument.getElementsByTagName('resut')[0];	
	
	if(root.getElementsByTagName("shipping")[0].firstChild){	
		var shipperListObj = root.getElementsByTagName("shipping");
		
		for(var i=0;i<shipperListObj.length;i++){
			shipobj = shipperListObj[i];
			var id = shipobj.getElementsByTagName("id")[0].firstChild.nodeValue;
			var name = shipobj.getElementsByTagName("name")[0].firstChild.nodeValue;
			var calculationmethod = shipobj.getElementsByTagName("calculationmethod")[0].firstChild.nodeValue;
			var isdefault = shipobj.getElementsByTagName("isdefault")[0].firstChild.nodeValue;
			var note = shipobj.getElementsByTagName("note")[0].firstChild.nodeValue;
			
			if(isdefault==1){
				selectedindex = i;
				if(calculationmethod==2){
					showCountryOption('slCountry','td_international');
				}
				//put special note
				document.getElementById("span_courier_note").innerHTML = note;
			}
			
			theOption = document.createElement("OPTION");
			theText=document.createTextNode(name);
			theOption.appendChild(theText);
			theOption.setAttribute("value",id);
			mycourierselect.appendChild(theOption);
			
			//create hidden field
			var hdCalculation = document.createElement("input");
			hdCalculation.setAttribute("type", "hidden");
			hdCalculation.setAttribute("id", "calculattion_"+id);
			hdCalculation.setAttribute("value", calculationmethod);
			document.getElementById("form1").appendChild(hdCalculation);
			
			var hdNote = document.createElement("input");
			hdNote.setAttribute("type", "hidden");
			hdNote.setAttribute("id", "hdNote_"+id);
			hdNote.setAttribute("value", note);
			document.getElementById("form1").appendChild(hdNote);
			
		}
	}
	
	document.getElementById("tr_courier").style.display="";
	mytd.appendChild(mycourierselect);
	mycourierselect.onchange=myOnChange;
	mycourierselect.selectedIndex = selectedindex+1;
	
}


function showCountryOption(slObjectName,parentElement){
	document.getElementById("tr_international").style.display="";
	populateCountry('',slObjectName,parentElement);
}



