function tab_click(id,ordid){
		$('.button').removeClass('button_actv')
		$('#'+id).addClass('button_actv');
		get_buku(id,ordid);
	}

function get_tab_header(action){
	
	if(action!=0){
			if($('#jenjang').val()==""){return false;}
			if($('#kelas').val()==""){return false;}
	}
	
	$.post('tabheader.php',{
				kelas:$('#kelas').val(),
				autoid:$('#autoid').val(),
				act:action,
				jenjang:$('#jenjang').val(),
				kurikulum:$('#kurikulum').val()
				},function(data){
			$('#tabheader').html(data);
		})
	}
	
		
	function export_xls(div){
		window.open('data:application/vnd.ms-excel,' + encodeURIComponent($(div).html()))
		}
		
function view_del(act,id){
	
	$id = $('#row'+id).val();
	$('#autoid').val($id)
	
			if(act==1){dosubmit('../sp/view');}
			if(act==3){dosubmit('../sp/cancel');}
			if(act==4){dosubmit('../fb/view');}
	}

		function getrecord(hal){
			url = $('#url').val();
			query = {page:hal,q:$('#q').val(),f1:$('#f1').val(),f2:$('#f2').val(),targetpage:url};
				$.post(url,query,function(data){
				$('#rec_display').html(data)	
				})
			}

function print_sp_pdf(id){
	if(id==1){
		dosubmit2('../sp/printpdf');}
	if(id==2){
		dosubmit2('../sp/spesifikasi_buku');
		}
	}

function get_buku(id,ordid){
	$.post('getproduct.php',{autoid:id,order_id:ordid},function(data){
			$('#output').html(data);
		})
	}
	

function get_jenjang(nkode){
	
	if($('#kurikulum').val()==""){return false;}
	
	$.post('../sp/getjenjang.php',{kuri:$('#kurikulum').val(),jenjang:$('#jenjang').val(),kode:nkode},function(data){
			if(nkode==1){
				$('#span_jenjang').html(data);
			}
			if(nkode==2){
				$('#span_kelas').html(data);
			}
		})
	}	
function calc_qty(){
	row = $('#row').val();
	
	totalqty = 0;
	totalhrg = 0;
	for(i=0;i<row;i++){
			
			total = new Number($('#qty'+i).val().replace(/[^\d\.\-]/g, "")) * new Number($('#price'+i).val().replace(/[^\d\.\-]/g, ""));
			$('#total'+i).val(total);
			$('#span_jumlah'+i).html(formatDecimal(total));
			
			totalqty = totalqty + new Number($('#qty'+i).val());
			totalhrg = totalhrg + new Number($('#total'+i).val());

		}
		
		$('#totalqty').html(formatDecimal(totalqty));
		$('#totalhrg').html(formatDecimal(totalhrg));
		chk_qty();
	}
	
function auto_qty(){
		row 	= $('#row').val();
		qty_all = $('#qty_all').val().replace(/[^\d\.\-]/g, "");
		
			for(i=0;i<row;i++){	
			if($('#status'+i).val()==0){
				$('#qty'+i).val(qty_all);
			}
			}
			calc_qty();
	}
	
function chk_qty(){
	row = $('#row').val();
		for(i=0;i<row;i++){
			if( new Number($('#qty'+i).val()) >= 1){ $('#tr'+i).addClass('qty_isi')}
		}

	
	}	
	


function del_tab(id,desc){
	
	bootbox.confirm('Hapus '+ desc +' ?',function(result){
		
			if(result==true){
				$.post('delheader.php',{autoid:id},function(data){
				get_tab_header(0);
				})
			}
		
		})
	
}

function del_sp(){
	desc = "Batalkan Surat Pesanan ?";
	bootbox.confirm(desc,function(result){
		
			if(result==true){
				$.post('deletesp.php',{autoid:$('#autoid').val()},function(data){
						if(data==1){
							dosubmit('../home/beranda');
							}
				})
			}
		
		})
	
}


function dosubmit(url){
		$('#iform').attr('method','post');
		$('#iform').attr('action',url);
		$('#iform').attr('target','_self');
		$('#iform').submit();
	}
	
	
