/* 

used by tg@component:userimage or userlogo

takes an image object and ensures that its width and height are
constrained to a maximum, but no less than necessary. constraints
are auto-proportioned in the process.

 */
function MaximizeImage(img, mw, mh)
{
	var imgPreload = new Image();

	imgPreload.src = img.src;
	
	var w = imgPreload.width;
	var h = imgPreload.height;

	if(h > mh && mh != 0)
	{
		h = mh;
		w = Math.round(w * (h/imgPreload.height));

	}
	else if(w > mw && mw != 0)
	{
		w = mw;
		h = Math.round(h * (w/imgPreload.width));
	}

	img.width = w;
	img.height = h;
}




/*--------------------------------------------------------
used by tg@component:clientdate

returns the current date and time

*/

var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
      
function GetClientDate()
{
    var now = new Date();
    var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
    return days[now.getDay()] + ", " + months[now.getMonth()] + " " + date + ", " + (fourdigits(now.getYear()));
}

function fourdigits(number)
{
      return (number < 1000) ? number + 1900 : number;
}
/* end clientdate*/


function URLEncode(text)
{
	// 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 < text.length; i++ ) {
		var ch = text.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 BuildSearchAction(formname)
{
	var form = document.getElementById(formname);
	var domain = form.mydomain.value;
	var query = URLEncode(form.q.value);
	var search = "";
	var returl = "";

	if(domain != "")
		query += "+site%3A" + domain;
		
	search = "http://www.google.com/search?hl=en&q=" + query;
	returl = "target.aspx?h=1200&url=" + URLEncode(search);

	PostToAction(form, returl);
}

function PostToAction(form, url)
{
	form.action = url;
	form.submit();
}

//inject iframe resizing parent script
document.write("<script type='text/javascript' src='http://tools.1parkplace.com/scripts/resizeIframeParent.js'></script>");
