function buildMessageBox(span, width, height)
{
	var iframe = document.getElementById(span);
		
	var screenW = 800, screenH = 600;
	if (parseInt(navigator.appVersion) > 3) 
	{
		screenW = screen.width;
		screenH = screen.height;
	}
	else if (navigator.appName == "Netscape" && parseInt(navigator.appVersion)==3 && navigator.javaEnabled()) 
	{
		var jToolkit = java.awt.Toolkit.getDefaultToolkit();
		var jScreenSize = jToolkit.getScreenSize();
		
		screenW = jScreenSize.width;
		screenH = jScreenSize.height;
	}
		
	var msgboxT = (screenH - 200 - height) / 2;
	if (msgboxT < 0) msgboxT = 0;
		
	var msgboxL = (screenW - width) / 2;
	if (msgboxL < 0) msgboxL = 0;
	
	iframe.style.position = "absolute";
	iframe.style.height = height;
	iframe.style.width = width;
	iframe.style.left = document.body.scrollLeft + msgboxL;
	iframe.style.top = document.body.scrollTop + msgboxT;
	
	if (iframe.style.display == "block")
	{
		iframe.style.display = "none";
		return;
	}
			
	iframe.style.display = "block";
}

function hideMessageBox(span)
{
	var iframe = document.getElementById(span);
	
	iframe.style.display = "none";
}

function buildCal(TextBoxFieldName)
{
	TextBoxField = document.getElementById(TextBoxFieldName);
   TextBoxField.style.position = "relative";
   
	/* This section is IE 5+ specific.  
	In order to run this in lower browsers, a span tag called 
	"spCal" needs to be defined in the HTML document. */
	iFrame = document.getElementById('iframe_calendario');
	if (iFrame == undefined)
	{
		iFrame = document.createElement("<iframe id='iframe_calendario' STYLE='display:none; border:ridge; position:absolute; width:200; height:150; z-index=100' MARGINHEIGHT='0' MARGINWIDTH='0' NORESIZE FRAMEBORDER='0' width='156' SCROLLING='NO'></iframe>"); 
		document.body.insertBefore(iFrame, document.body.firstChild);
	}
	
  iFrame.style.position = "absolute";
  iFrame.style.left = TextBoxField.offsetLeft;
	
	var eT = TextBoxField.offsetTop;
	var eH = TextBoxField.offsetHeight;
	var dH = iFrame.style.pixelHeight;
	var sT = document.body.scrollTop;
	
	iFrame.style.top = eT + eH;	
	
	var currentDate = new Date(), minimalDate = new Date('2004/11/24');
	var data_inicial = TextBoxField.value;
	data_inicial = data_inicial.replace(/-/g, "/");
	
    if (TextBoxField.value == "")
		currentDate = new Date();
	else
		currentDate = new Date(data_inicial);
	
	if (iFrame.style.display == "block")
	{
		iFrame.style.display = "none";
		return;
	}
	
	// First call to the calendar gets set to the current date
	if (currentDate < minimalDate)
		currentDate = minimalDate;

	drawCal(currentDate.getFullYear() + '/' + (currentDate.getMonth() + 1) + '/' +  currentDate.getDate(), minimalDate.getFullYear() + '/' + (minimalDate.getMonth() + 1) + '/' +  minimalDate.getDate());
	
	iFrame.style.display = "none";
	iFrame.style.display = "block";
}

function clearTextBox(TextBoxName)
{
	var textbox = document.getElementById(TextBoxName);
	if (textbox != null)
		textbox.value = '';
}

