﻿var modalIcon = ({
	None: 0,
	Blank: 3,
	Exclamation: 1,
	ExclamationGreen: 4,
	Question: 2
})

var modal = ({

});
var _reshow_modalid = '';
modal.showValidator = function (lines) {
	if (_modalid != '') {
		modal.Hide();
		_reshow_modalid = '';
		_reshow_modalid = _modalid;
	}
	var obj = getObj('modalValidatorItems');
	var html = '<ul style="line-height:20px;">';
	//	for (var i = 0; i < lines.length; i++) {
	//		html += '<li>' + lines[i] + '</li>';
	//	}
	lines.ForEach(function (n) {
		html += '<li>' + n + '</li>';
	});
	html += '</ul>';
	obj.innerHTML = html;

	var obj2 = getObj('modalValidatorItemContainer');


	var _height = (lines.length * 20)
	obj2.style.height = (_height + 30) + 'px';

	modal.show(350, _height + 75 + 25 + 10, 'modalValidator', modalIcon.Exclamation);
}


modal.rfcRequest = function () {
	rfc.SendRequest()
}

modal.rfcCancel = function () {
	modal.Hide();
}


// set default modalConfirm handlers
var modalRFC_Request = modal.rfcRequest;
var modalRFC_Cancel = modal.rfcCancel;

// executor of confirm handlers
function _modalRFC_Request() {
	modalRFC_Request();
}
function _modalRFC_Cancel() {
	modalRFC_Cancel();
}
modal.showRFC = function (field, fieldId, fieldValue, fn_request, fn_cancel) {
	if (_modalid != '') {
		modal.Hide();
		_reshow_modalid = '';
		_reshow_modalid = _modalid;
	}
	var value = '';
	if (fieldValue) { value = fieldValue } else {
		value = getObj(fieldId).value
	}

	// set the correct modalConfirm handlers
	modalRFC_Request = (fn_request) ? fn_request : modal.rfcRequest;
	modalRFC_Cancel = (fn_cancel) ? fn_cancel : modal.rfcCancel;

	getObj('modalRFC_Field').innerHTML = field;
	getObj('modalRFC_FieldValue').innerHTML = value;

	getObj('txtRFC_FieldName').value = field;
	getObj('txtRFC_FieldValue').value = value;
	getObj('txtRFC_NewValue').value = "";
	modal.show(400, 300, 'modalRFC', modalIcon.Question);
}

modal.showAlert = function (msg, header) {
	header = (header) ? header : translator.get('notification');
	getObj('modalAlert_Content').innerHTML = msg;
	getObj('modalAlert_Header').innerHTML = header;

	modal.show(400, 300, 'modalAlert', modalIcon.Exclamation);
}
modal.showMap = function (lat, lng, markerTitle, header) {
	header = (header) ? header : translator.get('notification');
	getObj('modalMap_Header').innerHTML = header;
	getObj('modalMap_hfLat').value = lat;
	getObj('modalMap_hfLng').value = lng;
	getObj('modalMap_hfTitle').value = markerTitle;
	modal.show(400, 540, 'modalMap', modalIcon.Question);
	initializeModalMap();
}
modal.ShowBlockingEdit = function (recurrnce) {
	if(recurrnce) Recurrence_SetValues(recurrnce);
	modal.show(450, 525, 'ModalBlockingEdit', modalIcon.Exclamation);
}

modal.showNotify = function(msg, header) {
	header = (header) ? header : translator.get('notification');
	getObj('modalNotify_Content').innerHTML = msg;
	getObj('modalNotify_Header').innerHTML = header;

	modal.show(300, 200, 'modalNotify', modalIcon.ExclamationGreen);
}


modal.confirmYes = function () {
	WebSite.Submit();
}

modal.confirmNo = function () {
	modal.Hide();
}

modal.confirmCancel = function () {
	modal.Hide();
}

// set default modalConfirm handlers
var modalDefault_Yes = modal.confirmYes;
var modalDefault_No = modal.confirmNo;
var modalDefault_Cancel = modal.confirmCancel;

