﻿
function getCookieVal (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}


function GetCookie (name) 
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
    return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
    }
    return null;
}


function SetCookie (name, value) 
{
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

/*
'-----------------------------------------------------------------------------
'Created By:	Rajesh Bhat 09/10/2002
'Page Name:		Helper.js
'Purpose:		Commonly used Javascript functions
'Required:		
'MODIFICATIONS:
'	DATE		BY		DESCRIPTION
'-----------------------------------------------------------------------------
*/


//-----------------------------------------------------------------------------
//Created By:	Rajesh Bhat 09/10/2002
//Function:		EncodeString(strText)
//Purpose:		Used to endcode a string. This can be used to whenever to pass
//				data between pages, so that cetain charectors like %, space will not
//				be mis-interpreted
//Required:		
//MODIFICATIONS:
//	DATE		BY		DESCRIPTION
//-----------------------------------------------------------------------------
function EncodeString(strText)
{
	var strEncoded;
	var i;
	
	strEncoded = "";
	for (i = 0; i<strText.length; i++)
	{
		strEncoded = strEncoded + strText.charCodeAt(i).toString(16);
	}
	
	return strEncoded;
}

//-----------------------------------------------------------------------------
//Created By:	Rajesh Bhat 09/10/2002
//Function:		DecodeString(strText)
//Purpose:		Used to decode a string encoded with EncodeString() function
//Required:		
//MODIFICATIONS:
//	DATE		BY		DESCRIPTION
//-----------------------------------------------------------------------------
function DecodeString(strText)
{
	var strDecoded
	var i
	
	strDecoded = "";
	for (i=0; i<strText.length; i=i+2)
	{
		strDecoded = strDecoded + String.fromCharCode(Number("0x" + strText.substr(i, 2)));
	}
	return strDecoded;
}

/****************************** dateformat.js *********************************
 *
 * Object Extension for Date object
 *   description:
 *          -Stores day and month arrays in Date object 
 *          -Provides a format function to 'pretty' print
 *           the date in custom formats
 *   parameters:
 *          format - accpets any variation of the following list
 *                 yyyy  is a 4-digit year - i.e., 2002   
 *                 yy    is a 2-digit year - i.e., 02
 *                 month is the full month - i.e., September
 *                 mon   is the first three letters of the month - i.e., Sep
 *                 mmm   is the number of the month - i.e., 9
 *                 hh    is hours - i.e., 3
 *                 mm    is minutes (always 2-digit) - i.e., 05
 *                 ss    is seconds (always 2-digit) - i.e., 08
 *                 ddd   is the first three letters of the day - i.e., Wed
 *                 dd    is the numerical day of the month - i.e, 25
 *                 day   is the full day of the week - i.e., Wednesday
 *                 timezone is the the timezone in hours from GMT - i.e., GMT+5
 *                 time24   is the time based on a 24 hour clock - i.e., 18:24   
 *                 time     is the time based on am/pm - i.e., 6:24PM  
 *   example:
 *           myDate = newDate()
 *           myDate.format("day, month dd, yyyy hh:mm:ss timezone")
 *           would return "Wednesday, September 25, 2002 12:14:11 GMT-5"
 *   note: 
 *           If customizing the dateFormat function be aware that the ordering
 *           of the replace calls
 ******************************************************************************/ 
//Store the date info in the Date object
Date.prototype.Months = ["January", "February", "March", 
                         "April", "May", "June", "July", 
                         "August", "September", "October", 
                         "November", "December"];
Date.prototype.Days = ["Sunday", "Monday", "Tuesday", 
                       "Wednesday", "Thursday", 
                       "Friday", "Saturday"];
Date.prototype.format = dateFormat;
function dateFormat(format) {
   var dateString = format;

   //yyyy  is a 4-digit year - i.e., 2002  
   dateString = dateString.replace( new RegExp("yyyy", "gi"), this.getFullYear() );
   //yy    is a 2-digit year - i.e., 02
   dateString = dateString.replace( new RegExp("yy", "gi"), new String( this.getYear() ).substring(2,4) );
   //month is the full month - i.e., September
   dateString = dateString.replace( new RegExp("month", "gi"), this.Months[this.getMonth()] );
   //mon   is the first three letters of the month - i.e., Sep
   dateString = dateString.replace( new RegExp("mon", "gi"), new String( this.Months[this.getMonth()] ).substring(0,3) );
   //mmm   is the number of the month - i.e., 9
   dateString = dateString.replace( new RegExp("mmm", "gi"), (this.getMonth() + 1) );   
   //hh    is hours - i.e., 3
   dateString = dateString.replace( new RegExp("hh", "gi"), this.getHours() );
   //mm    is minutes (always 2-digit) - i.e., 05
   var mm = new String( this.getMinutes() );
   if (mm.length == 1) mm = "0" + mm; //pad if single digit
   dateString = dateString.replace( new RegExp("mm", "gi"), mm );
   //ss    is seconds (always 2-digit) - i.e., 08
   var ss = new String( this.getSeconds() );
   if (ss.length == 1) ss = "0" + mm; //pad if single digit
   dateString = dateString.replace( new RegExp("ss", "gi"), ss ); 
   //ddd   is the first three letters of the day - i.e., Wed
   dateString = dateString.replace( new RegExp("ddd", "gi"), new String( this.Days[this.getDay()] ).substring(0,3) );
   //dd    is the numerical day of the month - i.e, 25
   dateString = dateString.replace( new RegExp("dd", "gi"), this.getDate() );
   //day   is the full day of the week - i.e., Wednesday
   dateString = dateString.replace( new RegExp("day", "gi"), this.Days[this.getDay()] );

   //timezone is the the timezone in hours from GMT - i.e., GMT+5
   tz = this.getTimezoneOffset();
   timezone = "";
   if (tz < 0)
      timezone = "GMT-" +  tz / 60;
   else if (tz == 0)
      timezone = "GMT";
   else
      timezone = "GMT+" + tz / 60;
   dateString = dateString.replace( new RegExp("timezone", "gi"), timezone );
   
   //time24   is the time based on a 24 hour clock - i.e., 18:24   
   var minutes = new String( this.getMinutes() );
   if (minutes.length == 1) minutes = "0" + minutes; //pad if single digit
   var time24 = new String( this.getHours() + ":" + minutes );
   dateString = dateString.replace( new RegExp("time24", "gi"), time24 );
   
   //time     is the time based on am/pm - i.e., 6:24PM
   var time;
   var ampm;
   var hour = this.getHours();
   if ( hour < 12) {
      if (hour == 0) hour = 12;
         ampm = "AM"
   } else {
      if (hour !=12)
         hour = hour - 12;
      ampm = "PM";   
   }
   time = new String(hour + ":" + minutes + ampm);     
   dateString = dateString.replace( new RegExp("time", "gi"), time );

   return dateString;   
}

//-----------------------------------------------------------------------------
//Created By:	Rajesh Bhat 10/30/2002
//Function:		IsValidDate(date)
//Purpose:		To check whether the date is valid
//Required:		
//MODIFICATIONS:
//	DATE		BY		DESCRIPTION
//-----------------------------------------------------------------------------
function IsValidDate(date) 
{
    var test = Date.parse(date)

    if (isNaN(test)) {
		return false;
    }
    else {
		return true;
    }
}

//-----------------------------------------------------------------------------
//Created By:	Rajesh Bhat 09/15/2002
//Function:		replace(originalString,searchText,replaceText)
//Purpose:		Replace a searchText from originalString with replaceText
//Required:		
//MODIFICATIONS:
//	DATE		BY		DESCRIPTION
//-----------------------------------------------------------------------------
function replace(originalString,searchText,replaceText) {
	var strLength = originalString.length; 
	var txtLength = searchText.length; 
	
	if ((strLength == 0) || (txtLength == 0))
	{
		return originalString;
	}
	
	var i = originalString.indexOf(searchText); 
	if ((!i) && (searchText != originalString.substring(0,txtLength))) 
	{	
		return originalString;	
	} 
	if (i == -1) 
	{
		return originalString;
	}
	
	var newstr = originalString.substring(0,i) + replaceText; 
	if (i+txtLength < strLength) 
	{	newstr += replace(originalString.substring(i+txtLength,strLength),searchText,replaceText);	
	} 
	return newstr;
}

function lost_focus( textObject, dateMask )
{
	var dateMask = new String( dateMask );
	var dateValue = new String( textObject.value );
	
	var dateMask1 = dateMask.replace( "/", "-" );
	var dateValue1 = dateValue.replace( "/", "-" );
	var btnSave = window.document.getElementById( "btnSave" );
	var dateParts = null;
	var maskParts = null;
	
	if ( dateValue.length > 0 )
	{
		if ( btnSave != null ) btnSave.disabled = true;

		//var dateParts = new Array();
		dateParts = dateValue1.split( "-" );
		maskParts = dateMask1.split( "-" );
		
		if ( dateParts.length != maskParts.length )
		{
			alert( "Date entered not in required format: " + dateMask.toUpperCase() );
			textObject.focus();
			return;
		}
		
		for ( var i = 0; i < dateParts.length; i++ )
		{
			if ( dateParts[ i ].length != maskParts[ i ].length )
			{
				alert( "Date entered not in required format: " + dateMask.toUpperCase() );
				textObject.focus();
				return;
			}
		}
	}
	var currPos = textObject.value.length;
		
	if ( currPos == dateMask.length && !checkDate( textObject.value, dateMask ) )
	{
		if ( btnSave != null ) btnSave.disabled = true
		alert( "invalid date." );
		textObject.focus();
		return;
	}
	
	//Start checking whether the date is in future
	if ( btnSave != null )	//checking only for date entry screen
	{
		var today = new Date();
		var m = 0;
		var d = 0;
		var y = 0;
		for ( var i = 0; i < maskParts.length; i++ )
		{
			var maskChar = maskParts[ i ].toLowerCase().charAt( 0 );
			if ( maskChar == "m" )
				m = dateParts[ i ];
			else if ( maskChar == "d" )
				d = dateParts[ i ];
			else if ( maskChar == "y" )
				y = dateParts[ i ];
		}
		var theDate = new Date( y, m - 1, d, 0, 0, 0 );
		if ( theDate > today )
		{
			if ( btnSave != null ) btnSave.disabled = true
			alert( "future date not valid for this field." );
			textObject.focus();
			return;
		}
	}
	//End checking whether the date is in future
	
	if ( btnSave != null ) btnSave.disabled = false;

	if ( currPos > dateMask.length )
	{
		textObject.value = textObject.value.substr( 0, dateMask.length );
		return;
	}
	
	var oneChar = dateMask.substr( currPos, 1 );
	if( oneChar == "-" || oneChar == "/" || oneChar == " " )
		textObject.value = textObject.value + oneChar;
	return;
}

function textFormat( textObject, maskDesc )
{
	//about mask description:
	//~ is dilimiter
	//~4~-~7~-~
	//Meaning: the fourth and the senventh chars are -
	//eg: 123-45-7890, which is a SSN
	//~1~(~5~)~9~-~
	//meaning: the first char is (, the fifth char is ), and the ninth is -
	//eg: (123)456-7890, which is a phone number
	
	//keycode >= 48 && keycode < 57  /*0-9*/
	   //if ( keycode >= 37 && keycode <= 40 /*Arrow keys*/) return;
	var keycode = event.keyCode;

    if ( keycode == 8  /*backspace*/ || keycode == 9  /*tab*/ || 
		keycode == 27 /*escape*/ || keycode == 13 /*enter*/ ||
		keycode == 46 /*delete*/ ) return;
	
	if ( keycode >= 37 && keycode <= 40 /*Arrow keys*/) return;

	//alert( keycode );
	//if ( keycode < 48 || keycode > 90 ) return;
	//if ( keycode > 57 && keycode < 65 ) return;

	var maskDesc = new String( maskDesc );
	
	if ( textObject.value.length == 1 && maskDesc.indexOf( "~1~" ) >= 0)
	{
		textObject.value = maskDesc.substr( 3, 1 ) + textObject.value;
	}
	
	//
	//while ( true )
	//{
	//	var currPos = new String("~" + (textObject.value.length + 1) + "~");
	//	var pos = maskDesc.indexOf( currPos );
	//	if ( pos >= 0 )
	//	{
	//		textObject.value = textObject.value + maskDesc.substr( pos + currPos.length, 1 );
	//		return;
	//	}
	//}
	//
	
	maskDesc = maskDesc.substr( 1, maskDesc.length - 2 );	
	var masks = maskDesc.split( "~" );
	
	//alert( maskDesc + ", len: " + masks.length );
	
	var curValue = new String( textObject.value );

	for ( var i = 0; i < masks.length - 1; i = i + 2 )
	{	
		//alert( curValue + ",i=" + i + "," + masks[ i ] + ",m: " + masks[ i + 1 ] );
		if ( curValue.length < masks[ i ] - 1 ) break;
		if ( curValue.length == masks[ i ] - 1 )
		{
			curValue = curValue + masks[ i + 1 ];
			break;
		}
		if ( curValue.substr( masks[ i ] - 1, 1 ) != masks[ i + 1 ] )
		{
			var curValue1 = curValue.substr( 0, masks[ i ] - 1 ) + masks[ i + 1 ] +
						curValue.substr( masks[ i ] - 1, curValue.length - masks[ i ] + 1 );
			curValue = curValue1;
		}
	}
	textObject.value = curValue;
	return;
}

function checkInput( inputType )
{
	var keycode = event.keyCode;
	//alert( keycode );
	//keycode >= 48 && keycode <= 56  /*0-9*/
	if ( keycode == 190 ) return;
	if ( keycode == 110 ) return;
	if ( keycode == 32 ) return;	//spacebar

	if ( keycode >= 37 && keycode <= 40 /*Arrow keys*/) return;
    if ( keycode == 8  /*backspace*/ || keycode == 9  /*tab*/ || 
		keycode == 27 /*escape*/ || keycode == 13 /*enter*/ ||
		keycode == 46 /*delete*/ ) return;
		
	if ( event.ctrlKey ) return;
	//{
	//	event.returnValue = false;
	//	return;
	//}

	if ( inputType == 'numberOnly' )
	{
		if ( !( ( keycode >= 48 && keycode <= 57 ) || ( keycode >= 96 && keycode <= 105 ) ) )
			event.returnValue = false;
		return;
	}
}

function check_SSN( textObject )
{
	if ( textObject.value.length = "" ) return;
	
	var btnSave = window.document.getElementById( "btnSave" );
	if ( btnSave == null ) return;
	
	var ssnParts = textObject.value.split( "-" );
	if ( ssnParts.length != 3 )
	{
		alert( "Invalid SSN." );
		btnSave.disabled = true;
		textObject.focus();
		return;
	}
	
	if ( ssnParts[ 0 ].length != 3 || ssnParts[ 1 ].length != 2 || ssnParts[ 2 ].length != 4 )
	{
		alert( "Invalid SSN." );
		btnSave.disabled = true;
		textObject.focus();
		return;
	}
	btnSave.disabled = false;
}

function dateFormat( textObject, dateMask )
{
	//about mask description:
	//~ is dilimiter
	//~4~-~7~-~
	//Meaning: the fourth and the senventh chars are -
	//eg: 123-45-7890, which is a SSN
	//~1~(~5~)~9~-~
	//meaning: the first char is (, the fifth char is ), and the ninth is -
	//eg: (123)456-7890, which is a phone number
	
	//keycode >= 48 && keycode < 57  /*0-9*/
	   //if ( keycode >= 37 && keycode <= 40 /*Arrow keys*/) return;
	   
	var keycode = event.keyCode;

    if ( keycode == 8  /*backspace*/ || keycode == 9  /*tab*/ || 
		keycode == 27 /*escape*/ || keycode == 13 /*enter*/ ||
		keycode == 46 /*delete*/ ) return;
	
	//alert( keycode );

	//if ( keycode > 57 && keycode < 65 ) return;
	
	var dateMask = new String( dateMask );
	
	var currPos = textObject.value.length;

	//if ( currPos == dateMask.length && !checkDate( textObject.value, dateMask ) )
	//{
	//	alert( "invalid date." );
	//	textObject.focus();
	//	return;
	//}
	
	if ( currPos > dateMask.length )
	{
		textObject.value = textObject.value.substr(0, dateMask.length );
		return;
	}
	
	var oneChar = dateMask.substr( currPos, 1 );
	if( oneChar == "-" || oneChar == "/" || oneChar == " " )
		textObject.value = textObject.value + oneChar;
	return;
}

function checkDate( dateValue, dateMask )
{
	if ( dateValue == null ) return;
	if ( dateMask == null ) return;
	
	var m = '';
	var d = '';
	var y = '';

	dateMask1 = new String( dateMask );
	dateMask1 = dateMask1.toLowerCase();
	//alert( dateMask );

	for( var i = 0; i < dateMask1.length; i++ )
	{
		//alert( "here" );
		
		var dmi = dateMask1.charAt( i );

		var dvi = "";
		if ( dateValue.length >= dateMask1.length )
		{
			dvi = dateValue.charAt( i );
		}
		if ( dmi == "m" ) m = m + dvi;
		if ( dmi == "d" ) d = d + dvi;
		if ( dmi == "y" ) y = y + dvi;
	}
	//alert( m + "-" + d + "-" + y );
		
	return isValidDate( d, m - 1, y, false );
}

function isValidDate( day, month, year, not_in_future )
{
	/*
		Purpose: return true if the date is valid, false otherwise
		Arguments: day integer representing day of month
		month integer representing month of year
		year integer representing year
		Variables: dteDate - date object
	*/

	//set up a Date object based on the day, month and year arguments
	//javascript months start at 0 (0-11 instead of 1-12)
	var dteDate = new Date(year,month,day);

	/*
	Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must have been an invalid date to start with...
	*/
	
	return (day==dteDate.getDate()) && (month==dteDate.getMonth()) && (year==dteDate.getFullYear());
}

function ResetTimer()
{
	try
	{
		parent.frames["fraLeftFrame"].formMenu.TimeOutSeconds.value =
			parent.frames["fraLeftFrame"].formMenu.TimeOutMin.value * 60;
		//alert(parent.frames["fraLeftFrame"].formMenu.TimeOutSeconds.value);
	} catch ( ex )
	{}
}

	function disableField( enabled )
	{
		//txtFieldNamePreffix = "txtField_"
		for ( var i=0; i < document.frmSearch.length; i++ )
		{
			var obj = document.frmSearch.item(i);
			
			var objName = new String( obj.name );
			if ( objName.indexOf( "txtField_" ) == 0 || objName.indexOf( "txtFieldAnn" ) == 0 )
			{
				//alert( obj.name + ": disabled=:" + enabled  );
				obj.disabled = enabled;
			}
		}
	}
	
	function selectedIndex_changed( controlID, disableFieldIDs )
	{
		//disableFieldIDs="I1:23,24|I3:35,36"
		var obj = window.document.getElementById( controlID );
	
		if ( obj == null ) return;
		if ( disableFieldIDs == null || disableFieldIDs == "" ) return;
		
		var info = new String( disableFieldIDs );
		var allDisOptions = info.split( "|" );
		
		var modIndex = new String( "I" + obj.selectedIndex + ":" );
		
		var ids = new String( );
		
		for ( var i = 0; i < allDisOptions.length; i++ )
		{
			var pos = allDisOptions[ i ].indexOf( modIndex );
			if ( pos >= 0 )
			{
				ids = allDisOptions[ i ];
				ids = ids.substring( pos + modIndex.length, ids.length );
				break;
			}
		}
		
		if ( ids.length == 0 ) return;
		ids = ids.replace( "|", "" );
		
		disableAllFields( false );
		
		//alert( "allID: " + ids );

		var allIDs = ids.split( "," );
		for ( var i = 0; i < allIDs.length; i++ )
		{
			var obj = window.document.getElementById( "txtField_" + allIDs[i] );
			if ( obj == null ) continue;
			obj.disabled = true;
		}
	}
	
	function disableAllFields( enabled )
	{
		//txtFieldNamePreffix = "txtField_"
		for ( var i=0; i< document.frmSearch.length; i++ )		
		{
			var obj = document.frmSearch.item(i);
			
			var objName = new String( obj.name );
			if ( objName.indexOf( "txtField_" ) == 0 )
			{
				obj.disabled = enabled;
			}
			else if ( objName.indexOf( "ckbField_" ) == 0 )
			{
				obj.disabled = enabled;
			}
		}
	}
	
	
	function showElement(elementID)
    {        
	    elementID.style.display = '';    
    }
	
	function hideElement(elementID)
    {          
	    elementID.style.display = 'none';	      
    }
    
    function toggleElement(elementID)
    {
        if (elementID.style.display =='')
	        elementID.style.display = 'none';
	    else
	        elementID.style.display = '';    
    }
    
    function getCrtlPrefix()
    {
       var prefix;             
       var objCrtlPrefix = document.getElementById("ctrlPrefix");             

       if (objCrtlPrefix)
             prefix = objCrtlPrefix.value;                 

       return prefix;
    }
    
    
    function validateDate(dateStr)
    {    
    
      var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;                
    var matchArray = dateStr.match(datePat); 
    var d = new Date();
         
    if (matchArray == null) 
    {
        alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
        return false;
    }

    month = matchArray[1];
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) 
    { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) 
    {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
    {
        alert("Month "+month+" doesn`t have 31 days!")
        return false;
    }

    if (month == 2) 
    { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) 
        {
            alert("February " + year + " doesn't have " + day + " days!");
            return false;                
        }
    }

    
    if (year <1900 || year > d.getFullYear())
    {
        alert ("Year is out of range, please enter a valid year");
        return false;
    }

    return true; // date is valid                                  
  }


  function TogleDisplay(ele, value) {
      if (!value)
      {
          ele.style.display = "none";
      }
      else {
          ele.style.display = "block";
      }
      return true;
  }


  function trim(str) {
      if (!str || typeof str != 'string')
          return null;

      return str.replace(/^[\s]+/, '').replace(/[\s]+$/, '').replace(/[\s]{2,}/, ' ');
  }

  function SelectedHtmlRadioListValue(className) {
      var value;

      objs = $$(className);
      for (var i = 0; i < objs.length; i++) {
          if (objs[i].checked == true) {
              value = objs[i].value;
              break;
          }
      }
      return value;
  }

  function SelectedRadioListValue(ctlName) {
      var radioButtons = document.getElementsByName(ctlName);
      for (var x = 0; x < radioButtons.length; x++) {
          if (radioButtons[x].checked) {
              return radioButtons[x].value;
          }
      }
  }

