/*
*******************************************************************************
** Filename: /includes/validation.js
**
** File Description: Common java scripts validation function for MBAE
**
** Date: 27th August 2003
**
** Author: Venkata Subbarao Ponduri
**
** Company Name: The Boston Group
***************************************************************************************************************
** This software is the confidential and proprietary information of The Boston Group. ("Confidential Information").  You shall not disclose 
** such Confidential Information and shall use it only in accordance with the terms of the license agreement you entered into with TBG.
**************************************************************************************************************
*/
 
 //////////function for Message alert and and focus to control
  function msgAlert(Obj,msg)  { alert(msg);	Obj.select(); Obj.focus();	return false;  }
 
 ///////////function for validating email 
  function isEmail(Obj,msg) 
	{      
	        //chrReplace(Obj," ","")
			var str = Obj.value;
			var pass = 0;
			if(!fnCombination("-,_,.,@",str))	
				{ msgAlert(Obj,msg);return false;}			
			if (window.RegExp) {var tempStr = "a";var tempReg = new RegExp(tempStr);if (tempReg.test(tempStr)) pass = 1;}
			if (!pass) {return (str.indexOf(".") > 2) && (str.indexOf("@") > 3);}
			var RE1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
			var RE2 = new RegExp("^[a-zA-Z0-9\\.\\_\\-]*[a-zA-Z0-9]\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
			msg =  "* " + msg + "\n\nPlease enter a valid email address it should be in format \n                 yourname@domainname"
			if(! (!RE1.test(str) && RE2.test(str)))	{msgAlert(Obj,msg);	Obj.select();Obj.focus();	return false; }
			return true 
	}
	
	function fnCombination(strCombi,strVal)
	{
	var arr = strCombi.split(",")
	for(i=0;i<arr.length;i++)
	 for(j=0;j<arr.length;j++) 
	  if(strVal.indexOf(arr[i]+arr[j])>-1 || strVal.indexOf(arr[j])== 0)
			return false 
	return true		
	 } 
    
	function isNULL(Obj,msg)   { if (Obj.value=="" || Obj.value==" " ){ msgAlert(Obj,msg); return false ; }return true; }
	function chrReplace(Obj,Find,Replace) 	{ var str = Obj.value, Search = new RegExp(Find,"g");	 Obj.value = str.replace(Search,Replace); return true; }
	
	//////////////////trim
	function trimAll(Obj)
	{
	 for (i=0;i<Obj.elements.length;i++)
	    if (Obj.elements[i].type == "text" || Obj.elements[i].type == "textarea" )      
	            trim(Obj.elements[i])
	 return true;  
	}
	
	function trim(Obj)
	{
	if (Obj.value.charAt(0)==" " )
	  {
	    Obj.value = Obj.value.substring(1,Obj.value.length)
	    trim(Obj)
	  } 
	 else if (Obj.value.charAt(Obj.value.length-1)==" " )
	  {
	    Obj.value = Obj.value.substring(0,Obj.value.length-1)
	    
	  }  	    
	 return true 
	}
	
	/////////// Replace all ' and "  with html codes
	function replaceAll(Obj)
	{ 
	//	Obj=document.formName
	for (i=0;i<Obj.elements.length;i++)
	    if (Obj.elements[i].type == "text" || Obj.elements[i].type == "textarea" )      
	      {
	        	chrReplace(Obj.elements[i],"\"","&quot;")
	            chrReplace(Obj.elements[i],"'","&#39;")	
	            chrReplace(Obj.elements[i],"’","&#39;")
	           
	            trim(Obj.elements[i])
	      }	    
	  return true;  
	 }
