$(document).ajaxError(function() { // jQuery kļūdu paziņojumi
    if (window.console && window.console.error) {
        console.error(arguments);
    }
});

// Cepuma nolasīšana.
function getCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length ))) {
		return null;
	}
	if (start == -1) return null;
	var end = document.cookie.indexOf(';', len);
	if (end == -1) end = document.cookie.length;
	return unescape( document.cookie.substring(len, end));
}

// Cepuma uzstādīšana.
function setCookie(name, value, expires, path, domain, secure) { 
	var curCookie = name + "=" + escape(value) + 
	((expires) ? "; expires=" + expires.toGMTString() : "") + 
	((path) ? "; path=" + path : "") + 
	((domain) ? "; domain=" + domain : "") + 
	((secure) ? "; secure" : ""); 
	document.cookie = curCookie; 
}

// Cepuma dzēšana.
function deleteCookie(name, path, domain) {
	if (getCookie(name)) document.cookie = name + '=' +
			((path) ? ';path=' + path : '') +
			((domain) ? ';domain=' + domain : '') +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

// Atgriež tekstu bez nullēm sākumā.
function trim_null(s) {
	while (s.substring(0, 1) == '0') {
		s = s.substring(1, s.length);
	}
	return s;
}

// Atver palīga logu.
function help(strURL) {
	oWin = open("help/" + strURL, "vid_pdb_help", "height=400, width=400, directories=no, fullscreen=no, location=no, menubar=no, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=no");
}

// Atver izdrukas logu.
function printWindow(strURL) {
	oWin = open(strURL, "vid_pdb_print", "height=500, width=650, directories=no, fullscreen=no, location=yes, menubar=yes, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=yes");
}

// Atver vietas tipa logu akcīzes nodokļa licences adresei.
function apTypeWindow(strURL) {
	oWin = open(strURL, "ap_type", "height=400, width=400, directories=no, fullscreen=no, location=yes, menubar=yes, resizable=yes, scrollbars=yes, status=no, titlebar=no, toolbar=yes");
}

// Paziņojuma attēlošana lapā.
function showInfo(str_info) {
	if ($("#data").length > 0) {
		$("#data").html("<p class=\"notice\">" + str_info + "</p>");
	}
}

// Ja nepieciešams pārbaudes kods, pārbauda, vai ievadīts.
function isCheckCodeSet() {
	if ($("#cap_kods").length > 0) {
		if (jQuery.trim($("#cap_kods").val()).length == 0) {
			showInfo("Kods netika ievadīts, atkārtojiet vēlreiz.");
			return false;
		}
	}
	return true;
}

// Pārbaudes kods.
function loadCheckCode(bReqCheck, bFocus) {
	var uri_prm = (bReqCheck) ? "?check=true" : "";
	$("#checkCode").load("reqcode.asp" + uri_prm, function () {
		$("#checkCode>label:first").width($("#frmQuery label:first").width());
		if (bFocus) $("#cap_kods").focus();
	});
}

// Datu pieprasījums.
function doSubmitRequest(uri_str, req_data, check_code) {
	$("#data").empty();
	$("#data").addClass("on-loading");

	$.ajax({
		type: "POST",
		url: uri_str,
		data: req_data,
		dataType: "html",
		success: function(data) {
			// pieprasījuma atbildes apstrāde
			$("#data").removeClass("on-loading");
			if (data.substr(0, 10) == "check_code") {
				var respData = data.split('|');
				if (respData.length > 1) showInfo(respData[1]);	// +paziņojums
				loadCheckCode(false, true);
			} else {
				$("#data").html(data);
				loadCheckCode(check_code, false);
			}
		},
		error: function(xhr, err, e) {
			// pieprasījuma kļūdas apstrāde
			$("#data").removeClass("on-loading");
			showInfo("Kļūda, nosūtot pieprasījumu!");
		}
	});
}

$(document).ready(function() { // onload...do
	if (typeof onBeforeLoad == "function") {	// vai ir f-ja?
		onBeforeLoad();		// izsauc pirms ielādētā dokumenta iesniegšanas
	}
	// Submit apstrāde.
	$("#frmQuery").submit(function() {
		$("input#from").val(0);
		return Submit();
	})
})
