function $(id)
{
  return document.getElementById(id);
}

function BTip(s)
{
  Tip(s, BALLOON, true, ABOVE, true, OFFSETX, -17, TEXTALIGN, 'justify', PADDING, 8, DELAY, 800);
}

function ArtImgMTip(art, title, i, ext)
{
  Tip('<img src=\''+upload_dir+art+'-'+i+'-web-medium.'+ext+'\' width=240 height=180 border=0 alt=\'\'>', TITLE, title, PADDING, 0);
}

function ArtImgBTip(art, title, i, ext)
{
  Tip('<img src=\''+upload_dir+art+'-'+i+'-web-big.'+ext+'\' width=600 height=400 border=0 alt=\'\'>', TITLE, title, PADDING, 0, CLOSEBTN, true, CLICKCLOSE, true, DELAY, 0, STICKY, true, EXCLUSIVE, true, FIX, ['imgpreview_pos', 10, 10]);
}

function Div2Tip(div_id, title)
{
  if ($(div_id)) TagToTip(div_id, TITLE, title, PADDING, 0, CLOSEBTN, true, CLICKCLOSE, false, DELAY, 0, STICKY, true, EXCLUSIVE, true);
}

function ArtQtyWin(art, title, pu, unit)
{
  if (pu=='') pu = 1;
  var body = '          <table cellspacing=0 cellpadding=0 border=0 style="margin-top: 10px;">'+"\n";
  body += '            <tr>'+"\n";
  body += '              <td>'+"\n";
  body += '                <table cellspacing=1 cellpadding=0 border=0>'+"\n";
  body += '                  <tr>'+"\n";
  body += '                    <td rowspan=2><input type="text" class="int" name="qty" id="qty_'+art+'" value="'+pu+'" size=3></td>'+"\n";
  body += '                    <td valign="bottom"><a HREF="javascript:;" onclick="setQty(\'qty_'+art+'\', \'+\', '+pu+');"><img src="'+style_dir+'img/arrow-up.gif" border=0 alt="+" style="padding: 2px 2px 1px 2px;"></a></td>'+"\n";
  body += '                  </tr>'+"\n";
  body += '                  <tr>'+"\n";
  body += '                    <td valign="top"><a HREF="javascript:;" onclick="setQty(\'qty_'+art+'\', \'-\', '+pu+');"><img src="'+style_dir+'img/arrow-down.gif" border=0 alt="-" style="padding: 1px 2px 2px 2px;"></a></td>'+"\n";
  body += '                  </tr>'+"\n";
  body += '                </table>'+"\n";
  body += '              </td>'+"\n";
  body += '              <td><div style="margin: 0 4px;">'+unit+'</div></td>'+"\n";
  body += '              <td><div id="basket_'+art+'"><a HREF="javascript:;" onclick="addToCart(\''+art+'\', $(\'qty_'+art+'\').value);"><img src="'+style_dir+'img/icon_cart.gif" border=0 alt=""></a></div></td>'+"\n";
  body += '              <td><div id="msg_'+art+'"></div></td>'+"\n";
  body += '            </tr>'+"\n";
  body += '          </table>'+"\n";
  Tip(body, TITLE, title, PADDING, 0, CLOSEBTN, true, CLICKCLOSE, false, DELAY, 0, STICKY, true, EXCLUSIVE, true);
}

function OpenWindow(url, params)
{
  window.open(url, '', params? params:'width=620, height=550, scrollbars=yes');
}

function changeImage()
{
  if (document.images)
    for (var i=0; i<changeImage.arguments.length; i+=2)
      $(changeImage.arguments[i]).src = changeImage.arguments[i+1];
}

function setQty(id, dir, step)
{
  var obj = $(id);
  if (isNaN(obj.value) || obj.value<=0 || obj.value/step!=Math.floor(obj.value/step, 0)) return obj.value = (dir=='-')? 0:step;
  return obj.value = (dir=='-')? obj.value*1-step:obj.value*1+step;
}

function switchDiv(id, int)
{
  if ($(id)) $(id).style.display = ($(id).style.display=='none' || (typeof int!='undefined' && int>0))? 'block':'none';
}

function blend(id, percent)
{
  if ($(id))
  {
    $(id).style.MozOpacity=percent/100;
    if ($(id).style.filters) $(id).style.filters.alpha.opacity='100';
  }
}

function startLoading(id)
{
  if ($(id))
  {
    $(id).innerHTML = '<a HREF="javascript:sajax_cancel();"><img src="'+style_dir+'img/icon_loading.gif" border=0 alt="loading.." style="margin: 4px;"></a>';
    switchDiv(id, 1);
  }
}

function xmlVar(x, name)
{
  return (x && x.getElementsByTagName(name) && x.getElementsByTagName(name)[0] && x.getElementsByTagName(name)[0].firstChild)? x.getElementsByTagName(name)[0].firstChild.nodeValue:null;
}

function addToCart(artno, qty)
{
  switchDiv('basket_'+artno, 0);
  startLoading('msg_'+artno);
  blend('nav_cart_content', 60);
  return sajax_do_call(ajax_url+'?mode=cart&artno='+artno+'&content_id=nav_cart_content&qty='+qty, addToCart_cb, addToCart.arguments);
}

function addToCart_cb(xmldata, args)
{
  artno = args[0];
  blend('nav_cart_content', 100);
  switchDiv('msg_'+artno, 0);
  switchDiv('basket_'+artno, 1);
  message = xmlVar(xmldata, 'message');
  if (xmlVar(xmldata, 'status')>=200) tt_HideInit();
  if (message.length>0) showMessage('alert', message, xmlVar(xmldata, 'status')<200? 'error':'');
}

