$(function(){

	startSearch();
	$("#keyword").live('focus',function(e){
		var keyword = $("#keyword").val();
		var defText = $("#keyword").attr('title');
		if (keyword==defText)
			$("#keyword").val('');
	});
	$("#keyword").live('blur',function(e){
		var keyword = $("#keyword").val();
		var defText = $("#keyword").attr('title');
		if (keyword=='')
			$("#keyword").val(defText);
	});
	$(".txtpinFake").live('focus',function(e){
		$(this).parent('form').children('input:first').show().focus();
		$(this).hide();
		$(this).parent('form').children('input:first').blur(function(e){
			if ($(this).val()==''){
				$(this).hide();
				$(this).parent('form').children('.txtpinFake').show();
			}
		});
	});
	
	$("#keyword").live('keypress',function(e){
		var keyword = $("#keyword").val();
		var defText = $("#keyword").attr('title');
		if (e.which == 13){
			if (keyword=='')
				$("#keyword").val(defText);
			else
				startSearch();
		}

	});
	$("#btnSearch").live('click',function(e){
		startSearch();
	});
	
	$(".txtpin").live('keypress',function(e){
		var nis = $(this).attr('nis');
		if (e.which == 13){
			$("#err"+nis).html('Please wait...');
		}
	});
	$(".btnLogin").live('click',function(e){
		var nis = $(this).parent('td').children('#pin').attr('nis');
		$("#err"+nis).html('Please wait...');
	});
	
	$("#loadMore").live('click',function(){
		var page = $(this).attr('page');
		var keyword = $("#keyword").val();
		var defText = $("#keyword").attr('title');
		var dat		= "";
		if (keyword!=defText && keyword!=''){
			dat		= keyword;
		} else {
			dat		= "";
			$("#keyword").val(defText);
		}
		$.ajax({
			url : 'showResult.php',
			data: 'page='+page+'&keyword='+dat,
			beforeSend : function(){
				$("#loadMore").html("<img src='images/loading.gif' border=0>");
			},
			success : function(msg){
				$("#loadMore").parent('div').remove();
				$("#result").append(msg);
			}
		});
	})
});
startSearch = function(){
	var keyword = $("#keyword").val();
	var defText = $("#keyword").attr('title');
	var dat		= "";
	if (keyword!=defText && keyword!=''){
		dat		= keyword;
	} else {
		dat		= "";
		$("#keyword").val(defText);
	}
	$.ajax({
		url : 'showResult.php',
		data: 'keyword='+dat,
		beforeSend : function(){
			$("#result").attr('align','center');
			$("#result").html("<img src='images/loading.gif' border=0>");
		},
		success : function(msg){
			$("#result").removeAttr('align');
			$("#result").html(msg);
		}
	});
}
login = function(nis,x,y,z){
	$("#err"+nis).removeClass('errMsgWrapper');
	$("#frm"+nis+" #eNis").remove();
	$("#frm"+nis+" #eCrl").remove();
	$("#frm"+nis+" #ePin").remove();
	$("#frm"+nis+" #eAct").remove();
	var pin = $("#pin"+nis).val();
	if (pin!=""){
		if (md5(pin)==x || md5(pin)==y || md5(pin)==z){
			var eNis,ecrl,ePin,eAct,chl;
			chl  = $("#frm"+nis).children('#btnLogin');
			eNis = chl.attr('i');
			eCrl = chl.attr('j');
			eAct = chl.attr('k');
			ePin = pin;
			var txt =	"<input type='hidden' id='eNis' name='nis' value='"+eNis+"'>"+
						"<input type='hidden' id='eCrl' name='crl' value='"+eCrl+"'>"+
						"<input type='hidden' id='ePin' name='pin' value='"+ePin+"'>"+
						"<input type='hidden' id='eAct' name='action' value='"+eAct+"'>";
			$("#frm"+nis).append(txt);
			$("#err"+nis).html('');
			$("#pin"+nis).val('');
			return true;
		} else {
			$("#err"+nis).addClass('errMsgWrapper');
			$("#err"+nis).html('PIN Salah!');
			return false;
		}
	} else {
		$("#err"+nis).addClass('errMsgWrapper');
		$("#err"+nis).html('PIN Salah!');
		return false;
	}
	
}

