function show_picture(script, src, alt, width, height) {

// alt must be escaped or empty because escape(str) doesn't work with russian symbols
  if (  (script.substring(0,7).toLowerCase()) != "http://"  &&  (script.substring(0,8).toLowerCase() != "https://" ) ){
    script = frontBaseHref + script;
  }

  var w_width = 40 + parseInt(width);
  var w_height = 100 + parseInt(height);

  if (w_height > window.screen.availHeight)
    w_height = window.screen.availHeight;
  if (w_width > window.screen.availWidth)
    w_width = window.screen.availWidth;

  window.open(script+"?src=" + escape(src) + "&alt=" + alt + "&width="+width + "&height="+height, "pic",
              "resizable=yes, status=yes, scrollbars=yes, width=" + w_width + ", height=" + w_height);
}

function show_details(script) {
  if (  (script.substring(0,7).toLowerCase()) != "http://"  &&  (script.substring(0,8).toLowerCase() != "https://" ) ){
    script = frontBaseHref + script;
  }

  var w_width = 200;
  var w_height = 250;

  if (w_height > window.screen.availHeight)
    w_height = window.screen.availHeight;
  if (w_width > window.screen.availWidth)
    w_width = window.screen.availWidth;

  window.open(script, "pic", "resizable=yes, status=yes, scrollbars=yes, width=" + w_width + ", height=" + w_height);
}


function none(){
  return false;
}

function isEmail(string) {
//    if (string.search(/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+(;|,|$))+$/) != -1)
    if (string.search(/^(\w+[\w.-]*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+(;|,|$))+$/) != -1)
        return true;
    else
        return false;
}

//
// following functions will be deleted later
//

function collect_link(cform){
  var vlink = '';
  var first = 1;

  for(var i=0; i<cform.length; i++){
    el = cform.elements[i];
    if(el.type == 'hidden'){
      delim = (first)?'':'&';
      vlink = vlink + delim + el.name + '=' + escape(el.value);
      first = 0;
    }
  }
 return vlink;
}

function view_item(id){
  var sform = document.forms[_cms_document_form];
  var link = _cms_script_link;

  sform.elements['sort'].value = '';
  sform.elements['sdim'].value = '';

  return user_click('view', id);
}

function user_click(action, id){
  var sform = document.forms[_cms_document_form];
  var link = _cms_script_link;
  var anchor = '#anchor';

  if(action!='edit') anchor='';
  sform.elements['action'].value = action;
  sform.elements['id'].value = id;
  document.location = link + collect_link(sform) + anchor;

  return false;
}

function _go_page(sform, start, varname) {
  if(typeof(sform.elements[varname]) == 'object') {
    sform.elements[varname].value = start;
  } else {
    sform.elements['offset'].value = start;
  }
}

function go_page(start, varname){
  var sform = document.forms[_cms_document_form];
  var link = _cms_script_link;
  _go_page(sform, start, varname);
  sform.elements['action'].value = 'rsrtme';
  document.location = link + collect_link(sform);
  return false;
}

function go_pageSubmit(start, varname, action){
  var sform = document.forms[_cms_document_form];
  var link = _cms_script_link;
  _go_page(sform, start, varname);
  sform.elements['action'].value = action;
  sform.submit();
  return false;
}

function go_pagesize(size){
  var sform = document.forms[_cms_document_form];
  var link = _cms_script_link;

  sform.elements['action'].value = 'rsrtme';
  sform.elements['limit'].value = size;
  if(typeof(sform.elements['enc_limit']) == 'object') {
    sform.elements['enc_limit'].value = size;
  }
  document.location = link + collect_link(sform);

  return false;
}

function resort(ccol,cdim){
  var sform = document.forms[_cms_document_form];
  var link = _cms_script_link;

  sform.elements['action'].value = 'rsrtme';
  sform.elements['sort'].value = ccol;
  sform.elements['sdim'].value = cdim;
  
  if(typeof(CheckFilterForm) == "function")
    CheckFilterForm(sform, 0, 0);
  else
    document.location = link + collect_link(sform);
    
  return false;
}

function resortSubmit(ccol,cdim){
  return advResortSubmit(ccol,cdim,"sort","sdim");
}

function advResortSubmit(ccol,cdim,ccolname,cdimname){
  var sform = document.forms[_cms_document_form];
  //var link = _cms_script_link;
  //sform.elements['action'].value = 'rsrtme';
  sform.elements[ccolname].value = ccol;
  sform.elements[cdimname].value = cdim;
  sform.submit();
  return false;
}

