var xajaxcall = null;
var prompted = false;

function doPickupCall() {
	 if (xajaxcall == null || xajaxcall.readyState == 0 || xajaxcall.readyState == 4) {
	 	try {
	         if (window.XMLHttpRequest){
	            xajaxcall = new XMLHttpRequest();
	         } else {
	            xajaxcall = new ActiveXObject('Microsoft.XMLHTTP');
	         }
	         // Set the JavaScript function that will act as a callback for
	         // any events the instance fires.
	         xajaxcall.onreadystatechange = doCallHandler;
	         // Set the target URI for the request. Note that we append a
	         // value that will ensure that the URL is always unique.
	         // This is to deal with caching issues in IE.
		     
	         target = "http://" + window.location.hostname + ":" + window.location.port + "/webapp/PickupRequest/zipChecker?zips=" + getZips();
	         //alert(target);
			 //target = "http://localhost:9080/webapp/PickupRequest/zipChecker?zips=" + getZips();
	         xajaxcall.open("post", target, true);
	         xajaxcall.send(null);
	 
	      } catch(e) {
	         alert("Error in doCall() - " + e.message);
	      }
	 
	 }
	
}

function doCallHandler () {
	//alert("Here1");
	if (xajaxcall.readyState == 4) {
		//alert("Here2");
		if (xajaxcall.status == 200) {
			//alert("Here3");
			msgDOM = xajaxcall.responseXML;
      		root = msgDOM.getElementsByTagName("message")[0];
      		result = root.getElementsByTagName("result");
      		for (i = 0; i < result.length; i++) {
         		value1 = result[i];
        	   	outValue = value1.getAttribute("value1");
	      	}
	      	
	      	if (!prompted) {
		      	if (outValue == "true") {
		      		prompted = true;
		      		var promptText="" + 
		      		"A part of this shipment is going to Canada.  Please enter the customs broker information\n" + 
		      		"in to the Special Instruction section. If the customs broker information is not available\n" +
		      		"at this time, please provide this information on the Bill of Lading.\n\n" +
		      		"\tClick:\n" +
		      		"\tOk - To continue submitting the Pickup Request.\n" +
		      		"\tCancel - To enter the custom broker information.";
		      		var ret = confirm(promptText);
		      		if (ret) {
		      			doPickupValidation();
		      		}
		      	}else {
		      		doPickupValidation();
		      	}
	      	}else {
	      		doPickupValidation();
	      	}
      	}
    }
}

function getZips(){
	//alert("here");
	var num = (document.getElementById("theValue").value -1)+ 1;
	var outString = "";
	for (var i = 0; i <= num; i=i+1) {
		try {
			if (document.forms[1].detaildestzip[i].value != null) {
				if (document.forms[1].detaildestzip[i].value != "") {
					if (outString == "") {
						outString = document.forms[1].detaildestzip[i].value
					}else{
						outString = outString + "+" + document.forms[1].detaildestzip[i].value
					}
				}
			}
		} catch (e) {
			//do nothing
		}
	}
	return outString;
}
