//$Id: validation.js,v 1.3.4.1 2010/02/09 11:12:07 sherly Exp $

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
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 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;
	}else
	{
     return true;
	 }
}

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

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
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 parseIntFromString(str)
{
	var temp = parseInt(str, 10);
	return temp;
}

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)) 
	{
		return false;
	}
	var objRegExp = /(^\d\d*$)/;
	return objRegExp.test(temp);
}

function isValidString(str) 
{
	/****************************************************************
	input   : str
	purpose : check if string is valid or not (string having only characters and numbers and no special characters)
	output  : returns true or false
	*****************************************************************/
	var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
	if(str.length < 1) 
	{
		return false;
	}
	if (str.charAt(0) == ' ' || str.charAt(str.length-1) == ' ')
	{
		return false;
	}
	if(str.search(mikExp) == -1) 
	{
		return true;
	}
	else 
	{
		return false;
	}
	return false;
}

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

	var errorMsg = ""; 
	// regular expression to match required time format  
	re = /^(\d{1,2}):(\d{2})$/;
	if(value != '') 
	{ 
		if(regs = value.match(re)) 
		{
			if(regs[2]) 
			{ // 12-hour time format with am/pm 

				if(regs[1] > 23) 
				{ 
					errorMsg = "Please enter a valid time format[00:00 to 23:59] " ; } 
			} 
			if(!errorMsg && regs[2] > 59) 
			{ 
				errorMsg = "Please enter a valid time format[00:00 to 23:59] "; 
			}
			
		} 
		else 
		{ 
			errorMsg = "Please enter a valid time format[00:00 to 23:59] " ; 
		} 
	} 
	if(errorMsg != "") 
	{
		alert(errorMsg); 
		return false; 
	}
	return true; 
}

function checkForTimeFormat(value)
{
	var errorMsg = ""; 
	// regular expression to match required time format  
	re = /^(\d{1,2}):(\d{1,2}):(\d{1,2})$/;
	if(!isEmpty(value)) 
	{ 
		if(regs = value.match(re)) 
		{
			if(regs[1] > 23) 
			{ 
					errorMsg = "Please enter a valid time format for hour[00 to 23] " ;  
			} 
			if(!errorMsg && regs[2] > 59) 
			{ 
				errorMsg = "Please enter a valid time format for minutes[00 to 59] "; 
			}
			if(!errorMsg && regs[3] > 59) 
			{ 
				errorMsg = "Please enter a valid time format for seconds[00 to 59] "; 
			}
			
		} 
		else 
		{ 
			errorMsg = "Please enter a valid time format[00:00:00 to 23:59:59] " ; 
		} 
	}
	else
	{ 
		errorMsg = "Start Time should not be left empty. " ; 	
	}

	if(errorMsg != "") 
	{
		alert(errorMsg); 
		return false; 
	}
	return true; 
	
}

function checkDateScheduler(value)
{
	var errorMsg = ""; 
	// regular expression to match required time format  
	re = /^(\d{4})-(\d{2})-(\d{2})$/;
	if(!isEmpty(value)) 
	{ 
		if(regs = value.match(re)) 
		{
			if(regs[1] > 9999) 
			{ 
					errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] " ;
			} 
			if(!errorMsg && regs[2] > 12) 
			{ 
				errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] "; 
			}
			if(!errorMsg && regs[3] > 31) 
			{ 
				errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] "; 
			}
			
		} 
		else 
		{ 
			errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] " ; 
		} 
	} 
	else
	{
		errorMsg = "Start Date should not be left empty. " ; 	
	}
	if(errorMsg != "") 
	{
		alert(errorMsg); 
		return false; 
	}
	return true; 
}


function checkDateReport(value)
{

// THIS FUNCTION IS DUPLICATED TO AVOID NATIVE ALERT MESSAGE FOR REPORTS FILTER PAGE- REMOVE ONCE ALL NATIVE ALERTS ARE REMOVED
	var errorMsg = ""; 
	// regular expression to match required time format  
	re = /^(\d{4})-(\d{2})-(\d{2})$/;
	if(!isEmpty(value)) 
	{ 
		if(regs = value.match(re)) 
		{
			if(regs[1] > 9999) 
			{ 
					errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] " ;
			} 
			if(!errorMsg && regs[2] > 12) 
			{ 
				errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] "; 
			}
			if(!errorMsg && regs[3] > 31) 
			{ 
				errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] "; 
			}
			
		} 
		else 
		{ 
			errorMsg = "Please enter a valid date format[0000-00-00 to 9999-12-31] " ; 
		} 
	} 
	if(errorMsg != "") 
	{
		showAlert(errorMsg); 
		return false; 
	}
	return true; 
	
}





