/*
* Function to format a floating point distance value to 2 decimals
* Pre-Condtion: The dist value must be > 0
* Post-Condition: The distance is returned as a floating point number formatted to 2 decimal places.
*
* @param        dist - floating point number containing a distance value
* @return		Formated distance value to 2 decimals
* @author       Seisan Consulting   2-16-2006
*/
function formatDistance(dist){
	dist = parseFloat(dist);
	if(dist < .01 && dist > 0)
		return .01;
	else {
		//var d = parseInt(dist * 100);
		return dist.toFixed(2);
		
		
	}
}

/*
* Function to change a value of time to the provided format.
* Pre-Condtion: The time must exist.
* Post-Condition: The time is returned in the requested format.
*
* @param        time - a strign containing a value of time.
* @param		format - a string containing the requested format.
* @return		format - the time string in the request format
* @author       Seisan Consulting   2-16-2006
*/
function formatTime(time, format){
	//If a format is not provided, the default is used
	if(!format){
		format = "%h:%m:%s";
	}
	time = parseFloat(time);
	var h = parseInt(time / (60 * 60));
	time = parseFloat(time % (60 * 60));
	var m = parseInt(time / 60);
	var s = parseFloat(time % 60);

	if(format.indexOf("%h") >= 0){
		format = format.replace("%h", h);
	}
	if(format.indexOf("%m") >= 0){
		format = format.replace("%m", m);
	}
	if(format.indexOf("%s") >= 0){
		format = format.replace("%s", s);
	}
	return format;
}

/*
* Function to build a route directions table based on the results and options provided.
* Pre-Condtion: The routeresults must != null
* Post-Condition: A html table element is returned containing route directions as per options specified.
*
* @param        routeresult - an MQRouteResult object.
* @param        showCount - a boolean flag whether to display the direction count.
* @param        showText - a boolean flag whether to display the direction text.
* @param        showDistance - a boolean flag whether to display the route distances.
* @param        showTime - a boolean flag whether to display the route times.
* @param		headers - a boolean flag whether to display the Total Route time, distance and maneuvers count.
* @return		tbl - an html table element containing the route directions as per options specified.
* @author       Seisan Consulting   2-16-2006
*/
function getRouteDirections(routeResult, showCount, showText, showDistance, showTime, headers){
	var tbl, tr, td;

	tbl = document.createElement("table");
	//tbl.height = "100%";
	tbl.width=530;
	tbl.className = "routeresults"
	tbl.cellSpacing = 0;
	tbl.cellPadding = 4;
	tbl.border = 0;
	// If headers is true, append total route time, total route distance and maneuver count
	if(headers){
		tr = document.createElement("tr");		
		td = document.createElement("td");
		td.className = "text_form_second";
		td.appendChild(document.createTextNode("Step"));
		tr.appendChild(td);
			
			
		var cnt = 0;
		if(showCount|| showText){
			td = document.createElement("td");
			td.className = "text_form_second";
			td.appendChild(document.createTextNode(headers[cnt++]));
			tr.appendChild(td);
		}

		if(showDistance){
			td = document.createElement("td");
			td.className = "text_form_second";
			if(headers.length > cnt){
				td.appendChild(document.createTextNode(headers[cnt++]));
			}
			else {
				td.appendChild(document.createTextNode(" "));
			}
			tr.appendChild(td);
		}

		if(showTime){
			td = document.createElement("td");
			td.className = "storeloctabletextb"
			if(headers.length > cnt){
				td.appendChild(document.createTextNode(headers[cnt++]));
			}
			else {
				td.appendChild(document.createTextNode(" "));
			}
			tr.appendChild(td);
		}

		tbl.appendChild(tr);
		
		
			tr = document.createElement("tr");
			td = document.createElement("td");
			td.colSpan=8;
			td.className="bg_dashseparator";
			b = document.createElement("p");
			b.appendChild(document.createTextNode('\u00A0'));//white space
			td.appendChild(b);	
			tr.appendChild(td);
			tbl.appendChild(tr);
		
	}

	//Iterate through all maneuvers and display text, time and distance for each maneuver based on options
	var trek, treks, maneuvers, maneuver, text;
	treks = routeResult. getTrekRoutes();
	for(var i=0; i < treks.getSize(); i++){
		trek = treks.getAt(i);
		maneuvers = trek.getManeuvers();
		for(var j=0; j < maneuvers.getSize(); j++){
			maneuver = maneuvers.getAt(j);

			tr = document.createElement("tr");
			
			tr.className = "breakPageFF" //+ (j % 2);

			if(showCount || showText){
				td = document.createElement("td");
				td.vAlign="top";
				text = "";
				if(showCount){
					text += (j+1) + ".";
				}
				if(showCount && showText){
					text += " ";
				}
				td.className = "storeloctabletext"
				td.appendChild(document.createTextNode(text));
				tr.appendChild(td);
				if(showText){
					text = maneuver.getNarrative();
				}
				td = document.createElement("td");
				td.className = "storeloctabletext"
				td.appendChild(document.createTextNode(text));
				tr.appendChild(td);
			}

			if(showDistance){
				td = document.createElement("td");
				td.className = "storeloctabletext"
				//td.appendChild(document.createTextNode(formatDistance(maneuver.getDistance()) + " mi"));
				td.appendChild(document.createTextNode(formatDistance(maneuver.getDistance())));
				tr.appendChild(td);
			}

			if(showTime){
				td = document.createElement("td");
				td.appendChild(document.createTextNode(formatTime(maneuver.getTime())));
				tr.appendChild(td);
			}

			tbl.appendChild(tr);
		}
	}

	return tbl;
}

/*
* Function to take an MQAddress object and return an string containing address information in a single line.
* Pre-Condtion: The MqAddress object != null
* Post-Condition: A string is returned containing address information on a single line.
*
* @param        mqaddress - an MQAddress object
* @return		A string containing address information on a single line.
* @author       Seisan Consulting   2-16-2006
*/
function mqAddressToSingleLineString(mqAddress){
	var element = mqAddressToSingleLineHTMLElement(mqAddress, document.createElement("div"));
	return element.innerHTML;
}

