/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	CHECK DATE IS VALID OR NOT I.E. '2006-02-29' IS NOT VALID '2006-04-31' IS NOT VALID
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
function IsDateValid(frm,prefix)
{
	
	var mn=""+frm.elements[prefix+"Mon"].value;
	var day=""+frm.elements[prefix+"Day"].value;
	var yr=""+frm.elements[prefix+"Year"].value;
	if((mn=='02') && (parseInt(day)>28)){	
		if ((((parseInt(yr) % 4 == 0) && (parseInt(yr) % 100 != 0)) || (parseInt(yr) % 400 == 0)) && (parseInt(day) < 30))
			return true;
		else
			return false;
	}
	else if((mn=='04' || mn=='06' || mn=='09' || mn=='11') && (parseInt(day)>30))
		return false;
	else
		return true;
}
/*~~~~~~~~~~~~~~~~~~~~RETURN TRUE IF Given Date IS GREATER THAN CURRENT DATE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
function IsDateGreaterThenCurrent(frm,tdt)
{
  	var cur_date=new Date();
	var cur_year=cur_date.getYear();
	if(cur_year<999)
		cur_year+=1900;
	var cur_month=cur_date.getMonth();
	var cur_day=cur_date.getDate();
	var cdate=new Date(cur_year,cur_month,cur_day);
	var todt = new Date(frm.elements[tdt+"Year"].value,(frm.elements[tdt+"Mon"].value - 1),frm.elements[tdt+"Day"].value);
	if(todt>cdate)		return 'greater';
	else if(todt<cdate)	return 'less';
	else			return 'equal';
}

function IsDateGreater(formObj,startDate,endDate)
{
	if(endDate=='Today')
	{
		return IsDateGreaterThenCurrent(formObj,startDate);
	}
	var firstDate = new Date(formObj.elements[startDate+"Year"].value,formObj.elements[startDate+"Mon"].value,formObj.elements[startDate+"Day"].value);
	var secondDate = new Date(formObj.elements[endDate+"Year"].value,formObj.elements[endDate+"Mon"].value,formObj.elements[endDate+"Day"].value);
//	return firstDate > secondDate;
	if (firstDate < secondDate)		return "less";
	else if (firstDate > secondDate)	return "greater";
	else 					return "equal";
}

function dateCombo(vname,addyear,lessyear,selday,selmonth,selyear)
{
	today = new Date();
	thisyear = today.getYear();
    	if (thisyear < 999 )
        thisyear = today.getYear()+1900;
	maxyear=thisyear + addyear;
    	minyear=thisyear - lessyear;
	if(!selday && !selmonth && !selyear)
	{
		thisday = today.getDate();
		thismonth = today.getMonth()+1;
	}   
	else
	{
		thisday=selday;
		thismonth=selmonth;
		thisyear=selyear;
	}
	days=vname + "Day";
	months=vname + "Mon";
	years=vname + "Year";
	//Display days
	
	document.write(" <select name=");
	document.write(days);
	document.write(" size=1 style=width:50>");
	for (var theday = 1; theday <= 31; theday++) 
	{
		var theday = "" + theday;
		if (theday.length == 1) 
			theday = "0" + theday;
		document.write("<option value='" + theday + "' ");
		if (theday == thisday) document.write(" selected");
		document.write(">");
		document.write(theday);
	}
	document.write("</select>");
	//display month
	document.write(" <select name=");
	document.write(months);
	document.write(" size=1 style=width:50>");
	for (var themn = 1; themn <= 12; themn++) 
	{
		var themn = "" + themn;
		if (themn.length == 1) 
			themn = "0" + themn;
		document.write("<option value='" + themn + "' ");
		if (themn == thismonth) document.write(" selected");
		document.write(">");
		document.write(themn);
	}
	document.write("</select>");
	//display year
	document.write(" <select name=");
	document.write(years);
	document.write(" size=1 style=width:70>");
	for (var theyr = minyear; theyr <= maxyear; theyr++) 
	{
		document.write("<option value='" + theyr + "' ");
		if (theyr == thisyear) document.write(" selected");
		document.write(">");
		document.write(theyr);
	}
	document.write("</select>");
}    //end of function

function dt(vname,addyear,lessyear)
{
    today = new Date();
    thismonth = today.getMonth()+1;
    thisyear = today.getYear();
    if (thisyear < 999 )
        thisyear = today.getYear()+1900;
    thisday = today.getDate();
    maxyear=thisyear + addyear;
    minyear=thisyear - lessyear;
    days=vname + "days";
    months=vname + "months";
    years=vname + "years";

    //Display days

    document.write(" <select name=");
    document.write(days);
    document.write(" size=1 style=width:50>");
    for (var theday = 1; theday <= 31; theday++) {
        var theday = "" + theday;
        if (theday.length == 1) {
            theday = "0" + theday;
        }
        document.write("<option value='" + theday + "' ");
        if (theday == thisday) document.write(" selected");
        document.write(">");
        document.write(theday);
    }
    document.write("</select>");

    //display month

    document.write(" <select name=");
    document.write(months);
    document.write(" size=1 style=width:50>");
    for (var themn = 1; themn <= 12; themn++) {
        var themn = "" + themn;
        if (themn.length == 1) {
            themn = "0" + themn;
        }
        document.write("<option value='" + themn + "' ");
        if (themn == thismonth) document.write(" selected");
        document.write(">");
        document.write(themn);
    }
    document.write("</select>");


    //display year

    document.write(" <select name=");
    document.write(years);
    document.write(" size=1 style=width:70>");
    for (var theyr = minyear; theyr <= maxyear; theyr++) {
        document.write("<option value='" + theyr + "' ");
        if (theyr == thisyear) document.write(" selected");
        document.write(">");
        document.write(theyr);
    }
    document.write("</select>");

}    //end of function
// this function used when not refreshed your combo box
function dt_notrefresh(vname,addyear,lessyear,day,month,year)
{
  	today = new Date();
    thismonth = today.getMonth()+1;
    thisyear = today.getYear();
    if (thisyear < 999 )
        thisyear = today.getYear()+1900;
    thisday = today.getDate();
    maxyear=thisyear + addyear;
    minyear=thisyear - lessyear;
    days=vname + "days";
    months=vname + "months";
    years=vname + "years";

    //Display days

    document.write(" <select name=");
    document.write(days);
    document.write(" size=1 style=width:50>");
    day=""+day;
	if (day.length == 1) {
		day = "0" + day;
	}
	for (var theday = 1; theday <= 31; theday++) {
        var theday = "" + theday;
        if (theday.length == 1) {
            theday = "0" + theday;
        }
        document.write("<option value='" + theday + "' ");
        if (theday == day){
			document.write(" selected");
		}
		document.write(">");
        document.write(theday);
    }
    document.write("</select>");

    //display month

    month=""+month;
	if (month.length == 1) {
		month = "0" + month;
	}

	document.write(" <select name=");
    document.write(months);
    document.write(" size=1 style=width:50>");
    for (var themn = 1; themn <= 12; themn++) {
        var themn = "" + themn;
        if (themn.length == 1) {
            themn = "0" + themn;
        }
        document.write("<option value='" + themn + "' ");
        if (themn == month){ 
		document.write(" selected");
		}
		document.write(">");
        document.write(themn);
    }
    document.write("</select>");


    //display year

    document.write(" <select name=");
    document.write(years);
    document.write(" size=1 style=width:70>");
    for (var theyr = minyear; theyr <= maxyear; theyr++) {
        document.write("<option value='" + theyr + "' ");
        if (theyr == year) document.write(" selected");
        document.write(">");
        document.write(theyr);
    }
    document.write("</select>");
	
}    //end of function


// this will compare two dates 
//and return false if first date is greater than to second
function date_compare_new(frm,startdt,enddt)
{
	//var cur_date=new Date();
	var firstdt = new Date(frm.elements[startdt+"years"].value,(frm.elements[startdt+"months"].value - 1),frm.elements[startdt+"days"].value);
    var seconddt = new Date(frm.elements[enddt+"years"].value,(frm.elements[enddt+"months"].value - 1),frm.elements[enddt+"days"].value);
	return (firstdt<=seconddt);
       
}
function date_compare_new1(frm,startdt,enddt)
{
	//var cur_date=new Date();
	var firstdt = new Date(frm.elements[startdt+"years"].value,frm.elements[startdt+"months"].value);
    var seconddt = new Date(frm.elements[enddt+"years"].value,frm.elements[enddt+"months"].value);
	return (firstdt<=seconddt);
       
}
function compare_date(frm,startdt,enddt)
{
	var firstdt = new Date(frm.elements[startdt+"years"].value,(frm.elements[startdt+"months"].value - 1),frm.elements[startdt+"days"].value);
    var seconddt = new Date(frm.elements[enddt+"years"].value,(frm.elements[enddt+"months"].value - 1),frm.elements[enddt+"days"].value);
	return (firstdt<seconddt);
}
/* RETURN TRUE IF TODAY IS GREATER THAN CURRENT DATE */
function check_to_date(frm,tdt)
{
    var cur_date=new Date();
	var cur_year=cur_date.getYear();
	if(cur_year<999)
		cur_year+=1900;
	var cur_month=cur_date.getMonth();
	var cur_day=cur_date.getDate();
	var cdate=new Date(cur_year,cur_month,cur_day);
	var todt = new Date(frm.elements[tdt+"years"].value,(frm.elements[tdt+"months"].value - 1),frm.elements[tdt+"days"].value);	
	return (todt>cdate);
}
// THIS FUNCTION CHECK CHEQUE-DATE SHOULD NOT BE MORE THAN TWO MONTHS OLD.
// MADE BY PRANAV JOSHI
function validate_cheque(frm,tdt)
{
	if(valid_chequedate(frm,tdt))
	{
		alert("Cheque should not be more than Two months old !!!");
		frm.elements[tdt+"days"].focus();
		return false;
	}
	return true;
}
function valid_chequedate(frm,tdt)
{
	var cur_date=new Date();
	var cur_year=cur_date.getYear();
	if(cur_year<999)
		cur_year+=1900;
	var cur_month=cur_date.getMonth();
	var cur_day=cur_date.getDate();
	
	var new_day=cur_day;
	if(cur_month==1 || cur_month==2)
	{	
		if(cur_month==1)
		{
			if(cur_day > 30)
			{
				var new_day=30;
			}
			var new_month=11;
			var new_year=cur_year - 1;
		}
		else if(cur_month==2)
		{
			var new_month=12;
			var new_year=cur_year - 1;
		}
	}
	else
	{
		if(cur_month==4)
		{
			if(cur_day > 28)
			{
				var new_day=28;
			}
		}
		else if(cur_month==8)
		{
			if(cur_day > 30)
			{
				var new_day=30;
			}
		}
		var new_month=cur_month - 2;
		var new_year=cur_year;
	}
	var new_date=new Date(new_year,new_month,new_day);
	var todt = new Date(frm.elements[tdt+"years"].value,(frm.elements[tdt+"months"].value - 1),frm.elements[tdt+"days"].value);
	return (new_date > todt);
}
// THIS FUNCTION CHECK CHEQUE-DATE SHOULD NOT BE MORE THAN TWO MONTHS OLD.
// MADE BY PRANAV JOSHI
function validate_dd(frm,tdt)
{
	if(valid_dddate(frm,tdt))
	{
		alert("DD should not be more than Two months old !!!");
		frm.elements[tdt+"days"].focus();
		return false;
	}
	return true;
}
function valid_dddate(frm,tdt)
{
	var cur_date=new Date();
	var cur_year=cur_date.getYear();
	if(cur_year<999)
		cur_year+=1900;
	var cur_month=cur_date.getMonth();
	var cur_day=cur_date.getDate();
	
	var new_day=cur_day;
	if(cur_month==1 || cur_month==2)
	{	
		if(cur_month==1)
		{
			if(cur_day > 30)
			{
				var new_day=30;
			}
			var new_month=11;
			var new_year=cur_year - 1;
		}
		else if(cur_month==2)
		{
			var new_month=12;
			var new_year=cur_year - 1;
		}
	}
	else
	{
		if(cur_month==4)
		{
			if(cur_day > 28)
			{
				var new_day=28;
			}
		}
		else if(cur_month==8)
		{
			if(cur_day > 30)
			{
				var new_day=30;
			}
		}
		var new_month=cur_month - 2;
		var new_year=cur_year;
	}
	var new_date=new Date(new_year,new_month,new_day);
	var todt = new Date(frm.elements[tdt+"years"].value,(frm.elements[tdt+"months"].value - 1),frm.elements[tdt+"days"].value);
	return (new_date > todt);
}
function valid_date(frm,tdt)
{
	var mn=frm.elements[tdt+"months"].value;
	var day=frm.elements[tdt+"days"].value;
	var yr=frm.elements[tdt+"years"].value;
	//alert(mn+'==='+day+'---'+yr);
	if(yr == 0 || day == 0 | mn == 0)
	{
		return true;
	}
	if((mn=='02') && (day>28))
	{	
		if ((((yr % 4 == 0) && (yr % 100 != 0)) || (yr % 400 == 0)) && (day < 30))
			return false;
		else
			return true;
	}
	else if((mn=='04' || mn=='06' || mn=='09' || mn=='11') && (day>30))
		return true;
	else
		return false;
}

