window.onload = initForm;

function initForm() {
     document.getElementById("rlsKategori").selectedIndex = 0;
     document.getElementById("rlsKategori").onchange = populatesubkategori;
	 //document.getElementById("tahun_awal").selectedIndex = 1;
	 document.getElementById("tahun_awal").onchange= resetTanggal1;
	 document.getElementById("tahun_akhir").onchange= resetTanggal2;
	 document.getElementById("bulan_awal").selectedIndex = 0;
     document.getElementById("bulan_awal").onchange = hariawal;
	 document.getElementById("bulan_akhir").selectedIndex = 0;
     document.getElementById("bulan_akhir").onchange = hariakhir;
	 //document.getElementById("klik").onclik= klik;
}

function populatesubkategori() {
     var subktgr = new Array('All','Agro','Kesehatan','Sains dan Teknik','Sosial-Humaniora');
     var kategoriStr = this.options[this.selectedIndex].value;

     if (kategoriStr == "Liputan/Berita") {
        document.getElementById("rlsSubkategori").options.length = 0;
        for(var i=0; i<5; i++) {
           document.getElementById("rlsSubkategori").options[i] = new Option(subktgr[i]);
		   
        }
     }
	 else {
		document.getElementById("rlsSubkategori").options.length = 0;
		document.getElementById("rlsSubkategori").options[0]= new Option("Tidak Ada");
        
	}
	
}
function resetTanggal1(){
	document.getElementById("bulan_awal").selectedIndex = 0;
	document.getElementById("hari_awal").length=0;
	document.getElementById("hari_awal").options[0]=new Option('Hari');
}
function resetTanggal2(){
	document.getElementById("bulan_akhir").selectedIndex = 0;
	document.getElementById("hari_akhir").length = 0;
	document.getElementById("hari_akhir").options[0]=new Option('Hari');
}
function hariawal() {
	 var a = document.cariRilis.tahun_awal.selectedIndex;
	 var year = document.cariRilis.tahun_awal.options[a].value;
	 if (year % 4 == 0){
		var monthDays = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31);
	}else{
		var monthDays = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31);
	}
     var monthStr = this.options[this.selectedIndex].value;

     if (monthStr != "") {
        var theMonth = parseInt(monthStr);

        document.getElementById("hari_awal").options.length = 0;
        for(var i=0; i<monthDays[theMonth]; i++) {
           document.getElementById("hari_awal").options[i] = new Option(i+1);
        }
     }
}

function hariakhir() {
     var a = document.cariRilis.tahun_akhir.selectedIndex;
	 var year = document.cariRilis.tahun_akhir.options[a].value;
	 if (year % 4 == 0){
		var monthDays = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31);
	}else{
		var monthDays = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31);
	}
     var monthStr = this.options[this.selectedIndex].value;

     if (monthStr != "") {
        var theMonth = parseInt(monthStr);

        document.getElementById("hari_akhir").options.length = 0;
        for(var i=0; i<monthDays[theMonth]; i++) {
			document.getElementById("hari_akhir").options[i] = new Option(i+1);
			
        }
     }
}
function ValidasiTanggal(){	
		var a = document.cariRilis.tahun_awal.options[document.cariRilis.tahun_awal.selectedIndex].value;
		var b = document.cariRilis.tahun_akhir.options[document.cariRilis.tahun_akhir.selectedIndex].value;
		var c = document.cariRilis.bulan_awal.options[document.cariRilis.bulan_awal.selectedIndex].value;
		var d = document.cariRilis.bulan_akhir.options[document.cariRilis.bulan_akhir.selectedIndex].value;
		var e = document.cariRilis.hari_awal.options[document.cariRilis.hari_awal.selectedIndex].value;
		var f = document.cariRilis.hari_akhir.options[document.cariRilis.hari_akhir.selectedIndex].value;
		if ((a*10000+c*100+e) > (b*10000+d*100+f)){
			alert("Tanggal akhir harus lebih besar dari tanggal awal.");
			return false;
		}else{ 
			formSubmit();
			return true;
		}
}
