/*
#############################################################################
# eLuminous Technologies - Copyright (C)  http://eluminoustechnologies.com 
# This code is written by eLuminous Technologies, Its a sole property of 
eLuminous Technologies and cant be used / modified without license.  
Any changes/ alterations, illegal uses, unlawful distribution, copying is strictly
prohibhited #
#############################################################################

# Name: common_functions.js
# Usage: included ( WRITE USAGE DETAILS ) 
  {
   -- It is called from index.php & header.php in admin panel
  } 
# Update: created on 11-Dec-2006 by swati patel
# Status: production
# Purpose: Contains javascript validations functions for various files in admin panel.
 
#############################################################################
ALSO STRICTLY MAINTAINING THE LOGS OF CHANGES AND PERSON NAME 
#############################################################################

##################################### List of functions ##################################

1.  function isblank_text(txt)
2.  function emailcheck(str,idofstr)
3.  function isValid(parm,val)
4.  function isNum(parm)
5.  function isLower(parm)
6.  function isUpper(parm)
7.  function isAlpha(parm) 
8.  function isAlphanum(parm) 
9.  function isAlphaSpace(parm)
10. function show_err_border(cnt_id)
11. function show_reg_border(cnt_id)
12. function LTrim( value )
13. function RTrim( value )
14. function trim( value ) 

##################################### end of functions list ###############################
*/

//function to check if the value is not having only blanks
//if it is so then this funtion will return true else false
function isblank_text(txt)
{
	title1=new String(txt);
	len=title1.length;
	//alert(len);
	tot_space=0;
	for(i=0;i<len;i++)
	{
		if(title1.charAt(i)==' ')
		{
			tot_space++;
		}
	}
	//alert("tot_place:"+tot_space+"\nlen:"+len);
	if(tot_space==len)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//function to validate email id
function emailcheck(str,idofstr)
{
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (typeof idofstr == "undefined" || idofstr==null || idofstr=='')
	{
		var idofstr="E-mail ID";
	}
	if (str.indexOf(at)==-1)
	{
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
	    //alert("Invalid E-mail ID");
	    alert("Invalid "+idofstr+"!!");
	    return false;
	}
	if (str.indexOf(at,(lat+1))!=-1)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.indexOf(dot,(lat+2))==-1)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	if (str.indexOf(" ")!=-1)
	{
	   //alert("Invalid E-mail ID");
	   alert("Invalid "+idofstr+"!!");
	   return false;
	}
	 
	return true					
}

var numb = '0123456789.';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

//general function to check whether the given parm is valid according to the given val
//val can be any of the single or combination of the above three variables
//if there is any need to validate for special characters than one var can be added and allowed chars can be stored
function isValid(parm,val)
{
  if (parm == "")
  {
  	 return true;
  }
  for (i=0; i<parm.length; i++) 
  {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}
 
//function to check numeric values
function isNum(parm)
{
	return isValid(parm,numb);
}

//function to check for lower case
function isLower(parm)
{
	return isValid(parm,lwr);
}

//function to check for upper case
function isUpper(parm)
{
	return isValid(parm,upr);
}

//function to check for aplhabetic value
function isAlpha(parm) 
{
	return isValid(parm,lwr+upr);
}

//function to check for aplhanumeric value
function isAlphanum(parm) 
{
	return isValid(parm,lwr+upr+numb);
} 
function isAlphaSpace(parm) 
{
	return isValid(parm,lwr+' '+upr);
} 

//function to change the border color to red
function show_err_border(cnt_id)
{
	if(document.getElementById(cnt_id))
	{
		document.getElementById(cnt_id).style.border="1px solid #FF0000";	
	}
}

function show_reg_border(cnt_id)
{
	if(document.getElementById(cnt_id))
	{
		document.getElementById(cnt_id).style.border="1px solid #999999";
	}	
}

// Removes leading whitespaces
function LTrim( value )
{
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value )
{
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) 
{
	return LTrim(RTrim(value));
}

function is_checkboxes_checked(obj)
 {
 	var obj_len = parseInt(obj.length);
	
	if(! isNaN(obj_len))// -- has some lenght
	{
		var uncheck_cnt = obj_len;
		
		for(var i=0; i < obj_len; i++)
		{
			if(obj[i].checked == false)
				--uncheck_cnt;
		}//-- for.
		
		if(uncheck_cnt == '0')
			return false; 
		
	}
	else //-- to check if their is only one checkbox.
	if(obj.checked == false)
			return false
	
	return true;
 }
 
 function is_radio_selected_value(obj)
 {
 	var obj_len = parseInt(obj.length);
	//alert(obj_len);
	if(! isNaN(obj_len))// -- has some lenght
	{
		for(var i=0; i < obj_len; i++)
		{
			if(obj[i].checked == true)
				return obj[i].value
		}//-- for.
	}
	else //-- to check if their is only one checkbox.
	if(obj.checked == true)
			return obj.value;
	
	return false;
 }
 
 function uncheckRadioIfChecked(obj)
 {
 	var obj_len = parseInt(obj.length);
	//alert(obj_len);
	
	if(! isNaN(obj_len))// -- has some lenght
	{
		//var uncheck_cnt = obj_len;
		
		for(var i=0; i < obj_len; i++)
		{
			//alert(obj[i].checked);
			if(obj[i].checked == true)
				obj[i].checked=false;
		}//-- for.
		
	}
	else //-- to check if their is only one checkbox.
	if(obj.checked == true)
			obj.checked=false;
			
			
	
	return true;
 }
 
 function is_radio_checked(obj)
 {
	if(!obj) return false;
	
 	var obj_len = parseInt(obj.length);
	
	if(! isNaN(obj_len))// -- has some lenght
	{
		var uncheck_cnt = obj_len;
		
		for(var i=0; i < obj_len; i++)
		{
			if(obj[i].checked == false)
				--uncheck_cnt;
		}//-- for.
		
		if(uncheck_cnt == '0')
			return false; 
		
	}
	else //-- to check if their is only one checkbox.
	if(obj.checked == false)
			return false
	
	return true;
 }