var hubungi=(function(){
	var timererr=null;
	var waktu=5000;
	var dataerr=[];
	return{
		kirim:function(){
			hubungi.reseterr();
			var nm=$("#nama").val();
			var al=$("#alamat").val();
			var tp=$("#telp").val();
			var em=$("#email").val();
			var ps=$("#pesan").val();
			var cpc=grecaptcha.getResponse();
			
			dataerr=[];
			if(nm=="") dataerr.push({"el":"nama","err":"Nama belum diisi!"});
			if(al=="") dataerr.push({"el":"alamat","err":"Alamat belum diisi!"});
			if(tp=="") dataerr.push({"el":"telp","err":"No Telp belum diisi!"});
			if(em=="") dataerr.push({"el":"email","err":"Email belum diisi!"});
			if(ps=="") dataerr.push({"el":"pesan","err":"Pesan belum diisi!"});
			
			if(dataerr.length>0) {
				hubungi.error();
				return;
			}
			
			
			mload.sh("boxloader_pesan");
			var ik_data = {
				ajax: 1,
				mode: "mod",
				cl: "hubungi",
				ikdata: {0:"kirimpesan",nama:nm,alamat:al,telp:tp,email:em,pesan:ps,cpc:cpc}
			};
			
			$.ajax({
				url: lokal+"man.ajax.php",
				type: "POST",
				dataType: "json",
				async:true,
				data: ik_data,
				success: function(e){
					mload.hd();
					if(e==1){
						document.location=document.location.href;
					}else if(e==2){
						alert("Error robot");
						document.location=document.location.href;
					}else if(e==3){
						pesan.load("Pesan gagal.","boxloader_pesan");
					}else if(e==4){
						pesan.load("Email sudah di gunakan.","boxloader_pesan");
					}else if(e==0){
						pesan.load("Pesan gagal.","boxloader_pesan");
					}
				}
			});
		},
		reseterr:function(){
			$("#err_nama").html("");
			$("#err_alamat").html("");
			$("#err_telp").html("");
			$("#err_email").html("");
			$("#err_pesan").html("");
		},
		error:function(){
			if(timererr!=null){
				clearTimeout(timererr);
			}
			for(var i=0;i<dataerr.length;i++){
				$("#err_"+dataerr[i]['el']).html(dataerr[i]['err']);
				if(i==0) document.getElementById(dataerr[i]['el']).focus();
			}
			timererr=setInterval(function(){
				hubungi.reseterr();
			},waktu);
		}
	}
})();