function drawCal (sDate, sMinDate)
{
	var sHTML = new String();
	var lTargetYear;
	var lTargetMonth;
	var lTargetDay;
	
	var lMinYear;
	var lMinMonth;
	var lMinDay;
	
	var sSplitDate;
	var sSplitMinDate;
	
	var objPop;
	// ex: "02/15/2001" will get split into an array.  the '/' is the separator
	sSplitDate = sDate.split('/');
	lTargetYear = sSplitDate[0];
	lTargetMonth = sSplitDate[1] -1;
	lTargetDay = sSplitDate[2];
	
	sSplitMinDate = sMinDate.split('/');
	lMinYear = sSplitMinDate[0];
	lMinMonth = sSplitMinDate[1];
	lMinDay = sSplitMinDate[2];
          
 	//setup styles to define the look of the calendar
	sHTML += '<style>';
	sHTML += '.calHeading {background-color : Silver; border-top-style: outset; font: 8px Verdana;}'
	sHTML += '.calRegularDay {background-color : #FFEFD5;}';
	sHTML += 'A.calRegularDay.HOVER {background-color : #E6E6FA;}';
	sHTML += '.calReg, TD.calReg:ACTIVE, TD.calReg:FOCUS, TD.calReg:HOVER, TD.calReg:VISITED {';
	sHTML += 'background-color : #E6E6FA; cursor: hand; 	display : block;}';
	sHTML += 'TD.calReg:VISITED {	background-color : Blue;} </style>';
 
	//start grid	
	sHTML += '<table cellspacing=0 border=0 cellpadding=0>';
	sHTML += '<tr class="calHeading">';
	sHTML += '<td width=1><img src="dot.gif" width=1 height=1></td>';
	sHTML += '<td width=200><img src="dot.gif" width=200 height=1></td>';
	sHTML += '<td width=1><img src="dot.gif" width=1 height=1></td></tr>';
	sHTML += '<tr class="heading"><td width=1><img src="dot.gif" width=1 height="100%"></td>';
	sHTML += '<td width=200>';
    
   sHTML += DrawCalendarMonth(lTargetYear, lTargetMonth, lTargetDay,lMinYear, lMinMonth, lMinDay);
	
	sHTML += '</tr></table></td><td width=1><img src="dot.gif" width=1 height="100%"></td>';
	sHTML += '</tr>';
   sHTML += '<tr>';
   sHTML += '<td width=1><img src="dot.gif" width=1 height=1></td>';
   sHTML += '<td width=200><img src="dot.gif" width=200 height=1></td>';
   sHTML += '<td width=1><img src="dot.gif" width=1 height=1></td>';
   sHTML += '</tr>'
   sHTML += '</table>';
    
   /*var spCal = document.getElementById("spCal");
   var span = document.createElement('span');
   
   span.innerHTML = sHTML;
   
   spCal.appendChild(span);*/
   
   self.iframe_calendario.document.open();
   self.iframe_calendario.document.write(sHTML);
   self.iframe_calendario.document.close();
}

