//EGS 09-12 nov 2007
var gCurSel = ""; //Global: current selection
var gTarget = ""; //Global: target field

Date.prototype.getWeek = function(){ //Extend Date with week-function
	var Jan01 = new Date(this.getFullYear(), 0, 1);
	return Math.ceil((((this - Jan01) / 86400000) + Jan01.getDay()) / 7);
}

Date.prototype.compare = function(Value){ //Extend Date with comparing
	if(this.getFullYear() == Value.getFullYear() && this.getMonth() == Value.getMonth() && this.getDate() == Value.getDate())
		return true;
	else
		return false;
}

function calendar(pTarget){ //Constructor
try{
	var Id = "cal"  + pTarget.id;
	if(!document.getElementById(Id) && gTarget == ""){ //Allow one instance per target and one instance in general
		gTarget = pTarget;
		var Pos = getPos(pTarget);
		var Cal = document.createElement("div");
		gCurSel = pTarget.value != "" ? parseDate(pTarget.value) : new Date(); //Selected date
		gCurSel = new Date(gCurSel.getFullYear(), gCurSel.getMonth(), gCurSel.getDate()); //Recreate without time
		var Month = gCurSel.getMonth() + 1;
		Month = Month < 10 ? "0" + Month : Month;
		Cal.id = Id;
		Cal.style.width = "160px";
		Cal.style.height = "130px";
		Cal.style.position = "absolute";
		Cal.style.left = Pos.Left + "px";
		Cal.style.top = (Pos.Top + Pos.Height + 1) + "px";
		Cal.style.border = "solid 1px";
		Cal.style.backgroundColor = "#FFFFFF";
		Cal.style.textAlign = "center";
		Cal.style.fontFamily = "Courier";
		Cal.style.fontSize = "12";
		var Body = "";
		Body += "<table border=\"0\" cellspacing=\"1\" cellpadding=\"0\" style=\"width:100%; height:100%;\">";
		Body += "<tr>";
		Body += "    <th colspan=\"8\">";
		Body += "      <a class=\"cal\" onclick=\"addMonth('" + Id + "', -12)\">&lt;</a>";
		Body += "      <b><span id=\"" + Id + "Year\">" + gCurSel.getFullYear() + "</span></b>";
		Body += "      <a class=\"cal\" onclick=\"addMonth('" + Id + "', 12)\">&gt;</a>";
		Body += "      <a class=\"cal\" onclick=\"addMonth('" + Id + "', -1)\">&lt;</a>";
		Body += "      <b><span id=\"" + Id + "Month\">" + Month + "</span></b>";
		Body += "      <a class=\"cal\" onclick=\"addMonth('" + Id + "', 1)\">&gt;</a>";
		Body += "      <b><a class=\"cal\" style=\"color:#FF1111;\" onclick=\"setClose('" + Id + "')\">X</a></b>";
		Body += "    </th>";
		Body += "</tr>";
		Body += "  <tr>";
		Body += "    <th class=\"calheader\">wk</th>";
		Body += "    <th class=\"calheader\">ma</th>";
		Body += "    <th class=\"calheader\">di</th>";
		Body += "    <th class=\"calheader\">wo</th>";
		Body += "    <th class=\"calheader\">do</th>";
		Body += "    <th class=\"calheader\">vr</th>";
		Body += "    <th class=\"calheader\">za</th>";
		Body += "    <th class=\"calheader\">zo</th>";
		Body += "  </tr>";
		for(var i = 0; i < 6; i++){
			Body += "  <tr>";
			Body += "    <th class=\"calheader\" id=\"" + Id + "Week" + i + "\">-</th>";
			for(var j = 0; j < 7; j++){
				Body += "    <td id=\"" + Id + "Day" + (i*7 + j) + "\">.</td>";
			}
			Body += "  </tr>";
		}
		Body += "</table>";
		Cal.innerHTML = Body;
		document.body.appendChild(Cal);
		setCalendar(Id, gCurSel);
	}
} catch(e) {}
}

