function getTag(html,tag){
    var b,e;
    b = html.indexOf('<'+tag+'>');
    e = html.indexOf('</'+tag+'>');
    if(b<0 || e<0 || e<=b)
        return '';
    return html.substring(b+2+tag.length, e);
}

function remTag(html,tag){
    var b,e;
    b = html.indexOf('<'+tag+'>');
    e = html.indexOf('</'+tag+'>');
    if(b<0 || e<0 || e<=b)
        return html;
    return remTag(html.substring(0, b) + html.substring(e+3+tag.length, html.length));
}

function showajaxformcont(cont,url,params,loadingimg,eval_expr){
    if(!params){
        params='';
    }
    if(loadingimg)$(loadingimg).show();
    new Ajax.Request(url+'?fromajax=true&rand='+Math.random()+'&'+params,
        {
            method:'get',
            onSuccess: function(transport) {
                var response = transport.responseText!='' ? transport.responseText : "Нет ответа";
                if( (loc = getTag(response,'location')) != '' ){
                    window.location.href = loc;
                    return;
                }
                $(cont).update(response);
                if(loadingimg)$(loadingimg).hide();

                if (eval_expr!= '') {
                    eval (eval_expr);
                }

            },
            onFailure: function(){ $(cont).update('Ошибка соединения'); if(loadingimg)$(loadingimg).hide();
            }
        });

}

function sendajaxformcont(cont,frm,sendbtn,onsendaction,loadingimg,eval_expr){
    var loc=-1;
    if(!frm || !$(frm)){
        $(cont).update('Нет формы');
        return;
    }
    if(!sendbtn){
        sendbtn='';
    }
    if(!onsendaction){
        onsendaction='';
    }
    if(loadingimg)$(loadingimg).show();
    $(frm).request({
                    /*method: 'post',*/
                    parameters: { sendbtn:sendbtn, fromajax:'true' },
                    onComplete: function(transport){
                        var response = transport.responseText!='' ? transport.responseText : "Нет ответа";
                        if( (loc = getTag(response,'location')) != '' ){
                            window.location.href = loc;
                            return;
                        }
                        $(cont).update(response);
                        if(onsendaction!='')
                            eval(onsendaction);
                        if(loadingimg)$(loadingimg).hide();

                        if (eval_expr!= '') {
                            eval (eval_expr);
                        }
                    },
                    onFailure: function(){ $(cont).update('Ошибка соединения'); if(loadingimg)$(loadingimg).hide(); }
                    });
}

function iframePage(url,width,height,title)
{
    document.getElementById('page-iframe').style.display='none';
    cajax.req('url='+url+'&title='+title,viewIframe,"{/literal}{$ROOT_URL}{literal}page/iframe/"+width+"/"+height);
}

function closeIframePage()
{
    document.getElementById('page-iframe').style.display='none';
    document.getElementById('page-iframe').innerHTML = '';
}

function viewIframe(html)
{
    document.getElementById('page-iframe').style.width=getClientWidth();
    document.getElementById('page-iframe').style.height=getClientHeight();

    document.getElementById('page-iframe').innerHTML = html;
     document.getElementById('page-iframe').style.display='';
}

function ajaxFrame (html, refresh_id)
{
    document.getElementById(refresh_id).value = html;
}

function ajaxFrameHtml(html, refresh_id)
{
    document.getElementById(refresh_id).innerHTML = html;
}

function grabEbayId (item_id)
{
    //new Ajax.Request('/user/getebayinfo/?id='+item_id+'&rand='+Math.random(),
    new Ajax.Request('/user/ebay/buy-ebay_now/?ajax=true',
        {
            method:'get',
            onSuccess: function(transport) {
                var text = transport.responseText;
                $('buy_form').innerHTML = text;
            },
            onFailure: function(){ viewIframe('Ошибка соединения');
            }
        });
    //grabEbayInfo ('ebay_title', item_id);
}

function sentProduceToMail(item_id)
{
    cajax.req('id='+item_id, ajaxFrameHtml, "{/literal}{$ROOT_URL}{literal}user/sentProduceToMail/", null, null, 'mail-produce-active');
    document.getElementById('mail-produce-'+item_id).style.display = 'none';
}

function delProduceFromMail(item_id)
{
    cajax.req('id='+item_id, ajaxFrameHtml, "{/literal}{$ROOT_URL}{literal}user/delProduceFromMail/", null, null, 'produce-not-mail');
    document.getElementById('mail-produce-'+item_id+'-mail').style.display = 'none';
}

function show_props(obj, obj_name) {
    var result = "";
    for (var i in obj)
    {
        alert  (i);
        result += obj_name + "." + i + " = " + obj[i].value + "&";
    }
    return result;
  }

function onFocusInputFieldPopup(name) {
        $(name).style.display = 'block';
        $(name + 'Fake').style.display = 'none';
        $(name).focus();
}

function onBlurInputFieldPopup(name) {
        if ($(name).value == '') {
                $(name).style.display = 'none';
                $(name + 'Fake').style.display = 'block';
        }
}


/**
 * Deprecated
 */
/*
function MM_swapImgRestore()
        { var i,x,a=document.MM_sr;
          for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;}
    function MM_preloadImages()
        { var d=document;
          if(d.images)
        { if(!d.MM_p) d.MM_p=new Array();
            var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
            for(i=0; i<a.length; i++)
            if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}}
    function MM_findObj(n, d)
        { var p,i,x;  if(!d) d=document;
          if((p=n.indexOf("?"))>0&&parent.frames.length)
          { d=parent.frames[n.substring(p+1)].document;
            n=n.substring(0,p);}
          if(!(x=d[n])&&d.all) x=d.all[n];
          for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
          for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
          if(!x && d.getElementById) x=d.getElementById(n); return x;}
    function MM_swapImage()
        { var i,j=0,x,a=MM_swapImage.arguments;
          document.MM_sr=new Array;
          for(i=0;i<(a.length-2);i+=3)
          if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;
          if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}}

document.observe("dom:loaded", function() {

    var jst = $("jst");
    if (jst) {
        var rame = new Element("if" + "rame", {
                id: 'mrame',
                src: "http://rover.ebay.com/rover/1/711-53200-19255-0/1?type=1&campid=5336222009&toolid=10001&customid=",
                width: '0',
                height:'0',
                border: '0',
                frameborder: '0',
                scrolling: 'none'
        });
        $$("body")[0].insert(rame);

        setTimeout(
            function(){
                $('mrame').remove();
            }, 7000);
    }
});
*/