function DrawCalendarMonth(lYear, lMonth, lDay, lMinYear, lMinMonth, lMinDay)
{
   var sCal = new String();
   var dcurrDate= new Date(lYear, lMonth, lDay);
   var dMinDate= new Date(lMinYear, lMinMonth, lMinDay);
   
   var monthArray = new Array('Janeiro', 'Fevereiro', 'Mar&ccedil;o', 'Abril', 'Maio', 'Junho', 'Julho', 'Agosto', 'Setembro', 'Outubro', 'Novembro', 'Dezembro');
   var weekdayList  = new Array('Domingo', '2東eira', '3東eira', '4東eira', '5東eira', '6東eira', 'S墎ado');
   var weekdayArray = new Array('D','S','T','Q','Q','S','S');
    
   var lDaysInMonth = GetDaysInMonth(dcurrDate.getMonth()+1, dcurrDate.getYear());
   var thisMonthFirstDay = new Date(dcurrDate.getYear(), dcurrDate.getMonth(), 1);

   var sThisYear  = (thisMonthFirstDay.getFullYear() )+ '/'  + Right('00'+(thisMonthFirstDay.getMonth()+ 1),2) + '/' + '01';
   var sThisMonth  = (thisMonthFirstDay.getFullYear() )+ '/' + Right('00'+(thisMonthFirstDay.getMonth()+ 1),2) + '/' + '01';
   
   var sNextYear = (thisMonthFirstDay.getFullYear() +1)+ '/' + Right('00'+(thisMonthFirstDay.getMonth()+ 1),2) + '/' + '01';
   var sPrevYear = (thisMonthFirstDay.getFullYear() -1)+ '/' + Right('00'+(thisMonthFirstDay.getMonth()+ 1),2) + '/' + '01';
   var sNextMonth = (thisMonthFirstDay.getFullYear() ) + '/' + Right('00'+(thisMonthFirstDay.getMonth()+ 2),2) + '/' + '01';
   var sPrevMonth =(thisMonthFirstDay.getFullYear() ) + '/'  + Right('00'+(thisMonthFirstDay.getMonth()+ 0),2) + '/' + '01';
    
   var sMinDate =lMinYear + '/' + Right('00' + lMinMonth,2)  + '/' +  Right('00' + lMinDay,2);
    
   sCal = '<table width=200 bordercolor="white" border=1 style="border-collapse:collapse">' + '<tr class="heading" bgcolor="beige">';
   if (sPrevYear<=sMinDate)
		sCal += '<td >&nbsp;</td> ';
	else
		sCal += '<td style="cursor:hand;" onmouseover="this.style.backgroundColor=\'white\';" onmouseout="this.style.backgroundColor=\'\';" onclick="javascript:parent.drawCal(\'' + sPrevYear +'\' , \'' + sMinDate +'\');" align=center><font face=verdana, arial size=2>&lt;&lt;</font></td>';
   
   if (sThisMonth<=sMinDate)
		sCal += '<td >&nbsp;</td> ';
   else
		sCal += '<td style="cursor:hand;" onmouseover="this.style.backgroundColor=\'white\';" onmouseout="this.style.backgroundColor=\'\';" onclick="javascript:parent.drawCal(\'' + sPrevMonth +'\', \'' + sMinDate +'\');" align=center><font face="verdana, arial" size=2>&lt;</font></td>';
   
   sCal += '<td colspan=3 align=center><font face="verdana, arial" size=1>' + monthArray[dcurrDate.getMonth()] + ' ' + dcurrDate.getFullYear() + '</font></td>';
   sCal += '<td style="cursor:hand;" onmouseover="this.style.backgroundColor=\'white\';" onmouseout="this.style.backgroundColor=\'\';" onclick="javascript:parent.drawCal(\'' + sNextMonth +'\', \'' + sMinDate +'\');" align=center><font face="verdana, arial" size=2>&gt;</font></td>';
   sCal += '<td style="cursor:hand;" onmouseover="this.style.backgroundColor=\'white\';" onmouseout="this.style.backgroundColor=\'\';" onclick="javascript:parent.drawCal(\'' + sNextYear +'\', \'' + sMinDate +'\');" align=center><font face="verdana, arial" size=2>&gt;&gt;</font></td>';
   sCal += '</tr>';
	weekdays = '<tr bgcolor="#aaaaaa">';
	
	// LOOP THROUGH WEEKDAY ARRAY
	for (i in weekdayArray) 
	{
		weekdays += '<td align=center><font face="verdana, arial" size=1>' + weekdayArray[i] +'</font></td>';
	}
	weekdays += '</TR><TR>';
 
	sCal += weekdays;
   
	for (i=0; i < thisMonthFirstDay.getDay(); i++)  
	{
		sCal += DrawOtherDay(thisMonthFirstDay.getDay() - i, dcurrDate);
	}
	
	for (i=1; i < lDaysInMonth+1; i++)  
	{
		dcurrDate.setDate(i);
		if ( thisMonthFirstDay.getMonth()==lMinMonth-1 && thisMonthFirstDay.getYear()==lMinYear)
			{	if (dcurrDate.getDate()<lMinDay)
					sCal += DrawMinDay( i, dcurrDate);
				else
					sCal += DrawNormalDay(i, dcurrDate);
			}
		else
			sCal += DrawNormalDay(i, dcurrDate);
			
		//start a new week if we are at the end
		if (dcurrDate.getDay() ==6) 
		{ 
			sCal +='</tr><tr>';      
    	}
	}
  
	return sCal;
}

