/* VD Basic Functionality*/
	function assert( fact , details ){
		if(!fact){
			var msg = "Assert failure! "+details;

			try{

				if(arguments.callee.caller != null){
					msg = msg + " in function "+arguments.calle.caller.toString().match( /function\s+(\w+)/ )[1];
				}
			}catch(e){}
			alert(callee);
		}
	}
	
	function checkForForm( input ){ if(!input.form){ alert('Calling function requires to be inside form') } return false;}
	
	/* PAGE LISTENERS */
	var skey;var scode;var shiftcode;var kpcode;var kdcode;var kucode;var kpkey;var kdkey;var kukey;
	function finder(evt){ 
		evt=(evt) ? evt : ((event) ? event : null);
		if(evt.charCode){ scode=evt.charCode; } else if(evt){ scode=evt.keyCode; }
		if(evt.charCode){ skey=String.fromCharCode(evt.charCode); } else if(evt){ skey=String.fromCharCode(evt.keyCode) }
		if(evt.type=='keyup'){ kucode=scode; kukey=skey }
		if(evt.type=='keydown'){ kdcode=scode; kdkey=skey }
		if(evt.type=='keypress'){ kpcode=scode; kpkey=skey }
		if(evt.shiftKey==1){ shiftcode=1; } else{ shiftcode=0; }
	
	//	document.getElementById('test').innerHTML='KeyPress: '+kpkey+' / '+kpcode+' - KeyDown: '+kdkey+' / '+kdcode+' - KeyUp: '+kukey+' / '+kucode;
	}
	
	document.onkeydown=finder;
	document.onkeyup=finder;
	document.onkeypress=finder;
	document.onmousedown=finder;
	document.onmouseup=finder;
	/* end */

	/* CURSOR FOCUS SPECIFIC FUNCTIONS */
		function autotab(input,charchar){
			checkForForm(input);
			if(input.value.length==charchar && kdcode!=9 && kdcode!=37 && kdcode!=39 && kdcode!=13 && kdcode!=8){ 
				var ifl=input.form.length;
				for(i=0; i <= ifl; ++i){ 
					if(input == input.form[i]){
						for(var t=0; t<10000; ++t){
							++i;
							temp=input.form[i].type;
							if(temp=='text'){ 
								input.form[i].select();i=input.form.length; break; break;
							}else if(temp=='button' || temp=='select-one'){
								input.form[i].focus();i=input.form.length; break; break;
							}
						}
					}
				}
			} 
		}
		
		function deletemoveback(input){
			checkForForm(input);
			var i=0; var ivl=input.value.length;
			if(kdcode==8 && ivl==0){
				var ifl=input.form.length;
				for (i=0; i < ifl; ++i){
					if(input == input.form[i]){
		
						--i;
						if(input.form[i].type=='text'){
							input.form[i].select();
						} else{
							input.form[i].focus();
						}
						break;
					}
				}
			}
		}
		
		function periodtab(input){ 
			checkForForm(input);
			if(input.value.search(/\./) > 0){
				var i=input.value.replace(/\./g,'');
				input.value=i;
				var ifl=input.form.length;
				for (i=0; i < ifl; ++i){
					if(input==input.form[i]){
						++i;
						if(input.form[i].type=='text'){
							input.form[i].select()
						}else{
							input.form[i].focus();
						}
						break;
					}
				}
		
			}
		}
		

		function spacetab(input){
			checkForForm(input);
			i=0;input.value=input.value.replace(/ /g,'');

			if(kpcode==32){
				var ifl=input.form.length;
		
				for (i=0; i < ifl; ++i){
		
					if(input == input.form[i]){ ++i; if(input.form[i].type=='text'){input.form[i].select()}else{input.form[i].focus()} break; }
				}
			}
		}
		
		function dashtab(input){ 
			checkForForm(input);
			i=0;
			if(kpcode==45){ 
				var ifl=input.form.length;
				for (i=0; i < ifl; ++i){ 
		
					if(input == input.form[i]){
						++i; input.form[i].focus(); i=input.form.length;
					}
				}
		
			}
		}
	/* end CURSOR FOCUS SPECIFIC FUNCTIONS */

	function returnNumbers( string ){
		var t='';
		for( i=0; i < string.length; ++i){
			var tt=string.charAt(i);
			if(tt==0 || tt==1 || tt==2 || tt==3 || tt==4 || tt==5 || tt==6 || tt==7 || tt==8 || tt==9 ){
				t+=tt;
			}
		}
		return t;
	}
	
	function numberonly( input , strictMode ){
		var t='';
		var tvl=input.value.length;
		if(typeof strictMode=='undefined'){ var strictMode=null }
	
		for( i=0; i < tvl; ++i){
			tt=input.value.charAt(i);
			if(tt==0 || tt==1 || tt==2 || tt==3 || tt==4 || tt==5 || tt==6 || tt==7 || tt==8 || tt==9 || ( ( tt=='-' || tt=='.' ) && !strictMode )){
				t+=tt;
			}
		}
		if(input.value!=t){input.value=t};
	}

	/* TIME FORMATTING AND VALIDATION */
		/* CALL THIS FUNCTION FOR PROPER TIME FORMATTING */
		function timeFormat( input , e ){
			var timeArray=input.value.split(':');
			var eKey=getEventKey( e );
			var Validate=0;
	
			if(eKey!=8){
				var newValue='';		
				if(timeArray.length==3 && timeArray[2].search(/ /) >= 0){
					var tempArray=timeArray[2].split(' ');
					timeArray[2]=tempArray[0];
					timeArray[3]=tempArray[1];
				}
		
				for(var i=0;i < timeArray.length; ++i){
					if(i < 2){
						timeArray[i]=returnNumbers( timeArray[i] );
					}
					if(i < 2){
						if(timeArray[i].length==2){
							newValue+=timeArray[i]+':'; 
						}else if(  i!=(timeArray.length-1) ){
							newValue+=timeArray[i]+':'; 
						}else{
							newValue+=timeArray[i];
						}
					}else if(i==2){
						if(timeArray[i].length==2){
							newValue+=timeArray[i]+' '; 
						}else{
							newValue+=timeArray[i];
						}			
					}else{
						if(timeArray[i].length > 2){
							timeArray[i]=timeArray[i].substr(0,2);
						}
						newValue+=timeArray[i];
						Validate=1;
					}
				}
				input.value=newValue;
			}
		}
	
		/* CALL THIS FUNCTION FOR PROPER TIME VALIDATION */
		function validateTime( input ){
			if( input.value.substr( (input.value.length-1) , 1) == ':' || input.value.substr( (input.value.length-1) , 1) == ' ' ){
				input.value=input.value.substr( 0 , (input.value.length-1) );
			}
			
			if(input.value.length==0){
				input.value=input.defaultValue
			}else if( !IsValidTime(input.value) ){
				if(confirm('Replace Time with Default Value?')){
					input.value=input.defaultValue;
				}
			}
		}
		
		/* CALL THIS FUNCTION TO CHECK IF TIME IS VALID */
		function IsValidTime(timeStr) {
			
			// Checks if time is in HH:MM:SS AM/PM format.
			// The seconds and AM/PM are optional.
			
			var timePat = /^(\d{1,2}):(\d{1,2})(:(\d{1,2}))?(\s?(AM|am|PM|pm))?$/;
			
			var matchArray = timeStr.match(timePat);
			if (matchArray == null) {
				alert("Time is not in a valid format.");
				return false;
			}
			hour = matchArray[1];
			minute = matchArray[2];
			second = matchArray[4];
			ampm = matchArray[6];
			
			if (second=="") { second = null; }
			if (ampm=="") { ampm = null }
			
			if (hour < 0  || hour > 23) {
				alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
				return false;
			}
			if  (hour > 12 && ampm != null) {
				alert("You can't specify AM or PM for military time.");
				return false;
			}
			if (minute<0 || minute > 59) {
				alert ("Minute must be between 0 and 59.");
				return false;
			}
			if (second != null && (second < 0 || second > 59)) {
				alert ("Second must be between 0 and 59.");
				return false;
			}
			return true
		}
	
	/* END */

	/* IP NUMBER FORMATTING AND VALIDATION */
		function validateIpNumber( input , fillNumber ){
			if( input.value.length > 0 && !isValidIpNumber(input.value) && confirm('Invalid IP Number\nReplace with Default Value?')){
				input.value=input.defaultValue
			}else if( input.value.length > 0 && typeof fillNumber != 'undefined'){
				var inputArray=input.value.split('.');
				var newValue='';
				for(var i=0; i < 4; ++i){
					if(inputArray.length > i && inputArray[i].length > 0){
						newValue+=inputArray[i];
					}else{
						newValue+=fillNumber;
					}
					if(i!=3){newValue+='.';}
				}
				input.value=newValue;
			}
		}
		function formatIpNumber( p_obj , p_event ){
			numberonly(p_obj);
			var v_key = getEventKey(p_event);
			var ipSets=p_obj.value.split('.');
			var v_obj=p_obj.value;
			
			if( ipSets.length && v_key!=8 && v_key != 37 && v_key != 39 ){
				v_obj='';
				for(i=0; i < ipSets.length; ++i){
					if(ipSets[i].length > 3 && i == 3){ ipSets[i]=ipSets[i].substr(0,3); }
					v_obj+=ipSets[i];
					if( ( ipSets[i].length == 3 && i!=3 && p_obj.value.substr(p_obj.value.length-1,1) != '.' ) || i!=(ipSets.length-1) ){
						v_obj+='.';
					}
				}
			}
			if( !isNaN( String.fromCharCode( v_key ) ) || v_key == 190 || v_key == 110 ){
				v_obj=v_obj.replace(/\.\./g,'.');
				p_obj.value = v_obj;
			}
			if(ipSets.length > 3 && ipSets[3].length == 3 && v_key!=13 && v_key != 37 && v_key != 39  ){
				if(!isValidIpNumber( p_obj.value )){
					alert('IP Number is invalid')
					p_obj.select();
				}
			}
		}
		
		function isValidIpNumber( IPvalue ) { 
			if(IPvalue.length < 1){return false}
			var ipArray=IPvalue.split(".");
			if (!ipArray||!ipArray[0]||!ipArray[1]||!ipArray[2]||!ipArray[3]||ipArray.length>4){
				return false
			}else { 
				for (i = 0; i < 4; i++) { 
					thisSegment = ipArray[i]; 
					if (thisSegment > 255 || thisSegment < 0) { 
						return false
						i = 4;
					}
				}
			}
			return true; 
		} 
	/* end */

