if (window.com == null) {
    window.com ={};
}
if (window.com.intrice == null) {
    window.com.intrice ={};
}
com.intrice.Request = function () {
    var reJsessionId = /;jsessionid=[\dA-Fa-f]*/;
    this.baseLink = (new String(window.location.pathname)).replace(reJsessionId, '');
    var matches = window.location.href.match(reJsessionId);
    this.jsessionid = matches ? matches[0] : '';
    this.params = new Array();
    var tempParams = window.location.search.substring(1).split("&");
    for (var i = 0; i < tempParams.length; i++) {
        if (tempParams[i].length > 0) {
            var paramAr = tempParams[i].split("=");
            this.params[paramAr[0]] = paramAr;
        }
    }
    this.hash = window.location.hash;
};
com.intrice.Request.prototype.getParameter = function (parametrName) {
    var paramAr = this.getParamArray(parametrName);
    return (paramAr) ? paramAr[1] : paramAr;
};
com.intrice.Request.prototype.hasJsessionId = function() {
    return this.jsessionid.length > 0;
};
com.intrice.Request.prototype.getParamArray = function (parametrName) {
    return this.params[parametrName];
};
com.intrice.Request.prototype.removeParameter = function (parametrName) {
    delete this.params[parametrName];
};
com.intrice.Request.prototype.setParameter
        = function (parametrName, parametrValue) {
    var paramAr = this.getParamArray(parametrName);
    if (!paramAr) paramAr = new Array();
    paramAr[0] = parametrName;
    paramAr[1] = parametrValue;
    this.params[parametrName] = paramAr;
};
com.intrice.Request.prototype.paramsToStr = function () {
    var result = "";
    for (var param in this.params) {
        result += this.params[param][0] + "=" + this.params[param][1] + "&";
    }
    return result.substr(0,result.length - 1);
};
com.intrice.Request.prototype.getLink = function () {
    var params = this.paramsToStr();
    return this.baseLink
            + this.jsessionid
            + ((params.length > 0) ? "?" + params : "")
            + this.hash;
};
com.intrice.Request.prototype.redirect = function (link) {
    if (link) {
        window.location.replace(link);
    } else {
        window.location.replace(this.getLink());
    }
};
var request = new com.intrice.Request();

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name) {
    var cookie = " " + document.cookie;
    var search = " " + name + "=";
    var setStr = null;
    var offset = 0;
    var end = 0;
    if (cookie.length > 0) {
        offset = cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = cookie.indexOf(";", offset);
            if (end == -1) {
                end = cookie.length;
            }
            setStr = unescape(cookie.substring(offset, end));
        }
    }
    return(setStr);
}

function getParameter(queryString, parameterName) {
    // Add "=" to the parameter name (i.e. parameterName=value)
    parameterName = parameterName + "=";
    if (queryString.length > 0) {
        // Find the beginning of the string
        var begin = queryString.indexOf(parameterName);
        // If the parameter name is not found, skip it, otherwise return the value
        if (begin != -1) {
            // Add the length (integer) to the beginning
            begin += parameterName.length;
            // Multiple parameters are separated by the "&" sign
            var end = queryString.indexOf("&", begin);
            if (end == -1) {
                end = queryString.length;
            }
            // Return the string
            return unescape(queryString.substring(begin, end));
        }
    }
    // Return "null" if no parameter has been found
    return null;
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var res = new Array();
    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this)
      {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
          res.push(val);
      }
    }

    return res;
  };
}

//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license

if (!Array.prototype.every)
{
  Array.prototype.every = function(fun /*, thisp*/)
  {
    var len = this.length;
    if (typeof fun != "function")
      throw new TypeError();

    var thisp = arguments[1];
    for (var i = 0; i < len; i++)
    {
      if (i in this &&
          !fun.call(thisp, this[i], i, this))
        return false;
    }

    return true;
  };
}

function getChildById(obj,idObj){
	var childs = obj.children;
	var result = null;
	for(var i=0;i<childs.length;i++){
		if(childs.item(i).id == idObj)
			return childs.item(i);
		result = getChildById(childs.item(i),idObj);
		if(result) break;
	}
	return result;
}

