var EcmDropdownEditor_show = function(container) {
 
    var div = $('[name='+container+']');
    var module = $('form[name=EditView]').find('input[name=module]').val();
    
    var params = new Array;
    params[0] = module;
    params[1] = container;
    
    // AJAX call
    jQuery.ajax({
        type : 'POST',
        url : 'index.php?entryPoint=HandleEcmAjax',
        data : {
            ecmclass : 'EcmDropdownEditor',
            job : 'getEditView',
            params : utf8_to_b64(JSON.stringifyNoSecurity(params)),
        },
        dataType : 'json',
        async : false,
        success : function(response) {
            div.html(response[0]);
            div.show();
            return;
        },
        error : function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError);
        }
    });
    
    div.show();
}

var EcmDropdownEditor_addOption = function(key) {
	// AJAX call
	jQuery.ajax({
		type : 'POST',
		url : 'index.php?entryPoint=HandleEcmAjax',
		data : {
			ecmclass : 'EcmDropdownEditor',
			job : 'getOptionTemplate',
		},
		dataType : 'json',
		async : false,
		success : function(response) {
			var optDiv = jQuery.parseHTML(response[0]);
			jQuery(response[0]).insertBefore(
					jQuery('[name=' + key + ']').find('span'));
			return;
		},
		error : function(xhr, ajaxOptions, thrownError) {
			console.log(thrownError);
		}
	});
}

var EcmDropdownEditor_save = function(dom, container) {
	if (!EcmDropdownEditor_validate(dom)) {		
		return false;
	}
	var doms = EcmDropdownEditor_parseArrays(dom);
	var params = new Array;
	params[0] = dom; //name of list
	params[1] = doms;//list of values
	// AJAX call
	jQuery.ajax({
		type : 'POST',
		url : 'index.php?entryPoint=HandleEcmAjax',
		data : {
			ecmclass : 'EcmDropdownEditor',
			job : 'saveDom',
			params : utf8_to_b64(JSON.stringifyNoSecurity(params)),
		},
		dataType : 'json',
		async : false,
		success : function(response) {
		    var div = $('[name='+container+']');
		    div.hide();
		    EcmDropdownEditor_refreshField(container);
			return;
		},
		error : function(xhr, ajaxOptions, thrownError) {
			console.log(thrownError);
		}
	});	
}

var EcmDropdownEditor_refreshField = function(container) {
    
    var module = $('form[name=EditView]').find('input[name=module]').val();
    
    var params = new Array;
    params[0] = module;//list of values
    params[1] = container;//list of values
    // AJAX call
    jQuery.ajax({
        type : 'POST',
        url : 'index.php?entryPoint=HandleEcmAjax',
        data : {
            ecmclass : 'EcmDropdownEditor',
            job : 'refreshField',
            params : utf8_to_b64(JSON.stringifyNoSecurity(params)),
        },
        dataType : 'json',
        async : false,
        success : function(response) {
            var tmp = container.split("___");
            $('form[name=EditView]').find('select[name='+tmp[0]+']').html(response[0]);
            return;
        },
        error : function(xhr, ajaxOptions, thrownError) {
            console.log(thrownError);
        }
    }); 
}

var EcmDropdownEditor_cancel = function(container) {
    var div = $('[name='+container+']');
    div.html('');
    div.hide();
}

var EcmDropdownEditor_parseArrays = function(dom) {
	var normal = {};
	var options = jQuery('[name=' + dom + ']').children('div');
	jQuery.each(options, function(key, val) {
		//check normal inputs
		var optionKey = jQuery(val).attr('name');
		var values = jQuery(val).children('input:not([name$=ext])');
		jQuery.each(values, function(key2, val2) {
			name = jQuery(val2).attr('name');
			if (!normal[name])
				normal[name] = {};
			normal[name][optionKey] = jQuery(val2).val();
		});
	});
	return normal;
}

var EcmDropdownEditor_validate = function(dom) {
	var error = false;
	// get all options in DIV name = dom
	var options = jQuery('[name=' + dom + ']').children('div');
	// loop throw options looking for errors
	jQuery.each(options, function(key, val) {
		// check empty options
		var empty = true;
		var values = jQuery(val).children('input');
		jQuery.each(values, function(key2, val2) {
			valInput = jQuery(val2).val();
			valInput = valInput.trim();
			if (valInput.length != 0)
				empty = false;
		});
		if (empty) 
			jQuery(val).remove(); // delete empty option
		values = null;
		//check normal inputs
		values = jQuery(val).children('input:not([name$=ext])');
		jQuery.each(values, function(key2, val2) {
			valInput = jQuery(val2).val();
			valInput = valInput.trim();
			if (valInput.length == 0) {
				jQuery(val2).addClass('dropdowneditor_error');
				error = true;
			} else
				jQuery(val2).removeClass('dropdowneditor_error');
		});
	});
	if (error) {
		return false;
	}
	return true;
}

var EcmDropdownEditor_moveElementUp = function(el) {
	var parent = jQuery(el).parent();
	var prev = parent.prev();
	if (prev.length == 0)
		return;
	parent.insertBefore(prev);
}

var EcmDropdownEditor_moveElementDown = function(el) {
	var parent = jQuery(el).parent();
	var next = parent.next();
	if (next.length == 0)
		return;
	parent.insertAfter(next);
}

//helper 
function utf8_to_b64( str ) {
    return window.btoa(unescape(encodeURIComponent( str )));
}

function b64_to_utf8( str ) {
    return decodeURIComponent(escape(window.atob( str )));
}


var cl = function(m) {
	console.log(m);
}