function getEventKey(p_event){
	var v_key;
	p_event=(p_event) ? p_event : ((p_event) ? p_event : null);
	if(p_event.keyCode){ v_key=p_event.keyCode; } else if(p_event.charCode){ v_key=p_event.charCode; }
	return v_key
}

function isValidEmail(emailaddress) {
	var email = /\b(^[a-z0-9-_\.]*[a-z0-9-_\.]+@[a-z0-9-_\.]*[a-z0-9-_\.]+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.biz)|(\.info)|(\..{2,2}))$)\b/gi
	return email.test(emailaddress)
}

function isValidName(name) {
	var validName = /[^a-zA-Z]/;
	return !validName.test(name);
}

function GetRawNumber( p_num ) {
	var v_output = '';
   
	for (var v_idx = p_num.length - 1; v_idx >= 0; v_idx --) {
		var v_char = p_num.substr(v_idx,1);
		if (v_char >= '0' && v_char <= '9') v_output = v_char + v_output;
	}
	return v_output;
}

function NoLeadZeros( p_num ) {
   var v_out2 = '';
   var fnd = 0;
   
   for (var v_idx = 0; v_idx < p_num.length; v_idx ++) {
     var v_char = p_num.substr(v_idx,1);
     if (v_char > '0') fnd = 1;
	 if (fnd == 1) v_out2 = v_out2 + v_char;
   }
   
   return v_out2;
}

