var Jan			= (language == "en")?"Jan":"ene";
var Feb			= (language == "en")?"Feb":"feb";
var Mar			= (language == "en")?"Mar":"mar";
var Apr			= (language == "en")?"Apr":"abr";
var May			= (language == "en")?"May":"may";
var Jun			= (language == "en")?"Jun":"jun";
var Jul			= (language == "en")?"Jul":"jul";
var Aug			= (language == "en")?"Aug":"ago";
var Sep			= (language == "en")?"Sep":"sep";
var Oct			= (language == "en")?"Oct":"oct";
var Nov			= (language == "en")?"Nov":"nov";
var Dec			= (language == "en")?"Dec":"dic";

var arrMonths	= new Array("",Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
var MonthsLength = 4;

//******************************************************
//Calendar Drop Downs
//******************************************************

function clear(cb) {
	var len = cb.length;
	for(var i=len-1; i >=0 ;i--)
		cb.options[i] = null;
}

function isLeap(year) {
	return ((year % 4==0 && year % 100 != 0) || (year % 400 ==0)); // milenium?
}

function getMonthDays(year, month) 
{
	if(month == 2)
	{
	    if (isLeap(year))
	        return 29;
	    else
	        return 28;
	}
	return [0, 31, 29, 31, 30, 31, 30 , 31 ,31, 30, 31, 30, 31][month];
}

function findElement(cb, val) {
	for(var i=0;i<cb.options.length;i++)
		if(cb.options[i].value==val)
			return i
	return -1
}

function setCheckDay(year, month, day, cbDay) {
	var pos = findElement(cbDay, day);
	cbDay.selectedIndex = (pos==-1)?0:cbDay.selectedIndex = pos;
}

function onMonthChange (month, year, cbDay, startfrom) {	
	var days = getMonthDays(year, month);
	clear(cbDay);
	
	for(var i=(startfrom == null)?1:startfrom; i<=days; i++) {
		var opt = new Option(i,i);
		cbDay.options[cbDay.length]=opt;
	}
}

function populateDateFrom(day, month, year, cbDay, cbMonth, cbYear) {
	cbYear.options[cbYear.length]=new Option(year,year);
	cbYear.options[cbYear.length]=new Option(year+1,year+1);
	cbYear.options[cbYear.length]=new Option(year+2,year+2);
	cbYear.selectedIndex = 0;
	for(var i=1;i<=12;i++)
		cbMonth.options[cbMonth.length]=new Option(arrMonths[i],i);	
	
	cbMonth.selectedIndex=month-1;
	onMonthChange(month, year, cbDay);
	
	// populate cbDay
	var date1 = new Date(year, month, 1);
	var date2 = new Date(year, month + 1, 1);
	var diff = date2 - date1;

	var days = (month == 3)? // convert the difference to a number of days
		Math.ceil (diff/(1000*60*60*24)): 
		Math.floor(diff/(1000*60*60*24)); 
	
	//ClearCombo(cbDay);
	for(var i=1; i <= days; i++) {		
		var oOption	= new Option(i, i);
		cbDay.options[cbDay.length]	= oOption;
	}
	
	cbDay.selectedIndex=day -1
}

function setDefaultDate(day, month, year, cbDay, cbMonth, cbYear) {
	var dd = parseInt(day)
	var mm = parseInt(month)
	var yy = parseInt(year)
	
	//cbDay.selectedIndex = dd -1
	
	cbMonth.selectedIndex = 0
	for (var i=0;i<cbMonth.options.length;i++) 
		if(cbMonth.options[i].value==month) {
			cbMonth.selectedIndex = i
			break;
		}

	cbYear.selectedIndex = 0;
	for(var i=0;i<cbYear.options.length;i++)
		if(cbYear.options[i].value==year) {
			cbYear.selectedIndex = i
			break;
		}
	
	cbDay.selectedIndex = 0;
	
	for(var i=0;i<cbDay.options.length;i++)
		if(cbDay.options[i].value == day) {
			cbDay.selectedIndex = i;
			break;
		}
}

function same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY) {
	return (parseInt(cbToMM.value)==parseInt(cbFromMM.value) && parseInt(cbToYY.value)==parseInt(cbFromYY.value));
}

