﻿//----------------------------------------------------------
//	Project Validation Routines
//----------------------------------------------------------
function getChecked(e)
{
	if (e.length != null) {
		for (var i = 0; i < e.length; i++) {
			if(e[i].checked) {
				return e[i].value;
			}	
		}
	}
	else {
		if(e.checked) {
			return e.value;
		}	
	}
	return "";
}

function getSelected(e)
{
	if (e.length != null) {
		for (var i = 0; i < e.length; i++) {
			if(e[i].selected) {
				return e[i].value;
			}	
		}
	}
	else {
		if(e.selected) {
			return e.value;
		}	
	}
	return "";
}

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(strDate)
{
	var parsedDate = strDate.split ("/");
	var day, month, year;

	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 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)
{
	if (!(/^([a-z0-9]+([_-]+[a-z0-9]+)*\.)*[a-z0-9_-]+([_-]+[a-z0-9]+)*@([a-z0-9]+([-]+[a-z0-9]+)*\.)+[a-z]{2,4}$/.test(input_addr))) { 
       return false;
    }
    return true;
}

function isEmailChars(s)
{
	var i;
	var usrName, domainName;  //break the email address into two parts for testing

	var ErrorAddrChars = "";
	var invalidAddrChars = "\\\/\"<>(),;:$&!\`\'^*|[]{}";
	var invalidDomainChars = "\\\/\"<>(),;:$&!\`\'^*@|{}";

	if (isEmpty(s)) {
		return (isEmailChars.arguments.length == 1) ? 
			defaultEmptyOK : (isEmailChars.arguments[1] == true);
	}
    // Search through string's characters one by one until we find an invalid character.
    // When we do, return false; if we don't, return true.
    
	// breaking the email address into two substrings for char testing
	i = s.indexOf('@');
	usrName = s.substring(0, i);
	domainName = s.substring(i, s.length);

    // Check that current character before @ is valid
    for (i = 0; i < usrName.length; i++) {
		if (invalidAddrChars.indexOf(usrName.charAt(i)) != -1) {
			ErrorAddrChars += usrName;
			emptyAddr = false;   
		}
		if (ErrorAddrChars.length > 0) return false;
	} 
    // Check that current character after @ is valid
    for (i = 1; i < domainName.length; i++) {
		if (invalidDomainChars.indexOf(domainName.charAt(i)) != -1 ) {
			ErrorAddrChars += domainName;
			emptyAddr = false;   
		}
		if (ErrorAddrChars.length > 0) return false;
	}
	return true;
}

function isEmail(s)
{
    if (isEmpty(s)) {
		return (isEmailChars.arguments.length == 1) ? 
			defaultEmptyOK : (isEmailChars.arguments[1] == true);
	}
    // there must be >= 1 character before @, so we start looking at character position 1
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;
 
    // look for @
    while ((i < sLength) && (s.charAt(i) != "@")) {
		i++
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) {
		return false;
	}
    else {
		i += 2;
	}
    // look for .
	while ((i < sLength) && (s.charAt(i) != ".")) {
		i++
	}
	// there must be at least one character after the .
    return ! ((i >= sLength - 1) || (s.charAt(i) != "."));
}

function ValidateCheckBoxes(inp, message)
{
	var selected = false;
	
	if (inp == null) { 
		alert(message);
		return 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 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 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 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 removePElements(currentHTML)
{
	var temp;
	var re1,re2,re0; 
	
	re0 = new RegExp("<P>&nbsp;</P>","ig")
	re1 = new RegExp("</p>","ig"); 
	re2 = new RegExp("<p>","ig");
	
	temp = currentHTML.replace(re0, "")
	temp = temp.replace(re1,"<br>");
	temp = temp.replace(re2,"");
	
	return temp;
}

function isDateSmaller(strDateSmall, strDateBig)
{
	var dateSmall	= Date.parse(strDateSmall);
	var dateBig		= Date.parse(strDateBig);
	var blnresult	= dateSmall < dateBig
	
	return blnresult
}
		