// Javascript String Processing Functions
// 
// Code written by Webscope Services Ltd
// (scripting@webscopeservices.com)
//
// SUMMARY
//
// These functions are useful to determine what has been entered in text
// input boxes.  They were designed to be called from a javascript form 
// validation routine, not directly by using form control events.
//
// To make use these functions just add the following line into
// the <HEAD></HEAD> section of your web page and copy this file into the same
// directory.
//
// <script language="Javascript" src="stringProcessing.js"></script>
//

//
// This function is used to check a form field
// parameters: f = form name
// 			   c = control name
// 			   n = descriptive name
//			   t = data type
//			   o = optional flag
//			   max = number of characters / maximum value
//			   opt = optional parameter
function validateFormField(f,c,n,t,o,max,opt1,opt2)
{
    var ctl = eval("document."+f+"."+c);
	if(t=="dropdown"){
	    var s = ctl[ctl.selectedIndex].value;
	}else{
	    var s = ctl.value;
	}
	var strCheck = stripSpaces(s);
	if (strCheck.length==0 && o!=0)
	{
	    alert("The "+n+" is not optional, please specify.");
		ctl.focus();
		return false;
	}
	switch (t)
	{
		case "text":
		    if (strCheck.length>max)
			{
		        alert("The "+n+" you specified is too long (max "+max+" characters), please correct.");
				ctl.focus();
				return false;
		    }
			if (arguments.length > 6){
		        if (strCheck.length<opt1)
				{
		            alert("The "+n+" you specified is too short (min "+opt1+" characters), please correct.");
					ctl.focus();
					return false;
		    	}
			}
			if (arguments.length > 7){
			    if (opt2=="login"){
			        var res = validateForLogin(s,opt1,max,0);
					switch (res)
					{
						case 2:
		        		    alert("The "+n+" you specified contains invalid characters.  You can only use a-z and 0-9, please correct.");
							ctl.focus();
							return false;
						    break;
						default:
						    // continue
					}
				}
			    if (opt2=="securePassword"){
			        var res = validateForLogin(s,opt1,max,1);
					switch (res)
					{
						case 2:
		        		    alert("The "+n+" you specified contains invalid characters.  You can only use a-z and 0-9, please correct.");
							ctl.focus();
							return false;
						    break;
						case 3:
		        		    alert("The "+n+" you specified is not in the correct format.  It must contain at least 2 characters and 2 numbers, please correct.");
							ctl.focus();
							return false;
						    break;
						default:
						    // continue
					}
				}
			}
			break;
		case "integer":
		    if (strCheck.length>0)
			{
			    if (validateForIntegers(s)==false)
				{
			        alert("The "+n+" you specified has not been recognised as a valid whole number, please correct.");
					ctl.focus();
			    	return false;
				}
			    if (s.length>13)
				{
			        alert("The "+n+" you specified is too large, please correct.");
					ctl.focus();
			    	return false;
				}
				if (arguments.length > 6)
				{
    				if (s<opt1)
    				{
    		            alert("The "+n+" you specified is too small (minimum is "+opt1+"), please correct.");
    					ctl.focus();
    		    		return false;
    				}
				}
				if (s>max)
				{
		            alert("The "+n+" you specified is too large (maximum is "+max+"), please correct.");
					ctl.focus();
		    		return false;
				}
			}
			break;
		case "decimal":
		    if (strCheck.length>0)
			{
			    if (validateForDecimals(s)==false)
				{
			        alert("The "+n+" you specified has not been recognised as a valid number, please correct.");
					ctl.focus();
			    	return false;
				}
			    if (s.length>12)
				{
			        alert("The "+n+" you specified is too large, please correct.");
					ctl.focus();
			    	return false;
				}
				// max = the maximum value
				// opt1 = number of decimal places
				
				if (s>max)
				{
		            alert("The "+n+" you specified is too large, please correct.");
					ctl.focus();
		    		return false;
				}
				// find the position of the decimal point
				var pos1=s.indexOf(".");
				if (pos1>-1)
				{
				    // decimal point is found
					if ((s.length-pos1)>(opt1+1))
					{
			            alert("The "+n+" you specified has too many decimal places (maximum "+opt1+"dp), please correct.");
						ctl.focus();
			    		return false;
					}
				}
			}
			break;
		case "email":
		    if (strCheck.length>0)
			{
			    if (validateForEmail(s)==false)
				{
			        alert("The email you entered has not been recognised as a valid email, please correct before trying to update.");
					ctl.focus();
			    	return false;
				}
			}
			break;
		case "date":
		    if (strCheck.length>0)
			{
			    var strDate = verifyDate(s);
				ctl.value = strDate;
				if (strDate == "")
				{
					ctl.focus();
			    	return false;
				}
    			// ensure the date is not a future date
    			if (max<0)
    			{
    		        if (daysSinceNow(strDate)>0)
    				{
    			        alert(n+" cannot be a future date, please correct.");
						ctl.value="";
    					ctl.focus();
    			    	return false;
    				}
    			}
    			// ensure the date is not a historic date
    			if (max>0)
    			{
    		        if (daysSinceNow(strDate)<0)
    				{
    			        alert(n+" cannot be a historic date, please correct.");
						ctl.value="";
    					ctl.focus();
    			    	return false;
    				}
    			}
			}
			break;
		case "dropdown":
		    // Check to see if 'max' has been specified
			// If specified it is the value of the OPTION that represents no selection
			if (arguments.length > 5){
		        if (strCheck=max)
				{
		            alert("The "+n+" is not optional, please specify.");
					ctl.focus();
					return false;
		    	}
			}
			break;
		default:
	        alert("The data type was not recognised for this form field.");
			return false;
	}
	return true;
}