var count=0;
function counter(frm)
{
	count++;
	if(count==1)
		return true;
	else 
		return false;
}

//THIS WILL VALIDATE THE DATE 
//ALSO PROMPT IF DATE IS GREATER THAN SYSTEM CURRENT DATE
function validate_date(frm,tdt)
{
	if(valid_date(frm,tdt))
	{
		alert("Please Enter Valid Date !!!");
		frm.elements[tdt+"days"].focus();
		return false;
	}
	else if(check_to_date(frm,tdt))
	{
		alert("Date Should not be Greater than Current Date !!!");
		frm.elements[tdt+"days"].focus();
		return false;
	}
	return true;
}
function dtyears(vname,addyear,lessyear)
{
	today = new Date();
	thisyear = today.getYear();
	if (thisyear < 999 )
        thisyear = today.getYear()+1900;

	maxyear=thisyear + addyear;
    	minyear=thisyear - lessyear;
	years=vname + "years";
	document.write(" <select name=");
    	document.write(years);
	document.write(" size=1 style=width:70 id="+years+">");
    for (var theyr = minyear; theyr <= maxyear; theyr++) {
        document.write("<option value='" + theyr + "' ");
        if (theyr == thisyear) document.write(" selected");
        document.write(">");
        document.write(theyr);
    }
    document.write("</select>");
}
function dtmonths(vname)
{
	today = new Date();
	thismonth = today.getMonth()+1;
	months=vname + "months";
	var mon=new Array(12);
	mon[1]="Jan";mon[2]="Feb";mon[3]="March";mon[4]="April";
	mon[5]="May";mon[6]="June";mon[7]="July";mon[8]="Aug";
	mon[9]="Sep";mon[10]="Oct";mon[11]="Nov";mon[12]="Dec";
	document.write("<select name=");
	document.write(months);
	document.write(" style=width:70 id="+months+">");
	
	for(var i=1;i<=12;i++)
	{
		
		document.write("<option value='" + i + "' ");
		if(i==thismonth)document.write(" selected");
		document.write(">");
		document.write(mon[i]);
			
	}
	document.write("</select>");
	//alert(document.write(thismonth));
	return ;
}

