//------------------------------------------------------------------------------------------
// function: formatDate
//           Function to manipulate single digit entry of date and month to double digits...
//------------------------------------------------------------------------------------------

function formatDate(publishDate)
{
	var dateSeparator = "-";
	var bufPublishArray = publishDate.split("-");
	var publishDateDay = bufPublishArray[1]; 
	var publishDateMonth = bufPublishArray[0]; 
	var publishDateYear = bufPublishArray[2];
	
	if(publishDateDay.length < 2)
	{
		publishDateDay = "0" + publishDateDay;
	}
	else
	{
		publishDateDay  = publishDateDay;
	} // end of if else publishDateDay.length < 2
	
	if(publishDateMonth.length < 2)
		{
			publishDateMonth = "0" + publishDateMonth;
		}
		else
		{
			publishDateMonth  = publishDateMonth;
		} // end of if else publishDateMonth.length < 2
	
	var effectivePublishDate = ( publishDateMonth + dateSeparator + publishDateDay+ dateSeparator + publishDateYear);
	return effectivePublishDate;
	
} // end of function formatDate

//------------------------------------------------------------------------------------
// function: modifyDates
//           Function calls formatDate and return a proper formatted date. 
//------------------------------------------------------------------------------------

function modifyDates(publishDate)
{	
	 var formatteddate = formatDate(publishDate);
	 return formatteddate;
	 
} // end of modify dates


//------------------------------------------------------------------------------------
// function: validateDateFormat
//           Function to ensure that the string is in date format.....................
//------------------------------------------------------------------------------------


function validateDateFormat(publishDate)
{		
		//var dateSeparator = "-";
		
		if(publishDate.length > 0)
			{
				
					if( (publishDate.charAt(1) == '-') && (publishDate.charAt(3) == '-') || (publishDate.charAt(2) == '-') && (publishDate.charAt(5) == '-') || (publishDate.charAt(2) == '-') && (publishDate.charAt(4) == '-') || (publishDate.charAt(1) == '-') && (publishDate.charAt(4) == '-') )
					{
						//var publishDate = modifyDates(publishDate);
						return true;
					}//  end of if publishDate.charAt(2)
					else
					{
						//alert("Please enter Suggested Date as shown in date format ");
						//showMessage(20);
						return false;
					} // end of if else
					
					//return true;
			} // end o if 
						
} // end o func publish date

//---------------------------------------------------------------------------------------
// function: isBeforeSysDate() 
//           Function to compare a date with the SysDate......It takes one parameter and 
//				that is the date which is to be compared with the System Date.....
//---------------------------------------------------------------------------------------

function isBeforeSysDate(cDate)
{
	var dateSeparator = "-";
	var sysDateTemp = new Date(); 	

	var theYear = sysDateTemp.getYear()
	if (theYear < 1800)
	{
		theYear = theYear + 1900;
	}
	var sysDate = new Date(theYear, sysDateTemp.getMonth(), sysDateTemp.getDate());


	var sYear = sysDate.getYear();
	if (sYear < 1800)
	{
		sYear = sYear + 1900;
	}	

	var sMonth = sysDate.getMonth()+1;

	var sDate = sysDate.getDate();


	var fullDate = "" + sMonth + dateSeparator + sDate + dateSeparator + sYear;

	//fullDate = modifyDates(fullDate);
//alert("system date"+fullDate);
  	var difference = dateDifference(fullDate, cDate);
	
	if (difference > 0)
	{
		
		return true;
	} // end of if
	else
	{ 
		return false;
	}
} // end of function isBeforeSysDate

function isBeforeDate(firstDate,secondDate)
{
  	var difference = dateDifference(firstDate, secondDate);
	
	if (difference > 0)
	{			
		return true;
	} // end of if
	else
	{ 		
		return false;
	}
} // end of function isBeforeSysDate


//------------------------------------------------------------------------------------
// function: isValidDate
//           Function to avoid rollback and //alert user to enter a proper date .......
//  		Expects input in date in String format eg: "dd-mm-yyyy" ..........
//------------------------------------------------------------------------------------