function send2friend(artno, email)
{
  $('box_s2f_msg').innerHTML = '';
  switchDiv('box_s2f_button', 0);
  startLoading('box_s2f_loading');
  sajax_do_call(ajax_url+'?mode=sendtofriend&artno='+artno+'&email_to='+email, send2friend_cb, send2friend.arguments);
}

function send2friend_cb(xmldata, args)
{
  var sclass = parseInt(xmlVar(xmldata, 'status'))>=200? 'ok':'error';
  $('box_s2f_msg').innerHTML = '<span class="small b '+sclass+'">'+xmlVar(xmldata, 'message')+'</span>';
  switchDiv('box_s2f_loading', 0);
  switchDiv('box_s2f_button', 1);
}

function notify(artno, status, price, email)
{
  $('box_notify_msg').innerHTML = '';
  switchDiv('box_notify_button', 0);
  startLoading('box_notify_loading');

  istatus = status? 1:0;
  iprice = price? 1:0;
  sajax_do_call(ajax_url+'?mode=notify&artno='+artno+'&status='+istatus+'&price='+iprice+'&email='+email, notify_cb, notify.arguments);
}

function notify_cb(xmldata, args)
{
  var sclass = parseInt(xmlVar(xmldata, 'status'))>=200? 'ok':'error';
  $('box_notify_msg').innerHTML = '<span class="small b '+sclass+'">'+xmlVar(xmldata, 'message')+'</span>';
  switchDiv('box_notify_loading', 0);
  switchDiv('box_notify_button', 1);
}

function showMessage(type, msg, className)
{
  if (type=='alert')
  {
    if (typeof className=='undefined') className = '';
    var smessage = {
        type: "alert",
        title: '',
        text: msg,
        yes_button: "OK",
        width: 400,
        className: className
    };
    ModalWindow.open(smessage);
  }
}


// remote scripting library
// (c) copyright 2005 modernmethod, inc
var sajax_debug_mode = false;
var sajax_request_type = "GET";
var sajax_failure_redirect = "";

function sajax_debug(text) {
        if (sajax_debug_mode)
                alert(text);
}

function sajax_init_object() {
        sajax_debug("sajax_init_object() called..")

        var A;

        var msxmlhttp = new Array(
                'Msxml2.XMLHTTP.5.0',
                'Msxml2.XMLHTTP.4.0',
                'Msxml2.XMLHTTP.3.0',
                'Msxml2.XMLHTTP',
                'Microsoft.XMLHTTP');
        for (var i = 0; i < msxmlhttp.length; i++) {
                try {
                        A = new ActiveXObject(msxmlhttp[i]);
                } catch (e) {
                        A = null;
                }
        }

        if(!A && typeof XMLHttpRequest != "undefined")
                A = new XMLHttpRequest();
        if (!A)
                sajax_debug("Could not create connection object.");
        return A;
}

var sajax_requests = new Array();

function sajax_cancel() {
        for (var i = 0; i < sajax_requests.length; i++)
                sajax_requests[i].abort();
}

function sajax_do_call(uri, callback, args) {
        var i, x, n;
        var post_data;
        var func_name = callback;

        sajax_debug("in sajax_do_call().." + sajax_request_type);
        if (typeof(sajax_request_type) == "undefined" || sajax_request_type == "")
                sajax_request_type = "GET";

        if (sajax_request_type == "GET") {
                if (uri.indexOf("?") == -1)
                        uri += "?rsrnd=" + new Date().getTime();
                else
                        uri += "&rsrnd=" + new Date().getTime();

                post_data = null;
        }
        else if (sajax_request_type == "POST") {
                post_data = "rsrnd=" + new Date().getTime();
        }
        else {
                alert("Illegal request type: " + sajax_request_type);
        }

        x = sajax_init_object();
        if (x == null) {
                if (sajax_failure_redirect != "") {
                        location.href = sajax_failure_redirect;
                        return false;
                } else {
                        sajax_debug("NULL sajax object for user agent:\n" + navigator.userAgent);
                        return false;
                }
        } else {
                x.open(sajax_request_type, uri, true);
                // window.open(uri);

                sajax_requests[sajax_requests.length] = x;

                if (sajax_request_type == "POST") {
                        x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
                        x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                }

                x.onreadystatechange = function() {
                        if (x.readyState != 4)
                                return;

                        sajax_debug("received " + x.responseText);

                        var status;
                        if (x.status == 200 && x.responseXML) {
                            if (x.responseXML.getElementsByTagName('status'))
                            {
                              status = parseInt(xmlVar(x.responseXML, 'status'));
                              msg = xmlVar(x.responseXML, 'message');
                              content = xmlVar(x.responseXML, 'content');
                              content_id = xmlVar(x.responseXML, 'content_id');

                              if (status>=200 && content!='' && $(content_id)) { $(content_id).innerHTML = content; }
                            } else { msg = 'INVALID_DATA'; }
                        }
                        else {
                                status = 0;
                                try {
                                        msg = x.statusText? x.statusText:'';
                                } catch (e) {
                                        msg = '';
                                }
                        }

                        try {
                                callback(eval(x.responseXML), eval(args));
                        } catch (e) {
                                sajax_debug("Caught error " + e + ": Could not eval " + x.responseXML + ", " + args );
                        }
                }
        }

        sajax_debug(func_name + " uri = " + uri + "/post = " + post_data);
        x.send(post_data);
        sajax_debug(func_name + " waiting..");
        delete x;
        return true;
}