// Created By   --- Deepak B
// Created on   --- 06/01/2002
// Purpose      --- Some common Javascript validation functions used in all pages.
// 1. Function to check BLANK space, function returns TRUE if the argument passed is blank else returns FALSE

function fn_bol_isBlank(value)
{
	var i,length=value.length
	for(i=0;i<length;i++)
		if(value.charAt(i)!=" ") return false // Valid
	return true // Invalid
}  


/* 2. Function checks for the presence of special characters i.e, ~`!%^' which are not allowed 
		returns true if any of the symbols present else false  */
		
function fn_bol_isSymbols(value)
{

	if ((value.indexOf("~")>=0) || (value.indexOf(';')>=0) || (value.indexOf("!")>=0) || (value.indexOf("%")>=0) || (value.indexOf("-")>=0) || (value.indexOf("(")>=0) || 
		(value.indexOf("^")>=0) || (value.indexOf("#")>=0) || (value.indexOf(",")>=0) || (value.indexOf("<")>=0) || (value.indexOf(">")>=0) || (value.indexOf(")")>=0) || 
		(value.indexOf("/")>=0) || (value.indexOf("`")>=0) || (value.indexOf("@")>=0) || (value.indexOf("*")>=0) || (value.indexOf("?")>=0) || (value.indexOf("$")>=0) || 
		(value.indexOf("&")>=0) || (value.indexOf("|")>=0) || (value.indexOf("_")>=0))
		return true  // invalid
	else
		return false  // valid 
}

//Function for address
function fn_bol_isSymbolsAddress(value)
{

	if ((value.indexOf("~")>=0) || (value.indexOf(';')>=0) || (value.indexOf("!")>=0) || (value.indexOf("%")>=0) ||  
		(value.indexOf("^")>=0) || (value.indexOf("<")>=0) || (value.indexOf(">")>=0) || (value.indexOf("@")>=0) ||
		(value.indexOf("`")>=0) || (value.indexOf("*")>=0) ||(value.indexOf("_")>=0))
		return true  // invalid
	else
		return false  // valid 
}


// 3. Function returns true if the value is numeric else false

function fn_bol_isNumeric(value)
{
	
	if(isNaN(value)) // invaild
		return false
	else
	if (value.indexOf(".")>=0) 
		return false

	else
	if (value.indexOf("-")>=0) 
		return false

		
	else
		return true  // valid
}


/* 4. Function returns TRUE if valid E-Mail ID else return FALSE

	For validity Email ID must contain
		a. @ and . symbol
		b. @ or . must not be first character
		c. One @ symbol
		d. @ symbol and . must not be appear adjacent i.e., @.
		
																		*/
/*function fn_bol_isMail(value)
{
	var tmp_int_firstIndex = value.indexOf("@")    // Stores first index value of @ symbol it is used to check for presence of more then one @ symbol in given Email id.
	var tmp_int_lastIndex = value.lastIndexOf("@") // Stores first index value of @ symbol it is used to check for presence of more then one @ symbol in given Email id.
	var tmp_int_dotIndex = value.indexOf(".")        // Stores index of .
	
	if ((tmp_int_firstIndex > 0) && (tmp_int_dotIndex > 0) && (tmp_int_lastIndex == tmp_int_firstIndex) && (tmp_int_dotIndex !=  (tmp_int_firstIndex+1)) )
		return true  // valid
	else
		return false  // invalid 
}*/

function fn_bol_isMail(value)
{
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(value);
}

//6.  Function returns true if the value is Float or Integer else false

function fn_bol_isFloat(value)
{
	if (value.indexOf("-") >= 0)
	{	
		return false;
	}
	else if (value.indexOf(".") != -1)
	{
	
		if( (value.indexOf(".") == value.lastIndexOf(".")) )
			return true
		else
			return false
	}
	else
	{
	
		if(fn_bol_isNumeric(value))
			return true
		else
			return false
	}
}

//7.  Function returns true if the number of decimals more than 2 

function fn_bol_Precision(value)
{ 
   var tmp_len,tmp_index_len,tmp_val
   tmp_len = value.length
   tmp_index_len = (value.indexOf("."))+1
   tmp_val = tmp_len - tmp_index_len
  
   if ((tmp_val > 2) && (tmp_index_len > 0))
	{
		return true
	}
	else
	{
	 return false
	}
}
   


//8 Function to Select and Unselect all Checkboxes..
//It takes 3 parametes Select all Checkbox name, Indiv..Checbox name & status of the checkall checkbox.

