
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*\

Purpose :
This file is used for form validations using Javascript.

CFD Path : Z:\WEBSITES\shaaditimes.com\dev\ssi\common-js.js

Change History :

Mod#		Date			Who			Description
-------		--------		------		---------------------------------
00000009	21-Jul-2007 	Aasif R		function added select_unselect_all
00000008	28-Mar-2007		Vishnu U	function added IsNumericDecimal
00000007	1/11/2007		Preeti		functions for telephone and customised email validation
00000006	12/19/2006		Preeti		Added functions for name check n numeric field check
00000005	10/11/2006		Yograj		Modified the error message.
00000004	9/6/2006		Preeti		function checkNumber(input, kbEvent, dataType) added
00000003	8/28/2006		Preeti		function check_radiobutton return value changed
00000002	7/8/2006		Preeti		function check_radiobutton(field_val) added
00000001	7/7/2006		Yograj		Validations functions added.
\*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/

// Validate the email address.
function CheckEmail(field)
{
	if(field.value != '')
	{
	//	alert(field);
		var emailStr = new String();
		emailStr=field.value;
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

		var matchArray=emailStr.match(emailPat);
		if (matchArray==null) {
			alert("Invalid email address");
			field.focus();
			field.select();
			return false;
		}
		var user=matchArray[1];
		var domain=matchArray[2];

		if (user.match(userPat)==null) {
			alert("Invalid email address.");
			field.focus();
			field.select();

			return false;
		}
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null)
			{
			for (var i=1;i<=4;i++)
				{
				if (IPArray[i]>255)
					{
					alert("Invalid email address");
					field.focus();
					field.select();
					return false;
					}
				}
			return true;
			}
		var domainArray=domain.match(domainPat);
		if (domainArray==null)
			{
				alert("Invalid email address");
				field.focus();
				field.select();
				return false;
			}
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 ||
			domArr[domArr.length-1].length>3)
			{
				// the address must end in a two letter or three letter word.
				alert("Invalid email address.");
				field.focus();
				field.select();
				return false;
			}

		if (len<2)
			{
				var errStr="Invalid email address!";
				alert(errStr);
				field.focus();
				field.select();
				return false
			}
		return true;
	}
	else
	{
		alert("Please enter the email address");
		return false;
	}


}//function CheckEmail - to check email address ends here.

// function to trim the stings
function trim(s)
{
	// Remove leading spaces and carriage returns

	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{
		s = s.substring(1,s.length);
	}

	// Remove trailing spaces and carriage returns

	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{
		s = s.substring(0,s.length-1);
	}

	return s;
}


// Validate the textfield value
function validate_textfield(txt)
{
	if(trim(txt) == "")
	{
		return false;
	}
	else
	{
		return true;
	}
}


