/*
* 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() + ")"));
	}
}


/*
* 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 newsearch()
{
	location.href='PrxInput.aspx';
}

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";
}
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";
}

/*
* 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 = 0;
	tbl.cellSpacing = 0;
	tbl.border = 0;

	var tr, td, th, a, frm, href, ind, val;

	//start - new search button
	tr = document.createElement("tr");
	td = document.createElement("td");
	td.appendChild(document.createElement("br"));
	tr.appendChild(td);
	tbl.appendChild(tr);
	
	
	tr = document.createElement("tr");	
	td = document.createElement("td");	
	img = document.createElement("img");
	img.id="imgSearch";
	if(!browserCheck.ie){
		img.onmouseover = onMouseoverImage;
		img.onmouseout = onMouseoutImage;
		img.onclick = newsearch;
	}
	else {
		img.onmouseover = "javascript:this.src='../../images/about_us/btn_new_search_2_on.gif'";
		img.onmouseout = "javascript:this.src='../../images/about_us/btn_new_search_2.gif'";
		img.onclick = "javascript:location.href='PrxInput.aspx'";
	}
	
	img.alt="NEW SEARCH";
	img.style.cursor = "pointer";
	img.src = "../../images/about_us/btn_new_search_2.gif";
	td.appendChild(img);
	
	tr.appendChild(td);
	tbl.appendChild(tr);
	//end - new search button
	
	var loc;
	//alert('data:' + locationcollection.getSize());
	for(var i=0; i < locationcollection.getSize(); i++){
		loc = locationcollection.getAt(i);

		tr = document.createElement("tr");
		tr.className = "textbody_gray_register";
		td = document.createElement("td");
		//td.className = "ambgeocode";
		a = document.createElement("a");
		a.className="link_store_locator";
		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);
		
		tr = document.createElement("tr");
		td = document.createElement("td");
		td.appendChild(document.createElement("br"));
		tr.appendChild(td);
		tbl.appendChild(tr);
	}

	var parent = document.getElementById(parentid);
	//alert('parentid: ' + parentid);
	if(!browserCheck.ie){
		removeAllChildren(parent);
		parent.appendChild(tbl);
	}
	else {
		//alert('1:' + tbl.outerHTML)
		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);

}