/*
* Function to take an MQAddress object and return an html element containing address information in a single line.
* Pre-Condtion: The MqAddress object != null
* Post-Condition: An html element is returned containing address information on a single line.
*
* @param        mqaddress - an MQAddress object
* @param		element - the html element the address information is being appended to
* @return		A string of html containing address information.
* @author       Seisan Consulting   2-16-2006
*/
function mqAddressToSingleLineHTMLElement(mqAddress, element){
	if(!element)
		element = document.createElement("span");

	if(mqAddress.getName){
		if(mqAddress.getName() != ""){
			var b = document.createElement("b");
			b.appendChild(document.createTextNode(mqAddress.getName()));
			element.appendChild(b);
			element.appendChild(document.createTextNode(" "));
		}
	}

	if(mqAddress.getStreet() != ""){
		element.appendChild(document.createTextNode(mqAddress.getStreet()));
		element.appendChild(document.createTextNode(" "));
	}

	var spacer = false;
	if(mqAddress.getCity()){
		element.appendChild(document.createTextNode(mqAddress.getCity()));
		spacer = true;
	}

	if(mqAddress.getState()){
		if(spacer){
			element.appendChild(document.createTextNode(", "));
		}
		element.appendChild(document.createTextNode(mqAddress.getState()));
		spacer = true;
	}

	if(mqAddress.getPostalCode()){
		if(spacer){
			element.appendChild(document.createTextNode(" "));
		}
		element.appendChild(document.createTextNode(mqAddress.getPostalCode()));
		spacer = true;
	}


	return element;
}

/*
* Function to take an MQAddress object and return a string containing all address information.
* Pre-Condtion: MQAddress != null
* Post-Condition: A string containing address information is returned.
*
* @param        mqaddress - an MqAddress object
* @return		A string containing address information.
* @author       Seisan Consulting   2-16-2006
*/
function mqAddressToString(mqAddress, address2){
	var element = mqAddressToHTMLElement(mqAddress, document.createElement("div"), address2);
	return element.innerHTML;
}

/*
* Function to take an MQAddress object and return an html element containing all address information.
* Pre-Condtion: MQAddress != null
* Post-Condition: An html element containing address information is returned.
*
* @param        mqaddress - an MqAddress object
* @param        element - an html element
* @return		An html element containing the appended address information.
* @author       Seisan Consulting   2-16-2006
*/
function mqAddressToHTMLElement(mqAddress, element, address2){
	if(!element)
		element = document.createElement("span");
	//alert(mqAddress.getStreet());
	if(mqAddress.getName){
		if(mqAddress.getName() != ""){
			var b = document.createElement("b");
			b.appendChild(document.createTextNode(mqAddress.getName()));
			element.appendChild(b);
			element.appendChild(document.createElement("br"));
		}
	}
	if(mqAddress.getStreet()) {
		if(mqAddress.getStreet() != ""){
			element.appendChild(document.createTextNode(mqAddress.getStreet()));
			element.appendChild(document.createElement("br"));
		}
	}
	
	if(address2 != ""){
		element.appendChild(document.createTextNode(address2));
		element.appendChild(document.createElement("br"));
	}
	

	var spacer = false;
	if(mqAddress.getCity()){
		element.appendChild(document.createTextNode(mqAddress.getCity()));
		spacer = true;
	}

	if(mqAddress.getState()){
		if(spacer){
			element.appendChild(document.createTextNode(", "));
		}
		element.appendChild(document.createTextNode(mqAddress.getState()));
		spacer = true;
	}

	if(mqAddress.getPostalCode()){
		if(spacer){
			element.appendChild(document.createTextNode(" "));
		}
		element.appendChild(document.createTextNode(mqAddress.getPostalCode()));
		spacer = true;
	}
	
	return element;
}


function mqAddressToHTMLElementPhone(mqAddress, element, phone, address2){
	if(!element)
		element = document.createElement("span");
	if(mqAddress.getName){
		if(mqAddress.getName() != ""){
			var b = document.createElement("b");
			b.appendChild(document.createTextNode(mqAddress.getName()));
			element.appendChild(b);
			element.appendChild(document.createElement("br"));
		}
	}

	if(mqAddress.getStreet() != ""){
		element.appendChild(document.createTextNode(mqAddress.getStreet()));
		element.appendChild(document.createElement("br"));
	}
	
	if(address2 != ""){
		element.appendChild(document.createTextNode(address2));
		element.appendChild(document.createElement("br"));
	}
	
	var spacer = false;
	if(mqAddress.getCity()){
		element.appendChild(document.createTextNode(mqAddress.getCity()));
		spacer = true;
	}

	if(mqAddress.getState()){
		if(spacer){
			element.appendChild(document.createTextNode(", "));
		}
		element.appendChild(document.createTextNode(mqAddress.getState()));
		spacer = true;
	}

	if(mqAddress.getPostalCode()){
		if(spacer){
			element.appendChild(document.createTextNode(" "));
		}
		element.appendChild(document.createTextNode(mqAddress.getPostalCode()));
		spacer = true;
	}
	
	element.appendChild(document.createElement("br"));
	element.appendChild(document.createTextNode(phone));

	return element;
}
/*
* Function to take a Record Set and build a string containing address information.
* Pre-Condtion: The Recordset != null
* Post-Condition: A strign is returned containing address information.
*
* @param        recordset - a recordset object
* @return		str - a string containing address information pulled from the record set.
* @author       Seisan Consulting   2-16-2006
*/
function getContentString(recordset){
	var str = "";
	str += recordset.getField("Address") + "<br/>";
	str += recordset.getField("City") + ", ";
	str += recordset.getField("State") + " ";
	str += recordset.getField("ZIP");

	return str;

}

/*
* Function to take a Record Set and build a string containing address information on a single line.
* Pre-Condtion: The Recordset != null
* Post-Condition: A strign is returned containing address information on a single line.
*
* @param        recordset - a recordset object
* @return		str - a string containing address information on a single line pulled from the record set.
* @author       Seisan Consulting   2-16-2006
*/
function getContentStringSingleLine(recordset){
	var str = "";
	str += recordset.getField("Address") + " ";
	str += recordset.getField("City") + ", ";
	str += recordset.getField("State") + " ";
	str += recordset.getField("ZIP");

	return str;

}

