﻿/*
This function trims the value.
09/18/2010 GS  Created.
*/
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,'');
}


/*
This function allows only valid characters in date textbox.
09/18/2010 GS  Created.
*/
function DateKeyPress(TextBox,e)
{
    var keyPress;
    var fireFox;
    if (navigator.appName == 'Netscape')
    {
        keyPress = (e.which != 0) ? e.which : e.keyCode;
        fireFox = true;
    }
    else
    {
        keyPress = window.event.keyCode;
    }
    var DateValue = TextBox.value;
    if ((keyPress <= 57) && (keyPress >= 48))
    {
        if (DateValue.length > 0)
        {
            if (DateValue.length == 2)
                TextBox.value = DateValue + "/";
            if (DateValue.length == 5)
                TextBox.value = DateValue + "/";
            if (DateValue.length > 9)
            {
                if (fireFox)
                {
                    e.returnValue = false;
                    e.cancel = true;
                    e.stopPropagation();
                    e.preventDefault();
                    return false;
                }
                else
                {
                    window.event.keyCode = 0;
                }
            }
            if (isNaN(DateValue.substring(0,1)))
                TextBox.value = 0;
        }
    }
    else
    {
        //ignore anything that's not a number
        if (fireFox)
        {
            switch (keyPress)
            {
                // unless its an arrow or delete or tab
                case 37: case 38: case 39: case 40: case 8: case 9: case 46: break;
                default:
                    e.returnValue = false;
                    e.cancel = true;
                    e.stopPropagation();
                    e.preventDefault();
                    return false;
            }
        }
        else
        {
            window.event.keyCode = 0;
        }
    }
}

/*
This function gets the full date in mm/dd/yyyy format.
09/18/2010 GS  Created.
*/
function GetFullDate(currd)
{
	var dtStr;
	var day,month,year;
	var montharr = new Array("31", "29", "31", "30", "31", "30","31", "31", "30", "31", "30", "31");
	if (currd != "")
	{
		var chkneg = currd.search("-")
		if (chkneg == 0)
		{				
			return -1;
		}
					
		if ((currd.indexOf("-") == -1) && (currd.indexOf("/") == -1))
		{			
			return -1;
		}
					
		if (currd.indexOf("-") != -1) // "-" separaters present
		{
			month = currd.substring(0 , currd.indexOf("-"));
			day = currd.substring(currd.indexOf("-")+1, currd.lastIndexOf("-"));
			year = currd.substring(currd.lastIndexOf("-")+1);
		}
		else if (currd.indexOf("/") != -1) // "/" separaters present
		{
			month = currd.substring(0 , currd.indexOf("/"));
			day = currd.substring(currd.indexOf("/")+1, currd.lastIndexOf("/"));
			year = currd.substring(currd.lastIndexOf("/")+1);
		}
        
        month = trim(month);
		day = trim(day);
		year = trim(year);
		
		if (isNaN(day))
		{			
			return -1;
		}
					
		if (day.length > 2 || day.length == 0)
		{			
			return -1;
		}
					
		if (parseInt(day,10) > 31 || parseInt(day,10) == 0 )
		{			
			return -1;
		}
		
		if (isNaN(month))
			return -1;
				
		if (month.length > 2 || month.length == 0)
			return -1;
        
        if (parseInt(month,10) > 12 || parseInt(month,10) == 0 )
			return -1;
		
		if (isNaN(year))
		{			
			return -1;
		}
		
		if (!(year.length == 4))
		{			
			return -1;
		}
					
		if((year < 1800))
		{			
			return -1;
		}
		
		var mth = 'valid'
		mParsed = month
		if (isNaN(month))
		{
				if (eval(month)>12)
				{
					mth = 'invalid';
					return -1;					
				}
			
		}					
		
		dParsed = parseInt(day,10)
		
		yParsed = parseInt(year,10)

		if ((eval(month) ==2) && (dParsed==30 || dParsed==31))
		{			
			return -1;
		}
		if(montharr[eval(month)-1]<dParsed)
        {                                                                                                                                                                                                                           
			return -1;		
		}
		if ( !(yParsed%4==0) && eval(month)==2 && (dParsed==29))
		{			
			return -1;
		}

		if (day.length == 1)
			day = "0"+day
					
        dtstr = month + "/" + day + "/" + year;
		return dtstr;
	}	
	return -1;
}

/*
This function returns true if the date is valid.
09/18/2010 GS  Created.
*/
function checkDate(obj)
{
	var dt = GetFullDate(obj.value)
	if(dt == -1)
	{
		obj.focus();		
		return false;
	}
	else
	{
		obj.value = dt
		return true;
	}
}

/*
This function validates the date.
09/18/2010 GS  Created.
*/
function ValidateDate(dateTxtbox)
{
    var isValid = true;
    if (trim(dateTxtbox.value) != '' && !checkDate(dateTxtbox))
    {
        isValid = false;
    }
    return isValid;
}


//Functions Created by Virender Asija
//Dated 02/05/2011

	function getDateFmt(firstDate)
	{
		var dates=firstDate.split('/');
		var day = dates[1];
		var month = dates[0]-1;
		var year = dates[2];
		firstDate = new Date(year, month, day);
		return firstDate;
	}

	//Difference between 2 dates
	function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return difference_ms/ONE_DAY;

	}
