// Version: 17-Jul-2009 

var form_debug = false;
var zipcode_prev_length = 0;
function generate_password( fill_ids ) {
	var length = 9;
	
	chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
	pass = "";
	
	for( x = 0; x < length; x++ ) {
		i = Math.floor( Math.random() * chars.length );
		pass += chars.charAt( i );
	}
	for( x = 0; x < fill_ids.length; x++ ) {
		if ( element = el(fill_ids[ x ]) )
			element.value = pass;
	}
}
function update_checkbox(id){
	if(el(id + '_checkbox').checked){
		el(id + '_input').value = 'checked';
	} else {
		el(id + '_input').value = '';
	}
}

function update_date(id)
{
	var months = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	
	if (el(id + '_year').value > 0 && el(id + '_month').value > 0 && el(id + '_day').value > 0)
	{
		if(isLeapYear(el(id + '_year').value))
		{
			months[1] = 29;
		}
		else
		{
			months[1] = 28;
		}
		
		if(months[el(id + '_month').value-1] < el(id + '_day').value)
		{
			el(id + '_input').value =null; 
			alert("De ingevulde datum is onjuist.");
		}
		else
		{
			el(id + '_input').value = el(id + '_year').value + '-' + (el(id + '_month').value<10?"0":"") + el(id + '_month').value + '-' + (el(id + '_day').value<10?"0":"") + el(id + '_day').value;
		}
	}
}

function update_time(id){
	el(id + '_input').value =  (el(id + '_hour').value<10?"0":"") + el(id + '_hour').value + ':' + (el(id + '_minute').value<10?"0":"") + el(id + '_minute').value + ':' + (el(id + '_second')!=null ? (el(id + '_second').value<10?"0":"") + el(id + '_second').value : '00');
}

function zipcode_onkeyup(id){
	if (el(id + '_numbers').value.length == 4 && el(id + '_numbers').value.length != zipcode_prev_length) el(id + '_letters').focus();
	zipcode_prev_length = el(id + '_numbers').value.length;
	update_zipcode(id);
}

function update_zipcode(id){
	el(id + '_letters').value = el(id + '_letters').value.toUpperCase();
	el(id + '_input').value = el(id + '_numbers').value + el(id + '_letters').value;
}

function submit_form(id){
	el(id).submit();
}

function action_show(id, condition){
	if(el(id) != null){
		if(condition){
			el(id).style.display = 'block';
		}else{
			el(id).style.display = 'none';
		}
	}else{
		if(form_debug) alert(id + ' not found');
	}
}
function action_hide(id, condition){ action_show(id, !condition); }

function action_enable(id, condition){
	if(el(id + '_input') != null){
		el(id + '_input').disabled = !condition;
	}else{
		if(form_debug) alert(id + ' not found');
	}
}
function action_disable(id, condition){ action_enable(id, !condition); }

function action_visible(id, condition){
	if(condition){
		el(id).style.visibility = 'visible';
	}else{
		el(id).style.visibility = 'hidden';
	}
}


function action_reload(id, condition, url){
	if (condition){
		value = getValue(id);
		el(id).innerHTML = loading_image + get_hidden_field(id, value);
		AJAX.get(url, reload_content, {'id': id});
	}
}

function action_reload_value(id, condition, url){
	value = getValue(id);
	AJAX.get(url, reload_value, {'id': id});
}


function action_formula(id, condition){
	el(id+'_score').innerHTML = condition;
}


function get_hidden_field(id, value){
	return '<input type="hidden" name="" id="' + id + '_input" value="'+ value+'"/>';
}


function reload_content(content, params){
	el(params.id).innerHTML = content;

	var command = "on_" + params.id + "_event();";
	if(command){
		//alert(command);
		eval(command);
	}
}

function reload_value(content, params){
	el(params.id+'_input').value = content;
	var command = "on_" + params.id + "_event();";
	if(command){
		eval(command);
	}
}

