var lokal="";
var lokal_gambar="";
var lokal_userfile="";
var lokal_home="";
var timererr=null;
var kudus=(function(){
	var htm="";
	return{
		set:function(a){
			lokal=a;
		},
		setgb:function(a){
			lokal_gambar=a;
		},
		setUsefile:function(a){
			lokal_userfile=a;
		},
		sethome:function(a){
			lokal_home=a;
		},
		tinggifit:function(a){
			var tg=$(window).height();
			return tg;
		},
		tanggal:function(a,b,c){
			if(a==null || a.length<=0){
				return "";
			}
			b=b||"/";
			c=c|| 0;
			var hasil=" - ";
			if(c==0){
				var arr=a.split(" ");
				var waktu="";
				if(arr.length==2){
					waktu=" "+arr[1];
				}
				
				var str=arr[0].split("-") || "";
				if(str.length==3){
					hasil=str[2]+b+str[1]+b+str[0]+waktu;
				}
			}else if(c==1){
				var str=a.split("/");
				if(str.length==3){
					hasil=str[2]+b+str[1]+b+str[0];
				}
			}else if(c==2){
				var str=a.split(" ");
				var tgl="";
				var waktu="";
				if(str.length==2){
					var strtg=str[0].split("-");
					if(strtg.length==3){
						tgl=strtg[2]+b+strtg[1]+b+strtg[0];
					}
					
					waktu=str[1];
					hasil=tgl+" "+waktu;
				}
			}else if(c==3){
				var str=a.split(" ");
				var tgl="";
				var waktu="";
				if(str.length==2){
					var strtg=str[0].split("-");
					if(strtg.length==3){
						tgl=strtg[2]+b+strtg[1]+b+strtg[0];
					}
					
					waktu=str[1];
					hasil=tgl+"<br/>"+waktu;
				}
			}
			return hasil;
		},
		umur:function(awal,akhir){
			var today;
			if(akhir=="now"){
				today = new Date();
			}else{
				today = new Date(akhir);
			}
			
		    var birthDate = new Date(awal);
		    var age = today.getFullYear() - birthDate.getFullYear();
		    var m = today.getMonth() - birthDate.getMonth();
		    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
		        age--;
		    }
		    return age;
		},
		tanggalsql:function(a){
			var hasil=a;
			
			var arr=a.split("/");
			if(arr.length==3){
				hasil=arr[2]+"-"+arr[1]+"-"+arr[0];
			}
			return hasil;
		},
		buatdigit:function(a,b,c){
			var ch = c || "0";
			if(b<a.length) {
				return a;
			}
			
			var sl=b-a.length;
			var dp="";
			for(var i=0;i<sl;i++){
				dp+=ch;
			}
			return dp+a;
		},
		gethari:function(a){
			var hasil="-";
			var hari=["Minggu","Senin","Selasa","Rabu","Kamis","Jumat","Sabtu"];
			if(parseInt(a)<hari.length){
				hasil=hari[parseInt(a)];
			}
			return hasil;
		},
		datedif:function(d1,d2){
			var start = Math.floor( d1.getTime() / (3600*24*1000)); //days as integer from..
			var end   = Math.floor( d2.getTime() / (3600*24*1000)); //days as integer from..
			var daysDiff = end - start;
			return daysDiff;
		},
		selecttext:function(elementId) {
		    var elt = document.getElementById(elementId);
		
		    if (elt.selectedIndex == -1)
		        return null;
		
		    return elt.options[elt.selectedIndex].text;
		},
		cekform:function(a){
			var status=true;
			for(var i=0;i<a.length;i++){
				if($("#"+a[i]).val()==""){
					$("#err_"+a[i]).html("Wajib diisi !");
					status=false;
				}
			}
			
			if(timererr!=null){
				clearTimeout(timererr);
			}
			timererr=setTimeout(function(){
				for(var i=0;i<a.length;i++){
					$("#err_"+a[i]).html("");
				}
				
			},5000);
			return status;
		},
		getattrselect:function(a,b){
			var el=document.getElementById(a);
			return el.options[el.selectedIndex].getAttribute(b);
		},
		getindexarr:function(a,b){
            var idx=0;
            for(var i=0;i<b.length;i++){
                if(b==a){
                    idx=i;
                }
            }
            return idx;
        },
        getindexarrkey:function(a,b,c){
            var idx=0;
            for(var i=0;i<b.length;i++){
                if(b[c]==a){
                    idx=i;
                }
            }
            return idx;
        },
        buatduit:function(a,b){
        	var des=b || 0;
			var jm=a;
			var hasil=a;
			if(jm.length>0){
				var arrjm=jm.split(",");
				var belakang="";
				if(arrjm.length==2){
					belakang=","+arrjm[1];
				}
				var jmstr=arrjm[0].replace(/\./g,"");
				hasil=accounting.formatNumber(jmstr, des, ".", ",")+belakang;
				
			}
			
			return hasil;
		},
		buatdesimal:function(a){
			return a.replace(/\./g,"").replace(/\,/g,".");
		},
		buatduitel:function(a){
			var jm=a.value;
			var hasil="";
			if(jm.length>0){
				var arrjm=jm.split(",");
				var belakang="";
				if(arrjm.length==2){
					belakang=","+arrjm[1];
				}
				var jmstr=arrjm[0].replace(/\./g,"");
				hasil=accounting.formatNumber(jmstr, 0, ".", ",")+belakang;
			}
			a.value=hasil;
		},
		genrp:function(a){
			var jm=$("#"+a).val();
			if(jm.length>0){
				var jmstr=jm.replace(/\./g,"");
				$("#"+a).val(accounting.formatNumber(jmstr, 0, ".", ","));
			}
		},
		randomColorFactor:function() {
            return Math.round(Math.random() * 255);
       	},
       	randomColor:function() {
            return 'rgb(' + kudus.randomColorFactor() + ',' + kudus.randomColorFactor() + ',' + kudus.randomColorFactor() + ')';
       	}
	};
})();
