
//////
//////
//////
//////
//////
//////
//////
//////
//////
//////
//////
//////

function IsCreditCardExpirationDate(obj,str)
{
	//must be mm/yy so 5 digits total
	var t="";
	var mm=0;
	var yy=0;

	t=Trim(str);
	if (t=="")
		return false;
	if (t.length != 5)
		return false;
	if (t.charAt(2) != "/")
		return false;
	if (IsDigit_numeric(t.charAt(0)) != true)
		return false;
	if (IsDigit_numeric(t.charAt(1)) != true)
		return false;
	if (IsDigit_numeric(t.charAt(3)) != true)
		return false;
	if (IsDigit_numeric(t.charAt(4)) != true)
		return false;
	mm = atoi(t.substring(0,2));
	if (mm < 1 || mm > 12)
		return false;
	yy = atoi(t.substring(3,5));
	if (yy > 10) //2010
		return false;
	return true;
}

function IsCreditCardNumber(obj,str,type)
{
	var CCnum="";
	var CCtype="";
	var len=0;
	CCtype = Trim(type);
	CCnum = Trim(str);

	//remove any "-" characters, any types will cause a rejection
	var regexp = /-/g;
	CCnum=CCnum.replace(regexp,"");
	len=CCnum.length

	if (CCtype == "American Express")
	{
		//3700-000000-00000
		if (len != 15)
			return false;
		if (CCnum.charAt(0) != "3" && CCnum.charAt(1) != "7")
			return false;
	}
	else if (CCtype == "Discover Card")
	{
		//6000-0000-0000-0000
		if (len != 16)
			return false;
		if (CCnum.charAt(0) != "6")
			return false;
	}
	else if (CCtype == "Mastercard")
	{
		//5000-0000-0000-0000
		if (len != 16)
			return false;
		if (CCnum.charAt(0) != "5")
			return false;
	}
	else if (CCtype == "Visa")
	{
		//4000-0000-0000-0000
		if (len != 16)
			return false;
		if (CCnum.charAt(0) != "4")
			return false;
	}
	else
		return false;

	//now test to see that all digits are integral 0 to 9
	for (i=0; i < len; i++)
	{
		if (IsDigit_numeric(CCnum.charAt(i)) != true)
			return false;
	}
	//validate the number combinations
	//#123-4567-8901-2345
	//#987-6543-2109-8765
	//#000-0000-0000-0000
	//#111-1111-1111-1111
	//#222-2222-2222-2222
	//#333-3333-3333-3333
	//#444-4444-4444-4444
	//#555-5555-5555-5555
	//#666-6666-6666-6666
	//#777-7777-7777-7777
	//#888-8888-8888-8888
	//#999-9999-9999-9999
	//EACH OF THE SECTIONS WITHIN A SPLIT MUST NOT BE ALL THE SAME DIGIT ?
	return true;
}

//Phone must be ###-###-####, or 12 digits
function IsValidPhone(obj,val)
{
	var s="";
	var c="";
	var i=0;
	s=Trim(val);
	if (s.length != 12)
	{
		alert("Invalid Phone Number: 12 digits required; for example: 800-555-1212");
		obj.focus();
		obj.select();
		return false;
	}
	if (s.charAt(3) != "-" || s.charAt(7) != "-")
	{
		alert("Invalid Phone Number: enter proper separators '-', for example: 800-555-1212");
		obj.focus();
		obj.select();
		return false;
	}
	for (i=0; i < s.length; i++)
	{
		if (i==3 || i==7)
			continue;
		c = s.charAt(i);
		if (IsDigit_numeric(c) != true)
		{
			alert("Invalid Phone Number: found non-numeric digits, use 0-9");
			obj.focus();
			obj.select();
			return false;
		}
	}
	//now check validity
	//the following numbers are not legitimate
	//***-555-****
	//1**-***-****
	//***-976-****
	//0**-***-****
	//100-***-****
	//200-***-****
	//300-***-****
	//400-***-****
	//500-***-****
	//600-***-****
	//700-***-****
	//900-***-****
	//***-000-****
	//***-***-0000
	//***-1**-****
	//***-123-4567
	//987-654-3210 ?
	return true
}

