// codi del calendari 
function yformat(yyr)
{
	var b = "";
	for(a=0; a < 4; a++)
	{
		b = b + yyr.substr(a, 1) + " ";
	}
	return b;
}

function dlookup(mm, yy)
{
	var response=31;
	if (mm<8 && mm/2==Math.round(mm/2))
	{
		response=30;
	}
	
	if (mm>7 && mm/2!=Math.round(mm/2))
	{
		response=30;
	}
	
	if(mm==2)
	{
		if (!leapyear(yy)) 
		{
			response=28;
		}
		else
		{
			response=29; 
		}
	}
	return response;
}

function leapyear(year)
{
	return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
}

function namedate2(mmddyy)
{
	ddate=mmddyy;
	yys=ddate.substr(1,4);
	mms=ddate.substr(5,2);
	dds=ddate.substr(7,2) * 1;
	ndate=mlookup2(mms) + " " + dds + ", " + yys;
	return ndate;
}

function init()
{
	var now = new Date();
	document.getElementById("mm").value=month(now.getMonth());
	document.getElementById("yy").value=year(now.getYear());
	document.getElementById("dd").value=day(now.getDate());
	pcalendar();
}

function pcalendar()
{
	mnt=document.getElementById("mm").value;
	yyr=document.getElementById("yy").value;
	ddy=document.getElementById("dd").value;
	var date = new Date();
	date.setDate(1);
	date.setMonth(parseInt(mnt)-1);
	date.setFullYear(yyr);
	fday= date.getDay();
	lday=dlookup(mnt, yyr)
	innerHTML="<a class='tdy' href='javascript: showPCmonth()'>&nbsp; " + mlookup(mnt) +  " &nbsp;&nbsp; " + yformat(yyr) + "&nbsp;</a>";
	document.getElementById("my").innerHTML=innerHTML;

	innerHTML="<a class='tdy' href='javascript: hidePCmonth()'>&nbsp;" + yformat(yyr) + "&nbsp;</a>"
	document.getElementById("y").innerHTML=innerHTML;
	var c = 0;
	var d = 0;
	for(a=1; a<=6; a++)				// six weeks
	{
		for(b=1; b<=7; b++)			// seven days
		{
			c++;
			if(c>=fday)
			{
				d++
			}
			else 	
			{
				d=0;
			}
			
			//targetId=a & b
			targetId=a + "" + b;
			if (d<=lday)
			{
				var now = new Date();
				if (d==now.getDate() && mnt==now.getMonth() && yyr==now.getYear())
				{
					innerHTML="<a class='tdz' ";
				}
				else
				{
					innerHTML="<a class='cal' ";
				}
				innerHTML = innerHTML + "href='javascript: outday(" + d + ")'>&nbsp;" + d + "&nbsp;</a>";
				if(d==0) 
				{
					innerHTML="";
				}
				document.getElementById(targetId).innerHTML=innerHTML;
			}
			else
			{
				document.getElementById(targetId).innerHTML="";
			}
		}
		fday=1;
	}	
}

function prevyr()
{
	showPCmonth();
	document.getElementById("yy").value=parseInt(document.getElementById("yy").value)-1;
	pcalendar();
}

function nextyr()
{
	showPCmonth();
	document.getElementById("yy").value=parseInt(document.getElementById("yy").value)+1;
	pcalendar();
}

function prevmo()
{
	showPCal();
	document.getElementById("mm").value=parseInt(document.getElementById("mm").value)-1;
	if(parseInt(document.getElementById("mm").value)==0)
	{
		document.getElementById("mm").value=12;
		document.getElementById("yy").value=parseInt(document.getElementById("yy").value)-1;
	}
	pcalendar();
}

function nextmo()
{
	showPCal();
	document.getElementById("mm").value=parseInt(document.getElementById("mm").value)+1;
	if(document.getElementById("mm").value==13)
	{
		document.getElementById("mm").value=1;
		document.getElementById("yy").value=parseInt(document.getElementById("yy").value)+1;
	}
	pcalendar();
}

function outday(dd)
{
	mms=document.getElementById("mm").value;
	yys=document.getElementById("yy").value;
	dds=document.getElementById("dd").value;

	dout=formatNumber(dd, "00") + "/" + formatNumber(mms, "00") + "/" + yys;

	dtgo=document.getElementById("cinput").value;
	document.getElementById(dtgo).value=dout;
	
	document.getElementById(document.getElementById("cinput1").value).value=dd;
	document.getElementById(document.getElementById("cinput2").value).selectedIndex=mms;
	document.getElementById(document.getElementById("cinput3").value).value=yys;
		
	document.getElementById(document.getElementById("cinput3").value).focus();
	
	hidePCal();
	hideBase();
}