//function updateDateCB(id, cbDD_ID, cbMM_ID, cbYY_ID) {
function updateDateCB(el, elDD, elMM, elYY) {
	var oldDD = elDD.value;
	var oldMM = elMM.value;
	var oldYY = elYY.value;
	var d = Date.parseDate(el.value,'%d/%m/%Y');
	elDD.value = d.getDate();
	elMM.value = d.getMonth()+1;
	elYY.value = d.getFullYear();
	if (oldYY!=elYY.value) syncronizeDates(((elYY.id=='DateFromYear' )?'FromYY':'ToYY'),'DateFromDay','DateFromMonth','DateFromYear','DateToDay','DateToMonth','DateToYear');
	if (oldMM!=elMM.value) syncronizeDates(((elMM.id=='DateFromMonth')?'FromMM':'ToMM'),'DateFromDay','DateFromMonth','DateFromYear','DateToDay','DateToMonth','DateToYear');
	return false;
}

function syncronizeDates(whochanged, cbFromDD_ID, cbFromMM_ID, cbFromYY_ID, cbToDD_ID, cbToMM_ID, cbToYY_ID) {
	var cbFromDD	= getElement(cbFromDD_ID);
	var cbFromMM	= getElement(cbFromMM_ID);
	var cbFromYY	= getElement(cbFromYY_ID);
		
	var cbToDD		= getElement(cbToDD_ID);
	var cbToMM		= getElement(cbToMM_ID);
	var cbToYY		= getElement(cbToYY_ID);
	
	if(whochanged=='FromMM') { //from month changed
	
		var cm = parseInt(cbFromMM.value)
		var cy = parseInt(cbFromYY.value)
		var cd = parseInt(cbFromDD.value)
		//update the From days combo
		onMonthChange(cbFromMM.value, cbFromYY.value, cbFromDD)

		//update the To combos
		if(cm> (12 - MonthsLength + 1)) {
			clear(cbToYY);
			//add current year and next year options
			cbToYY.options[cbToYY.length]=new Option(cy,cy);
			cbToYY.options[cbToYY.length]=new Option(1+cy,1+cy);
			//set selection the the first year
			cbToYY.options.selctedIndex = 0
			clear(cbToMM)
			//fill the To months combo
			for(var i=cm; i<=12; i++)
				cbToMM.options[cbToMM.length] = new Option(arrMonths[i],i)
			
			//set To default to first month
			cbToMM.options.selectedIndex = 0
			
			//synchronyze the To days combo
			if (same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY))
				onMonthChange(cbToMM.value, cy, cbToDD, cd)
			else 
				onMonthChange(cbToMM.value, cy, cbToDD)
		} else {
			clear(cbToMM)
			clear(cbToYY)
			//fill current year
			cbToYY.options[cbToYY.length]=new Option(cy,cy);
			//fill the To months combo
			for(var i=cm;i<cm+MonthsLength;i++){
				cbToMM.options[cbToMM.length] = new Option(arrMonths[i],i)
			}
			//synchronize th To days combo
			if (same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY))
				onMonthChange(cbToMM.value, cy, cbToDD, cd)
			else 
				onMonthChange(cbToMM.value, cy, cbToDD)
		}

		setCheckDay(cy, cm, cd, cbFromDD)
		
		var ctm = parseInt(cbToMM.value)
		var cty = parseInt(cbToYY.value)
		var ctd = parseInt(cbToDD.value)
		var cfd = parseInt(cbFromDD.value)

		if (ctd < cfd)
			cbToDD.selectedIndex = findElement(cbToDD,cfd)
		else {
			cbToDD.selectedIndex = findElement(cbToDD,ctd)
			if(cbToDD.selectedIndex==-1){cbToDD.selectedIndex=0}
		}
	}
	
	if (whochanged=='FromYY') {//from year changed
	
		//save the prevoius month
		var cm = parseInt(cbFromMM.value)
		var cy = parseInt(cbFromYY.value)
		var cd = parseInt(cbFromDD.value)
		//refresh the days combo (because the year we selected migth be a leap year)

		onMonthChange(cm, cy, cbFromDD)
		setCheckDay(cy, cm, cd, cbFromDD)
		//refresh the To controls
		syncronizeDates('FromMM', cbFromDD_ID, cbFromMM_ID, cbFromYY_ID, cbToDD_ID, cbToMM_ID, cbToYY_ID)
	}
	
	if(whochanged=='FromDD') { //from day changed
	
		var cfd = parseInt(cbFromDD.value)
		var ctd = parseInt(cbToDD.value)
		var ctm = parseInt(cbToMM.value)
		var cty = parseInt(cbToYY.value)
		
		if(same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY))
			onMonthChange(ctm, cty, cbToDD, cfd)
		else
			onMonthChange(ctm, cty, cbToDD)
				
		//if february 
		if (ctm==2) {
			//if the From year is not leap and To year is leap 
			//and the selected day in the From combo is 29 we set the corresponding day
			//in the To combo to 28
			if(!isLeap(cbFromYY.value) && isLeap(cbToYY.value) && cfd>=29)
				cbToDD.selectedIndex = cbToDD.options.length-1 //corresponds to value 28 in the combo
			else if (ctd<cfd && same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY))
				//set the same day in the To days combo
				cbToDD.selectedIndex = findElement(cbToDD, cfd)
			else
				cbToDD.selectedIndex = findElement(cbToDD, ctd)
			
		} else if (ctd<cfd && same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY))
				//set the same day in the To days combo
				cbToDD.selectedIndex = findElement(cbToDD, cfd)
		else {
				cbToDD.selectedIndex = findElement(cbToDD,ctd)
				if(cbToDD.selectedIndex==-1){cbToDD.selectedIndex=0}
		}
	}
	
	if(whochanged=='ToMM') { //To month changed
		var cfd = parseInt(cbFromDD.value)
		var ctd = parseInt(cbToDD.value)

		if (same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY))
			onMonthChange(cbToMM.value, cbToYY.value, cbToDD, cfd)
		else
			onMonthChange(cbToMM.value, cbToYY.value, cbToDD )
		
		if (ctd<cfd && same_year_and_month(cbFromMM, cbFromYY, cbToMM, cbToYY))
			//set the same day in the To days combo
			cbToDD.selectedIndex = findElement(cbToDD, cfd)
		else {
			cbToDD.selectedIndex = findElement(cbToDD, ctd)
			if(cbToDD.selectedIndex==-1){cbToDD.selectedIndex=0}
		}
	}
	
	if(whochanged=='ToYY') { //To year changed
		var cfm = parseInt(cbFromMM.value)
		var cfy = parseInt(cbFromYY.value)
		var cty = parseInt(cbToYY.value)
		if (cty==cfy+1) { //we selected the second year in the ToYY combo
			clear(cbToMM)
			//fill in the rest of the months
			for (var i=1; i<MonthsLength-(12-cfm); i++)
				cbToMM.options[cbToMM.length] = new Option(arrMonths[i],i)
		} else {
			clear(cbToMM)
			//fill in the rest of the months
			for (var i=cfm;i<=12;i++)
				cbToMM.options[cbToMM.length] = new Option(arrMonths[i],i)
		}
		syncronizeDates('ToMM', cbFromDD_ID, cbFromMM_ID, cbFromYY_ID, cbToDD_ID, cbToMM_ID, cbToYY_ID)
	}
	
	//if(whochanged=='ToDD') {}//To day changed
}

