//<SCRIPT LANGUAGE="JavaScript">
<!--
// Genaral validation functions
var digits = "0123456789";
var letters = "abcdefghijklmnopqrstuvwxyz";
var capitalletters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var space = " ";
var dash = "-";
var underline = "_";
var dot = "."; 
var whitespace = " \t\n\r";
var phoneNumberDelimiters = "()- ";
var validPhoneChars = digits + phoneNumberDelimiters;
var brackets = "()";
var comma = ",";
var qw = "\"";
var colon = ":";
var slash = "/";
var plus = "+";
// Returns true if single character c (actually a string)
// is contained within string s.
function charInString (c, s)
{
	var i
	for (i = 0; i < s.length; i++) {
		if (s.charAt(i) == c) return true;
	}
	return false;
}

// Removes initial (leading) whitespace characters from s
function LTrim(s) {
	for (var i = 0; i < s.length; i++) if (whitespace.indexOf(s.charAt(i)) == -1) break;
	return s.substring(i, s.length);
}
// Removes ending (trailing) whitespace characters from s
function RTrim(s) {
	for (var i = s.length - 1; i >= 0; i--) if (whitespace.indexOf(s.charAt(i)) == -1) break;
	return s.substring(0, i + 1);
}
 // Returns string with both leading and trailing whitespaces removed from s
function Trim(s) {
	return RTrim(LTrim(s));
}
// Returns true if all chars of string s belong to string bag
function charsInBag(s, bag) {
    for (var i = 0; i < s.length; i++) if (bag.indexOf(s.charAt(i)) == -1) return false;
    return true;
}
// Returns true if all chars of string s don't belong to string bag
function charsNotInBag(s, bag)
{
    for (var i = 0; i < s.length; i++) if (bag.indexOf(s.charAt(i)) != -1) return false;
    return true;
}
// Returns true if string s is empty
function isEmpty(s) {
	return ((s == null) || (s.length == 0));
}

// Returns true if string s1 equal string s2
function isEqual (s1, s2)
{
	if (s1 == s2) return true;
	else return false;
}
// Returns true if character c is a digit
function isDigit(c) {
	return ((c >= "0") && (c <= "9"));
}
// Returns true if character c is a alphabetic (english only)
function isAlpha(c) {
	return ( ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) );
}
// Returns true if all characters in string s are numbers (non-signed integers only)
function isInteger(s) {
	return (!isEmpty(s) && charsInBag(s, digits)); //not empty and belong to digits
}
// Returns true is s is valid IP-address
function isIP(s){
	var ip;
	if (!isEmpty(s)) ip = s.split('.');
		else return false;
	return (isInteger(ip[0]) && (ip[0] >= 0) && (ip[0] <= 255) &&
			isInteger(ip[1]) && (ip[1] >= 0) && (ip[1] <= 255) &&
			isInteger(ip[2]) && (ip[2] >= 0) && (ip[2] <= 255) &&
			isInteger(ip[3]) && (ip[3] >= 0) && (ip[3] <= 255));
}
// Check IP string: all 4ips should be not empty, numbers 0-255. If error return order of error,
// else return 0;
function checkIPStrings(ip1,ip2,ip3,ip4){
	if (!(isInteger(ip1) && (ip1 >= 0) && (ip1 <= 255))) return 1;
	if (!(isInteger(ip2) && (ip2 >= 0) && (ip2 <= 255))) return 2;
	if (!(isInteger(ip3) && (ip3 >= 0) && (ip3 <= 255))) return 3;
	if (!(isInteger(ip4) && (ip4 >= 0) && (ip4 <= 255))) return 4;
	return 0;
}
// Check ip array of text inputs wethere they contain valid ip numbers. If not return error index.
function checkIPArrayText(ip){
	if (!(isInteger(ip[0].value) && (ip[0].value >= 0) && (ip[0].value <= 255))) return 1;
	if (!(isInteger(ip[1].value) && (ip[1].value >= 0) && (ip[1].value <= 255))) return 2;
	if (!(isInteger(ip[2].value) && (ip[2].value >= 0) && (ip[2].value <= 255))) return 3;
	if (!(isInteger(ip[3].value) && (ip[3].value >= 0) && (ip[3].value <= 255))) return 4;
	return 0;
}
function validateIPArrayText(ip) {	//text : input type= text (4)
	var i = checkIPArrayText(ip);
	if (i) {
		alert('Incorrect IP address: ' + 
			ip[0].value + '.' + ip[1].value + '.' + ip[2].value + '.' + ip[3].value);
		ip[i-1].focus();
		ip[i-1].select();
	}
	return i;
}

/////////
// Not used yet by me...
////////

// Returns true if string s is a positive float value
function isPositiveFloat(s) {
	if (isEmpty(s)) return false;
	var bDotPassed = false;
	for (var i=0; i<s.length; i++)
		if (!isDigit(s.charAt(i)))
			if (s.charAt(i) == '.' && !bDotPassed)
				bDotPassed = true;
			else
				return false;
	return true;
}

