function Button(value, href){
	this.create = function(){
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		var leftTd = document.createElement("td");
		var middleTd = document.createElement("td");
		var rightTd = document.createElement("td");
		var a = document.createElement("a");
		var text = document.createTextNode(value);
		
		a.href = href;
		a.style.color = "white";
		a.style.textDecoration = "none";
		
		table.style.height = "22px";
		
		leftTd.style.width = "7px";
		leftTd.style.backgroundImage = "url('images/left-button.gif')";
		
		middleTd.style.textAlign = "center";
		middleTd.style.backgroundImage = "url('images/ground-button.gif')";
		middleTd.style.backgroundRepeat = "repeat-x";
		middleTd.style.fontWeight = "bold";
		
		rightTd.style.width = "7px";
		rightTd.style.backgroundImage = "url('images/right-button.gif')";
		
		a.appendChild(text);
		middleTd.appendChild(a);
		tr.appendChild(leftTd);
		tr.appendChild(middleTd);
		tr.appendChild(rightTd);
		tbody.appendChild(tr);
		table.appendChild(tbody);
		
		return table;
	}
}
function Submit(value, form){
	this.create = function(){
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		var leftTd = document.createElement("td");
		var middleTd = document.createElement("td");
		var rightTd = document.createElement("td");
		var a = document.createElement("a");
		var text = document.createTextNode(value);
		
		a.href = "javascript: document.getElementById('" + form + "').submit();";
		a.style.color = "white";
		a.style.textDecoration = "none";
		
		table.style.height = "22px";
		
		leftTd.style.width = "7px";
		leftTd.style.backgroundImage = "url('images/left-button.gif')";
		
		middleTd.style.textAlign = "center";
		middleTd.style.backgroundImage = "url('images/ground-button.gif')";
		middleTd.style.backgroundRepeat = "repeat-x";
		middleTd.style.fontWeight = "bold";
		
		rightTd.style.width = "7px";
		rightTd.style.backgroundImage = "url('images/right-button.gif')";
		
		a.appendChild(text);
		middleTd.appendChild(a);
		tr.appendChild(leftTd);
		tr.appendChild(middleTd);
		tr.appendChild(rightTd);
		tbody.appendChild(tr);
		table.appendChild(tbody);
		
		return table;
	}
}
function Input(name, value, width){
	this.create = function(){
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		var leftTd = document.createElement("td");
		var middleTd = document.createElement("td");
		var rightTd = document.createElement("td");
		var input = document.createElement("input");
		var hidden = document.createElement("input");
		
		hidden.name = name;
        hidden.id = name;
		hidden.type = "hidden";
		hidden.setAttribute("value", value);
		
		table.style.height = "20px";
		
		input.type = "text";
		input.setAttribute("value", value);
		input.style.height = "14px";
		input.style.border = "0px";
		input.style.fontSize = "10px";
		input.style.width = width + "px";
		input.onkeyup = function(){
			hidden.value = input.value;
		};
		
		leftTd.style.width = "3px";
		leftTd.style.backgroundImage = "url('images/left-input.gif')";
		
		middleTd.style.width = width + "px";
		middleTd.style.backgroundImage = "url('images/ground-input.gif')";
		middleTd.style.backgroundRepeat = "repeat-x";
		
		rightTd.style.width = "3px";
		rightTd.style.backgroundImage = "url('images/right-input.gif')";
		
		middleTd.appendChild(input);
		middleTd.appendChild(hidden);
		tr.appendChild(leftTd);
		tr.appendChild(middleTd);
		tr.appendChild(rightTd);
		tbody.appendChild(tr);
		table.appendChild(tbody);
		
		return table;
	}
}
function Select(name, options, values, selected, width){
	this.create = function(){
		for(var index = 0; index < options.length; index++){
			var current = options[index].length * 6;
			width = current > width ? current : width;
		}
		
		var height = options.length < 8 ? options.length : 8;
		height *= 16;
		
		var container = document.createElement("div");
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		var leftTd = document.createElement("td");
		var middleTd = document.createElement("td");
		var rightTd = document.createElement("td");
		var div = document.createElement("div");
		var hidden = document.createElement("input");
		
		hidden.name = name;
        hidden.id = name;
		hidden.type = "hidden";
		hidden.setAttribute("value", values[selected]);
		
		table.style.height = "20px";
		table.style.cursor = "pointer";
		
		div.style.border = "0px";
		div.style.padding = "2px 0px 1px 1px";
		div.style.fontSize = "10px";
		div.style.width = width + "px";
		
		
		var dropdown = document.createElement("div");
		dropdown.style.position = "absolute";
		dropdown.style.zIndex = "2";
		dropdown.style.display = "none";
		
		var select = document.createElement("select");
		select.style.width = width + (options.length < 8 ? 0 : 18) + "px";
		select.style.height = height + "px";
		select.size = options.length < 8 ? options.length : 8;
		select.style.border = "1px solid gray";
		select.style.fontSize = "10px";
		select.onchange = function(){
			dropdown.style.display = "none";
			var text = document.createTextNode(options[select.selectedIndex]);
			div.innerHTML="";
			div.appendChild(text);
            document.getElementById(name).value = select.value;
		};
		
		for(var index = 0; index < options.length; index++){
			var option = document.createElement("option");
			var text = document.createTextNode(options[index]);
			option.value = values[index];
			option.appendChild(text);
			select.appendChild(option);
		}
		dropdown.appendChild(select);
		
		leftTd.style.width = "3px";
		leftTd.style.backgroundImage = "url('images/left-input.gif')";
		
		middleTd.style.width = width + "px";
		middleTd.style.backgroundImage = "url('images/ground-input.gif')";
		middleTd.style.backgroundRepeat = "repeat-x";
		
		rightTd.style.width = "18px";
		rightTd.style.backgroundImage = "url('images/right-select.gif')";
		table.onmousedown = function (){
			if(dropdown.style.display == "none"){
				var bound = getBound(div);
				dropdown.style.top = bound["top"] + bound["height"] + 2 + "px";
				dropdown.style.left = bound["left"] - 1 + "px";
				dropdown.style.display = "block";
			}else dropdown.style.display = "none";
		};
		
		var text = document.createTextNode(options[selected]);
		div.appendChild(text);
		middleTd.appendChild(div);
		middleTd.appendChild(hidden);
		tr.appendChild(leftTd);
		tr.appendChild(middleTd);
		tr.appendChild(rightTd);
		tbody.appendChild(tr);
		table.appendChild(tbody);
		container.appendChild(table);
		container.appendChild(dropdown);
		
		return container;
	}
	function getBound(object){
		var current = object;  
		var left = 0, top = 0;  
		while(current){  
			left += current.offsetLeft;  
			top += current.offsetTop;  
			current = current.offsetParent;  
		}
		var width = object.offsetWidth;
		var height = object.offsetHeight;
		return {"left":left, "top":top, "width":width, "height":height};
	}
}
function Calendar(name, from, to){
	var config = new Array();
	config["monthes"] = new Array();
	config["monthes"][1] = "Gennaio";
	config["monthes"][2] = "Febbraio";
	config["monthes"][3] = "Marzo";
	config["monthes"][4] = "Aprile";
	config["monthes"][5] = "Maggio";
	config["monthes"][6] = "Giugno";
	config["monthes"][7] = "Luglio";
	config["monthes"][8] = "Agosto";
	config["monthes"][9] = "Settembre";
	config["monthes"][10] = "Ottobre";
	config["monthes"][11] = "Novembre";
	config["monthes"][12] = "Decembre";

	config["day"] = new Array();
	config["day"][1] = "Lu";
	config["day"][2] = "Ma";
	config["day"][3] = "Me";
	config["day"][4] = "Gi";
	config["day"][5] = "Ve";
	config["day"][6] = "Sa";
	config["day"][7] = "Do";

	var date = new Date();
	var monthCalendar, yearCalendar, cell, target, info, hidden, dropdown;
	

	this.create = function(){
		var container = document.createElement("div");
		var table = document.createElement("table");
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		var leftTd = document.createElement("td");
		var middleTd = document.createElement("td");
		var rightTd = document.createElement("td");
		target = document.createElement("div");
		hidden = document.createElement("input");
		
		hidden.name = name;
        hidden.id = name;
		hidden.type = "hidden";
		hidden.setAttribute("value", date.getFullYear() + "-" + ((date.getMonth() + 1) > 9 ? "" : "0") + (date.getMonth() + 1) + "-" + ((date.getDate()) > 9 ? "" : "0") + date.getDate());
		
		table.style.height = "20px";
		table.style.cursor = "pointer";
		
		target.style.border = "0px";
		target.style.padding = "2px 0px 1px 1px";
		target.style.fontSize = "10px";
		target.style.width = "100px";
		
		
		dropdown = document.createElement("div");
		dropdown.style.position = "absolute";
		dropdown.style.zIndex = "2";
		dropdown.style.display = "none";
		
		var tableCalendar = document.createElement("table");
		var tbodyCalendar = document.createElement("tbody");
		var trCalendar = document.createElement("tr");
		var tdCalendar = document.createElement("td");
		
		tableCalendar.style.cursor = "pointer";
		tableCalendar.style.backgroundColor = "white";
		tableCalendar.style.border = "1px solid gray";
		
		monthCalendar = document.createElement("select");
		monthCalendar.style.fontSize = "10px";
		monthCalendar.onchange = update;
		for(var monthIndex = 1; monthIndex <= 12; monthIndex++){
			var optionCalendar = document.createElement("option");
			var nodeCalendar = document.createTextNode(config["monthes"][monthIndex]);
			optionCalendar.appendChild(nodeCalendar);
			optionCalendar.value = monthIndex;
			monthCalendar.appendChild(optionCalendar);
		}
		monthCalendar.selectedIndex = date.getMonth();
		tdCalendar.appendChild(monthCalendar);
		trCalendar.appendChild(tdCalendar);
		
		yearCalendar = document.createElement("select");
		yearCalendar.style.fontSize = "10px";
		yearCalendar.onchange = update;
		for(var yearIndex = from; yearIndex <= to; yearIndex++){
			var optionCalendar = document.createElement("option");
			var nodeCalendar = document.createTextNode(yearIndex);
			optionCalendar.appendChild(nodeCalendar);
			optionCalendar.value = yearIndex;
			yearCalendar.appendChild(optionCalendar);
		}
		yearCalendar.selectedIndex = date.getFullYear() - from;
		
		tdCalendar.appendChild(yearCalendar);
		trCalendar.appendChild(tdCalendar);
		tbodyCalendar.appendChild(trCalendar);
		trCalendar = document.createElement("tr");
		cell = document.createElement("td");
		trCalendar.appendChild(cell);
		tbodyCalendar.appendChild(trCalendar);
		trCalendar = document.createElement("tr");
		info = document.createElement("td");
		info.style.border = "1px solid gray";
		info.style.textAlign = "center";
		info.style.verticalAlign = "middle";
		info.style.height = "20px";
		info.style.fontSize = "10px";
		trCalendar.appendChild(info);
		tbodyCalendar.appendChild(trCalendar);
		tableCalendar.appendChild(tbodyCalendar);
		dropdown.appendChild(tableCalendar);
		
		leftTd.style.width = "3px";
		leftTd.style.backgroundImage = "url('images/left-input.gif')";
		
		middleTd.style.width = "100px";
		middleTd.style.backgroundImage = "url('images/ground-input.gif')";
		middleTd.style.backgroundRepeat = "repeat-x";
		
		rightTd.style.width = "18px";
		rightTd.style.backgroundImage = "url('images/right-calendar.gif')";
		table.onmousedown = function (){
			if(dropdown.style.display == "none"){
				var bound = getBound(target);
				dropdown.style.top = bound["top"] + bound["height"] + 3 + "px";
				dropdown.style.left = bound["left"] - 3 + "px";
				dropdown.style.display = "block";
			}else dropdown.style.display = "none";
		};
		
		var text = document.createTextNode(date.getDate() + " " + config["monthes"][(date.getMonth() + 1)] + " " + date.getFullYear());
		target.appendChild(text);
		middleTd.appendChild(target);
		middleTd.appendChild(hidden);
		tr.appendChild(leftTd);
		tr.appendChild(middleTd);
		tr.appendChild(rightTd);
		tbody.appendChild(tr);
		table.appendChild(tbody);
		container.appendChild(table);
		container.appendChild(dropdown);
		
		update();
		
		return container;
	}
	
	function update(){
		var daysInMonth = new Date(parseInt(yearCalendar.value), parseInt(monthCalendar.value), 0);
		daysInMonth = daysInMonth.getDate();
		var firstDay = new Date(parseInt(yearCalendar.value), parseInt(monthCalendar.value) - 1, 1);
		firstDay = firstDay.getDay();
		if(firstDay < 1) firstDay = 7;
		var currentDay = date.getDate();
		var numberOfWeeks = Math.ceil((firstDay + daysInMonth - 1) / 7);
		var table = document.createElement("table");
		table.style.margin = "3px";
		var tbody = document.createElement("tbody");
		var tr = document.createElement("tr");
		tr.style.backgroundColor = "#b8d3ef";
		for(var dayIndex = 1; dayIndex <= 7; dayIndex++){
			var node = document.createTextNode(config["day"][dayIndex]);
			var td = document.createElement("td");
			td.style.width = "18px";
			td.style.height = "18px";
			td.style.textAlign = "center";
			td.style.verticalAlign = "middle";
			td.style.fontSize = "10px";
			if(dayIndex > 5) td.style.color = "#d25656";
			td.appendChild(node);
			tr.appendChild(td);
		}
		tbody.appendChild(tr);
		var dayCounter = 1;
		var cellCounter = 1;
		for(var week = 1; week <= numberOfWeeks; week++){
			var tr = document.createElement("tr");
			for(var dayIndex = 1; dayIndex <= 7; dayIndex++){
				var td = document.createElement("td");
				if(cellCounter >= firstDay && cellCounter <= (daysInMonth + firstDay - 1)){
					var node = document.createTextNode(dayCounter);
					if(dayCounter == currentDay && parseInt(monthCalendar.value) == (date.getMonth() + 1) && parseInt(yearCalendar.value) == date.getFullYear()){
						td.style.fontSize = "11px";
						td.style.fontWeight = "bold";
					}else td.style.fontSize = "10px";
					if(dayIndex > 5) td.style.color = "#d25656";
					td.onclick = select;
					td.onmouseover = moveover;
					td.id = dayCounter;
					td.appendChild(node);
					td.style.width = "18px";
					td.style.height = "18px";
					td.style.textAlign = "center";
					td.style.verticalAlign = "middle";
					dayCounter++;
				}else{
					var node = document.createTextNode("");
					td.appendChild(node);
				}
				tr.appendChild(td);
				tbody.appendChild(tr);
				cellCounter++;
			}
		}
		table.appendChild(tbody);
		cell.innerHTML = "";
		cell.appendChild(table);
	}
	function select(event){
		var dayCalendar;
		if(event) dayCalendar = event.target.id;
    	else dayCalendar = window.event.srcElement.id;
        document.getElementById(name).value = yearCalendar.value + "-" + (monthCalendar.value.length == 2 ? "" : "0") + monthCalendar.value + "-" + (dayCalendar.length == 2 ? "" : "0") + dayCalendar;
    	target.innerHTML = dayCalendar + " " + config["monthes"][monthCalendar.value] + " " + yearCalendar.value;
    	dropdown.style.display = "none";
	}
	function moveover(event){
		var dayCalendar;
		if(event) dayCalendar = event.target.id;
    	else dayCalendar = window.event.srcElement.id;
    	var result = dayCalendar + " " + config["monthes"][monthCalendar.value] + " " + yearCalendar.value;
    	var node = document.createTextNode(result);
    	info.innerHTML = "";
    	info.appendChild(node);
	}
	function getBound(object){
		var current = object;  
		var left = 0, top = 0;  
		while(current){  
			left += current.offsetLeft;  
			top += current.offsetTop;  
			current = current.offsetParent;  
		}
		var width = object.offsetWidth;
		var height = object.offsetHeight;
		return {"left":left, "top":top, "width":width, "height":height};
	}
}