function getRealValue(id){
	if (el(id+'_input')!=null && el(id+'_input').value == "--other--"){
		return el(id+'_alternative_input').value;
	}else {
		return getValue(id);
	}
}

function getValue(id){
	if(el(id+'_input')!=null){
		if(el(id+'_input').value=="__check_group__"){
			var children = el(id).getElementsByTagName('input');
			var value = '';
			for(i = 0; i < children.length; i++){
				if(children.item(i).type == 'checkbox' && children.item(i).checked) value += children.item(i).value + ",";
			}
			return value;
		}else{
			return el(id+'_input').value;
		}
	}
	if (el(id) == undefined){
		alert("Element "+id+" is not found.");
		return false;
	}
	var children = el(id).getElementsByTagName('input');
	for(i = 0; i < children.length; i++){
		if(children.item(i).type == 'radio'){
			if(children.item(i).checked){
				return children.item(i).value;
			}
		}else{
			return children.item(i).value;
		}
	}
	var children = el(id).getElementsByTagName('select');
	for(i = 0; i < children.length; i++){
		return children.item(i).value;
	}
	return false;
}

function getScore(id){
	if(score[id] == undefined){
	} else {
		v = getValue(id);
		if (el(id+'_input')!=null && el(id+'_input').value=="__check_group__") {
			v_array = v.split(',');
			ret_score = 0;
			for (i = 0; i < v_array.length; i++){
				if (!(score[id][v_array[i]] == undefined)){
					ret_score += score[id][v_array[i]];
				}
			}
			return ret_score;
		} else if(score[id] == Infinity){
			v = getValue(id);
			if (parseInt(v) > 0 || parseInt(v) < 0) // is blijkbaar niet meer gelijk aan != 0
				return parseInt(v);
		} else if (score[id][v] == undefined) {		
		} else {
			return score[id][v]; 
		}
	}
	return 0; 
}

function file_selector_select(value, id){
	el(id).value = value;
}


function confirm_delete_row(parent, id, row){
	el('delete_confirmation_text').innerHTML = 'Weet u zeker dat u deze rij wilt verwijderen?';
	el('delete_confirmation_link').href = "javascript: delete_row('"+parent+"','"+id+"','"+row+"');";
	el('delete_confirmation').style.display = 'block';
}

function delete_row(parent, id, row){
	el('delete_confirmation').style.display = 'none';
	el(parent).removeChild(el(id));
	update_numbers(el(parent));
}

function update_numbers(parent){
	var children = getElementsByClassName(parent, 'dynamic_array_row_number', "div");
	for(i = 0; i < children.length; i++){
		children[i].innerHTML = (i + 1) + ".";
	}
}
if (typeof(dyn_array_count) == "undefined"){
	var dyn_array_count = 0;
	var row_count = new Array();
}

function add_row(parent, url, element, id_prefix){
	var loading_div = document.createElement("div");
	loading_div.id = 'loading_div_'+row_count[element];
	loading_div.innerHTML = loading_image;
	el(parent).appendChild(loading_div);

	AJAX.get(url+'&row='+row_count[element], add_row_content, {'id': row_count[element], 'parent': parent, 'id_prefix':id_prefix});
	row_count[element]++;
	
}

function add_row_content(content, params){
	var new_row = document.createElement("div");

	new_row.innerHTML = content;
	new_row.id = params.id_prefix + params.id;
	new_row.className = "dynamic_array_row move_item";


	el(params.parent).removeChild(el('loading_div_'+params.id));
	el(params.parent).appendChild(new_row);

	var scripts = new_row.getElementsByTagName("script");
	var head = document.getElementsByTagName("head")[0];
	for(var i=0;i<scripts.length;i++)
	{
		var script = document.createElement("script");
		script.type="text/javascript";
		// inline javascript
		if (scripts[i].text != '') {
			script.text=scripts[i].text;
		// externe javascript
		} else if (scripts[i].src != '') {
			script.src=scripts[i].src;
		}
		head.appendChild(script);
	}
	
	update_numbers(el(params.parent));
}

