var nbsp = 160;		// non-breaking space char
var node_text = 3;	// DOM text node-type
var emptyString = /^\s*$/ ;
var global_valfield;	// retain valfield for timer thread


function validateFirstName(valfield,   // element to be validated
                           infofield ) // id of element to receive info/error msg
{
  //alert("fname!");
  var stat = commonCheck (valfield, infofield, true);
  if (stat != proceed) return stat;

  msg (infofield, "warn", "");
  return true;
}

function validateGender(valfield,   // element to be validated
                        infofield ) // id of element to receive info/error msg
{
  var rad_val = "";

  for (var i=0; i < valfield.length; i++)
   {
   if (valfield[i].checked)
      {
       rad_val = valfield[i].value;
      }
   }

 // alert(rad_val);

  if(rad_val == "") {msg (infofield, "error", "ERROR: required");}
  else { msg (infofield, "warn", ""); }

  return true;
}


function validateStatus(valfield,   // element to be validated
                        infofield ) // id of element to receive info/error msg
{
   if(valfield.value == "" || valfield.value == null) {msg (infofield, "error", "ERROR: required");}
   else { msg (infofield, "warn", ""); }

  return true;
}

function validateBirthdate(valfield1,   // element to be validated
                           valfield2,
                           valfield3,
                           infofield ) // id of element to receive info/error msg
{
  //alert("fname!");
  //var stat = commonCheck (valfield, infofield, true);
  //if (stat != proceed) return stat;

  if((valfield1.value == "") || (valfield2.value == "") || (valfield3.value == ""))
  {msg (infofield, "error", "ERROR: required");}
  else { msg (infofield, "warn", ""); }

  return true;
}

function validateCountry(valfield,   // element to be validated
                         infofield ) // id of element to receive info/error msg
{
  //alert("fname!");
  //var stat = commonCheck (valfield, infofield, true);
  //if (stat != proceed) return stat;

  if(valfield.value == "") {msg (infofield, "error", "ERROR: required");}
  else { 
         if(valfield.value == "Philippines") {show("mobile_infos");}
         else{ hide("mobile_infos"); }

         msg (infofield, "warn", "");
       }

  return true;
}

function hide(valfield){
document.getElementById(valfield).style.visibility='collapse';
}

function show(valfield){
document.getElementById(valfield).style.visibility='visible';
}



var proceed = 2;

function commonCheck    (valfield,   // element to be validated
                         infofield,  // id of element to receive info/error msg
                         required)   // true if required
{
  //alert("checking");
  if (!document.getElementById)
   return true;  // not available on this browser - leave validation to the server
  var elem = document.getElementById(infofield);
  if (!elem.firstChild) return true;  // not available on this browser
  if (elem.firstChild.nodeType != node_text) return true;  // infofield is wrong type of node

  if (emptyString.test(valfield.value)) {
    if (required) {
     // alert("required");
      msg (infofield, "error", "ERROR: required");
      setfocus(valfield);
      return false;
    }
    else {
    //  alert("not required");
      msg (infofield, "warn", "");   // OK
      return true;
    }
  }
  return proceed;
}

function msg(fld,     // id of element to display message in
             msgtype, // class to give element ("warn" or "error")
             message) // string to display
{
  // alert("msg");
  // setting an empty string can give problems if later set to a
  // non-empty string, so ensure a space present. (For Mozilla and Opera one could
  // simply use a space, but IE demands something more, like a non-breaking space.)
  var dispmessage;
  if (emptyString.test(message))
    dispmessage = String.fromCharCode(nbsp);
  else
    dispmessage = message;

  var elem = document.getElementById(fld);
  elem.firstChild.nodeValue = dispmessage;
}

function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

// --------------------------------------------
//                  setfocus
// Delayed focus setting to get around IE bug
// --------------------------------------------

function setFocusDelayed()
{
  global_valfield.focus();
}

function setfocus(valfield)
{
  // save valfield in global variable so value retained when routine exits
  global_valfield = valfield;
  setTimeout( 'setFocusDelayed()', 100 );
}

function isAlphabet(elem, infofield){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		msg (infofield, "error", "ERROR: required");
      setfocus(elem);
      return false;
	}
}

function isAlphaNumeric(alphane, infofield){
//alert("here!!!!!!!!!!!!!!!!");
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
		   // alert("uhm???");
			 msg (infofield, "error", "INVALID: use letters and numbers only!");
          setfocus(alphane);
          return false;
		  }
		}
 return true;
}


function emailValidator(elem, infofield){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		msg (infofield, "error", "Invalid email add!");
		elem.focus();
		return false;
	}
}

function comparePwd(pwd, c_pwd, infofield){
    if(pwd.value != c_pwd.value)
      {
        alert("Passwords do not match");
        return false;
      }
      return true;
}