/*
* Function to build an html hidden form element containing address information from a record set.
* Pre-Condtion: The Record Set != null
* Post-Condition: An html hidden form element is created containing address information.
*
* @param        recordset - a record set object
* @param		i - index of the record set being passed
* @return		An html hidden form element containing address information aquired from the record set.
* @author       Seisan Consulting   2-16-2006
*/
function getResultForm(recordset, i){
	var frm, input;
	frm = document.createElement("form");
	frm.method="get";
	frm.action = "";
	frm.id = "frm" + i;

	input = createInputElement("hidden", "txtName", recordset.getField("N"));
	frm.appendChild(input);

	input = createInputElement("hidden", "txtAddress", recordset.getField("Address"));
	frm.appendChild(input);

	input = createInputElement("hidden", "txtCity", recordset.getField("City"));
	frm.appendChild(input);

	input = createInputElement("hidden", "selStateProvince", recordset.getField("State"));
	frm.appendChild(input);

	input = createInputElement("hidden", "txtPostalCode", recordset.getField("ZIP"));
	frm.appendChild(input);

	input = createInputElement("hidden", "hdnLatitude", recordset.getField("Lat"));
	frm.appendChild(input);

	input = createInputElement("hidden", "hdnLongitude", recordset.getField("Lng"));
	frm.appendChild(input);

	input = createInputElement("hidden", "hdnType", type);
	frm.appendChild(input);

	input = createInputElement("hidden", "rdoUnit", DEFAULT_UNIT);
	frm.appendChild(input);

	input = createInputElement("hidden", "txtDistance", DEFAULT_RADIUS);
	frm.appendChild(input);
	
	input = createInputElement("hidden", "txtMatchesperPage", PAGE_SIZE);
	frm.appendChild(input);
	
	
	return frm;
}

/*
* Function to submit a form with the appropriate form action passed
* Pre-Condtion: none
* Post-Condition: The form provided is submitted with the action passed.
*
* @param        id - id of the form being submitted
* @param		action - the action of the form, example POST, GET etc
* @author       Seisan Consulting   2-16-2006
*/
function submitForm(id, action){
	var frm = document.getElementById(id);
	frm.action = action;
	frm.submit();
}

function o( strId ) {
	if(document.getElementById(strId)) {
		return document.getElementById(strId);
	}
	else return false;
}

/*
* Function to handle the click event on a poi and table result
* Pre-Condtion: The event must exist.
* Post-Condition: The appropriate action is taken based on the object that triggered the event.
*
* @param        e - a javascript event
* @author       Seisan Consulting   2-16-2006
*/
function onPoiClick(e){
	var poiid = false;
	var poi = null;
		
	//If the key is defined then a Poi has triggered this event
	if(this.getKey){
		//alert('infowindow:' + map.getInfoWindow().getMaxWidth());
		//retrieve the correct table rows from the results table based on the key of the poi that triggered the event
		poiid = this.getKey();		
	}
	else {
		//The result on the result table has triggered the event
		var obj = getEventCurrentTarget(e);
		poiid =  obj.id;
		poiid = poiid.substring(poiid.lastIndexOf("_")+1);		
	}
	
	if (typeof(map) != 'undefined') {
		//Retrieve the poi based on the key read from the table row that triggered the event
		poi = map.getPoiByKey(poiid);

		if(poi != null){
			poi.showInfoWindow();
		}
	}

}
/*
* Function to handle the mouseover event on a poi and table result
* Pre-Condition: The event must exist.
* Post-Condition: The appropriate action is taken based on the object that triggered the event.
*
* @param        e - a javascript event
* @author       Seisan Consulting   2-16-2006
*/
function onPoiMouseover(e){
	var poiid = false;
	var poi = null;
	var infowindow = null;
	
	if (map != null) infowindow = map.getInfoWindow();
	
	if (document.getElementById('minipopup')!=null)
	{
		//map.setInfoWindow().setMaxWitdh(550);
		var myRollover = document.getElementById('minipopup');
		var cadena=myRollover.innerHTML;
		//myRollover.innerHTML=cadena.replace('mqminipopuphead','') + 'gdfgdfg ggdf gfdgfg dfgdg' ;
		myRollover.innerHTML=cadena.replace('mqminipopuphead','');
		myRollover.style.paddingLeft='5px';
		myRollover.style.backgroundRepeat= 'no-repeat';
		myRollover.style.fontSize='8pt';
		myRollover.style.fontFamily='arial';
		myRollover.style.textAlign='left';
		myRollover.style.fontWeight='bold';
		myRollover.style.whitespace='wrap';
		//myRollover.style.width = '200px';
		//myRollover.style.height = '75px';
		//myRollover.style.overflow = false;
	}
	//If the key is defined then a Poi has triggered this event
	if(this.getKey){
		if(infowindow != null && this != infowindow.getOpener()){
			if(!infowindow.isHidden()){
				infowindow.hide();
			}
		}
		//retrieve the correct table rows from the results table based on the key of the poi that triggered the event
		poiid = this.getKey();
		var tr1 = document.getElementById("tr1_" + poiid);
		//var tr2 = document.getElementById("tr2_" + poiid);
		//Switch the class names of the table rows to the class for the highlighted table row
		if(tr1.className.indexOf("_over") == -1){
			tr1.className = "textbody_gray_register";
		}
		/*
		if(tr2.className.indexOf("_over") == -1){
			tr2.className = tr2.className + "_over";
		}
		*/
		if(!e.supressScroll){
			//scroll to the search result on the table
			scrollToSearchResult(poiid);
		}
	}
	else {
		//The result on the result table has triggered the event
		if(infowindow != null && !infowindow.isHidden()){
			infowindow.hide();
		}

		var obj = getEventCurrentTarget(e);
		poiid =  obj.id;
		poiid = poiid.substring(poiid.lastIndexOf("_")+1);
		//Retrieve the poi based on the key read from the table row that triggered the event
		if (typeof(map) != 'undefined') 
		poi = map.getPoiByKey(poiid);

		if(poi != null){
			//Create a mouseover event and send it to the matching poi on the map to have the poi show the infowindow
			var event = new Object();
			event.type ="mouseover";
			event.supressScroll = true;
			poi.onMouseOver(event);
		}
	}
}

