/* $Id: validation.js,v 1.5 2007/09/27 09:07:02 shanmugampl Exp $ */

/**
 * Generic validate methods that can be used anywhere.
 */

function checkTime(fromObj, toObj) {
	var fromTime = fromObj.value;
	var toTime = toObj.value;
	if(fromTime != "" && toTime != "") {
		if(Date.parse(fromTime) > Date.parse(toTime)) {
			if(document.getElementById(fromObj.name + "_TIME") != null) {
				formObj.value = document.getElementById(fromObj.name + "_TIME").innerHTML;
			}
			if(document.getElementById(toObj.name + "_TIME") != null) {
				toObj.value = document.getElementById(toObj.name + "_TIME").innerHTML;
			}
			return false;
		}
	}
	return true;
}

function trimAll(str)
{
	/*************************************************************
	Input Parameter :str
	Purpose         : remove all white spaces in front and back of string
	Return          : str without white spaces    
	***************************************************************/

	//check for all spaces
	var objRegExp =/^(\s*)$/;
	if (objRegExp.test(str))
	{
		str = str.replace(objRegExp,''); 
		if (str.length == 0)
		return str; 
	} 

	// check for leading and trailling spaces
	objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	if(objRegExp.test(str))
	{
		str = str.replace(objRegExp, '$2');
	}
	return str;
}

function isEmailId(str)
{
	/*****************************************************************
	Input   :  str
	purpose :  To validate for email
	output  :  valid email true/false
	*******************************************************************/
	var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
	return objRegExp.test(str); 
}

function isEmpty(str)
{
	/*************************************************************
	input    : str
	purpose  : To check if empty
	output   : if empty true or false
	***************************************************************/

	var temp = trimAll(str);
	if (temp.length > 0 )
		return false;
	return true;
}

function isIpAddress(str)
{
	/***************************************************************
	input   : str
	purpose : To check the ip Address
	output  : returns true or false
	*****************************************************************/
	
	
	// we removed the usage of regular expression
	// as some of the browsers ( NN in Sun OS )
	// crashes when doing this validation.
	// now we use simple check for doing the same.

	var ipAddress = str.split(".");
	if(ipAddress.length != 4)
	{
		return false;
	}
	for(i=0;i<ipAddress.length;i++)
	{
		if(isPositiveInteger(ipAddress[i]))
		{
			var temp = parseInt(ipAddress[i],10);
			if(temp > 255)
			{
				return false;
			}
		}
		else
		{
			return false;
		}

	}
	return true;
}

function isInteger(str)
{
	/****************************************************************
	input   : str
	purpose : check if number is integer
	output  : returns true or false
	*****************************************************************/
	var objRegExp = /(^-?\d\d*$)/;
	return objRegExp.test(str);
}

function isPositiveInteger(str)
{
	/****************************************************************
	input   : str
	purpose : check if number is positive integer
	output  : returns true or false
	*****************************************************************/
	var temp  = parseInt(str,10);
	if ( isNaN(temp) || temp.toString().length != str.length) {
		return false;
	}
	var objRegExp = /(^\d\d*$)/;
	return objRegExp.test(temp);
}

function isDate(str)
{
	/***************************************************************
	input  : str
	purpose : To check if given str is in date format
	output  : true or false
	***************************************************************/
	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	return objRegExp.test(str); 
}

function isCharacter(str)
{
	/***************************************************************
	input : str
	purpose : To check if given str contains only characters
	output : true or false
	******************************************************************/
	var objRegExp = /^\w$/;
	return objRegExp.test(str);
}

function isTrueFalse(str)
{
	/***************************************************************
	input : str
	purpose : To check if given str contains only true or false 
	output : true or false
	******************************************************************/
	if((str!="true")&&(str!="false"))
	{
		return false;
	}	
	else
	{
		return true;
	}
}

function isInArray(str,arr)
{
	/***************************************************************
	input : str
	purpose : To check if given string str is contained in the given array arr.
	output : true or false
	******************************************************************/
	var len =arr.length;
	for(var i =0;i<len;i++)
	{
		if (str==arr[i])
		{
			return true;
		}
	}
	return false;
}

function isDouble(str) {
	var objRegExp = /^\d\d*(\.\d\d*)?$/;
	return objRegExp.test(str);
}
