function save_brokeragent() {
	var mainpage 	= $("#main_page").val();
	var maintype 	= $("#main_type").val();
	var mainaction 	= $("#main_action").val();
	var mainid 		= $("#main_id").val();

	var current_broker 		= $("#current_broker").val();
 

	 
	var brokeragent = $("#brokeragent_form");
	var brokeragentserial = $("#brokeragent_form").serialize();	
 
	var dataString	= brokeragentserial + "&action=" + encodeURIComponent(mainaction) + "&mainid=" + encodeURIComponent(mainid) + "&realty=" + encodeURIComponent(current_broker);

	$.post("./ajax/brokeragent.php", dataString, function(result) {
		  	var obj	= $.parseJSON(result);
			if(obj.output==true){
				bootbox.alert("Successfully Saved ...");
				window.setTimeout(function() { window.location.href = 'index.php?pg='+mainpage+'&type='+maintype; }, 1000);
			}else{
				bootbox.alert("Failed in saving."); 
			} 
	});
 
}

function delete_brokeragent(e) {
	var a = "brokeragent",
		o = $("#main_page").val(),
		t = $("#main_type").val(),
		n = "mainid=" + encodeURIComponent(e) + "&transaction=" + encodeURIComponent(a);
	$.post("./ajax/delete_transaction.php", n, function(e) {
		var a = $.parseJSON(e);
		1 == a.output ? (bootbox.alert("Successfully Deleted ..."), window.setTimeout(function() {
			window.location.href = "index.php?pg=" + o + "&type=" + t
		}, 1e3)) : (bootbox.alert("Failed in Deleting ..."), window.setTimeout(function() {
			window.location.href = "index.php?pg=" + o + "&type=" + t
		}, 1e3))
	})
}

function load_brokeragent() {
 
	var mainpage 	= $("#main_page").val();
	var maintype 	= $("#main_type").val();
	var mainaction 	= $("#main_action").val();
	var mainid 		= $("#main_id").val();
	 
	var dataString	= "action=" + encodeURIComponent(mainaction) + "&mainid=" + encodeURIComponent(mainid);
 
	$.post("./ajax/load_brokeragent.php", dataString, function(result) {
		  	var obj							= $.parseJSON(result);
			var code 						= obj.data[0].code; 
			var fullname 					= obj.data[0].fullname; 
			//var firstname 					= obj.data[0].firstname; 
			//var middlename 					= obj.data[0].middlename; 
			var realty 						= obj.data[0].realty; 
			var date_application_received 	= obj.data[0].date_application_received; 
			var date_applied_at_metrobank 	= obj.data[0].date_applied_at_metrobank; 
			var broker_or_agent 			= obj.data[0].broker_or_agent; 
			var cashcardnumber 				= obj.data[0].cashcardnumber;  
			var accountnumber 				= obj.data[0].accountnumber;  
			var remarks 					= obj.data[0].remarks;  
 
 
			$('#code').val(code);
			$('#fullname').val(fullname);
			//$('#firstname').val(firstname);
			//$('#middlename').val(middlename); 
			$('#brokername').val(realty); 
			reloadBroker(realty);
			$('#dateapplicationreceived').val(date_application_received);
			$('#dateappliedatmetrobank').val(date_applied_at_metrobank);
			$('#brokeragent').val(broker_or_agent);
			$('#cashcardnumber').val(cashcardnumber);
			$('#accountnumber').val(accountnumber);
			$('#remarks').val(remarks);


			var action 			=	$('#main_action').val(); 

			if(action=="view"){
				$('input').attr("disabled", true);
				$('textarea').attr("disabled", true);
				$('select').attr("disabled", true);
				$('#current_project').select2('disable');
				$('#current_model').select2('disable'); 
				$('.glyphicon-trash').hide();
				$('.glyphicon-plus').hide();  
				$('.refresh').hide();  
			}

	});
 
}
 

	var mods 			=	$('#main_page').val();
	var types 			=	$('#main_type').val(); 
	var action 			=	$('#main_action').val(); 
	if(mods=="settings" && types=="brokeragent" && (action=="edit" || action=="view")){
			
 			load_brokeragent();
 
 	}


reloadBroker("");

function reloadBroker(defaultvalue){  
	loadSelectRealty('broker','#div_brokeragent','.sel2broker',defaultvalue);
 }