function ajax_do(action, j_id, j_conf, j_post, j_container) {
	call = 'action=' + encodeURIComponent(action) + '&j_id=' + encodeURIComponent(j_id) + '&j_conf=' + encodeURIComponent(j_conf) + '&j_post=' + jsap(j_post) + '&j_container=' + j_container;
	var postReq = new XHR();
	postReq.connect(url,"POST",call,showContent);
	function showContent(XML) {  
		xml = XML.responseText;
		var temp = xml.split('<||>');
		var body = document.getElementById('leftcolumn');
		if (temp[1]!='') {
			body.innerHTML = temp[1];
			if (temp[1].search(/textarea/i) > 0) {
				activateEditor('text_1');
			}
		}
		var smenu = document.getElementById('subnav_content');
		smenu.innerHTML = temp[2];
	}
}

function fakejax_do(action, j_id, j_conf, j_post, j_container) {
        // Does URL begin with http?
        call = '?' + Math.random(0, 1000) + '=' + Math.random(0, 1000);
        		//call = base_url + call + '&action=' + encodeURIComponent(action) + '&j_id=' + encodeURIComponent(j_id) + '&j_conf=' + encodeURIComponent(j_conf) + '&j_post=' + jsap(j_post) + '&j_extra=' + jsap(j_extra) + '&j_container=' + j_container;
        call = url + call + '&action=' + encodeURIComponent(action) + '&j_id=' + encodeURIComponent(j_id) + '&j_conf=' + encodeURIComponent(j_conf) + '&j_post=' + jsap(j_post) + '&j_container=' + j_container;
        // Create new JS element
        var jsel = document.createElement('SCRIPT');
        jsel.type = 'text/javascript';
        jsel.src = call;

        // Append JS element (therefore executing the 'AJAX' call)
		document.body.appendChild(jsel);
}

function jsap(a) {
    var a_php = "";
    var total = 0;
    for (var key in a)
    {
        ++ total;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + encodeURIComponent(String(a[key])) + "\";";
    }
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php;
}

function submit_form(action, id, conf, container) {
	deactivateEditors();
	if (action.substring(0,5) != 'read_' && conf!='cancel') {
		var input_array = document.getElementsByTagName('input');
		var select_array = document.getElementsByTagName('select');
		var textarea_array = document.getElementsByTagName('textarea');
		var input_post = new Array();
		//var extra_post = new Array();
		for(i=0;i<input_array.length;i++) {
			input_name = encodeURIComponent(input_array[i].getAttribute('name'));
			input_type = encodeURIComponent(input_array[i].getAttribute('type'));
			input_value = input_array[i].value;
			var input_incl = input_name.substring(0,5);
			if (input_incl=='incl_') {
				input_name = input_name.substring(5,input_name.length);
				if (input_type=='text' || input_type=='password' || input_type=='hidden' ||  input_type=='file') {
					input_post[input_name] = input_value;
				}
				else {
					//extra_post[input_type] = input_value;
				}
			}
		}
		for(i=0;i<textarea_array.length;i++) {
			input_name = encodeURIComponent(textarea_array[i].getAttribute('name'));
			input_name = input_name.substring(5,input_name.length);
			input_value = textarea_array[i].value;
			input_post[input_name] = input_value;
		}
		for(i=0;i<select_array.length;i++) {
			input_name = encodeURIComponent(select_array[i].getAttribute('name'));
			input_name = input_name.substring(5,input_name.length);
			input_value = select_array[i].value;
			input_post[input_name] = input_value;
		}
		//window.console.log(input_post);
		if (action=='login' || action.substring(0,7) != 'update_' || action.substring(0,7) != 'create_') {
			var div = document.getElementById('leftcolumn');
			div.innerHTML = '<center><p><br /><br /><br /><br /></p><img src=\"/img/login_loader.gif\" /><br /><br /><br /><br /><br /></center>';
		}
	}
	if (action=='confirm') {
		fakejax_do(action, id, conf, input_post, container);
	}
	else {
		if (action=='update') {
			if (document.getElementsByName('incl_prod')) {
				var prod = document.getElementsByName('incl_prod');
			}
			if (document.getElementsByName('incl_qnty')) {
				var qnty = document.getElementsByName('incl_qnty');
			}
			for(i=0;i<prod.length;i++) {
				ajax_do(action, prod[i].value, qnty[i].value);
			}
		}
		else {
			ajax_do(action, id, conf, input_post, container);
		}
	}
}

function autoUpload() {
	if(document.all && !document.getElementById) {
    	document.getElementById = function(id) {
    	     return document.all[id];
    	}
	}
	var file = document.getElementById('file').value;
	if (file != "" && file.indexOf('.') != -1) {
		document.upload.submit();
	}
}

function loader() {
	var file = document.getElementById('file').value;
	if (file != "" && file.indexOf('.') != -1) {
		var div = document.getElementById('uploadLoader');
		div.style.background = 'url(/img/login_loader.gif) no-repeat';
		div.innerHTML = '<br />';
	}
	else {
		var div = document.getElementById('uploadLoader');
		div.innerHTML = 'You cannot currently upload folders, please add files only.';
	}
}

var activeEditors = new Array();

function activateEditor(id) {
    activeEditors[activeEditors.length] = id;
    toggleEditor(id);
}

function deactivateEditors() {
    for(x=0;x<activeEditors.length;x++) {
        toggleEditor(activeEditors[x]);
    }
    activeEditors.length = 0;
}

// functions
function toggleEditor(id) {
    if (tinyMCE.getInstanceById(id) == null) {
        tinyMCE.execCommand('mceAddControl', false, id);
    }
    else {
    	var bodyClass = document.getElementById('body').className;
    	if (bodyClass == 'mceFullscreenPos') {
    		tinyMCE.execInstanceCommand(id,'mceFullScreen');
    	}
        tinyMCE.execCommand('mceRemoveControl', false, id);
    }
}

tinyMCE.init({
    mode : "none",
	theme : "advanced",
	plugins : "simplebrowser,table,save,advimage,advlink,iespell,insertdatetime,preview,zoom,searchreplace,print,contextmenu,paste,directionality,fullscreen",
    plugin_simplebrowser_width : "600",
    plugin_simplebrowser_height : "400",
    plugin_simplebrowser_browselinkurl : "/jscripts/plugins/simplebrowser/browser.html?Connector=connectors/php/connector.php",
    plugin_simplebrowser_browseimageurl : "/jscripts/plugins/simplebrowser/browser.html?Type=Image&Connector=connectors/php/connector.php",
    plugin_simplebrowser_browseflashurl : "/jscripts/plugins/simplebrowser/browser.html?Type=Flash&Connector=connectors/php/connector.php",
	theme_advanced_buttons1_add_before : "save,newdocument,print,separator",
	theme_advanced_buttons1_add : "separator,forecolor,backcolor,separator,fullscreen",
	theme_advanced_buttons2_add : "separator,insertdate,inserttime,zoom",
	theme_advanced_buttons2_add_before: "search,replace,separator",
	theme_advanced_buttons3_add_before : "tablecontrols,separator",
	theme_advanced_buttons3_add : "iespell",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	content_css : "/css/styles.css",
	plugin_insertdate_dateFormat : "%B %d, %Y",
	plugin_insertdate_timeFormat : "%I:%M %p",
	paste_use_dialog : false,
	theme_advanced_resizing : true,
	theme_advanced_resize_horizontal : false,
	paste_auto_cleanup_on_paste : true,
	paste_convert_headers_to_strong : false,
	paste_strip_class_attributes : "all",
	paste_remove_spans : false,
	paste_remove_styles : false		
});