function FillDates(cbDateFromMonthID, cbDateFromDayID, cbDateFromYearID, cbDateToMonthID, cbDateToDayID, cbDateToYearID) {
	var cbDateFromMonth		= getElement(cbDateFromMonthID);
	var cbDateFromDay		= getElement(cbDateFromDayID);
	var cbDateFromYear		= getElement(cbDateFromYearID);
	var cbDateToMonth		= getElement(cbDateToMonthID);
	var cbDateToDay			= getElement(cbDateToDayID);
	var cbDateToYear		= getElement(cbDateToYearID);
	
	var date				= new Date();
	var yearFrom			= g_DateFrom.getFullYear();
	var monthFrom			= g_DateFrom.getMonth();
	var dayFrom				= g_DateFrom.getDate();
	
	var yearTo				= g_DateTo.getFullYear();
	var monthTo				= g_DateTo.getMonth();
	var dayTo				= g_DateTo.getDate();
		
	populateDateFrom(date.getDate(), date.getMonth(), date.getFullYear(), cbDateFromDay, cbDateFromMonth, cbDateFromYear);
	setDefaultDate(dayFrom, monthFrom, yearFrom, cbDateFromDay, cbDateFromMonth, cbDateFromYear);	
	
	syncronizeDates('FromYY', cbDateFromDayID, cbDateFromMonthID, cbDateFromYearID, cbDateToDayID, cbDateToMonthID, cbDateToYearID);
	setDefaultDate(dayTo,monthTo,yearTo, cbDateToDay, cbDateToMonth, cbDateToYear);
}

