//Current date in form script

arrMonths = new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var arrDays   = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

//set todays date
FromDate = addDays(new Date(),7);	//Start date is set to be 7 days into the future
TillDate = addDays(new Date(),14);	//End date is set to be 14 days into the future

FromDateDay = FromDate.getDate();
FromDateMonth = FromDate.getMonth();
FromDateYear = FromDate.getYear();
if (FromDateYear < 2000) FromDateYear += 1900; //for Netscape
CurrentYear = FromDateYear;

TillDateDay = TillDate.getDate();
TillDateMonth = TillDate.getMonth();
TillDateYear = TillDate.getYear();
if (TillDateYear < 2000) TillDateYear += 1900; //for Netscape

//function to add x days to current date
function addDays(myDate,days) {
    return new Date(myDate.getTime() + parseInt(days)*24*60*60*1000);
}
// add/subtract dates

//function set end date equal to start date
function setEndDate(WhichField) {
	if (WhichField == 'anio'){
		TillDateYear = document.forma.anio_desde[document.forma.anio_desde.selectedIndex].value;
	}
	if (WhichField == 'mes'){
		TillDateMonth = parseInt(document.forma.mes_desde[document.forma.mes_desde.selectedIndex].value)-1;
		//alert("Mes: "+TillDateMonth);
	}
	if (WhichField == 'dia'){
		TillDateMonth = parseInt(document.forma.mes_desde[document.forma.mes_desde.selectedIndex].value)-1;
		TillDateDay = document.forma.dia_desde[document.forma.dia_desde.selectedIndex].value;
	}	
	SetToToday('hasta');

	document.forma.dia_hasta.options[document.forma.dia_hasta.selectedIndex].value=document.forma.dia_desde.options[document.forma.dia_desde.selectedIndex].value;

	//alert(document.forma.dia_hasta.options[document.forma.dia_hasta.selectedIndex].value);
}

