//----------------------------------------------------------
//	Project Validation Routines
//----------------------------------------------------------

function isSpace(ch) 
{
	// This function examines a character if it is a blank space.
	if ((ch == " ") || (ch == "\n") || (ch == "\t") || (ch == "\r")) {
		return true;
	}
	else {
		return false;
	}
}

function isWhiteSpace(s)
{
	var white_space = "\n\t\r ";
		
	for (var i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
			
		if (white_space.indexOf(c) == -1)
			return false;
	}
	return true;
}
	
function isEmpty(s)
{
	return s == null || s.length == 0 || s.value == 0 || isWhiteSpace(s);
}
	
function trimAll(str)
{
	// This function trims all blank space at start
	// and at the end of the specified string.
	var i = 0;
	var j = 0;
		
	if ((str == "") || ((str.length == 1) && (! isSpace(str.charAt(0))))) {
		return str;
	}
	for (i = 0; i < str.length; i++) {
		if (! isSpace(str.charAt(i))) {
			break;
		}
	}
	if (i >= str.length) {						// Empty string
		return "";
	}
	for (j = str.length  - 1; j > 0; j--) {		// Not empty string. We will trim it.
		if (! isSpace(str.charAt(j))) {
			break;
		}
	}
	return str.substring(i, j + 1);
}

function isFieldEmpty(inp, msg)
{
	// This function performs test on input value and returns false if it is empty.
	inp.value = trimAll(inp.value);

	if (inp.value == "") {
		if (msg == "") {
			msg = "Required field is not completed.\nPlease correct this before continuing.";
		}
		alert(msg);
		inp.focus();
		return true;
	} 
	return false;
}

function validateDate(inp, field_name, required)
{
	inp.value = trimAll(inp.value);
	
	if (field_name.length == 0 || field_name.length == null) {
		field_name = "this"
	}
	else {
		field_name = "the " + field_name
	}
	if (inp.value == "") {
		if (required) {
			alert("Please complete " + field_name + " field before continuing.");
			inp.focus();
			return false;
		}
		else {
			return true;
		}
	}
	if (isNaN(Date.parse(inp.value))) {
		alert("Please enter a valid date in " + field_name + " field.");
		inp.focus();
		inp.select();
		return false;
	}
	return true;
}

function validateDate2(strDate)
{
	var parsedDate;
	var day, month, year;
	
	if (strDate.indexOf("-") > 0) {
		parsedDate = strDate.split ("-");
	}
	else {
		parsedDate = strDate.split ("/");
	}	

	if (parsedDate.length != 3) return false;

	month = parsedDate[0]-1;
	day = parsedDate[1];
	year = parsedDate[2];

	if (year.length != 4) return false;

	var objDate = new Date (strDate);

	if (objDate.getFullYear() < 1900) return false;
	if (objDate.getFullYear() > 9999) return false;

	if (month != objDate.getMonth()) return false;
	if (day != objDate.getDate()) return false;
	if (year != objDate.getFullYear()) return false;

	 return true;
}

function validateFileName(inp, field_name, required)
{
	inp.value = trimAll(inp.value);
	
	if (field_name.length == 0 || field_name.length == null) {
		field_name = "this"
	}
	else {
		field_name = "the " + field_name
	}
	if (inp.value == "") {
		if (required) {
			alert("Please complete " + field_name + " field before continuing.");
			inp.focus();
			return false;
		}
		else {
			return true;
		}
	}
	var notValid = false;
	var invalidChars = '?*<>:/\\"|';
	
	for (var i = 0; i < inp.value.length; i++) {
		if (invalidChars.indexOf(inp.value.charAt(i)) != -1 || inp.value.charCodeAt(i) < 32) {
			notValid = true;
			break;
		}
	}
	if (notValid) {
		alert("Please enter a valid file name in " + field_name + " field.");
		inp.focus();
		inp.select();
		return false;
	}
	return true;
}

function validateNumber(inp, field_name, term, required)
{
	inp.value = trimAll(inp.value);
	
	if (field_name.length == 0 || field_name.length == null) {
		field_name = "this"
	}
	else {
		field_name = "the " + field_name
	}
	if (term.length == 0 || term.length == null) {
		term = "number"
	}
	if (inp.value == "") {
		if (required) {
			alert("Please complete " + field_name + " field before continuing.");
			inp.focus();
			return false;
		}
		else {
			return true;
		}
	}
	if (isNaN(inp.value)) {
		alert("Please enter a valid " + term + " in " + field_name + " field.");
		inp.focus();
		inp.select();
		return false;
	}
	return true;
}

