/* $Id: tbl_change.js,v 2.18 2005/10/06 09:23:58 nijel Exp $ */


/**
 * Modify from controls when the "NULL" checkbox is selected
 *
 * @param   string   the MySQL field type
 * @param   string   the urlencoded field name
 * @param   string   the md5 hashed field name
 *
 * @return  boolean  always true
 */
function nullify(theType, urlField, md5Field, multi_edit)
{
    var rowForm = document.forms['insertForm'];

    if (typeof(rowForm.elements['funcs' + multi_edit + '[' + urlField + ']']) != 'undefined') {
        rowForm.elements['funcs' + multi_edit + '[' + urlField + ']'].selectedIndex = -1;
    }

    // "SET" field , "ENUM" field with more than 20 characters
    // or foreign key field
    if (theType == 1 || theType == 3 || theType == 4) {
        rowForm.elements['field_' + md5Field + multi_edit + '[]'].selectedIndex = -1;
    }
    // Other "ENUM" field
    else if (theType == 2) {
        var elts     = rowForm.elements['field_' + md5Field + multi_edit + '[]'];
        // when there is just one option in ENUM:
        if (elts.checked) {
            elts.checked = false;
        } else {
            var elts_cnt = elts.length;
            for (var i = 0; i < elts_cnt; i++ ) {
                elts[i].checked = false;
            } // end for

        } // end if
    }
    // Other field types
    else /*if (theType == 5)*/ {
        rowForm.elements['fields' + multi_edit + '[' + urlField + ']'].value = '';
    } // end if... else if... else

    return true;
} // end of the 'nullify()' function


/**
 * Unchecks the "NULL" control when a function has been selected or a value
 * entered
 *
 * @param   string   the urlencoded field name
 *
 * @return  boolean  always true
 */
function unNullify(urlField, multi_edit)
{
    var rowForm = document.forms['insertForm'];

    if (typeof(rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']']) != 'undefined') {
        rowForm.elements['fields_null[multi_edit][' + multi_edit + '][' + urlField + ']'].checked = false
    } // end if

    if (typeof(rowForm.elements['insert_ignore_' + multi_edit]) != 'undefined') {
        rowForm.elements['insert_ignore_' + multi_edit].checked = false
    } // end if

    return true;
} // end of the 'unNullify()' function

var day;
var month;
var year;
var hour;
var minute;
var second;
var clock_set = 0;

/**
 * Opens calendar window.
 *
 * @param   string      calendar.php parameters
 * @param   string      form name
 * @param   string      field name
 * @param   string      edit type - date/timestamp
 */
function openCalendar(params, form, field, type) {
    //window.open("./calendar.php?" + params, "calendar", "width=400,height=200,status=yes");
	self.location.href = "./calendario.php?" + params;
    dateField = eval("document." + form + "." + field);
    dateType = type;
}

/**
 * Formats number to two digits.
 *
 * @param   int number to format.
 * @param   string type of number
 */
function formatNum2(i, valtype) {
    f = (i < 10 ? '0' : '') + i;
    if (valtype && valtype != '') {
        switch(valtype) {
            case 'month':
                f = (f > 12 ? 12 : f);
                break;

            case 'day':
                f = (f > 31 ? 31 : f);
                break;

            case 'hour':
                f = (f > 24 ? 24 : f);
                break;

            default:
            case 'second':
            case 'minute':
                f = (f > 59 ? 59 : f);
                break;
        }
    }

    return f;
}

/**
 * Formats number to two digits.
 *
 * @param   int number to format.
 * @param   int default value
 * @param   string type of number
 */
function formatNum2d(i, default_v, valtype) {
    i = parseInt(i, 10);
    if (isNaN(i)) return default_v;
    return formatNum2(i, valtype)
}

/**
 * Formats number to four digits.
 *
 * @param   int number to format.
 */
function formatNum4(i) {
    i = parseInt(i, 10)
    return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
}

/**
 * Initializes calendar window.
 */
