/**************************************************************************************************************/
// Create xmlhttp object based on browsertype
function getHTTPObject() {
	if (window.XMLHttpRequest)
	{
		  // If IE7, Mozilla, Safari, etc: Use native object
		  var xmlHttp = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject)
		{
		  // ...otherwise, use the ActiveX control for IE5.x and IE6
		  var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
/****** INIT APPLICATION VARIABLES ***********************/
var marker;
var map;
var customerHttp = getHTTPObject();
var requestHttp = getHTTPObject();
var zipHttp = getHTTPObject();
var geoHttp = getHTTPObject();
var customerBusy = false;
var requestBusy = false;
var geoBusy = false;
var icon = new GIcon();
var lastFocused = '';

icon.image = "../images/point-red.png";
icon.shadow = "../images/point-shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);
/******* HIGHLIGHT INPUT FIELDS *******************************/
function addHighlights()
{
	var liveTable = document.getElementById('liveTable');
   	var inputs = liveTable.getElementsByTagName("input");
	var textareas = liveTable.getElementsByTagName("textarea");
	var selects = liveTable.getElementsByTagName("select");

	var numInputs = inputs.length;
	var numTextAreas = textareas.length;
	var numSelects = selects.length;

	for ( i=0; i < numInputs; i++ )
	{
		var currentInput = inputs[i];
		currentInput.onfocus = onFocus;
		currentInput.onblur = onBlur;
		currentInput.onmouseover = onOver;
		currentInput.onmouseout = onOut;
	}

	for ( i=0; i < numTextAreas; i++ )
	{
		var currentTextArea = textareas[i];
		currentTextArea.onfocus = onFocus;
		currentTextArea.onblur = onBlur;
		currentTextArea.onmouseover = onOver;
		currentTextArea.onmouseout = onOut;
	}

	for ( i=0; i < numSelects; i++ )
	{
		var currentSelect = selects[i];
		currentSelect.onfocus = onFocus;
		currentSelect.onblur = onBlur;
		currentSelect.onmouseover = onOver;
		currentSelect.onmouseout = onOut;
	}
}
function onFocus()
{
	if (this.type != "button")
	{
		lastFocused = this;
		this.style.background = '#FFFFFF';
		this.style.border = '1px solid #C00000';
		this.style.color = '#0000C0';
	}
}
function onBlur()
{
	if (this.type != "button")
	{
		this.style.background = '#ECECEC';
		this.style.border = 'solid 1px #B0B0B0';
		this.style.color = '#000000';
	}
}
function onOver()
{
	if (this.type != "button" && this != lastFocused)
	{
		this.style.background = '#F8F8F8';
	}
	else if (this.type == "button")
	{
		this.style.background = '#ECECEC';
	}
}
function onOut()
{
	if (this.type != "button" && this != lastFocused)
	{
		this.style.background = '#ECECEC';
		this.style.border = 'solid 1px #B0B0B0';
		this.style.color = '#000000';
	}
	else if (this.type == "button")
	{
		this.style.background = '#FFFFFF';
	}
}
/******* APPLICATION FUNCTIONS  *******************************************************************************/
function getCustomers()
{
	if (!customerBusy)
	{
		var firstName = document.getElementById('firstName').value;
		var lastName = document.getElementById('lastName').value;
		var phone1 = document.getElementById('phone1').value;
		var phone2 = document.getElementById('phone2').value;
		var phone3 = document.getElementById('phone3').value;		
		var address = document.getElementById('address').value;
		var url = "getCustomers.php?firstName=" + firstName + '&lastName=' + lastName + '&phone1=' + phone1 + '&phone2=' + phone2 + '&phone3=' + phone3 + '&address=' + address + '&';
		//alert(url);		
		customerHttp.open("GET", url, true);
		customerHttp.onreadystatechange = customersResponse;
		customerBusy = true;
		customerHttp.send(null);
	}
}
function customersResponse()
{
	if (customerHttp.readyState == 4)
	{
		if (customerHttp.responseText.indexOf('invalid') == -1)
		{
			try
			{
				xmlResults = customerHttp.responseXML.documentElement;						
				var output;
				output = '<table class="data">';
				output += '<tr><th>Name</th><th>Phone</th><th>City</th><th>Address</th></tr>';
				
				// Create table rows for each record
				for (var i=0; i < xmlResults.childNodes.length; i++)
				{
					output += '<tr onclick="updateCustomer(\''+xmlResults.getElementsByTagName('first').item(i).firstChild.data+'\',';
					output += '\''+xmlResults.getElementsByTagName('last').item(i).firstChild.data+'\',';
					output += '\''+xmlResults.getElementsByTagName('phone').item(i).firstChild.data+'\',';
					output += '\''+xmlResults.getElementsByTagName('city').item(i).firstChild.data+'\',';
					output += '\''+xmlResults.getElementsByTagName('address').item(i).firstChild.data+'\',';
					output += '\''+xmlResults.getElementsByTagName('zip').item(i).firstChild.data+'\',';
					output += '\''+xmlResults.getElementsByTagName('phoneEve').item(i).firstChild.data+'\')" ';
					output += 'onmouseover="this.style.background=\'#D1D1D1\'; this.style.cursor=\'pointer\';" onmouseout="this.style.background=\'none\';">';
					output += '<td class="data">'+xmlResults.getElementsByTagName('first').item(i).firstChild.data;
					output += ' '+xmlResults.getElementsByTagName('last').item(i).firstChild.data+'</td>';
					output += '<td>'+xmlResults.getElementsByTagName('phone').item(i).firstChild.data+'</td>';
					output += '<td>'+xmlResults.getElementsByTagName('city').item(i).firstChild.data+'</td>';
					output += '<td>'+xmlResults.getElementsByTagName('address').item(i).firstChild.data+'</td>';
					output += '</tr>';
				}
				output += '</table>';
				document.getElementById('custHist').innerHTML = output;
				customerBusy = false;
			}
			catch(e)
			{
				alert("Please report this error to the network admin: " + e) ;
			}
		}
	}
}
function updateCustomer(first, last, phone, city, address, zip, phoneEve)
{
	// Auto-fill customer info on entry form
	document.getElementById('firstName').value = first;
	document.getElementById('lastName').value = last;
	document.getElementById('city').value = city;
	document.getElementById('zip').value = zip;
	document.getElementById('address').value = address;
	var phone1 = phone.substr(1,3);
	var phone2 = phone.substr(5,3);
	var phone3 = phone.substr(9,4);
	document.getElementById('phone1').value = phone1;
	document.getElementById('phone2').value = phone2;
	document.getElementById('phone3').value = phone3;
	document.getElementById('phoneEve').value = phoneEve;	
	getLatLong();
}
/**************************************************************************************************************/
function getRequests()
{
	if (!requestBusy)
	{
		var firstName = document.getElementById('firstName').value;
		var lastName = document.getElementById('lastName').value;
		var address = document.getElementById('address').value;
		var url = "getRequests.php?firstName=" + firstName + '&lastName=' + lastName + '&address=' + address + '&';
		requestHttp.open("GET", url, true);
		requestHttp.onreadystatechange = requestsResponse;
		requestBusy = true;
		requestHttp.send(null);
	}
}
function requestsResponse()
{
	if (requestHttp.readyState == 4)
	{
		if (requestHttp.responseText.indexOf('invalid') == -1)
		{
			try
			{
				xmlServiceRs = requestHttp.responseXML.documentElement;						
				var output;
				output = '<table class="data">';
				output += '<tr><th>Order Date</th><th>Last Name</th><th>Address</th></tr>';
				
				// Build a table row for each record
				for (var i=0; i < xmlServiceRs.childNodes.length; i++)
				{
					output += '<tr onclick="viewRequest(\''+xmlServiceRs.getElementsByTagName('serial').item(i).firstChild.data+'\')" ';
					output += 'onmouseover="this.style.background=\'#D1D1D1\'; this.style.cursor=\'pointer\';" onmouseout="this.style.background=\'none\';">';
					output += '<td>'+xmlServiceRs.getElementsByTagName('desc').item(i).firstChild.data+'</td>';
					output += '<td class="data">';
					output += xmlServiceRs.getElementsByTagName('last').item(i).firstChild.data+'</td>';
					output += '<td>'+xmlServiceRs.getElementsByTagName('address').item(i).firstChild.data+'</td>';								
					output += '</tr>';
				}
				output += '</table>';
				document.getElementById('custRequests').innerHTML = output;
				requestBusy = false;
			}
			catch(e)
			{
				alert("Please report this error to the network admin: " + e) ;
			}
		}
	}
}
/**************************************************************************************************************/
function getLatLong()
{
	if (!geoBusy)
	{
		var address = document.getElementById('address').value;
		var city = document.getElementById('city').value;
		var zip = document.getElementById('zip').value;
		var url = "getLatLong.php?address=" + address + '&city=' + city + '&zip=' + zip + '&';
		//alert(url);		
		geoHttp.open("GET", url, true);
		geoHttp.onreadystatechange = geoResponse;
		geoBusy = true;
		geoHttp.send(null);
	}
}
function geoResponse()
{
	if (geoHttp.readyState == 4)
	{
		if (geoHttp.responseText.indexOf('invalid') == -1)
		{
			try
			{
				xmlResults = geoHttp.responseXML.documentElement;									
				var latitude = xmlResults.getElementsByTagName('lat').item(0).firstChild.data;
				var longitude = xmlResults.getElementsByTagName('long').item(0).firstChild.data;
				geoBusy = false;
				//alert(latitude+', '+longitude);
				viewAddress(latitude,longitude);
			}
			catch(e)
			{
				alert("Please report this error to the network admin: " + e) ;
			}
		}
	}
}
function viewAddress(latitude, longitude)
{
	if (latitude > 0 && longitude < 0)
	{
		map.removeOverlay(marker);
		marker = new GMarker(new GPoint(longitude, latitude), icon);	
		map.addOverlay(marker);
		map.centerAndZoom(new GPoint(longitude, latitude), 2);
	}
}
/**************************************************************************************************************/
function getData(event)
{
	// Only get data when alphanumeric key is pressed
	if ( (event.keyCode >= 48 && event.keyCode <= 57) || // 0-9 on top row keyboard
	     (event.keyCode >= 60 && event.keyCode <= 105) || // Alphanumeric and keypad
		 (event.keyCode == 8 || event.keyCode == 46) // Backspace and delete
	   )
	{
		getRequests();
		getCustomers();
	}
}
function autoTab(spot,nextO,sender,event) 
{
	if ( sender.value.length == spot && 
	   ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105)))
			sender.form[nextO].focus();
}
function viewRequest(serial)
{
	window.open('process.php?request='+serial);
}