//
// This function strips out any spaces from the passed string and returns the result
// Useful to ensure a valid string has been passed and not just spaces
//
function stripSpaces(astr)
{
	var i=0;
	var c;
	var rstr="";
	for (i=0;i<astr.length;i++)
	{
		c = astr.substring(i,i+1);
		if (c!=" ") rstr = rstr + c
	}
	return rstr;
}
function stripLeadingSpaces(astr)
{
	var i=0;
	var x=-1;
	var c;
	var rstr="";
	if (astr==" "){
	    rstr = "";
	}else{
		for (i=0;i<astr.length;i++)
		{
		    c = astr.substring(i,i+1);
			if (c!=" " && x==-1) x=i
		}
		rstr = astr.substring(x,astr.length);
	}
	return rstr;
}

function validateForLogin(astr,minLen,maxLen,security)
{
	var i=0;
	var j=0;
	var c;
	var n;
	var cstr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var nstr="1234567890";
	var isOk=0;
	var isFound=false;
	var numInts=0;
	var numChars=0;
	if (astr.length<minLen){
	    isOk=-1;
	}else{
	    if (astr.length>maxLen)
		{
	        isOk=1;
		}else{
		    for (i=0;i<astr.length;i++)
			{
			    c = astr.substring(i,i+1);
				isFound=false;
				for (j=0;j<nstr.length;j++)
				{
				    n = nstr.substring(j,j+1);
					if (c==n)
					{
					    isFound=true;
						numInts+=1;
						break;
					}
				}
				if (isFound==false)
				{
    				for (j=0;j<cstr.length;j++)
    				{
    				    n = cstr.substring(j,j+1);
    					if (c==n)
    					{
    					    isFound=true;
    						numChars+=1;
    						break;
    					}
    				}
    				if (isFound==false)
    				{
    				    isOk=2;
    				    break;
    				}
				}
			}
			if (isOk==0 && security==1)
			{
			    if (numChars<2 || numInts<2) isOk=3
			}
		}
	}
	return isOk;
}
//
// This function takes a string and checks to ensure all the characters are numbers
// Returns True if successful, else returns False
//
function validateForIntegers(astr)
{	
	var i=0;
	var j=0;
	var c;
	var n;
	var nstr="1234567890";
	var isOk;
	for (i=0;i<astr.length;i++)
	{
		c = astr.substring(i,i+1);
		isOk=false;
		for (j=0;j<nstr.length;j++)
		{
			n = nstr.substring(j,j+1);
			if (c==n)
			{
				isOk=true;
				break;
			}
		}
		if (isOk==false) break
		else continue
	}
	return isOk;
}

//
// This function takes a string and checks to ensure all the characters are numbers.
// Returns True if successful, else returns False
//

function validateForDecimals(astr)
{	
	var i=0;
	var j=0;
	var c;
	var n;
	var nstr="1234567890.";
	var nd=0;
	var isOk;
	var pos1=0;
	var pos2=0;
	
	pos1=astr.indexOf(".");
	if (pos1>-1)
	{
		pos1=pos1+1;
		pos2=astr.indexOf(".",pos1);
		if (pos2>-1)
		{
			isOk = false;
			return isOk;
		}
	}
	
	for (i=0;i<astr.length;i++)
	{
		c = astr.substring(i,i+1);
		isOk=false;
		for (j=0;j<nstr.length;j++)
		{
			n = nstr.substring(j,j+1);
			if (c==n)
			{
				isOk=true;
				break;
			}
		}
		if (isOk==false) break
		else continue
	}
	return isOk;
}

