/*
 * ADOBE SYSTEMS INCORPORATED
 * Copyright 2007 Adobe Systems Incorporated
 * All Rights Reserved
 * 
 * NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the 
 * terms of the Adobe license agreement accompanying it. If you have received this file from a 
 * source other than Adobe, then your use, modification, or distribution of it requires the prior 
 * written permission of Adobe.
 */

function show_as_buttons_func() {
	var toret = false;
	if (!(typeof $NXT_LIST_SETTINGS == 'undefined' || typeof $NXT_LIST_SETTINGS['show_as_buttons'] == 'undefined' || $NXT_LIST_SETTINGS['show_as_buttons'] == false)) {
		toret = true;
	}
 	if (!(typeof $NAV_SETTINGS == 'undefined' || typeof $NAV_SETTINGS['show_as_buttons'] == 'undefined' || $NAV_SETTINGS['show_as_buttons'] == false)) {
		toret = true;
	}
	return toret;
}
show_as_buttons = "show_as_buttons_func()";
not_show_as_buttons = "!" + show_as_buttons;
/*
 * transforms a link to a button, keeping the link inner text, and adding the onclick event
 */
function KT_style_replace_with_button(el, add_event) {
	if (typeof add_event == 'undefined') {
		add_event = false;
	}
	var elnou = utility.dom.createElement('input', {
		'type' : 'button', 
		'value': el.innerHTML
	});

	el.style.display = 'none';
	elnou = utility.dom.insertAfter(elnou, el);

	if (add_event) {
		var onclick = el.onclick;
		elnou.onclick = onclick;
	}

	elnou.style.visibility = el.style.visibility;
	if (el.innerHTML == '') {
		elnou.style.display = 'none';
	}

	return elnou;
}

function KT_style_modify_custom_links(el) {
    var classes = utility.dom.getClassNames(el);
    if (1
    &&  Array_indexOf(classes, 'KT_link') < 0
    ) {
        return;
    }

    var elnou = KT_style_replace_with_button(el);
    /*utility.dom.attachEvent(*/elnou.onclick = function(e) {
        var a = this.previousSibling;
        if (!a.onclick) {
            var act = utility.dom.getLink(a);
            var parts = act.toString().split('?');
            if (parts.length == 1) {
                parts[1] = '';
            }
            var qs = new QueryString(parts[1]); var action_url = parts[0], variables = [];
            Array_each(qs.keys, function(key, i) {
                Array_push(variables, [key, qs.values[i]]);
            });

            var frm = utility.dom.createElement(
                "FORM", 
                {'action': act, 'method': 'GET', 'style': "display: none"}
            );
            Array_each(variables, function(input, i){
                frm.appendChild(utility.dom.createElement('INPUT', {'type': 'hidden', 'id': input[0], 'name': input[0], 'value': input[1]}));
            });

            frm = document.body.appendChild(frm);
            frm.submit();
        } else {
            var to_exec = a.onclick;
            a.onclick();
        }
    };/*);*/
    //elnou.className = 'button_big';
}

//[mtm]_[detail_key_value]
var tng_mtm_detail_key_re = /^mtm_(\d+)$/;
function tng_form_enable_details (checkbox_name) {
	var cbx = document.getElementById(checkbox_name);
	var state = !cbx.checked;
	var parts = checkbox_name.match(tng_mtm_detail_key_re);
	var related_input_re = new RegExp("^mtm_(.+?)_" + parts[1] + "$", "");

	Array_each(cbx.form.elements, function(input) {
		var input_name = input.name;
		if (input_name && related_input_re.test(input_name)) {
			if (typeof(input.widget_id) == 'undefined') {
				if (input.disabled != state) {
					input.disabled = state;
				}
			} else {
				try {
					window[input.widget_type][input.widget_id].setEnabled(!state);
				} catch(err) {}
			}
		}
	});
}


/*
 * this array holds the transformations for the list / form elements
 * each array item is an object with tthe following properties:
 * 	* selector : the string passwd to utility.dom.getElementsBySelector
 * 	* transform : transformation function whichch has a single parameter : the element to handle
 * 	* eval : condition which tells if the transformation is executed
 */
