

var dDate = new Date();
var dCurMonth = dDate.getMonth();
var dCurDayOfMonth = dDate.getDate();
var dCurYear = dDate.getFullYear();
var objPrevElement = new Object();
var selezionato = null;

function fToggleColor(myElement) {
	var toggleColor = "#ff0000";
	
	if (myElement.id == "calDateText") {
	if (myElement.color == toggleColor) {myElement.color = "";} else {myElement.color = toggleColor;}
	} else if (myElement.id == "calCell") {
	for (var i in myElement.children) {
	if (myElement.children[i].id == "calDateText") {
	if (myElement.children[i].color == toggleColor) {myElement.children[i].color = "";} else {myElement.children[i].color = toggleColor;}}}}
}

function fSetSelectedDay(nome,myElement){
	//alert(myElement.id);
	if (myElement.id == "calCell") {
	if (!isNaN(parseInt(myElement.children["calDateText"].innerText))) {
	myElement.bgColor = "#c0c0c0";
	objPrevElement.bgColor = "";
	document.form1[selezionato].value = parseInt(myElement.children["calDateText"].innerText) + "/" + frmCalendarSample.tbSelMonth.options[frmCalendarSample.tbSelMonth.selectedIndex].value + "/" + frmCalendarSample.tbSelYear.options[frmCalendarSample.tbSelYear.selectedIndex].value;
	objPrevElement = myElement;
	MM_showHideLayers('calext','','hide')
	}}
}

function fGetDaysInMonth(iMonth, iYear) {
	var dPrevDate = new Date(iYear, iMonth, 0);
	return dPrevDate.getDate();
}

function fBuildCal(iYear, iMonth, iDayStyle) {
	var aMonth = new Array();
	aMonth[0] = new Array(7);
	aMonth[1] = new Array(7);
	aMonth[2] = new Array(7);
	aMonth[3] = new Array(7);
	aMonth[4] = new Array(7);
	aMonth[5] = new Array(7);
	aMonth[6] = new Array(7);
	var dCalDate = new Date(iYear, iMonth-1, 1);
	var iDayOfFirst = dCalDate.getDay();
	var iDaysInMonth = fGetDaysInMonth(iMonth, iYear);
	var iVarDate = 1;
	var i, d, w;
	if (iDayStyle == 2) {
	aMonth[0][0] = "Domenica";
	aMonth[0][1] = "Lunedì";
	aMonth[0][2] = "Martedì";
	aMonth[0][3] = "Mercoledì";
	aMonth[0][4] = "Giovedì";
	aMonth[0][5] = "Venerdì";
	aMonth[0][6] = "Sabato";
	} else if (iDayStyle == 1) {
	aMonth[0][0] = "Dom";
	aMonth[0][1] = "Lun";
	aMonth[0][2] = "Mar";
	aMonth[0][3] = "Mer";
	aMonth[0][4] = "Gio";
	aMonth[0][5] = "Ven";
	aMonth[0][6] = "Sab";
	} else {
	aMonth[0][0] = "Do";
	aMonth[0][1] = "Lu";
	aMonth[0][2] = "Ma";
	aMonth[0][3] = "Me";
	aMonth[0][4] = "Gi";
	aMonth[0][5] = "Ve";
	aMonth[0][6] = "Sa";
	}
	for (d = iDayOfFirst; d < 7; d++) {
	aMonth[1][d] = iVarDate;
	iVarDate++;
	}
	for (w = 2; w < 7; w++) {
	for (d = 0; d < 7; d++) {
	if (iVarDate <= iDaysInMonth) {
	aMonth[w][d] = iVarDate;
	iVarDate++;
	}}}
	return aMonth;
}

function fDrawCal(nome,iYear, iMonth, iCellWidth, iCellHeight, sDateTextSize, sDateTextWeight, iDayStyle) {
	var myMonth;
	myMonth = fBuildCal(iYear, iMonth, iDayStyle);
	document.write("<table border='1' cellpadding='1' cellspacing='0'><tr>")
	var tds = "<td align='center' style='FONT-FAMILY:Arial;FONT-SIZE:9px;FONT-WEIGHT: bold'>"
	document.write(tds + myMonth[0][0] + "</td>");
	document.write(tds + myMonth[0][1] + "</td>");
	document.write(tds + myMonth[0][2] + "</td>");
	document.write(tds + myMonth[0][3] + "</td>");
	document.write(tds + myMonth[0][4] + "</td>");
	document.write(tds + myMonth[0][5] + "</td>");
	document.write(tds + myMonth[0][6] + "</td></tr>");
	for (w = 1; w < 7; w++) {
	document.write("<tr>")
	for (d = 0; d < 7; d++) {
	document.write("<td align='left' valign='top' width='" + iCellWidth + "' height='" + iCellHeight + "' id=calCell style='CURSOR:Hand' onMouseOver='fToggleColor(this)' onMouseOut='fToggleColor(this)' onclick=fSetSelectedDay(\"" + nome + "\",this)>");
	if (!isNaN(myMonth[w][d])) {
	document.write("<font id=calDateText onMouseOver='fToggleColor(this)' style='CURSOR:Hand;FONT-FAMILY:Arial;FONT-SIZE:" + sDateTextSize + ";FONT-WEIGHT:" + sDateTextWeight + "' onMouseOut='fToggleColor(this)' onclick=fSetSelectedDay(\"" + nome + "\",this)>" + myMonth[w][d] + "</font>");
	} else {
	document.write("<font id=calDateText onMouseOver='fToggleColor(this)' style='CURSOR:Hand;FONT-FAMILY:Arial;FONT-SIZE:" + sDateTextSize + ";FONT-WEIGHT:" + sDateTextWeight + "' onMouseOut='fToggleColor(this)' onclick=fSetSelectedDay(\"" + nome + "\",this)> </font>");
	}
	document.write("</td>")
	}
	document.write("</tr>");
	}
	document.write("</table>")
}

