/***
 ** FORM FUNCTIONS
 ** --------------
 ** This file holds JavaScript functions necessary for form interactivity
***/

/* *** GLOBALS */
var DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); // days in month
var Months = new Array('Januar', 'Februar', 'M�rz', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember');
var Today = new Date();

/* *** COMMON FUNCTIONS */
function isLeapYear(year) {
	if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)) {
		return true;
	}
	return false;
}

function getDaysInMonth(year, month) {
	if(month == 2) {
		return DaysInMonth[1] + isLeapYear(year);
	}
	return DaysInMonth[month - 1];
}


/* *** DATE functions *** */
function initDate(formId, elementName, year, month, day) {
	var yearObj = document.forms[formId].elements[elementName + "_year"]
	var monthObj = document.forms[formId].elements[elementName + "_month"];
	var dayObj = document.forms[formId].elements[elementName + "_day"];
	var i;
	
	if(year == 0) year = Today.getFullYear();
	if(month == 0) month = Today.getMonth() + 1;
	if(day == 0) day = Today.getDate();
	
	// set up days
	for(i = 1; i <= getDaysInMonth(year, month); i++) {
		dayObj.options[i - 1] = new Option(i, i);
	}
	dayObj.options.selectedIndex = (day - 1);
	
	// set up month
	for(i = 1; i <= 12; i ++) {
		monthObj.options[i - 1] = new Option(Months[i - 1], i);
	}
	monthObj.options.selectedIndex = (month - 1);
	
	// set up year
	for(i = 0; i < 5; i++) {
		yearObj.options[i] = new Option(i + year);
	}
	yearObj.options.selectedIndex = 0;
}

function updateDate(formId, elementName, suffix) {
	var dayObj = document.forms[formId].elements[elementName + "_day"];
	
	var day = document.forms[formId].elements[elementName + "_day"].value;
	var year = document.forms[formId].elements[elementName + "_year"].value;
	var month = document.forms[formId].elements[elementName + "_month"].value;
	
	var currentDays = document.forms[formId].elements[elementName + "_day"].length;
	switch(suffix) {
		case 'day':
			// nothing to do
			break;
		case 'month':
		case 'year':
			// update days, as these are differnt in other months or years
			var newDays = getDaysInMonth(year, month);
			while(newDays != currentDays) {
				if(newDays > currentDays) {
					dayObj.options[currentDays] = new Option(currentDays + 1, currentDays + 1);
					currentDays ++;
				}
				else {
					dayObj.options[currentDays - 1] = null;
					currentDays --;
				}
				if(day > currentDays) day = currentDays; // if 31.3 is selected and you go back to 28.2 (there is no 31st feb)
			}
			dayObj.selectedIndex = (day - 1);
			break;
	}
}

/* *** TIME functions *** */
function initTime(formId, elementName, hour, minute) {
	var hourObj = document.forms[formId].elements[elementName + "_hour"]
	var minuteObj = document.forms[formId].elements[elementName + "_minute"];
	var i;
	
	
	// set up hours
	for(i = 0; i <= 23; i++) {
		hourObj.options[i] = new Option(i, i);
	}
	hourObj.options.selectedIndex = hour;
	
	// set up minutes
	for(i = 0; i <= 59; i ++) {
		minuteObj.options[i] = new Option(i, i);
	}
	minuteObj.options.selectedIndex = minute;
}

function updateTime(formId, elementName, suffix) {
	// currently nothing to do
}

/* *** DATETIME functions *** */
function initDateTime(formId, elementName, year, month, day, hour, minute) {
	var yearObj = document.forms[formId].elements[elementName + "_year"]
	var monthObj = document.forms[formId].elements[elementName + "_month"];
	var dayObj = document.forms[formId].elements[elementName + "_day"];
	var hourObj = document.forms[formId].elements[elementName + "_hour"];
	var minuteObj = document.forms[formId].elements[elementName + "_minute"];
	var i;
	
	if(year == 0) year = Today.getFullYear();
	if(month == 0) month = Today.getMonth() + 1;
	if(day == 0) day = Today.getDate();
	
	// set up days
	for(i = 1; i <= getDaysInMonth(year, month); i++) {
		dayObj.options[i - 1] = new Option(i, i);
	}
	dayObj.options.selectedIndex = (day - 1);
	
	// set up month
	for(i = 1; i <= 12; i ++) {
		monthObj.options[i - 1] = new Option(Months[i - 1], i);
	}
	monthObj.options.selectedIndex = (month - 1);
	
	// set up year
	for(i = 0; i < 5; i++) {
		yearObj.options[i] = new Option(i + year);
	}
	yearObj.options.selectedIndex = 0;
	
	// set up hour
	for(i = 0; i < 23; i++) {
		hourObj.options[i] = new Option(i, i);
	}
	hourObj.options.selectedIndex = hour;
	
	// set up minute
	for(i = 0; i < 59; i++) {
		minuteObj.options[i] = new Option(i, i);
	}
	minuteObj.options.selectedIndex = minute;
}