function select_group(id, select){
	var children = el(id).getElementsByTagName('input');
	for(i = 0; i < children.length; i++){
		children.item(i).checked = select;
	}
}

function ajaxSubmitForm(form_id, function_name, params)
{
	// Update eventuele FCK-editor fields
	if(typeof(FCKeditorAPI) != "undefined") {
		for (var name in FCKeditorAPI.Instances) {
			var oEditor = FCKeditorAPI.Instances[name];
			oEditor.UpdateLinkedField();
			delete FCKeditorAPI.Instances[name];
		}
	}
	
	if (typeof(el(form_id).method) == "undefined"){
		forms = el(form_id).getElementsByTagName('form');		
		if (forms.length == 1){
			form_id = forms[0].getAttribute("id");
		}
	}	

	url = el(form_id).action;
	new Ajax.Request(url, {
		parameters: Form.serialize(form_id),
		onSuccess: function(transport) {
			window[function_name](form_id, transport, params);
		}
	});
}

function fck_create(name, width, height, toolbarset, value, additional_options){
	var additional_options = additional_options;
	var FCKfield = new FCKeditor(name);
	FCKfield.Width = width;
	FCKfield.Height = height;
	FCKfield.ToolbarSet = toolbarset;
	FCKfield.Value = urldecode(value);
	FCKfield.Create();
	FCKfield= null;
}

/**
 * @author Martin
 * @since 2008-12-05
 * @param obj		object wat de aanroep doet
 * @param form_url	URL die de inhoud van het formulier ophaalt
 * @return
 */
function selectorShow(id, url)
{
	el(id + '_selector').innerHTML = loading_image;
	el(id + '_selector_dialog').style.display = "block";
	AJAX.get(url, showSelectorList, {'id': id});
}

function showSelectorList(content, params)
{
	document.getElementById(params.id + '_selector').innerHTML = content;
}

function selectorCancel(id)
{
	el(id + '_selector_dialog').style.display = "none";
}

function getSelectorValue(content, params)
{
	el(params.id + '_text').innerHTML = content;
	var command = "on_" + params.id + "_event();";
	if(command){
		eval(command);
	}
}

function selectorSelect(id, selector_name)
{
	eval("get"+selector_name+"Value(id)");
	el(selector_name + "_input").value = id;
	el(selector_name + '_selector_dialog').style.display = "none";
	el(selector_name + '_text').style.display = "block";
}

/**
 *
 *
 * @author Jamie
 * @since
 * @param
 * @return
 */
function selectorBase64Select(id)
{
	el(current_parameter+"_input").value = base64Decode(id);
	hideSelector();
}

/**
 * Laat de kalender zien
 *
 * @author Jamie
 * @since
 * @param
 * @return
 */
function toggleSelectorDisplay(id)
{
	if (el(id+"_selector_dialog").style.display == "block") {
		// Laat de kalender verdwijnen
		el(id+"_selector_dialog").style.display = "none";
	} else {
		el(id+"_selector_dialog").style.display = "block";
	}
}
/**
 * Laat de kalender zien
 *
 * @author Jamie
 * @since
 * @param
 * @return
 */
