function getData(item){
	$('.tab').removeClass('tab_over');
	$('#'+item).addClass('tab_over');
	
	var total_elem = $('#red-tab > .tab').size();
	$('#red-tab > .tab').each(function(index, domEle){
		
		if($(domEle).hasClass('tab_over')){
			$(domEle).css("z-index", total_elem + 1);
		}else{
			$(domEle).css("z-index", total_elem - index);
		}
		
	});
	
	var html_loading = "<div class='loading-content'><img src='images/loading45.gif' /></div>";
	$('#tab-display').html(html_loading);
	
	
	
	$.post("data_loading/get_member_data.php", {item:item},
			   function(data){
				$('#tab-display').html(data);
				if(item == "become-member"){
					runCKEditor();
					$("#member_category").multiselect();
				}
	});

	
}

function change_membertype(member_type){
	
	$('#corporate').hide();
	$('#individual').hide();
	
	$('#'+member_type).show();
	
}

function searchByCate(){
	
	$('#member-sort-alp > li').removeClass('active');
	$('#member-alp-all').addClass('active');
	$('#member_name_search').val('')
	
	var cate = $('#member_cate').val();
	var html_loading = "<img style='margin:80px 280px;' src='images/loading45.gif' />";
	$('#member-list').html(html_loading);
	
	
	
	$.post("data_loading/get_member_list.php", {cate:cate,item:'corporate'},
			   function(data){
				$('#member-list').html(data);
	});
	
}

function indiSearchByCate(){
	
	$('#member_indi-sort-alp > li').removeClass('active');
	$('#member_indi-alp-all').addClass('active');
	$('#member_indi_name_search').val('')
	
	var cate = $('#member_indi_cate').val();
	var html_loading = "<img style='margin:80px 280px;' src='images/loading45.gif' />";
	$('#member_individual_list').html(html_loading);
	
	
	
	$.post("data_loading/get_member_indi_list.php", {cate:cate,item:'corporate'},
			   function(data){
				$('#member_individual_list').html(data);
	});
	
}

function search_member_alp(alp,elem){
	
	$('#member_cate').val('all');
	$('#member_name_search').val('')
	
	$('#member-sort-alp > li').removeClass('active');
	$(elem).addClass('active');
	
	var html_loading = "<img style='margin:80px 280px;' src='images/loading45.gif' />";
	$('#member-list').html(html_loading);
	
	$.post("data_loading/get_member_list.php", {alp:alp,item:'alphabet'},
			   function(data){
				$('#member-list').html(data);
	});
	
}

function search_member_indi_alp(alp,elem){
	
	$('#member_indi_cate').val('all');
	$('#member_indi_name_search').val('')
	
	$('#member_indi-sort-alp > li').removeClass('active');
	$(elem).addClass('active');
	
	var html_loading = "<img style='margin:80px 280px;' src='images/loading45.gif' />";
	$('#member_individual_list').html(html_loading);
	
	$.post("data_loading/get_member_indi_list.php", {alp:alp,item:'alphabet'},
			   function(data){
				$('#member_individual_list').html(data);
	});
	
}

function member_name_search2(){
	
	$('#member_cate').val('all');
	
	$('#member-sort-alp > li').removeClass('active');
	$('#member-alp-all').addClass('active');
	
	var name = $('#member_name_search').val();
	
	var html_loading = "<img style='margin:80px 280px;' src='images/loading45.gif' />";
	$('#member-list').html(html_loading);
	
	$.post("data_loading/get_member_list.php", {name:name,item:'name_search'},
			   function(data){
				$('#member-list').html(data);
	});
}

function member_indi_name_search2(){
	
	$('#member_indi_cate').val('all');
	
	$('#member_indi-sort-alp > li').removeClass('active');
	$('#member_indi-alp-all').addClass('active');
	
	var name = $('#member_indi_name_search').val();
	
	var html_loading = "<img style='margin:80px 280px;' src='images/loading45.gif' />";
	$('#member_individual_list').html(html_loading);
	
	$.post("data_loading/get_member_indi_list.php", {name:name,item:'name_search'},
			   function(data){
				$('#member_individual_list').html(data);
	});
}

