//<script language="JavaScript1.2">
<!--
// General 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 brackets = "()";
var comma = ",";
var qw = "\"";
var colon = ":";
var slash = "/";
var plus = "+";
var atSymbol = "@";
var phoneNumberDelimiters = "()- ";
var validPhoneChars = digits + phoneNumberDelimiters;
var validEmail = digits+letters+capitalletters+underline+atSymbol+dot+dash;

// 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;
}

//Left Trim
function LTrim (s)
{
	var i = 0;
	while ((i < s.length) && charInString(s.charAt(i), whitespace))
	{
		i++;
	}
	return s.substring (i, s.length);
}

//Right Trim
function RTrim (s)
{
	var i = s.length - 1;
	while ((i >= 0) && charInString(s.charAt(i), whitespace))
	{
		i--;
	}
	return s.substring(0, i+1);
}
//Trim left and right
function Trim (s)
{
	return RTrim (LTrim(s));
}


// 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 letter
function isAlpha (c)
{
	return ( ((c >= 'A') && (c <= 'Z')) || ((c >= 'a') && (c <= 'z')) );
}

//Returns true if string s is a positive integer value
function isInteger (s)
{
	s = Trim(s);
	
	if (isEmpty(s)) return false;
	isDashed = s.charAt(0) == dash;
	isPlused = !isDashed;
	for (var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i) // Check that current character is number
		if (!isDigit(c) && !((isPlused || isDashed) && i == 0)) return false;
	}
	// All characters are numbers.
	return true;
}

//Returns true if string s is a positive integer value
function isPositiveInteger (s)
{
	return isInteger(s) && s >= 0;
}

//Returns true if string s is a negative integer value
function isNegativeInteger (s)
{
	return isInteger(s) && s < 0;
	
}


// 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;
}

// Returns true if string s is an float value
function isFloat (s)
{
	if (isEmpty(s)) return false;
	var bDotPassed = false;
	for (var i=0; i<s.length; i++)
	{
		if (!(i==0 && s.charAt(i) == '-'))
		if (!isDigit(s.charAt(i)))
		{
			if (s.charAt(i) == '.' && !bDotPassed) bDotPassed = true;
			else return false;
		}
	}
	return true;
}

// Returns true if string s is an float value
function isFloat3 (s)
{
	if (isEmpty(s)) return false;

	if (isNaN(s)) 
		return false;
	else 
		return true;

}

function isWhitespace (s)
{
	if (isEmpty(s)) return true;
	return CharsInBag(s,whitespace);
}

// Returns true if all chars of string s belong to string bag
function CharsInBag (s, bag)
{
	for (i = 0; i < s.length; i++)
	{
		if (bag.indexOf(s.charAt(i)) == -1) return false;
	}
	return true;
}

// Returns true if string s is a valid phone number
function isPhoneNumber (s)
{
	var validChars = digits+dash+space+brackets+plus;
	return CharsInBag(s, validChars);
	//return CharsInBag(s,validPhoneChars)
}

// Returns true if string s is a valid email address
function isEmail (s)
{
    reEmail = /^.+\@.+\..+$/;
	if (!reEmail.test(s)) return false;
	return true;
}

// Returns true if string s is a valid WWW URL
function isWWWurl(s)
{
	if(s.length < 8) return false;
	var protocol = s.substring(0, 7);
	protocol = protocol.toLowerCase();
	if ('http://' != protocol) return false;
	reWWW = /^.+\..+$/;
	s = s.substring(7,s.length);
	if (!reWWW.test(s)) return false;
	return true;
}

// Returns true if file upload control contains valid image filename
function isImage (fc)
{
	if (emptyInput(fc))
		return false;
	f = fc.value;
	ext = f.slice(f.lastIndexOf(".")).toLowerCase();
	if (ext != ".gif" && ext != ".jpg" && ext != ".png") {
		return false;
	}
	return true;
}

// Returns true if there is selected option in the specified selectbox
// You can use also EmptyInput for this purpose
function isSelected(sel) {
	return (sel.selectedIndex != -1);
}

// 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;
}


// 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;
}


// 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;
}

