// JavaScript Document

//Below declaration is not used anywhere now.
//var arrLegendAll = {
//					'W' : 'W: Working Day',
//					'WW' : 'WW: Weekend work',
//					'H' : 'H: Holiday',
//					'C' : 'C: Compensatory day',
//					'S' : 'S: Sick leave',
//					'V' : 'V: Vacation day',
//					'L' : 'L: Leave without pay'
//				   };

/**
* Get object from any field.
*/
function ele(id)
{
	return document.getElementById(id);	
}

/**
ALERT BOX
*/
function a(str)
{
	return alert(str);	
}

// -------- alert blank links ---------------- //
function fun()
{
  alert("This link is under construction !");	   
}

/**
* Showing Link is under construction
*/
function showUnderConstruction()
{
  alert("This link is under construction !");	   
} 
  
//To Get Key Code of any key pressed.
function getKeyCode(e)
{
	// 'e' will be 'event', whatever event has been fired (here: 'keypress').
	//alert(e.charCode + '' + e.keyCode);
		 
	 if(navigator.appName=="Microsoft Internet Explorer")
	 {
	 	key = e.keyCode;
	 }
	 else
	 {
		 //alert("which = "+e.which);
	     key = e.which;
		 //keyUnicode = e.charCode;
		//key = String.fromCharCode(keyUnicode); //To Get the Actuakl Key name pressed.
		//keyUnicode is the 'Unicode value' for mozilla of the character key that was pressed.
		//alert(event.charCode);
	 }
	 return key;	
}

//Only entering Numbers in text box. 
//@Usage : onkeypress="return onlyNum(event);"
function onlyNum(e)
{
     var key;
	 
	 //Getting Key character code.
	 key = getKeyCode(e); //'e' is EVENT here.
	 
	 //alert(key);
	 if( key==13 || key==22 || key==127 || key==0 || key==8)  // for 'ENTER' key etc..
	 {
		  return true;
		  
		  //NOTE: Here in above case, key=8 for BACKSPACE character in Mozilla firefox(SEE).
		          // Also, key=22(Delete) (and  key=127(Backspace) in IE)
				  // key=0 for arrow keys.
	 }
	 if(key<48 || key>57)
	 {
        window.status=" Field accepts numbers only !";
		return false;		 
	 }	 
	 else
	 {
        
		window.status="";
		//return true;
	 }
		
}

/***
set focus on keyup of any phone text box automatically when numbers are entered 
eg. When Phone number consist of Country Code, Area code, Phone number means 3 text boxes.
***/
function setFocus(thisTextboxId, prevTextboxId, nextTextboxId, limitNum)
{
	if( document.getElementById(thisTextboxId).value.length >= limitNum )
	{
		//alert(document.getElementById(nextTextboxId));
		if( nextTextboxId != '' && document.getElementById(nextTextboxId) )
			document.getElementById(nextTextboxId).focus();
	}
	else if( document.getElementById(thisTextboxId).value.length <= 0)
	{
		if( prevTextboxId != '' && document.getElementById(prevTextboxId) )
			document.getElementById(prevTextboxId).focus();
	}
}

 //FUNCTION TO CHECK Date Field (Made by Saurabh).
function dateFormat(datefield, e) // onKeyPress..phone_no => Name of the field.
{
     var key, len;
	// 'e' will be 'event', whatever event has been fired (here: 'keypress').
	//alert(e.charCode + '' + e.keyCode);
		 
	 if(navigator.appName=="Microsoft Internet Explorer")
	 {
	 	key = e.keyCode;
	 }
	 else
	 {
	    key = e.charCode;
		//alert(event.charCode);
	 }
	//alert(key);
	 len = document.getElementById(datefield).value.length;
	 
//	 if(len==2 || len==5)
//	 {
//	   document.getElementById(datefield).value=document.getElementById(datefield).value+'/';
//	   return true;
//	 }
	 
	 if( key==13 || key==22 || key==127 || key==0)  // for 'ENTER' key..
	 {
		  return true;
		  
		  //NOTE: Here in above case, key=0 means charCode=0 for BACKSPACE character in Mozilla firefox(SEE).
		          // Also, key=22(Delete) and  key=127(Backspace)  
	 }
	 else if(len==2 || len==5)
	 {
	   document.getElementById(datefield).value=document.getElementById(datefield).value+'/';
	   return true;
	 }
	 if(key<48 || key>90)
	 {
	   window.status=" Field accepts numbers only !";
       return false;		 
	 }
	 else window.status="";
	 
	 if(len>=10)
	    return false;
}