//SS must be ###-##-####, or 11 digits
function IsValidSocialSecurityNumber(obj,val)
{
	var s="";
	var c="";
	var i=0;
	s=Trim(val);
	if (s.length != 11)
	{
		alert("Invalid Social Security Number: 11 digits required; for example: 123-45-6789");
		obj.focus();
		obj.select();
		return false;
	}
	if (s.charAt(3) != "-" || s.charAt(6) != "-")
	{
		alert("Invalid Social Security Number: enter proper separators '-', for example: 123-45-6789");
		obj.focus();
		obj.select();
		return false;
	}
	for (i=0; i < s.length; i++)
	{
		if (i==3 || i==6)
			continue;
		c = s.charAt(i);
		if (IsDigit_numeric(c) != true)
		{
			alert("Invalid Social Security Number: found non-numeric digits, use 0-9");
			obj.focus();
			obj.select();
			return false;
		}
	}
	//now check validity
	//the following numbers are not legitimate
	//123-45-6789
	//012-34-5678
	//987-65-4321
	//876-54-3210
	//000-00-0000
	//111-11-1111
	//222-22-2222
	//333-33-3333
	//444-44-4444
	//555-55-5555
	//666-66-6666
	//777-77-7777
	//888-88-8888
	//999-99-9999
	//many others but this should help
	return true
}

function IsValidAddress_street(obj,val)
{
	var s="";
	var c="";
	var i=0;
	s=Trim(val);
	if (s=="")
	{
		alert("Invalid Street Address: empty");
		obj.focus();
		obj.select();
		return false;
	}
	c= s.charAt(0);
	if (c=='.' || c==',' || c=='\'' || c=='\/' || c=='-' || c=='#' || c=='&' || c==' ')
	{
		alert("Invalid Street Address: bad characters");
		obj.focus();
		obj.select();
		return false;
	}
	for (i=0; i < s.length; i++)
	{
		c=s.charAt(0);
		if (!(IsDigit_alphanumeric(c)== true || c=='.' || c==',' || c=='\'' || c=='\/' || c=='-' || c=='#' || c=='&' || c==' '))
		{
			alert("Invalid Street Address: bad characters");
			obj.focus();
			obj.select();
			return false;
		}
	}
	return true;
}

function IsValidAddress_city(obj,val)
{
	var s="";
	var c="";
	var i=0;
	s=Trim(val);
	if (s=="")
	{
		alert("Invalid City: empty");
		obj.focus();
		obj.select();
		return false;
	}
	c= s.charAt(0);
	if (c=='.' || c==',' || c=='\'' || c=='\/' || c=='-' || c=='&' || c==' ')
	{
		alert("Invalid City: bad characters");
		obj.focus();
		obj.select();
		return false;
	}
	for (i=0; i < s.length; i++)
	{
		c=s.charAt(0);
		if (!(IsDigit_alphanumeric(c)== true || c=='.' || c==',' || c=='\'' || c=='\/' || c=='-' || c=='&' || c==' '))
		{
			alert("Invalid Address: bad characters");
			obj.focus();
			obj.select();
			return false;
		}
	}
	return true;
}

function IsValidAddress_state(obj,val)
{
  return IsValidAddress_city(obj,val);
}


function IsValidAddress_zip5(obj,val)
{
	//zip is numeric only for USA
	var s="";
	var c="";
	var i=0;
	s=Trim(val);
	if (s.length != 5)
	{
		alert("Invalid Zip: 5 numeric digits required, use 0-9, for example: 12345");
		obj.focus();
		obj.select();
		return false;
	}
	for (i=0; i < s.length; i++)
	{
		c = s.charAt(i);
		if (IsDigit_numeric(c) != true)
		{
			alert("Invalid Zip: found non-numeric digits, use 0-9");
			obj.focus();
			obj.select();
			return false;
		}
	}
	//now check validity
	//the following numbers are not legitimate
	//00000
	//99999
	//12345
	if (s=="00000" || s=="99999")
	{
		alert("Invalid Zip: found non-legitimate zipcode |"+s+"|");
		obj.focus();
		obj.select();
		return false;
	}
	return true;
}

