// JavaScript Document

function flogin(){
	
	var p = hex_sha1(document.getElementById("passlogin").value);
	var u = document.getElementById("userlogin").value;

	var xmlhttp;
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	
		xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			var t = xmlhttp.responseText;
			alert(t);
		}
	  }
	xmlhttp.open("GET","login.php?us="+u+"&pw="+p,true);
	xmlhttp.send();

}
function ajaxlogin(){
	var ssl=0;
	if(getEl("id_ssl").checked==true){
		ssl=1;
	}else{
		ssl=0;
	}
	if((getEl("userlogin").value=="")||(getEl("passlogin").value=="")){
		return;
	}
	var action = $("#loginform").attr('action');
	var form_data = {
		username: $("#userlogin").val(),
		password: hex_sha1($("#passlogin").val()),
		is_ajax: {0:1,1:ssl}
	};
	$.ajax({
		type: "POST",
		url: action,
		data: form_data,
		success: function(response)
		{
			
			if(response == 'sukses'){
				//alert(window.location.protocol);
				window.location =  "dekstop.pl";
				
			}else{
				alert("Username atau Password salah");
			}
		}
	});
}

function loginpage(){
	window.location = "index.php";
}

function keluar(){
	/*var action = $("#frmkeluar").attr('action');
	
	$.ajax({
		type: "POST",
		url: action,
		
		success: function(response)
		{
			if(response == 'sukses'){
				window.location ="index.php";
				
			}else{
				alert("error logout");
			}
		}
	});*/
	var ik_data = {
				mode: "globalClass",
				f: "logout",
				ikdata: 1
			};
					
			$.ajax({
				url: "manage.class.php",
				type: "POST",
				dataType: "json",
				data: ik_data,
				success: function(e){
					if((e==1)||(e==404)){
						//alert('logout');
						window.location ="index.pl";
					}else{
						alert('error logout');
					}
				}
			});
}
function element(a,b,c){
	var x = document.createElement(a);
	x.setAttribute('width',b);
	x.setAttribute('height',c);
	return x;
}
function getW(a){
	var b = document.getElementById(a).offsetWidth;
	return b;
}
function getH(a){
	var b = document.getElementById(a).offsetHeight;
	return b;
}
function getEl(a){
	var b = document.getElementById(a);
	return b;
}
function getLf(a){
	var b = document.getElementById(a).offsetLeft;
	return b;
}
function openWin(a,wg,hg,t){
	var w=window.innerWidth|| document.documentElement.clientWidth|| document.body.clientWidth;
	var h=window.innerHeight|| document.documentElement.clientHeight|| document.body.clientHeight;
	var y = document.documentElement.scrollTop;
		
		var wind = $(a).kendoWindow({
              actions: ["Minimize"],
              title: t,
			  height:hg,
			  width:wg,
			  visible: false,
			  draggable: false,
			  resizable: false,
			  modal: true
			}).data("kendoWindow");
			
			
			$(a).closest(".k-window").css({
				 top: y+(h-parseInt(hg))/2,
				 left: (w-parseInt(wg))/2
			 });
			 wind.open();
}
function oWin(a,wg,hg,t,d,m){
	var w=window.innerWidth|| document.documentElement.clientWidth|| document.body.clientWidth;
	var h=window.innerHeight|| document.documentElement.clientHeight|| document.body.clientHeight;
		
		var wind = $(a).kendoWindow({
              actions: ["Minimize","Close"],
              title: t,
			  height:hg,
			  width:wg,
			  visible: false,
			  draggable: d,
			  resizable: false,
			  modal: m
			}).data("kendoWindow");
			
			
			$(a).closest(".k-window").css({
				 top: (h-parseInt(hg))/2,
				 left: (w-parseInt(wg))-20
			 });
			 wind.open();
}
var dateFormat = function () {
	var	token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) {
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date;
		if (isNaN(date)) throw SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") {
			mask = mask.slice(4);
			utc = true;
		}

		var	_ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d:    d,
				dd:   pad(d),
				ddd:  dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m:    m + 1,
				mm:   pad(m + 1),
				mmm:  dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy:   String(y).slice(2),
				yyyy: y,
				h:    H % 12 || 12,
				hh:   pad(H % 12 || 12),
				H:    H,
				HH:   pad(H),
				M:    M,
				MM:   pad(M),
				s:    s,
				ss:   pad(s),
				l:    pad(L, 3),
				L:    pad(L > 99 ? Math.round(L / 10) : L),
				t:    H < 12 ? "a"  : "p",
				tt:   H < 12 ? "am" : "pm",
				T:    H < 12 ? "A"  : "P",
				TT:   H < 12 ? "AM" : "PM",
				Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) {
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
}();

// Some common format strings
dateFormat.masks = {
	"default":      "ddd mmm dd yyyy HH:MM:ss",
	shortDate:      "m/d/yy",
	mediumDate:     "mmm d, yyyy",
	longDate:       "mmmm d, yyyy",
	fullDate:       "dddd, mmmm d, yyyy",
	shortTime:      "h:MM TT",
	mediumTime:     "h:MM:ss TT",
	longTime:       "h:MM:ss TT Z",
	isoDate:        "yyyy-mm-dd",
	isoTime:        "HH:MM:ss",
	isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};