function NumericFormater( p_num, p_format ) {
	if (p_format.indexOf(',') != -1){ 
		var v_num = GetRawNumber( p_num ); 
		var v_numidx = v_num.length - 1;
		var v_formatlen = p_format.length;
		v_chr = p_format.substr(0,1);
		p_format = p_format.substr(0,v_formatlen);
		var v_output = p_num;
		if (p_num.length <= p_format.length) {
			var v_output = '';
			for (var i=v_formatlen-1; i>=0; i--) {
				if (p_format.substr(i,1) == 'N') {
					v_output = v_num.substr(v_numidx,1) + v_output;
					v_numidx --;
				}else{
					v_output = p_format.substr(i,1) + v_output;
				}
				if (v_numidx < 0) break;
			}
			return v_output;
		}else return v_output.substr(0,p_format.length)
	}else {
		var v_num = GetRawNumber( p_num );
		var v_numlen = v_num.length;
		var v_numidx = 0;
		var v_formatlen = p_format.length;
	
		var v_output = '';
		for (var i=0; i<v_formatlen; i++) { 
			if (p_format.substr(i,1) == 'N') {
				if (v_numidx >= v_numlen) break;
				v_output = v_output + v_num.substr(v_numidx,1);
				v_numidx ++;
			} else {
				v_output = v_output + p_format.substr(i,1);
			}
		}
		return v_output;
	}
}

function DollarFormater( p_num, p_format ) {
   var v_num = GetRawNumber( p_num ); 
  // alert(v_num)
   //var v_num = NoLeadZeros(GetRawNumber( p_num ));
   //if (v_num == 0) return '';

   var v_numidx = v_num.length - 1;
   var v_formatlen = p_format.length - 1;
   v_chr = p_format.substr(0,1);
   p_format = p_format.substr(1,v_formatlen);

   var v_output = '';
   for (var i=v_formatlen-1; i>=0; i--) {
      if (p_format.substr(i,1) == 'N' | p_format.substr(i,1) == '0') 
	  	{
	     v_output = v_num.substr(v_numidx,1) + v_output;
		 v_numidx --;
		} 
	  else 
	  	{
	     v_output = p_format.substr(i,1) + v_output;
		}
	
	  if (v_numidx < 0) break;
   }

   for (var j=i - 1; j>=0; j--) {
//     if (p_format.substr(j,1) == 'N') break;
	 if (p_format.substr(j,1) == '0' | p_format.substr(j,1) == '.') v_output = p_format.substr(j,1) + v_output;
   }
   
   if (v_chr == '$') v_output = '$' + v_output;
   
   return v_output;
}

