﻿
// JScript source code
/// global variables needed in this library
var infilteroldValue

/////////////////////////////////////////////////////////////////////////////////////////////////
///
///
///
////////////////////////////////////////////////////////////////////////////////////////////////
function rememberMe(elem)
{
	if(arguments.length>0)
	{
		if(typeof(elem)=="object")
		{
			infilteroldValue  = elem.value ; 
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function will limit the input on the field to the specified filter.
/// Please note that this is not a formating tool it is merely a keystroke
/// handling function, that is it will cancel the keystroke if it does not
/// match the filter.
/// - elem is the element you need to filter input on, only textboxes are 
///   allowed at this stage
/// - the filter will be :
/// - n  an integer.
/// - d  a decimal value. unlimited number of decimal
/// - d2 a decimal value. 2 maximum of two decimals.
/// - c  a character based field upper or lower.
/// - lc a character based field lower case.
/// - uc a character based field upper case.
/// - a  an alpanumeric field where characters are upper or lower case.
/// - la an alpanumeric field where characters are all lower case.
/// - ua an alpanumeric field where characters are al upper case.
/// - tm a time value ranging from 00:00 to 23:59 [am,pm]. The separator could be a SPACE or a COLUMN.
/// - dt a date value at the moment this will only check for [d]d/[d]d/dd[dd] the separator is SPACE, COLUMN or DASH.
/// - pf a phone fax field this will only allow the input of DIGITS, SPACE, (, ) or DASH.
/// - dc a digital code only integer and an spaces are allowed
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function filterInput(elem,filter)
{
	var infilter;
	var value = TrimStr(elem.value);
	var re;
	var tre1, tre2, tre3;
	
	// This to ensure that any selected text will be replaced by the next typed character
	// Please do not remove this statement.
	if(document.selection.type != "None")
	{
		document.selection.clear();
	}
	
	if((!elem)||(typeof(elem)!="object")||((elem.type!="text") && (elem.type!="password")))
	{
		alert("Wrong usage: The input field is missing or not a text field.");
	}
	
	if((!filter)||(filter==""))
	{
		alert("Wrong usage: the filter is missing.");
	}
	else
	{
		infilter = filter.toLowerCase()
	}
	////////////////////////////////////////////////////////////////
	/// build the regular expression to match the passed filter.
	////////////////////////////////////////////////////////////////
	if(infilter=="n")
	{
		re = /^\d+$/;
	}
	else if(infilter=="ns")
	{
		re = /^[\d|\s]+$/;
	}
	else if((infilter=="d")||(infilter=="d2")||(infilter=="d3")||(infilter=="d4"))
	{
		if(value.indexOf(".") > 0)
		{
			re = /^(\d)$/;
		}
		else
		{
			re = /^([.]|\d)$/;
		}
	}
	/*
	else if(infilter=="d2")
	{
		re = /^\d+$|^\d+[.]$|^\d+[.]\d$|^\d+[.]\d\d$/;
	}
	else if(infilter=="d3")
	{
		re = /^\d+$|^\d+[.]$|^\d+[.]\d$|^\d+[.]\d\d$|^\d+[.]\d\d\d$/;
	}
	else if(infilter=="d4")
	{
		re = /^\d+$|^\d+[.]$|^\d+[.]\d$|^\d+[.]\d\d$|^\d+[.]\d\d\d$|^\d+[.]\d\d\d\d$/;
	}*/
	else if(infilter=="c")
	{
		re = /^[a-zA-Z]+$/;
	}
	else if(infilter=="lc")
	{
		re = /^[a-z]+$/;
	}
	else if(infilter=="uc")
	{
		re = /^[A-Z]+$/;
	}
	else if(infilter=="a")
	{
		re = /^[a-zA-Z0-9]+$/;
	}
	else if(infilter=="la")
	{
		re = /^[a-z0-9]+$/;
	}
	else if(infilter=="ua")
	{
		re = /^[A-Z0-9]+$/;
	}
	else if(infilter=="ps")
	{
		re = /^[^\s]+$/;
	}
	else if(infilter=="us")
	{
		re = /^[^\W|\s]+$/;
	}
	else if(infilter=="tm")
	{
		tre1 = /[ :](.*)[ :]/g;
		if(value=="")
		{
			re = /\d/;
		}
		else if(tre1.test(value))
		{
			re	= /\d|[apm]/i;
		}
		else
		{
			re	= /\d|[ :]|[apm]/i;
		}

		//re = /^[012]$|^[0][0-9]$|^[1][0-9]$|^[2][0-3]$|^\d{1,2}[ :]$|^\d{1,2}[ :][0-5]$|^\d{1,2}[ :][0-5][0-9]$|^\d{1,2}[ :]\d{1,2}\s$|^\d{1,2}[ :]\d{1,2}\s[ap]m?$/i;
	}
	else if(infilter=="dt")
	{
		//re = /^[0123]$|^0[1-9]$|^[12][0-9]$|^3[01]$|^\d{1,2}[ /-]$|^\d{1,2}[ /-][01]$|^\d{1,2}[ /-][0][1-9]$|^\d{1,2}[ /-][1][0-2]$|^\d{1,2}[ /-]\d{1,2}[ /-]$|\d{1,2}[ /-]\d{1,2}[ /-]\d{1,4}$/;
		tre1 = /[. /-](.*)[. /-]/g;
		if((value=="") || (tre1.test(value)))
		{
			re	= /\d/;
		}
		else
		{
			re	= /\d|[-]|[. - /]/;
		}
	}
	else if(infilter=="pf")
	{
		tre1 = /[)]/;
		tre2 = /[(]$/;
		tre3 = /[(]\d\d$/;
		tre4 = /[(]/;
		tre5 = /^\d/;
		if(value=="")
		{
			re	= /\d|[+-]|[ ()]/;
		}
		else if(tre1.test(value))
		{
			re	= /\d|[-+]|[ ]/;
		}
		else if(tre2.test(value))
		{
			re	= /\d|[+]/;
		}
		else if(tre3.test(value))
		{
			re	= /[)]/;
		}
		else if(tre4.test(value))
		{
			re	= /\d|[-+]|[ ()]/;
		}
		else if(tre5.test(value))
		{
			re	= /\d|[-+]|[ ()]/;
		}
		else
		{
			re	= /\d|[-+]|[ ()]/;
		}
	}
	else if(infilter=="dc")
	{
		tre1 = /\s$/;
		if(value=="")
		{
			re	= /\d/;
		}
		else if(tre1.test(value))
		{
			re	= /\d/;
		}
		else
		{
			re	= /\d|[ ]/;
		}
	}
	else
	{
		alert(filter + " is not a known input filter");
	}
	return limitInput(elem,re)
}

////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002.
/// This function act as a slave to filterInput it expects an element and a
/// regular expression to work.
/// - For more powerful usage and filtering call this function directly with 
///   the appropriate regular expression.
///////////////////////////////////////////////////////////////////////////////
function limitInput(elem,pre)
{
	var value		= elem.value;
	var keyCode		= event.keyCode;
	var willBe		= String.fromCharCode(keyCode);

	////////////////////////////////////////////////////////////
	// Make sure that the keystoke is printable, if not return.
	////////////////////////////////////////////////////////////
	if((keyCode<32)||(keyCode>126))
	{
		return;
	}
	else
	{
	}
	if(!(pre.test(willBe)))
	{
		event.keyCode = 0;
		return false;
	}
	else
	{
		return true;
	}
}
/////////////////////////////////////////////////////////////////////////////////////////////
/// This function will check the content (text) of the object (usually a text area against
/// the passed length and display the appropriate message if the length allowed is exceeded.
/// - This function should be called :
/// 1. using the onkeyup event and in this case flag should be set to 0.
/// 2. using the onpaste event and in this case flag should be set to 1.
/////////////////////////////////////////////////////////////////////////////////////////////
function CheckAgainst(elem,maxlen,flag)
{
	var content = elem.value;
	var contlen = content.length;
	var willbelen;
	var selrange,cliptext,allowedChunk;
	var allowedlength 

	if((typeof(flag) == "undefined") || (flag ==""))
	{
		flag = 0;
	}
	
	if(flag==1)
	{
		cliptext		= window.clipboardData.getData("Text");
		selrange		= document.selection.createRange();
		allowedlength	= maxlen - contlen + selrange.text.length;
		willbelen		= contlen + cliptext.length - selrange.text.length;
		if(cliptext != "")
		{
			allowedChunk = cliptext.substr(0,allowedlength);
			window.clipboardData.setData("Text",allowedChunk);
		}
	}
	else
	{
		willbelen = contlen;
	}
	
	if(willbelen > maxlen)
	{
		if(flag == 0)
		{
			PopMessage("A","You have exceeded the " + maxlen + " characters allowed on this field.","Click OK to return to screen.");
			//alert("You have exceeded the " + maxlen + " characters allowed on this field.","Click OK to return to screen.");
			elem.value = content.substr(0,maxlen);
		}
		else
		{
			PopMessage("A", "By pasting the selected text you will have <B>" + willbelen + "</B> characters in a field that can only take <B>" + maxlen + "</B>.", "The pasted text will be chuncked to fit.");
			//alert("selected text too big");
		}
		elem.focus();
	}
}

