(function () {
var externalModules = {}; // local variable to be enclosed in global function STC
STC = function () // this is global variable
{
//
// this function defines stc utilities library
//
// usage:
// var stc = STC();
// var rand = stc.utils.randomizer()
//
var stc = {};
////////////////////////////////////////////
//
// define stc.utils
//
(function () {
var utils = {};
utils.randomizer = function () {
return Math.floor(Math.random() * 1000000);
}
utils.getNewId = function () {
bOk = false;
while (!bOk) {
var id = "_CDOMID_" + Math.floor(Math.random() * 1000000);
bOk = (0 == $("#" + id).size());
}
return id;
}
var eMailPattern = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/gi; // останній флаг /gi означає global case insensitive, УВАГА! В інших regex середовищах (відмінних від JavaScript) можуть бути інші варіанти з case insensitive
utils.extractEMail = function (str, exctractSeveral) {
var mail = str.match(eMailPattern);
if (mail && !exctractSeveral) {
return mail[0]; // повертаємо лише першу адресу
}
else {
return mail;
}
}
utils.validateEMail = function (str) {
// чомусь валідація, на відміну від екстракції працює через раз - розібратись
return eMailPattern.test(str);
}
utils.validateRealNumber = function (str) {
var hasSign = false;
var hasIntPart = false;
var hasSeparator = false;
var hasFractionalPart = false;
var i = 0;
if (i < str.length && str[i] == '-') {
hasSign = true;
i += 1;
}
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
hasIntPart = true;
i += 1;
}
if (str.length == i && hasIntPart) {
return true;
}
if (str[i] == '.' || str[i] == ',') {
hasSeparator = true;
i += 1;
}
else {
return false;
}
while (i < str.length && str[i] >= '0' && str[i] <= '9') {
hasFractionalPart = true;
i += 1;
}
return str.length == i && hasFractionalPart;
}
stc.utils = utils;
})();
//
// end of stc.utils
//
////////////////////////////////////////////
////////////////////////////////////////////
//
// define stc.http
//
(function () {
var http = { dummy: null };
http.detemplate = function (urlTemplate, values) {
values = values || {};
var url = urlTemplate.replace("{randomizer}", stc.utils.randomizer());
for (e in values) {
url = url.replace("{" + e + "}", values[e]);
}
return url;
}
http.completeUrl = function (url) {
url = (url) ? url.toString() : "";
if (url == "") {
return "/";
}
else {
if (url[url.len - 1] == "/") {
return url;
}
else {
return url + "/";
}
}
}
http.stripUrlL = function (url) {
url = (url) ? url.toString() : "";
while (url.length > 0 && "/" == url[0]) {
url = url.substring(1, url.length - 1);
}
return url;
}
http.stripUrlR = function (url) {
url = (url) ? url.toString() : "";
while (url.length > 0 && "/" == url[url.length - 1]) {
url = url.substring(0, url.length - 1);
}
return url;
}
http.composeUrl = function (arr) {
if (arguments.length == 0) {
return "";
}
else if (arguments.length == 1) {
if (((arr.length && (typeof arr.length) === 'number') || arr.length === 0) && !arr.substring) // this is to check that arr parameter is an array
{
var url = "";
for (var i = 0; i < arr.length; i++) {
url += (url == "" ? http.stripUrl(arr[i]) : "/" + http.stripUrl(arr[i]));
}
return url;
}
else {
return http.stripUrl(arr.toString());
}
}
else {
return http.composeUrl(arguments);
}
}
http.stripUrl = function (url) {
return http.stripUrlL(http.stripUrlR(url));
}
http.protocol = function () {
return "http://";
}
http.host = function () {
return document.location.host;
}
http.siteUrl = function () {
return http.protocol() + http.host();
}
http.url = function (relativeUrl) {
if (!relativeUrl) {
return http.siteUrl();
}
relativeUrl = relativeUrl ? relativeUrl.toString() : "";
if ("" == relativeUrl) {
return http.siteUrl();
}
else {
//return http.stripUrl(http.siteUrl()) + (relativeUrl[0] == "/" ? relativeUrl : "/" + relativeUrl);
return http.composeUrl([http.siteUrl(), relativeUrl]);
}
}
http.createDefaultErrorHandler = function (continuation) {
// створює jquery callback для обробки помилок у ajax-сценаріях
// цей callback відображатиме errorBox після закриття якого можливе виконання continuation-методу
return function (xhr, textStatus, errorThrown) {
var msg = "";
if (400 == xhr.status || 403 == xhr.status || 404 == xhr.status) {
msg += "";
}
else {
msg += "Код помилки " + xhr.status + "
";
}
// знімаємо url повідомлення про помилку з спеціального http header
var xHeader = xhr.getResponseHeader("x-stc-ajaxErrorMessageUrl"); // ім'я заголовку має бути параметором ініціалізації модуля...
if (xHeader && "" != xHeader) {
var messagePaneId = stc.utils.getNewId();
msg += '