function publish(id, act){
  var sform = document.forms[_cms_document_form];
  var link = _cms_script_link;

  sform.action.value = 'publish';
  sform.publish.value = act;
  sform.id.value = id;
  document.location = link + collect_link(sform);

  return false;
}

function setCookie(sName, sValue, path, days, hours){

  var oDate = new Date();
  if (typeof(days) == "undefined" && typeof(hours) == "undefined"){
    days = 30;
  }

  if (typeof(days) != "undefined") oDate.setDate(oDate.getDate() + days);
  if (typeof(hours) != "undefined") oDate.setHours(oDate.getHours() + hours);

  var pathStr = "";

  if( typeof(path) !="undefined" && path != "") {
      pathStr = "; path="+path;
  }
  delCookie(sName);
  document.cookie = sName + "=" + escape(sValue) + pathStr + "; expires="+oDate.toGMTString();
}

function delCookie(name, path, domain) {
    if(getCookie(name)){
        document.cookie = name + "=" + (path ? ";path=" + path : "") + (domain ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}

function getCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  var value = "";
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) {
      if(aCrumb[1] === undefined) {
        value = null;
      } else {
        value = unescape(aCrumb[1]);
      }
      return value;
    }
  }

  // a cookie with the requested name does not exist
  return null;
}

function AddToCart(url, itemId, numPrice, wrongPriceMsg){
    var qty = "1";
    if(document.forms["qty_"+itemId+'_'+numPrice] && document.forms["qty_"+itemId+'_'+numPrice].qty && itemId != 0)
        qty = document.forms["qty_"+itemId+'_'+numPrice].qty.value;
    var price = "";
    if(document.forms["qty_"+itemId+'_'+numPrice] && document.forms["qty_"+itemId+'_'+numPrice].price && itemId != 0){
        price = document.forms["qty_"+itemId+'_'+numPrice].price.value;
        if(isNaN(price) || price <= 0){
            if(wrongPriceMsg != 'none' && wrongPriceMsg != undefined)
                alert(wrongPriceMsg);
            return;
        }
    }
    document.location = frontBaseHref + url+'&qty='+escape(qty)+(price != "" ? "&price="+escape(price) : "");
}

function AddToCartProp(url, itemId, propId, numPrice){
    if(document.forms["qty_"+itemId+'_'+propId+'_'+numPrice] && document.forms["qty_"+itemId+'_'+propId+'_'+numPrice].qty && itemId != 0)
        document.location = frontBaseHref + url+'&qty='+document.forms["qty_"+itemId+'_'+propId+'_'+numPrice].qty.value;
    else
        document.location = frontBaseHref + url+'&qty=1';
}

/* ADVERTISING BLOCK */

var advCurTmStamp = new Date();
document.usedAdvData = "";
var isInnerHtmlSupported = -1;
var shownAdvPlaces = '';
var shownAdvPlacesCnt = 0;
var advReferrer = ''
var advPlaceCnts = new Array();
if(document.referrer)
    advReferrer = document.referrer;
function processShownAdvPlaces(shownAdvPlaces){
    counter = 0;
    pos = -1;
    res = "";
    while((pos = shownAdvPlaces.indexOf(';', pos+1)) >= 0){
        if((pos1 = shownAdvPlaces.indexOf(';', pos+1)) < 0)
            pos1 = shownAdvPlaces.length;
        if(pos1-pos-1 > 0){
            curAdvPlace = shownAdvPlaces.substr(pos+1, pos1-pos-1);
            
            if((cpos = curAdvPlace.indexOf('_')) >= 0){
                counter = curAdvPlace.substr(cpos+1)-1;
                curAdvPlace = curAdvPlace.substr(0, cpos);
            }
            
            advPlaceCnts[curAdvPlace] = ++counter;
            res += ';'+curAdvPlace+"_"+counter;
        }
    }
    if(res.length > 0)
        res += ';';
    return res;
}
function showAdvPlace(idPlace, viewURL){
    if(isInnerHtmlSupported == -1){
        if(document.body.innerHTML)
            isInnerHtmlSupported = 1;
        else
            isInnerHtmlSupported = 0;
    }
    // After the document is loaded (new scheme)
    if(isInnerHtmlSupported == 1){
        shownAdvPlacesCnt ++;
        document.write('<span id="advp_'+idPlace+'_'+(advPlaceCnts[idPlace] ? advPlaceCnts[idPlace] : shownAdvPlacesCnt)+'"></span>');
        shownAdvPlaces += (shownAdvPlaces == '' ? ';' : '')+idPlace+';';
    // Old scheme - inline method
    }else{
        if (!document.usedAdvData)
            document.usedAdvData = '';
        rndseed = new String(Math.random()); rndseed = rndseed.substring(2,11);
        document.write ("<" + "script language='JavaScript' type='text/javascript' src='"+viewURL+"aproc.php?action=view&rs="+advCurTmStamp.getTime()+rndseed+"&place="+idPlace+"&used="+document.usedAdvData+"&curl="+escape(viewURL)+"&referer="+escape(advReferrer)+"'><"+"/script>");
    }
}