// Return true if string s is a pisitive integer
function isPositiveInteger(s)
{
	return (!isNaN(parseInt(s)) && parseInt(s) > 0);
}

// Returns true if string s is empty or whitespace characters only
function isWhitespace (s)
{
    if (isEmpty(s)) return true;
	return charsInBag(s,whitespace);
}

// Returns true if string s is a valid phone number
function isPhoneNumber(s) {
	if (isEmpty(s) || (digits.indexOf(s.charAt(0)) == -1) || (digits.indexOf(s.charAt(s.length - 1)) == -1)) return false;
	return charsInBag(s,validPhoneChars)
}

// Returns true if string s is a valid email address
function isEmail(s) {
	var regex = /^[a-z0-9\.\+_-]+@[a-z0-9\._-]+\.[a-z]{2,4}$/i
	return regex.test(s);
}

// Returns true if string s is a valid url
function isUrl(s) {
	var regex = new RegExp("^[a-z0-9\\._-]+\\.[a-z]{2,4}(/.*)?$", "i"); 
	return regex.test(s);
}


// Returns whether the specified year is leap
function leapYear(yr) {
	if (((yr % 4 == 0) && yr % 100 != 0) || yr % 400 == 0)
		return true;
	else return false;
}

// Returns number of days in specified month & year
// month = [1,12]
function numDaysIn(mth, yr) {
	if (mth==4 || mth==6 || mth==9 || mth==11) return 30;
	else if ((mth==2) && leapYear(yr)) return 29;
	else if (mth==2) return 28;
	else return 31;
}

// Form specific validation functions

function emptyInput (input) {
	switch (input.type) {
		case "hidden":
		case "password":
		case "text": return isWhitespace (input.value); break;
		case "textarea": return isWhitespace (input.value); break;
		case "file": return isWhitespace (input.value); break;
		case "select": return (input.selectedIndex == -1);	break;
		default:
			if ((input[0].type == "radio") || (input[0].type == "checkbox")) {
				for (var i=0; i<input.length; i++)
					if (input[i].checked) return false;
			}
			return true;
	}
}

function alertInput (input, message) {
	alert (message);
	input.focus ();
	if ((input.type == "text") || (input.type == "file") || (input.type == "password"))
		input.select ();
	return false;
}

function checkRequiredFields (form, fields, messages) {
	for (var i=0; i<fields.length; i++) {
		if (emptyInput (form.elements [fields [i]])) {
			alertInput (form.elements [fields [i]], messages [i]);
			return false;
		}
	}
	return true;
}

function checkRequiredFieldsLn (form, fields, messages, flengths)
{
	for (var i=0; i<fields.length; i++)
	{
		if (IsShorter (form.elements[fields [i]], flengths[i]))
		{
			alertInput (form.elements [fields [i]], messages [i]);
			return false;
		}
	}
	return true;
}

function IsShorter (element, len)
{
	if (element.value.length < len)
	{
		return true;
	}
	return false;
}

// Check date
// fields - array of form field names
// fields/messages arrays should be {day, month, year}
// yearbounds - {min, max} year values
function checkDate (form, fields, messages, yearbounds) {
	if ((fields.length != 3) || (messages.length != 3) || (yearbounds.length != 2))
		return false;
	year = form.elements[fields [2]];
	month = form.elements[fields [1]];
	day = form.elements[fields [0]];
	if (!isInteger(year.value) || (year.value < yearbounds[0]) || (year.value > yearbounds[1]))
		return alertInput (year, messages[2]+': should be '+yearbounds[0]+'-'+yearbounds[1]);
	if (!isInteger(month.value) || (month.value < 1) || (month.value > 12))
		return alertInput (month, messages[1]+': should be 1-12');
	maxdays = numDaysIn(month.value, year.value);
	if (!isInteger(day.value) || (day.value < 1) || (day.value > maxdays))
		return alertInput (day, messages[0]+': should be 1-'+maxdays);
	return true;
}

function isCreditCard (ccnumber)
{
	var doubledigit = ccnumber.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < ccnumber.length; i++)
	{
		tempdigit = eval(ccnumber.charAt(i))

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
			{
				checkdigit++;
			}

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;


}

// Returns true if string s is a valid US phone number
//function isUSPhoneNumber (s)
//{
//	return false;
//}

function checkDateFromArray(DateArray){
	if (!isInteger(DateArray[0]))
		return false;
	if (!isInteger(DateArray[1]) || (DateArray[1] < 1) || (DateArray[1] > 12))
		return false;
	maxdays = numDaysIn(DateArray[1], DateArray[0]);
	if (!isInteger(DateArray[2]) || (DateArray[2] < 1) || (DateArray[2] > maxdays))
		return false;
	return true;
}


function isLogin(str)
{
	var loginChars = digits + letters + capitalletters + underline;
	return charsInBag(str, loginChars);
}

// -->
//</SCRIPT>