function dosubmit2(url){
		$('#iform').attr('method','post');
		$('#iform').attr('action',url);
		$('#iform').attr('target','_blank');
		$('#iform').submit();
	}
	
	function login(){
	if($('#npsn').val()==""){
		alert('Harap Masukan No NPSN Anda')
		$('#npsn').focus();
		return false;
		}
		
	if($('#passwd').val()==""){
		alert('Harap Masukan Password')
		$('#passwd').focus();
		return false;
		}

		dosubmit('processlogin');
	}

	function pop_pass(){
		$('#pass_div').bPopup();
			$('#prevpass').val('');
			$('#newpass').val('');
			$('#cnewpass').val(''); 
			$('#prevpass').focus();
		}
		
	function close_pass(){
		
			$('#pass_div').bPopup().close();
		}

    function save_pass(){
		
		$prevpass = $('#prevpass').val();
		$newpass = $('#newpass').val();
		$cnewpass = $('#cnewpass').val(); 
		
		if($newpass!=$cnewpass){
				$('#info_pass').html('Password Baru Tidak Sesuai');
				$('#newpass').focus();
			}else{
				$('#info_pass').html('');	
					$.post('savepasswd',{prevpass:$prevpass,newpass:$newpass},function(data){
								if(data==0){
									$('#info_pass').html('Password Berhasil Diubah');
									}else{
										$('#info_pass').html('Password Lama Anda Salah');
										$('#prevpass').focus();										
										}
							

						})		
				}

		}

	//EMAIL UPDATE
	function pop_email(){
			$email_status = $('#email_status').val();
			if($email_status==0){$('#info_email').html('');}
			
			$('#email_div').bPopup({
					onClose: function() { 
					close_popemail();
					}
			});
			$('#email').focus();
		}


	function close_popemail(){
				$email_status = $('#email_status').val();
					if($email_status==0){
							$('#email_div').bPopup().close();
						}else{
							$('#email_div').bPopup().close();
						//pop_email();
							}
		}

	 function validateEmail($email) {
 		 var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		  return emailReg.test( $email );
	}
	
	
	function save_mail(){
		email_addr = $('#email').val();

		$namakepsek 	= $('#namakepsek').val();
		$nipkepsek 		= $('#nipkepsek').val();
		$namabendahara 	= $('#namabendahara').val(); 
		$nipbendahara 	= $('#nipbendahara').val(); 
		$notelp 		= $('#notelp').val();
		
		query = {namakepsek:$namakepsek,nipkepsek:$nipkepsek,namabendahara:$namabendahara,nipbendahara:$nipbendahara,mail:email_addr,notelp:$notelp};


		if(email_addr==""){
			$('#info_email').html('Email Anda Tidak Valid');
			$('#email').focus();
			$('#email_status').val(0);
			return false;			
			}
		if($namakepsek==""){$('#namakepsek').focus();return false;}
		if($nipkepsek==""){$('#nipkepsek').focus();return false;}
	
		if( !validateEmail(email_addr)) { 
			$('#info_email').html('Email Anda Tidak Valid');		
				$('#email').focus();
				$('#email_status').val(1);
				return false;
		 }
				$.post('../common/saveprofile',query,function(data){
				if(data==0){
					$('#info_email').html("<b>Profil Anda Berhasil Di Update</b>");
					$('#email_status').val(0);
					}
				})
			
		
		}




function arrow_key(id,enter,esc){

			row = $('#row').val();	
			
			lastrow = new Number(row) - 1;

			if(id=="#qty0"){esc = "#button_select";}			
			if(id=="#qty"+lastrow){enter = "#qty0";}
	
			$(id).bind('keydown',function(e){


						switch(e.which){
						case 13:
							$(enter).trigger('focus');	//enter
							$(id).unbind();
							$(id).destroy();
						break;		
						
						case 40:
						$(enter).trigger('focus');	//bawah
							$(id).unbind();
							$(id).destroy();
						break;
	
						case 27:
							$(esc).trigger('focus');	//esc
							$(id).unbind();
							$(id).destroy();
						break;		
						
						case 38:
							$(esc).trigger('focus'); //atas
							$(id).unbind();
							$(id).destroy();
						break; 
						
						}
						
						

				})
}

function formatDecimal(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
   
   if(cents<10)
      cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
    
    return (((sign)?'':'-') + num );
   //return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function formatDecimalb(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
   
   if(cents<10)
      cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
    
    return (((sign)?'':'-') + num + '.' + cents);
   //return (((sign)?'':'-') + '$' + num + '.' + cents);
}