/*
#############################################################################
# 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 ) 
15. function doClick(buttonName, e)
16. function validateDates(obj_FDate, obj_TDate, frm_Default, to_Default)
17. function validateUsername(strUserName)
18. function validateCity(strCityName)
19. function validateState(strStateName)
20. function validateZipcode(strZipcode)

##################################### 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 #CCCCCC";
	}	
}

// 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));
}

//the purpose of this function is to allow the enter key to 
function doClick(buttonName, e)
{
	//point to the correct button to click.

	var key;

	 if(window.event)
	 {
		  key = window.event.keyCode;     //IE
	 }
	 else
	 {
		  key = e.which;     //firefox
	 }

	if (key == 13)
	{
		//Get the button the user wants to have clicked

		var btn = document.getElementById(buttonName);
		if (btn != null)
		{ //If we find the button click it

			btn.click();
			event.keyCode = 0
		}
	}
}

function validateDates(obj_FDate, obj_TDate, frm_Default, to_Default)
{
	if(obj_FDate.value != "" && obj_FDate.value != frm_Default && obj_TDate.value != "" && obj_TDate.value != to_Default)
	{
		var month = new Array();
		month['01'] = "January";
		month['02'] = "February";
		month['03'] = "March";
		month['04'] = "April";
		month['05'] = "May";
		month['06'] = "June";
		month['07'] = "July";
		month['08'] = "August";
		month['09'] = "September";
		month['10'] = "October";
		month['11'] = "November";
		month['12'] = "December";
		
		// -- get from date time stamp.
		arr_fd = obj_FDate.value.split("-");
		var strMonthfd = month[arr_fd[1]];
		var myDate = new Date(strMonthfd+' '+arr_fd[2]+', '+arr_fd[0]+' 00:00:00');
		var timestampFD = myDate.getTime()/1000.0;
		//alert(strMonthfd+' '+arr_fd[2]+', '+arr_fd[0]+' 00:00:00 ======== ' + timestampFD);
		
		// -- get to date time stamp.
		arr_td = obj_TDate.value.split("-");
		var strMonthtd = month[arr_td[1]];
		var myDate1 = new Date(strMonthtd+' '+arr_td[2]+', '+arr_td[0]+' 00:00:00');
		var timestampTD = myDate1.getTime()/1000.0;
		//alert(strMonthtd+' '+arr_td[2]+', '+arr_td[0]+' 00:00:00 ======== ' + timestampTD);
		
		if(timestampFD > timestampTD)
		{
			return false;
		}
	}
	return true;
}

function validateUsername(strUserName)
{
	return isValid(strUserName, lwr+upr+numb+" '"+'_-');		
}

function validateCity(strCityName)
{
	return isValid(strCityName, lwr+upr+" '"+'&-()');		
}

function validateState(strStateName)
{
	return isValid(strStateName, lwr+upr+" ',"+'&-()');		
}

function validateZipcode(strZipcode)
{
	if(strZipcode.length < 3)
	{
		alert("Zip code should be at least 3 characters long.");
		return false;
	}
	else if(!isValid(strZipcode, lwr+upr+numb))
	{
		alert("Zip code can only contain alphanumeric values.");
		return false;
	}
	else
	{
		return true;		
	}
}

function IsAlphaNumeric(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
	{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  
		  //SDS//alert(alphaa+' = '+hh);
		  
		  //32 means space
		  
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (hh == 32))
		  {
		  }
		  else
		  {
			 return false;
		  }
	}
 	return true;
}


function IsUsername(alphane)
{
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
	{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  
		  //alert(alphaa+' = '+hh);
		  
		  //95 means underscore means _
		  //45 means dash means -
		  
		  if((hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || (hh == 45) || (hh == 95))
		  {
		  }
		  else
		  {
			 return false;
		  }
	}
 	return true;
}

/* function used in manage page for no of records per page */
function apply_noof_rec(cnt_name, opt_value)
{
	
	var obj 	= document.getElementById(cnt_name);
	obj.value 	= opt_value;
		
	var sort 	= document.getElementById("hid_sort").value;
	var start 	= document.getElementById("hid_start").value;
	var fname 	= document.getElementById("hid_filename").value;
	var params 	= document.getElementById("hid_params").value;
	var records     = opt_value;
	//alert (sort+"\n"+start+"\n"+fname+"\n"+params+"\n"records);
	path		= fname+"?hid_sort="+sort+"&hid_start="+start+"&tot_rec_todisp="+records+params;
	location.href	= path;
}

function change_cursor(obj)
 {	 
 	obj.style.cursor='pointer';
  }
  
/* Added by pavan on 23-11-09 */  
//,page_name,x,y,leftx,lefty
function popUp_multiple(url,page_name,x,y,leftx,lefty) 
{
	//alert('Hi');
	//alert(url);
	var str		=	'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,titlebars=No,resizable=1,width='+x+',height='+y+',left='+leftx+',top='+lefty;
	sealWin		=	window.open(url,page_name,str);
	self.name 	=	"mainWin";
	//sealWin.moveTo(220,230);
}


