/*
author="Vishal R.Mistry" create_date="2-NOV-2001"
This file includes all the functions which are often used,  as mentioned below 

the following is the description of the functions created in javascript 

takes text obj name as a param and msg to display, check for the Currency value --added by Latha on 02-Jun-2004
function isCurrency(obj,msg) 

takes text obj name as a param and msg to display, check for the Alphabets value --added by Latha on 02-Jun-2004
function isAlphabets(obj,msg) 

takes text obj name as a param and msg to display, check for the Positive Integer value --added by Latha on 05-Apr-2004
function isPosInteger(obj,msg) 

takes text obj name as a param and msg to display, check for the Integer value --added by Latha on 05-Apr-2004
function isInteger(obj,msg) 

takes text obj name as a param and msg to display, check for the minutes>=60 --added by Latha on 05-Apr-2004
function minsCheck(obj,msg) 

takes text obj name as a param and msg to display, check for the numberic value 
function isnumberc(obj,msg) 

takes text obj name as a param and msg to display, check for the non-numberic value
function isnotnumberc(obj,msg)

takes text obj name as a param and msg to display, check for the blank value
function isblank(obj,msg)

takes listbox object name as a param and error msg to display, checks for the value = 0 means nothing is selected
function isselected(obj,msg)

checks for the checkbox checked or not , returns false when not checked 
function ischecked(obj,msg)

date function 
function isdate(dd,mm,yyyy)

takes obj name and num as para 
function putcomma(obj,num){

function replacesubstring( originalString , searchForString , replaceWithString )

takes 3 parameters the 3rd is checks for the special character , words param can be true/false , if true means there can be many words in a field and space is allowed
function chkspchar(obj,msg,words)

*/
//This function checks for Currency
//added by Latha on 02-Jun-2004
function isCurrency(obj,msg) 
{
var o = eval("document.form1." + obj)
val = o.value
val =  val.replace(/\s+/g,'')
if (val!="")
{
	if (val.length!=3)
	{
		alert(msg);
		o.focus();
		o.select();	
		flag = false;
	}
	if (flag)
	{
		for (i=0;i<val.length;i++)
		{
			keycode = val.charCodeAt(i);
			if (((keycode<65) || (keycode>90)) && ((keycode<97) || (keycode>122)))
			{
				alert(msg);
				o.focus();
				o.select();	
				flag = false;
				i=val.length
			}
		}
	}
	
}
else
	flag = true;

}


// takes text obj name as a param and msg to display and maxlenth to check, check for value is morethan max specified
function chkLength(obj,msg,maxlen)
{
		var o = eval("document.form1." + obj)
		val = o.value

//		val =  val.replace(/\s+/g,'')
		if( val.length > maxlen ) 	
		{
			alert(msg)
			o.focus()
			o.select()	
			flag = false
		}
		else
		{
			flag = true 
		}

}


//This function checks for Alphabets
//added by Latha on 02-Jun-2004
function isAlphabets(obj,msg) 
{
var o = eval("document.form1." + obj)
val = o.value
val =  val.replace(/\s+/g,'')
if (val!="")
{
	for (i=0;i<val.length;i++)
	{
		keycode = val.charCodeAt(i);
		if (((keycode<65) || (keycode>90)) && ((keycode<97) || (keycode>122)))
		{
			alert(msg);
			o.focus();
			o.select();	
			flag = false;
			i=val.length
		}
	}
}
else
	flag = true;

}

