// Created By: Armando Peņa

var HtmlDate_Months=new Array(12);
HtmlDate_Months[0] = "January";
HtmlDate_Months[1] = "Febraury";
HtmlDate_Months[2] = "March";
HtmlDate_Months[3] = "April";
HtmlDate_Months[4] = "May";
HtmlDate_Months[5] = "June";
HtmlDate_Months[6] = "July";
HtmlDate_Months[7] = "August";
HtmlDate_Months[8] = "September";
HtmlDate_Months[9] = "October";
HtmlDate_Months[10] = "November";
HtmlDate_Months[11] = "December";

function HtmlDate(did,value)
{
	this.id = did;
	var date = new Date();
	if(value != null)
	{
		var values = value.split("-");
		this.year = parseInt(values[0]);
		this.month = parseInt(values[1]) - 1;
		this.day = values[2];
	}
	else
	{
		var currYear = date.getYear();	
		if(currYear < 1000)
			currYear += 1900;
		this.year=currYear;
		this.month=date.getMonth();
		this.day = date.Date;
	}
	this.format = 1;
	this.monthCount = (12-this.month) + 12;
	this.yearsCount = 2;
	this.paramValue = value;
	this.abbreviateMonths = false;
			
}

function HtmlDate_ShowCalendar(source,target,format)
{
 var elemPicker=source;
 var left=elemPicker.offsetLeft + window.screenLeft; 
 var top=elemPicker.offsetTop;
 
 for(var p = elemPicker; p && (p.tagName != 'BODY'); p = p.offsetParent) 
 {
  left += p.offsetLeft; 
  top += p.offsetTop; 
 }
	var param = new Object();
    
	result=window.showModalDialog("Calendar.htm",param,"dialogWidth: 179px; dialogHeight: 235px; dialogTop:"+ top +"px; dialogLeft:"+ left +"px; status:no; edge: raised; help: no; scroll:no;");
	if(result+"" != "undefined")
	{
		var fecha=result.split("/");
		var dlist = document.getElementById(target+"_day");
		dlist.selectedIndex = fecha[0];
		var mlist = document.getElementById(target+"_month");
		mlist.selectedIndex = 0;
		options = mlist.options;

		if(format == 2)
		{
			var year = fecha[2]+"-"+fecha[1];
		
			for(var x=1; x < options.length; x++)
			{
				if(options[x].value == year)
					mlist.selectedIndex = x;
			}
		}
		else
		{
			for(var x=1; x < options.length; x++)
			{
				if(options[x].value == fecha[1])
					mlist.selectedIndex = x;
			}
			var ylist = document.getElementById(target+"_year");
			options = ylist.options;

			for(var x=1; x < options.length; x++)
			{
				if(options[x].text == fecha[2])
					ylist.selectedIndex = x;
			}					
		}
		HtmlDate_Update(target,format);
	}
}


function HtmlDate_GetHtml()
{
	html="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>";
	if(this.format == 2)
	{
		html +="<td>"+HtmlDate_GetHtmlMonthList(this)+"</td>";
		html +="<td>&nbsp;"+HtmlDate_GetHtmlDayList(this)+"</td>";
	}
	else
	{
		html +="<td>"+HtmlDate_GetHtmlMonthList(this)+"</td>";
		html +="<td>&nbsp;"+HtmlDate_GetHtmlDayList(this)+"</td>";
		
		html +="<td>&nbsp;"+HtmlDate_GetHtmlYearList(this)+"</td>";				
	}
	html +="<td><img id='cal_"+ this.id +"' src=\"cal.gif\" onclick=\"HtmlDate_ShowCalendar(this,'" + this.id + "',"+ this.format+")\" hspace=\"5\"/></td>";
	html +="</tr></table><input type=\"hidden\" id=\"" + this.id +"\" name=\"" + this.id +"\">";
	return html;
}
HtmlDate.prototype.getHtml = HtmlDate_GetHtml;

function HtmlDate_Refresh()
{
	HtmlDate_Update(this.id,this.format)
}
HtmlDate.prototype.refresh = HtmlDate_Refresh;

function HtmlDate_GetHtmlDayList(me)
{
	var html="<select onchange=\"HtmlDate_Update('" + me.id + "',"+ me.format +")\" class=\"input\" id=\""+ me.id + "_day\" name=\""+ me.id + "_day\"><option></option>";
	for(var x=1; x<=31; x++)
		html +="<option " + (x == me.day ? "selected>" : ">") + (x<10 ? "0" : "")+ x + "</option>";
	html +="</select>";
	return html;
}

function HtmlDate_GetHtmlMonthList(me)
{
	var html ="<select onchange=\"HtmlDate_Update('" + me.id + "',"+ me.format +")\" class=\"input\" id=\""+ me.id + "_month\" name=\""+ me.id + "_month\"><option></option>";
	if(me.format == 2)
	{
		var mi = 0;
		var y = me.year;
		var m = me.month;
		while(mi < me.monthCount)
		{
			html +="<option value='"+y+"-"+ (m < 9 ? "0" : "")+ (m+1) +"'>"+ (me.abbreviateMonths ? HtmlDate_Months[m].substr(0,3) : HtmlDate_Months[m]) +" "+y+"</option>";
			m++;
			if(m >=12)
			{
				m = 0; 
				y++;
			}
			
			mi++;
		}	
	}
	else
	{
		for(var x=0; x< 12; x++)
			html +="<option value='" + (x <9 ? "0" :"") + (x+1) + "'" + (x == me.month ? " selected>" : ">") + (me.abbreviateMonths ? HtmlDate_Months[x].substr(0,3) : HtmlDate_Months[x]) + "</option>";			
	}
	html +="</select>";

	return html;
}

function HtmlDate_GetHtmlYearList(me)
{
	var date = new Date();
	var currYear = date.getYear();	
	if(currYear < 1000)
		currYear += 1900;	
		
	var html="<select onchange=\"HtmlDate_Update('" + me.id + "',"+ me.format +")\" class=\"input\" id=\""+ me.id + "_year\" name=\""+ me.id + "_year\"><option></option>";
	for(var x=currYear; x<currYear+me.yearsCount; x++)
		html +="<option " + (x == me.year ? "selected='selected'" : "") +">"+ x+"</option>";
	html +="</select>";
	return html;
}

function HtmlDate_Update(id,format)
{
	var dl = document.getElementById(id+"_day");
	var ml = document.getElementById(id+"_month");
	
	var date = document.getElementById(id);
	
	date.value = "";
	if(format == 2 && dl.selectedIndex >0 && ml.selectedIndex >0)
		date.value = ml.options[ml.selectedIndex].value + "-" + dl.options[dl.selectedIndex].text;
	else
	{
		var yl = document.getElementById(id+"_year");
		if(dl.selectedIndex >0 && ml.selectedIndex >0 && yl.selectedIndex >0)
			date.value = yl.options[yl.selectedIndex].text + "-" + ml.options[ml.selectedIndex].value + "-" + dl.options[dl.selectedIndex].text;
	}	
}



function HtmlDate_GetText()
{
	return document.getElementById(this.id).value;
}
HtmlDate.prototype.getText = HtmlDate_GetText;


function HtmlDate_GetValue()
{
	var text = this.getText();
	if(text == "")
		return null;
	var elems = text.split("-");
	var year = elems[0] * 1;
	var month = elems[1]*1 - 1;
	var day = elems[2] * 1;

	var date = new Date(year,month,day);

	if(date.getMonth() == month && date.getDate() == day)
		return date;
	else
		return null;
	
}
HtmlDate.prototype.getValue = HtmlDate_GetValue;





