
var nbsp = 160;    // non-breaking space char
var node_text = 3; // DOM text node-type
var emptyString = /^\s*$/
var glb_vfld;      // retain vfld for timer thread

function setFocusDelayed(vfld)
{
  glb_vfld.focus()
}

function setfocus(vfld)
{
  glb_vfld = vfld;
  setTimeout( 'setFocusDelayed()', 100 );
}


function display(elmid,msgtype,msg){
 
  var message;
  if (emptyString.test(msg)) 
    message = String.fromCharCode(nbsp);    
  else  
    message = msg;

  var elem = document.getElementById(elmid);
  elem.firstChild.nodeValue = message;  
  
};


var proceed = 2;  

function checkField (field, elmid,reqd)   {
  if (!document.getElementById) 
    return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(elmid);
  if (!elem.firstChild)
    return true;  // not available on this browser 
  if (elem.firstChild.nodeType != node_text)
    return true;  // ifld is wrong type of node  

  if (emptyString.test(field.value)) {
    if (reqd) {
      display (elmid, "error", " * Required");  
      setfocus(field);
      return false;
    }
    else {
      display (elmid, "warn", "");   // OK
      return true;  
    }
  }
  return proceed;
}


function validatePresent(field,elmid)  {
  var stat = checkField (field, elmid, true);
  if (stat != proceed) return stat;

  display (elmid, "warn", "");  
  return true;
};

function validateRadios(r_group,elmid){

var choice = false;

for (i = 0;  i < r_group.length; i++){
	if (r_group[i].checked)	
		choice = true;	
}

if (!choice){	
	display (elmid, "error", " * Required");    	
	setfocus(r_group[0]);
	return (false); 
}else{
  	display (elmid, "warn", "");
  	return (true);
}	

};

//if radio button value is'yes', then this text 'field' become mandatory
function validate(field,elmid,radios){

var pos = -1;

for (i = 0;  i < radios.length; i++){
	if (radios[i].checked)	{
	pos = i;
	}	
}

if (pos == 0){	  
	return validatePresent(field,elmid);
}else{	
	display (elmid, "warn", "");
	return true;
	}
};

//tests if 'Yes' field is checked for a specifyed group of radio buttons (assuming first rad button is "Yes")
function isYesButtonChecked(radios){

var choice = false;

	if(radios[0].checked){
		choice = true;
	}
	
	return choice;
};

//simple test if a radio button is checked
function isButtonChecked(buttons){

var choice = false;

for (i = 0;  i < buttons.length; i++){
	if (buttons[i].checked)	{
		choice = true;	
		break;
	}
}
	return choice;
};

//if previous radio button value is'yes', then these radio buttons become mandatory fields
function isChecked(field,elmid,radios){

var choice = false;

	if(radios[0].checked){   // assuming the first rad button is "yes"
	
	if (!isButtonChecked(field)){
		display (elmid, "error", " * Required");    	
		setfocus(field[0]);
	}else{
		display (elmid, "warn", "");
  		choice = true;
  	}
 
  }else{ choice = true; }
  return choice;
};

function displayRequired(field,elmid){
	display (elmid, "error", " * Required");    	
	setfocus(field);
};

function removeRequired(elmid){
	display (elmid, "warn", "");
};