var dataXML = null;

$(document).ready(function() {
	
	var isrc = null;
	var istyle = null;
	var list_hospital = null;
	
	
	$(function(){
		//menu
		animatedcollapse.addDiv('menuid_0', 		'fade=1,speed=400,group=menuid,hide=0');
		animatedcollapse.addDiv('menuid_1', 		'fade=1,speed=400,group=menuid,persist=1,hide=1');
		animatedcollapse.addDiv('menuid_00', 		'fade=1,speed=400,group=menuid_0,persist=1');
		animatedcollapse.addDiv('menuid_01', 		'fade=1,speed=400,group=menuid_0');
		animatedcollapse.addDiv('menuid_000', 		'fade=1,speed=400,group=menuid_00,persist=1');
		animatedcollapse.addDiv('menuid_10', 		'fade=1,speed=400,group=menuid_1,persist=1');
		animatedcollapse.addDiv('menuid_11', 		'fade=1,speed=400,group=menuid_1,persist=1');
		animatedcollapse.addDiv('menuid_12', 		'fade=1,speed=400,group=menuid_1,persist=1');
		animatedcollapse.addDiv('menuid_13', 		'fade=1,speed=400,group=menuid_1,persist=1');
		animatedcollapse.addDiv('menuid_14', 		'fade=1,speed=400,group=menuid_1,persist=1');
		animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
		}
		animatedcollapse.init();
		
		//datepicker
		$('#datepicker').datepicker({
			dateFormat: 'yy-mm-dd',
			yearRange: '1910:2010',
			showOn: 'button',
			buttonImage: 'templates/images/b_calendar.png',
			buttonImageOnly: true,
		}).bind('change',	function(){
			var tgl = ($('#datepicker').val()).split("-");
			$('#d option[value=' + tgl[2] + ']').attr('selected', 'selected');
			$('#m option[value=' + tgl[1] + ']').attr('selected', 'selected');
			$('#y option[value=' + tgl[0] + ']').attr('selected', 'selected');
		
		});
		$('#d, #m, #y').bind('change',function(){
			var tgl = $('#y').val()+"-"+$('#m').val()+"-"+$('#d').val();
			$('#datepicker').val(tgl);
		});
		
		//datepicker
		$('#datepicker2').datepicker({
			dateFormat: 'yy-mm-dd',
			yearRange: '1910:2010',
			showOn: 'button',
			buttonImage: 'templates/images/b_calendar.png',
			buttonImageOnly: true,
		}).bind('change',	function(){
			var tgl = ($('#datepicker2').val()).split("-");
			$('#d2 option[value=' + tgl[2] + ']').attr('selected', 'selected');
			$('#m2 option[value=' + tgl[1] + ']').attr('selected', 'selected');
			$('#y2 option[value=' + tgl[0] + ']').attr('selected', 'selected');
		
		});
		$('#d2, #m2, #y2').bind('change',function(){
			var tgl = $('#y2').val()+"-"+$('#m2').val()+"-"+$('#d2').val();
			$('#datepicker2').val(tgl);
		});
		
		//get city by seleted province
		$('#provinsi').bind('change',function(){
			var id_induk = $('#provinsi').val();
			$().ajaxStart(function() {
				$('#load_provinsi').show();
			}).ajaxStop(function() {
				$('#load_provinsi').hide();
			});
			$.ajax({
				type: 'GET',
				url: $(this).attr('class'),
				data: "id_induk="+id_induk,
				async: false,
				dataType: "html", 
				success: function(html, textStatus) {
					$('#kota').html(html);
				}
			})
		});
		
		$('#provinsi_surat').bind('change',function(){
			var id_induk = $('#provinsi_surat').val();
			$().ajaxStart(function() {
				$('#loading_provinsi_surat').show();
			}).ajaxStop(function() {
				$('#loading_provinsi_surat').hide();
			});
			$.ajax({
				type: 'GET',
				url: $(this).attr('class'),
				data: "id_induk="+id_induk,
				async: false,
				dataType: "html", 
				success: function(html, textStatus) {
					$('#kota_surat').html(html);
				}
			})
		});
		
		//get hospital list by text search
		$('#sbutton').bind('click',function(){
			var stext = $('#stext').val();
			$().ajaxStart(function() {
				$('#loading_hospital').show();
			}).ajaxStop(function() {
				$('#loading_hospital').hide();
			});
			$.ajax({
				type: 'GET',
				url: $(this).attr('class'),
				data: "stext="+stext,
				async: false,
				dataType: "html", 
				success: function(html, textStatus) {
					$('#hospital').html(html);
				}
			})
		});
		
		$('#iderror').bind('click',function(){
			$('#iderror').hide();
		});

	});
	
	// ajax form submit
	$('#myForm').submit(function() {
		var msg;
		$().ajaxStart(function() {
			$('#iderror').show();
			$('#loading').show();
			$('#result').hide();
		}).ajaxStop(function() {
			$('#iderror').show();
			$('#loading').hide();
			$('#result').fadeIn('fast');
		});
		$.ajax({
			type: 'POST',
			url: $(this).attr('action'),
			data: $(this).serialize(),
			async: false,
			dataType: "html", 
			success: function(html, textStatus) {
				msg = html;
				$('#result').html(html);
			}
		})
		if (($(this).attr('action').indexOf("noreset") == -1) && (msg.indexOf("GAGAL") == -1)){
			$( "form" )[ 0 ].reset();
		}
		location.href="#atas";
		return false;
	});
	
	// checkbox [un]check all
	 $('#checkall').click(function () { 
		var checked_status = this.checked;
		$("input[name=delid]").each(function(){
			this.checked = checked_status;
		});
  });
  
  $("div.enterleave").bind("mouseenter",function(){
  	isrc = $("img:first",this).attr("src");
  	istyle = $("img:first",this).attr("style");
  	
		$(this).css({ color:"#EE8833" });
		$("img:first",this).fadeIn('fast');
		if ($("img:first",this).attr("src").indexOf("asc") == -1){
			$("img:first",this).attr("src", "templates/images/s_asc.png");
		}else{
			$("img:first",this).attr("src", "templates/images/s_desc.png");	
		}
    
  }).bind("mouseleave",function(){
  	$(this).css({ color:"#FFFFFF" });
    $("img:first",this).attr("src", isrc);
    $("img:first",this).attr("style", istyle);
  });
  
  $("div.enterleave").click(function(){
  	okey = $("img:first",this).attr("class");
  	otype = ($("img:first",this).attr("src").indexOf("asc") == -1) ? "desc" : "asc";
  	stext = $('#stext').val();
  	location.href=okey+"&otype="+otype+"&stext="+stext;

	});
  
  // rumah sakit dan lainnya
	$("input[name='tipe']").change(function(){
    if ($("input[name='tipe']:checked").val() == '1'){
    	$("#rumah_sakit").show();
			$("#lainnya").fadeIn('fast');
    }else{
      $("#rumah_sakit").fadeIn('fast');
			$("#lainnya").show();
		}
	});
	
	$('#fmChangeChart').submit(function () {
		$('#descchart_2').show();
		$('#chartid_2').show();
		$('#areachart_2').hide();
		if ($("input[name='tgl_mulai']").val() > $("input[name='tgl_akhir']").val()){
			alert("Tanggal Mulai tidak boleh lebih besar dari Tanggal Akhir");
			return false;
		}
		$.ajax({
			type: 'POST',
			url: $(this).attr('action'),
			data: $(this).serialize()+"&ajaxReturn=1",
			async: false,
			dataType: "text", 
			success: function(txt, textStatus) {
				
				var ret = txt.split("###");
				var retSKP = ret[0].split("#_#");
				var n = 0;
				
				$('#tableHasil tr').each(function() {
					if (!this.rowIndex) return; 
			    $(this).find("td:last").html(retSKP[n]); 
					n++;
				});

				setFCNewData("chartid2", ret[1]);
				dataXML = ret[1];
			}
		});
		
		return false;
  });
  
  $('#fmPeriodChart').submit(function () {
		$.ajax({
			type: 'POST',
			url: $(this).attr('action'),
			data: $(this).serialize()+"&periodChart=1",
			async: false,
			dataType: "text", 
			success: function(txt, textStatus) {
				setFCNewData("chartid1", txt);
			}
		});
		return false;
  });
  
})


