var curr='';
var isNS = (navigator.appName == "Netscape");

window.onerror=error_handler

function error_handler(msg, url, lno) {
	return true; }
	
function showit(layername){
	if (isNS) { if (curr) { document[curr].visibility = 'hide' }
				document[layername].visibility = 'show' }
	else { if (curr) { document.all[curr].style.visibility = 'hidden' }
		   document.all[layername].style.visibility = 'visible' }
	curr = layername
	
}

function checkdate(object_value)
{   //Returns true if value is a eurodate format or is NULL
    //otherwise returns false	
    if (object_value.length == 0) return true;
    //Returns true if value is a date in the dd/mm/yyyy format
	isplit = object_value.indexOf('/');
	if (isplit == -1) {	isplit = object_value.indexOf('.');	}
	if (isplit == -1 || isplit == object_value.length) return false;
    sDay = object_value.substring(0, isplit);
	monthSplit = isplit + 1;
	isplit = object_value.indexOf('/', monthSplit);
	if (isplit == -1) {	isplit = object_value.indexOf('.', monthSplit);	}
	if (isplit == -1 ||  (isplit + 1 )  == object_value.length)	return false;
    sMonth = object_value.substring((sDay.length + 1), isplit);
	sYear = object_value.substring(isplit + 1);
	if (!checkinteger(sMonth)) return false;
	else if (!checkrange(sMonth, 1, 12)) return false;
	     else if (!checkinteger(sYear)) return false;
			  else if (!checkrange(sYear, 0, null)) return false;
	               else if (!checkinteger(sDay)) return false;
					    else if (!checkday(sYear, sMonth, sDay)) return false;
							 else return true;
}

function checkday(checkYear, checkMonth, checkDay)
{	maxDay = 31;
	if (checkMonth == 4 || checkMonth == 6 || checkMonth == 9 || checkMonth == 11) maxDay = 30;
	else if (checkMonth == 2)
	     { if (checkYear % 4 > 0) maxDay =28;
		   else if (checkYear % 100 == 0 && checkYear % 400 > 0) maxDay = 28;
		        else maxDay = 29; }
	return checkrange(checkDay, 1, maxDay);
}

function checkinteger(object_value)
{   //Returns true if value is a number or is NULL
    //otherwise returns false	
    if (object_value.length == 0) return true;
    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;
    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)	return checknumber(object_value);
    else return false;
}

function numberrange(object_value, min_value, max_value)
{   // check minimum
    if (min_value != null) { if (object_value < min_value) return false; }
    // check maximum
    if (max_value != null) { if (object_value > max_value) return false; }
	//All tests passed, so...
    return true;
}

function checknumber(object_value)
{   //Returns true if value is a number or is NULL
    //otherwise returns false	
    if (object_value.length == 0) return true;
    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1) decimal = true;
	else if (check_char < 1) return false;
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{ check_char = number_format.indexOf(object_value.charAt(i))
	  if (check_char < 0) return false;
	  else if (check_char == 1)
		   { if (decimal) return false;
			else decimal = true; }
		   else if (check_char == 0)
				{ if (decimal || digits) trailing_blank = true; }
	            else if (trailing_blank) return false;
		             else digits = true; }	
    //All tests passed, so...
    return true
}

function checkrange(object_value, min_value, max_value)
{ //if value is in range then return true else return false
  if (object_value.length == 0) return true;
  if (!checknumber(object_value)) { return false; }
  else { return (numberrange((eval(object_value)), min_value, max_value)); }
  //All tests passed, so...
  return true;
}

function checkemail(object_value)
{ if (object_value.length == 0) return true;
  invalidChars = ' /:,;' 
  for(j=0; j<invalidChars.length; j++)
  { badChar = invalidChars.charAt(j) 
    if(object_value.indexOf(badChar,0) != -1) { return false } } 
  atPos = object_value.indexOf('@',1) 
  if(atPos == -1) { return false } 
  if(object_value.indexOf('@',atPos+1) != -1) { return false } 
  periodPos = object_value.indexOf('.',atPos) 
  if(periodPos == -1) { return false } 
  if(periodPos+3 > object_value.length) { return false } 
  return true
} 

function required(obj,obj_type,obj_name)
{	
	if (obj_type == "TEXT" || obj_type == "PASSWORD")
	{ if (obj.value.length == 0) missing=missing+obj_name+'\n'; }
    if (obj_type == "SELECT")
	{ for (i=0; i < obj.length; i++)
	  { if (obj.options[i].selected) return true; }
	  missing=missing+obj_name+'\n'; return false; }
    if (obj_type == "RADIO" || obj_type == "CHECKBOX")
	{ if (obj.checked) return true;
	  else missing=missing+obj_name+'\n';}
	if (obj_type == "RADIOS" || obj_type == "CHECKBOXS")
	{ for (i=0; i < obj.length; i++)
	  { if (obj[i].checked) return true; }
	missing=missing+obj_name+'\n'; }
	return false;
}

function addit(x,y){
	f=x.options.selectedIndex;
	if(f != -1)
	{ addsel(x,y,f);
	  sortit(y);
	  sortit(x); }
}

function addval(x,y,z){
	n=x.length;
	found=-1;
	for (var i=0; i<n; i++) if(x.options[i].value == z) { found=i; break;};
	if(found != -1) addsel(x,y,found); 
}

function addsel(x,y,f){
	n=y.length;
	found=0;
	v=x.options[f].value;
    for (var i=0; i<n; i++) if(y.options[i].value == v) { found=1; break;};
	if(found == 0)
	{ y.length=n+1; 
	  y.options[n].text=x.options[f].text;
	  y.options[n].value=v; }	
	delsel(y,x,f);
	x.options.selectedIndex=-1;
}

function sortit(y){
	n=y.length-1;
	for (var i=0; i<n; i++) for (var j=i+1; j<n+1; j++) if(y.options[j].text < y.options[i].text) swapit(y,i,j);
}

function delall(x,y){
	while (x.length != 0) { addsel(x,y,0); }
	sortit(y);
}

function delsel(x,y,f){
	n=y.length-1;
    if(f !=  n) for (var i=f; i<n; i++) swapit(y,i,i+1);
	y.length=n;
}

function swapit(y,i,j){
	str=y.options[j].text;
	val=y.options[j].value;
	y.options[j].text=y.options[i].text;
	y.options[j].value=y.options[i].value;
	y.options[i].text=str;
	y.options[i].value=val;
}

function resetit(x,y,z){
	delall(y,x);
	for (p=2; p<arguments.length; p++) addval(x,y,arguments[p]);
   	sortit(y);
}

function makelist(fld,x){
	fld.value='';
	for (i=0; i<x.length; i++) fld.value=fld.value+x.options[i].value+',';
}

function entries(obj)
{	n=0;
    for (i=0; i < obj.length; i++)
	  { if (obj[i].checked) n=n+1; }
	return n;
}