$TRANSFORMATIONS = [
	{
		'selector': function() {
			//'div.KT_tnglist div.KT_bottombuttons a.KT_edit_op_link'
			//'div.KT_tnglist div.KT_bottombuttons a.KT_delete_op_link', 
			//'div.KT_tnglist div.KT_bottombuttons a.KT_additem_op_link', 
			//'div.KT_tnglist div.KT_opbuttons a.KT_edit_op_link'
			//'div.KT_tnglist div.KT_opbuttons a.KT_delete_op_link', 
			//'div.KT_tnglist div.KT_opbuttons a.KT_additem_op_link', 
			var toret = [];
			for (var i = 0; i < $lists.length; i++) {
				if ($lists[i].kt_styles_attached) {
					continue;
				}
				var as = $lists[i].bottombuttons.getElementsByTagName('a');
				for (var j = 0; j < as.length; j++) {
					if (/(KT_edit_op_link|KT_delete_op_link|KT_additem_op_link)/.test(as[j].className)) {
						toret.push(as[j]);
					}
				}
				if ($lists[i].topbuttons) {
					var as = $lists[i].topbuttons.getElementsByTagName('a');
					for (var j = 0; j < as.length; j++) {
						if (/(KT_edit_op_link|KT_delete_op_link|KT_additem_op_link)/.test(as[j].className)) {
							toret.push(as[j]);
						}
					}
				}
			}
			return toret;
		}, 
		'transform': function(el) { 
			var elnou = KT_style_replace_with_button(el, true);
		}, 
		'eval': show_as_buttons
	}, 
	{
		'selector': function() {
			//'div.KT_tnglist th.KT_sorter a.KT_move_op_link', 
			var toret = [];
			for (var i = 0; i < $lists.length; i++) {
				if ($lists[i].kt_styles_attached) {
					continue;
				}
				var ths = $lists[i].table.getElementsByTagName('th');
				for (var j = 0; j < ths.length; j++) {
					if (/KT_sorter/.test(ths[i].className)) {
						var as = ths[i].getElementsByTagName('a');
						for (var k = 0; k < as.length; k++) {
							if (/KT_move_op_link/.test(as[k].className)) {
								toret.push(as[k]);
								break;
							}
						}
					}
				}
			}
			return toret;
		}, 
		'transform': function(el) {
			var elnou = KT_style_replace_with_button(el, true);
			elnou.style.display = 'none';
		}, 
		'eval': show_as_buttons
	}, 
	{
		'selector' : function() {
			var toret = [];
			for (var i = 0; i < $lists.length; i++) {
				if ($lists[i].kt_styles_attached) {
					continue;
				}
				var as = $lists[i].table.getElementsByTagName('a');
				for (var j = 0; j < as.length; j++) {
					if (/(KT_edit_link|KT_moveup_link|KT_movedown_link|KT_delete_link|KT_link)/.test(as[j].className)) {
						toret.push(as[j]);
					}
				}
			}
			return toret;
		}, 
		'transform': function(el) {
			var elnou = KT_style_replace_with_button(el);
			elnou.onclick = function(e) {
				var a = this.previousSibling;
				if (/(KT_movedown_link|KT_moveup_link|KT_delete_link)/.test(a.className)) {
					var to_exec = a.onclick;
					try {
						a.onclick(e);
					} catch(e) { }
				} else if (/(KT_link)/.test(a.className)) {
					if (!a.onclick) {
						var act = utility.dom.getLink(a);
						var parts = act.toString().split('?');
						if (parts.length == 1) {
							parts[1] = '';
						}
						var qs = new QueryString(parts[1]); var action_url = parts[0], variables = [];
						Array_each(qs.keys, function(key, i) {
							Array_push(variables, [key, qs.values[i]]);
						});

						var frm = utility.dom.createElement(
							"FORM", 
							{'action': act, 'method': 'GET', 'style': "display: none"}
						);
						Array_each(variables, function(input, i){
							frm.appendChild(utility.dom.createElement('INPUT', {'type': 'hidden', 'id': input[0], 'name': input[0], 'value': input[1]}));
						});

						frm = document.body.appendChild(frm);
						if (typeof PanelForm_overrideSubmit == 'function') {
							frm.submit = PanelForm_overrideSubmit;
						}
						frm.submit();
					} else {
						var to_exec = a.onclick;
						a.onclick();
					}
				} else if (/(KT_edit_link)/.test(a.className)) {
					try {
						var o = utility.dom.setEventVars(e);
						var table = utility.dom.getParentByTagName(this, 'table');
						var row = utility.dom.getParentByTagName(this, 'tr');

						var tmp = utility.dom.getElementsByClassName(row, 'id_checkbox')[0];
						var myinput = null;
						if (tmp.type && tmp.type.toLowerCase() == 'checkbox' && tmp.name.toString().match(/^kt_pk/)) {
							myinput = tmp;
						}

						var inputs = utility.dom.getElementsByClassName(table, 'id_checkbox');
						Array_each(inputs, function(input) {
							if (input.type && input.type.toLowerCase() == 'checkbox' && 
								input.name.toString().match(/^kt_pk/)) {
								input.checked = (input == myinput);
							}
						});
						nxt_list_edit_link_form(this, myinput.previousSibling.href);
					} catch(e) {
						window.location.href = a.href;
					}					
				} else {
					window.location.href = a.href;
				}
			};/*);*/
			var move_up = /KT_moveup_link/.test(el.className);
			var move_down = /KT_movedown_link/.test(el.className);
			if (move_up || move_down) {
				if (move_up && typeof $nxt_move_up_background_image != 'undefined' || move_down && typeof $nxt_move_down_background_image != 'undefined') {
					elnou.value = "";
				}
				elnou.className = 'button_smallest KT_button_move_' + (move_up?'up':'down');
			} else {
				elnou.className = 'button_big';
			}

		}, 
		'eval': show_as_buttons
	},
	{
		/* ajaxify the back to master link*/
		'selector' : function() {
			var toret = [];
			if (typeof $ctrl != 'undefined') {
				for (var i = 0; i < $lists.length; i++) {
					if ($lists[i].kt_styles_attached) {
						continue;
					}
					var trs = utility.dom.getElementsByClassName($lists[i].inner, "KT_masterlink", "TR");
					for (var j=0; j<trs.length; j++) {
						var links = trs[j].getElementsByTagName("A");
						for (var k=0; k < links.length; k++) {
							if (links[k].href.indexOf('includes/nxt/back.php') != -1) {
								toret.push(links[k]);
							}
						}
					}
				}
			}
			return toret;
		},
		'transform': function(el) {
			el.onclick = function() {
				$ctrl.loadPanels(el.href);	
				return false;
			}
		},
		'eval': 1
	},
	{
		'selector' : function() {
			var toret = [];
			for (var i = 0; i < $lists.length; i++) {
				if ($lists[i].kt_styles_attached) {
					continue;
				}
				var as = $lists[i].bottombuttons.getElementsByTagName('a');
				for (var j = 0; j < as.length; j++) {
					if (/KT_link/.test(as[j].className)) {
						toret.push(as[j]);
					}
				}
				if ($lists[i].topbuttons) {
					var as = $lists[i].topbuttons.getElementsByTagName('a');
					for (var j = 0; j < as.length; j++) {
						if (/KT_link/.test(as[j].className)) {
							toret.push(as[j]);
						}
					}
				}
			}
			return toret;
		}, 
		'transform': KT_style_modify_custom_links, 
		'eval': show_as_buttons
	}, 
	{
		'selector' : function() {
			var toret = [];
			if ($lists.length > 0) {
				for (var i = 0; i < $lists.length; i++) {
					if ($lists[i].kt_styles_attached) {
						continue;
					}
					if ($lists[i].toptextnav) {
						var as = $lists[i].toptextnav.getElementsByTagName('a');
						for (var j = 0; j < as.length; j++) {
							if (/(first|prev|next|last)/.test(as[j].parentNode.className)) {
								toret.push(as[j]);
							}
						}
					}
					if ($lists[i].bottomtextnav.getAttribute("kt_styles_attached")) {
						continue;
					}
					$lists[i].bottomtextnav.setAttribute("kt_styles_attached", true);
					var as = $lists[i].bottomtextnav.getElementsByTagName('a');
					for (var j = 0; j < as.length; j++) {
						if (/(first|prev|next|last)/.test(as[j].parentNode.className)) {
							toret.push(as[j]);
						}
					}
				}
			} else {
				var divs = utility.dom.getElementsByClassName(document, 'KT_textnav', 'div');
				if (divs) {
					for (var i = 0; i < divs.length; i++) {
						if (divs[i].getAttribute("kt_styles_attached")) {
							continue;
						}
						divs[i].setAttribute("kt_styles_attached", true);
						var as = divs[i].getElementsByTagName('a');
						for (var j = 0; j < as.length; j++) {
							if (/(first|prev|next|last)/.test(as[j].parentNode.className)) {
								toret.push(as[j]);
							}
						}
					}
				}
			}
			return toret;		
		}, 
		'transform' : function(el) {
			var li = el.parentNode;
			var elnou = KT_style_replace_with_button(el);
			if (!el.href.match(/void\(0\)/)) {
				elnou.onclick = function(e) {
					if (typeof $ctrl != 'undefined') {
						$ctrl.loadPanels(el.href);
					} else {
						window.location.href = el.href;
					}
				};
			} else {
				//utility.dom.classNameAdd(el.parentNode, 'disabled');
				var inp = el.parentNode.getElementsByTagName('input');
				if (inp.length > 0) {
					inp[0].disabled = true;
				}
			}
			var values = {'first': '<<', 'prev': '<', 'next': '>', 'last': '>>'};
			elnou.value = values[li.className.toString().replace(/ disabled/, '')];
			elnou.className = 'button_smallest' + (el.href.match(/void\(0\)/) ? ' disabled' : '');
		}, 
		'eval': show_as_buttons
	}, 
	{
		'selector' : function() {
			var toret = [];
			if ($lists.length > 0) {
				for (var i = 0; i < $lists.length; i++) {
					if ($lists[i].kt_styles_attached) {
						continue;
					}
					if ($lists[i].toptextnav) {
						var as = $lists[i].toptextnav.getElementsByTagName('a');
						for (var j = 0; j < as.length; j++) {
							if (/(first|prev|next|last)/.test(as[j].parentNode.className)) {
								toret.push(as[j]);
							}
						}
					}
					var as = $lists[i].bottomtextnav.getElementsByTagName('a');
					for (var j = 0; j < as.length; j++) {
						if (/(first|prev|next|last)/.test(as[j].parentNode.className)) {
							toret.push(as[j]);
						}
					}
				}
			} else {
				var divs = utility.dom.getElementsByClassName(document, 'KT_textnav', 'div');
				if (divs) {
					for (var i = 0; i < divs.length; i++) {
						if (divs[i].getAttribute("kt_styles_attached")) {
							continue;
						}
						divs[i].setAttribute("kt_styles_attached", true);
						var as = divs[i].getElementsByTagName('a');
						for (var j = 0; j < as.length; j++) {
							if (/(first|prev|next|last)/.test(as[j].parentNode.className)) {
								toret.push(as[j]);
							}
						}
					}
				}
			}
			return toret;		
		}, 
		'transform' : function(el) {
			if (!el.href.match(/void\(0\)/)) {
			} else {
				utility.dom.classNameAdd(el, 'disabled');
			}
		}, 
		'eval': not_show_as_buttons
	}, 
	{
		'selector': function() { 
			var toret = [];
			var div = document.getElementById('KT_tngtrace');
			if (div && !div.getAttribute("kt_styles_attached")) {
				div.setAttribute("kt_styles_attached", true);
				var as = div.getElementsByTagName('a');
				for (var i = 0; i < as.length; i++) {
					toret.push(as[i]);
				}
			}
			return toret;
		}, 
		'transform': function(el) { 
			var elnou = KT_style_replace_with_button(el, true);
		}, 
		'eval': show_as_buttons
	}, 
	{
		'selector_text': 'div.KT_tnglist table.KT_tngtable tr.KT_row_filter input[type="submit"]', 
		'selector' : function() {
			var toret = [];
			for (var i = 0; i < $lists.length; i++) {
				if ($lists[i].kt_styles_attached) {
					continue;
				}
				var inps = $lists[i].table.getElementsByTagName('input');
				for (var j = 0; j < $lists[i].table.rows.length; j++) {
					var row = $lists[i].table.rows[i];
					if (/KT_row_filter/.test(row.className)) {
						var inps = row.getElementsByTagName('input');
						var type = '';
						for (var k = 0; k < inps.length; k++) {
							type = inps[k].getAttribute('type');
							if (type == null) {
								type = 'text';
							}
							if (type.toString().toLowerCase == 'submit') {
								toret.push(inps[k]);
							}
						}
					}
				}
			}
			return toret;
		}, 
		'transform': function(el) {
			el.className = 'KT_row_filter_submit_button';
		}, 
		'eval': "1"
	}, 
	{
		'selector' : function() {
			//'div.KT_tng input[type="text"]'
			//'div.KT_tng input[type="widget"]'
			//'div.KT_tng input[type="password"]'
			var toret = [];
			for (var i = 0; i < $lists.length; i++) {
				if ($lists[i].kt_styles_attached) {
					continue;
				}
				var inps = $lists[i].main.getElementsByTagName('input