// Check date - for pages with multiple controls
// fields - array of form field names
// fields/messages arrays should be {day, month, year}
// yearbounds - {min, max} year values
function checkDate1 (form, fields, messages, yearbounds, i) {
	if ((fields.length != 3) || (messages.length != 3) || (yearbounds.length != 2))
		return false;
	if (i != 'undefined') indx = "["+i+"]"; else indx = "";
	year = form.elements[fields [2]+indx];
	month = form.elements[fields [1]+indx];
	day = form.elements[fields [0]+indx];
	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;
}
// Check date (ISO format)
// field - form field
// fields/messages arrays should be {day, month, year}
// yearbounds - {min, max} year values
function checkDate2 (form, field, messages, yearbounds) {
	CDate = Trim(field.value);
	if (CDate == '') return true;
	var DateParts = CDate.split('-');
	if (DateParts.length != 3)
		return alertInput (field, 'Incorrect date');

	year = DateParts[0];
	month = DateParts[1];
	day = DateParts[2];

	if (!isInteger(year) || (year < yearbounds[0]) || (year > yearbounds[1]))
		return alertInput (field, messages[2]+': should be '+yearbounds[0]+'-'+yearbounds[1]);
	if (month.length != 2)
		return alertInput (field, 'Use a two-digit format to specify a month');
	if (!isInteger(month) || (month < 1) || (month > 12))
		return alertInput (field, messages[1]+': should be 01-12');
	maxdays = numDaysIn(month, year);
	if (day.length != 2)
		return alertInput (field, 'Use a two-digit format to specify a day');
	if (!isInteger(day) || (day < 1) || (day > maxdays))
		return alertInput (field, messages[0]+': should be 01-'+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;
}

function isNumber(value)
{
	if (isEmpty(value))
		return false;

	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	check_char = start_format.indexOf(value.charAt(0));

	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	for (var i = 1; i < value.length; i++)
	{
		check_char = number_format.indexOf(value.charAt(i));
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
		}
		else if (trailing_blank)
			return false;
		else
			digits = true;
	}	

	return true
}

function checkNumberRange(value, min_value, max_value)
{
	if (!isEmpty(min_value))
	{
		if (value < min_value)
			return false;
	}

	if (!isEmpty(max_value))
	{
		if (value > max_value)
			return false;
	}

	return true;
}


function checkRange(value, min_value, max_value)
{
	if (isEmpty(value.length))
		return false;

	if (!isNumber(value))
		return false;
	else
		return (checkNumberRange((eval(value)), min_value, max_value));

	return true;
}


function isTime(value)
{
	if (isEmpty(value))
		return false;

	isplit = value.indexOf(':');

	if (isplit == -1 || isplit == value.length)
		return false;

	sHour = value.substring(0, isplit);
	iminute = value.indexOf(':', isplit + 1);

	if (iminute == -1 || iminute == value.length)
		sMin = value.substring((sHour.length + 1));
	else
		sMin = value.substring((sHour.length + 1), iminute);

	if (!isPositiveInteger(sHour))
		return false;
	else if (!checkRange(sHour, 0, 23))
		return false;

	if (!isPositiveInteger(sMin))
		return false;
	else
	if (!checkRange(sMin, 0, 59))
		return false;

	if (iminute != -1)
	{
		sSec = value.substring(iminute + 1);

		if (!isPositiveInteger(sSec))
			return false;
		else if (!checkRange(sSec, 0, 59))
			return false;	
	}

	return true;
}

function isZip(value)
{
	if (isEmpty(value))
		return false;

	if (value.length != 5 && value.length != 10)
		return false;

	if (value.charAt(0) == "-" || value.charAt(0) == "+")
		return false;

	if (!isPositiveInteger(value.substring(0,5)))
		return false;

	if (value.length == 5)
		return true;

	if (value.charAt(5) != "-" && value.charAt(5) != " ")
		return false;

	if (value.charAt(6) == "-" || value.charAt(6) == "+")
		return false;

	return (isPositiveInteger(value.substring(6,10)));
}

// get/set inner browser dimmensions
function getInnerDim() {
    var dim=new Array(2);
    //if browser supports window.innerWidth
    if (window.innerWidth) {
        dim[0] = window.innerWidth;
        dim[1] = window.innerHeight;
    }
    //else if browser supports document.all (IE 4+)
    else if (document.all) {
        dim[0] = document.body.clientWidth;
        dim[1] = document.body.clientHeight;
    }
    return dim;
}

//-->
//</script>
