function get_check_value(chkObj){
	if(!chkObj)
		return false;
	var c_value = '';
	for (var i=0; i < chkObj.length; i++){
		if (chkObj[i].checked){
			c_value += chkObj[i].value+',';
		}	
	}
	return c_value;
}

(
	function(mtmetabox){
		mtmetaboxObj = 
			{
				init : function(){
						if(mtmetabox("#info_message").html() == null){
							return;
						}
						var msCkedVal;
				//=================== set up object for activate palette form ========================================================================
						var h = mtmetabox("#dummy_form form");
						h.submit(function(){
								mtmetaboxObj.ajaxSwitchpalette(this);
								return false;
							});										
						mtmetabox("#update_ms").click(function(){	
								msCkedVal = get_check_value(document.getElementsByName("menu_sections[]"));
								var tempV =mtmetabox("#hpostId input[name=my_postId]").val();
								actionChoise='&actionChoise=none&menu_sections='+msCkedVal+'&thePostId='+tempV;
								h.submit();
								return true;
							});
						return;				
					},
				ajaxSwitchpalette : function(f){						
						f=mtmetabox(f);					
						var data=f.serialize();
							data+=actionChoise;
						mtmetabox.ajax({
							url: "/wp-admin/admin-ajax.php",
							type: 'POST',
							data: data,
							dataType: 'html',// == can be script
							success:function(result, status, XMLHttpRequest){
								var s=mtmetabox("#info_message");
								var r=result.replace(/&#8203;/gi,'');
								// if(r.search(/document.getElementById/i)==-1){
									//s.html("");
									// document.getElementById("info_message").innerHTML="<div style=\'color: #111111\' class=\'error\'>action failed</div>";
									// document.getElementById("info_message").innerHTML="<div style=\'color: #111111\' class=\'error\'>"+r+" fail</div>"
									// return false;
								// }
								// document.getElementById("info_message").innerHTML="<div style=\'color: #111111\' class=\'error\'>"+r+"</div>"
								document.getElementById("menu_sd").innerHTML = r;
								s.show();
								
								return false;
							},
							error:function(x,e){
								if(x.status==0){
								alert('You are offline!!\n Please Check Your Network.');
								}else if(x.status==404){
								alert('Requested URL not found.');
								}else if(x.status==500){
								alert('Internel Server Error.');
								}else if(e=='parsererror'){
								alert('Error.\nParsing JSON Request failed.');
								}else if(e=='timeout'){
								alert('Request Time out.');
								}else {
								alert('Unknow Error.\n'+x.responseText);
								}
								return false;
							}								
						});
					}
					
			};		
		mtmetabox(document).ready(function(){
			mtmetaboxObj.init()
		})
	}
)(jQuery);