function setClose(pId, pValue){ //Close calendar
	Obj = document.getElementById(pId);
	if(typeof pValue != "undefined"){ //Set selected value
		gTarget.value = pValue;
	}
	document.body.removeChild(Obj);
	gCurSel = "";
	gTarget = "";
}

function getPos(pObj){ //Get absolute position of object
	var Pos = new position();
	Pos.Width = pObj.clientWidth;
	Pos.Height = pObj.clientHeight;
	Pos.Left = pObj.offsetLeft;
	Pos.Top = pObj.offsetTop;
	Parent = pObj;
	while(Parent.offsetParent != null){
		Parent = Parent.offsetParent;
		Pos.Left += Parent.offsetLeft;
		Pos.Top += Parent.offsetTop;
	}
	return Pos;
}

function position(){ //Position constructor
	this.Left = 0;
	this.Top = 0;
	this.Width = 0;
	this.Height = 0;
}

function parseDate(Value){ //Custom date formatting-parsing
	var Parts = Value.split(/\W+/);
	if(Parts[0] && Parts[1] && Parts[2])
		return new Date(Parts[0], Parts[1]-1, Parts[2], 0, 0, 0);
	else
		return new Date();
}

function addMonth(pId, pNr){ //Add month(s) to target
	var ObjYear = document.getElementById(pId + "Year");
	var ObjMonth = document.getElementById(pId + "Month");
	var AddYears = pNr > 0 ? Math.floor(pNr / 12) : Math.ceil(pNr / 12);
	var AddMonths = pNr - AddYears*12;
	var Year = parseInt(ObjYear.innerHTML) + AddYears;
	var Month = parseInt(ObjMonth.innerHTML) + AddMonths;
	if(Month < 1){
		Month = 12;
		Year -= 1;
	}
	else if(Month > 12){
		Month = 1;
		Year += 1;
	}
	Month = Month < 10 ? "0" + Month : Month;
	ObjYear.innerHTML = Year;
	ObjMonth.innerHTML = Month;
	setCalendar(pId, new Date(Year, Month - 1, 1));
}

function setCalendar(pId, pDate){ //Write calendar month including date
try{
	var PrevLast = new Date(pDate.getFullYear(), pDate.getMonth(), 0); //Last day of previous month
	var First = new Date(pDate.getFullYear(), pDate.getMonth(), 1); //First day of month
	var Last = new Date(pDate.getFullYear(), pDate.getMonth() + 1, 0); //Last day of month
	
	//Week numbers
	var Week = First.getWeek();
	for(var i = 0; i < 6; i++){
		var Obj = document.getElementById(pId + "Week" + i);
		Obj.childNodes[0].data = Week + i;
	}
	
	//Days
	var Offset = First.getDay();
	Offset = Offset == 0 ? Offset = 6 : Offset - 1; //0-6 starting monday
	dp = PrevLast.getDate();
	df = First.getDate();
	dl = Last.getDate();
	var Day = 0;
	for(var i = 0; i < 42; i++){ //7 x 6
		var Obj = document.getElementById(pId + "Day" + i);
		var Year = pDate.getFullYear();
		var Month = pDate.getMonth() + 1;
		if(i < Offset){ //Before shown month
			Month -= 1;
			Day = dp - Offset + i + 1;
			Obj.className = "calother";
		}
		else if(i - Offset > dl - 1){ //After shown month
			Month += 1;
			Day = i - dl - Offset + 1;
			Obj.className = "calother";
		}
		else{ //Shown month
			Day = i - Offset + 1;
			Obj.className = gCurSel.compare(new Date(Year, Month-1, Day)) ? "calselected" : "calmonth";
		}
		//Correct month and year
		if(Month < 1){
			Month = 12;
			Year -= 1;
		}
		else if(Month > 12){
			Month = 1;
			Year += 1;
		}
		DatStr = Year + "-" + (Month < 10 ? "0" + Month : Month) + "-" + (Day < 10 ? "0" + Day : Day); //Format
		Obj.innerHTML = "<a class=\"cal\" onclick=\"setClose('" + pId + "','" + DatStr + "')\">" + Day + "</a>";
	}
} catch(e) {}
}