var _CAL_IS_IE = (navigator.userAgent.toLowerCase().indexOf("msie") > -1) ? true : false;

var jscalcallerfrm = "";
var jscalcallerel = "";
var jscalalignment = "";

function jscalbuild(yy, mm) {
	d = new Date(yy, mm, 1);
	s = "";
	
	oldmonth = d.getMonth();
	oldyear = d.getRealYear();
	
	firstweekcnt = 0;
	firstweek = false;
	
	i = 1;
	
	while(oldmonth == d.getMonth()) {
		if (!firstweek) firstweekcnt++;
		
		if (d.getDay() == 1 && i > 1) {
			s += "</tr><tr>";
			firstweek = true;
		}
		
		if (d.getDay() == 0 || d.getDay() == 6) {
			bgtag = "style=\"background-color: #f8f8f0;\" ";
		} else {
			bgtag = "";
		}
		s += "<td " + bgtag + "align=\"center\" onMouseUp=\"jscalcellclick(this)\"><a href=\"#\" onClick=\"return jscalsetdate('" + (d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getRealYear()) + "')\">" + d.getDate() + "</a></td>";
		d.setDate(++i);
	}
	
	if (firstweekcnt == 8) firstweekcnt = 0;
	
	if (firstweekcnt > 1) {
		for (i = 0; i <  8 - firstweekcnt; i++) {
			s = "<td></td>" + s;
		}
	}
	
	nextmonth = d.getMonth();
	nextyear = d.getRealYear();
	d.setYear(oldyear);
	d.setMonth(oldmonth - 1);
	prevmonth = d.getMonth();
	prevyear = d.getRealYear();
	
	s = "<table class=\"cal\" width=\"100%\" cellspacing=\"1\" cellpadding=\"1\" border=\"0\"><tr><th>Mn</th><th>Tu</th><th>Wn</th><th>Th</th><th>Fr</th><th>Sa</th><th>Su</th></tr><tr>" + s + "</tr></table>";
	t = "<table border=\"0\" cellspacing=\"0\" cellpadding=\"1\" width=\"100%\" class=\"cal\"><tr>";
	if (jscalalignment=='right')
		t += "<td valign=\"top\" width=\"10\"><a href=\"#\" onClick=\"return jscalhide()\"><img src=\"/img/calico.gif\" border=\"0\"/></a></td>";
	t += "<td>&nbsp;<a title=\"Month Back\" style=\"text-decoration: none;\" href=\"#\" onMouseUp=\"return jscalrecalc(" + prevyear + ", " + prevmonth + ")\">&#9668;</a>&nbsp;</td>";
	t += "<td width=\"100%\" align=\"center\">" + (mm + 1) + "&nbsp;/&nbsp;" + yy + "</td>";
	t += "<td align=\"right\"><a title=\"Month Forward\" style=\"text-decoration: none;\" href=\"#\" onMouseUp=\"return jscalrecalc(" + nextyear + ", " + nextmonth + ")\">&#9658;</a></td>";
	if (jscalalignment=='left')
		t += "<td valign=\"top\" width=\"10\"><a href=\"#\" onClick=\"return jscalhide()\"><img src=\"/img/calico.gif\" border=\"0\"/></a></td>";
	t += "</tr></table>";
	
	return t + s;
}

function jscalsetdate(dt) {
	document.forms[callerfrm].elements[callerel].value = dt;
	jscalhide();
	return false;
}

function jscalcellclick(a) {
	a.firstChild.onclick();
	return false;
}

function jscalshow(w, targetfrm, targetel) {
	if (!w) w = event.srcElement;
	
	var tmpelwidth = w.offsetWidth;
	
	for ( var posl = 0, post = 0; w.style.position != 'absolute' && w.style.position != 'relative' && w.offsetParent; w = w.offsetParent ) {
		posl += w.offsetLeft; post += w.offsetTop;
	}
	
	post -= 3;
	posl -= 3;

	w = document.body.clientWidth;
	
	jscalalignment = "right";
	
	if (w - posl < 151) {
		jscalalignment = "left";
		posl -= 150 - tmpelwidth - 5;
	}

	var c = document.getElementById("calid");

	var ddd = new Date();
	c.innerHTML = jscalbuild(ddd.getRealYear(), ddd.getMonth());

	c.style.top = (post - 2) + 'px';
	c.style.left = (posl - 2) + 'px';
	c.style.display = "block";
	
	if (_CAL_IS_IE) {
		var fm = document.getElementById("calidfrm");
		fm.style.top = (post - 2) + 'px';
		fm.style.left = (posl - 2) + 'px';
		fm.style.width = c.offsetWidth;
		fm.style.height = c.offsetHeight;
		fm.style.display = "block";
	}
	
	callerfrm = targetfrm;
	callerel = targetel;
	
	return false;
}

function jscalhide() {
	c = document.getElementById("calid");
	c.style.display = "none";
	if (_CAL_IS_IE) {
		fm = document.getElementById("calidfrm");
		fm.style.display = "none";
	}
	
	return false;
}

function jscalrecalc(yy, mm) {
	c = document.getElementById("calid");
	c.innerHTML = jscalbuild(yy, mm);
	
	if (_CAL_IS_IE) {
		fm = document.getElementById("calidfrm");
		fm.style.height = c.offsetHeight;
	}
	
	return false;
}

// window.onload = jscalcreateparentel;

// tva e za da se create-va dinamichno - za momenta ne go izpolzvame
function jscalcreateparentel() {
	// div: z-index: 2; display: none; position: absolute; width: 150px; height: auto; background-color: White;
	// iframe: z-index: 1; display: none; position: absolute; left: 0px; top: 0px;
	if (_CAL_IS_IE) {
		var oFrm=document.createElement("IFRAME");
		document.body.appendChild(oFrm);
		oFrm.id = "calidfrm";
		oFrm.src = "about:blank";
		oFrm.border = 0;
		oFrm.frameBorder = 0;
		oFrm.style.position = "absolute";
		oFrm.style.zIndex = 1;
		oFrm.style.display = "none";
		oFrm.style.width = "150px";
		oFrm.style.height = "auto";
		oFrm.style.backgroundColor = "White";
		oFrm.style.left = "0px";
		oFrm.style.top = "0px";
	}
	
	var oDiv=document.createElement("DIV");
	document.body.appendChild(oDiv);
	oDiv.id = "calid";
	oDiv.style.position = "absolute";
	oDiv.style.zIndex = 2;
	oDiv.style.display = "none";
	oDiv.style.width = "150px";
	oDiv.style.height = "auto";
	oDiv.style.backgroundColor = "White";
}

// tva e shoto getYear pod vsichki browseri vrushta razlichni neshta
Date.prototype.getRealYear = function() {
	var lyear = this.getYear();
	if (lyear < 1500) lyear += 1900; // deba i skapanite browseri
	return lyear;
}

