// +--------------------------------------------------------------------------------+
// | Verification routines                                                          |
// |	The following code provides a number of verification routines to check      |
// |		- A given date (month, day, year) is valid	(CheckValidDate)            |
// |		- A passed parameter is all numbers (CheckNumber)                       |
// |		- Checks and converts a passed parameter to (xxx) yyy-zzzz (CheckPhone) |
// |		- Checks a passed ZipCode is either xxxxx or xxxxx-yyyy (CheckZipCode)  |
// |		- Checks a passed date parameter (mm/dd/yyyy) is valid (VerifyDate)     |
// |                                                                                |
// | Author : Kevin Alexander (kalexander@pga-inc.com)								|
// +--------------------------------------------------------------------------------+

// Function to check whether a passed date is valid
function CheckValidDate(month,day,year) 
{
	var numofdays;
	
	switch (month) {
		case '1' : numofdays=31; break;
		case '2' :
			if (year % 4 != 0) {numofdays=28;}
			if (year % 100 == 0 & year % 400 == 0) {numofdays=29;}
			if (year % 100 == 0 & year % 400 != 0) {numofdays=28;}
			if (year % 4 == 0 & year % 100 != 0) {numofdays=29;}
			break;
		case '3' : numofdays=31; break;
		case '4' : numofdays=30; break;
		case '5' : numofdays=31; break;
		case '6' : numofdays=30; break;
		case '7' : numofdays=31; break;
		case '8' : numofdays=31; break;
		case '9' : numofdays=30; break;
		case '10' : numofdays=31; break;
		case '11' : numofdays=30; break;
		case '12' : numofdays=31; break;
	}
	
	if (day <= numofdays)	{	return true; }
	else {return false;	}
	
}

// Function to check whether the passed value just contains number (0-9)
function CheckNumber(value)
{	if (isNaN(parseFloat(value)) == true)
	{	return false;	}
	else
	{	return true;	}
}

function CheckPhone(phone)
{
	tempstr = '';
	// Strip out everything other than digits
	for (x = 0; x < phone.length;x++)
	{	if (phone.charCodeAt(x) > 47 && phone.charCodeAt(x) < 58)
		{	tempstr = tempstr + phone.charAt(x);	}
	}
	if (tempstr.length == 10)
	{	return '(' + tempstr.substr(0,3) + ') ' + tempstr.substr(3,3) + '-' + tempstr.substr(6);	}
	else
	{	return 'Invalid phone number - The phone number needs a 3 digit area code and 7 digit number';	}
}

// Function to check whether the passed value just contains number (0-9) or '-')
function CheckZipCode(zipcode)
{	
	var errorcount = 0;
	
	if (zipcode.length != 5 && zipcode.length != 10)
	{	return 'Invalid Zip Code - The zipcode should be 5 (12065) or 10 (12065-8814) characters long.';}
	
	for (var x = 0; x < zipcode.length; x++)
	{
		if (zipcode.charCodeAt(x) == 45 || (zipcode.charCodeAt(x) > 47 && zipcode.charCodeAt(x) < 58))	{}
		else { errorcount++;}
	}
	if (errorcount > 0) {return 'Invalid Zip Code - The zipcode contains invalid characters, only characters 0-9 are valid.';}
	else {return 'OK';}
}

function VerifyEmail(value)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(value)) return true;
	else return false;
}

