// JavaScript Document

/*AJAX DYNAMIC SCRIPTING*/
	var req;
	
	/*LOAD COUNTRY DROPDOWN*/
	
	function setdelay() {
		setTimeout("loadDistance()", 500);	
	}
	
	
	
	function loadCountry(url) {
		
	   /* 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 = processCountry;
		  req.open("GET", url, true);
		  req.send('');
	   }
	   
	   setTimeout("testload()", 1000);
	   /*if(document.fcl.DCountry.value!=null){
		   if (document.fcl.OPort.value!=''&&document.fcl.OPort.value!='0'&&document.fcl.DCountry.value!=''&&document.fcl.DCountry.value!='0'){
			loadDport('http://140.174.69.243/services/parts/fcl/fcl.dport.asp?id='+document.fcl.OPort.value+'&co='+document.fcl.DCountry.value+'&Act='+document.fcl.DPort.value);
		   }
	   }*/
	}
	
	function testload() {
	   	if (document.fcl.OPort.value!=''&&document.fcl.OPort.value!='0'&&document.fcl.DCountry.value!=''&&document.fcl.DCountry.value!='0'){
			loadDport('/services/parts/fcl/fcl.dport.asp?id='+document.fcl.OPort.value+'&co='+document.fcl.DCountry.value+'&Act='+document.fcl.DPort.value);
	   	}
	}
	
	function processCountry(evt) {
	//alert ("readyState - "+req.readyState + "status - "+req.status);
	// The page has loaded and the HTTP status code is 200 OK
	   if (req.readyState == 4) {
		  if ((req.status == 200) || (req.status == 0)) {
	
			  // Write the contents of this URL to the searchResult layer
			  if (req.responseText.match('Please Select') == null){
				getObject("DCountryDiv").innerHTML = req.responseText;
				loadDistance();
			  }else{
				getObject("DCountryDiv").innerHTML = "There is a error";
			  }
			  
		}
	 }
	}
	
	/*LOAD DESTINATION PORT*/
	function loadDport(url) {
		
	   /* 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) {
	//alert ("readyState - "+req.readyState + "status - "+req.status);
	// The page has loaded and the HTTP status code is 200 OK
	   if (req.readyState == 4) {
		  if ((req.status == 200) || (req.status == 0)) {
	
			  // Write the contents of this URL to the searchResult layer
			  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) {
		//alert ("readyState - "+req.readyState + "status - "+req.status);
		// The page has loaded and the HTTP status code is 200 OK
		//alert(evt);
		if (req.readyState == 4) {
			if ((req.status == 200) || (req.status == 0)) {
				// Write the contents of this URL to the searchResult layer
				if (req.responseText.match('Please Select') == null){
					start = document.fcl.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.fcl.OZipCode.value+"</b> to <b>"+end+"</b>";
						GUnload();
						load();
						//document.getElementById('Distance').innerHTML = 'zipcode';
					}else{
						document.fcl.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) {
	//alert ("readyState - "+req.readyState + "status - "+req.status);
	// The page has loaded and the HTTP status code is 200 OK
	   if (req.readyState == 4) {
		  if ((req.status == 200) || (req.status == 0)) {
	
			  // Write the contents of this URL to the searchResult layer
			  if (req.responseText.match('Please Select') == null){
				getObject("DCountryDiv").innerHTML = req.responseText;
				loadDport(RLDport);
				setTimeout("loadDistance()", 2000);
			  }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.fcl.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.fcl.Dist.value = distance
				document.getElementById('Distance').innerHTML = 'The estimated driving distance between pickup zip code and port is <b>'+distance+' miles</b>';
			} else {
				document.fcl.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="4" />';
			} 

		  });
		  dirn.loadFromWaypoints([start,end],{getSteps:true}); 
		  
		}
			
	function loadDistance() {
		if(document.fcl.OPort.value!="0") {
			if (isZip(document.fcl.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/fcl/get.port.zip.asp?id='+document.fcl.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}$)|(^\d{5}-\d{4}$)/);
		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;
	}
	
	function getObject(name) {
	
		if (document.getElementById) {
			//alert('getEelementByID');
			return document.getElementById(name);
		
		} else if (document.all) {
			//alert('all');
			return document.all[name];
		
		} else if (document.layers) {
			//alert('layers');
			return document.layers[name];
		} 
	}
	
	