//
// This function takes a string and checks to ensure all the characters are numbers.
// Returns True if successful, else returns False
//

function validateForEmail(astr)
{	
	var pos1=0;
	var pos2=0;
	
	pos1=astr.indexOf("@");
	if (pos1 <= 0)
	{
	   return false;
	}
	pos1=pos1+1;
	pos2=astr.indexOf(".",pos1);
	if (pos2 <= 0)
	{
	   return false;
	}
	return true;
}

// Javascript 1.2 Date Validation Functions
// 
// Version 1
// 5 Nov 2000
//
// Code written by Daniel Smith, Webscope Services Ltd
//
// SUMMARY
//
// These functions expect dates in the format of 'dd/mm/yy'.
//
// To validate a date simply call the function 'checkDate' and
// pass it the control to be validated.  The easiest way to do
// this is to add an onBlur event to the text input box i.e.
//
// <input type="text" value="" size="20" name="txtDate" onBlur="checkDate(this)">
//
// The checkDate function calls the 'verifyDate' function that does all the
// analysis.  This function uses the 'split' method of the string object which
// is why the routines are Javascript 1.2.
//
// To use these functions just add the following line into
// the <HEAD></HEAD> section of your web page and copy this file into the same
// directory.
//
// <script language="Javascript1.2" src="dateValidation.js"></script>
//
var leapdays = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
var yeardays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var todaysDate = new Date(); 

function isLeapYear( year ){
  // is it leap year ? returns a boolean
  return ( (0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))); 
  // ie, if the year divides by 4, but not by 100 except when it divides by
  // 400, it is leap year
} 
function isValidDayOfMonth( day, month, year)
{ 
  // determines whether a day is valid 
  // (ie, prevents Feb 30 from being processed) 
  // ( not used but left here for other pages that use 
  // this calendar, like the time-off request form ) 
  // 
  if (day <= 0) { return false; }
  if (isLeapYear(year)) { return (day <= leapdays[month])}
  return ( day <= yeardays[month]); 
} 
function canonicalDate(day, month, year) 
{ 
  // return the number of days since the Jan 0 2000 (ie, 1/1/2K returns 1, 31/12/1999 returns 0) 
  // for days before Jan 1 2000, returns negative numbers
  var canonDate = 0;
  // if the function had no arguments, use today's date; 
  var mday = todaysDate.getDate(); 
  var mmon = todaysDate.getMonth(); 
  var myr  = todaysDate.getFullYear(); 
  if( arguments.length > 0 ) 	 { mday = arguments[0];	 } 
  if( arguments.length > 1 ) 	 { mmon = arguments[1];	 } 
  if( arguments.length > 2 ) 	 { myr  = arguments[2];	 } 
 if(myr >= 2000) 
	{ canonDate += mday; 
	  while(mmon > 0)  { canonDate += isLeapYear(myr) ? leapdays[mmon]: yeardays[mmon]; mmon--;} 
	  while(myr > 2000){ canonDate += isLeapYear(myr) ? 366: 365; myr--;  } 
	} 
 else
	{ canonDate -= isLeapYear(myr) ? leapdays[mmon] - mday: yeardays[mmon] - mday; 
	  while(mmon < 11)  { mmon++; canonDate -= isLeapYear(myr) ? leapdays[mmon]: yeardays[mmon];} 
	  while(myr < 1999){ myr++; canonDate -= isLeapYear(myr) ? 366: 365;} 
	} 
 return canonDate; 
} 
function dateDiff(firstDate, secondDate) 
{ 
  // returns the result in days of subtracting firstDate from secondDate. Result is 
  // negative if secondDate came before firstDate. 
  var days= ( canonicalDate(secondDate.getDate(), secondDate.getMonth(), secondDate.getFullYear()) -  
				  canonicalDate(firstDate.getDate(), firstDate.getMonth(), firstDate.getFullYear())); 
  return days; 
}
function daysBetween(yr, mo, dy) {
    var SECOND = 1000; // the number of milliseconds in a second
    var MINUTE = SECOND * 60; // the number of milliseconds in a minute
    var HOUR = MINUTE * 60; // the number of milliseconds in an hour
    var DAY = HOUR * 24; // the number of milliseconds in a day
    var WEEK = DAY * 7; // the number of milliseconds in a week
    
    var nDate = new Date(); // current date (local)
    var nTime = nDate.getTime(); // current time (UTC)
    var dTime = Date.UTC(yr, mo, dy); // specified time (UTC)
	
    var nowDays = (nTime/DAY);
	var specifiedDays = (dTime/DAY);
	if (Math.round(nowDays)>nowDays){
	    nowDays = Math.round(nowDays)-1;
	}else{
	    nowDays = Math.round(nowDays);
	}
	if (Math.round(specifiedDays)>specifiedDays){
	    specifiedDays = Math.round(specifiedDays)-1;
	}else{
	    specifiedDays = Math.round(specifiedDays);
	}
    return (specifiedDays-nowDays); //Math.round(bTime / DAY);
}
function daysSinceNow(s)
{
	// Breakdown the seperate parts of the date
	var strMu = new String("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec");
	var arrMonths = strMu.split(",");
	var arrDate = s.split(" ");
	var part1 = arrDate[0];
	var part2 = arrDate[1];
	var part3 = arrDate[2];
	var m = 0;
	for (i=0;i<=11;i++){
	    if (arrMonths[i]==part2){
		    m = i;
		}
	}
	var d = daysBetween(part3,m,part1);
	return d;
}
function convertStringIntoDate(s)
{
	// Breakdown the seperate parts of the date
	var strMu = new String("Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec");
	var arrMonths = strMu.split(",");
	var arrDate = s.split(" ");
	var part1 = arrDate[0];
	var part2 = arrDate[1];
	var part3 = arrDate[2];
	var m = 0;
	for (i=0;i<=11;i++){
	    if (arrMonths[i]==part2){
		    m = i;
		}
	}
	var d = new Date(part3,m,part1);
	return d;
}

