////////////////////////////////////
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 = "Please enter a valid postcode\n";
	}
	return message;
}

///////////////////////////////////////////////////////
// isTelephoneNumber function
// Checks that telephone number is valid, ie. one of the following formats:
//
//  01234 987654
//  (01234) 123456
//  +44 1234 876543
//  020 12345678
//  (020) 12345678
//  +44 20 12345678
//
///////////////////////////////////////////////////////
function isTelephoneNumber(obj)
{
	var pString = obj.value;
	var message = "";
	var numCount = 0;

	if (pString.length <= 0)
		return message;

	if ((pString.charAt(0) != "0")
		&& (pString.charAt(0) != "+") 
		&& (pString.charAt(0) != " ") 
		&& (pString.charAt(0) != "(")
		//&& (pString.charAt(0) != ")") 
		//&& (pString.charAt(0) != "-"))
		//&& (!parseFloat(pString.charAt(0))
		) 
	{
		//the first digit wasn't valid
		message = "Please enter a valid telephone number";
	}
	else
	{
		if ((pString.charAt(0) == "0"))
			numCount++;
		
		//the first digit was valid, so check the rest
		for (var i=1; i<pString.length; i++)
		{
			if ((pString.charAt(i) != " ") 
				&& (pString.charAt(i) != "(") 
				&& (pString.charAt(i) != ")") 
				&& (pString.charAt(i) != "-"))
			{
				if (!parseInt(pString.charAt(i), 10) && pString.charAt(i) != "0")
				{
					message = "Please enter a valid telephone number";
					break;
				}
				else
				{
					numCount ++;
				}
			}
		}
		if (numCount < 11)
			message = "Please enter a valid telephone number complete with area code ";
	}

	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,6}|[0-9]{1,3})(\]?)$/; // is valid

	if( (!reg01.test(objText) && reg02.test(objText)) || objText.length == 0)
	{
		message = "";
	}
	else
	{
		obj.select();
        //obj.focus ();
		message = "Please enter a valid 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;
        obj.select();
        obj.focus();
		return false;
	}
	else
	{
		return true;
	}
}

