/**
 This function is used to remove all the spaces from 
 right of the string passed.
*/
function rtrim(inString) {
	while (1) {
		var ch = inString.charAt(inString.length - 1);
		if (ch == ' ' || ch == '\r' || ch == '\n') {
			inString = inString.substring(0, inString.length - 1);
		}
		else {
			break;
		}
  }
  return inString;
}

/**
 This function is used to remove all the spaces from 
 left of the string passed.
*/
function ltrim(inString) {
	while (1) {
		var ch = inString.charAt(0);
		if (ch == ' ' || ch == '\r' || ch == '\n') {
			inString = inString.substring(1, inString.length);
		}
		else {
			break;
		}
	}
	return inString;
}

/**
 This function is used to remove spaces from both
 left and right. It calls rtrim and ltrim to
 perform the operation. 
*/
function trim(inString) {
  if((inString == null) || (inString.length == 0)) {
  	return "";
  }
  var tmpStr = ltrim(inString);
  return rtrim(tmpStr);
}

/*
 This function is used to check if any option is
 selected. It will return -1 if no option is 
 selected or will return the index of the option
 selected.
 This function should be called by passing two attribute.
 1. radio button element, 2. size of the list
*/
function getCheckedInd(ele, size)
{
	var selIndex = -1;
	var ind = 0;
	if (size > 1)
	{
		for (iLoop = 0 ; iLoop < size ; iLoop++)
		{
			if (ele[iLoop].checked)
			{
				selIndex = ind;
				break;
			}
			ind ++ ;
		}
	}else
	{
		// Even if the radio button is not selected then select the 
		// radio button as there is only one value.
		ele.checked = true;
		selIndex = 0;
	}	
	return selIndex;	
}


/**
 This function will compare all the characters in
 InString with RefString. It will take character by
 character of InString and will check if the same is
 present in the RefString. Will return false if it is 
 not present. true otherwise.
*/
function allowInString (InString, RefString)  {
    //if(InString.length==0) return (false);
    for (var Count=0; Count < InString.length; Count++) { 
        var tempChar= InString.substring (Count, Count+1);
        if (RefString.indexOf (tempChar, 0)==-1)  
        {
            return false;
		}          
    }
    return true;
}

/**
 This function will be used to check if the String passed
 contains only alphabets and search paramter. searchParam 
 will be "" while validating only alphabets and will be "%"
 while validating for partial search.
*/
function checkAlphabets(elementVal,searchParam){
   toBeChecked = trim(elementVal).toUpperCase();
   if (toBeChecked.length > 0)
   {
	   var legalChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
	   legalChars = legalChars + searchParam;
	   if ( !allowInString(toBeChecked,legalChars))
	   {
			return false;	   
	   }
    }	 
   return true;
}

/**
 This function will be used to check if the String passed
 contains only alphabets and search paramter. searchParam 
 will be "" while validating only alphabets and will be "%"
 while validating for partial search.
*/
function checkNumeric(elementVal,searchParam){
   toBeChecked = trim(elementVal).toUpperCase();
   if (toBeChecked.length > 0)
   {
	   var legalChars = '0123456789';
	   legalChars = legalChars + searchParam;
	   if ( !allowInString(toBeChecked,legalChars))
	   {
			return false;	   
	   }
	}	   
   return true;
}

/**
 This function will be used to check if the string 
 passed contains only characters and Numeric entires.
*/
function checkAlphaNumeric(elementVal,searchParam){
   toBeChecked = trim(elementVal).toUpperCase();
   if (toBeChecked.length > 0)
   {
	   var legalChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ';
	   legalChars = legalChars + searchParam;
	   if ( !allowInString(toBeChecked,legalChars))
	   {
			return false;	   
	   }
	}	   
   return true;
}