function isValidDate(publishDate)
{
	var yearLower = 1900;
	var yearUpper = 2100;
		
		
		if(validateDateFormat(publishDate))
		{
			
			var bufPublishArray = publishDate.split("-"); 
	  		var publishDateMonth = bufPublishArray[0]; 
	  		var publishDateDay = bufPublishArray[1]; 
	   		var publishDateYear = bufPublishArray[2];
			
			//alert("publishdate is "+publishDate);
			
			/*if( (publishDateYear.length == 4) && (publishDateYear > yearLower && publishDateYear < yearUpper) )
			{
				//alert("old condition");
				publishDateYear = publishDateYear;
			}
			else
			{	
				//alert("I need a valid year between 1900 and 2100");
				
				showMessage(19);
				return false;
			} // end of if else publishDateMonth.length < 2
			*/
			//alert("Just before modified function");
			// Adding modified form of blocked code that is above!!!
			
			if( (publishDateYear.length != 4) || (publishDateYear < yearLower) || (publishDateYear > yearUpper) )
			{
				showMessage(19);
				return false;
			}
			
			
			
			var validDate = new Date(publishDateYear, publishDateMonth-1, publishDateDay);
			
			var validyear = validDate.getYear();// + 1900;
			
			/* 		this is required because some of the browsers take the year count after 1900...... 
					Hence adding 1900 after the condition is checked.... 
			*/
			
			if(validyear < 1800)
			{
				validyear = validyear + 1900;
				//publishDateYear = publishDateYear+1900;
				
			}
			
			var validmonth = validDate.getMonth();
			var validdate = validDate.getDate();
			var validDate = new Date(validyear, validmonth-1, validdate); 
			
			// this conditionality is done to avoid the rollback of dates..........
			if ((validyear == parseInt(publishDateYear,10)) && (validmonth == parseInt(publishDateMonth-1,10)) && (validdate == parseInt(publishDateDay,10)))
			{
	
				return true;
				
				
			}
			else
			{
				//alert("Please enter a Valid Date");
			//	showMessage(18);
				return false;
			} // end of if else
			
	
			
		} // end of if validateDateFormat(publishDate)

			
} // end of function isValid Date

//------------------------------------------------------------------------------------
// function: dateDifference() 
//           Function to compare two dates.......It takes those two dates as parameters
//				and the difference is given in terms of number of days.................
//------------------------------------------------------------------------------------

function dateDifference(startDate, endDate)
{	
	//alert("inside date difference first statement..startDate" +startDate + "endDate" +endDate);
	if (isValidDate(startDate) && isValidDate(endDate))
	{	
		//alert("after validating inside date difference......");
		var bufStartDateArray = startDate.split("-"); 
  		var startDateMonth = bufStartDateArray[0];
  		var startDateDate = bufStartDateArray[1]; 
   		var startDateYear = bufStartDateArray[2]; 
	
		var effectiveStartDate = new Date(startDateYear, startDateMonth-1, startDateDate); 
		
		var bufEndDateArray = endDate.split("-"); 
  		var endDateMonth = bufEndDateArray[0]; 
  		var endDateDate = bufEndDateArray[1]; 
   		var endDateYear = bufEndDateArray[2]; 
	
		var effectiveEndDate = new Date(endDateYear, endDateMonth-1, endDateDate);
		var difference = effectiveStartDate - effectiveEndDate;
		//alert("inside date difference-difference:"+difference);
		
		return difference / (24 * 60 * 60 * 1000);	
	}// end of if
	else
	{	
		//alert("inside else condition");
		return -9999999;
	} // end of if else....
	
} // end of function dateDifference



