﻿// JavaScript
// 11. 12. 2008
// Written By Peter Širka

function getID(i)
{
    return document.getElementById(i);
}

function getNETID(i)
{
    return document.getElementById("ctl00_PageBody_" + i);
}

function check_integer(n){return RegExp ( "^[0-9]+$" ).test(n)}
function check_string(s){return RegExp ( "^[a-zA-Z]+$" ).test(s)}
function check_alfanum_string(s){return RegExp ( "^[a-zA-Z0-9]+$" ).test(s)}
function check_date(s){return RegExp ( "[0-9]{2,2}.[0-9]{2,2}.[0-9]{4,4}$" ).test(s)}
function check_time(s){return RegExp ( "^[012][0-9]:[0-5][0-9]$" ).test(s)}
function check_email(s){return RegExp ( "^[a-zA-Z0-9-_.]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$" ).test(s)}
function check_url(s){var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; return regexp.test(s)}
function check_sqldate(s){reg_date = /^\d{4}-\d{2}-\d{2}$/; return reg_date.test(s)}
function check_hex(n) {return (n<16 ? '0' : '' ) + n.toString(16)}

HttpRequest.prototype.MS_PROGIDS = new Array("Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP");
HttpRequest.prototype.READY_STATE_UNINITIALIZED = 0;
HttpRequest.prototype.READY_STATE_LOADING = 1;
HttpRequest.prototype.READY_STATE_LOADED = 2;
HttpRequest.prototype.READY_STATE_INTERACTIVE = 3;
HttpRequest.prototype.READY_STATE_COMPLETED = 4;
HttpRequest.prototype.successCallback = null;
HttpRequest.prototype.failureCallback = null;
HttpRequest.prototype.url = null;
HttpRequest.prototype.username = null;
HttpRequest.prototype.password = null;
HttpRequest.prototype.requestHeaders = new Array();
HttpRequest.prototype.status = null;
HttpRequest.prototype.statusText = null;
HttpRequest.prototype.responseXML = null;
HttpRequest.prototype.responseText = null;
HttpRequest.prototype.abort = HttpRequestAbort;
HttpRequest.prototype.setRequestHeader = HttpRequestSetRequestHeader;
HttpRequest.prototype.clearRequestHeaders = HttpRequestClearRequestHeaders;
HttpRequest.prototype.get = HttpRequestGet;
HttpRequest.prototype.post = HttpRequestPost;
HttpRequest.prototype.initiateRequest = HttpRequestInitiateRequest;
HttpRequest.prototype.getResponseHeader = HttpRequestGetResponseHeader;
HttpRequest.prototype.getAllResponseHeaders = HttpRequestGetAllResponseHeaders;

function HttpRequest(tag)
{
    this.tag = tag;
	this.xmlHttpRequest = null;
	if (window.XMLHttpRequest != null)
        this.xmlHttpRequest = new window.XMLHttpRequest();
	else if (window.ActiveXObject != null)
    {
	    var success = false;
		for (var i = 0; i < HttpRequest.prototype.MS_PROGIDS.length && !success; i++)
        {
			try
            {
			    this.xmlHttpRequest = new ActiveXObject(HttpRequest.prototype.MS_PROGIDS[i]);
				success = true
            }
			catch (ex) {}
        }
     }
	if (this.xmlHttpRequest == null)
    {
		alert("Chyba vo vytvorení XMLHTTP objektu.\nDoporučujeme Vám nainštalovať internetový prehliadač Mozilla Firefox.");
		return;
    }
}

function HttpRequestAbort()
{
    this.xmlHttpRequest.abort();
}

function HttpRequestSetRequestHeader(name, value)
{
	for (var i = 0; i < this.requestHeaders.length; i++)
    {
	    var pair = this.requestHeaders[i].split("\n");
		if (pair[0].toLowerCase() == name.toLowerCase())
        {
			this.requestHeaders[i] = name + "\n" + value;
			return;
        }
    }
	var n = this.requestHeaders.length;
	this.requestHeaders.push(name + "\n" + value);
}

function HttpRequestClearRequestHeaders()
{
    this.requestHeaders = new Array();
}

function HttpRequestGet()
{
    this.initiateRequest("GET", null);
}
function HttpRequestPost(data)
{
    this.initiateRequest("POST", data);
}

function HttpRequestGetResponseHeader(name)
{
    return this.xmlHttpRequest.getResponseHeader(name);
}

function HttpRequestGetAllResponseHeaders()
{
    return this.xmlHttpRequest.getAllResponseHeaders();
}

function HttpRequestInitiateRequest(method, data)
{
    this.status       = null;
	this.statusText   = null;
	this.responsetext == null;
	this.responseXML  = null;
	var refObj = this;

	this.xmlHttpRequest.onreadystatechange = function()
    {
	    refObj.readyState = refObj.xmlHttpRequest.readyState
		if (refObj.readyState == HttpRequest.prototype.READY_STATE_COMPLETED)
        {
		    refObj.status       = refObj.xmlHttpRequest.status;
		    refObj.statusText   = refObj.xmlHttpRequest.statusText;
			refObj.responseText = refObj.xmlHttpRequest.responseText;
			refObj.responseXML  = refObj.xmlHttpRequest.responseXML;
			if (refObj.status == 200)
            {
                if (refObj.successCallback != null)
                    refObj.successCallback(refObj);
            }
            else
            {
				if (refObj.failureCallback != null)
                    refObj.failureCallback(refObj);
            }
        }
    }

	var url = this.url;

	if (this.queryString != null)
        url = url + "?" + this.queryString;

	this.xmlHttpRequest.open(method, url, true, this.username, this.password);
	for (var i = 0; i < this.requestHeaders.length; i++)
    {
	    var pair = this.requestHeaders[i].split("\n");
		this.xmlHttpRequest.setRequestHeader(pair[0], pair[1]);
    }
	this.xmlHttpRequest.send(data);
}

function ajax_error(httpRequest)
{
   alert("Chyba, HTTP: " + httpRequest.status + " " + httpRequest.statusText + ".");
}

function ajax_call(url,obj)
{
    var ajax_id = new HttpRequest();
    ajax_id.tag = obj;
    ajax_id.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajax_id.failureCallback = ajax_error;
    ajax_id.url = url;
    return ajax_id;
}