function checkMail(e_mail)
{
	var atPos=e_mail.indexOf("@",0);
    var dotPos=e_mail.indexOf(".",0);
    var len=e_mail.length;
	if(e_mail!="" && (atPos<1 || dotPos==-1 || e_mail.substring(atPos+1, dotPos)=="" || e_mail.substring(dotPos+1, len).length<2))
	return false
	else 
	return true;
}

/* To check a Domain Name */
function checkDomain(str)
{
	var atPos=str.indexOf("@",0);
    var dotPos=str.indexOf(".",0);
    var len=str.length;
	var isError = 0;
	/***
	Checking for Special characters
	***/
	var iChars = "!@#$%^&*()+=-[]\\\/;,{}|\":<>?";
	for(i=0;i<iChars.length;i++)
	{
		if(str.indexOf(iChars.charAt(i))!=-1)
		{
			isError = 1;
			break;
			//return false;
		}
	}
	if(i==iChars.length)
		isError = 0;//return true;
	/*** END checking for Special characters ***/	
	
	if(str!="" && (dotPos==-1 || str.substring(dotPos+1, len).length<2))
		return false;
	else if( isError == 1 ) //Special character found.
		return false;
	else 
		return true;
}

function checkAll(chkAllId, isMultipleSelect, selectedColor)
{
	//When arguments[1], [2] i.e. second and third arguments will not be passed.
	//For setting default parameters in Javaacript (Note: we may not set default parameters directly into javascript).
	//'isMultipleSelect' will be passed here for checking whether it is a Multiple Select.
	if( typeof isMultipleSelect	== 'undefined' )
		isMultipleSelect = false;
		
	if( typeof selectedColor == 'undefined' )
		selectedColor = '#0a246a';
		
    //label_node = checkbox_node.parentNode;

		//label_node.style.backgroundColor = selectedColor;    

	//alert(document.getElementById('frm').length);

	if(document.getElementById(chkAllId).checked==true)	
	{
		for(var i=0; i<document.frm.length; i++)
		{
            if(document.frm.elements[i].type=='checkbox' && document.frm.elements[i].disabled == false)
			{
			    document.frm.elements[i].checked = true;
    			//'isMultipleSelect' will be passed here for checking whether it is a Multiple Select.
				if( isMultipleSelect != false )
				{
					label_node = document.frm.elements[i].parentNode;
					label_node.style.backgroundColor = selectedColor;
					label_node.style.color='#fff';
				}
			}
		}
		
	}
	else if(document.getElementById(chkAllId).checked==false)	
	{
		for(var i=0; i<document.frm.length; i++)
		{
            if(document.frm.elements[i].type=='checkbox' && document.frm.elements[i].disabled == false)
			{
			    document.frm.elements[i].checked = false;
				//'isMultipleSelect' will be passed here for checking whether it is a Multiple Select.
				if( isMultipleSelect != false )
				{
					label_node = document.frm.elements[i].parentNode;
					label_node.style.backgroundColor = '#fff'; 
					label_node.style.color='#000';
					
				}			  
			}
		}
		
	}
}


//if any of the checkbox hasn't been selected ( called onsubmit of button).
 function confirmDel(mesg) // if no records are checked(in delete) 
 {
	   var flag=false;
	   
		 for(var i=0; i<document.frm.length; i++) //check for all checkboxes
		 {
		   if( document.frm.elements[i].type=="checkbox" && document.frm.elements[i].checked==true )
		   {  				 					
				flag=true;
				break;	
			}
			
		 }  
		 
		 if( flag==false )  // if no checkbox selected
		 {
			   alert("Select any of the checkboxes !");
			   return false;
		 }  
		 else
		 {					 
			   return confirm(mesg);	 
		 }
}