function memberSubmit(type){
   
	var data;
	if(type == 'Corporate'){
		data = $('#corporate_frm').serialize();
	}else{
		data = $('#individual_frm').serialize();
	}
   
	$('#individual').hide();
	$('#corporate').hide();
	$('#member_type_control').hide();
	
   var html_loading = "<div style='width:100px;margin:120px auto;'><img src='images/loading45.gif' /></div>";
   $('#submit_respond').html(html_loading).show();
	
	$.post(WEB_HOME + "member_submit.php", {type:type,data:data},
	   function(data){
		$('#submit_respond').html(data);
	});
}

function change_member_group(memg_id){
    
	
	$('.wysiwyg').ckeditor(function(){
		  this.destroy();
	});
	
	if(memg_id != 0){
		
		if(memg_id == 1){
			$('#member_title').html('Corporate Member');
		}else if(memg_id == 2){
			$('#member_title').html('Individual Member');
		}
		
		var html_loading = "<div style='width:100px;margin:120px auto;'><img src='images/loading45.gif' /></div>";
		   $('#member_submit-frm').html(html_loading);
		
		var file = WEB_HOME + "data_loading/change_member_group.php";
		$.post(file, { memg_id:memg_id,mem_id:0 },
				function(data){
					$('#member_submit-frm').html(data);
					
					runCKEditor();
				}
		);
	}else{
		$('#m_member-group_field').html("");
	}
}

