
<!--common scripts... --> 

String.prototype.trim = function(){    
 //trims white space off both ends of this string...
 return( (ar=/^\s*([\s\S]*\S+)\s*$/.exec(this)) ? ar[1] : "" );
}


function dokeydown(event){
 //trap carriage return if no value for input field...
 var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
 var ls_value;
  
 if ((event.srcElement.tagName == 'INPUT') && (keyCode == 13)){
  ls_value = event.srcElement.value.trim();  
  if (ls_value.length == 0) return false;   
 }
 return true;	
}


function doconfirm(as_text) {
 //confirm user intentions...
 return window.confirm(as_text); 
}


function isInteger(s){
 //return TRUE if all characters are numbers...
 var i;
 
 for (i = 0; i < s.length; i++){ 
  var c = s.charAt(i);
  if (((c < "0") || (c > "9"))) return false;
 }
 return true;
}


function isDecimal(s){
 var i, cnt;

 //only if all characters are numbers or "."...
 for (i = 0; i < s.length; i++){ 
  var c = s.charAt(i);
  if (( (c < "0") || (c > "9") )) {
   if (c != ".") return false;
  }
 }
 
 //only if 1st character is a number...
 if (isInteger(s.charAt(0)) == false) return false;
 
 //only if last character is a number...
 if (isInteger(s.charAt(s.length -1)) == false) return false;
 
 //only if at most there is only one "."...
 cnt = 0;
 for (i = 0; i < s.length; i++){ 
  if (s.charAt(i) == ".") cnt++;  
 }
 if (cnt > 1) return false;
 
 return true;
}


function filterstring(as_text, as_filter){
	//Search through the characters of as_text one by one. If character is not in 
	//as_filter, append to ls_text...
 var i;
 var ls_text = "";
 
 for (i = 0; i < as_text.length; i++){ 
  var c = as_text.charAt(i);
  if (as_filter.indexOf(c) == -1) ls_text += c;
 }
 return ls_text;
} 


function daysInFebruary (year){
 //returns the number of days in February for the given year...
 return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}


function DaysArray(n) {
 for (var i = 1; i <= n; i++) {
 	this[i] = 31
 	if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
 	if (i==2) {this[i] = 29}
 } 
 return this
}


function isDate(s_date){
 //Returns true if s_date is a valid date. Returns false otherwise...
	var daysInMonth = DaysArray(12)
	var dtCh = "/"
	var pos1 = s_date.indexOf(dtCh)
	var pos2 = s_date.indexOf(dtCh, pos1+1)
	var strDay = s_date.substring(0,pos1)
	var strMonth = s_date.substring(pos1+1, pos2)
	var strYear = s_date.substring(pos2+1)
	
	strYr = strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	
	//format must be dd/mm/yyyy...
	if (pos1==-1 || pos2==-1) return false;
	
	//validate month...
	if (strMonth.length<1 || month<1 || month>12) return false;
	
	//validate day...
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]) return false;
	
	//year must be 4 digit...	
	if (strYear.length != 4 || year==0)	return false;
		
	if (s_date.indexOf(dtCh,pos2+1)!=-1 || isInteger(filterstring(s_date, dtCh))==false)	return false;
		
 return true
}


function f_warn_length(as_fieldname, as_element, al_length) {
 //Issues a warning if the length of the trimmed value in as_element is greater than al_length. 
 //Returns true if the user chooses to continue anyway, returns false otherwise.
 
 var ls_element, ls_value;
 
 as_fieldname = as_fieldname.trim();
 as_fieldname = as_fieldname.toUpperCase();
 as_element = as_element.trim();
 
 ls_element = document.getElementById(as_element);
 ls_value = ls_element.value.trim();
 
 if (ls_value.length > al_length) { 
  if (!window.confirm("The field " + as_fieldname + " will be reduced to " + al_length + " characters. Continue?")) {
   ls_element.focus();
   return false; 
  }
 } 
 
 return true;
}


function f_validate_string(as_fieldname, as_element) {
 //Returns true if the trimmed value in as_element is not empty. Returns false otherwise and 
 //in addition it displays an error message and sets focus on the element. 
 
 var ls_element, ls_value;
 
 as_fieldname = as_fieldname.trim();
 as_fieldname = as_fieldname.toUpperCase();
 as_element = as_element.trim();
  
 ls_element = document.getElementById(as_element);
 ls_value = ls_element.value.trim();
 
 if (ls_value.length == 0) { 
  alert("A value is required for the field " + as_fieldname + "."); 
  ls_element.focus();
  return false; 
 } 
 
 return true;
}


function f_validate_date(as_fieldname, as_element) {
 //Returns true if the value in as_element is a valid date. Returns false otherwise and 
 //in addition it displays an error message and sets focus on the element. 
 
 var ls_element, ls_date;
 var ls_today = new Date();
 
 as_fieldname = as_fieldname.trim();
 as_fieldname = as_fieldname.toUpperCase();
 as_element = as_element.trim();
 
 ls_element = document.getElementById(as_element);
 ls_date = ls_element.value.trim();
 
 if (!f_validate_string(as_fieldname, as_element)) return false;
 
 if (ls_date.length != 10 || ls_date.charAt(2) != "/" || ls_date.charAt(5) != "/") { 
  alert("The specified " + as_fieldname + " is invalid. The correct format is DD/MM/YYYY (eg. 31/12/" + ls_today.getFullYear() + ")."); 
  ls_element.focus();
  return false; 
 } 
 
 if (!isDate(ls_date)) {
  alert("The specified " + as_fieldname + " is invalid."); 
  ls_element.focus();
  return false; 
 }
 
 return true;
}