function IsDate( p_date,field ) { 
   var v_dash1 = p_date.indexOf('/',1);
   var v_dash2 = p_date.indexOf('/',v_dash1 + 1);
   var v_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

   if (v_dash1 == 0 || v_dash2 == 0) return 'false';

   var v_month = p_date.substr(0,v_dash1);
   var v_day = p_date.substr(v_dash1 + 1,v_dash2 - v_dash1 - 1);
   var v_year = p_date.substr(v_dash2 + 1);
   var v_leap_year = false;

   if (v_year >= 1890) {
      // We have a valid year
      if (v_year % 4 == 0) v_leap_year = true;
   } else {
      alert("Invalid Year.\nPlease re-enter.");
	  if (field) {
	  	field.focus();
		field.select();
	 }
	 return false;
   }

   if (v_month >= 1 && v_month <= 12) {
      // We have a valid month
      if (v_month == 2 && v_leap_year) v_days[1] = 29;
   } else {

      alert("Invalid Month.\nPlease re-enter.");
	  if (field) {
	  	field.focus();
		field.select();
	 }      return false;
   }

   if (v_day >= 1 && v_day <= v_days[v_month - 1]) {
      // We have a valid day
   } else {
      alert("Invalid Day.\nPlease re-enter.");
	  if (field) {
	  	field.focus();
		field.select();
	 }
      return false;
   }

   return true;
}

function IsDateTime( p_datetime ) {
   //date and time are separated by a space..so isolate with split() 

   var v_datetime = p_datetime.split(" "); 
   var v_date = v_datetime[0];
   var v_time = v_datetime[1];
	
   //parse and validate date
   var v_dash1 = v_date.indexOf('/',1);
   var v_dash2 = v_date.indexOf('/',v_dash1 + 1);
   var v_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

   if (v_dash1 == 0 || v_dash2 == 0) return 'false';

   var v_month = v_date.substr(0,v_dash1);
   var v_day = v_date.substr(v_dash1 + 1,v_dash2 - v_dash1 - 1);
   var v_year = v_date.substr(v_dash2 + 1);
   var v_leap_year = false;
   

   if (v_year >= 1890) {
      // We have a valid year
      if (v_year % 4 == 0) v_leap_year = true;
   } else {
      alert("Invalid Year.\nPlease re-enter.");
      return false;
   }

   if (v_month >= 1 && v_month <= 12) {
      // We have a valid month
      if (v_month == 2 && v_leap_year) v_days[1] = 29;
   } else {
      alert("Invalid Month.\nPlease re-enter.");
      return false;
   }

   if (v_day >= 1 && v_day <= v_days[v_month - 1]) {
      // We have a valid day
   } else {
      alert("Invalid Day.\nPlease re-enter.");
      return false;
   }

   //parse and validate time
   var v_time = v_time.split(":");
   var v_hours = v_time[0];

   if (v_hours > 23) {
   	alert("Invalid Hours.\nPlease re-enter.")
   	return false;
   }
   
   var v_minutes = v_time[1];   
   if (v_minutes > 59) {
   	alert("Invalid Minutes.\nPlease re-enter.")
   	return false;
   }   

   if (v_time.length == 3) {	
	   var v_seconds = v_time[2];   
	   if (v_seconds > 59) {
	   	alert("Invalid Seconds.\nPlease re-enter.")
	   	return false;
   	   }   
   }
   
   return true;
}

function isValidBirthDate(whichfield,yrsdiff,todaydate)
{
	today = new Date();
	target = new Date();

	target.setMonth(eval(whichfield.value.substring(0,2) - 1));
	target.setDate(whichfield.value.substring(3,5));
	target.setYear(whichfield.value.substring(6,10));

	today.setMonth(eval(todaydate.substring(0,2) - 1));
	today.setDate(todaydate.substring(3,5));
	today.setYear(todaydate.substring(6,10));

	// Calculate difference between the two dates
	diff = target.getTime() - today.getTime();

	if(diff < 0)
		{
		diff = parseInt(diff / (1000 * 60 * 60 * 24 * 365));
		if(diff > -18)
			{
			alert('Invalid birthdate, please double-check the date and try again!');
			whichfield.focus();
			}
		}
	else 
		{
		alert('Invalid birthdate, please double-check the date and try again!');
		whichfield.focus();
		}
		
	if (whichfield.value.length < 10) {
		alert('Invalid birthdate, please double-check the date and try again!');
		whichfield.focus();
	}
}
	