function showPCalE(ddd,dd,dm,dy) 
{
	ddate=document.getElementById(ddd).value;
	yys=document.getElementById(ddd).value.substr(6, 4);
	mms=document.getElementById(ddd).value.substr(3, 2);
	dds=document.getElementById(ddd).value.substr(0, 2);
	
	// Sentencia de decisión para el seteo del mes sin el 0.
	if (mms.substr(0, 1)=='0') mms=mms.substr(1);

	document.getElementById("yy").value=yys;
	document.getElementById("mm").value=mms;
	pcalendar();

	showPCal();
	document.getElementById("cinput").value=ddd;
	document.getElementById("cinput1").value=dd;
	document.getElementById("cinput2").value=dm;
	document.getElementById("cinput3").value=dy;
}

function cmonth(mnt)
{
	document.getElementById("mm").value=mnt;
	hidePCmonth();
	pcalendar();
}

function showPCmonth()
{
	document.getElementById("pcmonth").style.visibility="visible";
}

function hidePCmonth()
{
	showPCal();
	document.getElementById("pcmonth").style.visibility="hidden";
}

function showPCal()
{
	document.getElementById("ppcalendar").style.visibility="visible";
	showBase();
}

function hidePCal()
{
	document.getElementById("ppcalendar").style.visibility="hidden";
	document.getElementById("pcmonth").style.visibility="hidden";
	hideBase();
}

function showBase()
{
	document.getElementById("base").style.visibility="visible";
}

function hideBase()
{
	document.getElementById("base").style.visibility="hidden";
}

// Original JavaScript code by Duncan Crombie: dcrombie@chirp.com.au
// Please acknowledge use of this code by including this header.

// CONSTANTS
var separator = ",";  // use comma as 000's separator
var decpoint = ".";  // use period as decimal point
var percent = "%";
var currency = "$";  // use dollar sign for currency

function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
  if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

  if (number - 0 != number) return null;  // if number is NaN return null
  var useSeparator = format.indexOf(separator) != -1;  // use separators in number
  var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
  var useCurrency = format.indexOf(currency) != -1;  // use currency format
  var isNegative = (number < 0);
  number = Math.abs (number);
  if (usePercent) number *= 100;
  format = strip(format, separator + percent + currency);  // remove key characters
  number = "" + number;  // convert number input to string

   // split input value into LHS and RHS using decpoint as divider
  var dec = number.indexOf(decpoint) != -1;
  var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
  var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

   // split format string into LHS and RHS using decpoint as divider
  dec = format.indexOf(decpoint) != -1;
  var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
  var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

   // adjust decimal places by cropping or adding zeros to LHS of number
  if (srightEnd.length < nrightEnd.length) {
    var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
    nrightEnd = nrightEnd.substring(0, srightEnd.length);
    if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

// patch provided by Patti Marcoux 1999/08/06
    while (srightEnd.length > nrightEnd.length) {
      nrightEnd = "0" + nrightEnd;
    }

    if (srightEnd.length < nrightEnd.length) {
      nrightEnd = nrightEnd.substring(1);
      nleftEnd = (nleftEnd - 0) + 1;
    }
  } else {
    for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
      if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
      else break;
    }
  }

   // adjust leading zeros
  sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
  while (sleftEnd.length > nleftEnd.length) {
    nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
  }

  if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
  var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
  output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
  if (isNegative) {
    // patch suggested by Tom Denn 25/4/2001
    output = (useCurrency) ? "(" + output + ")" : "-" + output;
  }
  return output;
}

function strip(input, chars) {  // strip all characters in 'chars' from input
  var output = "";  // initialise output string
  for (var i=0; i < input.length; i++)
    if (chars.indexOf(input.charAt(i)) == -1)
      output += input.charAt(i);
  return output;
}

function separate(input, separator) {  // format input using 'separator' to mark 000's
  input = "" + input;
  var output = "";  // initialise output string
  for (var i=0; i < input.length; i++) {
    if (i != 0 && (input.length - i) % 3 == 0) output += separator;
    output += input.charAt(i);
  }
  return output;
}