/*
* This mouse over is explicitly for managing the mouse over event on the softserve icon
* @author       Studiocom Jcelis - April 25th/2008
*/

function onPoiMouseoverSS(e)
{
	Tip("Soft Serve Available");
	onPoiMouseover(e);
}/*
* This mouse over is explicitly for managing the mouse over event on the softserve icon
* @author       Studiocom Jcelis - April 25th/2008
*/

function onPoiMouseoverBR(e)
{
	Tip("Baskin Robbins Express&reg; Available");
	onPoiMouseover(e);
}
/*
* This mouse over is explicitly for managing the mouse over event on the DD icon
*/

function onPoiMouseoverDD(e)
{
	Tip("Dunkin' Donuts at this location");
	onPoiMouseover(e);
}
/*
* Function to handle the mouseout event on a poi and table result
* Pre-Condtion: The event must exist.
* Post-Condition: The appropriate action is taken based on the object that triggered the event.
*
* @param        e - a javascript event
* @author       Seisan Consulting   2-16-2006
*/
function onPoiMouseout(e){
	//If the key is defined then a Poi has triggered this event
	var poiid = false;
	var poi = null;
	UnTip();//disappear the tooltip for the soft serve icon
	if(this.getKey){
		//retrieve the correct table rows from the results table based on the key of the poi that triggered the event
		poiid = this.getKey();
		var tr1 = document.getElementById("tr1_" + poiid);
		//var tr2 = document.getElementById("tr2_" + poiid);
		//Switch the class names of the table rows to the class for the non-highlighted table row
		var cn = tr1.className;
		if(cn.indexOf("_over") > -1){
			cn = cn.substring(0,cn.indexOf("_over"));
			tr1.className = cn;
		}
		/*
		var cn = tr2.className;
		if(cn.indexOf("_over") > -1){
			cn = cn.substring(0,cn.indexOf("_over"));
			tr2.className = cn;
		}
		*/
	}
	else {
		//The result on the result table has triggered the event

		var obj = getEventCurrentTarget(e);
		poiid =  obj.id;
		poiid = poiid.substring(poiid.lastIndexOf("_")+1);
		//Retrieve the poi based on the key read from the table row that triggered the event
		if (typeof(map) != 'undefined') {
			poi = map.getPoiByKey(poiid);
			if(poi != null){
				//Create a mouseover event and send it to the matching poi on the map to have the poi show the infowindow
				var event = new Object();
				event.type ="mouseout";
				poi.onMouseOut(event);
			}
		}
	}
}

/*
* Function to sort the datamanager results ordered by distance.
* Pre-Condtion: none
* Post-Condition: The results are sorted numerically by distance from smallest to largest.
*
* @param        dm1 - a datamanager result
* @param		dm2 - a datamanager result
* @return		The differenc between the results.
* @author       Seisan Consulting   2-16-2006
*/
function sortDataManagerResults(dm1, dm2){

	var d1 = dm1.getDistance();
	var d2 = dm2.getDistance();
	if(d1 == null && d2 == null)
		return 0;
	else if(d1 == null){
		return 1;
	}
	else if(d2 == null){
		return -1;
	}
	else {
		return parseFloat(d1) - parseFloat(d2);
	}
}


/*
* Function to sort the datamanager results ordered by route time.
* Pre-Condtion: none
* Post-Condition: The results are sorted numerically by time from smallest to largest.
*
* @param        dm1 - a datamanager result
* @param		dm2 - a datamanager result
* @return		The differenc between the results.
* @author       Seisan Consulting   2-16-2006
*/
function sortDataManagerResultsTime(dm1, dm2){

	var t1 = dm1.getRouteTime();
	var t2 = dm2.getRouteTime();
	if(t1 == null && t2 == null)
		return 0;
	else if(t1 == null){
		return 1;
	}
	else if(t2 == null){
		return -1;
	}
	else {
		return parseFloat(t1) - parseFloat(t2);
	}
}

/*
* Function to build an array of Datamanager records from the recordset and feature collection.
* Pre-Condtion: none
* Post-Condition: An array of datamanager records is returned.
*
* @param        recordset - a recordset object.
* @param        featureCollection - an MQFeatureCollection object.
* @return		results - an array of data manager record objects obtained from the feature collection
* @author       Seisan Consulting   2-16-2006
*/
function getResultsAsArray(recordSet, featureCollection, time){
	// An array of data manager records
	var results = new Array();
	var dmRecord;
	while(!recordSet.isEOF()){
		dmRecord = new DataManagerRecord(recordSet);
		if(featureCollection){
			for(var i=0; i < featureCollection.getSize(); i++){
				if(dmRecord.getId() == featureCollection.getAt(i).getKey()){
					dmRecord.setDistance(featureCollection.getAt(i).getDistance());
					break;
				}
			}
		}

		results.push(dmRecord);

		recordSet.moveNext();
	}
	if(featureCollection){
		results.sort(sortDataManagerResults);
	}
	return results;
}
/*
* Function to change the value of the page number and reload the results based on that page number.
* Pre-Condtion: pageNum is numeric
* Post-Condition: the page number is changed and the results reloaded.
*
* @param        pagenum - number of the page to be changed to
* @author       Seisan Consulting   2-16-2006
*/
var currentPage = 0;
var currentResults = new Array();

function changePage(pageNum){
	currentPage = pageNum;
	poi_collection = showResults();
	
	if (typeof(map) != 'undefined') {		
		displayMap();
	}
}

/*
* Function to scroll the results table to the location of the id passed.
* Pre-Condtion: The key != null
*
* @param        key - a poi id key
* @author       Seisan Consulting   2-16-2006
*/
function scrollToSearchResult(key){
	var div = document.getElementById("divResults");
	if(div){
		var cnt = 0;

		for(var i=currentPage * PAGE_SIZE; i < currentResults.length && i <= MAX_MATCHES && i < (currentPage * PAGE_SIZE + PAGE_SIZE); i++){
			if(currentResults[i].getId() == key){
				break;
			}
			cnt++;
		}

		div.scrollTop = cnt * SCROLL_DISTANCE;

	}
}