//this checks for Positive integer.
//added by Latha on 05-Apr-2004
function isPosInteger(obj,msg)
{
var o = eval("document.form1." + obj)
val = o.value
val =  val.replace(/\s+/g,'')						//trims the spaces  
if (val!="")
{
	if (!(val.match(/^\d+$/)?true:false))
	{
		alert(msg);
		o.focus();
		o.select();	
		flag = false;
	}
}
else
	flag = true	
}
//this checks for integer.
//added by Latha on 05-Apr-2004
function isInteger(obj,msg)
{
var o = eval("document.form1." + obj)
val = o.value
val =  val.replace(/\s+/g,'')						//trims the spaces  
if (val!="")
{
	if (!(val.match(/^-?\d+$/)?true:false))
	{
		alert(msg)
		o.focus()
		o.select()	
		flag = false
		
	}
}
	else
		flag = true	
}
//this checks for minutes >=60.
//added by Latha on 05-Apr-2004
function minsCheck(obj,msg)
{
	var o = eval("document.form1." + obj)
	if (o.value>=60) //checking for mins>60
	{
		alert(msg);
		o.focus();
		o.select();
		flag=false;
	}
else
	flag = true;
}
//takes text obj name as a param and msg to display, check for the numberic value
//done by Latha on 07-Jun-2004
function isnumberc(obj,msg)
{
var o = eval("document.form1." + obj)
val = o.value
//val =  val.replace(/\s+/g,'')						//trims the spaces  
val = Trims(val)									//trims the spaces
if (val!="")
{
	if((isNaN(val)) || (val.length < 1))	
	{
		alert(msg);
		o.focus();
		o.select();	
		flag = false;
	}	

}	
else
		flag = true	

}
//takes text obj name as a param and msg to display, check for the numberic value
function isnumberc1(obj,msg)
{
var o = eval("document.form1." + obj)
val = o.value
val =  val.replace(/\s+/g,'')						//trims the spaces  
if (val!="")
{
	if((isNaN(val)) || (val.length < 1))	
	{
		alert(msg);
		o.focus();
		o.select();	
		flag = false;
	}	

}	
else
		flag = true	

}

//takes text obj name as a param and msg to display, check for the less than value
function isless(obj,val,msg)
{
	var o = eval("document.form1." + obj)
	if(o.value < val ) 
	{
		alert(msg)
		o.focus()
		o.select()	
		flag = false
	}
	else
	{
		flag = true	
	}
}

//takes text obj name as a param and msg to display, check for the non-numberic value
function isnotnumberc(obj,msg)
{
	var o = eval("document.form1." + obj)
	if(!isNaN(o.value)) 
	{
		alert(msg)
		o.focus()
		o.select()	
		flag = false
	}
	else
	{
		flag = true	
	}
}

// takes text obj name as a param and msg to display, check for the blank value
function isblank(obj,msg)
{

		var o = eval("document.form1." + obj)
		val = o.value

		val =  val.replace(/\s+/g,'')
		if((val == "") || (val.length < 1)) 	
		{
			alert(msg)
			o.focus()
			o.select()	
			flag = false
			
		}
		else
		{
			flag = true 
		}
}

// this takes listbox object name as a param and error msg to display , returns false if the value is 0 or null
//value = 0 means nothing is selected
function isselected(obj,msg)
{
	var o = eval("document.form1." + obj)
	if((o.options[o.selectedIndex].value == "0") 	|| (o.options[o.selectedIndex].value == ""))
	{
		alert(msg)
		o.focus()
		flag = false
	}
	else
		flag = true	
}

//takes checkbox object name as param and error message as msg to display, returns false when not checked 
function ischecked(obj,msg)
{
	var o = eval("document.form1." + obj)
	if(o.checked=="false")
	{
		alert(msg)
		o.focus()
		flag = false
	}
	else
		flag = true	
}

//date function 
function isdate(dd,mm,yyyy) {
	var dat=dd;
	var mon=mm;
	var yer=yyyy;
	
	if (dat == 0 || mon == 0 || yer == 0) 
	{	return false; }
	else
	{		
	switch (mon) {
		case '2':
			if ((yer%4==0) && (dat > 29)) 
			{   return false; }

			else if ((yer%4>0) && (dat > 28)) 
			{   return false; }
			else
			{   return true; }

			break;
		case '4':
		case '6':
		case '9':
		case '11':
			if (dat>30) 
			   return false; 
			else
				return true;

			break;
		default :
			return true;
	}
	}	
} 