//---- To check Floating Point Numbers ----//
function checkFloat(fieldId)
{
    var fieldVal = document.getElementById(fieldId).value;
	var fieldLen = document.getElementById(fieldId).value.length;
	//alert(fieldLen);
	
	if(fieldVal!='')
	{
         //var key = window.event.keyCode;

		 //var RE = new RegExp("[-+]?([0-9]*[\.][0-9]+|[0-9]+)");
		 var RE = /^([-+]?[0-9]*\.?[0-9]+)$/;
		
		 var arrFieldVal = fieldVal.split('');
		 
		 		
		 if(RE.test(fieldVal)==false)
		 {
		     //Finding ans Replacing Last Character.
			 var replaceStr = arrFieldVal[fieldLen-1];
			 if(replaceStr != '.')
			 {
				 var str = fieldVal.replace(replaceStr, '');
				 document.getElementById(fieldId).value = str;
			 }
			 
			 window.status = "Invalid format !";
			 return false;
		 }
		 else
		 {			 
			 window.status = "";
			 return true;
		 }
		 
	}
}

//FUNCTION TO CHECK Input Field FOR SPECIAL CHARS
function checkValidEntry(txtValue)
{
	var iChars = "!@#$%^&*()+=-[]\\\;,.{}|\":<>?";
	for(i=0;i<iChars.length;i++)
	{
		if(txtValue.indexOf(iChars.charAt(i))!=-1)
			return false;
	}
	if(i==iChars.length)
		return true;
}

//------- showing and hiding (display) ------//
function displayShowHide(showIds, hideIds)
{
  // alert(showIds + ' - ' + hideIds);
   
   var arrShowIds = showIds.split("|");
   var arrHideIds = hideIds.split("|");
   
   if(arrShowIds.length>0)
   {
	   for(var i=0; i<arrShowIds.length; i++)
	   {
		  if(arrShowIds[i])
			document.getElementById(arrShowIds[i]).style.display = "";
	   }
   }
   
   if(arrHideIds.length)
   {
	   for(var j=0; j<arrHideIds.length; j++)
	   {
		  if(arrHideIds[j])
			document.getElementById(arrHideIds[j]).style.display = "none";
	   }
   }
}

//-------------------------------------//

/**
@ Date Functions ( Sudhir and Saurabh)
*/

//###### To Check whether year is a Leap Year! ( return 'noofdays' in February ) ######//
function checkLeapYear(year)
{
	 var noofdays ='';
	 if (((year%4==0) && (year%100!=0)) || (year%400==0))  
		noofdays=29;
	 else
		noofdays=28;
		
	//alert(noofdays);	
	
	return noofdays; //if 'noofdays = 29', means LEAP YEAR.
}

// ###### To get No of Days in a month for a particular year ######	//
function getDays(monthname, year)
{
   var noofdays='';
	
	//alert(checkLeapYear(year));
   
   if(monthname =='JAN')	 
		noofdays=31;
   else if(monthname =='FEB')
		noofdays = checkLeapYear(year); //defined above.
  
	else if (monthname =='MAR') 
		noofdays=31;

	else if (monthname =='APR') 
		noofdays=30;
	else if (monthname =='MAY') 
		noofdays=31;
	else if (monthname =='JUN') 
		noofdays=30;
	
	else if (monthname =='JUL') 
		noofdays=31;
	
	else if (monthname =='AUG') 
		noofdays=31;
	else if (monthname =='SEP') 
		noofdays=30;
	else if (monthname =='OCT') 
		noofdays=31;
	
	else if (monthname =='NOV') 
		noofdays=30;
		
	else if (monthname =='DEC') 
		noofdays=31;
		
	return noofdays;
 
 }  
//---------- End 'getDays()' ------------//

//this function returns the month no.
function getMonthNo(monthname)
{
	month=0;
	if(monthname =='JAN')	 
	  month='01';
	else if (monthname =='FEB')		
	  month='02';  
	else if (monthname =='MAR') 
	  month='03';
	else if (monthname =='APR') 
	  month='04';
	else if (monthname =='MAY') 
	  month='05';
	else if (monthname =='JUN') 
	  month='06';	
	else if (monthname =='JUL') 
	  month='07';	
	else if (monthname =='AUG') 
   	  month='08';
	else if (monthname =='SEP')
	  month='09';
	else if (monthname =='OCT') 
	  month='10';	
	else if (monthname =='NOV') 
	  month='11';		
	else if (monthname =='DEC') 
	  month='12';
		
	return month;
 
}