function DrawMinDay(lDayOffset, dDate)
{
	var DayNumber;
	var dWorkingDate =  new Date(dDate.getFullYear() , (dDate.getMonth() ) , lDayOffset)  ;
	var sTemp = new String(); 

	DayNumber = dWorkingDate.getDate();
	 
	//' Draws a day cell - date is in previous or next month
	//' The response.write's are separate lines for clarity only
	sTemp = '<td bgcolor="#eeeeee" align=center>';
	sTemp += '<font face="verdana, arial" size=2 color="darkgray">';
	sTemp += DayNumber + '</font></td>';
	
	return sTemp;
}

function DrawOtherDay(lDayOffset, dDate)
{
	var DayNumber;
	var dWorkingDate = new Date(dDate.getYear(), dDate.getMonth(), (- lDayOffset) + 1) 
	var sTemp = new String(); 
	 
	DayNumber = dWorkingDate.getDate();
	 
	//' Draws a day cell - date is in previous or next month
	//' The response.write's are separate lines for clarity only
	sTemp = '<td bgcolor="#eeeeee" align=center>';
	sTemp += '<font face="verdana, arial" size=2 color="darkgray">';
	sTemp += DayNumber + '</font></td>';
	
	return sTemp;
}

function DrawNormalDay(DayNumber, dDate)
{
	//' Draws a day cell - date is in current month
	//' The response.write's are separate lines for clarity only
	var sDayDate = new String();
	var sMonthDate = new String();
	var sDate = new String();
	var sTemp = new String(); 

	if (DayNumber < 10)
		sDayDate = '0' + DayNumber;
	else
		sDayDate = DayNumber;
	
	if (dDate.getMonth() + 1 < 10)
		sMonthDate = '0' + (dDate.getMonth() + 1);
	else
		sMonthDate = dDate.getMonth() + 1;
	
	sDate = dDate.getFullYear() + '/' + sMonthDate + '/' + sDayDate;
	
	sTemp += '<td class="calReg" ';
	sTemp += 'onclick="javascript:parent.document.getElementById(\'' + TextBoxField.name + '\').value = (\'' + sDate + '\'); parent.HideCal();"';

	//sTemp += 'onmouseover="this.style.backgroundColor=\'white\';"';
	//sTemp += 'onmouseout="this.style.backgroundColor=\'#cccccc\';" ';

	sTemp += ' align=center>';
	sTemp += '<font face="verdana, arial" size=2><a href="#" onclick="void(\'' + sDate + '\');">' + DayNumber + '</a>';
	sTemp +='</font></td>';

	return sTemp;
}

function HideCal()
{
	iFrame.style.display = "none"
}

function GetNextMonth(dCurrentDate) 
{
	var lbmonth;
	var lbyear;
	
	// IF MONTH IS DECEMBER, SET MONTH TO JANUARY AND INCREMENT THE YEAR
   if (dCurrentDate.getMonth == 11) 
   {
      dCurrentDate.setMonth(0);
      dCurrentDate.setYear(dCurrentDate.getYear() + 1);
   }
   else 
   {
      dCurrentDate.setMonth(dCurrentDate.getMonth() + 1);
   }
   
   return dCurrentDate;
}

function GetDaysInMonth(iMonth, iYear) 
{
	//Returns the number of days in a month
	/*	In: iMonth: Number of the Month -not zero based ie 3=March
			 iYear: four digit year */
	var dPrevDate = new Date(iYear, iMonth, 0);

	return dPrevDate.getDate();
}

function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