// executor of confirm handlers
function _modalDefault_Yes() {
	modalDefault_Yes();
}
function _modalDefault_No() {
	modalDefault_No();
}
function _modalDefault_Cancel() {
	modalDefault_Cancel();
}
modal.showConfirm = function (msg, header, fn_yes, fn_no, fn_cancel, settings) {
	header = (header) ? header : 'Confirmation';
	getObj('modalConfirm_Content').innerHTML = msg;
	getObj('modalConfirm_Header').innerHTML = header;

	// set the correct modalConfirm handlers
	modalDefault_Yes = (fn_yes) ? fn_yes : modal.confirmYes;
	modalDefault_No = (fn_no) ? fn_no : modal.confirmNo;
	modalDefault_Cancel = (fn_cancel) ? fn_cancel : modal.confirmCancel;

	var _width, _height;
	_width = 400;
	_height = 300;
	if (settings) {
		_width = settings.Width;
		_height = settings.Height;
	}

	if (getObj('modalConfirm_ContentHolder')) {
		getObj('modalConfirm_ContentHolder').style.width = _width + 'px';
		getObj('modalConfirm_ContentHolder').style.height = _height - 90 + 'px';
	}
	if (getObj('modalConfirm_Buttons')) {
		getObj('modalConfirm_Buttons').style.width = _width + 'px';
	}

	modal.show(_width, _height, 'modalConfirm', modalIcon.Question);
}
modal.showLogout = function (msg, header) {
	header = (header) ? header : 'Logout';
	getObj('modalLogout_Content').innerHTML = msg;
	getObj('modalLogout_Header').innerHTML = header;

	modal.show(400, 300, 'modalLogout', modalIcon.Question);
}
modal.ShowContact = function (isNew, header, id, name, email, phone, type,isRecall) {
if(isRecall)
{
modal.show(400, 320, 'modalContact', modalIcon.Exclamation);
}
else
{

	header = (header) ? header : 'Kontakt';
	getObj('modalContact_Header').innerHTML = header;
	$('input[id*="ContactEditName"]').val((isNew) ? '' : name);
	$('input[id*="txtContactEditEmail"]').val((isNew) ? '' : email);
	$('input[id*="txtContactEditPhone"]').val((isNew) ? '' : phone);
	$('input[id*="txtContactEditType"]').val((isNew) ? '' : type);
	$('input[id*="hfContactEditId"]').val((isNew) ? '' : id);
	$('input[id*="hfContactIsNew"]').val(isNew);

	modal.show(400, 320, 'modalContact', modalIcon.Exclamation);
	}
}

var _modalid = '';
modal.show = function (width, height, modalid, icon) {
	_modalid = modalid;
	getObj('modal').style.display = '';
	getObj('modal').style.visibility = 'visible';
	$("#modalcontent").html("");
	$("#" + _modalid).children().appendTo("#modalcontent");
	var _modalcontainer = getObj('modalcontainer');
	var _h = height + 88;
	var _w = width + 88;
	_modalcontainer.style.height = String.format('{0}px', _h);
	_modalcontainer.style.width = String.format('{0}px', _w);
	_modalcontainer.style.marginLeft = String.format('-{0}px', parseInt(width / 2, 10));
	_modalcontainer.style.marginTop = String.format('-{0}px', parseInt(height / 2, 10));

	var _modalcontentcontainer = getObj('modalcontentcontainer');
	_modalcontentcontainer.style.height = String.format('{0}px', height);
	_modalcontentcontainer.style.width = String.format('{0}px', width);

	// modal icon
	var _modalicon = getObj('modalicon');
	switch (icon) {
		case modalIcon.Blank:
			_modalicon.src = '/theming/default/grfx/modal/yellow_rounded.png';
			break;
		case modalIcon.Exclamation:
			_modalicon.src = '/theming/default/grfx/modal/yellow_rounded_Exclamation.png';
			break;
		case modalIcon.ExclamationGreen:
			_modalicon.src = '/theming/default/grfx/modal/green_rounded_Exclamation.png';
			break;
		case modalIcon.Question:
			_modalicon.src = '/theming/default/grfx/modal/yellow_rounded_questionmark.png';
			break;
	}

}

modal.OnHideEnd = function () {  };
modal.OnNotifyOK = function () { modal.Hide() };
modal.Hide = function () {
	getObj('modal').style.display = 'none';
	getObj('modal').style.visibility = 'hidden';
	$("#modalcontent").children().appendTo("#" + _modalid);
	if (_reshow_modalid != '') {
		switch (_reshow_modalid.toLowerCase()) {
			case 'modalblockingedit':
				_reshow_modalid = '';
				modal.ShowBlockingEdit();

				break;
			case "modalcontact":
				_reshow_modalid = '';
				modal.ShowContact('', '', '', '', '', '', '', true);

				break;
			default:
				_reshow_modalid = '';

				break;
		}
	}

	modal.OnHideEnd();
}