function checkdate(dat,mon,yer)
{

      if (dat==0 && mon==0 && yer==0) {return false;}

	if ((dat == 0 | mon == 0 | yer == 0) & (dat != 0 | mon != 0 | yer != 0) )
			{return false;}

	switch (mon) {
		case '2':
			if ((yer%4==0) && (dat > 29)) 
				{return false;}

			else if ((yer%4>0) && (dat > 28)) 
				{return false;}
			
		case '4':
		case '6':
		case '9':
		case '11':
			if (dat>30) 
				{return false;}

			break;
		
	}
return true;

}


//takes obj name and num as para and format the numeric data with commas e.g. 100,000
function putcomma(obj,num){
var tmpNumStr = replacesubstring(num,",","")

if(!isNaN(tmpNumStr))
{
	if ((num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}
}

	x = eval("document.form1." + obj )
	x.value = tmpNumStr
}

function replacesubstring(originalString , searchForString , replaceWithString ){
var reg = /,/
	if(reg.test(originalString))
	{
		var objRegExp = eval( "/" + searchForString + "/g" );
		return (originalString.replace( objRegExp , replaceWithString ) );
	}
	else
		return  originalString
}


//function with 3 parameters, checks for the special character, words param can be true/false , if true means there can be many words in a field and space is allowed
function chkspchar(obj,msg,words)
{
flag = true 
var obj = eval("document.form1." + obj)
var objval =  obj.value
	for(i=0;i<objval.length;i++)
	{					
	  if(words=="false")		//also checks for the space char
	  {		
		keycode=objval.charCodeAt(i)
		if (((keycode<65) || (keycode>90)) && ((keycode<97) || (keycode>122)) && ((keycode<48) || (keycode>57)) )   
		{
			alert(msg)
			obj.focus()
			obj.select()
			flag = false
			i = objval.length
		}
	  }
	  else 				//words ="true"	 ignores for the space char
	  {	
		keycode=objval.charCodeAt(i);
		if (((keycode<65) || (keycode>90)) && ((keycode<97) || (keycode>122)) && ((keycode<48) || (keycode>57)) && (keycode !=32))   
		{
			alert(msg)
			obj.focus()
			obj.select()
			flag =  false
			i = objval.length
		}
	  }				 
	}
}


//this function cheks for the special character (not currently used)
function chkspcharx(obj,msg)
{
var obj = eval("document.form1." + obj)
var objval =  obj.value
	for(i=0;i<objval.length;i++)
	{					
		keycode=objval.charCodeAt(i);
		if (((keycode<65) || (keycode>90)) && ((keycode<97) || (keycode>122)) && ((keycode<48) || (keycode>57)) )   
		{
			alert(msg);
			obj.focus();
			obj.select();
			return false;
		} 
	}
			return true;	
}

//this funtion is used to set the taax to the statue bar. 
function showstatus()
{
	window.status = "ICICI Securities"
}

if (document.layers) window.captureEvents(Event.MOUSEDOWN);
if (document.layers) window.captureEvents(Event.MOUSEUP);

//on mouse click event calling the function showstatus
document.onmousemove=showstatus;
document.onmouseover=showstatus;

// function to check email validation

function check_email(thisField)
{
	
	if (thisField.value.indexOf('@') == -1 ) 
	{
		alert('Please enter valid e-mail ID');
		thisField.focus();
		thisField.select();
		flag=false;
	}	
	else if(thisField.value.substring(thisField.value.indexOf('@')+2).indexOf('@') >= 0||
		thisField.value.substring(thisField.value.indexOf('@')+2).indexOf('.') < 0 ) 
	{
			alert('Please enter valid e-mail ID');
			thisField.focus();
			thisField.select();			
			flag=false;
	}
	else if(thisField.value.charAt(thisField.value.length - 1) == '.') 
	{
			alert('Please enter valid e-mail ID');
			thisField.focus();
			thisField.select();			
			flag=false;
	}
	else
		flag=true;
}


function chk_alpha_numeric(obj,msg)
{
	
	var obj = eval("document.form1." + obj)
	var objval =  obj.value

	var has_chars = false
	var has_nos = false

	for(i=0;i<objval.length;i++)
	{					
		keycode=objval.charCodeAt(i);
		if (((keycode>=65) && (keycode<=90)) || ((keycode>=97) && (keycode<=122))  )   
			{
				has_chars = true
			} 
	}

	for(i=0;i<objval.length;i++)
		{
			keycode=objval.charCodeAt(i);
			if ( (keycode>=48) && (keycode<=57) )
				{
					has_nos = true
				}
		}

	if (has_chars == true && has_nos == true)
		{
			flag = true
		}	
	else
		{
			alert(msg);
			obj.focus();
			obj.select();
			flag = false
		}			   

}

// takes text obj name as a param , check for the blank value like isblank function but this returns true or false  
function isnull(obj)
{
	var o = eval("document.form1." + obj)
	val = o.value

	val =  val.replace(/\s+/g,'')

	if((val == "") || (val.length < 1)) 	
	{
			return true	
	}
		else
	{
			return false 
	}
}

/*/Check units and digits */
function checkUnits(txtobj, units)
{
	var units = eval("document.form1." + units)
	unitsval = units.options[units.selectedIndex].value

	//alert(unitsval)

	var txtobj = eval("document.form1." + txtobj)
	txtval = txtobj.value

	//alert(txtval)

	if(unitsval == "M")					//Millions
		maxdigits = "1" 
	else if (unitsval == "B")					//Billions
		maxdigits = "2" 

	//if no decimals are entered then concat decimals and zeros
	if(txtval.indexOf(".") == -1)	{
		txtval = txtval + "."	
		//alert("no decimals" + txtval)
	}

	//if decimal is there and after that no digit are entered then concat only zeros
	if (txtval.indexOf(".") + 1 == txtval.length)
	{
		 for(i=1;i<=maxdigits;i++) {
			txtval = txtval + "0"	
		}
		//alert("decimals without digits" + txtval)
	}

	//alert("index" + txtval.indexOf("."))
	var strlen = parseInt(txtval.indexOf(".")) + 1 + parseInt(maxdigits)
	//alert("txt val + max " + strlen)
	//alert("len = " + txtval.length)
	if (parseInt(txtval.indexOf(".")) + 1 + parseInt(maxdigits) != txtval.length) {
		alert("Please type in one digit after decimal in case of Units = \'Millions\' and \n two digits after decimal incase of Units = \'Billions\'")	
		txtobj.focus()	//	these two comments added by amudhan.e. on 7/12/2002.
		txtobj.select()
		flag = false
	}else{
	txtobj.value = txtval					//reset the value of the txt field with the decimal and zeros
	flag = true
	}
}

function checkEmail(email) 
{

if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
{
return (true)
}
alert("Invalid E-mail Address! Please re-enter.")
return (false)
}


function dateCharToNumber(obj)
{
	if ( obj != '' )
	{
		switch ( obj )
		 {
			case 'JAN':
				{
			    	return('1');
			    	break;
				}
			case 'FEB':
				{
			    	return('2');
			    	break;
				}
			case 'MAR':
				{
			    	return('3');
			    	break;
				}
			case 'APR':
				{
			    	return('4');
			    	break;
				}
			case 'MAY':
				{
			    	return('5');
			    	break;
				}
			case 'JUN':
				{
			    	return('6');
			    	break;
				}
			case 'JUL':
				{
			    	return('7');
			    	break;
				}
			case 'AUG':
				{
			    	return('8');
			    	break;
				}
	 		case 'SEP':
				{
			    	return('9');
			    	break;
				}
			case 'OCT':
				{
			    	return('10');
			    	break;
				}
			case 'NOV':
				{
			    	return('11');
			    	break;
				}
			case 'DEC':
				{
			    	return('12');
			    	break;
				}
			default:
			   return('0');
 		 }
	}
	else
		return(0);
}


function Trims(val)
{
	var str  = new String(val);
	var newStr = str;
	if (val=="") return val; 
	for (itrim=0;itrim<str.length;itrim++)
	          if(str.charCodeAt(itrim)==32)
	                   newStr = str.substring(itrim+1,str.length)
	          else
	                   break;
	str = newStr;
	for (itrim=str.length-1;itrim>=0;itrim--)
	          if(str.charCodeAt(itrim)==32)
	                   newStr = str.substring(0,itrim)
	          else
	                   break;

	return newStr;
}

//this function to add list to another list
//it accepts two param objects, from list object and to list object
	function listaddnew(stat,selstat)
	{
		for(i=0;i<stat.length;i++)
			{
				if(stat[i].selected==true)
				  {
					sFlag = true
					for(j=0;j<selstat.length;j++)
						{
							if(stat[i].value==selstat[j].value)
								sFlag = false
						}
					if(sFlag)
						selstat[selstat.length] = new Option(stat[i].text,stat[i].value)
				   }

			}

	}


//this function to add all the values from a list to another list
//it accepts two param objects, from list object and to list object

	function listaddall(stat,selstat)
	{
		for(i=0;i<stat.length;i++)
			{
	//			if(stat[i].selected==true)
	//			  {
					sFlag = true
					for(j=0;j<selstat.length;j++)
						{
							if(stat[i].value==selstat[j].value)
								sFlag = false
						}
					if(sFlag)
						selstat[selstat.length] = new Option(stat[i].text,stat[i].value)
	//			   }
			}
	}



//this function to delete list values which is selected 
//it accepts one object i.e from which object we have to delete the selected one

	function listdel(selstat)
	{
		//selstat = document.orderform.selstationery_id
		for(i=selstat.length - 1 ;i>=0;i--)
			{
				if(selstat[i].selected)
					selstat[i] = null
			}
	}

//this function to delete all values from a list
//it accepts one object i.e from which object we have to delete the selected one

	function listdelall(selstat)
	{
		//selstat = document.orderform.selstationery_id
		for(i=selstat.length - 1 ;i>=0;i--)
			{
//				if(selstat[i].selected)
					selstat[i] = null
			}
	}


/*/Check digits / decimal */
//object, message and maxdigits
function checkDecimal(txtobj,msg,maxdigitcnt)
{
	//alert(unitsval)
	var txtobj = eval("document.form1." + txtobj)
	txtval = txtobj.value

	//alert(txtval)
	maxdigits = maxdigitcnt 

	//if no decimals are entered then concat decimals and zeros
	if(txtval.indexOf(".") == -1)	{
		txtval = txtval + "."	
		//alert("no decimals" + txtval)
	}

	//if decimal is there and after that no digit are entered then concat only zeros
//	alert(txtval.substring(txtval.indexOf(".") + 1,txtval.length).length)
	var curDigits;
	curDigits = parseInt(txtval.substring(txtval.indexOf("."),txtval.length).length)
//	if (txtval.indexOf(".") + 1 == txtval.length)
//	{
		 for(i=curDigits;i<=maxdigits;i++) {
			txtval = txtval + "0"	
		}
		//alert("decimals without digits" + txtval)
//	}

	//alert("index" + txtval.indexOf("."))
	var strlen = parseInt(txtval.indexOf(".")) + 1 + parseInt(maxdigits)
	//alert("txt val + max " + strlen)
	//alert("len = " + txtval.length)
	if (parseInt(txtval.indexOf(".")) + 1 + parseInt(maxdigits) != txtval.length) {
//		alert("Please type in two digits after decimal");	
		alert(msg);
		txtobj.focus()	//	these two comments added by amudhan.e. on 7/12/2002.
		txtobj.select()
		flag = false
	}else{
	txtobj.value = txtval					//reset the value of the txt field with the decimal and zeros
	flag = true
	}
}

//takes text obj name as a param and msg to display, check for the numberic value [zero is positive]
function isPosNumber(obj,msg)
{
var o = eval("document.form1." + obj)
val = o.value
val =  val.replace(/\s+/g,'')						//trims the spaces  
if (val!="")
{
	if (val < 0 )
	{
		alert(msg);
		o.focus();
		o.select();	
		flag = false;
	}
}
else
	flag = true	
}