function IsExpDate( p_date ) {
   var v_dash1 = p_date.indexOf('/',1);

   if (v_dash1 == 0) return 'false';

   var v_month = p_date.substr(0,v_dash1);
   var v_year = p_date.substr(v_dash1 + 1);
   var v_leap_year = false;

   if (v_year >= 1890) {
      // We have a valid year
      if (v_year % 4 == 0) v_leap_year = true;
   } else {
      alert("Invalid Expiration Year.\nPlease re-enter.");
      return false;
   }

   if (v_month >= 1 && v_month <= 12) {
      // We have a valid month
   } else {
      alert("Invalid Expiration Month.\nPlease re-enter.");
      return false;
   }

   return true;
}

function IsMonthDate( p_date ) {
   var v_dash1 = p_date.indexOf('/',1);

   if (v_dash1 == 0) return 'false';

   var v_month = p_date.substr(0,v_dash1);
   var v_year = p_date.substr(v_dash1 + 1);
   var v_leap_year = false;

   if (v_year >= 1890) {
      // We have a valid year
      if (v_year % 4 == 0) v_leap_year = true;
   } else {
      alert("Invalid Year.\nPlease re-enter.");
      return false;
   }

   if (v_month >= 1 && v_month <= 12) {
      // We have a valid month
   } else {
      alert("Invalid Month.\nPlease re-enter.");
      return false;
   }

   return true;
}


function DateValidate( p_obj, p_event ) {
	var v_format = "NN/NN/NNNN";
	var v_obj = p_obj.value;
	var v_len = v_obj.length;
	var v_key=getEventKey(p_event);
		
	// If a backspace was entered, and it deleted a '/' then remove one more charater.
	if (v_key != 191 && v_key == 8 && v_format.substr(v_len,1) != "N"){
		v_obj = v_obj.substr(0,v_len - 1);
	}

	// Format the entry
	v_obj = NumericFormater( v_obj , v_format );
   
	if ( v_obj.length == v_format.length && v_key!=13 && v_key != 37 && v_key != 39 && v_key != 8 ) {
		// We have enough charaters, so see if it's a good date.
		if (IsDate(v_obj)) {
			p_obj.value = v_obj;
		} else {
			p_obj.select();
		}
	}else if( v_key!=13 && v_key != 37 && v_key != 39 && v_key != 8 ){
		// required.
		p_obj.value = v_obj;
	}
}


function DateTimeValidate( p_obj, p_event ) { 
	
	var v_format = "NN/NN/NNNN NN:NN:NN";
	var v_obj = p_obj.value;
	var v_len = v_obj.length;
	var v_key = getEventKey( p_event );

	if (v_key != 9 && v_key != 16){
		// If a backspace was entered, and it deleted a '/' then remove one more charater.
		if (v_key == 8 && v_format.substr(v_len,1) != "N") v_obj = v_obj.substr(0,v_len - 1);
	}
	
	// Format the entry
	v_obj = NumericFormater(v_obj,v_format);
	
	if (v_obj.length == v_format.length) {
	   // We have enough charaters, so see if it's a good date.
	   if ( IsDateTime(v_obj) && v_key!=13) {
		  p_obj.value = v_obj;
	   } else {
		  p_obj.select();
	   }
	} else {
	   // Don't test it yet, just pass it on.
	   p_obj.value = v_obj;
	}

}

function ExpirationValidate( p_obj, p_event ) {
	var v_format = "NN/NNNN";
	var v_obj = p_obj.value;
	var v_len = v_obj.length;
	var v_key = getEventKey( p_event );

	// If a backspace was entered, and it deleted a '/' then remove one more charater.
	if (v_key == 8 && v_format.substr(v_len,1) != "N") v_obj = v_obj.substr(0,v_len - 1);

	// Format the entry
	v_obj = NumericFormater(v_obj,v_format);
   
	if (v_obj.length == v_format.length) {
		// We have enough charaters, so see if it's a good date.
		if ( IsExpDate(v_obj) && v_key!=13 ) {
			p_obj.value = v_obj;
		} else {

			p_obj.select();
		}
	} else {
		// Don't test it yet, just pass it on.
		p_obj.value = v_obj;
   }
}

function MonthDateValidate( p_obj, p_event ) {
   var v_format = "NN/NNNN";
   var v_key = getEventKey( p_event );
   var v_obj = p_obj.value;
   var v_len = v_obj.length;

   // If a backspace was entered, and it deleted a '/' then remove one more charater.
   if (v_key == 8 && v_format.substr(v_len,1) != "N") v_obj = v_obj.substr(0,v_len - 1);

   // Format the entry
   v_obj = NumericFormater(v_obj,v_format);
   
   if (v_obj.length == v_format.length) {
      // We have enough charaters, so see if it's a good date.
      if (IsMonthDate(v_obj)) {
         p_obj.value = v_obj;

      } else {
         p_obj.value = "";
      }
   } else {
      // Don't test it yet, just pass it on.
      p_obj.value = v_obj;
   }
}

