$(document).ready (function() {
    
    $('#week_id_list').change(function() {
        var cl_id = $("#class_id").val();
        var w_id  = $(this).val();
        var sb_id = $("#new_materi select[name=subject_id]").val();
        
        var body = $.ajax ({
            type:'GET',
            url:'_config/_template/_content/_form/_plugin/_weekguideline.php',
            data:'cl_id='+cl_id+'&sb_id='+sb_id+'&w_id='+w_id,
            dataType:'html',
            success:function(msg) {
                $('#guideline_box').html(msg);
            },
            error:function(request,setting) {
                alert ('Error');
            }
        }).responseText;
    });
   
   /*
    $('.posmurid').blur(function() {
        var user_id = $(this).attr('name').split('_')[1];
        var posisi  = $(this).val();
        var cl_id = $(this).attr('name').split('_')[2];
        var namamurid = $(this).attr('name').split('_')[3];
        
        var error = 0;
        if (posisi == '')  return false; 
        if (posisi < 1 )  return false; 
        
        $.ajax ( {
           type:'GET',
           url:'_config/_template/_content/_form/_plugin/_set.student.position.php',
           data:'cl_id='+cl_id+'&u_id='+user_id+'&position='+posisi,
           dataType:'html', 
           success:function(msg) {
             if (msg == 1 && error == 0) { 
                alert ('Error. This position not available.');
                error = 1;
             } else {
                $("#map_"+posisi).html(namamurid);
             }
           },
           error:function(request,setting) {
             alert('error');
           }
        });   
    });
    */
    
    $('.posmurid').keyup(function(e) {
        //alert(e.keyCode);
        if(e.keyCode == 13) {
            var user_id = $(this).attr('name').split('_')[1];
            var posisi  = parseInt($(this).val());
            var cl_id = $(this).attr('name').split('_')[2];
            var namamurid = $(this).attr('name').split('_')[3];
            var maxposisi = parseInt($(this).attr('name').split('_')[4]);
            
            var error = 0;
            if (posisi == '')  return false; 
            if (posisi < 1 )  return false; 
            if (posisi > maxposisi) { alert ('Position to large. Press Esc to close this dialog.'); return false;}
            
            $.ajax ( {
               type:'GET',
               url:'_config/_template/_content/_form/_plugin/_set.student.position.php',
               data:'cl_id='+cl_id+'&u_id='+user_id+'&position='+posisi,
               dataType:'html', 
               success:function(msg) {
                 if (msg == 1 && error == 0) { 
                    alert ('Error. This position not available.');
                    error = 1;
                 } else {
                    location.reload(); 
                 }
               },
               error:function(request,setting) {
                 alert('error');
               }
            });   
        }
    });
    
    $('.sick,.sick2day,.sick3day,.other').mouseover(function(){
        index = $(this).attr('name').split('_')[1];
        $('#buttonsms_'+index).show();
    });
    
    $('.sick,.sick2day,.sick3day,.other').mouseout(function(){
        $('#buttonsms_'+index).hide();
    });
    
    $('.send_sms').click(function(){
    	var msisdn=$(this).attr('msisdn');
    	var sms=$(this).attr('smscontent');
    	var objectparent = $(this);
        //alert('Mi :' + msisdn + ' sms : ' + sms );
    	$.ajax({
    		type:'GET',
    		url:'http://202.59.200.77/m3classSMS/m3classSMS.php',
    		data:'uid=m3class&pwd=m3classisthebest&msisdn='+msisdn+'&sms='+sms,
    		dataType:'html',
    		success:function(msg){ 
    			alert('Success send Sms');
    			$(objectparent).hide();
    		},
    		error:function(request,setting) {
    			alert('error');
    		}
        });
    });
    
    $('#school_to_get').change(function() {
		var id_sekolah = $(this).val();
		var class_grade=$(this).attr('class_grade');
		
		$.ajax({
			type:'GET',
			url:'_config/_template/_content/_form/_plugin/_plug.transfer.teacher.php',
			data:'id_sekolah='+id_sekolah+'&class_grade='+class_grade,
			datatype:'html',
			success:function(msg) {
				$('#teacher_to_get').html(msg);
			},
			error:function(request,setting) {
				alert('Error');
			}
		});
	});
	
	$('#class_id').change(function(){
		var id_class = $(this).val();
		
		$.ajax({
			type:'GET',
			url:'ajax.page.loader.php',
			data:'s=getsubjectbyclass&id_class='+id_class,
			datatype:'html',
			success:function(msg) {
				$('#select_subjectbox').html(msg);
				getchapter();
			},
			error:function(request,setting) {
				alert('Error');
			}
		});
	});
	
	function getchapter() {
		$('#subject_id').change(function(){
			var id_subject = $(this).val();
			
			$.ajax({
				type:'GET',
				url:'ajax.page.loader.php',
				data:'s=getchapterbysubject&id_subject='+id_subject,
				datatype:'html',
				success:function(msg) {
					$('#select_chapter_box').html(msg);
				},
				error:function(request,setting) {
					alert('Error');
				}
			});
		});
	}
});