function dtmonths_yrs(vname)
{
	today = new Date();
	thismonth = today.getMonth()+1;
	thisyear = today.getYear();
	if(thisyear < 999)	// netscape browser compatibilty
		thisyear += 1900;
	months=vname + "months";
	var mon=new Array(12);
	mon[1]="Jan";mon[2]="Feb";mon[3]="March";mon[4]="April";
	mon[5]="May";mon[6]="June";mon[7]="July";mon[8]="Aug";
	mon[9]="Sep";mon[10]="Oct";mon[11]="Nov";mon[12]="Dec";
	document.write("<select name=");
	document.write(months);
	document.write("  id="+months+">");
	for(var j=(thisyear - 2); j <=thisyear; j++)
	{
		for(var i=1;i<=12;i++)
		{
			if(i <10)
				document.write("<option value='" + j + "-0" + i +"' ");
			else				
				document.write("<option value='" + j + "-" + i + "' ");
			if(i==thismonth && j==thisyear) document.write(" selected");
			document.write(">");
			document.write(mon[i]+"-"+j);
		}
	}
	document.write("</select>");
	//alert(document.write(thismonth));
	return ;
}

function dtmonthsqur(vname)
{
	today = new Date();
	thismonth = today.getMonth()+1;
	months=vname + "months";
	var mon=new Array(12);
	mon[01]="Jan-Mar";mon[02]="Apr-June";mon[03]="July-Sep";mon[04]="Oct-Dec";
	document.write("<select name=");
	document.write(months);
	document.write(" style=width:150 id="+months+">");
	document.write("<option value='ALL'>--Select Period--");
	for(var i=1;i<=4;i++)
	{
		document.write("<option value='" + mon[i] + "' ");
		//if(i==thismonth)document.write(" selected");
		document.write(">");
		document.write(mon[i]);
			
	}
	document.write("</select>");
	//alert(document.write(thismonth));
	return;
}