function showResultsDD(latitude, longitude, image)
{
	var icon, poi, latlng, numimage;
	numimage = parseInt(image)+1;
	//Populate the Map with the Results
	icon = new MQMapIcon();
	imgSrc = "../../images/about_us/logo_br.jpg";
	icon.setImage(imgSrc, 25, 25, true, false);
	
	latlng = new MQLatLng(latitude, longitude);
	poi = new MQPoi(latlng, icon);
	map.addPoi(poi);
	map.bestFit(false);
	map.setZoomLevel(13);
}

function enter()
{
	if (window.event.keyCode == 13)
		location.href='PrxInput.aspx';
}

function newsearch()
{
	location.href='PrxInput.aspx';
}

function nextpage()
{
	changePage(currentPage=currentPage+1);
}

function previouspage()
{
	changePage(currentPage=currentPage-1);
}

function onMouseoverImage(e)
{	
	var objid=this.id;
	var imag = document.getElementById(objid);
	if (objid=="imgSearch")
		imag.src="../../images/about_us/btn_new_search_2_on.gif";
	else if(objid=="imgprevresults")
		imag.src="../../images/about_us/btn_previousresult_ro.jpg";
	else
		imag.src="../../images/about_us/btn_more_result_on.gif";
}

function onMouseoutImage(id)
{
	var objid=this.id;
	var imag = document.getElementById(objid);
	if (objid=="imgSearch")
		imag.src="../../images/about_us/btn_new_search_2.gif";
	else if (objid=="imgprevresults")
		imag.src="../../images/about_us/btn_previousresult.jpg";
	else
		imag.src="../../images/about_us/btn_more_result.gif";
}

