﻿// JScript File

function ShowElement(controlName)
{	
	//$("#" + controlName).show("slow");
	if (document.getElementById(controlName))
		document.getElementById(controlName).style.display = "";
}
function HideElement(controlName)
{
	//$("#" + controlName).hide("slow");
	if (document.getElementById(controlName))
		document.getElementById(controlName).style.display = "none";
}
function vShowElement(controlName)
{
	if (document.getElementById(controlName))
		document.getElementById(controlName).style.visibility = "visible";
}
function vHideElement(controlName)
{
	if (document.getElementById(controlName))
		document.getElementById(controlName).style.visibility = "hidden";
}


function getKeyCode(e)
{
	if (window.event)
		return window.event.keyCode;
	else if (e)
		return e.which;
	else
		return null;
}
function keyRestrict(e, validchars) 
{    
	var key='', keychar='';
    key = getKeyCode(e);
    if (key == null) 
		return true;
    
    keychar = String.fromCharCode(key);
    keychar = keychar.toLowerCase();
    validchars = validchars.toLowerCase();
    
    if (validchars.indexOf(keychar) != -1)
		return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;
	return false;
}   
function keyAllow(e, invalidchars){
 var key='', keychar='';
 key = getKeyCode(e);
 if (key == null) return true;
 keychar = String.fromCharCode(key);
 keychar = keychar.toLowerCase();
 invalidchars = invalidchars.toLowerCase();
 if (invalidchars.indexOf(keychar) == -1)
  return true;
 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
  return true;
 return false;


}
function RestrictToDecimal(e)
{
/*	if ((event.keyCode < 33) || (event.keyCode > 40)) 
	{ */
		this.value = this.value.replace(/[^0-9.]/g, '').replace(/[.][0134689.]/g, '.').replace(/[.][2][012346789.]/g,'.25').replace(/[.][7][012346789.]/g,'.75').replace(/[.][2][5][0-9.]/g, '.25').replace(/[.][7][5][0-9.]/g,'.75').replace(/[.][5][0-9.]/g,'.5'); 
	/*} 
	if ((event.keyCode == 9) || (event.keyCode == 16)) 
		this.select(); */
		
	this.value = this.value.replace(/[.][1346890]/g,'.').replace(/[.][2][012346789.]/g,'.25').replace(/[.][7][012346789.]/g,'.75').replace(/[.][5][0-9.]/g,'.5').replace(/[.][2][5][0-9.]/g,'.25').replace(/[.][7][5][0-9.]/g,'.75');
}

function FormatCurrency(val)
{
	//Eliminate "$" and ","
	val = val.replace('$', '');
	while (val.indexOf(',') > -1)
		val = val.replace(',', '');

	/* Eliminate Leading Zeros */
	while ((val.length > 1) && (val.substring(0,1) == "0"))
		val = val.substring(1);

	commas = Math.floor( (val.length - 1) / 3);																				

	for (i = 0; i < commas; i++)
	{
		stop = val.length - (3 * (i + 1) + i);
		first = val.substring(0, stop);											
		last = val.substring(stop);
		
		val = first + "," + last;											
	}	
	return "$" + val;										
}
