/*
 * Copyright 2006, Jeffrey Palm.
 */

/**
 * XMLResponse -> (XMLDocument | _)
 */
function getDoc(res) {
	if (!res) return 0;
	var xml = res.responseXML;
	if (!xml) return 0;
	var doc = xml.documentElement;
	return doc;
}

function text(node) {
	return node && node.firstChild ? node.firstChild.nodeValue : '';
}

function getElementByTagName(doc,name) {
	var nodes = doc.getElementsByTagName(name);
	if (!nodes || nodes.length == 0) return 0;
	return nodes[0];
}

function $n(tag,on) {
	var e = document.createElement(tag);
	if (on) on.appendChild(e);
	return e;
}

function $t(text,on) {
	var e = document.createTextNode(text);
	if (on) on.appendChild(e);
	return e;
}

function matchAll(s,reString,flags) {
	if (!flags) flags = "";
	var re = new RegExp(reString,flags);
	var ms = s.match(re);
	var matches = [];
	if (ms) {
		var re2 = new RegExp(reString,flags + "g");
		$(ms).each(function(m) {
								 var mss = m.match(re2);
								 alert(m + ":" + mss);
								 if (mss) matches.push(mss[1]);
							 });
	}
	return matches;
}

function parameterString(arr) {
	var params = '';
	$H(arr).each(function(pair) {
								 params += params=='' ? '?' : ':';
								 params += pair.key + "=" + urlencode(pair.value);
							 });
	return params;
}

/**
 * String(location) (Jeff.Point -> Void) -> Void
 */
function geocodeLocation(location,callback) {

	var url = "/cgi-bin/geocode2.pl";

	var params = "loc=" + location;

	var cb = function(res) {
		if (!res) return;
		var doc = res.responseXML.documentElement;
		var lon = text(getElementByTagName(doc,'Longitude'));
		var lat = text(getElementByTagName(doc,'Latitude'));
		var p = new Jeff.Point(lon,lat);
		callback(p);
	};
	new Ajax.Request (url,
										{  method: 'get', 
												parameters: params, 
												onSuccess: cb
												});
}



// ====================================================================
//       URLEncode and URLDecode functions
//
// Copyright Albion Research Ltd. 2002
// http://www.albionresearch.com/
//
// You may copy these functions providing that 
// (a) you leave this copyright notice intact, and 
// (b) if you use these functions on a publicly accessible
//     web site you include a credit somewhere on the web site 
//     with a link back to http://www.albionresarch.com/
//
// If you find or fix any bugs, please let us know at albionresearch.com
//
// SpecialThanks to Neelesh Thakur for being the first to
// report a bug in URLDecode() - now fixed 2003-02-19.
// ====================================================================
function urlencode( plaintext ) {
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
}

function urldecode( encoded ) {
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = "0123456789ABCDEFabcdef"; 
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
		var ch = encoded.charAt(i);
		if (ch == "+") {
			plaintext += " ";
			i++;
		} else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
			plaintext += ch;
			i++;
		}
	} // while
	return plaintext;
}

/* function getState(abbrev) { */
/*    var states = new Array( */
/* 			  "AL", // Alabama  */
/* 			  "AK", // Alaska  */
/* 			  "AS", // American Samoa  */
/* 			  "AZ", // Arizona  */
/* 			  "AR", // Arkansas  */
/* 			  "CA", // California  */
/* 			  "CO", // Colorado  */
/* 			  "CT", // Connecticut  */
/* 			  "DE", // Delaware  */
/* 			  "DC", // District of Columbia  */
/* 			  "FL", // Florida  */
/* 			  "GA", // Georgia  */
/* 			  "GU", // Guam  */
/* 			  "HI", // Hawaii  */
/* 			  "NH", // New Hampshire  */
/* 			  "ID", // Idaho  */
/* 			  "IL", // Illinois  */
/* 			  "IN", // Indiana  */
/* 			  "IA", // Iowa  */
/* 			  "KS", // Kansas  */
/* 			  "KY", // Kentucky  */
/* 			  "LA", // Louisiana  */
/* 			  "ME", // Maine  */
/* 			  "MD", // Maryland  */
/* 			  "MA", // Massachusetts  */
/* 			  "MI", // Michigan  */
/* 			  "MN", // Minnesota  */
/* 			  "MS", // Mississippi  */
/* 			  "MO", // Missouri  */
/* 			  "MT", // Montana  */
/* 			  "NE", // Nebraska  */
/* 			  "NV", // Nevada  */
/* 			  "NJ", // New Jersey  */
/* 			  "NM", // New Mexico  */
/* 			  "NY", // New York  */
/* 			  "NC", // North Carolina  */
/* 			  "ND", // North Dakota  */
/* 			  "OH", // Ohio  */
/* 			  "OK", // Oklahoma  */
/* 			  "OR", // Oregon  */
/* 			  "PA", // Pennsylvania  */
/* 			  "PR", // Puerto Rico  */
/* 			  "RI", // Rhode Island  */
/* 			  "SC", // South Carolina  */
/* 			  "SD", // South Dakota  */
/* 			  "TN", // Tennessee  */
/* 			  "TX", // Texas  */
/* 			  "VI", // US Virgin Islands  */
/* 			  "UT", // Utah  */
/* 			  "VT", // Vermont  */
/* 			  "VA", // Virginia  */
/* 			  "WA", // Washington  */
/* 			  "WV", // West Virginia  */
/* 			  "WI", // Wisconsin  */
/* 			  "WY"  // Wyoming; */
/* 			  ); */

/* } */
