/**
 * wrapper function for contructing a request object
 * Parameters:
	@reqType:		The HTTP request type: GET/POST
	@url:			The URL of the server app
	@asynch:		Whether to send the request asynchronously or not
	@respHandle:	The name of the function that will handle the response
 * Any 5th params, represented as arguments[4], 
 * are the data a POST request is designed to send
 */
var request = null; // global
function httpRequest(reqType, url, asynch, respHandle)
{
	// Mozilla-based
	if (window.XMLHttpRequest)
	{
		request = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject)
	{
		request = new ActiveXObject('Msxml2.XMLHTTP');
		if (!request)
			request = new ActiveXObject('Microsoft.XMLHTTP');
	}

	// request could still be null if neither ActiveXObject
	// initialization succeeded
	if (request)
	{
		// if the reqType param is POST, the the
		// 5th argument to the function is the POSTed data
		if (reqType.toLowerCase() != "post")
		{
			initReq(reqType, url, asynch, respHandle);
		}else{
			// the POSTed data
			var args = arguments[4];
			if (args != null && args.length > 0)
			{
				initReq(reqType, url, asynch, respHandle, args);
			}
		}
	}else{
		alert("Your browser does not permit the use of all of this application's features.");
	}
}
/**
 * initialize a request object that is already constructed
 */
function initReq(reqType, url, bool, respHandle)
{
	try {
		// specify the function that will handle the HTTP response
		request.onreadystatechange = respHandle;
		request.open(reqType, url, bool);
		// if the reqType param is POST, then the
		// 5th argument to the function is the POSTed data
		if (reqType.toLowerCase() == "post")
		{
			request.setRequestHeader("Content-Type", "application/x-www-urlencoded; charset=UTF-8");
			request.send(arguments[4]);
		}else{
			request.send(null);
		}
	}catch(e){
		alert("The application cannot contact the server at the moment.\nPlease try again in a few seconds.\nError: "+e.message);
	}
}

function respHandle()
{
	if (request.readyState == 4)
	{
		if (request.status == 200)
		{
			// if route added already, break here
			if (request.responseText == 'break' || request.responseText == '')
				return false;


			// split response on '-'
			var respArr		= request.responseText.split('-');
			var adID		= respArr[0];
			var street		= decodeURI(respArr[1])+'...';
			var randNumID	= respArr[2];
			var icon		= respArr[3];

			// icons to be used
			var myRouteIcon = '/resources/img/mqIcons/'+icon+'small.gif';
			var trashIcon	= '/resources/img/iconset01/trash_13.gif';

			// content 
			var content = '<div id="'+randNumID+'" style="display: \'\';">';
			content += '<table border=0 width="175" cellpadding=3 cellspacing=1>';
			content += '<tr><td width="15"><img src="'+myRouteIcon+'" align="absmiddle" width=15></td>';
			content += '<td>'+street+'</td>';
			content += '<td style="text-align:right;width:15px;">';
			content += '<a href="javascript:void(0);" ';
			content += 'onClick="return removeRoute('+randNumID+','+adID+');">';
			content += '<img src="'+trashIcon+'" style="border: 0px;" border=0 hspace=0 vspace=0></a>';
			content += '</td></tr>';
			content += '</table>';
			content += '</div>';

			document.getElementById('mqRoutes').innerHTML+=content;


			if (document.getElementById('mqRoutes').style.display == 'none')
			{
				document.getElementById('mqRoutes').style.display='';
				document.getElementById('mqRouteLocations').innerHTML='<br /><div style="font-weight: bold;">Route These Locations:</div>';
				document.getElementById('mqShowAddressForm').style.display='';
			}

		}else{
				document.getElementById('mqRouteLocations').style.display='none';
				document.getElementById('mqGetRoute').style.display='none';
				document.getElementById('mqGetUserAddress').style.display='none';
				document.getElementById('numberIcon'+adID).src='0.gif';
				document.getElementById('numberIcon'+adID).style.display='none';
				alert("Problem in MapQuest Utility");
		}
	}
}	

function mapQuestRoute(action, adID, categoryID, street)
{
	var myStreet;
	if (street != '')
	{
		myStreet = street.replace(/[^a-zA-Z0-9]/g,' ');
		url = siteURL+'ajaxMapQuestRoute/'+action+'/'+adID+'/'+categoryID+'/'+myStreet;
	}
	else
	{
		url = siteURL+'ajaxMapQuestRoute/'+action+'/'+adID+'/'+categoryID;
	}
	httpRequest('GET', url, true, respHandle);
}

function removeRoute(randNumID, adID)
{
	mapQuestRoute('remove', adID,'','');
	document.getElementById(randNumID).style.display='none';
	
}