function IsValidName_person(obj,val)
{
	var s="";
	var c="";
	var i=0;
	s=Trim(val);
	if (s=="")
	{
		alert("Invalid Name: empty");
		obj.focus();
		obj.select();
		return false;
	}
	c= s.charAt(0);
	if (c=='-' || c==' '|| c=='\'')
	{
		alert("Invalid Name: bad characters");
		obj.focus();
		obj.select();
		return false;
	}
	for (i=0; i < s.length; i++)
	{
		c=s.charAt(0);
		if (!(IsDigit_alpha(c)== true || c=='-' || c==' ' || c=='\''))
		{
			alert("Invalid Name: bad characters");
			obj.focus();
			obj.select();
			return false;
		}
	}
	return true;
}

function IsValidName_company(obj,val)
{
	var s="";
	var c="";
	var i=0;
	s=Trim(val);
	if (s=="")
	{
		alert("Invalid Name: empty");
		obj.focus();
		obj.select();
		return false;
	}
	c= s.charAt(0);
	if (c=='-' || c==' ' || c=='&' || c=='.' || c==',' || c==':' || c=='\'' || c=='#' || c=='@')
	{
		alert("Invalid Name: bad characters");
		obj.focus();
		obj.select();
		return false;
	}
	for (i=0; i < s.length; i++)
	{
		c=s.charAt(0);
		if (!(IsDigit_alphanumeric(c)== true || c=='-' || c==' ' || c=='&' || c=='.' || c==',' || c==':' || c=='\'' || c=='#' || c=='@'))
		{
			alert("Invalid Name: bad characters");
			obj.focus();
			obj.select();
			return false;
		}
	}
	return true;
}

function IsValid_email(t,checkfor)
{
	var s="";
	var c="";
	var i=0;

	s=Trim(t);
	s=s.toLowerCase();
	if (s=="")
		return false;
	if (GetStringCount_byte(s,"@") != 1)
		return false;
	var newarray = s.split('@');
	if (newarray.length != 2)
		return false;
	v = newarray[0];
	w = newarray[1];

	//the name side could be complex
	

	//the domain must be same as webdomain rules
	var newarray2 = s.split('.');
	if (newarray2.length != 2)
		return false;
	x = newarray2[0];
	y = newarray2[1];

	//the tail must be
	//"com" || "net" || "org" || "edu" || "gov" || "mil"

	if (!(y == "com" || y == "net" || y == "org" || y == "edu" || y == "gov" || y == "mil"))
		return false;
	return true;
}

function IsValid_webdomain(t,checkfor)
{
	//format is subdomain.domain.com, subdomain.domain.org, subdomain.domain.edu, subdomain.domain.gov, or subdomain.domain.mil
	var s="";
	var c="";
	var i=0;
	var v=null;

	s=Trim(t);
	s=s.toLowerCase();
	if (s=="")
		return false;
	n=GetStringCount_byte(".");
	if (n != 2)
		return false;
	var v=s.split(".");
	s=v[0];
	if (s.charAt(0) == "_" || s.charAt(0) == "-")
		return false;
	for (i=0; i < s.length; i++)
	{
		c=s.charAt(i);
		if (!(IsDigit_alphanumeric(c) == true || c == "_" || c == "-"))
			return false;
	}
	s=v[1];
	if (s.charAt(0) == "_" || s.charAt(0) == "-")
		return false;
	for (i=0; i < s.length; i++)
	{
		c=s.charAt(i);
		if (!(IsDigit_alphanumeric(c) == true || c == "_" || c == "-"))
			return false;
	}
	s=v[2];
	if (checkfor==".com")
	{
		if (s != "com")
			return false;
	}
	else
	{
		if (!(s == "com" || s == "net" || s == "org" || s == "edu" || s == "gov" || s == "mil"))
			return false;
	}
	return true;
}

function IsValidInternet_emailuser()
{
	return true;
}
function IsValidInternet_subdomain()
{
	return true;
}
function IsValidInternet_domain()
{
	return true;
}
function IsValidInternet_division()
{
	return true;
}

////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////


function IsDigit_numeric(t)
{
	if (t < "0" || t > "9")
		return false;
	else
		return true;
}

function IsDigit_alpha(t)
{
	if ((t >= "a" && t <= "z") || (t >= "A" && t <= "Z"))
		return true;
	else
		return false;
}

function IsDigit_alphanumeric(t)
{
	if ((t >= "0" && t <= "9") || (t >= "a" && t <= "z") || (t >= "A" && t <= "Z"))
		return true;
	else
		return false;
}