function isSmallDate(p_date,yrLow,yrHigh,currMonth,currYear ) { 

   var v_dash1 = p_date.indexOf('/',1);
   if (v_dash1 == 0) return 'false';
   var v_month = p_date.substr(0,v_dash1);
   var v_year = p_date.substr(v_dash1 + 1);
   var v_leap_year = false;

   if (currYear != '' && currMonth != '') {
     currMonth = parseInt(currMonth);
     currYear = parseInt(currYear);	  
     if (v_month > currMonth && v_year >= currYear ) {
       alert("Invalid Date.\nPlease re-enter.");
       return false;
     }
   }

   yrLow = parseInt(yrLow);
   yrHigh = parseInt(yrHigh);
      
   if (v_year >= yrLow && v_year <= yrHigh) {
      // We have a valid year
      if (v_year % 4 == 0) v_leap_year = true;
   } else {
      alert("Invalid Year.\nPlease re-enter.");
      return false;
   }

   if (v_month >= 1 && v_month <= 12) {
      // We have a valid month
   } else {
      alert("Invalid Month.\nPlease re-enter.");
      return false;
   }
  

   return true;
}

function smallDateMandatory( p_obj, p_event,yrLow,yrHigh,currMonth,currYear ) {
   var v_format = "NN/NNNN";
   var v_obj = p_obj.value;

   if (v_obj.length > 0 && v_obj.length < v_format.length) {
     if (!isSmallDate(v_obj,yrLow,yrHigh,currMonth,currYear)) {
       p_obj.value = "";
       p_obj.focus();
     }
   }
}

function smallDateValidate( p_obj, p_event,yrLow,yrHigh,currMonth,currYear ) {
   var v_format = "NN/NNNN";
   var v_key = getEventKey( p_event );
   var v_obj = p_obj.value;
   var v_len = v_obj.length;

   // If a backspace was entered, and it deleted a '/' then remove one more charater.
   if (v_key == 8 && v_format.substr(v_len,1) != "N") v_obj = v_obj.substr(0,v_len - 1);

   // Format the entry
   v_obj = NumericFormater(v_obj,v_format);
   
   if (v_obj.length == v_format.length || v_key == 10) {
      // We have enough charaters, so see if it's a good date.
      if (isSmallDate(v_obj,yrLow,yrHigh,currMonth,currYear)) {
         p_obj.value = v_obj;
      } else {
         p_obj.value = "";
      }
   } else {
      // Don't test it yet, just pass it on.
      p_obj.value = v_obj;
   }
}

function ValidateString( p_obj, p_event, p_format ) {
   var v_key = getEventKey( p_event )
   var v_obj = p_obj.value;
   var v_len = v_obj.length;
   change_on_page = true;
  // alert(v_key)

   // Make sure that we didn't just tab into this field!
   if (v_key != 9 && v_key != 16 && v_len > 0) {
     // Format the entry
     if (p_format.substr(0,1) == "$" || p_format.substr(0,1) == "%")
       p_obj.value = DollarFormater(p_obj.value,p_format);
     else {
       // If a backspace was entered, and it deleted a non-numeric character then remove one more charater.
       if (v_key == 8 && p_format.substr(v_len,1) != "N") p_obj.value = p_obj.value.substr(0,v_len - 1);
	 
       p_obj.value = NumericFormater(p_obj.value,p_format);
     }
   }
}