function showAdvBanner(id, content){
    if(document.usedAdvData)
        document.usedAdvData += ','+id+',';
    else
        document.usedAdvData  = ','+id+',';
    document.writeln(content);
}

function aLnkClick(lnkID){
    var aLink = frontBaseHref+"aproc.php?action=lclick&id="+lnkID+"&page_url="+escape(document.location.href);
    advClick(aLink);
}

function advClick(clickURL){
    rndseed = new String(Math.random()); rndseed = rndseed.substring(2,11);
    clickURL+="&rs="+advCurTmStamp.getTime()+rndseed;
    img = new Image();
    img.src = clickURL;
}

var advPlacesContent = new Array();
var fillAdvPlaceWatcher = new Array();
function fillAdvPlace(idPlace, idCnt, placeContent){
    if(advPlacesContent[idPlace+'_'+idCnt] == undefined)
        advPlacesContent[idPlace+'_'+idCnt] = placeContent;
    if(fillAdvPlaceWatcher[idPlace+'_'+idCnt] == undefined)
        fillAdvPlaceWatcher[idPlace+'_'+idCnt] = 0;
    if(document.getElementById && document.getElementById('advp_'+idPlace+'_'+idCnt) && document.getElementById('advp_'+idPlace+'_'+idCnt).innerHTML != undefined){
        document.getElementById('advp_'+idPlace+'_'+idCnt).innerHTML = advPlacesContent[idPlace+'_'+idCnt];
    }else{
        fillAdvPlaceWatcher ++;
        if(fillAdvPlaceWatcher < 1000)
            setTimeout("fillAdvPlace('"+idPlace+"', '"+idCnt+"');", 100);
    }
}

/* MODULE ADVERTISEMENT PART [not in use] */
/*
// String that contains module show data
var modAdvIdsArr = new Array();

// Set item view
function setView(modName, bodyType, id, idPlace){
    if(isNaN(idPlace))
        idPlace = 0;
    var isFound = 0;
    for(var i = 0; i < modAdvIdsArr.length; i++){
        if(modAdvIdsArr[i][0] == modName){
            modAdvIdsArr[i][1] += '|'+bodyType+id+','+idPlace;
            isFound = 1;
            break;
        }
    }
    if(!isFound)
        modAdvIdsArr[modAdvIdsArr.length] = new Array(modName, bodyType+id+','+idPlace);
}
// Process gathered adv IDs
function processViewAdvIds(){
    var retStr = '';
    for(var i = 0; i < modAdvIdsArr.length; i++)
        retStr += (retStr != '' ? ';' : '')+modAdvIdsArr[i][0]+'='+modAdvIdsArr[i][1];
    alert(retStr);
    return retStr;
}
*/

// Returns cookie value
function getCookie(name) {
    var search = name + "=";
    if (document.cookie.length > 0) {
        // if there are any cookies
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            // if cookie exists
            offset += search.length;
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) {
                end = document.cookie.length;
            }
            return unescape(document.cookie.substring(offset, end));
        }
    }
}

// {{{ window.onLoad events queue implementation

var onLoadEvents = new Array ();
var previousOnLoadEvent;

function addOnLoadEvent(event) {
    onLoadEvents[onLoadEvents.length] = event;
}

function savePreviousOnLoadEvent() {
    previousOnLoadEvent = window.onload;
    window.onload = runOnLoadEventsQueue;
}

function runOnLoadEventsQueue() {
    if (previousOnLoadEvent) {
        previousOnLoadEvent();
    }
    for (var i = 0 ; i < onLoadEvents.length; i++) {
        onLoadEvents[i]();
    }
}

// }}}

