/*
* Function to break down and validate a MapQuest Result code from a Geocode.
* Pre-Condtion: The resultcode must exist.
* Post-Condition: A boolean value is returned containing the evaluation of the resultcode based on level of acceptance. 
*
* @param        rCode - a MapQuest Geocode Result Code
* @return		A boolean value containing the evaluation of the result code. True is acceptable, false is not.
* @author       Seisan Consulting   2-16-2006 
*/
function validateResultCode(rCode){
	var granularity = "", streetConfidence = "", adminAreaConfidence = "", postalCodeConfidence = "";
	//Invalid Result Code - garbage data
	if(rCode.length < 5)
		return false;
	//Breakdown of result code
	//First 2 digits of result code is granularity
	granularity = rCode.substring(0,2);
	//Breakdown of Quality Code
	streetConfidence = rCode.charAt(2);
	adminAreaConfidence = rCode.charAt(3);
	postalCodeConfidence = rCode.charAt(4);
	
	//Granularitys Accepted
	if(granularity.indexOf("L") >= 0 
		|| granularity.indexOf("I") >= 0 
		|| granularity.indexOf("B") >= 0 
		|| granularity.indexOf("Z") >= 0 
		|| granularity.indexOf("A5") >= 0){
	
		//Two possibilities of quality codes accepted
		if((adminAreaConfidence == "A" && postalCodeConfidence == "A")
			|| (adminAreaConfidence == "A" && postalCodeConfidence == "X")){
				return true;
		}
	}
	
	return false;
}


/*
* Function to append hidden fields to an existing html form element. 
* Pre-Condtion: The html form element must exist
* Post-Condition: Hidden fields containing address information are appended to the passed formed. 
*				  Any additional parameters specified in the array addParams are appended to the form as hidden fields. 
*
* @param        frm - an html form element
* @param		address - a MQGeoAddress object
* @param		addParams - an array containing additional parameters to be added as hidden form fields.
* @return		A boolean value containing the evaluation of the result code. True is acceptable, false is not.
* @author       Seisan Consulting   2-16-2006 
*/
function appendHiddenFields(frm, address, addParams){
	var input;
	
	input = createInputElement("hidden", "hdnAddress", address.getStreet());
	frm.appendChild(input);

	input = createInputElement("hidden", "hdnCity", address.getCity());
	frm.appendChild(input);
	
	input = createInputElement("hidden", "hdnStateProvince", address.getState());
	frm.appendChild(input);
	
	input = createInputElement("hidden", "hdnPostalcode", address.getPostalCode());
	frm.appendChild(input);
	
	if(address.getMQLatLng){
		input = createInputElement("hidden", "hdnLatitude", address.getMQLatLng().getLatitude());
		frm.appendChild(input);

		input = createInputElement("hidden", "hdnLongitude", address.getMQLatLng().getLongitude());
		frm.appendChild(input);
	}
	
	if(addParams){
		for(var i=0; i < addParams.length; i++){
			input = createInputElement("hidden", addParams[i][0], addParams[i][1]);
			frm.appendChild(input);			
		}
	}


}

/*
* Helper function to take an MQAddress and create a string from that address and add it to the node provided.
* Pre-Condtion: The node must exist
* Post-Condition: The MQAddress is converted to a string and appended to the node provided. 
*				 
* @param        loc - an MQAddress object
* @param		node - a node element
* @author       Seisan Consulting   2-16-2006 
*/
function addressToString(loc, node){
	var b;			
	if(loc.getStreet() != ""){
		node.appendChild(document.createTextNode(loc.getStreet()));
		node.appendChild(document.createElement("br"));
		
	}
	addSp = false;
	if(loc.getCity() != ""){
		node.appendChild(document.createTextNode(loc.getCity()));
		addSp = true;
	}
	if(loc.getState() != ""){
		if(addSp)
			node.appendChild(document.createTextNode(", "));
		node.appendChild(document.createTextNode(loc.getState()));		
		addSp = true;
	}
	if(loc.getPostalCode() != ""){
		if(addSp)
			node.appendChild(document.createTextNode(" "));
		node.appendChild(document.createTextNode(loc.getPostalCode()));
		addSp = true;
	}
	if(loc.getCounty() != ""){
		if(addSp)
			node.appendChild(document.createElement("br"));
		node.appendChild(document.createTextNode("(" + loc.getCounty() + ")"));
	}
}