function validateFilePath(inp, field_name, required)
{
	if (field_name.length) 
	{
		field_name = "the " + field_name;
	}	
	else 
	{
		field_name = "this";
		
	}
	if (inp.value == "") 
	{
		if (required) 
		{
			alert("Please complete " + field_name + " field before continuing.");
			inp.focus();
			return false;
		}
		else 
		{
			return true;
		}
	}
	
	var pattern = /(\?)|(\*)|(\<)|(\>)|(\|)|(\&)|(')|(\*)/;
	var match = inp.value.match(pattern)
	if (match != null)
	{
		alert("Please enter a valid file name in " + field_name + " field.");
		inp.focus();
		inp.select();
		return false;
	}

	return true;
}

function validateURL( inp, field_name, required )
{
	
	strURL = trimAll( inp.value );
	var pattern;
	var match;
	
	
	if (field_name.length == 0 || field_name.length == null) {
		field_name = "URL"
	}
	
	if( (strURL == "") && (!required) )
	{
		return true;
	}
	
	pattern = /http:\/\/\S+\.\S{2,4}/;
	match	= strURL.match(pattern);
	
	if(match)
	{
		if(match.toString().length == strURL.length)
		{
			return true;
		}
		else
		{
			alertInvaludURL();
			inp.focus();
			return false;
		}
	}
	else
	{
		alertInvaludURL();
		inp.focus();
		return false;
	}
}

function alertInvaludURL()
{

	alert("Please enter a valid URL. For example:\n\n" + 
		  "http://www.yourdomain.com\n\n-or-\n\n" +
		  "http://www.yourdomain.com/yourdirectory/yourpage.htm");
}

var defaultEmptyOK = false;

function validateEmailAddress(input_addr)
{
//	var email = trimAll(input_addr);
	
	if (!(/^([a-zA-Z0-9]+([_-]+[a-zA-Z0-9]+)*\.)*[a-zA-Z0-9_-]+([_-]+[a-zA-Z0-9]+)*@([a-zA-Z0-9]+([-]+[a-zA-Z0-9]+)*\.)+[a-zA-Z]{2,4}$/.test(input_addr))) { 
       return false;
    }
    return true;
}

function ValidateCheckBoxes(inp, message)
{
	var selected = false;
			
	if (inp.length != null) {
		for (i = 0; i < inp.length; i++) {
			if(inp[i].checked) {
				selected = true;
				break;
			}	
		}
	}
	else {
		selected = inp.checked;
	}
	if (! selected) {
		alert(message);
		return false;
	}
	return true;
}

function validateRadioButtons(inp, message)
{
	var checksLength = inp.length
	var checked		 = false;
	var counter;
	
	if( typeof(checksLength) == "undefined" )
	{
		if( inp.checked )
		{
			checked = true;
		}
	}
	else
	{
		for(counter = 0; counter < checksLength; counter++)
		{
			if( inp[counter].checked )
			{
				checked = true;
				break;
			}
		}
	}
	
	if(! checked )
	{
		alert( message );
	}
	return checked;	
}

function isTextValid(text, isFieldRequired, message1, message2)
{
	text = trimAll(text)
	
	if(text.length > 90000)
	{
		alert(message1)
		return false;
	}
	else
	{
		if(isFieldRequired)
		{
			if( ( text.length == 0 ) || (isHTMLEmpty(text)))
			{
				alert(message2)
				return false;
			}
		}
	}
	
	return true;
}

function isTextValid1(text, isFieldRequired, maxSymboles, message1, message2)
{
	text = trimAll(text)
	
	if(text.length > maxSymboles)
	{
		alert(message1)
		return false;
	}
	else
	{
		if(isFieldRequired)
		{
			if( ( text.length == 0 ) || (isHTMLEmpty(text)))
			{
				alert(message2)
				return false;
			}
		}
	}
	
	return true;
}

function isHTMLEmpty(htmlText)
{
	
	var temp = trimAll(htmlText);
	
		var re1,re2,re3,re4; 
	
		re1 = new RegExp("</p>","ig"); 
		re2 = new RegExp("<p>","ig");
		re3 = new RegExp("&nbsp;","ig")
		re4 = new RegExp("<br>","ig")
	
		temp = htmlText.replace(re1,"");
		temp = temp.replace(re2,"");
		temp = temp.replace(re3,"")
		temp = temp.replace(re4,"")
		
		if(temp.length == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
}

function isWord(text, message)
{
	var word, regExp
	
	if(text == null)
	{
		return false;
	}
	
	if(typeof(text) == "undefined")
	{
		return false;
	}
	
	regExp	= /\w+/g;
	word	= regExp.exec(text);
	
	if(word == null)
	{
		alert(message);
		return false;
	}
	else
	{
		if(word[0] != text)
		{
			alert(message);
			return false;
		}
	}
	
	return true;
}