/*
* Function to display the search results on the Map and in a table
* Pre-Condtion: results > 0
* Post-Condition: A table is displayed with the results from the search
*
* @param        results - Search Results Collection
* @param		origLatLng - an MqLatLng object containing the Origin Latitude and Longitude
* @param        isPoiSearch - Boolean value contains the evaluation of whether the results are of Pois or Hotel Addresses.
* @return
* @author       Seisan Consulting   2-16-2006
*/
function showResults(results, origLatLng, isPoiSearch){

	var icon, poi, latlng, poic;

	//If a new set of results have been passed in replace the current results being shown
	if(results){
		currentResults = results;
	}
	//Otherwise results have not changed, working with the same results
	else {
		results = currentResults;
	}

	//Remove all pois from the map except, start, end and origin
	if (typeof(map) != 'undefined') {
		var pois = map.getPois();
		for(var i = pois.getSize()-1; i >= 0; i--){
			poi = pois.getAt(i);
			if(poi.getKey() != "origin" && poi.getKey() != "start" && poi.getKey() != "end"){
				map.removePoi(poi);
			}
		}
	}
	
	
	//Build results table
	var tbl = document.createElement("table");
	var tbl1 = document.createElement("table");//icons about Baskin Robbins and Dunkin Donuts
	tbl.cellPadding = 0;
	tbl.width=554;
	tbl.cellSpacing = 0;
	tbl.border =0;
	
	var tr, td, th, img, a, b, distance, time, imgSrc, imgRef, imgRefWarning, tbl2, imgSoftServe;
	var spacer;

	var rowItt = 0;
	var cellItt = 0;
	var rowItt1 = 0;
	var cellItt1 = 0;
	var tr1, td1;
		
	tr = tbl.insertRow(rowItt++);
	cellItt = 0;
	td = tr.insertCell(cellItt++);
	td.appendChild(document.createElement("br"));
	
	
		tr = tbl.insertRow(rowItt++);
		cellItt = 0;
		td = tr.insertCell(cellItt++);
		td.colSpan=8;
//		td.className="bg_dashseparator";
		td.className='';
		td.style.cssText='display: none;';
		td.id = "mapdashseparator";
		b = document.createElement("p");
		b.appendChild(document.createTextNode('\u00A0'));//white space
		td.appendChild(b);
	
	
	tr = tbl.insertRow(rowItt++);
	cellItt = 0;
	
	td = tr.insertCell(cellItt++);
	td.colSpan=3;
	td.className="textbody_gray_register";
	b = document.createElement("b");
	
	if (results.length > 1)
		b.appendChild(document.createTextNode(results.length + " Locations found"));
	else
		b.appendChild(document.createTextNode(results.length + " Location found"));
		
	td.appendChild(b);
		
	td = tr.insertCell(cellItt++);
	td.colSpan=5;
	img = document.createElement("img");
	img.id="imgSearch";
	img.onmouseover = onMouseoverImage;
	img.onmouseout = onMouseoutImage;
	img.alt="NEW SEARCH";
	img.style.cursor = "hand";
	img.src = "../../images/about_us/btn_new_search_2.gif";
	img.onclick = newsearch;
	img.onkeypress = enter;
	td.appendChild(img);
	
	
	tr = tbl.insertRow(rowItt++);
	cellItt = 0;
	td = tr.insertCell(cellItt++);
	td.appendChild(document.createElement("br"));
	
	tr = tbl.insertRow(rowItt++);
	tr.className="text_form_second";
	cellItt = 0;
	
	
	td = tr.insertCell(cellItt++);
	td.width=15;
	b = document.createElement("p");
	b.appendChild(document.createTextNode(""));
	td.appendChild(b);
	
	td = tr.insertCell(cellItt++);
	td.width=15;
	b = document.createElement("p");
	b.appendChild(document.createTextNode(""));
	td.appendChild(b);
	
	
	td = tr.insertCell(cellItt++);
	td.width=120;
	td.className="storeloctabletextb";
	b = document.createElement("p");
	b.appendChild(document.createTextNode("Address"));
	td.appendChild(b);
	
	td = tr.insertCell(cellItt++);
	td.width=10;
	b = document.createElement("p");
	b.appendChild(document.createTextNode(""));
	td.appendChild(b);
	
	td = tr.insertCell(cellItt++);
	td.width=110;	
	td.className="storeloctabletextb";
	b = document.createElement("p");
	b.appendChild(document.createTextNode("Phone"));
	td.appendChild(b);

	td = tr.insertCell(cellItt++);
	td.width=70;
	td.className="storeloctabletextb";
	b = document.createElement("p");
	b.appendChild(document.createTextNode("Distance"));
	td.appendChild(b);
	
	td = tr.insertCell(cellItt++);
	td.width=10;
	b = document.createElement("p");
	b.appendChild(document.createTextNode(""));
	td.appendChild(b);
	
	td = tr.insertCell(cellItt++);
	td.width=75;
	td.className="storeloctabletextb";
	b = document.createElement("p");
	b.appendChild(document.createTextNode("Store Info"));
	td.appendChild(b);
	
	tr = tbl.insertRow(rowItt++);
	cellItt = 0;
	td = tr.insertCell(cellItt++);
	td.colSpan=8;
	td.className="bg_dashseparator";
	b = document.createElement("p");
	b.appendChild(document.createTextNode('\u00A0'));//white space
	td.appendChild(b);
	
	if(results.length > 0){
		imgRefWarning = 0;
		imgSoftServe = 0;
		imgBRExpress = 0;
		address2 = 0;
		poic = new MQPoiCollection();
		for(var i=currentPage * PAGE_SIZE; i < results.length && i <= MAX_MATCHES && i < ((currentPage * PAGE_SIZE) + PAGE_SIZE); i++)
		{
			//Populate the Map with the Results
			icon = new MQMapIcon();
			//If isPoiSearch is true, then use the corresponding poi icon based on the DT Style - displaying Points of Interest
			if(isPoiSearch){
				imgSrc = "images/mq_icons/" + results[i].getField("T") + ".gif";
				icon.setImage(imgSrc, 20, 20, true, false);
			}
			else {
			//Otherwise, use the numbered icons to display results - displaying Hotel Addresses
				img = document.createElement("img");
				if (results[i].getComboStoreInfo()==null || results[i].getComboStoreInfo()=="N")
					imgRef = 0;
				else
				{
					imgRef = 1;
					imgRefWarning = 1;
				}
								
				if (results[i].getSoftServeInfo()==null || results[i].getSoftServeInfo()=="N")
					imgSoftServe = 0;
				else
					imgSoftServe = 1;
					
				if (results[i].getBRexpress()==null || results[i].getBRexpress()=="N")
					imgBRExpress = 0;
				else
					imgBRExpress = 1;
					
				if (results[i].getAddress2Info()==null || results[i].getAddress2Info()=="")
					address2 = 0;
				else
					address2 = 1;

				imgSrc = "../../images/about_us/logo_br.jpg";
				img.id = "img_"+ results[i].getId();
				icon.setImage(imgSrc, 25, 25, true, false);
			}
			
			latlng = results[i].getMQLatLng();
			//Build the pois content and set the mouseover and mouseout events
			poi = new MQPoi(latlng, icon);
			poi.setInfoTitleHTML(results[i].getInfoTitleHTML());
			poi.setInfoContentElement(results[i].getInfoContentElement(i, isPoiSearch));
			//poi.setInfoContentHTML(results[i].getInfoContentHTML(i, isPoiSearch));
			poi.setRolloverEnabled(true);
			poi.setKey(results[i].getId());
			MQEventManager.addListener(poi,"mouseover", onPoiMouseover);
			MQEventManager.addListener(poi,"mouseout", onPoiMouseout);
			MQEventManager.addListener(poi,"click", onPoiClick);

			//Add each poi to the map
			poic.add(poi);

			//Build each Table row and add in events for mouseover and mouseout
			tr = tbl.insertRow(rowItt++);
			cellItt = 0;
			tr.vAlign = "top";
			tr.className = "textbody_gray_register";
			tr.id = "tr1_" + results[i].getId();
			tr.appendChild(results[i].getResultForm(i));
			
			td = tr.insertCell(cellItt++);
			td.onmouseover = onPoiMouseover;
			td.onmouseout = onPoiMouseout;
			td.onclick = onPoiClick;
			td.id = "td8_" + results[i].getId();
			td.className = "mq_br_icon";
			
			
			img = document.createElement("img");
			img.src = "../../images/about_us/logo_br.jpg";
			img.id = "img_"+ results[i].getId();
			td.appendChild(img);
			
			td = tr.insertCell(cellItt++);
			b = document.createElement("p");
			b.appendChild(document.createTextNode(""));
			td.appendChild(b);
	
			var br;
			td = tr.insertCell(cellItt++);
			td.onmouseover = onPoiMouseover;			
			td.onmouseout = onPoiMouseout;
			td.id = "td6_" + results[i].getId();
			a = document.createElement("a");
			a.className="link_store_locator";
			a.id = "a3_"+ results[i].getId();
			a.onmouseover = onPoiMouseover;
			a.onmouseout = onPoiMouseout;
			a.href = "javascript:submitForm('frm" + i + "', 'PrxDriveInput.aspx');";
			a.appendChild(document.createTextNode(results[i].getSingleLineHTML()));
			br=document.createElement("br");
			a.appendChild(br);
			if (address2==1)
			{
				a.appendChild(document.createTextNode(results[i].getAddress2Info()));
				br=document.createElement("br");
				a.appendChild(br);
			}			
			a.appendChild(document.createTextNode(results[i].getAddicInf()));			
			td.appendChild(a);
			
			td = tr.insertCell(cellItt++);
			b = document.createElement("p");
			b.appendChild(document.createTextNode(""));
			td.appendChild(b);
			
			td = tr.insertCell(cellItt++);
			td.onmouseover = onPoiMouseover;
			td.onmouseout = onPoiMouseout;
			td.id = "td12_" + results[i].getId();
			b = document.createElement("p");
			b.id = "a1_"+ results[i].getId();
			b.appendChild(document.createTextNode(results[i].getPhoneNumber()));
			td.appendChild(b);
		
			//Display the Distance and Time from the Origin
			if(results[i].getDistance() != null || results[i].getRouteTime() != null){
				td = tr.insertCell(cellItt++);
				td.className = "storeloctabletext";
				td.onmouseover = onPoiMouseover;
				td.onmouseout = onPoiMouseout;
				td.onclick = onPoiClick;
				//td.rowSpan = 2;
				td.id = "td5_" + results[i].getId();
				spacer = false;
				
				b = document.createElement("p");
				b.id = "a1_"+ results[i].getId();
				b.onmouseover = onPoiMouseover;
				b.onmouseout = onPoiMouseout;
				distance = formatDistance(results[i].getDistance());
				b.appendChild(document.createTextNode(distance + " miles"));
				td.appendChild(b);
			}
			
			td = tr.insertCell(cellItt++);
			b = document.createElement("p");
			b.appendChild(document.createTextNode('\u00A0'));//white space
			td.appendChild(b);	
	
			td = tr.insertCell(cellItt++);
			td.onmouseover = onPoiMouseover;
			td.onmouseout = onPoiMouseout;
			td.onclick = onPoiClick;
			td.id = "td1_" + results[i].getId();
			td.className = "mq_br_icon";
			
			tbl2 = document.createElement("table");
			tbl2.cellPadding = 0;
			tbl2.cellSpacing = 0;
			tbl2.border = 0;
	
			tr1 = tbl2.insertRow(rowItt1++);
			
			td1 = tr1.insertCell(cellItt1++);
			td1.height=34
			td1.vAlign = "middle";
			if (imgRef == 1){
				img = document.createElement("img");
				img.onmouseover = onPoiMouseoverDD;
				img.onmouseout = onPoiMouseout;
				img.onclick = onPoiClick;
				img.id = "td50_" + results[i].getId();
				img.src = "../../images/about_us/logo_dd.jpg";
				td1.appendChild(img);
			}
			else
			{
				td1.width=22;
				b = document.createElement("p");
				b.appendChild(document.createTextNode(" "));//white space
				td1.appendChild(b);
			}
			
			td1 = tr1.insertCell(cellItt1++);
			td1.width=25;
			b = document.createElement("p");
			b.appendChild(document.createTextNode(" "));//white space
			td1.appendChild(b);
			
			
			td1 = tr1.insertCell(cellItt1++);
			td1.height=34;
			td1.vAlign = "middle";
			if (imgSoftServe == 1)
			{
				img = document.createElement("img");
				img.onmouseover = onPoiMouseoverSS;
				img.onmouseout = onPoiMouseout;
				img.onclick = onPoiClick;
				img.id = "td60_" + results[i].getId();
				img.src = "../../images/about_us/logo_softserve.jpg";
				td1.appendChild(img);
			}
			else
			{
				td1.width=18;
				b = document.createElement("p");
				b.appendChild(document.createTextNode(" "));//white space
				td1.appendChild(b);
			}
			
			td1 = tr1.insertCell(cellItt1++);
			td1.width=25;
			b = document.createElement("p");
			b.appendChild(document.createTextNode(" "));//white space
			td1.appendChild(b);
			
			
			td1 = tr1.insertCell(cellItt1++);
			td1.height=34;
			td1.vAlign = "middle";
			if (imgBRExpress == 1)
			{
				img = document.createElement("img");
				img.onmouseover = onPoiMouseoverBR;
				img.onmouseout = onPoiMouseout;
				img.onclick = onPoiClick;
				img.id = "td60_" + results[i].getId();
				img.src = "../../images/about_us/logo_BR_Express.jpg";
				td1.appendChild(img);
			}
			else
			{
				td1.width=18;
				b = document.createElement("p");
				b.appendChild(document.createTextNode(" "));//white space
				td1.appendChild(b);
			}
			
			td.appendChild(tbl2);
			rowItt1=0;
			cellItt1=0;
			
			/*	
			tr = tbl.insertRow(rowItt++);
			cellItt = 0;
			
			td = tr.insertCell(cellItt++);
			td.colSpan=8;
			b = document.createElement("b");
			b.appendChild(document.createTextNode(""));
			td.appendChild(b);
			td.appendChild(document.createElement("br"));*/
		}

		//Handle the paging if the results > PAGE_SIZE
		//Build Paging Links to display results on other pages
		if(results.length > PAGE_SIZE){
			var pages = Math.ceil(results.length / PAGE_SIZE);
			if(pages * PAGE_SIZE > MAX_MATCHES){
				pages = Math.ceil(MAX_MATCHES / PAGE_SIZE);
			}
		}
		else
			pages = 0;
	}
	else {
		tr = tbl.insertRow(rowItt++);
		cellItt = 0;

		td = tr.insertCell(cellItt++);
		td.className = "textbody_gray_register";
		td.colSpan = 5;

		b = document.createElement("b");
		b.appendChild(document.createTextNode("No Results Found."));
		td.appendChild(b);
	}
	
	tr = tbl.insertRow(rowItt++);
	cellItt = 0;
	td = tr.insertCell(cellItt++);
	td.colSpan=8;
	td.className="bg_dashseparator";
	b = document.createElement("p");
	b.appendChild(document.createTextNode('\u00A0'));//white space
	td.appendChild(b);
	
	/*tr = tbl.insertRow(rowItt++);
	cellItt = 0;
	td = tr.insertCell(cellItt++);
	td.appendChild(document.createElement("br"));*/
	
	if(results.length > 0){
		if (currentPage==0 && pages > 0)
		{	
			tr = tbl.insertRow(rowItt++);
			cellItt = 0;
			
			td = tr.insertCell(cellItt++);
			td.align="right";
			td.colSpan=8;
			img = document.createElement("img");
			img.id = "imgmoreresults";
			img.alt = "MORE RESULTS";
			img.onmouseover = onMouseoverImage;
			img.onmouseout = onMouseoutImage;
			img.style.cursor = "hand";
			img.src = "../../images/about_us/btn_more_result.gif";
			img.onclick = nextpage;
			td.appendChild(img);
		}
		else if (currentPage == pages-1)
		{
			tr = tbl.insertRow(rowItt++);
			cellItt = 0;
			
			td = tr.insertCell(cellItt++);
			td.align="left";
			td.colSpan=8;
			img = document.createElement("img");
			img.id="imgprevresults";
			img.alt="PREVIOUS RESULTS";
			img.onmouseover = onMouseoverImage;
			img.onmouseout = onMouseoutImage;
			img.style.cursor = "hand";
			img.src = "../../images/about_us/btn_previousresult.jpg";
			img.onclick=previouspage;
			td.appendChild(img);
		}
		else if (pages!=0)
		{
			tr = tbl.insertRow(rowItt++);
			cellItt = 0;
			td = tr.insertCell(cellItt++);
			td.align="left";
			td.colSpan=4;
			img = document.createElement("img");
			img.id="imgprevresults";
			img.alt="PREVIOUS RESULTS";
			img.onmouseover = onMouseoverImage;
			img.onmouseout = onMouseoutImage;
			img.style.cursor = "hand";
			img.onclick=previouspage;
			img.src = "../../images/about_us/btn_previousresult.jpg";
			img.onclic=previouspage;
			td.appendChild(img);
			
			td = tr.insertCell(cellItt++);
			td.align="right";
			td.colSpan=4;
			img = document.createElement("img");
			img.id="imgmoreresults";
			img.alt="MORE RESULTS";
			img.onmouseover = onMouseoverImage;
			img.onmouseout = onMouseoutImage;
			img.style.cursor = "hand";
			img.src = "../../images/about_us/btn_more_result.gif";
			img.onclick=nextpage;
			td.appendChild(img);
		}
		
		/*if (imgRefWarning==1)
		{*/ tr = tbl.insertRow(rowItt++);
			cellItt = 0;
			td = tr.insertCell(cellItt++);
			td.appendChild(document.createElement("br"));
	
			tbl1.cellPadding = 0;
			tbl1.width=554;
			tbl1.cellSpacing = 0;
			tbl1.border =0;
	
			tr1 = tbl1.insertRow(rowItt1++);
			tr1.className="text_sub_title_bill";
			
			td1 = tr1.insertCell(cellItt1++);
			img = document.createElement("img");
			img.src = "../../images/about_us/logo_br.jpg";
			td1.appendChild(img);
			
			td1 = tr1.insertCell(cellItt1++);
			td1.align="left";
			p = document.createElement("p");
			p.appendChild(document.createTextNode("Baskin-Robbins Store"));
			td1.appendChild(p);
			
			td1 = tr1.insertCell(cellItt1++);
			td1.align="left";
			img = document.createElement("img");
			img.src = "../../images/about_us/logo_dd.jpg";
			td1.appendChild(img);
			
			td1 = tr1.insertCell(cellItt1++);
			td1.align="left";
			p = document.createElement("p");
			p.appendChild(document.createTextNode("Dunkin' Donuts Store"));
			td1.appendChild(p);
			
			//jcelis - April 7th/2008
			td1 = tr1.insertCell(cellItt1++);
			td1.align="left";
			img = document.createElement("img");
			img.src = "../../images/about_us/logo_softserve.jpg";
			td1.appendChild(img);
			
			td1 = tr1.insertCell(cellItt1++);
			td1.align="left";
			p = document.createElement("p");
			p.appendChild(document.createTextNode("Soft Serve Available"));
			td1.appendChild(p);
			
			td1 = tr1.insertCell(cellItt1++);
			td1.align="left";
			img = document.createElement("img");
			img.src = "../../images/about_us/logo_BR_Express.jpg";
			td1.appendChild(img);
			
			td1 = tr1.insertCell(cellItt1++);
			td1.align="left";
			p = document.createElement("p");
			p.innerHTML =" BR Express&reg; &nbsp;Available"
			//p.appendChild(document.createTextNode("BR Express(R) Available"));
			td1.appendChild(p);
		//}
}
	
	//Append the results to the page
	var parent = document.getElementById("divResults");
	//if(!browserCheck.ie){
		removeAllChildren(parent);
		parent.appendChild(tbl);
		parent.appendChild(tbl1);
	/*}
	else {
		//alert(tbl.outerHTML);
		parent.innerHTML = tbl.outerHTML;
	}*/

	//Best fit the results on the map
	//if(results.length > 0)
	//	map.bestFit(false);
	
	return poic;
}

