function changeclass(x,style,isfocus)
{
	x.className = style;
	if(x.type == "text" && isfocus)
		x.select();
}

function GetPressedKey(ev)
{
	var keyCode = ev.which ? ev.which : ev.keyCode;
	
	return keyCode;

}

function SetPressedKey(ev, key)
{
	if(ev.which)
	{
		ev.which = key;
	} 
	else
	{
		ev.keyCode=key;
	}
}

function KeyToUpper(ev)
{
	var theKey = GetPressedKey(ev);
	if (theKey > 31) 
	{
		SetPressedKey(ev,String.fromCharCode(theKey).toUpperCase().charCodeAt(0));
	}
	return true;
}


function KeyPressNumberOnly(ev)
{

	if (GetPressedKey(ev) < 32)
		return true;
	
	//if the user is typing anything other than number reject
	var validChars = new String("0123456789");
	//Get the keycode character
	var keyChar = new String(String.fromCharCode(GetPressedKey(ev))); 
	if(validChars.indexOf(keyChar, 0)==-1)
	{
		SetPressedKey(ev,0);
		return false;
	}


	return true;
}

function GetNumberCodeFromKeyStroke(keycode)
{
	var result = keycode;
	if ((result > 47 && result < 58) || (result > 95 && result < 106)) 
	{
		if (result > 95) result -= (95-47);
	}
	else
	{
		result = Number.NaN;
	}
	return result;
}

//Floating point control masks
function KeyDownFilterFloat(control, ev)
{
	var currText = new String();
	currText = control.value;

	//Don't allow backarrow
	if(GetPressedKey(ev)==37 && !ev.shiftKey)
	{
		ev.returnValue=false;
		return false;
	}
	return true;
}

function KeyPressFilterFloat(control, ev, decimalseparatorchar, decimaldigits)
{
	//Check for decimal if the key is a decimal and already is typed return 	//false else let the decimal go through
	var currText = control.value;
	
	//Get the keycode character
	var keyChar = new String(String.fromCharCode(GetPressedKey(ev))); 	if(keyChar==decimalseparatorchar)
	{
		//does the current text have decimal?
		if(currText.indexOf(decimalseparatorchar)!=-1)
		{
			SetPressedKey(ev,0);
			return false;
		}
		
	}
	
	if(keyChar=="-" && currText.length==0)
	{
		return true;
	}

	//if the user is typing anything other than number reject
	var validChars = new String("0123456789");
	if(validChars.indexOf(keyChar, 0)==-1 && keyChar!=decimalseparatorchar)
	{
		SetPressedKey(ev,0);
		return false;
	}
	
	/* Its not working well
	//if the decimal point has been typed then check the length if required if 	//it exceeds then bail (note this is kicking the cursor out of the box)
	if(decimaldigits!=-1)
	{
		var l = currText.indexOf(decimalseparatorchar, 0);
		if(l!=-1)
		{
			if(currText.substr(l).length>decimaldigits)
			{
				SetPressedKey(ev,0);
				return false;
			}
		}
	}
	*/

	return true;
}



//Formats a floating point number in a text box control on evey keystroke. Can handle: 
//Group separator 
//Decimal separator
//Decimal digits (if -1 does not check for number of digits)
//Does NOT handle:
//negative sign positioning and character
//Number grouping (assumes groups of 3)
function KeyUpFormatFloat(control, ev, decimalseparatorchar, decimaldigits, groupseparatorchar)
{
	var keyPressed = GetPressedKey(ev);
	//Let the control chars work
	if(keyPressed<32 && keyPressed!=8)
	{
		return true;
	}	
	if(ev.ctrlKey || ev.altKey)
	{
		return true;
	}
	
	var validNumbers = new String("0123456789");
	var validChars = validNumbers+decimalseparatorchar;
	var currentText = new String(control.value); //Get what is typed so far
	if(currentText.length==0) return true; //nothing to do
	
	//ok now get the actual number (including the decimal)
	var numberString = new String();
	var isTrailingZero = true;
	var pickedDecimal = false;
	var countDecimal = 0;
	for (var x=0; x<currentText.length; x++)
	{
		if ((validChars.indexOf(currentText.charAt(x))!=-1)
			 && !(isTrailingZero && (currentText.charAt(x)=="0") && x!=0)
			)
		{
			if(pickedDecimal && (currentText.charAt(x)==decimalseparatorchar)) //Only one decimal allowed!
				continue;

			if(decimaldigits!=-1)
			{
				if(pickedDecimal)
				{
					if(countDecimal==decimaldigits) //were done no more entries only decimaldigit counts are allowed after decimal
					{
						break;
					}
					countDecimal++;
				}
			}
			if(x!=0 || currentText.charAt(x)!="0") isTrailingZero = false;		
			numberString = numberString + currentText.charAt(x);
			if(currentText.charAt(x)==decimalseparatorchar)
			{
				pickedDecimal=true;
			}
		}
	}
	if(numberString.length==0) //Nothing to do
	{
		control.value = numberString;
		return true;
	}
	
	if(numberString.length>1 && numberString.charAt(0)=="0") //One zero is allowed if that is the only thing being typed. Else take out trailing zero's
	{
		numberString = numberString.substr(1);
		
	}
	
	//Now format by going through the number from right to left 	//and putting the signs in
	var result = new String();
	var groupCount=0;
	var decimalCount = 0;
	var	passedDecimal = numberString.indexOf(decimalseparatorchar,0)==-1?true:false;
	
	for(var x=numberString.length-1; x>=0; x--)
	{

		result = numberString.charAt(x) + result;
		
		if(passedDecimal) 
			groupCount++; //start counting groups
		else
			decimalCount++;

		if(numberString.charAt(x)==decimalseparatorchar) //this was the decimal point so from now on we group
		{
			passedDecimal=true;
		}

		if(groupCount==3 && x!=0) //Group time!
		{
			result = groupseparatorchar + result;
			groupCount=0;
		}
	}
	
	//done
	control.value = result;
	return true;
}

// ====================================================================
//       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;
}