function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
	if (n <= 0)
		 return "";
	else if (n > String(str).length)
		 return str;
	else {
		 var iLen = String(str).length;
		 return String(str).substring(iLen, iLen - n);
	}
}
function IsDigit(o,e) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (((code < 48)||(code > 57 ))) 
	{
		if (code != 46 && code != 37 && code != 39 && code != 8 && code != 9)
		{
			return false;
		}
	}
}
function checkDigit(objParam)
{
	var str = document.getElementById(objParam).value;
	var str1 = "";
	for (i = 0 ;i <= str.length ;i++ )
	{
		for (j = 0; j < 10 ;j++ )
		{
			if (str.charAt(i) == j)
			{
				str1 = str1 + str.charAt(i);
			}
		}
	}
	document.getElementById(objParam).value = str1;
}
function stripFirstZero(objParam){
	var str = document.getElementById(objParam).value;
	
	while (str.charAt(0) == '0' && str != '0')
	{
		str = str.substring(1, str.length);
	}
	document.getElementById(objParam).value = str;
}
function Trim(inputString)
{
	var retValue = inputString;
	var ch = retValue.substring(0, 1);

	while (ch == " ")
	{ // Check for space at the start of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}

	ch = retValue.substring(retValue.length-1, retValue.length);

	while (ch == " ")
	{ // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}	
	return retValue;
}
function popUp(URL, w, h, t, l) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width="+w+",height="+h+",top="+t+",left="+l+"');");
}
function IsEmpty(v){
	varValue = Trim(v);
	if (varValue.length == 0){ return true; }else{ return false; }
}
function IsValidEmail(str){
	var at = '@';
	var dot = '.';
	var lat = parseInt(str.indexOf(at));
	var ldot = parseInt(str.lastIndexOf(dot));
	var lstr = parseInt(str.length);
	
	//no '@' or '@' is first character or '@' is the last character
	if ((lat <= 0) || (lat == parseInt(lstr-1)))
		return false;

	//no '.' or '.' is first character or '.' is the last character
	if ((ldot <= 0) || (ldot == parseInt(lstr-1)))
		return false;

	//presence of another '@'
	if (str.indexOf(at, parseInt(lat+1)) != -1) 
		return false;

	//presence of '.' before or after '@'
	if ((str.substr(parseInt(lat - 1), 1) == dot) || (str.substr(parseInt(lat + 1), 1) == dot))
		return false;

	//check '.' is at least one character after '@'
	if (str.indexOf(dot, parseInt(lat + 2)) == -1) 
		return false;

	//check for blank
	if (str.indexOf(" ") != -1) 
		return false;

	//check the length after the last '.' is not less than 2 characters
	if (str.substr(parseInt(ldot + 1)).length < 2) 
		return false;

	if (!IsAlphaNumeric(str.substr(ldot + 1)))
		return false;	
	
	return true;
}
function IsValidPassword(v){
	if (IsAlphaNumeric(v)) {
		if (v.length < 6) { return false; } else { return true; }
	} else { return false; }
}
function IsAlphaNumeric(str){
	for (i=0; i<str.length; i++){
		if (!((str.charCodeAt(i)>=97) && (str.charCodeAt(i)<=122)) && !((str.charCodeAt(i)>=65) && (str.charCodeAt(i)<=90)) && !((str.charCodeAt(i)>=48) && (str.charCodeAt(i)<=57))){
			return false;
		}
	}
	return true;
}

function clearText(objParam){
	document.getElementById(objParam).value = "";
}
function SetValue(controlID, value)
{
	var ctrl = document.getElementById(controlID);
	if (ctrl) { ctrl.value = value; }
}
function SetRadioValue(controlID, value)
{
	var index = 1;
	var ctrl = document.getElementById(controlID + index);
	while(ctrl)
	{
		if(ctrl.value == value)
		{
			ctrl.checked = true;
			break;
		}
		index++;
		ctrl = document.getElementById(controlID + index);
	}
}
function SetCheckBoxValue(controlID, value)
{
	var ctrl = document.getElementById(controlID);
	if (ctrl) { ctrl.checked = (value == "1"); }
}
function RequiredField(controlID, errorMessage)
{
	control = document.getElementById(controlID);
	
	if(IsEmpty(control.value))
	{
		alert(errorMessage);
		control.focus();
		return false;
	}
	else
		return true;
}
function CheckLength(v,l)
{
	return (Trim(v).length == l);
}
function CheckMinLength(v,l)
{
	return (Trim(v).length >= l);
}
function CheckRange(v,min,max)
{
	return (Trim(v).length >= min && Trim(v).length <= max);
}
function RequiredOption(controlID, controlCount, errorMessage)
{
	for (i = 1; i <= controlCount; i++)
	{
		option = document.getElementById(controlID + i);
		if (option.checked)
			return true;
	}
	alert(errorMessage);
	return false;
}
function CustomValidator(controlID, clientFunction)
{
	control = document.getElementById(controlID);
	var args = [];
	// add the control value as the first array value
	args.push(control.value);
	// copy all other arguments we want to "pass through"
	for(var i = 2; i < arguments.length; i++)
	{
		args.push(arguments[i]);
	}
	
	errorMessage = arguments[arguments.length - 1];
	if(clientFunction.apply(this, args))
	{
		return true;
	}
	else
	{
		alert(errorMessage);
		control.focus();
		return false;
	}
}
function checkEnter(e, loginFunction)
{
	if (e.keyCode == 13)
		loginFunction.apply(this);
	else
		return;
}