function toggleCalendar(calendar)
{
	if (el(calendar+"_calendar").style.display == "block") {
		// Laat de kalender verdwijnen
		el(calendar+"_calendar").style.display = "none";

	} else {
		// Haal op over welke datum het gaat
		date_time = el(calendar+"_input").value.split(" ");
		if (date_time.length == 2) {
			date_arr = date_time[0].split("-");
			time_arr = date_time[1].split(":");
		} else if (date_time.length == 1 && date_time[0].indexOf("-")>0) {
			date_arr = date_time[0].split("-");
			today = new Date();
			time_arr = new Array();
			time_arr[0] = today.getHours();
			time_arr[1] = today.getMinutes();
			time_arr[2] = today.getSeconds();
		} else {
			// Als er geen datum is gespecificeerd, moet de kalender van deze maand
			// worden getoond.
			today = new Date();
			date_arr = new Array();
			date_arr[2] = today.getDate();
			date_arr[1] = today.getMonth()+1;
			date_arr[0] = today.getFullYear();

			time_arr = new Array();
			time_arr[0] = today.getHours();
			time_arr[1] = today.getMinutes();
			time_arr[2] = today.getSeconds();
		}
		
		// Teken de kalender
		drawCalendar(calendar, Number(date_arr[0]), Number(date_arr[1]), Number(date_arr[2]), Number(time_arr[0]), Number(time_arr[1]), Number(time_arr[2]));

		
		// Plaats de kalender op de juiste positie
		el(calendar+"_calendar").style.display = "block";
	}
}

/**
 * Verander de kalender
 *
 * @author Jamie
 * @since
 * @param
 * @return
 */
function changeCalendar(obj, action)
{
	// Haal de waarden van de selectieboxen op
	infos = el(obj+"_calendar").getElementsByTagName("select");
	year_select = infos[0];
	month_select = infos[1];
	year = parseInt(year_select.value, 10);
	month = parseInt(month_select.value, 10);

	year_select_options = new Array();
	for(i=0; i<year_select.options.length; i++) {
		year_select_options[i] = parseInt(year_select.options[i].value, 10);
	}

	switch(action) {
		case "year-minus":
			year--;
			if(year_select_options.indexOf(year) == -1)
			{
				year++;
			}

			break;
		case "year-plus":
			year++
			if(year_select_options.indexOf(year) == -1)
			{
				year--;
			}

			break;
		case "month-minus":
			month--;
			if (month==0) {
				month = 12;
				year--;
				if(year_select_options.indexOf(year) == -1){
					year++;
					month =1;
				}
			}
			break;
		case "month-plus":
			month++;
			if (month==13) {
				month = 1;
				year++;
				if(year_select_options.indexOf(year) == -1){
					year--;
					month = 12;
				}
			}
			break;
		case "month-selected":

			break;
		case "year-selected":

			break;
	}

	drawCalendar(obj, year, month);
}

/**
 * Teken een kalender van het jaar "year" en de maand "month" met optioneel "day" gekleurd
 *
 * @author Jamie
 * @since
 * @param
 * @return
 */
function drawCalendar(obj, year, month, day, hour, minute, second)
{
	// Stel de selectieboxen in
	infos = el(obj+"_calendar").getElementsByTagName("select");
	infos[0].value = year;
	infos[1].value = month;
	// is er ook een tijd beschikbaar
	if (infos.length > 2){
		infos[2].value = hour;
		infos[3].value = minute;
		infos[4].value = second;
	}

	// Haal eerst de benodige datum gegevens op
	first_day_of_month = new Date(year, month-1, 1);
	first_weekday_of_month = (first_day_of_month.getDay()+6)%7;
	days_per_month = daysPerMonth(year, month);

	// Haal de rijen op
	rows = el(obj+"_calendar_table").getElementsByTagName("tr");
	var current_day = 0;

	// Vul de kalender
	for (row=1; row<=6; row++) {
		days = rows[row].getElementsByTagName("td");
		for(day=0; day<7; day++) {
			if ((row!=6 || day!=6) && (row!=6 || day!=5)) {
				days[day].innerHTML = "";
				days[day].onclick = "";
			}

			if ((row==1 && day==first_weekday_of_month) || (current_day>0 && current_day<days_per_month)) {
				current_day++;
				days[day].innerHTML = current_day;
				currentDate = el(obj+"_input").value.split("-");
				if(parseInt(current_day, 10) == parseInt(currentDate[2], 10) && parseInt(month, 10) == parseInt(currentDate[1], 10) && parseInt(year, 10) == parseInt(currentDate[0], 10)){
					days[day].style.backgroundColor = "yellow";
				}
				else{
					days[day].style.backgroundColor = "white";
				}
				days[day].onclick = function() {
					var f_day = this.innerHTML;
					var f_month = month;
					var f_year = year;

					if (f_month < 10) {
						f_month = "0"+Number(f_month);
					}
					if (f_day < 10) {
						f_day = "0"+Number(f_day);
					}

					selectDay(obj, f_year+"-"+f_month+"-"+f_day);
				}
			}
			else
			{
				days[day].style.backgroundColor = "white";
			}
		}
	}
}

