var Utility = {
	result: 0,
	comfim_status: 0,
	showLoading: function(id) {
		$(id).show();
		$(id).update('<div class="loading"><div class="img_loading">&nbsp;</div><div class="text_loading">Loading...</div></div>');
	},
	getBoxMessage: function(msg) {
		return '<div align="center" style="width: 250px; font-weight: bold; font-size: 14px;"><br>'+msg+'<br><br><input type="button" value="OK" onclick="FloatBox.close();"><br><br></div>';
	},
	getConfirmMessage: function(msg, file, pos) {
		var box = '<div align="center" style="width: 250px; font-weight: bold; font-size: 14px;">';
		box += '<br>'+msg+'<br><br>';
		box += '<input type="button" value="OK" onclick="Utility.del2(\''+file+'\', \''+pos+'\');">&nbsp;';
		box += '<input type="button" value="Cancel" onclick="FloatBox.close();">';
		box += '<br><br></div>';
		return box;
	},
	writeMessage: function(xml, target) {
		var color;
		if(xml.getElementsByTagName('result')[0].childNodes[0].nodeValue=='1') {
			this.result = 1;
			color = '#0000FF';
		}
		else {
			this.result = 0;
			color = '#FF0000';
		}
		var returnValue = xml.getElementsByTagName('return')[0].childNodes[0].nodeValue;
		var nodeMsg = xml.getElementsByTagName('text');
		var t = '<div style="color:'+color+'; padding: 10px;">';
		t += '<div style="display: none" id="return_value">'+returnValue+'</div>';
		if(nodeMsg.length == 1) {
			t += '<div>'+nodeMsg[0].childNodes[0].nodeValue+'</div>';
		}
		else {
			t += '<ul>';
			for(var i=0; i<nodeMsg.length; i++) {
				t += '<li>'+nodeMsg[i].childNodes[0].nodeValue+'</li>';
			}
			t += '</ul>';
		}
		t += '</div>';
		$(target).update(t);
	},
	save: function(formname, file, callback) {
		if(tinyMCE) tinyMCE.triggerSave(true, true);
		var frm = $(formname);
		var params = frm.serialize(true);
		var target = 'process';
		frm.hide();
		new Ajax.Request(file, {
			method: 'post',
			parameters: params,
			onCreate: Utility.showLoading(target),
			onComplete: function(transport) {//alert(transport.responseText)
				Utility.writeMessage(transport.responseXML, target);
				if(callback) setTimeout(callback, 100);
			}
		});
	},
	del: function(file, position) {
		FloatBox.create(300, 200);
		FloatBox.insert(Utility.getConfirmMessage('Please comfirm to delete', file, position.identify()));
	},
	del2: function(file, position) {
		var target = 'content';
		new Ajax.Request(file, {
			method: 'get',
			onCreate: this.showLoading(target),
			onComplete: function(transport) {//alert(transport.responseText)
				Utility.result = parseInt(transport.responseXML.getElementsByTagName('result')[0].childNodes[0].nodeValue);
				var msg = transport.responseXML.getElementsByTagName('text')[0].childNodes[0].nodeValue;
				if(Utility.result == 1) {
					Effect.Pulsate($(position).up('tr'));
					setTimeout(function(){$(position).up('tr').remove()}, 2000);
				}
				FloatBox.insert(Utility.getBoxMessage(msg));
			}
		});
	},
	xDeleteCreate: function(file) {
		document.write('<div style="display: none;" id="box_delete">');
		document.write('	<div id="box_delete_process" style="display: none;"></div>');
		document.write('	<div id="box_delete_body" style="padding-bottom: 10px;">Press "Delete" to delete all checked list</div>');
		document.write('	<span id="box_delete_list" style="display: none;"></span>');
		document.write('	<div id="box_delete_botton"><input type="button" value="Delete" onclick="Utility.xDelete(\''+file+'\')"> <input type="button" value="Cancel" onclick="Utility.xDeleteCancel()"></div>');
		document.write('</div>');
	},
	xDeleteShow: function() {
		$('box_delete_process').hide();
		$('box_delete_body').show();
		$('box_delete_botton').show();
		$('box_delete').show();
		var chk = $$('.chk_delete');
		var list = '';
		for(var i=0; i<chk.length; i++) {
			if(chk[i].checked) {
				if(list != '') list += ',';
				list += chk[i].value;
			}
		}
		$('box_delete_list').update(list);
	},
	xDelete: function(file) {
		if($('box_delete_list').innerHTML == '') alert('No checked list');
		else {
			if(confirm('Please confirm to delete following checked the list')) {
				$('box_delete_body').hide();
				$('box_delete_botton').hide();
				new Ajax.Request(file+'?do=delete_x&mode=xml&list='+$('box_delete_list').innerHTML, {
					method: 'get',
					onCreate: Utility.showLoading('box_delete_process'),
					onComplete: function(transport) {//alert(transport.responseText)
						result = parseInt(transport.responseXML.getElementsByTagName('result')[0].childNodes[0].nodeValue);
						var msg = transport.responseXML.getElementsByTagName('text')[0].childNodes[0].nodeValue;
						if(result == 1) {
							$('box_delete_list').innerHTML.split(',').each(function(id) {
								Effect.Pulsate($('row_'+id));
								setTimeout(function(){$('row_'+id).remove()}, 2000);
							});
						}
						$('box_delete_process').update('<p>'+msg+'</p>'+'<input type="button" value="Close" onclick="$(\'box_delete\').hide();">');
					}
				});
			}
		}
	},
	xDeleteCancel: function() {
		$$('.chk_delete').each(function(obj) {
			obj.checked = false;
		});
		$('box_delete').hide();
	},
	check: function(obj, check_type) {
		var valid_status = 1;
		var msg_pass = '<span class="pass" style="color: #0000FF">&nbsp;[&#10003;] Available!</span>';
		var msg_fail_empty = '<span class="fail" style="color: #FF0000">&nbsp;[&#10007;] Field can not empty.</span>';
		switch(check_type) {
			case 0:
				if(obj.value == '') {
					valid_status = 0;
					$(obj.id).up('span').next(0).innerHTML = msg_fail_empty;
				}
			break;
		}
		if(valid_status == 1) {
			$(obj.id).up('span').next(0).innerHTML = msg_pass;
		}
	},
	initEdittor: function() {
		tinyMCE.init({
			mode : "textareas",
			theme : "advanced",
			plugins : "table,advimage,advlink,inlinepopups,media,paste,noneditable,visualchars,xhtmlxtras,wordcount,advlist",
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontsizeselect,|,bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,code",
			theme_advanced_buttons2 : "forecolor,backcolor,|,tablecontrols,|,hr,|,sub,sup,|,charmap,image,media",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resize_horizontal : false,
			theme_advanced_resizing : true,
			content_css : "css/tinymce.css"
		});
	},
	rowOver: function(obj) {
		obj.className = 'row_over';
	},
	rowOut: function(obj) {
		obj.className = 'row_out';
	}
};