<!--//

function getTheDay(dateStr)
{
	var myDays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
	
	/**
	@Saurabh
	Above array is a NESTED ARRAY ( a shorthand notation for an array ) - JSON
	*/
	
	var myDate = new Date(eval('"'+ dateStr +'"'));
	
	return myDays[myDate.getDay()];

}
//-----------------
 //Usage: getTheDay('08/17/2008') gives "Friday"
        // one can give the date in any format.
//-----------------

//-->


//------------------ END.. Date Functions ----------------------//


//------------- Redirect to any page -----------//
function setPageLocation(pageUrl)
{
	window.location = pageUrl;
}
//-----------------------------------------------//

//POPUP window Functions.

		function showPopup_med(url) //in use.
		{
		  window.open(url, '_blank',  'toolbar=no,location=0,direction=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=500,top=15,left=200');
		}
		
		function popUpWin(targetPath, height, width, NewWinPutX, NewWinPutY)
        {
			//WINDOW HEIGHT AND WIDTH
			var NewWinHeight=height; //500
			var NewWinWidth=width; //400
			
			//WINDOW POSITION
			if( typeof NewWinPutX == 'undefined' )
				NewWinPutX=70;
			if( typeof NewWinPutY == 'undefined' )	
				NewWinPutY=20;
			
			//Get what is below onto one line
			TheNewWin = window.open(targetPath,'TheNewpop','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no');
			
			//TheNewWin = window.open(targetPath,'TheNewpop','fullscreen=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no'); 
			//Get what is above onto one line
			TheNewWin.resizeTo(NewWinHeight,NewWinWidth);

			/* Below line is commented as it's giving error on Google Chrome */
			//TheNewWin.moveTo(NewWinPutX,NewWinPutY);
        }
		
		
function showPopup_Pdf(url) //in use.
		{
		  window.open(url, '_blank',  'toolbar=no,location=0,direction=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=720,height=600,top=15,left=500');
		}		
		
  //Saurabh: To close this window after record is inserted.
  function closeWindow()
  {
      window.close();
  }
  
 //Getting System Date ().
  //dateformat ( 'Y-m-d', 'm-d-Y', d-m-Y )
 function getSystemDate(dateformat, seperator)
 {
	  var d, sMonth, sDate, sYear, arrDateformat, s;
	 
	  d = new Date();
	  sMonth = (d.getMonth() + 1);
	  sDate = d.getDate();
	  sYear = d.getFullYear();
	
	  if(dateformat == 'Y-m-d')
	  {
	  	 s = sYear + seperator + sMonth + seperator + sDate;
	  }
	  else if(dateformat == 'm-d-Y')
	  {
		 s = sMonth + seperator + sDate + seperator + sYear;	  
	  }
	  else if(dateformat == 'd-m-Y')
	  {
		 s = sDate + seperator + sMonth + seperator + sYear;	  
	  }	  
	 // alert(d + " " + " year = " + sYear + " " + s);
	  return(s);
 }
 
 //-------------------------------------//
 
 //Date Difference.
 function compareDates(date1, date2, dateFormat)
 {
	 if( typeof arguments[2] == 'undefined' ) //Here arguments[2] and dateFormat are same (we may use anything).
	 	dateFormat = '-'; //arguments[2] = '-';
	
	 //We supposed dates in "Y-m-d" format AND date1 is earlier than date2 (return true).
	 var arrDate1, arrDate2;
	 var date1Year, date2Year, date1Month, date2Month, date1Day, date2Day;
	 
	 arrDate1 = date1.split(dateFormat);
 	 arrDate2 = date2.split(dateFormat);
	 
	 date1Year = parseInt(arrDate1[0], 10);
	 date2Year = parseInt(arrDate2[0], 10);
	 
	 date1Month = parseInt(arrDate1[1], 10);
	 date2Month = parseInt(arrDate2[1], 10);
	 
	 date1Day = parseInt(arrDate1[2], 10);
	 date2Day = parseInt(arrDate2[2], 10);
	 
	 if(date1Year > date2Year)
	 {
		return false;
		//print "The second date is earlier than the first.";
		//The first date is bigger.
	 }
	 else if(date1Year == date2Year)
	 {
		if(date1Month > date2Month)
		{
			return false;
		}
		else if(date1Month == date2Month)
		{
			if(date1Day > date2Day)
			{
				return false;	
			}
			else return true;
		}
		else return true;
	 }
	 else return true;
 }