function IsDigit_punctuation(t)
{
	if (t == "," || t == ":" || t == ";" || t == "." || t == "?" || t == "!")
		return true;
	else
		return false;
}

function IsInRange_int(val,lo,hi)
{
	var num=0;
	var t="";
	t=Trim(val);
	if (IsInteger(t) != true)
		return false;
	num=parseInt(t);
	if (num < lo || num > hi)
		return false;
	return true;	
}

function IsInRange_float(val,lo,hi)
{
	var num=0.0;
	var t="";
	t=Trim(val);
	if (IsFloat(t) != true)
		return false;
	num=parseFloat(t);
	if (num < lo || num > hi)
		return false;
	return true;	
}

function IsInteger(val)
{
	var t="";
	t=Trim(val);
	if (isNaN(parseInt(t)))
		return false;
	return true;
}

function IsFloat(val)
{
	var t="";
	t=Trim(val);
	if (isNaN(parseFloat(t)))
		return false;
	return true;
}

//sample usage
//onChange="if (!IsInRange(this.value,0,100)) {this.focus();this.select()}"


function IsMaxLength(t,max)
{
	var s="";
	s=Trim(t);
	if (s.length > max)
	      return false;
	return true;
}

function IsMinLength(t,min)
{
	var s="";
	s=Trim(t);
	if (s.length < min)
	      return false;
	return true;
}

function IsLengthInRange(t,min,max)
{
	var s="";
	if (min > max)
	{
		alert("IsLengthInRange() ... parameter error min > max");
		return false;
	}
	s=Trim(t);
	if (s.length < min)
	      return false;
	if (s.length > max)
	      return false;
	return true;
}

function IsLength(t,len)
{
	var s="";
	s=Trim(t);
	if (s.length==len)
		return true;
	return false;
}

////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////

function GetStringCount_byte(str,c)
{
	var n=0;
	var max=0;
	var i=0;

	max=str.length;
	for (i=0; i < max; i++)
	{
		if (str.charAt(i) == c)
			n++;
	}
	return n;
}

function GetStringCount_substr()
{
	//use an indexOf loop
	return -1;
}

function REPLACE_byte_with(t,r,w)
{
	var i=0;
	if (r==w)
		return false;
	if (r.length != 1)
		return false;
	if (w.length != 1)
		return false;
	for (i=0; i< t.length; i++)
	{
		if (t.charAt(i) == r)
			t.charAt(i) = w;
	}
	return true;
}

////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////

function IsValidPassword(t,min,max)
{
	//no triming here

	//validate size bounding

	//prevent stupid passwords with easy to guess information (e.g., 12345 or password) also force a complex character or two into mix
	return true;
}

function STRING_identical(a,b)
{
	if (a != b)
		return false;
	return true;
}



////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////

function IsValidDateTime(t)
{
	return true;
}

function IsValidDate(t)
{
	return true;
}

function IsValidTime(t)
{
	return true;
}

////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////
////////

function isNumeric(a_string)
{
	var i;
	var sign;
	var decimalcount;
	var the_data = new String(a_string);
	var len = the_data.length;

	//check size
	if (len < 1)
		return false;  //no data entered

	//check for sign
	i = 0;
	decimalcount = 0;
	sign = 0;
	if ((the_data.charAt(i) == '-'))
	{
		if (len < 2)
			return false;  //only sign entered
		else
		{	
			sign = 1;  //there exists a sign
			i++;
		}
	}
	while (i < len)
	{
		if (IsDigit_numeric(the_data.charAt(i)))
			i++;
		else
		{
			if (decimalcount)
				return false;  //No other characters are allowed, and we ALREADY have one "."
			if (the_data.charAt(i) == '.')
			{
				decimalcount++; 
				i++; 
			}
			else
				return false; //the value is not a number
		}
	}

	return true; //the value is a number
}

function isInteger(a_string)
{
	var the_data = new String(a_string);
	var len = the_data.length;
        var i;
	var sign;
	

	//check size
	if (len < 1)
		return false;  //no data entered

	//check for sign
	i = 0;
	sign = 0;
	if ((the_data.charAt(i) == '-'))
	{
		if (len < 2)
			return false;  //only sign entered
		else
		{
			sign = 1;  //there exists a sign
			i++;
		}
	}
	
	while (i < len)
	{
		if (IsDigit_numeric(the_data.charAt(i)))
			i++;
		else
		{
			if (the_data.charAt(i) == '.')
				return false; //change to indicate float
			else
				return false; //change to indicate bad data
		}
	}		
	return true;  //the data is an integer
}

