/* Copyright © 2002 - 2007 Ten & Two Systems.  All Rights Reserved. */

//document.write('<script type="text/javascript" src="/common/js/prototype.js"></script>');
//document.write('<script type="text/javascript" src="/common/js/swfobject.js"></script>');

var TenAndTwo = {
	/* -------------------------------------------------- *\
		test
	\* -------------------------------------------------- */
	test : function() { 
		alert("TenAndTwo.test();"); 
	}

	/* -------------------------------------------------- *\
		noSpam
		<a href="javascript:TenAndTwo.noSpam('someone','somewhere.com');">someone&#64;somewhere.com</a>
	\* -------------------------------------------------- */
	, noSpam : function(t,d) {
		window.location="mailto:"+t+"@"+d;
	}

	/* -------------------------------------------------- *\
		getCookie
	\* -------------------------------------------------- */
	, getCookie : function( cookiename ) { 
//alert("getCookie(\"" + cookiename + "\")"); 
		var result = ""; 
		if (document.cookie.length > 0) { 
			var search = cookiename + "="; 
			var offset = document.cookie.indexOf(search); 
			if (offset != -1) { 
				offset += search.length; 
				var end = document.cookie.indexOf(";", offset); 
				if (end == -1) { end = document.cookie.length; } 
				result = unescape(document.cookie.substring(offset, end)); 
			} 
		}
		return result; 
	} 

	/* -------------------------------------------------- *\
		setCookie
	\* -------------------------------------------------- */
	, setCookie : function( cookiename, cookievalue ) { 
//alert("setCookie(\"" + cookiename + "\", \"" + cookievalue + "\")"); 
		if (cookiename) 
			{ document.cookie = cookiename + "=" + cookievalue; } 
	} 

	/*-------------------------------------------------------------*\
	// setMenuSelection
	\*-------------------------------------------------------------*/
	, setMenuSelection : function( menu, newvalue ) {
		var flag = false;
		for (i = 0; i < menu.options.length; i++) {
			if (menu.options[i].value == newvalue) { 
				menu.selectedIndex = i; 
				flag = true;
				break;
			}
		}
//		if (flag == false) menu.options[0].selected = true;                      // select first
		if (flag == false) menu.options[menu.options.length-1].selected = true;  // select last
	}

	/* -------------------------------------------------- *\
		ajaxRequest
	
		var url = "some_page.php";
		var method = "get";
		var params = 'name1=value1&name2=value2';
		var onLoading = function() { $('target').innerHTML = "Loading..."; };
		var onComplete = function(response) { $('target').innerHTML = response.responseText; };
	
		TenAndTwo.ajaxRequest( url, method, params, onLoading, onComplete );
	\* -------------------------------------------------- */
	, ajaxRequest : function( url, method, params, onLoading, onComplete ) {
//alert("ajaxRequest(\n'"+url+"'\n,'"+method+"'\n,'"+params+"'\n,'"+onLoading.name+"'\n,'"+onComplete.name+"'\n)");
		var request = new Ajax.Request(
			url
			, {
				method: ((method == "post") ? "post" : "get")
				, parameters: params
				, onLoading: onLoading
				, onComplete: onComplete
				}
			);
	}

	/* -------------------------------------------------- *\
		zeroPad - util
	\* -------------------------------------------------- */
	, zeroPad : function( num, zeros ) {
//alert('zeroPad(' + num + ',' + zeros + ')');
		num += '';
		if (!(zeros > 0)) { zeros = 2; }
		while (num.length < zeros)
			{ num = '0' + num; }
		return num;
	}

	/* -------------------------------------------------- *\
		URLEncode
	\* -------------------------------------------------- */
	, URLEncode : function( str ) {
		var result = "";
		var SAFECHARS = "0123456789" +			// Numeric
				"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
				"abcdefghijklmnopqrstuvwxyz" +
				"-_.!~*'()";					// RFC2396 Mark characters
		var HEX = "0123456789ABCDEF";
	
		var i = 0;
		while (i < str.length) {
			var ch = str.charAt(i);
			if (ch == " ") {
				result += "+";					// x-www-urlencoded
			} else if (SAFECHARS.indexOf(ch) != -1) {
				result += ch;
			} else {
				var charCode = ch.charCodeAt(0);
				if (charCode > 255) {
					result += "+";
				} else {
					result += "%";
					result += HEX.charAt((charCode >> 4) & 0xF);
					result += HEX.charAt(charCode & 0xF);
				}
			}
			i++;
		}
		return result;
	}

	/* -------------------------------------------------- *\
		URLDecode
	\* -------------------------------------------------- */
	, URLDecode : function( str ) {
		var result = "";
		var HEXCHARS = "0123456789ABCDEFabcdef";
		var i = 0;
		while (i < str.length) {
			var ch = str.charAt(i);
			if (ch == "+") {
				result += " ";
				i++;
			} else if (ch == "%") {
				if (i < (str.length-2) 
						&& HEXCHARS.indexOf(str.charAt(i+1)) != -1 
						&& HEXCHARS.indexOf(str.charAt(i+2)) != -1 ) {
					result += unescape( str.substr(i,3) );
					i += 3;
				} else {
					alert( 'Bad escape combination near ...' + str.substr(i) );
					result += "%[ERROR]";
					i++;
				}
			} else {
				result += ch;
				i++;
			}
		}
	
		return result;
	}

}  // end TenAndTwo