function runCKEditor(){
	ckeditor = $('.wysiwyg').ckeditor(
			
			{
				extraPlugins : 'uicolor',
				toolbar :
				[
					['Source'],
					['Cut','Copy','Paste','PasteText','PasteFromWord'],
					['Undo','Redo','-'],['Link','Unlink','Anchor'],['Image','Flash','Table','HorizontalRule','SpecialChar'],
					'/',
					['Bold','Italic','Underline','Strike','-','Subscript','Superscript'],
					['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'],
					['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
					'/',
					['Styles','Font','FontSize'],
					['TextColor','BGColor'],
					['Maximize', 'ShowBlocks']

				]
			}
	);
}

function ajaxFileUpload()
{
	
	var field_id = $('#company_logo_field_id').val();
	
	//$('#company_logo-dialog').dialog('open');
	$("#show_company_logo").html('<img src="'+WEB_HOME+'images/loader.gif" />');
	
	$.ajaxFileUpload
	(
		{
			url:WEB_HOME + 'system/method/met_member/company_logo_upload.php',
			secureuri:false,
			fileElementId:'file-company_logo',
			dataType: 'html',
			success: function (data, status)
			{
			    $('#show_company_logo').html(data);
				$('#file-company_logo').val('');
			},
			error: function (data, status, e)
			{
				 $("#show_company_logo").html(e);
			}
			,extendvar:{field_id:field_id}
		}
	);
	
	return false;
}



var _multiple_com_rep_element = 0;
function addMoreCompanyRepresentative(){
	   var data;
	   
	   data =   "<div class='company_representative' id='elem-comrep"+_multiple_com_rep_element+"' style='display:none;'>"
			 + "<a class='remove-com_repre ui-state-default ui-corner-all' href='javascript:remove_com_rep_block(\""+_multiple_com_rep_element+"\")'>" 
			   		+"<span class='ui-icon ui-icon-closethick'></span><div>Remove</div>"
			  +"</a>"
	     + "<div class='com_rep_photo'>" +
	     		"<div id='com_rep_photo"+_multiple_com_rep_element+"'>" +
	     				"<img src='"+ WEB_HOME+"images/person.jpg' />" +
	     				"<input type='hidden' name='m_com_rep_photo[]' value='' />" +
	     		"</div>" +
	           "<br/><input type='file' name='file_m_com_rep_"+_multiple_com_rep_element+"' id='file_m_com_rep_"+_multiple_com_rep_element+"' onchange='edit_com_rep_photo(this,\""+ _multiple_com_rep_element +"\")' />"
	        + "</div>"
		+ "<div class='com_rep_detail'>"
				+"<div><b>Name</b> <input type='text' name='m_com_rep_name[]' value='' /></div>"
				+"<div><b>Position</b> <input type='text' name='m_com_rep_position[]' value='' /></div>"
				+"<div><b>Direct Telephone</b> <input type='text' name='m_com_rep_derect_phone[]' value='' /></div>"
				+"<div><b>Mobile Phone</b> <input type='text' name='m_com_rep_mobile_phone[]' value='' /></div>"
				+"<div><b>Personal Email</b> <input type='text' name='m_com_rep_email[]' value='' />"
					
				+"</div>"
				+"<div><b>&nbsp;</b> <input type='checkbox' style='width:20px;' name='m_com_rep_allow_hide[]' value='com_rep_photo-"+_multiple_com_rep_element+"' /><span class='comment'>Show email on website</span></div>"
				+"<div><b>&nbsp;</b> <input type='checkbox' style='width:20px;' value='com_rep_photo-"+_multiple_com_rep_element+"' name='m_com_rep_receive_news[]' checked='checked'  /> <span style='color:green;font-weight:bold;'>Receive news from DTCC</span></div>"
				+"<input type='hidden' name='m_com_rep_this_box_name[]'  value='com_rep_photo-"+_multiple_com_rep_element+"'  />"
		+"</div>"
		+"<div style='clear:both;'></div>"
	+"</div>"
	+"";
	  
	   $('#multiple_company_representative').append(data);
	   $('#elem-comrep'+_multiple_com_rep_element).show('highlight');
	   _multiple_com_rep_element++;
	}



function remove_com_rep_block(elem){
	var item = "#elem-comrep"+elem;
	
	var options = {};

	
	$(item).hide('highlight',function(){$(this).remove()});
	
}

function edit_com_rep_photo(elem,tmp_id){
	
	var fileElement = "file_m_com_rep_"+tmp_id;
	
	$('#multiple_company_representative-dialog').dialog('open');
	$("#multiple_company_representative-dialog").html('<img style="margin:50px 245px;width:128px;" src="'+WEB_HOME+'images/loader.gif" />');
	
	$.ajaxFileUpload
	(
		{
			url:WEB_HOME + 'system/method/met_member/company_representative_upload.php'+'?param='+fileElement,
			secureuri:false,
			fileElementId:fileElement,
			dataType: 'html',
			success: function (data, status)
			{
			    
				$('#multiple_company_representative-dialog').dialog('option', 'buttons', { 
					 "Close": function() { 
						$(this).html(' ').dialog("close"); 
					}
				    ,"Update": function(){
				    	update_com_rep_photo(tmp_id);
				    }
				});
			
				$("#multiple_company_representative-dialog").html(data);
			    $('#tmp-company_representative-photo').Jcrop({
			    	 setSelect:   [ 0, 0, 100, 120 ]
			    	,minSize:[100,120]
		            ,aspectRatio: 5 / 6
		            ,allowSelect:false
		            ,onSelect: showCoords
			      });

			},
			error: function (data, status, e)
			{
				 $("#show_company_logo").html(e);
			}
			,extendvar:{fileElement:fileElement}
		}
	);
	//$(elem).parents().find('img').attr("src",WEB_HOME+"images/events_icon_1.gif");
}

function update_com_rep_photo(elem){
	var file = WEB_HOME + 'system/method/met_member/photo_confirm.php'
	var photo_name = $('#tmp-company_representative-photo-name').val();
	
	var x = $('#x').val();
	var y = $('#y').val();
	var w = $('#w').val();
	var h = $('#h').val();
	
	
	$.post(file, { photo_name:photo_name,type:'company_representative',x:x,y:y,w:w,h:h },
			function(data){
				var img =   "<img src='"+ data.image_path+"' />"
						    +"<input type='button' value='Remove Photo' onclick='remove_photo_company_representative(\""+elem+"\")' />"
							+ "<input type='hidden' name='m_com_rep_photo[]' value='"+ data.image +"' />";
				$('#com_rep_photo'+elem).html(img);
				$('#multiple_company_representative-dialog').html(' ').dialog("close"); 
			}
	,"json");
}



function uploadMemberPhoto(){
	
	$('#member_photo-dialog').dialog('open');
	$("#member_photo-dialog").html('<img src="'+WEB_HOME+'images/loader.gif" />');
	
	$.ajaxFileUpload
	(
		{
			url:WEB_HOME + 'system/method/met_member/member_photo_upload.php',
			secureuri:false,
			fileElementId:'file-member_photo',
			dataType: 'html',
			success: function (data, status)
			{
			    $("#member_photo-dialog").html(data);
			    $('#member_photo-img').Jcrop({
			    	 setSelect:   [ 0, 0, 100, 120 ]
			    	,minSize:[100,120]
		            ,aspectRatio: 5 / 6
		            ,allowSelect:false
		            ,onSelect: selectTheMemberPhotoBoxCrop
			      });
			    
			    $('#member_photo-dialog').dialog('option', 'buttons', { 
					 "Close": function() { 
						$(this).html(' ').dialog("close"); 
					}
				    ,"Update": function(){
				    	update_member_photo();
				    }
				});
			},
			error: function (data, status, e)
			{
				 $("#member_photo-dialog").html(e);
			}
			,extendvar:{}
		}
	);
	
	return false;
	
}

function update_member_photo(){
	
	var photo = $('#file-member_photo_name').val();
	var file = WEB_HOME + 'system/method/met_member/photo_confirm.php'
	
	var x = $('#mem_photo-x').val();
	var y = $('#mem_photo-y').val();
	var w = $('#mem_photo-w').val();
	var h = $('#mem_photo-h').val();
	
	$.post(file, { photo:photo,type:'member_photo',x:x,y:y,w:w,h:h },
			function(data){
				$('#show_member_photo').html(data);
				$('#member_photo-dialog').html(' ').dialog("close"); 
			}
	);
}

function submit_new_member(){
	var name = $('#name').val();
	var file = WEB_HOME + "member_submit.php";
	var mod_id = $('#mod_id').val();
	
	$('#submit_respond').removeClass('ui-state-error').html('').hide();
	
	var require_chk = member_require_chk();
	
	if(require_chk != ''){
		$('#submit_respond').addClass('ui-state-error').show().html(require_chk);
		
		return false;
	}else{
	
		$.post(file, { params:$('#met_frm').serialize() },
			function(data){
			var html = "<div style='color:green; margin:50px 0 100px 0; font-weight:bold; font-size:12px; text-align:center;'><img src='images/correction.jpg' /><br /><br />Thank you! Registration Successfully.</div>";
			$('#tab-display').html(html);
			 
			}
		);
		
	}
}

function member_require_chk(){
	var isNoError = true;
	var errorRespond = "";
	var name = $('#name').val();
	
	if(name == null || name == ""){
		errorRespond += '<div style="margin:2px 0 0 50px;color:#CD0A0A;">' + "- Please insert Member Name.</div>";
	}
	
	$('.field_require').each(function(){
		if($(this).val() == ''){
			errorRespond += '<div style="margin:2px 0 0 50px;color:#CD0A0A;">' + "- Please insert "+$(this).attr('rel')+".</div>";
		}
	});
	
	return errorRespond;
}

function remove_member_photo(){
	
	var data;
	
	data = '<input type="hidden" value="" name="file-member_photo">';
	
	$('#show_member_photo').html(data);
}

function remove_photo_company_representative(ref){
	var data;
	
	data =   '<img src="'+WEB_HOME+'images/person.jpg">'
			+'<input type="hidden" value="" name="m_com_rep_photo[]">';
	
	$('#com_rep_photo'+ref).html(data);
	
}

function remove_company_logo(){
	
	var data;
	data =   '<input type="hidden" value="" name="file-company_logo_name">';
	
	$('#show_company_logo').html(data);
}

function selectTheMemberPhotoBoxCrop(c){
	$('#mem_photo-x').val(c.x);
	$('#mem_photo-y').val(c.y);
	$('#mem_photo-w').val(c.w);
	$('#mem_photo-h').val(c.h);
}

function selectTheLogoBoxCrop(c){
	$('#logo-x').val(c.x);
	$('#logo-y').val(c.y);
	$('#logo-w').val(c.w);
	$('#logo-h').val(c.h);
}

function update_company_logo(){
	var logo_name = $('#file-company_logo_name').val();
	var file = WEB_HOME + 'system/method/met_member/photo_confirm.php'
	
	var x = $('#logo-x').val();
	var y = $('#logo-y').val();
	var w = $('#logo-w').val();
	var h = $('#logo-h').val();
	
	$.post(file, { logo_name:logo_name,type:'company_logo',x:x,y:y,w:w,h:h },
			function(data){
				$('#show_company_logo').html(data);
				$('#company_logo-dialog').html(' ').dialog("close"); 
			}
	);
}

function showCoords(c){
	$('#x').val(c.x);
	$('#y').val(c.y);
	$('#w').val(c.w);
	$('#h').val(c.h);
}

function isCheckboxChecked(){
	
	if($(".item-checkbox:checked").length > 0){
		return true;
	}else{
		return false;
	}
	
}