//request delete
function doRequest(param){
	$().ajaxStart(function() {
		$('#loading').show();
		$('#result').hide();
	}).ajaxStop(function() {
		$('#loading').hide();
		$('#result').fadeIn('fast');
	});
	
	if (param.indexOf("&id=") == -1){
		var aid = '0';
		$("input[name=delid]").each(function(){
			if (this.checked){
				aid += ","+this.value;	
			}
		});
	
		if (aid == '0'){
			alert('Data belum dipilih !');
			return;
		}else{
			param += '&aid='+aid;	
		}
	}
	if (!window.confirm('Apakah Anda yakin akan menghapus data tersebut ?')){
		return;	
	}
	
	$.ajax({
		type: 'GET',
		url: 'index.php',
		data: param,
		async: false,
		dataType: "html", 
		success: function(html, textStatus) {
			$('#result').html(html);
		}
	});
	return false;
}

//request delete Member
function delMember(param){
	if (window.confirm('Anda yakin akan menghapus data ?')){
	location=''+param;
	return false;
	}
	return;
}

function upload_fotos(callbackfield,folder){
	var oWindow;
	oWindow = window.open("upload_windows.php?menu=input&callbackfield="+callbackfield+"&folder="+folder,"FILE","top=300,left=280,height=50,width=400,status=no,resizable=no,toolbar=no,menubar=no,scrollbars=no,location=no");
	oWindow.focus();
	return true;
}
function update_callbackfield(callbackfield,backfieldvalue){	
	document.getElementById(callbackfield).value = backfieldvalue;	
	if (callbackfield == "foto"){
		var oimg = document.getElementById("idfoto");
		var old_src = oimg.src.split("/");
  	old_src[old_src.length - 1] = backfieldvalue.replace(".","K.");
  	oimg.src = old_src.join("/");
  }
}
function setFCNewData(objFlash, strXML) {
	//This function updates the data of a FusionCharts present on the page
	//Get a reference to the movie 
	var FCObject = document.getElementById(objFlash);
	//Set the data
	//Set dataURL to null
	FCObject.SetVariable('_root.dataURL',"");
	//Set the flag
	FCObject.SetVariable('_root.isNewData',"1");
	//Set the actual data
	FCObject.SetVariable('_root.newData',strXML);
	//Go to the required frame
	FCObject.TGotoLabel('/', 'JavaScriptHandler'); 
}
function getObject(objectName) {
	if (navigator.appName.indexOf ("Microsoft") !=-1) {
		return window[objectName]
	} else {
	return document[objectName]
	}
}
function getTableRanah(strUrl, areaChart){
	$('#descchart_2').hide();
	$('#chartid_2').hide();
	$('#areachart_2').show();
	var aChart = document.getElementById(areaChart);
	$.ajax({
		type: 'GET',
		url: 'index.php',
		data: strUrl,
		async: false,
		dataType: "html", 
		success: function(html, textStatus) {
			aChart.innerHTML = html;
			
		}
	});
}

function showChart(){
	$('#descchart_2').show();
		$('#chartid_2').show();
		$('#areachart_2').hide();	
	if (dataXML) setFCNewData("chartid2", dataXML);
}