function checkDate(ctrl)
{
//ctrl is a reference to the input box object holding the date
	var str = ctrl.value;
	str = stripLeadingSpaces(str);
	if (str.length>0)
	{
		var strDate = verifyDate(str);
		if (strDate == "")
		{
			ctrl.value = "";
			ctrl.focus();
		} else {
			ctrl.value = strDate;
		}
	}
}

function verifyDate(strDate)
{
	var i = 0;
	var mnth = 0;
	var pos1 = 0;
	var pos2 = 0;
	var strTemp = "";
	var numChars = strDate.length;
	
	// Check for the delimiting character
	var strD = getDelimiter(strDate);
	if (strD=="")
	{
		alert("The entry has not been recognised as a valid date.");
		return "";
	}
	
	// Breakdown the seperate parts of the date
	var arrDate = strDate.split(strD);
	var part1 = arrDate[0]+'';
	var part2 = arrDate[1]+'';
	var part3 = stripSpaces(arrDate[2]+'');

	// part1 = day
	// Check that the value is purely numbers
	if (validateForIntegers(part1)==false)
	{
		alert("The entry ahs not been recognised as a valid date, please try again.");
		return "";
	}
	intPart1 = parseInt(part1);
	
	// Define array of months
	var strMu = new String("blank,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec");
	var strMl = new String("blank,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec");
	var arrMu = strMu.split(",");
	var arrMl = strMl.split(",");
	
	// part2 = month
	// Check that if the value is purely numbers
	if (validateForIntegers(part2)==true)
	{
	// Check that it is between 1 and 12
		var intPart2 = parseInt(part2,10);
		if (intPart2<1 || intPart2>12)
		{
			alert("The date entered does not have a valid month, please try again.");
			return "";
		}
		mnth=intPart2;
	} else {
	// Check that if the value is text then it matches either the short or long version of the month
		var strPart2 = part2.substring(0,3);
		for (i=1; i<=12; i++)
		{
			if (strPart2==arrMu[i])
			{
				mnth = i;
			}
			if (strPart2==arrMl[i])
			{
				mnth = i;
			}
		}
		if (mnth==0)
		{
			alert("The date entered does not have a valid month, please try again.");
			return "";
		}
	}
	part2 = arrMu[mnth];
	
	// part3 = year
	// Check that the value is purely numbers
	if (part3.length==0)
	{
		alert("No year has been recognised for this date. Please include the year when entering a date.");
		return "";
	}
	if (validateForIntegers(part3)==false)
	{
		alert("The year has not been recognised as a number, please try again.");
		return "";
	}
	
	// If there are only 2 numbers then derive the 4 number year
	if (part3.length<3)
	{
		var intPart3 = parseInt(part3,10);
		if (intPart3>70)
		{
			part3 = "19" + part3
		} else {
		    if (part3.length==1) {
				 part3 = "200" + part3
			}else{
				 part3 = "20" + part3
			}
		}
	}else{
		  if (part3.length>4){
		  	 part3 = part3.substring(0,4)
		  }
	}
	intPart3 = parseInt(part3);
	
	// Check that the date IS a valid date
	if (isValidDayOfMonth(intPart1,(mnth-1),intPart3)==false){
		alert("The date you entered has not been recognised as a valid date, please try again.");
		return "";
	}
	
	strDate = part1 + " " + part2 + " " + part3;
	return strDate;
}

