// JavaScript Document

/*AJAX DYNAMIC SCRIPTING*/
	var req;
	
	/*LOAD COUNTRY DROPDOWN*/
	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()", 500);
	}
	
	function testload() {
	   	if (eval("document."+FN+".OPort.value!=''")&&eval("document."+FN+".OPort.value!='0'")&&eval("document."+FN+".DCountry.value!=''")&&eval("document."+FN+".DCountry.value!='0'")){
			loadDport('/services/parts/lcl/lcl.dport.asp?id='+eval("document."+FN+".OPort.value")+'&co='+eval("document."+FN+".DCountry.value")+'&Act='+eval("document."+FN+".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) {
	   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
		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.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>"+eval("document."+FN+".OZipCode.value")+"</b> to <b>"+end+"</b>";
						GUnload();
						load();
						//document.getElementById('Distance').innerHTML = 'zipcode';
					}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 reloadct(){
		
	   // FILL RESPONSE DIVS WITH LOADING MSG
	   getObject("DCountryDiv").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 = processReloadCountry;
		  req.open("GET", RLCountry, true);
		  req.send('');
	   }
	}
	
	function processReloadCountry(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){
				getObject("DCountryDiv").innerHTML = req.responseText;
				loadDport(RLDport);
				//setTimeout("loadDistance()", 2000);
			  }else{
				getObject("DCountryDiv").innerHTML = "There is a error";
			  }
			  
		}
	 }
	}
	
	
	/* END OF AJAX DYNAMIC SCRIPTING*/
	/*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');}
			}
		}
	}
	
	
	function load() {
		  var dirn = new GDirections();
		  
		  GEvent.addListener(dirn,"error", function() {
			//GLog.write("Failed: "+dirn.getStatus().code);
			//document.lcl.Dist.value = "0"
			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.lcl.Dist.value = 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}$)|(^\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];
		} 
	}
	
	