function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	//return null;
	return "";
}

function readCookieSubkey(name, subkey) {
	var nameEQ = name + "=";
	var subkeyEQ = subkey + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ')
		{
		    c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) 
		    {
		        var sa = c.substring(nameEQ.length,c.length).split('&');
		        for(var j=0;j<sa.length;j++)
		        {
		            if (sa[j].indexOf(subkeyEQ) == 0)
		            {
		                return sa[j].substring(subkeyEQ.length,sa[j].length);
		            }   
		        }
            }
        }		   
	}
	//return null;
	return "";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function getSelText()
{
	var txt = "";

	if (window.getSelection)
	{
		txt = window.getSelection();
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
	}

	return txt;
}

// Get the character number sent by the key press
function GetKeyNumFromEvent(e)
{
/*
	if(window.event) // IE
	{
		return e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		return e.which;
	}
*/

    var code;
    
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    
    return code;	
}

// Get the character sent by the key press
function GetKeyChar(keynum)
{
	return String.fromCharCode(keynum);
}

// Convert lower case alpha characters to upper case
function ForceUppercase(e)
{
/*
	if(window.event) // IE
	{
		keynum = e.keyCode;
		if ((keynum > 0x60) && (keynum < 0x7B))
		{
			e.keyCode = keynum-0x20;
		}
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
		if ((keynum > 0x60) && (keynum < 0x7B))
		{
			e.which = keynum-0x20;
		}
	}
*/

    var code;
    
    if (!e) var e = window.event;
    if (e.keyCode)
    {
        code = e.keyCode;
		if ((code > 0x60) && (code < 0x7B))
		{
			e.keyCode = code-0x20;
		}
    }
    else if (e.which)
    {
        code = e.which;
		if ((code > 0x60) && (code < 0x7B))
		{
			e.which = code-0x20;
		}
    }
 }

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// Parse a value out of the query string
function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

// Get the value of an elements css style
// ex: getStyle(document.getElementById("container"), "font-size");
// From robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/
function getStyle(oElm, strCssRule) {
    var strValue = "";
    if (document.defaultView && document.defaultView.getComputedStyle) {
        strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
    }
    else if (oElm.currentStyle) {
        strCssRule = strCssRule.replace(/\-(\w)/g, function(strMatch, p1) {
            return p1.toUpperCase();
        });
        strValue = oElm.currentStyle[strCssRule];
    }
    return strValue;
}

function OriginPostalCodeOnly() 
{
    var tbOriginPostalCode = document.getElementById("tbOriginPostalCode");
    var tbOriginCity = document.getElementById("tbOriginCity");

    return (tbOriginPostalCode != null && tbOriginCity == null);
}

function DestinationPostalCodeOnly() 
{
    var tbDestinationPostalCode = document.getElementById("tbDestinationPostalCode");
    var tbDestinationCity = document.getElementById("tbDestinationCity");

    return (tbDestinationPostalCode != null && tbDestinationCity == null);
}

function OriginOrDestinationPostalCodeOnly() 
{
    return OriginPostalCodeOnly() || DestinationPostalCodeOnly();
}

function printx($id) {
    var element = document.getElementById($id);
    var tableresults = document.getElementById("rateresultstable");
    tableresults.className = 'print-no';
    element.className = 'print';
    print();
    element.className = 'table-row';
    tableresults.className = 'print-yes';
    return;
}