//this function is used for restiction of current month selection
function Check_Cur_Month(frm,f)
{
	var fyear=frm.elements[f+"years"].value;
	var fmonth=frm.elements[f+"months"].value;
	
	today = new Date();
	thismonth = today.getMonth()+1;
	thisyear = today.getYear();
	if (thisyear < 999 )
	thisyear = today.getYear()+1900;
	
	if(thisyear==fyear)
	{		
		if(fmonth==thismonth)
		{
			alert("Current month can not be selected.");
			return false;
		}
	}
	return true;
}//end of function


function Check_to_Month(frm,f,t)
{
	var fyear=frm.elements[f+"years"].value;
	var fmonth=frm.elements[f+"months"].value;

	var tyear=frm.elements[t+"years"].value;
	var tmonth=frm.elements[t+"months"].value;
	
	if(fmonth.length <2)
		fmonth="0" + fmonth;
	if(tmonth.length <2)
		tmonth="0" + tmonth;
	/*alert("fyear-fmonth"+fyear+"-"+fmonth);
	alert("tyear-tmonth"+tyear+"-"+tmonth);*/
	if(tyear<fyear)
	{
		alert("From Period can not be greater than To Period.");
		return false;
	}
	
	if(tyear==fyear)
	{ 
		if(fmonth > tmonth)
		{
			alert("From Period can not be greater than To Period.");
			return false;
		}
	}
	
	return true;
}//end of function

function trimAll(sString) 
{ //file:///var/www/html/njindiainvest/includes/date_function.js
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