function fUpdateCal(iYear, iMonth) {
	myMonth = fBuildCal(iYear, iMonth);
	objPrevElement.bgColor = "";
	document.all.calSelectedDate.value = "";
	for (w = 1; w < 7; w++) {
	for (d = 0; d < 7; d++) {
	if (!isNaN(myMonth[w][d])) {
	calDateText[((7*w)+d)-7].innerText = myMonth[w][d];
	} else {
	calDateText[((7*w)+d)-7].innerText = " ";
	}}}
}

function myGetDate(d) {
	return(d.getDate() + "/" + d.getMonth() + "/" + d.getFullYear());
}

function setDates(s) {
	switch(s){
		case "s":
			var td = new Date();
			ms = td.getTime();
			var td2 = new Date(ms+ (1000*60*60*24*7)) ;
			
			document.form1.dt_datainizio.value = myGetDate(td);
			document.form1.dt_datafine.value = myGetDate(td2);
			MM_showHideLayers('calext','','hide')
		break;
		case "m":
			var td = new Date();
			ms = td.getTime();
			var td2 = new Date(ms+ (1000*60*60*24*31)) ;
			
			document.form1.dt_datainizio.value = myGetDate(td);
			document.form1.dt_datafine.value = myGetDate(td2);
			MM_showHideLayers('calext','','hide')
		break;
		}	
}

function popmodalWindow(url, larghezza, altezza, dati){
	width = Math.min(screen.width, larghezza);
	height = Math.min(screen.height, altezza);
	//tipobrowser = getbrowser();
	//if (window.showModalDialog) {
		//var myText = showModalDialog(url,dati,' dialogWidth:' + width + 'px; dialogHeight:' + height + 'px; resizable:yes; status:yes');
	//}else{
	  window.open(url,'','width=' + width + ',height=' + height + ',toolbar=no,location=no,resizable=no,status=no, menubar=no, scrollbars=yes');

	//}
}
function getbrowser(){
	agt = navigator.userAgent.toLowerCase();
	is_ie	   = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	is_opera  = (agt.indexOf("opera") != -1);
	is_mac	   = (agt.indexOf("mac") != -1);
	is_mac_ie = (is_ie && is_mac);
	is_win_ie = (is_ie && !is_mac);
	is_gecko  = (navigator.product == "Gecko");
	if (is_opera){
		return "opera";
	}else if (is_gecko){
		return "Gecko";
	}else if (is_win_ie){
		return "win_ie";
	}else if (is_mac_ie){
		return "mac_ie";
	}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
 var result = false;
 if (obj.getAttributeNode("class") != null) {
	 result = obj.getAttributeNode("class").value;
 }
 return result;
}   

function stripe(id) {

// the flag we'll use to keep track of 
// whether the current row is odd or even
var even = false;

// if arguments are provided to specify the colours
// of the even & odd rows, then use the them;
// otherwise use the following defaults:
var evenColor = arguments[1] ? arguments[1] : "#fff";
var oddColor = arguments[2] ? arguments[2] : "#eee";

// obtain a reference to the desired table
// if no such table exists, abort
var table = document.getElementById(id);
if (! table) { return; }

// by definition, tables can have more than one tbody
// element, so we'll have to get the list of child
// &lt;tbody&gt;s 
var tbodies = table.getElementsByTagName("tbody");

// and iterate through them...
for (var h = 0; h < tbodies.length; h++) {

 // find all the &lt;tr&gt; elements... 
  var trs = tbodies[h].getElementsByTagName("tr");
  
  // ... and iterate through them
  for (var i = 0; i < trs.length; i++) {

	// avoid rows that have a class attribute
	// or backgroundColor style
	if (! hasClass(trs[i]) &&
		! trs[i].style.backgroundColor) {
	  
	  // get all the cells in this row...
	  var tds = trs[i].getElementsByTagName("td");
	
	  // and iterate through them...
	  for (var j = 0; j < tds.length; j++) {
	
		var mytd = tds[j];

		// avoid cells that have a class attribute
		// or backgroundColor style
		if (! hasClass(mytd) &&
			! mytd.style.backgroundColor) {
	
		  mytd.style.backgroundColor =
			even ? evenColor : oddColor;
		
		}
	  }
	}
	// flip from odd to even, or vice-versa
	even =  ! even;
  }
}
}

function addEvent(obj, evType, fn)
{ 
		if (obj.addEventListener) { obj.addEventListener(evType, fn, true); return true; } 
		else if (obj.attachEvent) {  var r = obj.attachEvent("on"+evType, fn);  return r;  } 
		else {  return false; } 
}
function popimg(rootpth,idimg,nomefile){
        popmodalWindow(rootpth + "visualizza_img.php?id=" + idimg + "&nome_file=" + nomefile, 800, 600, "");
}