//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
	var DaysInMonth = 31;
	if (WhichMonth == "Apr" || WhichMonth == "Jun" || WhichMonth == "Sep" || WhichMonth == "Nov") DaysInMonth = 30;

	if (WhichMonth == "Feb" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;

	if (WhichMonth == "Feb" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;

	return DaysInMonth;
}


//function to change the available days in a months
function ChangeOptionDays(Which)
{
	DaysObject = eval("document.forma.dia_" + Which);
	MonthObject = eval("document.forma.mes_" + Which);
	YearObject = eval("document.forma.anio_" + Which);

	Year = YearObject[YearObject.selectedIndex].text;
	Month = MonthObject[MonthObject.selectedIndex].text;
	Day = DaysObject[DaysObject.selectedIndex].text;
	
	DaysForThisSelection = DaysInMonth(Month, Year);
	
	CurrentDaysInSelection = DaysObject.length;
	
	//alert("CurrentDaysInSelection: " + DaysObject.length + "  DaysForThisSelection: " + DaysForThisSelection);
	if (CurrentDaysInSelection > DaysForThisSelection)
	{
		for (i=0; i<(CurrentDaysInSelection-DaysForThisSelection); i++)
		{
			DaysObject.options[DaysObject.options.length - 1] = null
		}
		if (Which == "hasta") TillDateDay = 1;
	}

	if (DaysForThisSelection > CurrentDaysInSelection)
	{
		for (i=0; i<(DaysForThisSelection-CurrentDaysInSelection); i++)
		{
			NewOption = new Option(DaysObject.options.length + 1, CurrentDaysInSelection+i+1);
			DaysObject.add(NewOption);
		}
	}

	if (DaysObject.selectedIndex < 0) DaysObject.selectedIndex == 0;
	
	//document.forma.dia_hasta.options[document.forma.dia_hasta.selectedIndex].value=document.forma.dia_desde.options[document.forma.dia_desde.selectedIndex].value;
	//document.forma.dia_desde.options[document.forma.dia_desde.selectedIndex].value=document.forma.dia_desde.options[document.forma.dia_desde.selectedIndex].value;
}


//function to set options to today
function SetToToday(Which)
{
	DaysObject = eval("document.forma.dia_" + Which);
	MonthObject = eval("document.forma.mes_" + Which);
	YearObject = eval("document.forma.anio_" + Which);

	YearObject[Which == 'desde' ? FromDateYear-CurrentYear : TillDateYear-CurrentYear].selected = true;
	//YearObject[0].selected = true;
	
	MonthObject[Which == 'desde' ? FromDateMonth : TillDateMonth].selected = true;
	//MonthObject[FromDateMonth].selected = true;

	ChangeOptionDays(Which);

	DaysObject[Which == 'desde' ? FromDateDay-1 : TillDateDay-1].selected = true;
	//DaysObject[FromDateDay-1].selected = true;
}


//function to write option years plus x
function WriteYearOptions(YearSelected, YearsAhead)
{
	line = "";

	for (i=0; i<YearsAhead; i++)
	{
		if (eval(CurrentYear + i)==YearSelected)
			line += "<option SELECTED value='" + eval(CurrentYear + i) + "'>";
		else
			line += "<option value='" + eval(CurrentYear + i) + "'>";
		line += CurrentYear + i;
		line += "</option>";
	}

	return line;
}

//function to write option months
function WriteMonthOptions(MonthSelected)
{
	line = "";

	for (i=1; i<=12; i++)
	{
		if (i==MonthSelected)
			line += "<option SELECTED value='" + i + "'>";
		else
			line += "<option value='" + i + "'>";
		line += arrMonths[i-1];
		line += "</option>";
	}

	return line;
}

//function to write option months
function WriteDayOptions(DaySelected, MonthSelected)
{
	line = "";

	for (i=0; i<arrDays[MonthSelected-1]; i++)
	{
		if (i+1==DaySelected)
			line += "<option SELECTED value='" + eval(i+1) + "'>";
		else
			line += "<option value='" + eval(i+1) + "'>";
		line += eval(i+1);
		line += "</option>";
	}

	return line;
}

function setFocus(object,newIndex,textValue)
{
	if(!object || typeof(object)!="object" || typeof(object["options"])=="undefined" || !object.options.length){return false;};
	newIndex=(newIndex)?newIndex:0;
	newIndex=(textValue)?newIndex+"" : ( !isNaN(parseFloat(newIndex)) )?parseFloat(newIndex):newIndex;/*if textValue, newIndex is sth to be searched, as such must be String*/
	textValue=(!textValue && typeof(newIndex)=="string")?1:textValue;/*forgot to pass textValue BUT newIndex==String: textValue defaults to 1*/ 
	if(typeof(newIndex)=="number" && newIndex>size-1){return false;};
	if(!object.selectedIndex){object.selectedIndex=0;};
	var size=object.options.length;
	var noMatch=0;
	if(textValue){//find text or value?
		switch (textValue){
			case 1://search values
				for(var t=0;t<size;t++){
					if(object.options[t].value == newIndex){newIndex=t;noMatch=1; break;};
				}
			break;
			case 2://search texts
				for(var t=0;t<size;t++){
					if(object.options[t].text == newIndex){newIndex=t;noMatch=1; break;};
				}
			break;
		}
	}
	else{noMatch=1};
		if(!noMatch){return false;/*textValue was passed but search failed to find RELATED newIndex string*/};
	var current=0;
	if(object.selectedIndex<0){
		for(var c=0; c<size; c++){//no selection?
			if(object.options[c].selected){current=c; break;}
		}
	}
	else{ current=object.selectedIndex; };
	//store originals:
	var texts=new Array(size);
	var values=new Array(size);
	for(var i=0; i<size; i++){
		texts[i]=object.options[i].text;
		values[i]=object.options[i].value;
	};//stored.
	size=size-1;
	for(var i=0;;i++){
		object.options[current].text=texts[newIndex];
		object.options[current].value=values[newIndex];
		newIndex=(newIndex==size)?0:++newIndex;
		current=(current==size)?0:++current;
		if(i==size){return;};
	}
}