function trim(arg,func)
{
 		  var trimvalue = "";
          //var arglen = arg.length;

          if (arg.length < 1)
		  {
		  	return trimvalue;
		  }

          if (func == "left" || func== "both")
		  {
              var i = 0;
              pos = -1;
              	while (i < arg.length)
				{
		              if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i)))
					  {
                      		pos = i;
                            break;
                       } //end of if
                         		i++;
                 } // end of while
           } // end of if

          if (func == "right" || func== "both")
		  {
             var lastpos = -1;
             var i = arg.length;
                    while (i >= 0)
					{
                         if (arg.charCodeAt(i) != 32 &&  !isNaN(arg.charCodeAt(i)))
						 {
                             lastpos = i;
                             break;
                          } // end of if
                         		i--;
                      } // end of while
           } // end of if (func == "right" || func== "both")

          					if (func == "left")
							{
                                   trimvalue = arg.substring(pos,arg.length-1);
                            }

                            if (func == "right") {
                                    trimvalue = arg.substring(0,lastpos+1);
                            }

                            if (func == "both") {
                                    trimvalue = arg.substring(pos,lastpos + 1);
                            }

                            return trimvalue;

} // end of function


//------------------------------------------------------------------------------------
// function: isBlank()
//           Function to check whether any required field is left blank
//------------------------------------------------------------------------------------


function IsBlank(field)
{
	var temp = field.value;
	var temp1 = trim(temp,"both");
	if (temp1 == "")
	{
		return true;
	}
	else
	{
		return false;
	}
} // end of function isBlank


//----------------------------------------------------------------------------------------
//Function to validate the selected time. Megha added 27-02-04
//----------------------------------------------------------------------------------------

function validateTime(selectTime, curTime)
{
  
    if(curTime == null) {
	    
    	 curTime=new Date();
     }
   
	
	//Current date to string format for comparison
	var curDate=curTime.getMonth()+1+"-" + curTime.getDate() + "-" +curTime.getYear();

	var selTimeArr=selectTime.split('-');
	
	//Getting chosen date and time
	var selMth=parseInt(selTimeArr[0],10);
	var selDay=parseInt(selTimeArr[1],10);
	var selYr=parseInt(selTimeArr[2],10);

	var selHr=parseInt(selTimeArr[3],10);
	var selMin=parseInt(selTimeArr[4],10);	

	//Time zone and hr information
	var selAMPM=selTimeArr[5];
	var selTZ=selTimeArr[6];

	//Selected date to string format for comparison
	var selDate=selMth +"-" + selDay +"-" + selYr;
	
	
	//If Selected date>=Today enter loop
	//if(!dateDifference(selDate,curDate))
	//{
	
	
	
	//Hour range between 0-23
	if(selHr==12)
		selHr=0;
	if(selAMPM=='PM')
		selHr=selHr + 12;
	
	//Select time at GMT in Milliseconds
	var selTimeinMS=Date.UTC(selYr,selMth-1,selDay,selHr,selMin,0);

	//Current time at GMT in Milliseconds
	var curTimeinMS=Date.parse(curTime.toUTCString());
	
	//Offset from GMT due to time zone
	var timeZoneOffset=getTimeZoneOffset(selTZ);

		
	//Selected time +/- offset
	
	selTimeinMS=selTimeinMS+timeZoneOffset;
	
	var bufferTime=1*30*60*1000;//1 hr
	
	var timeDiff=selTimeinMS + bufferTime - curTimeinMS
	
	
	
	if(timeDiff<0)
		return false;		
		
	//}//End of check date difference if loop
	
	else
		return true;
}

function getTimeZoneOffset(timezone)
{
	if(timezone=='EST')
		return 5*60*60*1000;
		
	else if(timezone=='CST')
		return 6*60*60*1000;
		
	else if (timezone=='MST')
		return 7*60*60*1000;	
		
	else if(timezone=='PST')
		return 8*60*60*1000;
		
	else if(timezone=='IST')
		return -5.5*60*60*1000;
		
	else if(timezone=='EDT')
		return 4*60*60*1000;
		
	else
		return 0;
}
	
	
//-----------------------------------------------------------------------------------------------------
// Megha Added 11-2-05 - Simple Trimming function- Recursive elimination of starting and trailing spces	
//-----------------------------------------------------------------------------------------------------
function trimValue(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
  
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
//-----------------------------------------------------------------------------------------------------