﻿isset = function(obj) {
	return obj !== undefined;
}

isundef = function(obj) {
	return obj === undefined;
}

oneOf = function(testVal, defVal) {
	return (isset(testVal) ? testVal : defVal);
}

getSubarray = function(array, iIndexA, iIndexB) {
	if (iIndexA < 0) iIndexA = 0;
	if (iIndexB == null || iIndexB > array.length) iIndexB = array.length;
	if (iIndexA == iIndexB) return [];
	var aReturn = [];
	for (var i = iIndexA; i < iIndexB; i++) {
		aReturn.push(array[i]);
	}
	return aReturn;
}

addScript = function(src, charset) {
	try {
		alb_debug(src);
		var ss = '<scr' + 'ipt type="text/javascript" language=JavaScript charset="' + oneOf(charset, "utf-8") + '" src="' + src + '"></scr' + 'ipt>';
		document.write(ss);
	} catch (e) {
		alert("Exception in 'addScript' : " + e);
	}
}

addCSS = function(src, charset) {
	try {
		var ss = '<li' + 'nk rel="stylesheet" type="text/css" charset="' + oneOf(charset, "utf-8") + '" href="' + src + '" />';
		document.write(ss);
	} catch (e) {
		alert("Exception in 'addCSS' : " + e);
	}
}

window.alertEx = function(text, title, callback) {
	var div = $("<div>").attr({ "title": oneOf(title, '') });
	$("<p>").appendTo(div).html(text);
	div.dialog('destroy');
	div.dialog({
		resizable: false,
		height: 130,
		modal: true,
		closeOnEscape: true,
		show: 'drop',
		hide: 'drop',
		buttons: {
			'OK': function() {
				$(this).dialog('close');
				$(this).remove();
				$(".ui-effects-wrapper").remove();
				if (isset(callback)) callback();
			}
		}
	});
}

window.alertClassic = window.alert;

window.alert = alertEx;

window.confirmEx = function(title, text, okcb, cancelcb) {
	var div = $("<div>").attr({ "title": title });
	div.dialog('destroy');
	$("<p>").appendTo(div).html(text);
	div.dialog({
		resizable: false,
		height: 130,
		modal: true,
		closeOnEscape: true,
		show: 'drop',
		hide: 'drop',
		buttons: {
			'Да': function() {
				$(this).dialog('close');
				$(this).remove();
				$(".ui-effects-wrapper").remove();
				if (isset(okcb)) okcb();
			},
			'Нет': function() {
				$(this).dialog('close');
				$(this).remove();
				$(".ui-effects-wrapper").remove();
				if (isset(cancelcb)) cancelcb();
			}
		}
	});
	return true;
}

window.confirm = confirmEx;

window.overlay = function(src, noPreventCache) {

	var link = src + "&D=" + (new Date()).getTime();
	if (noPreventCache)
		link = src;

	var ScrollTop = document.body.scrollTop;
	if (ScrollTop == 0) {
		if (window.pageYOffset)
			ScrollTop = window.pageYOffset;
		else
			ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}

	var ScrollLeft = document.body.scrollLeft;
	if (ScrollLeft == 0) {
		if (window.pageYOffset)
			ScrollLeft = window.pageXOffset;
		else
			ScrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
	}

	var s_x = ScrollLeft;
	var s_y = ScrollTop;
	var c_x = $(window).width() / 2;
	var c_y = $(window).height() / 2;

	var preloader = $("<div>").attr("class", "preloader").
        css({ "position": "absolute", "z-index": "5", "display": "block", "width": "32px", "height": "32px", "left": s_x + c_x - 16, "top": s_y + c_y - 16, "cursor": "pointer" }).
        appendTo($(document.body)).
        click(function() { $(this).remove(); });
	$("<div>").attr("class", "wait").appendTo(preloader);


	$("<img>").attr({ "src": link, "id": "tempImg" }).load(function(res) {
		var w = this.width / 2;
		var h = this.height / 2;
		var x = s_x + c_x - w;
		var y = s_y + c_y - h;

		preloader.click();

		var div = $("<div>").attr("class", "divOverlay").css({ "left": x, "top": y });

		$("<div>").attr({ "class": "close-button" }).click(function() {
			$("div.divOpacity").remove();
			$("div.divOverlay").fadeOut('slow', function() {
				$(this).remove();

			});
		}).appendTo(div);
		$(this).appendTo(div);

		$("<div>").attr({ "class": "divOpacity" }).css({ "opacity": "0.8", "height": $(document).height() }).
                        click(function() {
                        	$("div.close-button").click();
                        }).appendTo($(document.body));

		div.appendTo($(document.body));

	});
}

function ShowMessage(message) {
    if (message.show == 1) {
        alert(message.text, message.title);
        message.show = 0;
    }
}

evalJSON = function(value) { return eval("(" + value + ")"); }

function refillDdl(ddlId) {

	var ddlToFill = document.getElementById(ddlId);
	
	if (ddlToFill.replaced) {
		var selArea = ddlToFill.selectArea;
		var optsDiv = selArea.optionsDiv;
		var optionsDiv = $(optsDiv);
		var id = $(optionsDiv).attr("id");
		var stWidth = optionsDiv.outerWidth();
		optionsDiv.remove();
		optionsDiv = document.createElement("div");
		$(optionsDiv).css({ "max-height": "300px", "overflow-y": "scroll" });
		var optionsList = document.createElement("ul");
		optionsDiv.innerHTML += "<div class='select-top'><div></div></div>";
		optionsDiv.appendChild(optionsList);
		ddlToFill._options = optionsList;
		optionsDiv.style.width = stWidth + "px";
		optionsDiv._parent = ddlToFill.selectArea;
		optionsDiv.className = "optionsDivInvisible";
		optionsDiv.id = id;
		ddlToFill.selectArea.optionsDiv = optionsDiv;
		populateSelectOptions(ddlToFill);
		optionsDiv.innerHTML += "<div class='select-bottom'><div class='select-bottom-left'></div><div class='select-bottom-right'></div></div>";
		document.getElementsByTagName("body")[0].appendChild(optionsDiv);
		ddlToFill.replaced = true;
	}

}

var Base64 = {

	// private property
	_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode: function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode: function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode: function (string) {
		string = string.replace(/\r\n/g, "\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if ((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode: function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while (i < utftext.length) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if ((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