function setCaptchaMD5Hash(sid)
{
    var cookie = getCookie('captcha_' + sid);
    if (cookie && cookie.length) {
        clearInterval(eval("intervalId_" + sid));
        eval("captcha_" + sid + " = cookie;");
        var path    = null;
        var domain  = null;
        if (location) {
            domain = location.host;
            path = frontBaseHref.replace(/^\w+\:\/\/[^\/]+/, '');
        }
        delCookie('captcha_' + sid, path, domain);
    }
}

if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function(val, fromIndex) {
        if (typeof(fromIndex) != 'number') {
            fromIndex = 0;
        }
        for (var index = fromIndex,len = this.length; index < len; index++) {
            if (this[index] == val) {
                return index;
            }
        }
        return -1;
    }
}

if (!Array.prototype.splice) {
    Array.prototype._splice = function(start) {
        if (start >= this.length) {
            return;
        }
        return this.slice(start);
    }
    Array.prototype._splice = function(start, deleteCount) {
        if (start >= this.length) {
            return;
        }
        return this.slice(0, start-1).concat(this.slice(start + deleteCount));
    }
}

function cloneArray(source)
{
    var destination = new Array();
    for (var i = 0 ; i < source.length ; i++) {
        if (!(typeof(source[i]) == 'undefined')) {
            destination[i] = source[i];
        }
    }
    return destination;
}

/* products comparison */

var
    mComparisonList = new Array (),
    mComparisonURL = '';

function compare(key)
{
    if (compareProducts.indexOf(key) >= 0) {
        alert(compareInComparisonAlready);
        return false;
    }
    if (mComparisonList.length && !confirm(compareAddSelected)) {
        return false;
    }
    if (compareProducts.length == compareMaxQuantity) {
        alert(compareMaxMessage);
        return false;
    }
    if (mComparisonList.indexOf(key) < 0) {
        mComparisonList.push(key);
    }

    // check for different datasets
    if (compareDisallowDifferentDatasets && compareProducts.length > 0) {
        var datasetId = compareDatasetId;
        var _mComparisonList = cloneArray(mComparisonList);
        for (var i = 0, qty = _mComparisonList.length ; i < qty ; i++) {
            var p = _mComparisonList[i].split('-'); // p[2] is containing datasetId now
            if (!compareDatasetId) {
                compareDatasetId = p[2];
            }
            if (compareDatasetId != p[2]) {
                if (!confirm(compareConfirmOtherDataset)) {
                    return false;
                }
                break;
            }
        }
    }
    return mSubmitAddToCompare();
}

function compareClear()
{
    if (confirm(compareConfirmListClearing)) {
        document.location = location.pathname + '?action=compareClear';
    }
    return false;
}

function mCompare(oCheckbox)
{
    var key = oCheckbox.value;
    if (oCheckbox.checked) {
        // exclude duplicates
        if (compareProducts.indexOf(key) >= 0) {
            alert(compareInComparisonAlready);
            oCheckbox.checked = false;
            return false;
        }
        // check for max quantity of products to compare
        if (compareProducts.length == compareMaxQuantity) {
            alert(compareMaxMessage);
            oCheckbox.checked = false;
            return false;
        }
        // check for different datasets
        if (compareDisallowDifferentDatasets) {
            var p = key.split('-');
            if ((compareProducts.length + mComparisonList.length) > 0 && p[2] != copmpareLastDatasetId && !confirm(compareConfirmOtherDataset)) {
                oCheckbox.checked = false;
                return false;
            }
            copmpareLastDatasetId = p[2];
            // uncheck checked products having other dataset
            var _mComparisonList = cloneArray(mComparisonList);
            for (var i = 0, qty = _mComparisonList.length ; i < qty ; i++) {
                var p = _mComparisonList[i].split('-');
                if (p[2] != copmpareLastDatasetId) {
                    var o = document.getElementById('cmp_' + _mComparisonList[i]);
                    o.checked = false;
                    mCompare(o);
                }
            }
            // count real number of products to comapere after adding
            var _compareProducts = cloneArray(compareProducts);
            for (var i = 0, qty = compareProducts.length ; i < qty ; i++) {
                var p = compareProducts[i].split('-');
                if (p[2] != copmpareLastDatasetId) {
                    var index = _compareProducts.indexOf(compareProducts[i]);
                    if (Array.prototype.splice) {
                        _compareProducts.splice(index, 1);
                    } else {
                        _compareProducts = mComparisonList._splice(index, 1);
                    }
                }
            }
            // check for max quantity of products to compare after manipulations
            if (_compareProducts.length + mComparisonList.length >= compareMaxQuantity) {
                alert(compareMaxMessage);
                oCheckbox.checked = false;
                return false;
            }
        }
        mComparisonList.push(key);
    } else {
        var index = mComparisonList.indexOf(key);
        if (index > -1) {
            if (Array.prototype.splice) {
                mComparisonList.splice(index, 1);
            } else {
                mComparisonList = mComparisonList._splice(index, 1);
            }
            copmpareLastDatasetId = compareDatasetId;
        }
    }
}