function ValidateField( p_obj, p_value, p_type )	{
	var v_type = p_type;
	change_on_page = true;
	
	if ( p_value.length < 1 ) { return false; }
	
	switch(p_type) {
		//ssn
		case "creditcardnumber" : {
			var isValid = false;
			var cardNumbersOnly = p_value;
    		var cardNumberLength = cardNumbersOnly.length;
			if (cardNumberLength >= 15 && cardNumberLength <=16) {
				var numberProduct;
				var numberProductDigitIndex;
				var checkSumTotal = 0;
				
				for (digitCounter = cardNumberLength - 1; 
				  digitCounter >= 0; 
				  digitCounter--)
				{
				  checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));

				  digitCounter--;
				  numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
				  for (var productDigitCounter = 0;
				    productDigitCounter < numberProduct.length; 
				    productDigitCounter++)
				  {
				    checkSumTotal += 
				      parseInt(numberProduct.charAt(productDigitCounter));
				  }
				}
				
				isValid = (checkSumTotal % 10 == 0);
			}
			if (!isValid) {
				alert("Invalid Credit Card Number");
				p_obj.focus();
				p_obj.select();
			}
								
		break;
		}
		case "ssn":	{
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length;
			
			if (v_len != 9)	{
				alert("Invalid Social Security Number. Please re-enter\rusing standard US SSN format. (999-99-9999)");
				p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			} 
			else {
				p_obj.value = NumericFormater( v_value, 'NNN-NN-NNNN' );
			}
		break;
		}
		case "tin":	{
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length;
			
			if (v_len != 9)	{
				alert("Invalid Tax Identification Number. Please re-enter\rusing standard TIN format. (NN-NNNNNNN)");
				p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			} 
			else {
				p_obj.value = NumericFormater( v_value, 'NN-NNNNNNN' );
			}
		break;
		}		
		// aba
		case "aba": {
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length;
			if ((v_value.length < 8) || (v_value.length > 9)) {
				alert("Invalid Routing Number. Routing numbers must be eight or nine digits in length.");
				p_obj.focus();
			} else {
				// The routing number must begin with 01 - 12 or 21 - 32 (http://www.bankersonline.com/regs/229/a229a.html)
				var digitRegEx = new RegExp("^(([02][1-9])|([13][0-2]))");
				if (digitRegEx.test(v_value)) {
					tnum = (v_value.charAt(0) * 3)
						+ (v_value.charAt(1) * 7)
						+ (v_value.charAt(2) * 1)
						+ (v_value.charAt(3) * 3)
						+ (v_value.charAt(4) * 7)
						+ (v_value.charAt(5) * 1)
						+ (v_value.charAt(6) * 3)
						+ (v_value.charAt(7) * 7);
					if (v_value.length == 9) {
						// Full routing number
						tnum += (v_value.charAt(8) * 1);
						tnum = tnum % 10;
						if(tnum != 0) {
							alert("Invalid Routing Number. Please check and correct your routing number.");
							p_obj.focus();			
						} else {
							return true;
						}
					} else {
						// Routing number without the "check digit", add it
						tmod = tnum % 10;
						if ( tmod ) {
							checkDigit = 10 - tmod;
						} else {
							checkDigit = 0;
						}
						p_obj.value = v_value.toString() + checkDigit.toString();
						return true;
					}
				} else {
					// Not a valid routing number because it doesn't start with 01 - 12 or 21 - 32
					alert("Invalid Routing Number. Please check and correct your routing number.");
					p_obj.focus();			
				}
			}
			break;		
		}
			
		//date	
		case "date": {
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length;
			if (v_len != 8)	{
				alert("Invalid Date. Please re-enter using standard\r4-digit year format. (mm/dd/yyyy)");
				//p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			} 
			else {
				p_obj.value = NumericFormater( v_value, 'NN/NN/NNNN' );
				if (!IsDate( p_obj.value )) {
					//p_obj.value = "";
					p_obj.focus();
					p_obj.select();
				}
			}
		break;	
		}
		case "datetime": {
			var format = "NN/NN/NNNN NN:NN:NN";
			//var v_value = GetRawNumber( p_obj.value );
			var v_value = p_obj.value;
			var v_len = v_value.length;
			if (v_len != format.length)	{
				alert("Invalid Date-Time. Please re-enter using the following format mm/dd/yyyy hh:mm:ss.");
				//p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			} else {
				p_obj.value = NumericFormater( v_value, format );
				if (!IsDateTime( p_obj.value )) {
					//p_obj.value = "";
					p_obj.focus();
					p_obj.select();
				}
			}
		break;	
		}		
		case "month": {
			var format = "NN/NNNN";
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length;
			if ( v_len != 6 ) {
				alert("Invalid month. Please re-enter in the following format: MM/YYYY.");
				p_obj.focus();
				p_obj.select();
			} else {
				p_obj.value = NumericFormater( v_value, format );
				if ( !IsMonthDate( p_obj.value ) ) {
					p_obj.focus();
					p_obj.select();
				}
			}
			break;
		}
		
		//zipcode
		case "zipcode":	{
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length;
	
			if (v_len != 5 && v_len != 9)	{
				alert("Invalid Zipcode. Please re-enter using standard\rUS zipcode format. (99999 or 99999-9999)");
				p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			} else {
				if (v_len == 5)
					p_obj.value = NumericFormater( v_value, 'NNNNN' );
			
				if (v_len == 9)

					p_obj.value = NumericFormater( v_value, 'NNNNN-NNNN' );
			}
		break;	
		}
		
		//expdate
		case "expdate":	{
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length
			
			if (v_len != 6 )	{
				alert("Invalid Expiration Date. Please re-enter using\rstandard 4-digit year format. (mm/yyyy)");
				p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			} else {
				p_obj.value = NumericFormater( v_value, 'NN/NNNN' );
				if (!IsExpDate( p_obj.value )) {
					p_obj.value = "";
					p_obj.focus();
					p_obj.select();
				}
			}
		break;
		}
		
		//ustelephone
		case "ustelephone":	{
			var v_value = GetRawNumber( p_obj.value );
			var v_len = v_value.length
		
			if (v_len != 10 )	{
				alert("Invalid US Telephone Number. Please re-enter using\rstandard US telephone format. ((999)999-9999)");
				//p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			} else {
				p_obj.value = NumericFormater( v_value, '(NNN)NNN-NNNN' );
			}
		break;
		}
		
		//username_pass
		case "username_pass":	{
			var v_value = p_value
			var v_len = v_value.length
	
			if (v_len < 10 )	{
				alert("Your username and password must be at least\r10 characters in length. Please try again.");
				//p_obj.value = "";
				p_obj.focus();
				p_obj.select();
			}
		break;
		}
		
		//business telephone
		case "business_telephone": {
			var v_value = GetRawNumber( p_obj.value );
			if(v_value.length <= 9)
				{
				alert("Invalid US Telephone Number. Please re-enter using\rstandard US telephone format. ((999)999-9999x999)");
				//p_obj.value = "";
				p_obj.focus();
				p_obj.select();
				
				}
		break;
		}
	}
}