function updateDateTime(formId, elementName, suffix) {
	var dayObj = document.forms[formId].elements[elementName + "_day"];
	
	var day = document.forms[formId].elements[elementName + "_day"].value;
	var year = document.forms[formId].elements[elementName + "_year"].value;
	var month = document.forms[formId].elements[elementName + "_month"].value;
	
	var currentDays = document.forms[formId].elements[elementName + "_day"].length;
	switch(suffix) {
		case 'day':
			// nothing to do
			break;
		case 'month':
		case 'year':
			// update days, as these are differnt in other months or years
			var newDays = getDaysInMonth(year, month);
			while(newDays != currentDays) {
				if(newDays > currentDays) {
					dayObj.options[currentDays] = new Option(currentDays + 1, currentDays + 1);
					currentDays ++;
				}
				else {
					dayObj.options[currentDays - 1] = null;
					currentDays --;
				}
				if(day > currentDays) day = currentDays; // if 31.3 is selected and you go back to 28.2 (there is no 31st feb)
			}
			dayObj.selectedIndex = (day - 1);
			break;
	}
	
	// currently nothing to be done for hour and minute
}

function myConfirmInvoiceSubmit(formName,actionLink){
	var invoiceType = $("select[@name='Invoices.InvoiceTypeID'] option:selected").text();
	var invoiceAddress = $("select[@name='entryselectionAddressBookTable'] option:selected").text();

    if(invoiceAddress == ""){
        invoiceAddress = $("textarea[@name='Invoices.addressBlock']").val();
    }
	var confirmText = translation["invoiceTypeLabel"] + ":\n" + invoiceType + "\n\n" + translation["invoiceAddressLabel"] + ":\n" + invoiceAddress + "\n\n" + translation["invoiceTextLabel"];
	
	if(confirm(confirmText)){
		document.getElementById(formName).action=actionLink;
		document.getElementById(formName).submit();
	}
}

function myConfirmBookingConfirmationSubmit(formName,actionLink){
	var invoiceType = $("select[@name='Invoices.InvoiceTypeID'] option:selected").text();
	var invoiceAddress = $("select[@name='entryselectionAddressBookTable'] option:selected").text();

    if(invoiceAddress == ""){
        invoiceAddress = $("textarea[@name='Invoices.addressBlock']").val();
    }

	var confirmText = translation["invoiceAddressLabel"] + ":\n" + invoiceAddress + "\n\n" + translation["invoiceTextLabel"];

	if(confirm(confirmText)){
		document.getElementById(formName).action=actionLink;
		document.getElementById(formName).submit();
	}
}

function mySubmit(formName, actionLink){
	document.getElementById(formName).action=actionLink;
	document.getElementById(formName).submit();
}

function myConfirmSubmit(formName, confirmText, actionLink){
	if(confirm(confirmText)){
		document.getElementById(formName).action=actionLink;
		document.getElementById(formName).submit();
	}
}


function myConfirmMailSubmit(formName,object){
	var aFilterFieldsString = new Array("filterFirstName","filterLastName","filterEmail","filterOrganisation","filterDepartment");
	var aFilterFieldsBool = new Array("filterActiveUser","filterOSRUser","filterNoMembership","filterFilledCart","filterRegistered",
			"filterNotRegistered","filterCorrispondingAuthor","filterPaperFormat","filterIsInSession","filterReadByEditor",
			"filterNotDispatched");
	var aFilterFieldsSelect = new Array("filterUserGroup","filterService","filterInvoices","filterSpecialTrack","filterAcceptanceStadium");
	var aFilterFieldsMulti = new Array("filterMembership","filterNotMembership");
	var params = "&formName="+formName;
	for(var count = 0; count < aFilterFieldsString.length; count++) {
		var name = aFilterFieldsString[count];
		var value = $("input[name='MailDraft."+name+"']").val();
		params += "&"+name+"="+value;
	}
	for(var count = 0; count < aFilterFieldsBool.length; count++) {
		var name = aFilterFieldsBool[count];
		var value = 0;
		var checkBoxes = $("input[name='MailDraft."+name+"']:checked");
		if(checkBoxes.length > 0){
			value = checkBoxes.val();
		}
		params += "&"+name+"="+value;
	}
	for(var count = 0; count < aFilterFieldsSelect.length; count++) {
		var name = aFilterFieldsSelect[count];
		var value  = $("select[name='MailDraft."+name+"']").val();
		params += "&"+name+"="+value;
	}
	for(var count = 0; count < aFilterFieldsMulti.length; count++) {
		var name = aFilterFieldsMulti[count];
		var value = "";
		var checkBoxes = $("input[name='MailDraft."+name+"[]']:checked");
		while(checkBoxes.length > 0){
			value += checkBoxes.val()+";";
			checkBoxes = checkBoxes.nextAll("input[name='MailDraft."+name+"[]']:checked");
		}
		params += "&"+name+"="+value;
	}
	$.ajax({url: "indexAjax.php",type: "GET", data: "object0="+object+"&action0=getConfirmText"+params, dataType: "json", success:processResponseForm});
}

function processResponseForm(data,status){
	if(data.status=="1"){
		if(confirm(data.confirmText)){
			document.getElementById(data.formName).action=data.actionLink;
			document.getElementById(data.formName).submit();
		}
	}
	else{
		alert(data.confirmText);
	}	
}


function myConfirmAction(name, confirmText, actionLink){
	if(confirm(confirmText)){
		window.location.href=actionLink;
	}
}