function LimitText(fieldId, maxlimit) {
    var ctl = $(fieldId);
    if (ctl.value.length >= maxlimit) {
        return false;
    }
    else {
        return true;
    }
}

function LimitTextPaste(fieldId, maxlimit) {
    var ctl = $(fieldId);
    if (ctl.value.length >= maxlimit) {
        return false;
    }
    else {
        return true;
    }
}

function LimitTextOnChange(fieldId, name, maxlimit) {
    var ctl = $(fieldId);
    if (ctl.value.length >= maxlimit) {
        ctl.value = ctl.value.substring(0, maxlimit);
        alert(name + " can only be " + maxlimit + " characters long. Field value is truncated");
        return false;
    }
    else {
        return true;
    }
}


function field_mask(original_value, mask_value) {
    var new_value = '';
    var valid = true;

    for (var i = 0, j = 0; i < mask_value.length && j < original_value.length; i++, j++) {
        var mask_char = mask_value.charAt(i);

        if (mask_char == '?') {
            j--;
            continue;
        }

        var original_char = original_value.charAt(j);

        var optional = false;

        if (mask_value.length > (i + 1)) {
            if (mask_value.charAt(i + 1) == '?') {
                optional = true;
            }
        }

        if (mask_char == 'd') {
            //digit

            if (value_is_digit(original_char)) {
                new_value += original_char;
            }
            else {
                if (optional) {
                    j--;
                }
                else {
                    valid = false;
                    break;
                }
            }
        }
        else if (mask_char == 'a') {
            //alphanumeric

            if (value_is_alphanumeric(original_char)) {
                new_value += original_char;
            }
            else {
                if (optional) {
                    j--;
                }
                else {
                    valid = false;
                    break;
                }
            }
        }
        else if (mask_char == 'l') {
            //alpha

            if (value_is_char(original_char)) {
                new_value += original_char;
            }
            else {
                if (optional) {
                    j--;
                }
                else {
                    valid = false;
                    break;
                }
            }
        }
        else {
            //literal

            if (mask_char != original_char) {
                //insert literal
                new_value += mask_char;
                original_value = original_value.substring(0, j - 1) + mask_char + original_value.substring(j - 1);
            }
            else {
                new_value += original_char;
            }
        }
    }

    if (valid && original_value.length > mask_value.length) {
        valid = false;
    }

    return [new_value, valid];
}