//============  End...compareDates() =================//
 
 //
 /* To change date format */
function changeDateFormat(dt, format)
{
	var dd, mm, YYYY;
	var newDt, dtFormatSeperator, newDtFormatSeperator;

	//Get old format seperator
	if( dt.search('-') != -1 )
	{
		dtFormatSeperator = '-';
	}
	else if( dt.search("/") != -1 )
	{
		dtFormatSeperator = '/';
	}

	//Get new format seperator
	if( format.search('-') != -1 )
	{
		newDtFormatSeperator = '-';
	}
	else if( format.search("/") != -1 )
	{
		newDtFormatSeperator = '/';
	}
	
	if( typeof format == 'undefined' )
		format = 'Y-m-d';

	/* extract date */
	//split using Old format seperator
	var arrDt = dt.split(dtFormatSeperator);
	
	if( format == 'Y-m-d' )
	{
		dd = arrDt[2];
		mm = arrDt[1];
		YYYY = arrDt[0];
		
		newDt = YYYY + newDtFormatSeperator + mm + newDtFormatSeperator + dd;
	}
	else if( format == 'm/d/Y' )
	{
		dd = arrDt[2];
		mm = arrDt[1];
		YYYY = arrDt[0];

		newDt = mm + newDtFormatSeperator + dd + newDtFormatSeperator + YYYY;		
	}
	
	return newDt;
}
//========================= End...changeDateFormat() ========================//
 
//========= ALTERNATIVE METHOD-1 for trim() in Javascript. ========//
			
			function ltrim(str)
			{
			   if(str!=false)              
			     return str.replace(/^\s+/, '');
            }
            function rtrim(str) 
			{
			   if(str!=false)
                return str.replace(/\s+$/, '');
            }
            function alltrim(str) //it will trim from Left & Right both.
			{  //alert(str);
			   if(str!=false)
                return str.replace(/^\s+|\s+$/g, '');
            }

//================================================================//


//----------- Remove all option elements from a combo box -----------//

function removeAllOptions(selectboxid)
{
	var i;
	var totalLength=document.getElementById(selectboxid).options.length;
	for(i=totalLength-1;i>=0;i--)
	{
		document.getElementById(selectboxid).remove(i);
	}	
}
//-------------------------------------------------------------------//

//------------ Getting Length of JSON Array ----------//
/*
	strJSON = "['one','two',{obj1:'obj1_one',obj2:'obj2_two'}]";
	
	to eval, you can use:
	
	Code:
	var arrObj = eval('(' +strJSON+ ')');
	//Note: for JSON string that represent an object, you cannot use length property. you need to loop through the object.
*/
function countJSON(obj) 
{
  var prop;
  var propCount = 0;
  
  for (prop in obj) 
  {
    propCount++;
  }
  return propCount;
}

//----------------------------//

//xxxxxxxxxx onselectstart = return false; (making a list box using a DIV ) xxxxxxxxxxxxxxx//
function rfalse()
{
	return false;
}

//For IE...onselectstart = return false (removing selection property from DIV - making a combo box like structure).
//no_sel(divId); //For IE only.
	//Note: For Mozilla a class 'no_sel {-moz-user-select:none;}' is made.	
function no_sel(divId)
{	
	if(document.attachEvent)
	{
		document.getElementById(divId).attachEvent('onselectstart', rfalse);
	}
}
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx//

/**
Get remaining characters of a textarea / text box.
*/
function textCounter( field, countfield, maxlimit ) 
{
  if ( ele(field).value.length > maxlimit )
  {
    ele(field).value = ele(field).value.substring( 0, maxlimit );
    //alert( 'Textarea value can only be '+ maxlimit +' characters in length.' );
    return false;
  }
  else
  {
    ele(countfield).value = maxlimit - ele(field).value.length;
  }
  //alert(ele(countfield).value)
}