/*
* Helper function to take an MQAddress and create a string from that address and add it to the node provided.
* Pre-Condtion: The node must exist
* Post-Condition: The MQAddress is converted to a string and appended to the node provided. 
*				 
* @param        loc - an MQAddress object
* @param		node - a node element
* @author       Seisan Consulting   2-16-2006 
*/
function addressToStringSingleLine(loc, node){
	var b;			
	if(loc.getStreet() != ""){
		node.appendChild(document.createTextNode(loc.getStreet()));
		node.appendChild(document.createTextNode(" "));
		
	}
	addSp = false;
	if(loc.getCity() != ""){
		node.appendChild(document.createTextNode(loc.getCity()));
		addSp = true;
	}
	if(loc.getState() != ""){
		if(addSp)
			node.appendChild(document.createTextNode(", "));
		node.appendChild(document.createTextNode(loc.getState()));		
		addSp = true;
	}
	if(loc.getPostalCode() != ""){
		if(addSp)
			node.appendChild(document.createTextNode(" "));
		node.appendChild(document.createTextNode(loc.getPostalCode()));
		addSp = true;
	}
	if(loc.getCounty() != ""){
		if(addSp)
			node.appendChild(document.createTextNode(" "));
		node.appendChild(document.createTextNode("(" + loc.getCounty() + ")"));
	}
}



/*
* Function to take an MQAddress and build a string of html containing address information.
* Pre-Condtion: The MQAddress object must exist.
* Post-Condition: An html string is returned containing address information.
*				 
* @param        loc - an MQAddress object.
* @return		html - string of html containing address information.
* @author       Seisan Consulting   2-16-2006 
*/
function addressToStringHTML(loc){
	var b, html = "";			
	if(loc.getStreet() != ""){
		html += loc.getStreet();
		html += "<br/>";
		
	}
	var addSp = false;
	if(loc.getCity() != ""){
		html += loc.getCity();
		addSp = true;
	}
	if(loc.getState() != ""){
		if(addSp)
			html += ", ";
		html += loc.getState();		
		addSp = true;
	}
	if(loc.getPostalCode() != ""){
		if(addSp)
			html += " ";
		html += loc.getPostalCode();
		addSp = true;
	}
	if(loc.getCounty() != ""){
		if(addSp)
			html += "<br/>";
		html += "(" + loc.getCounty() + ")";
	}
	return html;
}



/*
* Function to build a Geocode Result Table when ambiguous results are returned from a Geocode.
* The geocode result table is built, each result is its own form.
* Pre-Condtion: The html element id must exist
* Post-Condition: The MQAddress is converted to a string and appended to the node provided. 
*				 
* @param        parentid - id of the html element the table will be appended to
* @param		locationcollection - an MQLocationCollection containing ambiguous results
* @param		page - page the geocode result table is being passed to when created
* @param		addParams- matrix of additional parameters added to the form
* @author       Seisan Consulting   2-16-2006 
*/
function buildGeocodeResultTable(parentid, locationcollection, page, addParams){
	var tbl = document.createElement("table");
	tbl.className = "ambgeocode";
	tbl.cellPadding = 4;
	tbl.cellSpacing = 0;
	tbl.border = 0;
	
	var tr, td, th, a, frm, href, ind, val;
	
	/*tr = document.createElement("tr");
	th = document.createElement("th");
	th.className = "ambgeocode";
	th.appendChild(document.createTextNode("Geocode Result"));
	tr.appendChild(th);
	tbl.appendChild(tr);
	*/
		
	var loc;
	for(var i=0; i < locationcollection.getSize(); i++){
		loc = locationcollection.getAt(i);

		tr = document.createElement("tr");

		td = document.createElement("td");
		td.className = "ambgeocode";
		a = document.createElement("a");
		frm = document.createElement("form");
		
		frm.action = page;
		frm.id = "frm" + i;
		frm.method = "get";
		appendHiddenFields(frm, loc, addParams);
		td.appendChild(frm);
		
		if(page.indexOf("javascript:") < 0){
			href = "javascript:document.getElementById('" + frm.id + "').submit();"
		}
		else {
			href = page;
			ind = href.lastIndexOf(")");
			if(href.charAt(ind-1) == "(")
				val = "'" + frm.id + "'";
			else
				val = ",'" + frm.id + "'";
			href = href.substring(0, ind) + val + href.substring(ind); 
		}
		a.href = href;

		addressToString(loc, a); 
		
		td.appendChild(a);
		tr.appendChild(td);
		tbl.appendChild(tr);
	}
	
	var parent = document.getElementById(parentid);
	if(!browserCheck.ie){
		removeAllChildren(parent);
		parent.appendChild(tbl);
	}
	else {
		parent.innerHTML = tbl.outerHTML;
	}
	
}