//
//
//
//
function FillMonths96(cbMonthName, allowAllSearch)
{
	//**********************************************
	var cbMonth		= getElement(cbMonthName);
	var d			= new Date();
	//**********************************************
	var dNow		= d.getDate();
	var mNow		= d.getMonth();
	var yNow		= d.getFullYear();

	// add the current month
	var tomorrow	= new Date(yNow, mNow, dNow + 1)
	var nextMonth;
	//**********************************************
	if (mNow < 11)
		var nextMonth	= new Date(yNow, mNow + 1, 1);
	else
		var nextMonth	= new Date(yNow, 1, 1);
	
	//**********************************************
	var diff			= nextMonth - tomorrow;
	
	if (allowAllSearch == 'True')
		cbMonth.options[cbMonth.length] = new Option('All Months', '1/1/1980');
	if (Math.ceil( (diff)/(1000*60*60*24)) > 1) {
		var value	= (tomorrow.getMonth() + 1) + "/" + tomorrow.getDate() + "/" + tomorrow.getFullYear();
		var text	= arrMonths[tomorrow.getMonth() + 1] + " " + tomorrow.getFullYear();
		cbMonth.options[cbMonth.length] = new Option(text, value);
	}
	
	// add the list of months from the current year
	//**********************************************
	for(var idx = mNow + 1; idx < 12; idx++) {
		var value	= (nextMonth.getMonth() + 1) + "/1/" + nextMonth.getFullYear() ;
		var text	= arrMonths[nextMonth.getMonth() + 1] + " " + nextMonth.getFullYear();
		cbMonth.options[cbMonth.length] = new Option(text, value);
		nextMonth.setMonth(nextMonth.getMonth() + 1);
	}

	//**********************************************
	// add the list of months from the next year
	//**********************************************
	var nextYear	= new Date((d.getFullYear() + 1), 0, 1);
	for (var idx = 0; idx < 12; idx++) {
		var value	= (nextYear.getMonth() + 1) + "/1/" + nextYear.getFullYear() ;
		var text	= arrMonths[nextYear.getMonth() + 1] + " " + nextYear.getFullYear();
		cbMonth.options[cbMonth.length] = new Option(text, value);
		nextYear.setMonth(nextYear.getMonth() + 1);
	}
	
	SetDefaultSelection(cbMonth, g_currentMonth);
}