function initCalendar(evento1,evento2,evento3,evento4,evento5,evento6,evento7,evento8,evento9,evento10,evento11,evento12,evento13,evento14,evento15,evento16,evento17,evento18,evento19,evento20,evento21,evento22,evento23,evento24,evento25,evento26,evento27,evento28,evento29,evento30,evento31,year,month,day) {
	//year = 2007;
	//month = 1;
    if (!year && !month && !day) {

        if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) {
            dt      = new Date();
            year    = dt.getFullYear();
            month   = dt.getMonth();
            day     = dt.getDate();
        }
        if (isNaN(hour) || isNaN(minute) || isNaN(second)) {
            dt      = new Date();
            hour    = dt.getHours();
            minute  = dt.getMinutes();
            second  = dt.getSeconds();
        }
    } else {
        /* Moving in calendar */
        if (month > 11) {
            month = 0;
            year++;
        }
        if (month < 0) {
            month = 11;
            year--;
        }
    }

    /*if (document.getElementById) {
        cnt = document.getElementById("calendar_data");
    } else if (document.all) {*/
        cnt = document.all["calendar_data"];
    //}

    cnt.innerHTML = "";
    var day_names = new Array('D','S','T','Q','Q','S','S');
    str = "";
    str += '<table width="100%" id="wp-calendar" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF"><thead><tr>';
    for (i = 0; i < 7; i++) {
		str += '<td height="20" div align="center" bgcolor="#FFFFFF" scope="col"><font size="1" face="Verdana, Arial, Helvetica, sans-serif"><b>' + day_names[i] + '</b></font></td><td width="1" bgcolor="#E1E0E5"></td>';
    }
    str += "</tr></thead><tbody>";

    var firstDay = new Date(year, month, 1).getDay();
    var lastDay = new Date(year, month + 1, 0).getDate();

    str += '<tr>';

    dayInWeek = 0;
    for (i = 0; i < firstDay; i++) {
        str += '<td>&nbsp;</td><td width="1" bgcolor="#E1E0E5"></td>';
        dayInWeek++;
    }
    for (i = 1; i <= lastDay; i++) {
        if (dayInWeek == 7) {
            str += '<tr>';
            dayInWeek = 0;
        }

        dispmonth = 1 + month;

        //if (window.opener.dateType == 'datetime' || window.opener.dateType == 'date') {
            //actVal = "" + formatNum4(year) + "-" + formatNum2(dispmonth, 'month') + "-" + formatNum2(i, 'day');
			//actVal = "" + formatNum2(i, 'day') + "/" + formatNum2(dispmonth, 'month') + "/" + formatNum4(year);
        //} else {
            actVal = "" + formatNum4(year) + formatNum2(dispmonth, 'month') + formatNum2(i, 'day');
        //}
        if (i == day) {
            style = ' class="selected"';
            current_date = actVal;
        } else {
            style = '';
        }
		
        str += '<td bgcolor="#FFFFFF" align="center"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">';
		tem_link=0;
		
		// COLUNA 1
		if (i==1) {
			if (evento1) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 2
		if (i==2) {
			if (evento2) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		} 
		
		// COLUNA 3
		if (i==3) {
			if (evento3) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 4
		if (i==4) {
			if (evento4) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 5
		if (i==5) {
			if (evento5) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 6
		if (i==6) {
			if (evento6) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 7
		if (i==7) {
			if (evento7) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 8
		if (i==8) {
			if (evento8) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 9
		if (i==9) {
			if (evento9) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 10
		if (i==10) {
			if (evento10) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 11
		if (i==11) {
			if (evento11) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 12
		if (i==12) {
			if (evento12) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 13
		if (i==13) {
			if (evento13) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 14
		if (i==14) {
			if (evento14) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 15
		if (i==15) {
			if (evento15) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 16
		if (i==16) {
			if (evento16) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 17
		if (i==17) {
			if (evento17) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 18
		if (i==18) {
			if (evento18) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 19
		if (i==19) {
			if (evento19) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 20
		if (i==20) {
			if (evento20) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 21
		if (i==21) {
			if (evento21) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 22
		if (i==22) {
			if (evento22) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 23
		if (i==23) {
			if (evento23) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 24
		if (i==24) {
			if (evento24) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 25
		if (i==25) {
			if (evento25) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 26
		if (i==26) {
			if (evento26) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 27
		if (i==27) {
			if (evento27) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 28
		if (i==28) {
			if (evento28) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 29
		if (i==29) {
			if (evento29) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 30
		if (i==30) {
			if (evento30) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		
		// COLUNA 31
		if (i==31) {
			if (evento31) {
				str += '<a href="index.php?dia='+i+'&mes='+month+'&ano='+year+'">' + i;
				tem_link=1;
			} else {
				str += '' + i;
				tem_link=0;
			}
		}
		if (tem_link==1) {
		    str += '</a></font></td><td width="1" bgcolor="#E1E0E5"></td>';
		} else {
			str += '</font></td><td width="1" bgcolor="#E1E0E5"></td>';
		}
        dayInWeek++;
    }
    for (i = dayInWeek; i < 7; i++) {
        str += '<td>&nbsp;</td><td width="1" bgcolor="#E1E0E5"></td>';
    }

    str += "</tr></table>";
	var month_names = new Array('Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez');
	//heading table
    str += '<table class="calendar"><tr><th width="50%">';
    str += '<form name="form_mes" method="POST" action="index.php">';
    //str += '<a href="javascript:month--; initCalendar();">&laquo;</a> ';
    //str += '<select id="select_month" name="monthsel" onchange="month = parseInt(document.getElementById(\'select_month\').value); initCalendar();">';
	str += '<select id="select_month" name="monthsel" onchange="form_mes.submit();">';
    for (i =0; i < 12; i++) {
        if (i == month) selected = ' selected="selected"';
        else selected = '';
        str += '<option value="' + i + '" ' + selected + '>' + month_names[i] + '</option>';
    }
    str += '</select>';
    //str += ' <a href="javascript:month++; initCalendar();">&raquo;</a>';
	str += ' <input type="hidden" name="yearsel" value="'+year+'">';
    str += '</form>';
    str += '</th><th width="50%">';
    str += '<form name="form_ano" method="POST" action="index.php">';
    //str += '<a href="javascript:year--; initCalendar();">&laquo;</a> ';
    //str += '<select id="select_year" name="yearsel" onchange="year = parseInt(document.getElementById(\'select_year\').value); initCalendar();">';
	str += '<select id="select_year" name="yearsel" onchange="form_ano.submit();">';
    for (i = year - 25; i < year + 25; i++) {
        if (i == year) selected = ' selected="selected"';
        else selected = '';
        str += '<option value="' + i + '" ' + selected + '>' + i + '</option>';
    }
    str += '</select>';
    //str += ' <a href="javascript:year++; initCalendar();">&raquo;</a>';
	str += ' <input type="hidden" name="monthsel" value="'+month+'">';
    str += '</form>';
    str += '</th></tr></table>';

    cnt.innerHTML = str;
}

/**
 * Returns date from calendar.
 *
 * @param   string     date text
 */
function returnDate(d) {
    txt = d;
    if (window.opener.dateType != 'date') {
        // need to get time
        h = parseInt(document.getElementById('hour').value,10);
        m = parseInt(document.getElementById('minute').value,10);
        s = parseInt(document.getElementById('second').value,10);
        if (window.opener.dateType == 'datetime') {
            txt += ' ' + formatNum2(h, 'hour') + ':' + formatNum2(m, 'minute') + ':' + formatNum2(s, 'second');
        } else {
            // timestamp
            txt += formatNum2(h, 'hour') + formatNum2(m, 'minute') + formatNum2(s, 'second');
        }
    }

    window.opener.dateField.value = txt;
    window.close();
}