/*
* Function to build a Geocode Result Table when ambiguous results are returned from a Geocode.
* The geocode result table is built, each result is its own javascript function call.
* Pre-Condtion: The html element id must exist
* Post-Condition: The MQAddress is converted to a string and appended to the node provided. 
*				 
* @param        parentid - id of the html element the table will be appended to
* @param		locationcollection - an MQLocationCollection containing ambiguous results
* @param		fnct - javascript function created 
* @param		addParams- matrix of additional parameters added to the form
* @author       Seisan Consulting   2-16-2006 
*/
function buildGeocodeResultTableJS(parentid, locationcollection, fnct, additionalFields){
	var tbl = document.createElement("table");
	tbl.cellPadding = 4;
	tbl.cellSpacing = 0;
	tbl.border = 0;
	
	var tr, td, th, a, frm;
	
	tr = document.createElement("tr");
	td = document.createElement("td");
	td.appendChild(document.createTextNode("Geocode Result"));
	td.className = "ambgeocode";
	tr.appendChild(td);
	
	tbl.appendChild(tr);
	
	
	var loc, href;
	for(var i=0; i < locationcollection.getSize(); i++){
		loc = locationcollection.getAt(i);
		tr = document.createElement("tr");
		td = document.createElement("td");
		td.className = "ambgeocode";
		a = document.createElement("a");
		
		href = "javascript:" + fnct + "("
		href += "\"" + loc.getStreet() + "\",";
		href += "\"" + loc.getCity() + "\",";
		href += "\"" + loc.getState() + "\",";
		href += "\"" + loc.getPostalCode() + "\",";
		href += "\"" + loc.getCounty() + "\",";
		href += "\"" + loc.getCountry() + "\",";
		href +=  loc.getMQLatLng().getLatitude() + ",";
		href +=  loc.getMQLatLng().getLongitude();
		if(additionalFields){
			if (additionalFields != ""){
				href += ",";
				href += additionalFields;
			}
		}
		href += ");";
		a.href = href;
		addressToString(loc, a); 
		
		td.appendChild(a);
		tr.appendChild(td);
		tbl.appendChild(tr);
	}
	
	var node = document.getElementById(parentid);
	removeAllChildren(node);
	node.appendChild(tbl);
	
}


/* Function to Attach tr rollover, or poi rollover to highlight row/ show poi
PRE: tr is the row with the poi data to highlight
	poi is the MQPoi object to open the rollover or infowindow
	itt is the itteration number of this row
POST: 
*/
function attachRolloverEvents(tr, poi, itt){
	tr.id = "tr" + poi.getKey();
		
	if(itt % 2 == 1){
		tr.className = "resultstablerow2";
	}
	else {
		tr.className = "resultstablerow1";
	}
	MQEventManager.addListener(poi, "rolloveropen", highlightRow);
	MQEventManager.addListener(poi, "rolloverclose", highlightRow);
	MQEventManager.addListener(poi, "infowindowopen", highlightRow);
	MQEventManager.addListener(poi, "infowindowclose", highlightRow);
	
	if(navigator.appName == "Microsoft Internet Explorer"){
		SeisanEventHandler(tr, "mouseenter", highlightRow);
		SeisanEventHandler(tr, "mouseleave", highlightRow);
	}
	else {
		SeisanEventHandler(tr, "click", clickRow);
		SeisanEventHandler(tr, "mouseover", highlightRow);
		SeisanEventHandler(tr, "mouseout", highlightRow);					
	}
}

function clickRow(e){
	var tr = DOMFunctions.getEventCurrentTarget(e);
	var id = tr.id.replace("tr", "");
	
	var pois = map.getPois();
	for(var i=0; i < pois.getSize(); i++){
		var poi = pois.getAt(i);
		if(poi.getKey() == id){
			poi.showInfoWindow();
			break;
		}
	}
}

function highlightRow(e){
	if(this.getKey){
		var poi = this;
		tr = document.getElementById("tr" + poi.getKey())
		if(tr.className.indexOf("_over") == -1){
			tr.className = tr.className + "_over";
		}
		else {
			tr.className = tr.className.replace("_over", "");
		}
	}
	else {
		var tr = DOMFunctions.getEventCurrentTarget(e);
		var id = tr.id.replace("tr", "");
		
		var pois = map.getPois();
		for(var i=0; i < pois.getSize(); i++){
			var poi = pois.getAt(i);
			if(poi.getKey() == id){
				var event = new Object();
				if(!tr.highlightcolor)
					event.type ="mouseover";
				else 
					event.type ="mouseout";
				poi.onMouseOver(event);
				break;
			}
			
		}
		
	}
	
		
	
}
