/**
 * @author Kirk Ouimet
 * @website http://www.yougetsignal.com/tools/phone-location/
 * @copyright 2008 Kirk Ouimet Design. All rights reserved.
 */

/* Initialize the Google Map
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
var map;
function loadMap() {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.enableContinuousZoom();
		map.enableDoubleClickZoom();
		map.enableScrollWheelZoom();
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		gKeyboardHandler = new GKeyboardHandler(map);
		map.setCenter(new GLatLng(0, 0), 1);
		// Fix the page scroll on zoom issue
		GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
		map.getContainer().onmousewheel = wheelevent; 		
	}
}

/* Clear the map
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function clearMap() {
	// Pan to the center of the map and clear all overlay markers
	map.setCenter(new GLatLng(0, 0), 1);
	map.clearOverlays();
}

/* AJAX request to get phone location information
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
var ajaxRequest	= new Ajax.Request('', {});
var phoneLocationJsonData;
var gLatLng = new GLatLng(0, 0);
var phoneDescription = "";
var phoneType = "";
function getPhoneLocation(phoneNumber) {
	// Validate the phone number
	if(isValidPhoneNumber(phoneNumber)) {
		document.getElementById('statusDescription').innerHTML = "<img src=\"/img/loader.gif\" alt=\"Please wait...\" title=\"Please wait...\" style=\"width: 2.6875em; height: 0.6875em;\" /> Locating phone number...";
		ajaxRequest.transport.abort(); // Cancel the previous request
		var url = "/tools/phone-location/php/get-phone-location-json.php";
		new Ajax.Request(url, {
			method: 'post',
			parameters: {'phoneNumber': phoneNumber},
			onFailure: function(transport) {
				document.getElementById('statusDescription').innerHTML = "<p><img src=\"/img/flag_red.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;<span style=\"color: #DF454B;\">Service currently unavailable.</span></p>";
			},
			onException: function(transport) {
				document.getElementById('statusDescription').innerHTML = "<p><img src=\"/img/flag_red.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;<span style=\"color: #DF454B;\">Service currently unavailable.</span></p>";
			},						
			onSuccess: function(transport) {
				phoneLocationJsonData = transport.responseText.evalJSON(); // Place the results into JSON

				if(phoneLocationJsonData.status != "Fail") {		
					// Center on new address
					
					gLatLng = new GLatLng(phoneLocationJsonData.lat, phoneLocationJsonData.lng);
					map.setZoom(4);
					map.panTo(gLatLng);
				
					// Create a marker
					if(phoneLocationJsonData.type == "L") {
						phoneType = "Landline";
					}
					else if(phoneLocationJsonData.type == "W") {
						phoneType = "Wireless";						
					}
					phoneDescription = phoneLocationJsonData.city + ", " + phoneLocationJsonData.state + " (" + phoneType + ")";

					if(phoneLocationJsonData.type == "Unknown") {
						phoneType = "Estimate";
						phoneDescription = phoneLocationJsonData.state;
					}

					map.clearOverlays(); // Clear all previous markers off of the map
					var markerHTML = phoneDescription;
					placeMarker(gLatLng, markerHTML);

					document.getElementById('statusDescription').innerHTML = "<img src=\"/img/flag_green.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;" + phoneDescription;
				}
				else {
					clearMap();
					document.getElementById('statusDescription').innerHTML = "<img src=\"/img/flag_red.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;Phone number could not be located.";
				}
			}
		});
	}
	else { // Invalid phone number
		document.getElementById('statusDescription').innerHTML = "<img src=\"/img/flag_red.gif\" alt=\"\" style=\"height: 1em; width: 1em;\" />&nbsp;Please enter a ten digit phone number.";
	}
}

/* Function to place a new marker on the map
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function placeMarker(point, html) {
	var gMarker = new GMarker(gLatLng);
	map.addOverlay(gMarker);
	GEvent.addListener(gMarker, "click", function() {
		gMarker.openInfoWindowHtml(html);
	});
}

/* Function to validate an area code
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function isValidPhoneNumber(phoneNumber) {
	var stripped = phoneNumber.replace(/[\(\)\.\-\ ]/g, ''); // Strip out acceptable non-numeric characters
	if(isNaN(parseInt(stripped)) || (stripped.length != 10)) {
		return false;
	}
	else {
		return true;
	}	
}

/* Submit AJAX request using enter key
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function submitUsingEnter(e) {
	var characterCode;
	if(e && e.which){ // If which property of event object is supported (NN4)
		e = e;
		characterCode = e.which // Character code is contained in NN4's which property
	}
	else {
		e = event;
		characterCode = e.keyCode; // Character code is contained in IE's keyCode property
	}
	
	if(characterCode == 13){ // If generated character code is equal to ASCII 13 (the enter key)
		getPhoneLocation(document.getElementById('phoneNumber').value);
		return false;
	}
	else {
		return true;
	}
}

/* Function to fix the page scroll issue with map zoom
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function wheelevent(e) {
	if (!e){
		e = window.event
	}
	if (e.preventDefault){
		e.preventDefault()
	}
	e.returnValue = false;
}