//стандартная функция смены языка
function changeLang(lang) {
    if (lang != null) {
        setCookie("USER_LANGUAGE", lang.toUpperCase(), "10/01/2077", "/");
        if (getCookie("USER_LANGUAGE") == lang.toUpperCase() && ! request.hasJsessionId()) {
            request.removeParameter("LANG");
        } else {
            request.setParameter("LANG",lang);
        }
        request.redirect();
    }
}

function checkOS() {
   var OpSys = '';
   var n;
   if ('object' == typeof clientInformation){
       n = clientInformation;
   } else {
       n = navigator;
   }
   var ua = ' ' + n.userAgent.toLowerCase();
   if ((ua.indexOf('Win') != -1) && (ua.indexOf('95') != -1))
      OpSys = "Windows95";
   else if ((ua.indexOf('Win') != -1) && (ua.indexOf('98') != -1))
      OpSys = "Windows98";
   else if ((ua.indexOf('Win') != -1) && (ua.indexOf('Me') != -1))
      OpSys = "WindowsMe";
   else if (ua.indexOf('nt 4.0') > 0 && ua.indexOf('win') > 0) {
      OpSys = "Windows NT";
   }
   if (OpSys != '') {
      alert('Sorry but your operation system '+OpSys+' is not supported.');
      return false;
   }
   return true;
}

function contactUs(logged, onlinesupport, link) {
    if (logged == 'true' && onlinesupport == 'true') {
        var hwin = window.open('http://server.iad.liveperson.net/hc/37589485/?cmd=file&file=visitorWantsToChat&site=37589485&byhref=1&imageUrl=http://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a/', "Contact_Us", "width=610,height=567,scrollbars=1,status=0,toolbar=0,directories=0,menubar=1,location=1,resizable=1");
        if (hwin) {
            hwin.focus();
        }
    } else {
        window.location = link;
    }
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"0",-1);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function gotoDfCashier(action, url, params){
    if(!url){
        url = "/cashier/dfcashier";
    }
    if(!params){
        params = '';
    }
	createCookie("DF_AUTO_LOGIN", "1", "1");
    window.location.href = url + "?action=" + action + params;
}

function submitenter(myfield, e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13) {
        var form = myfield.form;
        if (form.onsubmit()) {
            form.submit();
            return false;
        } else {
            return true;
        }

    } else {
        return true;
    }
}

function submitForm(form) {
    if (!form) return;
    if (!(form.onsubmit instanceof Function) || (form.onsubmit())) {
        form.submit();
    }
}

function gotoCash2Net(url){
	var newUrl=url+"?PaymentMethodSelector=10&switch_flag=true";
	window.location.href=newUrl;
}

function openSendMessageWindow (url) {
	var wnd = window.open(url, '', "width=820,height=700,scrollbars=1,status=0,toolbar=0,directories=0,menubar=0,location=0,resizable=1");
	if (wnd != null) {
		wnd.focus();
	}
}

function writeOpenTag(tagName) {
	document.write('<' + tagName + '>');
}

function writeCloseTag(tagName) {
	document.write('</' + tagName + '>');
}

function isInteger(s)
{
    var i;

    if (isEmpty(s))
        if (isInteger.arguments.length == 1) return 0;
        else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return true;
}

function isEmpty(s)
{
    return ((s == null) || (s.length == 0));
}

function isDigit(c)
{
    return ((c >= "0") && (c <= "9"));
}

function enterDigits(e) {
    var code;
    if (!e) e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    if (code < 48 && code != 32) return true;
    var character = String.fromCharCode(code);
    return isDigit(character);

}

function openUrl (url) {
	var wnd = window.open(url, '', "scrollbars=1,status=1,toolbar=1,directories=0,menubar=1,location=0,resizable=1");
	if (wnd != null) {
		wnd.focus();
	}
}

function doTimezone() {
    var tzo = new Date().getTimezoneOffset();
    var cTimezone = getCookie("TIMEZONE");
    var cTzo = getCookie("timezoneOffset");
    var nTzo = escape(tzo * (-1));
    if (nTzo != cTimezone && (!cTzo || cTzo != nTzo)) {
        setCookie("timezoneOffset", tzo * (-1), "10/01/2077", "/");
        var nCTzo = getCookie("timezoneOffset");
        if (nCTzo) {
            request.redirect();
        }
    }
}

String.prototype.startsWith = function(str) {
    return this.substr(0, str.length) === str;
};

doTimezone();