function field_keyup_masking(obj, mask) {
    if (mask == null || mask.length == 0) {
        return;
    }

    var ret = field_mask(obj.value, mask);

    if (obj.value != ret[0]) {
        obj.value = ret[0];
    }
}

function value_is_digit(val) {
    return ('0123456789'.indexOf(val) > -1);
}

function value_is_char(val) {
    return ('abcdefghijklmnopqrstuvwxyz'.indexOf(val.toLowerCase()) > -1);
}

function value_is_alphanumeric(val) {
    return (value_is_digit(val) || value_is_char(val));
}

function doBeforePaste(control) {
    maxLength = control.attributes["maxLength"].value;
    if (maxLength) {
        event.returnValue = false;
    }
}

function doPaste(control) {
    maxLength = control.attributes["maxLength"].value;
    value = control.value;
    if (maxLength) {
        event.returnValue = false;
        maxLength = parseInt(maxLength);
        var oTR = control.document.selection.createRange();
        var iInsertLength = maxLength - value.length + oTR.text.length;
        var pasteText = window.clipboardData.getData("Text").replace(new RegExp("\\n", "g"), ' ');
        pasteText = pasteText.replace(new RegExp("\\r", "g"), ' ');
        var sData = pasteText.substr(0, iInsertLength);
        oTR.text = sData;
        if (pasteText.length > maxLength) {
            alert("You can only enter " + maxLength + " characters.");
        }
    }
}

function LimitInput(control) {
    maxLength = control.attributes["maxLength"].value;
    if (maxLength) {
        if (control.value.length > maxLength) {
            control.value = control.value.substring(0, maxLength);
            alert("You can only enter " + maxLength + " characters.");
        }
    }
}

function DiscardEnterKey(control) {
    var keycode;

    try {keycode = event.keyCode;}
    catch (ex) {keycode = event.charCode;}

    if (keycode == 13 || keycode == 10) {
        event.returnValue = false;
    }
}