// Validate the age.
function validate_age(age)
{
	var age = parseInt(age);

	if(age == "" || age >= 99 || age <= 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}


// Validate the day
function validate_day(day)
{
	var day = parseInt(day);

	if(day == "" || day <= 0 || day >= 31)
	{
		return false;
	}
	else
	{
		return true;
	}
}

// Validate month
function validate_month(month)
{
	var month = parseInt(month);

	if(month == "" || month <= 0 || month >= 13)
	{
		return false;
	}
	else
	{
		return true;
	}
}


// Validate the year.
function validate_year(year)
{
	var year = parseInt(year);

	if(year == "")
	{
		return false
	}
	else
	{
		return true;
	}
}

// function to allow only numeric data enter for telephone & age
function checkNumber(input, kbEvent, dataType)
{
	var keyCode, keyChar;

	keyCode = event.keyCode;

	if(keyCode == null)
	{
		return true;
	}

	// get character
	keyChar = String.fromCharCode(keyCode);

	var charSet = "0123456789";
	if(dataType == "F")
	{
		charSet = "0123456789.";
		var strValue = input.value;
		// checking for second dot(.) entry.
		if(keyChar == "." && strValue.indexOf(".") > -1)
		{
			return false;
		}
	}

	// check valid chars
	if(charSet.indexOf(keyChar) != -1)
	{
		return true;
	}

	// control keys
	if(keyCode==null || keyCode==0 || keyCode==8 || keyCode==9 || keyCode==13 || keyCode==27)
	{
		return true;
	}
	return false;

} // EO function checkNumber(input, kbEvent, dataType)

// Check for numeric value.
function is_numeric(val)
{
	var val = parseInt(val);

	if(val == "")
	{
		return false;
	}
	else
	{
		return true;
	}
}


// Check for radio button value.
function check_radiobutton(field_val)
{
	val = "";
	for(i = 0; i < field_val.length; i++)
	{
		if(field_val[i].checked)
		{
			val = field_val[i].value;
		}
	}
	if(val == "")
	{
		return false;
	}
	else
	{
		return val;
	}
}

// BOI, followed by one or more lower or uppercase English letters,
// followed by EOI.
var reAlphabetic = /^[a-zA-Z ]+$/


function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// isAlphabetic (STRING s [, BOOLEAN emptyOK])
//
// Returns true if string s is English letters
// (A .. Z, a..z) only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

function isAlphabetic (s)

{   var i;

    if (isEmpty(s))
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    else {
       return reAlphabetic.test(s)
    }
}


// BOI, followed by one or more digits, followed by EOI.
var reInteger = /^\d+$/


function isInteger (s)

{   var i;

    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}


/**
* DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/

// Declaring required variables
var digits = "0123456789";

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";

// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";

// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function is_Integer(s)
{
	var i;
	for (i = 0; i < s.length; i++)
	{
		// Check that current character is number.
		var c = s.charAt(i);

		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function checkInternationalPhone(strPhone)
{
	// do not allow - sign in beginining & at the end
	if(strPhone.indexOf("-") == 0 || strPhone.indexOf("-") == strPhone.length-1)
	{
		return false;
	}
	
	//allow + sign only in beginining
	if(strPhone.indexOf("+") > 0)
	{
		return false;
	}
	
	s=stripCharsInBag(strPhone, validWorldPhoneChars);
	if(is_Integer(s))
	{
		return true;
	}

	if(is_Integer(s) && s.length >= minDigitsInIPhoneNumber)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// this function allows to display the customized error messages
// call this message whenever we wan t o display customized erreo messages for email validations
function validate_email(field)
{
	if(field.value != '')
	{
		var emailStr = new String();
		emailStr=field.value;
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

		var matchArray=emailStr.match(emailPat);
		if (matchArray==null)
		{
			field.focus();
			field.select();
			return false;
		}
		var user=trim(matchArray[1]);
		var domain=matchArray[2];
		
		if (user.match(userPat)==null)
		{
			field.focus();
			field.select();
			return false;
		}
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null)
		{
			for (var i=1;i<=4;i++)
			{
				if (IPArray[i]>255)
				{
					field.focus();
					field.select();
					return false;
				}
			}
			return true;
		}
		var domainArray=domain.match(domainPat);
		if (domainArray==null)
		{
			field.focus();
			field.select();
			return false;
		}
		var atomPat=new RegExp(atom,"g");
		var domArr=domain.match(atomPat);
		var len=domArr.length;
		if (domArr[domArr.length-1].length<2 ||
		domArr[domArr.length-1].length>3)
		{
			// the address must end in a two letter or three letter word.
			field.focus();
			field.select();
			return false;
		}

		if (len<2)
		{
			field.focus();
			field.select();
			return false
		}
			
		var strValue = field.value;
		// checking for (#) entry.
		if(strValue.indexOf("#") > -1)
		{
			return false;
		}
		return true;
	}

}//function validate_email - to check email address ends here.

// digital number check function
function IsNumericDecimal(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for(i = 0; i < sText.length && IsNumber == true; i++)
	{
		Char = sText.charAt(i);
		if(ValidChars.indexOf(Char) == -1)
		{
			IsNumber =	false;
		}
	}
	return IsNumber;

} // EO function

// Selecting or Unselecting all the fields in page
function select_unselect_all(form,field_array,field_name)
{
	var status	= document.getElementById(field_name).checked;
	/*var count	= document[form_name][field_array + '[]'].length;

	for(var i = 0; i < count; i++)
	{
		document[form_name][field_array + '[]'][i].checked = status;
	}*/
	var obj = form.elements;
	
			for (i=0; i<obj.length; i++) {
				 if (obj[i].tagName == "INPUT") {
				if (obj[i].type == "checkbox") {
				   if (obj[i].name == field_array +'[]') {
						obj[i].checked = status;
				   }
				}
			 }
			}
}