function mSubmitAddToCompare()
{
    var qty = mComparisonList.length;

    if (!qty) {
        alert(compareListIsEmpty);
        return false;
    }

    var _mComparisonList = cloneArray(mComparisonList);

    // check for added already products and exclude its from mComparisonList
    for (var i = 0 ; i < qty ; i++) {
        var index = compareProducts.indexOf(_mComparisonList[i]);
        if (index >= 0) {
            // exclude duplicate product
            if (Array.prototype.splice) {
                mComparisonList.splice(_mComparisonList.indexOf(mComparisonList[i]), 1);
            } else {
                mComparisonList = mComparisonList._splice(_mComparisonList.indexOf(mComparisonList[i]), 1);
            }
        }
    }

    document.location = location.pathname + '?action=compare&products=' + _mComparisonList.join(';');
    return false;
}

function mCompareSelected(url)
{
    if (mComparisonList.length < 2) {
        if (mComparisonList.length < 1 && compareProducts.length > 1) {
            window.open(frontBaseHref + url + '?p=' + compareProducts.join(';') + '&h=&v=all&lay_id=100');
            return false;
        }
        alert(compareListInsufficient);
        return false;
    }
    if (compareProducts.length && !confirm(compareConfirmPreviousClearing)) {
        return false;
    }
    window.open(frontBaseHref + url + '?p=' + mComparisonList.join(';') + '&h=&v=all&lay_id=100');
    return false;
}

/* /products comparison */


var calendarBlock;
var calendarDateFieldName;
function getCalendar(in_dateField, lang){
    calendarTarget = in_dateField;
    calendarBlock = document.getElementById("calendar_block");
    if(calendarBlock && (calendarBlock.style.display!="block" || (in_dateField.form.name + in_dateField.name!=calendarDateFieldName))){
      calendarDateFieldName = in_dateField.form.name + in_dateField.name;
      cLeft = getx(calendarTarget)+calendarTarget.offsetWidth;
      cTop = gety(calendarTarget)+calendarTarget.offsetHeight;

      document.getElementById("calendar_block_frm").src = "calendar.php?v=2&lang="+lang;
      calendarBlock.style.display="block";
      var correctLeft = cLeft + calendarBlock.offsetWidth - document.body.scrollLeft - document.body.clientWidth;
      var correctTop = cTop + calendarBlock.offsetHeight - document.body.scrollTop - document.body.clientHeight;
      calendarBlock.style.left = cLeft - ((correctLeft > 0)?correctLeft:0);
      calendarBlock.style.top = cTop - ((correctTop > 0)?correctTop:0);
      document.getElementById("calendar_block_frm").contentWindow.document.body.focus();
    }else if(calendarBlock){
      calendarBlock.style.display="none";
    }
}

function replaceDateTitle(objId){
    var objObj = document.getElementById(objId);
    if(objObj){
        var curDate = objObj.innerHTML;
        var rxToday = new RegExp(DATE_CONVERTION[2].replace(/\./, "\\."));
        var rxYesterday = new RegExp(DATE_CONVERTION[3].replace(/\./, "\\."));
        curDate = curDate.replace(rxToday, DATE_CONVERTION[0]);
        curDate = curDate.replace(rxYesterday, DATE_CONVERTION[1]);
        objObj.innerHTML = curDate;
    }
}

/*
Пример использования календаря
<div  id="calendar_block"  style="display:none;position:absolute;background-color:#f8f8f8;width:220px;height:345px;"><table border="0" cellpadding="0" cellspacing="0" width=100% height=100%><tr><td style="padding:0px;"><iframe id="calendar_block_frm" width=100% height=100% src="calendar.php" frameborder=0 scrolling=no></iframe></td></tr></table></div>

<form name=fltform>
дата:
<input type="text" name="testday" value="01.04.2007">
<a href="javascript:getCalendar(document.fltform.testday, 'ru');">
<img src="_img/calendar.gif" border=0></a>
</form>
*/

 