function isZeroInteger(a_string)
{
	var the_data = new String(a_string);
	var len = the_data.length;

	//check size
	if (len != 1)
		return false;  //not a single digit

	//check for sign
	if (the_data.charAt(0) != '0')
		return false;  //data is not a zero
	else
		return true;  //data is a zero
}

function isNonNegativeInteger(a_string)
{
	//MEANS >=0
	var i;
	var sign;
	var the_data = new String(a_string);
	var len = the_data.length;

	//check size
	if (len < 1)
		return false;  //no data entered

	if ((len == 1) && (the_data.charAt(0) == '0'))
		return true;  //the value is zero

	//check for sign
	i = 0;
	sign = 0;
	if (the_data.charAt(i) == '-')
	{
		if (len < 2)
			return false;  //only sign entered
		else
		{
			sign = 1;
			i++;
		}
	}
	
	while (i < len)
	{
		if (IsDigit_numeric(the_data.charAt(i)))
			i++;
		else
		{
			if (the_data.charAt(i) == '.')
				return false; //change to indicate float
			else
				return false; //change to indicate bad data
		}
	}
	
	if (sign)
		return false;  //number is negative
	else
		return true;  //number is non-negative
}

function isNonPositiveInteger(a_string)
{
	//MEANS <=0
	var i;
	var negative;
	var the_data = new String(a_string);
	var len = the_data.length;

	//check size
	if (len < 1)
		return false;  //no data entered

	if ((len == 1) && (the_data.charAt(0) == '0'))
		return true;  //the data is zero

	//check for sign
	i = 0;
	negative = 0;
	if (the_data.charAt(i) == '-')
	{
		if (len < 2)
			return false;  //only sign entered
		negative = 1;
		i++;
	}
	
	while (i < len)
	{
		if (IsDigit_numeric(the_data.charAt(i)))
			i++;
		else
		{
			if (the_data.charAt(i) == '.')
				return false;  //change to indicate float
			else
				return false; //change to indicate bad data

		}
	}

	//case of "0" was checked above
	if (negative)
		return true;  //the number is negative
	else
		return false; //the number is positive
}

function isNonZeroInteger(a_string)
{
	//MEANS <=0
	var i;
	var negative;
	var the_data = new String(a_string);
	var len = the_data.length;

	//check size
	if (len < 1)
		return false;  //no data entered

	if ((len == 1) && (the_data.charAt(0) == '0'))
		return false;  //the data is zero

	//check for sign
	i = 0;
	negative = 0;
	if (the_data.charAt(i) == '-')
	{
		if (len < 2)
			return false;  //only sign entered
		negative = 1;
		i++;
	}
	
	while (i < len)
	{
		if (IsDigit_numeric(the_data.charAt(i)))
			i++;
		else
		{
			if (the_data.charAt(i) == '.')
				return false;  //change to indicate float
			else
				return false; //change to indicate bad data

		}
	}

	//case of "0" was checked above
	if (negative)
		return true;  //the number is negative
	else
		return true; //the number is positive
 
}

function isFloat(a_string)
{
	var AllowIntegerBypassYN = 'N';
	var i=0;
	var negative;
	var decimalcount; 
	var the_data = new String(a_string);
	var len = the_data.length;

	//check size
	if (len < 1)
		return false;  //no data entered

	//check for sign
	i = 0;
	negative = 0;
	decimalcount = 0;
	if (the_data.charAt(i) == '-')
	{
		if (len < 2)
			return false;  //only sign entered
		negative = 1;
		i++;
	}
	
	while (i < len)
	{
		if (IsDigit_numeric(the_data.charAt(i)))
			i++;
		else
		{
			if (decimalcount)
				return false;  //No other characters are allowed, and we ALREADY have one "."
			if (the_data.charAt(i) == '.')
			{
				decimalcount++;
				i++;
			}
			else
				return false;
		}
    }
	if (AllowIntegerBypassYN == 'Y')
		return true;  //function allows integers
		    
    if (decimalcount)
		return true;  //we must be numeric AND have one decimal ONLY
	else    
		return false; 
}


