var errorIcon = '<img src="/assets/images/icons/error.gif" width="12" height="12" alt="Error">';

function validate(f, errorDiv) {
	var returnVal = true;
	if (! isValidCombo(f)){
		var errMsg = "Please enter EITHER a valid zip/postal code <b>OR</b> city and state/province.";
		
		/* turning off ajax calls for now because Akamai is interfering with browser-sniffing and so IE is not working
		if(document.getElementById ){ // make sure browser support exists
			var err = document.getElementById(errorDiv);
			if(err){ // make sure element exists
				err.innerHTML = errorIcon + " " + errMsg;
			}
		}else{*/		
			alert(errMsg);
		//}
		returnVal = false;
	
	/*turning off ajax calls for now because Akamai is interfering with browser-sniffing and so IE is not working
	}else if (document.getElementById && document.createTextNode){ // if supported, make an ajax call to get the directions. Otherwise, return true, which will cause form to be submitted and directions retrieved traditionally
		getDirections(f);
		returnVal = false;*/
	}
	return returnVal;
}


function getDirections(f){ // AJAX - get directions
	
	var directionsDiv = document.getElementById("directions");
	var params = new Array();
	
	var startAddress = new Object();
	startAddress.address = f.address.value;
	startAddress.city = f.city.value;
	startAddress.state = f.state.value;
	startAddress.postalcode = f.zip.value;
	startAddress.latitude = f.state.shopLatitude;
	startAddress.longitude = f.zip.shopLongitude;

	var endAddress = new Object();
	endAddress.address = f.shopAddress.value;
	endAddress.city = f.shopCity.value;
	endAddress.state = f.shopState.value;
	endAddress.postalcode = f.shopZip.value;
	endAddress.latitude = f.shopLatitude.value;
	endAddress.longitude = f.shopLongitude.value;

	params[0] = startAddress;
	params[1] = endAddress;

	wddxSerializer = new WddxSerializer();
	wddxparams = wddxSerializer.serialize(params);
	wddxparams = "pois="+wddxparams;
	http('POST','/assets/components/scoop_shops/locator.cfc?method=getDirectionsWddx',directions_response, wddxparams);
	directionsDiv.innerHTML = '<img src="/assets/images/icons/bigrotation2.gif" width="32" height="32" alt="Loading directions..." align="absmiddle"> Loading directions...';  
}

function directions_response(obj){ // AJAX - display the directions
	if (navigator.userAgent.indexOf('MSIE') != -1){
		var wddxDeserializer = new WddxDeserializerForIE();
	}else{
		var wddxDeserializer = new WddxDeserializer();
	}
	
	var directionsDiv = document.getElementById("directions");
	var objResponse = wddxDeserializer.deserialize(obj);
	var route = objResponse.ROUTE;	
	var aSteps = route.MANEUVER;
	var mapImg = route.MAP.CONTENT;
	var mapWidth = route.MAP.SIZE[0];
	var mapHeight = route.MAP.SIZE[1];
	
	var dirHtml = '<img id="dirMap" src="' + mapImg + '" width="' + mapWidth + '" height="' + mapHeight + '" alt="Directions Map" border="1" vspace="7"><br>';

	dirHtml += '<b>Directions:</b><br>';
	dirHtml += '<table>';
	for(var i=0;i<aSteps.length;i++){
		var stepNum = i+1;
		dirHtml += '<tr><td valign="top"><strong>' + stepNum + '.</strong></td><td valign="top" width="300">' + aSteps[i].TEXT + '</td><td width="20">&nbsp;</td><td valign="top">';
		if (aSteps[i].MILES > 0) {
			dirHtml += aSteps[i].MILES + ' miles (' + miToKm(aSteps[i].MILES) + ' Km)';
		}
		dirHtml += '</td></tr>';			
	}
	dirHtml += '</table><br>';
	dirHtml += '<strong>Estimated time: ' + route.TOTAL_TIME + '&nbsp;&nbsp;&nbsp;&nbsp; Total distance: ' + route.TOTAL_MILES + ' miles (' + miToKm(route.TOTAL_MILES) + ' Km)</strong>';
	directionsDiv.innerHTML = dirHtml;
}

function checkValidCombo() {
	var f = document.forms[0];
	if (isValidCombo(f)){
		clearError();
	}
}

function isValidCombo(f){ // maqke sure they have entered EITHER a zip code OR a city and state
	var isValidZip = stripSpaces(f.zip.value).length >= 5 ? true : false;
	var cityEntered = stripSpaces(f.city.value).length > 0 ? true : false;
	var stateSelected = f.state.options[f.state.selectedIndex].value == '' ? false : true;
	var isValidCombo = (isValidZip || (cityEntered && stateSelected)) ? true : false; 
	return isValidCombo;
}

function clearError(){
	if(document.getElementById){		
		var err = document.getElementById("errorMsg");
		if(err){
			err.innerHTML = " ";
		}
	}
}

function zoom(val){
	f = document.forms["map"];
	f.panType.value = "zoom";
	f.panValue.value = val; 
	f.submit();
}

function miToKm(mi){
	var km = mi * 1.609344;
	return km.toFixed(2);
}