/*
* Function to retrieve the route distance and time for the search results when searching by minutes. Performs route Matrix.
* Pre-Condtion: OrigLatLng != null
* Post-Condition: The dataManagerRecordArray is returned with all route times and route distances for each result populated
*
* @param        originLatLng - MQLatLng object containing origin Latitude and Longitude
* @param		dataManagerRecordArray - array of Datamanager Records
* @return		dataManagerRecordArray - array containing each search result with the route times and route distances calculated and populated
* @author       Seisan Consulting   2-16-2006
*/
function getRouteDistanceTime(originLatLng, dataManagerRecordArray, searchtime){
	if(dataManagerRecordArray.length >= 0 ){
		var routeExec = new MQExec(routeServer, serverPath, serverPort, proxyServer, proxyPath, proxyPort);
		var origAddress = new MQGeoAddress();
		origAddress.setStreet("origin");
		origAddress.setMQLatLng(originLatLng);

		//populate array with all addresses to pass to function to calculate route matrix
		var destAddresses = new MQLocationCollection();
		destAddresses.add(origAddress);
		for(var i=0; i < dataManagerRecordArray.length; i++){
			destAddresses.add(dataManagerRecordArray[i]);
		}

		var rOptions = new MQRouteOptions();
		var rmResults = new MQRouteMatrixResults();

		//Perform Route Matrix
		routeExec.doRouteMatrix(destAddresses, false, rOptions, rmResults);


		var newList = new Array()
		//Populate Time and Distance Results
		var maxtime = parseInt(searchtime) * 60;
		for(var i=0; i < dataManagerRecordArray.length; i++){
			if( maxtime >= parseInt(rmResults.getTime(0, i+1))){
				var obj = dataManagerRecordArray[i]
				obj.setRouteTime(rmResults.getTime(0, i+1));
				obj.setRouteDistance(rmResults.getDistance(0, i+1));
				newList.push(obj);
			}
		}

		newList.sort(sortDataManagerResultsTime);
		return newList;
	}
	else {
		return dataManagerRecordArray;
	}
}