function oneY(a){
	var d = new Date();
	var x = new Date(d.getFullYear()+1);
	return x;
}
function d_date(a,b){
	var t = a.split('/');
	d = t[0];
	m = t[1];
	y = t[2];
	d= parseInt(d,10)+b;
	return d+"/"+m+"/"+y;
}
function m_date(a){
	var t = a.split("/");
	d = t[0];
	m = t[1];
	y = t[2];
	return m+"/"+d+"/"+y;
}
function sq_date(a){
	var t = a.split("-");
	y = t[0];
	m = t[1];
	d = t[2];
	return y+","+m+","+d;
}
function sql_date(a,b){
	var t = a.split("-");
	y = t[0];
	m = t[1];
	d = t[2];
	switch(b)
	{
		case "d/":
			return d+"/"+m+"/"+y;
			break;
		case "m/":
			return m+"/"+d+"/"+y;
			break;
		case "d-":
			return d+"-"+m+"-"+y;
			break;
	}
}
function sq_date_d(a){
	var t = a.split("-");
	y = t[0];
	m = t[1];
	d = t[2];
	return d+"/"+m+"/"+y;
}
function d_bulan(a){
	var t = a.split("/");
	d = t[0];
	m = t[1];
	y = t[2];
	return d+"  "+ghari(m)+"  "+y;
	//return d+"/"+m+"/"+y;
}
function ghari(a){
	var bulan = ["Januari","Pebruari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","Nopember","Desember"];
	return bulan[a-1];
}

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};
function colHex(color) {
    if (color.substr(0, 1) === '#') {
        return color;
    }
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
    
    var red = parseInt(digits[2]);
    var green = parseInt(digits[3]);
    var blue = parseInt(digits[4]);
    
    var rgb = blue | (green << 8) | (red << 16);
    return digits[1] + '#' + rgb.toString(16);
};
function closeW(a){
	$(a).data("kendoWindow").close();
}
function remove(id)
{
    return (elem=document.getElementById(id)).parentNode.removeChild(elem);
}
function create(a,b,c)
{
	var g = document.createElement(a);
	g.id = b;
	var p = document.getElementById(c);
	p.appendChild(g);
   
}
function DateDiff(date1, date2) {
    var datediff = date1.getTime() - date2.getTime();
    return (datediff / (24*60*60*1000));     
}
function getInd(ip,s,c){
	var l = s.length;
	var i = 0;
	var ind = 0;
	for(i;i<l;i++)
	{
		if(s[i][c]==ip){
			ind = i+1;
		}
	}
	return ind;
}
function getVal(ip,s,c,c2){
	var l = s.length;
	var i = 0;
	var Val = null;
	for(i;i<l;i++)
	{
		if(s[i][c]==ip){
			Val = s[i][c2];
		}
	}
	return Val;
}
function getInd_sp(ip,s){
	var str = ip.split("&");
	var l = s.length;
	var i = 0;
	var Val = 0;
	for(i;i<l;i++)
	{
		if((s[i]["tb"]==str[0])&&(s[i]["id"]==str[1])){
			Val = i+1;
		}
	}
	return Val;
}
function gw(){
	var w=window.innerWidth|| document.documentElement.clientWidth|| document.body.clientWidth;
	return w;
}
function gh(){
	var h=window.innerHeight|| document.documentElement.clientHeight|| document.body.clientHeight;
	return h;
}
function setW(a,b){
	getEl(a).style.width = b;
}
function lebar(a,b){
	getEl(a).style.width = b;
}
function display(a,b){
	getEl(a).style.display = b;
}
function kiri(a,b){
	getEl(a).style.left = b;
}
function huruf(a){
	var hur = ["Nol","Satu","Dua","Tiga","Empat","Lima","Enam","Tujuh","Delapan","Sembilan"];
	return hur[a];
}
var ik = (function(e){
	return {
		kiri: function(a){
			var w = getW(a);
			return ((w-(getW("grid_th_status")))/2)+"px";
		}
	}
	
})();
var loader = (function(e){
	return{
		show: function(){
			var y=document.documentElement.scrollTop;
			getEl("ik_loader").style.top = y+2+"px";
			getEl("ik_loader").style.left = (gw()/2)-100+"px";
			getEl("ik_modal").style.display="block";
			return true;
		},
		hide: function(){
			getEl("ik_modal").style.display="none";
			//getEl("ik_modal").style.display="none";
			return true;
		}
	}
	
})();
function gagal(){
	loader.hide();
	alert('gagal');
}
function redir(a){
	return document.location = "error/"+a+".php";
}
function getCl(a){
	return document.getElementsByClassName(a);
}
function hklik(){
	document.location = "index.pl";
}
function enter(e){
	if (e.keyCode == 13) {
		ajaxlogin();
	}
}
function on_demo(){
	var ssl=0;
	if(getEl("id_ssl").checked==true){
		ssl=1;
	}else{
		ssl=0;
	}
	
	var action = $("#loginform").attr('action');
	var form_data = {
		username: 'demo@demo.com',
		password: hex_sha1('admin'),
		is_ajax: {0:1,1:ssl}
	};
	$.ajax({
		type: "POST",
		url: action,
		data: form_data,
		success: function(response)
		{
			
			if(response == 'sukses'){
				//alert(window.location.protocol);
				window.location =  "dekstop.pl";
				
			}else{
				alert("Username atau Password salah");
			}
		}
	});
}
