// JavaScript Document

/*AJAX DYNAMIC SCRIPTING*/
	var req;
	
	/*LOAD COUNTRY DROPDOWN*/
	function loadCountry() {
		var url = '/services/parts/dta/dta.dcountry.asp?id='+document.TRForm.OAirport.value+'&Act='+document.TRForm.DCountry.value+'&shipper='+document.TRForm.KnownShipper[1].checked;
		
	   /* FILL RESPONSE DIVS WITH LOADING MSG*/	
	   getObject("DCountryDiv").innerHTML = "<img src=\"/images/framework/loading_animation.gif\" align=\"absmiddle\"> Loading Please Wait ";
	   getObject("DportDiv").innerHTML = "<select name='DAirport' class='field'><option value='0' disabled>Select</option></select>";
	   
	   // Internet Explorer
	   try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
	   catch(e) {
		  try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
		  catch(oc) { req = null; }
		  
	   }
	
	   // Mozailla/Safari
	   if (req == null && typeof XMLHttpRequest != "undefined") {
		  req = new XMLHttpRequest();
	   }
	
	   // Call the processChange() function when the page has loaded
	   if (req != null) {
		  req.onreadystatechange = processCountry;
		  req.open("GET", url, true);
		  req.send('');
	   }
	   
	   setTimeout("testload()", 500);
	}
	
	function testload() {
	   	if (document.TRForm.OAirport.value!=''&&document.TRForm.OAirport.value!='0'&&document.TRForm.DCountry.value!=''&&document.TRForm.DCountry.value!='0'){
			loadDport();
	   	}
	}
	
	function processCountry(evt) {
		if (req.readyState == 4) {
			if ((req.status == 200) || (req.status == 0)) {
				if (req.responseText.match('Please Select') == null){
					getObject("DCountryDiv").innerHTML = req.responseText;
				}else{
					getObject("DCountryDiv").innerHTML = "There is a error";
				}
			}
		}
	}
	
	/*LOAD DESTINATION PORT*/
	function loadDport() {
		
		url = '/services/parts/dta/dta.dairport.asp?id='+document.TRForm.OAirport.value+'&co='+document.TRForm.DCountry.value+'&Act='+document.TRForm.DAirport.value+'&shipper='+document.TRForm.KnownShipper[1].checked;
		
	   /* FILL RESPONSE DIVS WITH LOADING MSG*/	
	   getObject("DportDiv").innerHTML = "<img src=\"/images/framework/loading_animation.gif\" align=\"absmiddle\"> Loading Please Wait ";
	   
	   // Internet Explorer
	   try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
	   catch(e) {
		  try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
		  catch(oc) { req = null; }
		  
	   }
	
	   // Mozailla/Safari
	   if (req == null && typeof XMLHttpRequest != "undefined") {
		  req = new XMLHttpRequest();
	   }
	
	   // Call the processChange() function when the page has loaded
	   if (req != null) {
		  req.onreadystatechange = processDport;
		  req.open("GET", url, true);
		  req.send('');
	   }
	}
	
	function processDport(evt) {
		if (req.readyState == 4) {
			if ((req.status == 200) || (req.status == 0)) {
				if (req.responseText.match('Please Select') == null){
					getObject("DportDiv").innerHTML = req.responseText;
				}else{
					getObject("DportDiv").innerHTML = "There is a error";
				}
			}
		}
	}
	
	/*LOAD PORT ZIPCODE*/
	function loadPortzip(url) {
		
	   /* FILL RESPONSE DIVS WITH LOADING MSG*/	
	   getObject("Distance").innerHTML = "<img src=\"/images/framework/loading_animation.gif\" align=\"absmiddle\"> Getting Port Zipcode ";
	   
	   // Internet Explorer
	   try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
	   catch(e) {
		  try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
		  catch(oc) { req = null; }
		  
	   }
	
	   // Mozailla/Safari
	   if (req == null && typeof XMLHttpRequest != "undefined") {
		  req = new XMLHttpRequest();
	   }
	
	   // Call the processChange() function when the page has loaded
	   if (req != null) {
		  req.onreadystatechange = processPortzip;
		  req.open("GET", url, true);
		  req.send('');
	   }
	}
	
	function processPortzip(evt) {
		if (req.readyState == 4) {
			if ((req.status == 200) || (req.status == 0)) {
				if (req.responseText.match('Please Select') == null){
					start = document.getElementById('OZipCode').value;
					end = req.responseText;
					if(isZip(end)){
						getObject("Distance").innerHTML = "<img src=\"/images/framework/loading_animation.gif\" align=\"absmiddle\"> Calculating driving distance from <b>"+document.lcl.OZipCode.value+"</b> to <b>"+end+"</b>";
						GUnload();
						load();
					}else{
						document.getElementById('Dist').value = "0";
						getObject('Distance').innerHTML = 'Sorry we where not able to calculate the driving distance automaticly, please enter the driving distance between pickup zip code and port <input name="DistMan" type="text" class="field3" id="DistMan" value="'+DistMan+'" size="4" />';
					}
				}
			}
		}
	}
	
	function ReloadCountry() {
		
	   /* FILL RESPONSE DIVS WITH LOADING MSG*/	
	   getObject("DCountryDiv").innerHTML = "<img src=\"/images/framework/loading_animation.gif\" align=\"absmiddle\"> Loading Please Wait ";
	   //getObject("DportDiv").innerHTML = "<select name='DPort' class='field'><option value='0' disabled>Select</option></select>";
	   
	   // Internet Explorer
	   try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
	   catch(e) {
		  try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
		  catch(oc) { req = null; }
		  
	   }
	
	   // Mozailla/Safari
	   if (req == null && typeof XMLHttpRequest != "undefined") {
		  req = new XMLHttpRequest();
	   }
	
	   // Call the processChange() function when the page has loaded
	   if (req != null) {
		  req.onreadystatechange = processReloadCountry;
		  req.open("GET", RLCountry, true);
		  req.send('');
	   }
	}
	
	function processReloadCountry(evt) {
		if (req.readyState == 4) {
			if ((req.status == 200) || (req.status == 0)) {
				if (req.responseText.match('Please Select') == null){
					getObject("DCountryDiv").innerHTML = req.responseText;
					loadDport(RLDport);
				}else{
					getObject("DCountryDiv").innerHTML = "There is a error";
				}
			
			}
		}
	}
	
	
	/* END OF AJAX DYNAMIC SCRIPTING*/
	
	function load() {
		  var dirn = new GDirections();
		  
		  GEvent.addListener(dirn,"error", function() {
			//GLog.write("Failed: "+dirn.getStatus().code);
			document.getElementById('Dist').value = "0";
			document.getElementById('Distance').innerHTML = 'Sorry we where not able to calculate the driving distance automaticly, please enter the driving distance between pickup zip code and port <input name="DistMan" type="text" class="field3" id="DistMan" value="'+DistMan+'" size="4" />';
		  });
		
		  GEvent.addListener(dirn,"load", function() {
			distance = dirn.getDistance().meters
			
			if(distance != null && distance != 0){
				distance = (distance /1609.344); // distance in miles
				distance = Math.round(distance);
				document.getElementById('Dist').value = distance;
				document.getElementById('Distance').innerHTML = 'The estimated driving distance between pickup zip code and port is <b>'+distance+' miles</b>';
			} else { 
				document.getElementById('Dist').value = "0";
				document.getElementById('Distance').innerHTML = '2Sorry we where not able to calculate the driving distance automaticly, please enter the driving distance between pickup zip code and port <input name="DistMan" type="text" class="field3" id="DistMan" value="<%= HtmlEscape(DistMan)%>" size="6" />';
			} 

		  });
		  dirn.loadFromWaypoints([start,end],{getSteps:true}); 
		  
	}
			
	function loadDistance() {
		if(document.getElementById('OPort').value!="0") {
			if (isZip(document.getElementById('OZipCode').value)) {
				
				/* FILL RESPONSE DIVS WITH LOADING MSG*/
			   getObject("Distance").setAttribute("class", "Distance");
			   getObject("Distance").innerHTML = "<img src=\"/images/framework/loading_animation.gif\" align=\"absmiddle\"> Getting Port Zipcode ";
			   url = 'parts/lcl/get.port.zip.asp?id='+document.getElementById('OPort').value;
			   
			   // Internet Explorer
			   try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
			   catch(e) {
				  try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
				  catch(oc) { req = null; }
				  
			   }
			
			   // Mozailla/Safari
			   if (req == null && typeof XMLHttpRequest != "undefined") {
				  req = new XMLHttpRequest();
			   }
			
			   // Call the processChange() function when the page has loaded
			   if (req != null) {
				  req.onreadystatechange = processPortzip;
				  req.open("GET", url, true);
				  req.send('');
			   }
	
			}else{
				getObject("Distance").setAttribute("class", "Distance");
			    getObject("Distance").innerHTML = "Please enter a valid US Or CA Pickup Zip Code ";
			}
		}else{
			getObject("Distance").setAttribute("class", "DistanceHide");
			getObject('Distance').innerHTML = '';
		}
	}
	
	
	/*GENERAL FUNCTIONS*/
//	function isZip(s) {
//		reZip = new RegExp(/(^\d{5}$)/);
//		reg_exp_pcode = new RegExp(/(^s*([a-z](\s)?\d(\s)?){3}$)s*/i);
//		
//		 if (!reZip.test(s)&&!reg_exp_pcode.test()) {
//			  //alert("Pickup Zip Code Is Not Valid");
//			  return false;
//		 }
//		
//		return true;
//	}
	

	
	/*
	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	Copy data between forms
	XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	*/
	function copyover(objname,objType){
		if(objname.name=="OLocation"){	
			if(objname.value=="Business without dock"||objname.value=="Residence"){
				slidedown('OCPick');
			}else{
				slideup('OCPick');
			}
		}
		if(objname.name.indexOf('Haz')==0){
			if(objname.checked){
				slidedown('ocHazPart');
			}else{
				var se=true;
				for (i=0;i<=StartLines;i++){	//-> Loop through form Lines
					if(eval("document."+FN+".Haz["+i+"].checked")) {se=false;}
				}
				if(se){slideup('ocHazPart');}
			}
		}
	}