//
// This function derives which of the acceptable delimiters have been used
//
function getDelimiter(str)
{
	var strD = "";
	var pos1 = 0;
	var pos2 = 0;
	
	pos1 = str.indexOf(" ");
	if (pos1>=0) strD = " ";

	if (strD=="")
	{
		pos1 = str.indexOf("-");
		if (pos1>=0) strD = "-";
	}
	if (strD=="")
	{
		pos1 = str.indexOf("/");
		if (pos1>=0) strD = "/";
	}
	if (strD=="")
	{
		pos1 = str.indexOf("\\");
		if (pos1>=0) strD = "\\";
	}

	// Check for a second occurance of the delimiter
	if (strD!="")
	{
		pos2 = str.indexOf(strD,pos1);
		if (pos2==0) strD = "";
	}
	return strD;
}

// These functions are to trap key press actions to stop invalid entries into a field
// Example use : onkeydown="blockEntry();"

function blockEntry(){
    event.returnValue=false;
}
function allowNumbers(c,size)
{
    var myCode = event.keyCode;
	var allowed = false;
	var myStr = "";
  	if( arguments.length > 0){
	    myStr = c.value+"";
	}
	if (myCode==8) allowed=true; // Backspace
	if (myCode==9) allowed=true; // Tab
	if (myCode>=48 && myCode<=57) allowed=true; // Numbers
	if (allowed) {
	    // if a number has been entered then check to see if number of characters entered is ok
  	    if( arguments.length > 1){
			// check to see if the number of characters entered already is equal to 'size'
			if (myStr.length>=size){
		        event.returnValue=false;
				return;
			}
		}
	}
	if (!allowed) event.returnValue=false;
}
function allowDecimal(c,size,dp)
{
    var myCode = event.keyCode;
	var allowed = false;
	var myStr = c.value+"";
	var i=0;
	var hasDecimal=0;
	if (myStr.length>0)
	{
    	for (i=0;i<myStr.length;i++)
    	{
    		if (myStr.substring(i,i+1)==".") hasDecimal=i+1;
    	}
	}
	if (myCode==8) allowed=true; 			       // Backspace
	if (myCode==9) allowed=true; 				   // Tab
	if (myCode>=48 && myCode<=57) allowed=true;    // Numbers
	if (allowed) {
	    // if a number has been entered then check to see if number of characters entered is ok
  	    if( arguments.length > 1){
			// check to see if the number of characters entered already is equal to 'size'
			if (hasDecimal==0 && myStr.length>=size){
		        event.returnValue=false;
				return;
			}
		}
	}
	if (myCode==46 && hasDecimal==0) allowed=true; // Period
  	if( arguments.length > 2  && allowed){
		// check to see if the number of decimal places have already been used
		if (hasDecimal>0 && (myStr.length-hasDecimal)>=dp){
		    event.returnValue=false;
			return;
		}
	}
	if (!allowed) event.returnValue=false;
}
function allowEmailChars(c)
{
    var myCode = event.keyCode;
	var allowed = false;
	var myStr = c.value+"";
	var i=0;
	var hasAt=0;
	if (myStr.length>0)
	{
    	for (i=0;i<myStr.length;i++)
    	{
    		if (myStr.substring(i,i+1)=="@") hasAt=i+1;
    	}
	}
	if (myCode==8) allowed=true; 			 	// Backspace
	if (myCode==9) allowed=true; 				// Tab
	if (myCode==45) allowed=true; 				// - (minus)
	if (myCode==46) allowed=true; 				// . (period)
	if (myCode==64 && hasAt==0) allowed=true;   // @ symbol
	if (myCode==95) allowed=true; 			    // _ (underscore)
	if (myCode>=48 && myCode<=57) allowed=true; // Numbers
	if (myCode>=65 && myCode<=90) allowed=true; // Capital Letters
	if (myCode>=97 && myCode<=122) allowed=true;// Letters
	if (!allowed) event.returnValue=false;
}
function allowAlphaNumericOnly()
{
    var myCode = event.keyCode;
	var allowed = false;
	if (myCode==8) allowed=true; // Backspace
	if (myCode==9) allowed=true; // Tab
	if (myCode>=48 && myCode<=57) allowed=true; // Numbers
	if (myCode>=65 && myCode<=90) allowed=true; // Capital Letters
	if (myCode>=97 && myCode<=122) allowed=true;// Letters
	if (!allowed) event.returnValue=false;
}
function allowAlphaNumeric()
{
    var myCode = event.keyCode;
	var allowed = false;
	if (myCode==8) allowed=true; // Backspace
	if (myCode==9) allowed=true; // Tab
	if (myCode==32) allowed=true; // Space
	if (myCode>=48 && myCode<=57) allowed=true; // Numbers
	if (myCode>=65 && myCode<=90) allowed=true; // Capital Letters
	if (myCode>=97 && myCode<=122) allowed=true;// Letters
	if (!allowed) event.returnValue=false;
}
function allowAlphaNumericPlus()
{
    var myCode = event.keyCode;
	var allowed = false;
	if (myCode==8) allowed=true; // Backspace
	if (myCode==9) allowed=true; // Tab
	if (myCode==32) allowed=true; // Space
	if (myCode>=48 && myCode<=57) allowed=true; // Numbers
	if (myCode>=65 && myCode<=90) allowed=true; // Capital Letters
	if (myCode>=97 && myCode<=122) allowed=true;// Letters
	if (!allowed) event.returnValue=false;
}
function allowText(c,l)
{
    var myCode = event.keyCode;
	var allowed = false;
	if (myCode==8) allowed=true; // Backspace
	if (myCode==9) allowed=true; // Tab
	if (myCode==13) allowed=true; // Return
	if (myCode==32) allowed=true; // Space
	if (myCode==33) allowed=true; // !
	if (myCode==34) allowed=true; // "
	if (myCode==36) allowed=true; // $
	if (myCode==37) allowed=true; // %
	if (myCode==38) allowed=true; // &
	if (myCode==39) allowed=true; // '
	if (myCode==40) allowed=true; // (
	if (myCode==41) allowed=true; // (
	if (myCode==42) allowed=true; // *
	if (myCode==43) allowed=true; // +
	if (myCode==44) allowed=true; // ,
	if (myCode==45) allowed=true; // -
	if (myCode==46) allowed=true; // .
	if (myCode==47) allowed=true; // /
	if (myCode==60) allowed=true; // less than
	if (myCode==62) allowed=true; // greater than
	if (myCode==63) allowed=true; // ?
	if (myCode==95) allowed=true; // _
	if (myCode==163) allowed=true; // £
	if (myCode>=48 && myCode<=57) allowed=true; // Numbers
	if (myCode>=65 && myCode<=90) allowed=true; // Capital Letters
	if (myCode>=97 && myCode<=122) allowed=true;// Letters
  	if( arguments.length > 1){
		// check if the amount enterd is the maximum
	    if(c.value.length>=l) allowed=false
	}
	if (!allowed) event.returnValue=false;
}
function showKeyCode(t)
{
    var myCode = event.keyCode;
	t.value = myCode;
}
function createBarCode(ctl)
{
    var companyBarCodeID = "5025123";
    var myStr = ctl.value;
	var s = "";
	
    var myCode = event.keyCode;
	var allowed = false;
	if (myCode==8) allowed=true; // Backspace
	if (myCode>=48 && myCode<=57) allowed=true; // Numbers
	if (!allowed)
	{
	    event.returnValue=false;
	}else{
        switch (myStr.length)
    	{
    	    case 0:
    			// There is nothing entered so add the company bar code to the beginning
				if (myCode!=8) ctl.value = companyBarCodeID;
    			break;
    		case 7:
    			// Check to see if the code has been entered and deleted
    			if (myStr==companyBarCodeID) ctl.value="";
    			break;
    		default:
    			// Check that the initial 7 characters match the company bar code ID
    			s = myStr.substring(0,7);
    			if (s!=companyBarCodeID)
    			{
    			    myStr = companyBarCodeID+myStr;
    				myStr = myStr.substring(0,12);
    				ctl.value = myStr;
    			} 
    	}
	}
}