function selectNull(obj) {
	el(obj+"_input").value = null;
	el(obj+"_display").innerHTML = "";
	toggleCalendar(obj);
}


function selectDay(obj, date) {
	// Stel de selectieboxen in
	infos = el(obj+"_calendar").getElementsByTagName("select");
	if (infos.length > 2){
		hour = infos[2].value;
		minute = infos[3].value;
		second = infos[4].value;
	
		if (hour < 10) {
			hour = "0"+hour;
		}
		if (minute < 10) {
			minute = "0"+minute;
		}
		if (second < 10) {
			second = "0"+second;
		}
	} else {
		hour = "00";
		minute = "00";
		second = "00";
	}
	

	el(obj+"_input").value = date+" "+hour+":"+minute+":"+second;
	
	date_arr = date.split("-");
	drawCalendar(obj, infos[0].value, infos[1].value, date_arr[2], hour, minute, second);

	selectDateTime(obj);
	window["on_"+obj+"_event"]();
}

function selectDateTime(obj) {
	
	// Haal op over welke datum het gaat
	date_time = el(obj+"_input").value.split(" ");
	date_arr = date_time[0].split("-");

	if(typeof(date_arr[1]) == "undefined") // Op vinkje geklikt, zonder datum te selecteren
	{
		alert("Selecteer een datum!");
	}
	else
	{
		// Stel de selectieboxen in
		infos = el(obj+"_calendar").getElementsByTagName("select");
		if (infos.length > 2) {
			hour = infos[2].value;
			minute = infos[3].value;
			second = infos[4].value;
			if (hour < 10) {
				hour = "0"+hour;
			}
			if (minute < 10) {
				minute = "0"+minute;
			}
			if (second < 10) {
				second = "0"+second;
			}
			
			el(obj+"_input").value = date_arr[0]+"-"+date_arr[1]+"-"+date_arr[2]+" "+hour+":"+minute+":"+second;
			el(obj+"_display").innerHTML = date_arr[2]+"-"+date_arr[1]+"-"+date_arr[0]+" "+hour+":"+minute+":"+second;
			
		} else {
			el(obj+"_input").value = date_arr[0]+"-"+date_arr[1]+"-"+date_arr[2];
			el(obj+"_display").innerHTML = date_arr[2]+"-"+date_arr[1]+"-"+date_arr[0];
		}
		toggleCalendar(obj);
	}
}

/**
 * Geef aan of het een schrikkeljaar is
 *
 * @author Jamie
 * @since
 * @param
 * @return
 */
function isLeapYear(year) {
	// Wel: 1996, 2000, 2004
	// Niet: 1995, 1900
	if (year%400==0) {
		return true;
	} else if (year%100==0) {
		return false;
	} else {
		return (year%4==0)
	}
}

/**
 * Geef aan of het een schrikkeljaar is
 *
 * @author Jamie
 * @since
 * @param
 * @return
 */
function daysPerMonth(year, month) {
	if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) {
		return 31;
	} else if (month==4 || month==6 || month==9 || month==11) {
		return 30;
	} else if (month==2 && !isLeapYear(year)) {
		return 28;
	} else if (month==2 && isLeapYear(year)) {
		return 29;
	} else {
		return false;
	}
}


function insert_textarea_variable(id, element)
{
	insertAtCursor(id, element, "");
}