function VerifyDate(value)
{
	var ValidDate
	var pos, tempstr
	var month, day, year
	
	tempstr = value;
	
	// Get the month from the parameter
	pos = tempstr.search('/');
	if (pos <= 0)
	{	return 'Invalid date entry - the date must be in the format mm/dd/yyyy, i.e. 01/28/2000';	}
	else	
	{	month = tempstr.substr(0,pos);
		tempstr = tempstr.substr(pos+1);
		if (month.length==1)
		{	month = '0' + month;	}
	}
	
	// Get the day and year from the parameter
	pos = tempstr.search('/');
	if (pos <= 0)
	{	return 'Invalid date entry - the date must be in the format mm/dd/yyyy, i.e. 01/28/2000';	}
	else	
	{	day = tempstr.substr(0,pos);
		if (day.length == 1)
		{	day = '0' + day;	}
		year = tempstr.substr(pos+1);
		if (year.length == 2)
		{	year = '20' + year;	} 
	}
	
//	window.alert ('Month=' + month + " Day=" + day + ' Year=' + year);
	
	// Check it's a valid length
//	if (value.length < 10)	
//	{	return 'Invalid date entry - the date must be in the format mm/dd/yyyy, i.e. 01/28/2000';	}
		
	// Check mm/dd/yyyy format
//	if (value.substr(2,1) != '/' || value.substr(5,1) != '/')
//	{	return 'Invalid date entry - ' + value + ' is an invalid format';	}
		
	// Get the specific values
//	month = value.substr(0,2);	day = value.substr(3,2);	year = value.substr(6,4);
		
	if (CheckNumber(month)==false)
	{	return 'Invalid date entry - The month value ' + month + ' contains invalid characters, only 0-9 are valid.';}

	if (CheckNumber(day)==false)
	{	return 'Invalid date entry - The day value ' + day + ' contains invalid characters, only 0-9 are valid.';}

	if (CheckNumber(year)==false)
	{	return 'Invalid date entry - The year value ' + year + ' contains invalid characters, only 0-9 are valid.';}
			
	if (month < 1 || month > 12)
	{	return 'Invalid date entry - ' + month + ' is an invalid month value';	}

	if (year < 1970 || year > 2100)
	{	return 'Invalid date entry - ' + year + ' is an out of range year - range is 1970-2100';	}
	
	if (CheckValidDate(month,day,year)==false)
	{	return 'Invalid date entry - ' + value + ' is an invalid date.';	}
	
	return month + '/' + day + '/' + year;
}

// Function to work through a select list to see what's checked
function getvalue(obj)
{	var list = "";
	for (var i=0; i < obj.length; i++)
	{	if (obj.options[i].selected)			{	list = obj.options[i].value;	}		}
	return list;
}
// Function to work through a select list to see what's checked
function gettext(obj)
{	var list = "";
	for (var i=0; i < obj.length; i++)
	{	if (obj.options[i].selected)			{	list = obj.options[i].text;	}		}
	return list;
}

function _HideObj(lId)
{
  var ob;ob=new Array;
  var appVer=parseInt(navigator.appVersion);
  var isNC=false,isN6=false,isIE=false;
  if (document.all && appVer >= 4) isIE=true; else
    if (document.getElementById && appVer > 4) isN6=true; else
      if (document.layers && appVer >= 4) isNC=true;
  if (isNC)
  {
    w_str = "document." + lId;ob[lId] = eval(w_str);
    if (!ob[lId]) ob[lId] = _FindObj(document, lId);
    if (ob[lId]) ob[lId].visibility = "hide";
  }
  if (isN6)
  {
    ob[lId] = document.getElementById(lId);
    ob[lId].style.visibility = "hidden";
  }
  if (isIE)
  {
    w_str = "document.all.item(\"" + lId + "\").style";ob[lId] = eval(w_str);
    ob[lId].visibility = "hidden";
  }
}
function _ShowObj(lId)
{
  var ob;ob=new Array;
  var appVer=parseInt(navigator.appVersion);
  var isNC=false,isN6=false,isIE=false;
  if (document.all && appVer >= 4) isIE=true; else
    if (document.getElementById && appVer > 4) isN6=true; else
      if (document.layers && appVer >= 4) isNC=true;
  if (isNC)
  {
    w_str = "document." + lId;ob[lId] = eval(w_str);
    if (!ob[lId]) ob[lId] = _FindObj(document, lId);
    if (ob[lId]) ob[lId].visibility = "show";
  }
  if (isN6)
  {
    ob[lId] = document.getElementById(lId);
    ob[lId].style.visibility = "visible";
  }
  if (isIE)
  {
    w_str = "document.all.item(\"" + lId + "\").style";ob[lId] = eval(w_str);
    ob[lId].visibility = "visible";
  }
}
function _FindObj(doc, lId)
{
  for (var i=0; i < doc.layers.length; i++)
  {
    var w_str = "doc.layers[i].document." + lId;
    var obj;obj=new Array;
    obj[lId] = eval(w_str);
    if (!obj[lId]) obj[lId] = _FindObj(doc.layers[i], lId);
    if (obj[lId]) return obj[lId];
  }
  return null;
} 