function Validate_Min_Length (objField,nMinLength,strFieldName)
{
   var strFieldValue = objField.value;

	if ((strFieldValue.length != 0) && (strFieldValue.length != nMinLength))
		{
	 		alert(strFieldName+" must be " + nMinLength + " characters")
	 		objField.focus();
	 		objField.select();
		}
}

// Added by KF, 2/10/03
function roundOff(value, precision)
	{
        value = "" + value //convert value to string
        precision = parseInt(precision);
        var whole = "" + Math.round(value * Math.pow(10, precision));

        var decPoint = whole.length - precision;
        if(decPoint != 0)
        {
                result = whole.substring(0, decPoint);
				if(precision != 0)
					{
	                result += ".";
	                result += whole.substring(decPoint, whole.length);
					}
        }
        else
        {
                result = whole;
        }
        return result;
	}

function parseNumeric( p_num ) {
   var v_output = '';
   
   for (var v_idx = p_num.length - 1; v_idx >= 0; v_idx --) {
      var v_char = p_num.substr(v_idx,1);
      if ((v_char >= '0' && v_char <= '9') || v_char == '.') v_output = v_char + v_output;
   }
   
   return v_output;
}

  function formatNumber(number, format) {
	//constants 
	var separator = ","; // use comma as thousands separator
	var decpoint = ".";  // use period as decimal point
	var percent = "%";   // use percent sign for percent
	var currency = "$";  // use dollar sign for currency
	var	optional = "N"   // signifies an optional digit place holder
	
	//remove any formatting from input ("number")
	number = parseNumeric(number);	

    var useSeparator = format.indexOf(separator) != -1;  // use separators in number
    
	var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
    //if (usePercent) number *= 100;
	
	var useCurrency = format.indexOf(currency) != -1;  // use currency format
	
    var isNegative = (number < 0);
    number = Math.abs (number);
    
	// remove key characters from format
	tregex = new RegExp("[" + separator + currency + percent +"]","gi");
	format = format.replace(tregex,'');
 
    number = String(number);  // convert number input to string
 
     // split input value into LHS and RHS using decpoint as divider
    var dec = number.indexOf(decpoint) != -1;
    var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
    var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

     // split format string into LHS and RHS using decpoint as divider
    dec = format.indexOf(decpoint) != -1;
    var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
    var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

     // adjust decimal places by cropping or adding zeros to LHS of number
    if (srightEnd.length < nrightEnd.length) {
      var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
      nrightEnd = nrightEnd.substring(0, srightEnd.length);
      if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

      while (srightEnd.length > nrightEnd.length) {
        nrightEnd = "0" + nrightEnd;
      }

      if (srightEnd.length < nrightEnd.length) {
        nrightEnd = nrightEnd.substring(1);
        nleftEnd = (nleftEnd - 0) + 1;
      }
    } else {
      for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
        if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
        else break;
      }
    }

    // adjust leading zeros
	tregex = new RegExp("[" + optional +"]","gi"); 
  	sleftEnd = sleftEnd.replace(tregex,''); // remove hashes from LHS of format
	
    while (sleftEnd.length > nleftEnd.length) {
      nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
    }

    if (useSeparator) {
		//one day this should be regex

		tval = String(nleftEnd);
		tnleftEnd = "";
	    for (var i=0; i < tval.length; i++) {
	    	if (i != 0 && (tval.length - i) % 3 == 0) {
		  		tnleftEnd += separator;
			}
		  	tnleftEnd += tval.charAt(i);
	    }
		nleftEnd = tnleftEnd;
	}
   
    var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
	
    output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
	
    if (isNegative) {
      // patch suggested by Tom Denn 25/4/2001
      output = (useCurrency) ? "(" + output + ")" : "-" + output;
    }
	
    return output;


  }