function fn_bol_checkall(tmp_chkallname,tmp_chkindname,tmp_check_status)
  {
    var len,tmp_len
     len = document.forms[0].elements.length
     for(i=0;i<len;i++)
     {
      tmp_len = document.forms[0].elements[i]
       
      if (tmp_len.type == 'checkbox' && tmp_len.name != tmp_chkallname && tmp_len.name==tmp_chkindname && tmp_check_status == true)
      {
       document.forms[0].elements[i].checked = true
       
      } 
      if (tmp_len.type == 'checkbox' && tmp_len.name != tmp_chkallname && tmp_len.name==tmp_chkindname && tmp_check_status==false )
      {
       document.forms[0].elements[i].checked = false
       
      } 
     
     }
  }
  
 //9 Function to check all checkboxes onselect of all individual Checkboxes 
  
  function fn_indvcheckall(tmp_chkallname,tmp_chkindname)
  {
    var len,tmp_len,tmp_status
     len = document.forms[0].elements.length
     tmp_status = false
     
     for(i=0;i<len;i++)
     {
		tmp_len = document.forms[0].elements[i]
		 
		if (tmp_len.type == 'checkbox' && tmp_len.name!=tmp_chkallname && tmp_len.name==tmp_chkindname && document.forms[0].elements[i].checked == true)
		{
		 tmp_status = true
		 
		} 
		if (tmp_len.type == 'checkbox' && tmp_len.name!=tmp_chkallname && tmp_len.name==tmp_chkindname && document.forms[0].elements[i].checked == false)
		{
		 tmp_status = false
		 i = len
		 
		}
     }
       return tmp_status; 
    
  }
  
  
//---------------------

// 10. Date Validation Begin

// Function first converts given date format to "MDY" Format and then calls comparison method to compare
function fn_DateCompare(tmp_Date1, tmp_DtFrmt1, tmp_Date2, tmp_DtFrmt2, tmp_Oper)
{
	tmp_ResDate1 = fn_DateConvert(tmp_DtFrmt1, tmp_Date1)
	tmp_ResDate2 = fn_DateConvert(tmp_DtFrmt2, tmp_Date2)
	return(fn_DtCompare(tmp_ResDate1, tmp_ResDate2, tmp_Oper))
}

// Function converts single digit value into two(2) digits
// Eg,. 1 --> 01 


function fn_NumConvert(tmp_Value)
{
	var tmp_len
	tmp_len =  tmp_Value.length
	if (tmp_len  < 2)
		return("0" + tmp_Value)
	else
		return (tmp_Value)
}


// This function converts given date into 'YMD' Format 
/* 
	MDY --> "MM/DD/CCYY" 
	DMY --> "DD/MM/CCYY" 
	YDM --> "CCYY/DD/MM" 
	YMD --> "CCYY/MM/DD"
	
	tmp_FId		--> From date id
	tmp_TId		--> To date id
	tmp_date	--> Date 
*/

function fn_DateConvert(tmp_FId, tmp_date)
{
var tmp_Value, tmp_Result

tmp_Value = tmp_date.split("/")

switch(tmp_FId)
{
	case "MDY":				// "MM/DD/CCYY"  -->  "MM/DD/CCYY"
	{
		tmp_Result = "" + tmp_Value[2] + fn_NumConvert(tmp_Value[0]) + fn_NumConvert(tmp_Value[1])
		break
	}
	case "DMY":
	{
		tmp_Result = "" + tmp_Value[2] + fn_NumConvert(tmp_Value[1]) + fn_NumConvert(tmp_Value[0])
		break
	}
	case "YDM":
	{
		tmp_Result = "" + tmp_Value[0] + fn_NumConvert(tmp_Value[2]) + fn_NumConvert(tmp_Value[1])
		break
	}
	case "YMD":
	{
		tmp_Result = "" + tmp_Value[0] + fn_NumConvert(tmp_Value[1]) + fn_NumConvert(tmp_Value[2])
		break
	}
}
	return(tmp_Result)
}

// If (tmp_Date1 (tmp_Oper) tmp_Date2) then return True else False

function fn_DtCompare(tmp_Date1, tmp_Date2, tmp_Oper)
{
	switch (tmp_Oper)
	{
		case ">":
			{
			if (parseInt(tmp_Date1) > parseInt(tmp_Date2))
			return true
			break
			}
		case ">=":
			{
			if (parseInt(tmp_Date1) >= parseInt(tmp_Date2))
			return true
			break
			}
		case "<":
			{
			if (parseInt(tmp_Date1) < parseInt(tmp_Date2))
			return true
			break
			}
		case "<=":
			{
			if (parseInt(tmp_Date1) <= parseInt(tmp_Date2))
			return true
			break
			}
		case "=":
			{
			if (parseInt(tmp_Date1) == parseInt(tmp_Date2))
			return true
			break
			}
		case "!=":
			{
			if (parseInt(tmp_Date1) != parseInt(tmp_Date2))
			return true
			break
			}
	}
		return false
}  
  
function changeCase(strValue) 
{
	var index;
	var tmpStr;
	var tmpChar;
	var preString;
	var postString;
	var strlen;

	tmpStr = strValue.toLowerCase();
	strLen = tmpStr.length;
	if (strLen > 0)  
	{
		for (index = 0; index < strLen; index++)  
		{
			if (index == 0)  
			{
				tmpChar = tmpStr.substring(0,1).toUpperCase();
				postString = tmpStr.substring(1,strLen);
				tmpStr = tmpChar + postString;
			}
			else 
			{
				tmpChar = tmpStr.substring(index, index+1);
				if (((tmpChar == " ")|| (tmpChar =="\n")) && (index < (strLen-1)) ) 
				{
					tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
					preString = tmpStr.substring(0, index+1);
					postString = tmpStr.substring(index+2,strLen);
					tmpStr = preString + tmpChar + postString;
		        }
		    }
		}
	}
	return tmpStr;
}


		