/*
	This function is used to Validate the End Date
*/
function isEndDate(strDate)
{
	if(isValidDate(strDate)) {
		var iNoOfDays;
		var mydate= new Date();
		
		var theyear=mydate.getFullYear();
		var themonth=mydate.getMonth()+1;
		var thedate=mydate.getDate();
		
		if (themonth < 10) {
			themonth =  "0" + themonth ;
		}
		if (thedate < 10) {
			thedate = "0" + thedate;
		} 
		var currentDate = (themonth+"/"+thedate+"/"+theyear) ;	
		iNoOfDays = fnGetNumberOfDays(strDate, currentDate);
		if(iNoOfDays >= 1){
			return true;
		}else {
			alert("End date should be less than current date.");
			return false;
		}
	}else {	
		alert("Please enter the end date in MM/DD/YYYY format.");
		return false;
	}// End for isValidateDate for ending date		
}
/*
	This function is used to Validate the Beg Date
*/
function isBegDate(strDate)
{
	if(isValidDate(strDate)) {
		var iNoOfDays;
		var mydate= new Date();
		
		var theyear=mydate.getFullYear();
		var themonth=mydate.getMonth()+1;
		var thedate=mydate.getDate();
		
		if (themonth < 10) {
			themonth =  "0" + themonth ;
		}
		if (thedate < 10) {
			thedate = "0" + thedate;
		} 
		var currentDate = (themonth+"/"+thedate+"/"+theyear) ;	
		iNoOfDays = fnGetNumberOfDays(strDate, currentDate);
		if(iNoOfDays >= 1){
			return true;
		}else {
			alert("Start date should be less than current date.");
			return false;
		}
	}else{	
		alert("Please enter the start date in MM/DD/YYYY format.");
		return false;
	}// End for isValidateDate for beginning date
}

/*
 This function is used to Validate Date format for
 the Date passed. it check if the date entered is in 
 MM-DD-YYYY format.
*/
function isValidDate(strDate)
{
   strDate1 = trim(strDate);
   if (strDate1.length > 0)
   {
	   strDate1= new String(strDate1);
	   var intFirstSlash = strDate1.indexOf("/") ;
	   var intLastSlash  = strDate1.lastIndexOf("/");
	   var mon = strDate1.substring(0,intFirstSlash);
	   var day = strDate1.substring(intFirstSlash+1,intLastSlash);
	   var yr  = strDate1.substring(intLastSlash+1,strDate1.length);
	   if((isNaN(mon)) ||  (isNaN(day) ) || (isNaN(yr)))
	   {
	      //alert ('Please enter a valid date in MM/DD/YYYY format.');
	      return false;
	   }
	   dtDate = new Date(yr, mon-1, day);
	   var newyear = dtDate.getFullYear();
	   if (!((dtDate.getDate() == parseInt(day, 10)) && ((dtDate.getMonth()+ 1)== parseInt(mon,10)) &&
	      (newyear == parseInt(yr,10))))
	   {
	    //alert('Please enter a valid date in MM/DD/YYYY format.');
	    return false;
	   }
	   if ( day.length!=2 || mon.length!=2 || yr.length!=4)
	   {
	 	//alert('Please enter a valid date in MM/DD/YYYY format.');
	  	return false;
	   }
   }
   return true;
}

/*
 This function is used to calculate the difference
 between the two dates passed.
*/
function dateDiff(strDateFrom, strDateTo)
{
	intFirstHyphen = strDateFrom.indexOf("/") ;
    intLastHyphen  = strDateFrom.lastIndexOf("/") ;

    var monEff = strDateFrom.substring(0,intFirstHyphen);
    var dayEff = strDateFrom.substring(intFirstHyphen+1,intLastHyphen) ;
    var yrEff  = strDateFrom.substring(intLastHyphen+1,strDateFrom.length);
    intFirstHyphen = strDateTo.indexOf("/") ;
    intLastHyphen  = strDateTo.lastIndexOf("/") ;

    var monExp = strDateTo.substring(0,intFirstHyphen);
    var dayExp = strDateTo.substring(intFirstHyphen+1,intLastHyphen) ;
    var yrExp  = strDateTo.substring(intLastHyphen+1,strDateTo.length);

    DateTo    = new Date(yrExp, monExp - 1 , dayExp);
    DateFrom   = new Date(yrEff, monEff - 1 , dayEff);
    return ( DateTo.getTime() - DateFrom.getTime() );
}


/*
 This function is used to calculate the Numner of days
 between the two dates passed.
*/

function fnGetNumberOfDays(DateStr1,DateStr2)              
{
	var t1;
	var t2;
	var Diff;
	var NoOfDays; // This will give the output
	
	var mm1 = DateStr1.substring(0,2);
	var mm2 = DateStr2.substring(0,2);
	
	var dd1 = DateStr1.substring(3,5);
	var dd2 = DateStr2.substring(3,5);
 
	var yyyy1 = DateStr1.substring(6,10);
	var yyyy2 = DateStr2.substring(6,10);
     
	t1 = Date.UTC(yyyy1, mm1 - 1, dd1);
	t2 = Date.UTC(yyyy2, mm2 - 1, dd2);
 	
	Diff = t2 - t1;
	
	NoOfDays = Diff/86400000;

	return NoOfDays;
}//End of function