var RecurringFrequency = {
	Daily: 0,
	Weekly: 1,
	Monthly: 2,
	Yearly: 3
}


function Recurrence() {
	this.Setup = new RecurrenceSetup();
	this.Setup.SetNoEndDate();
	this.Text = '';
	this.RecID = 0;
	this.CourseID = 0;
	this.BlockID = 0;
}

function RecurrenceSetup(){
	this.Start = new Date();
	this.End = new Date();
	this.TimeStart = new DateTime.TimeSpan();
	this.TimeEnd = new DateTime.TimeSpan();
	this.Frequency = RecurringFrequency.Daily;
	this.DaysOfWeek = [];
	this.Month = [];
	this.DayOfMonth = 0;
	this.Instances = 0;
	this.Interval = 0;
	this.Position = 0;
	this.IncludeSaturdays = true;
	this.IncludeSundays = true;
	this.IncludeHolidays = false;
	this.HolidayCountry = 'DK';
	this.NewDaily = function () {
		this.Frequency = RecurringFrequency.Daily;
		this.Interval = 1;
	};
	this.NewWeekly = function () {
		this.Frequency = RecurringFrequency.Weekly;
	};
	this.NewMonthly = function () {
		this.Frequency = RecurringFrequency.Monthly;
	};
	this.NewYearly = function () {
		this.Frequency = RecurringFrequency.Yearly;
	};
	this.SetNoEndDate = function () {
		this.End = null;
		this.Instances = 0;
	};
	this.SetInstances = function (instances) {
		this.End = null;
		this.Instances = instances;
	}
};

//var RecurrenceSetup = ({
//	Start: new Date(),
//	End: new Date(),
//	TimeStart: new DateTime.TimeSpan(),
//	TimeEnd: new DateTime.TimeSpan(),
//	Frequency: RecurringFrequency.Daily,
//	DaysOfWeek: [],
//	Month: [],
//	DayOfMonth: 0,
//	Instances: 0,
//	Interval: 0,
//	Position: 0,
//	IncludeSaturdays: true,
//	IncludeSundays: true,
//	IncludeHolidays: false,
//	HolidayCountry: 'DK',
//	NewDaily: function () {
//		this.Frequency = RecurringFrequency.Daily;
//	},
//	NewWeekly: function () {
//		this.Frequency = RecurringFrequency.Weekly;
//	},
//	NewMonthly: function () {
//		this.Frequency = RecurringFrequency.Monthly;
//	},
//	NewYearly: function () {
//		this.Frequency = RecurringFrequency.Yearly;
//	},
//	SetNoEndDate: function () {
//		this.End = null;
//		this.Interval = 0;
//	},
//	SetInterval: function (interval) {
//		this.End = null;
//		this.Interval = interval;
//	}
//});

//RecurrenceSetup.toString = function () {
//	return RecurrenceSetup.ToString();
//}
//RecurrenceSetup.ToString = function () {
//	var _sb = new StringBuilder();
//	_sb.append('Start: ' + this.Start + '\n');
//	_sb.append('End: ' + this.End + '\n');
//	_sb.append('TimeStart: ' + this.TimeStart + '\n');
//	_sb.append('TimeEnd: ' + this.TimeEnd + '\n');
//	_sb.append('Frequency: ' + this.Frequency + '\n');
//	_sb.append('DaysOfWeek: ' + this.DaysOfWeek.join(',') + '\n');
//	_sb.append('Month: ' + this.Month.join(',') + '\n');
//	_sb.append('DayOfMonth: ' + this.DayOfMonth + '\n');
//	_sb.append('Instances: ' + this.Instances + '\n');
//	_sb.append('Interval: ' + this.Interval + '\n');
//	_sb.append('Position: ' + this.Position + '\n');
//	_sb.append('IncludeSaturdays: ' + this.IncludeSaturdays + '\n');
//	_sb.append('IncludeSundays: ' + this.IncludeSundays + '\n');
//	_sb.append('IncludeHolidays: ' + this.IncludeHolidays + '\n');
//	_sb.append('HolidayCountry: ' + this.HolidayCountry + '\n');
//	return _sb.toString();
//}
