////////////////////////////////////
function isAFloat(obj)
////////////////////////////////////
{
  var pString, message = "", decimal = 0;

  pString = obj.value;

  if (!pString.length > 0)
    return message;

  if(pString.indexOf(" ") > -1)
  {
    obj.select();
    obj.focus ();
    message = "Spaces between characters are not required";
  }
  else
  {
    //the first digit was numeric, so check the rest
    for (var i=0; i<pString.length; i++)
    {
      if (pString.charAt(i) != "0"
        && pString.charAt(i) != "."
        && !parseFloat(pString.charAt(i)))
      {
	obj.select();
    obj.focus ();
          message = "Please enter a numeric value.";
          break;
      }
      if (pString.charAt(i) == ".")
      {
        decimal++;
        if (decimal > 1)
        {
		obj.select();
        obj.focus ();
		message = "Please enter a numeric value.";
		break;
        }
      }
    }
  }

  return message;
}

////////////////////////////////////
function isANumber(obj)
////////////////////////////////////
{
  var pString, message = "";

  pString = obj.value;

  if(!pString.length > 0)
    return message;

  if(pString.indexOf(" ") > -1)
  {
    obj.select();
    obj.focus ();
    message = "Spaces between characters are not required";
  }
  else if ((pString.charAt(i) != "0")
    && (!parseFloat(pString)))
  {
    //the first digit wasn't numeric
    obj.select();
    obj.focus ();
    message = "Please enter a numeric value.";
  }
  else
  {
    //the first digit was numeric, so check the rest
    for (var i=0; i<pString.length; i++)
    {
      if ((pString.charAt(i) != "0")
        && (!parseFloat(pString.charAt(i))))
      {
	obj.select();
    obj.focus ();
        message = "Please enter a numeric value.";
        break;
      }
    }
  }

  return message;
}

////////////////////////////////////
// isPostCode function
// Checks Postcode is correct format
////////////////////////////////////
function isPostcode(obj)
{
	var postcodeexp = /^[a-z]{1,2}[0-9]{1,2}\s*\D{0,2}[0-9]{1,1}[a-z]{1,2}$/i;
	var objText=obj.value;
	var pString, message = "";

	if( objText.match(postcodeexp) || objText.length == 0)
	{
		objText=obj.value = objText=obj.value.toUpperCase();
	}
	else
	{
		obj.select();
        obj.focus ();
		message = "Invalid postcode\n";
	}
	return message;
}

///////////////////////////////////////////////////////
// isTelephoneNumber function
// Checks that telephone number is a string of numbers
///////////////////////////////////////////////////////
function isTelephoneNumber(obj)
{
	var pString, message = "";

	pString = obj.value;

	if (pString.length <= 0)
		return message;

	if ((pString.charAt(i) != "0") 
		&& (!parseFloat(pString)) 
		&& (pString.charAt(i) != " ") 
		&& (pString.charAt(i) != "(") 
		&& (pString.charAt(i) != ")") 
		&& (pString.charAt(i) != "-"))
	{
		//the first digit wasn't numeric
		//obj.select();
        //obj.focus ();
		message = "Please enter a valid telephone number";
	}
	else
	{
		//the first digit was numeric, so check the rest
		for (var i=0; i<pString.length; i++)
		{
			if ((pString.charAt(i) != "0") 
				&& (!parseFloat(pString.charAt(i))) 
				&& (pString.charAt(i) != " ") 
				&& (pString.charAt(i) != "(") 
				&& (pString.charAt(i) != ")") 
				&& (pString.charAt(i) != "-"))
			{
				obj.select();
                obj.focus ();
				message = "Please enter a valid telephone number";
			}
		}
	}

	return message;
}

////////////////////////////////////////////////////
// isEmail function
// checks that E-mail address is in correct format
///////////////////////////////////////////////////
function isEmail(obj)
{
	var message = "";
	var emailexp = /^[a-z][a-z_0-9\.]+@[a-z_0-9\.]+\.[a-z]+$/i;
	var objText=obj.value;
	var reg01 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;   // not valid
	var reg02 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; // is valid

	if( (!reg01.test(objText) && reg02.test(objText)) || objText.length == 0)
	{
		message = "";
	}
	else
	{
		obj.select();
        obj.focus ();
		message = "Invalid email address\n";
	}

	return message;
}

////////////////////////////////////////////////////
function isAChar(obj)
////////////////////////////////////////////////////
{
	var pString, message = "";

	pString = obj.value;

	if(!pString.length > 0)
		return "";

	if (parseFloat(pString))
	{
		//the first digit was numeric
		obj.select();
        obj.focus ();
		message = "Please enter a character only value for this field.";
	}
	else
	{
		//the first digit was char, so check the rest
		for (var i=0; i<pString.length; i++)
		{
			if ((pString.charAt(i) != "0")
				&& (parseFloat(pString.charAt(i))))
			{
				obj.select();
                obj.focus ();
				message = "Please enter a character only value for this field.";
			}
		}
	}

	return message;
}

////////////////////////////////////////////////////
function isPassword(obj)
////////////////////////////////////////////////////
{
	return "";
}

////////////////////////////////////////////////////
function isVerified(obj)
////////////////////////////////////////////////////
{
	return "";
}

////////////////////////////////////////////////////
function checkPwd(obj1, obj2)
////////////////////////////////////////////////////
{

	if (obj1.value != obj2.value) 
	{
		alert("Password entries do not match");
		obj2.select();
		obj2.focus ();
		return false;
	}
	else
	{
		return true;
	}
}

////////////////////////////////////////////////////
function validateField(obj, type, popupalert)
////////////////////////////////////////////////////
{
	var message = "";

	switch (type)
	{
		case "chr":
			message = message + isAChar(obj);
			break;
		case "eml":       
			message = message + isEmail(obj);
			break;
		case "flt":
			message = message + isAFloat(obj);
			break;
		case "num":
			message = message + isANumber(obj);
			break;
		case "pcd":
			message = message + isPostcode(obj);
			break;
		case "phn":
			message = message + isTelephoneNumber(obj);
			break;
		case "pwe":
			message = message + isPassword(obj);
			break;
		case "pwv":
			message = message + isVerified(obj);
			break;
		default :
			;
	}

	if (message != "")
	{
		popupalert ? alert (message): false;
		return false;
	}
	else
	{
		return true;
	}
}

