//JavaScript'овая реализация request позволяющая работать
// с параметрами незаморачиваясь.
var com ={};
com.intrice ={};
com.intrice.request = function () {
    this.baseLink = window.location.pathname;
    this.params = new Array();
    var tempParams = window.location.search.substring(1).split("&");
    for (var param in tempParams) {
        if (tempParams[param].length > 0) {
            var paramAr = tempParams[param].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.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 + ((params.length > 0) ? "?" + params : "");
};
com.intrice.request.prototype.getLinkWithHash = function () {
    return this.getLink() + 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;
}
//стандартная функция смены языка
function changeLang(lang) {
    if (lang != null) {
        setCookie("USER_LANGUAGE", lang.toUpperCase(), "10/01/2077", "/");
        if (getCookie("USER_LANGUAGE") == lang.toUpperCase()) {
            request.removeParameter("LANG");
        } else {
            request.setParameter("LANG",lang);
        }
        request.redirect();
    }
}


function getPersonaliText(text) {
    if (text.length > 15) {
        var txt = text.substring(0,15) + "...";
        document.write(txt);
    } else {
        document.write(text);
    }
}
function showPeriod(whatPeriod) {
    if (whatPeriod) {
        document.getElementById('calendarForm').PERIODTYPE.value=whatPeriod;
    }
    document.getElementById('calendarForm').page.value=1;
    document.getElementById('calendarForm').submit();
}

function goToUsersList(type, id) {
    document.getElementById('userslist').type.value = type;
    document.getElementById('userslist').ID.value = id;
    document.getElementById('userslist').submit();
}

  function checkPwdStrength(a,outVisual, outText) {
  var c = 0;
//  var l = new Array('Weak', 'Medium', 'Strong', 'Best', 'Too Short');
  var r = new Array('d8fe1d',  'f7f820',  'ffab10',  'fc5803', '81fe1e');
  var lvl = 0;
  var d = l[0];
  var e = "";
  if(a.length<5){c=(c+7)}else if(a.length>4&&a.length<8){c=(c+14)}else if(a.length>7&&a.length<16){c=(c+17)}else if(a.length>15){c=(c+23)}
  if(a.match(/[a-z]/)){c=(c+6)}
  if(a.match(/[A-Z]/)){c=(c+6)}
  if(a.match(/\D+/)){c=(c+10)}
  if(a.match(/(.*[0-9].*[0-9].*[0-9])/)){c=(c+10)}
  if(a.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)){c=(c+10)}
  if(a.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){c=(c+10)}
  if(a.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){c=(c+7)}
  if(a.match(/([a-zA-Z])/)&&a.match(/([0-9])/)){c=(c+7)}
  if(a.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)){c=(c+15)}
  var cc = 0;
  if(c<=30){lvl = 0; cc=25;}else
    if(c>30&&c<=50){lvl = 1; cc=50;}else
      if(c>50&&c<=70){lvl = 2;cc=75;}else
        if(c>70){lvl = 3; c=100;cc=100;}
  if(c>=100){c=100; lvl=3}
  document.registerform.PASSWORD_STRENGTH.value = lvl;
  if (a.length == 0) {
    c = 0;
    cc = 0;
    lvl = 4;
  }
  outVisual.style.background='#'+r[lvl];
  outVisual.innerHTML='<div style="background-color: #321; width: '+cc+'%; height: 100%;font-size:1px;">&nbsp;</div>';
  outText.innerHTML = l[lvl];
  return lvl;
  }

    function checkPassStrength(a) {
  var c = 0;
  var lvl = 0;
  if(a.length<5){c=(c+7)}else if(a.length>4&&a.length<8){c=(c+14)}else if(a.length>7&&a.length<16){c=(c+17)}else if(a.length>15){c=(c+23)}if(a.match(/[a-z]/)){c=(c+9)}
  if(a.match(/[A-Z]/)){c=(c+10)}
  if(a.match(/\d+/)){c=(c+10)}
  if(a.match(/(.*[0-9].*[0-9].*[0-9])/)){c=(c+10)}
  if(a.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)){c=(c+10)}
  if(a.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/)){c=(c+10)}
  if(a.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)){c=(c+7)}
  if(a.match(/([a-zA-Z])/)&&a.match(/([0-9])/)){c=(c+7)}
  if(a.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/)){c=(c+15)}
  var cc = 0;
  if(c<=30){lvl = 0;}else
    if(c>30&&c<=50){lvl = 1;}else
      if(c>50&&c<=70){lvl = 2;}else
        if(c>70){lvl = 3;}
  if(c>=100){lvl=3}
  if (a.length == 0) {
    lvl = 0;
  }
  return lvl;
  }

  var cc="asdfghjklASDFGHJKL";
  var vv="qwertyuiopQWERTYUIOP";
  var nn="0123456789";
  var ss="!@#$%^&*()-+";

function makeRand(max) {
    return Math.floor(Math.random()*max);
}

function makePW(PASS, PASS_CONFIRM) {
    var len = 8;
    var uc = true;
    var o=cc+vv+nn+vv+cc;
    var pw="";
    var p = 3;
    for (var i=0; i<len; i++) {
        switch (i%p) {
        case 0:
            if (uc) {
                pw+=cc.substr(makeRand(21), 1);
                break;
            }
        default:
            pw+=o.substr(makeRand(o.length), 1);
            break;
        }
    }
    var lvl = checkPassStrength(pw);
    while (lvl < 2) {
        var pw="";
        for (var i=0; i<len; i++) {
            switch (i%p) {
            case 0:
                if (uc) {
                    pw+=cc.substr(makeRand(21), 1);
                    break;
                }
            default:
                pw+=o.substr(makeRand(o.length), 1);
                break;
            }
        }
        lvl = checkPassStrength(pw);
    }
    PASS.value=pw;
    PASS_CONFIRM.value='';
    PASS.focus();

}

function changeInputType(
  oldElm, // a reference to the input element
  iType // value of the type property: 'text' or 'password'
  ) {
  var isMSIE=/*@cc_on!@*/false;
  if(!isMSIE){
    var newElm=document.createElement('input');
    newElm.type=iType;
  } else {
    var newElm=document.createElement('span');
    newElm.innerHTML='<input type="'+iType+'" name="'+oldElm.name+'">';
    newElm=newElm.firstChild;
  }
  var props=['name','id','className','size','tabIndex','accessKey', 'onblur', 'onchange', 'value'];
  for(var i=0,l=props.length;i<l;i++){
    if(oldElm[props[i]]) newElm[props[i]]=oldElm[props[i]];
  }
  oldElm.parentNode.replaceChild(newElm,oldElm);
  return newElm;
}

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();
	}
}
