// $ = getElementById (no matter if dojo, MochiKit, or plain javascript)
function twGetElement(id){
    return ((typeof(id) == "string") ? document.getElementById(id) : id);
}
if (typeof(dojo) != 'undefined'){ $ = dojo.byId; }
else if(typeof(MochiKit) != 'undefined'){}
else{ $ = twGetElement; }

function assert(fact, details){
    if (!fact){
        var msg = 'Assert failure! ' + details;
        if (arguments.callee.caller !== null){
            msg = msg + ' in function ' +
                    arguments.callee.caller.toString();
                    // this was supposed to display function name but balks if anonymous
                    // .match(/function\s+(\w+)/)[1];
        }
        alert(msg);
    }
}

// Display a 'Loading...' message while the browser is waiting
// depends on MochiKit
var waiting = 0;
function startWaiting(){
    assert(waiting >= 0);
    if(!waiting){
        hideElement($('container'));
        showElement($('wait'));
    }
    waiting += 1;
}

function stopWaiting(){
    assert(waiting >= 0);
    waiting -= 1;
    if(!waiting){
        showElement($('container'));
        hideElement($('wait'));
    }
}

function startLoading(msg){
    msg = msg || 'The document is loading.';
    var loading = DIV({'id':'loading'},
                IMG({'src':'/media/myadmin/img/wait.gif', 'alt':''}),
                H1(null, 'Please wait...'),
                P(null, msg));
    replaceChildNodes(document.body, loading);
}

// Return index of an element in anArray, or -1
function getIndex(anArray, element){
    var found = false;
    for (var i = 0; i < anArray.length; i++) {
      if ((found = anArray[i] == element)){ break; }
    }
    return found ? i : -1;
}

// Display information about a key event
function alertKey(e){
    var code;
    if (e.keyCode) { code = e.keyCode; }
    else if (e.which) { code = e.which; }
    var character = String.fromCharCode(code);
    var keyInfo = 'Event: ' + e.type + '\n\n';
    keyInfo += 'You pressed: ' + character + ' [Decimal: ' + code + ']\n';
    keyInfo += 'ALT: ' + e.altKey + '\n';
    keyInfo += 'CTRL: ' + e.ctrlKey + '\n';
    keyInfo += 'SHIFT: ' + e.shiftKey + '\n';
    keyInfo += 'REPEAT: ' + e.repeat + '\n';
    keyInfo += 'WHICH: ' + e.which;
    alert(keyInfo);
}

/*
* This function will not return until (at least)
* the specified number of milliseconds have passed.
* It does a busy-wait loop.  Burns CPU cycles!
*/
function pause(numberMillis){
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime) { return; }
    }
}

function showElement(id){ $(id).style.display = 'block'; }
function hideElement(id){ $(id).style.display = 'none'; }
function toggleElement(id){
    if ($(id).style.display == 'none'){ showElement(id); }
    else{ hideElement(id); }
}

function openWin(url, winx, winy, addfeatures){
// scrollbars=yes to add scrollbars
    features = "status=no";
    if (winx && winy) {
        features += ", width=" + winx + ", height=" + winy;
    }
    if (addfeatures) {
        features += ", " + addfeatures;
    }
    window.open(url, 'openwin', features);
}

function unselectTab(div){
    hideElement(div);
    tab = $(div.id + '-tab');
    tab.className = 'button';
}

function selectTab(id_div){
    tabs = dojo.html.getElementsByClass('shop-header-control-tab');
    for (var i=0; i < tabs.length; i++){ unselectTab(tabs[i]); }
    if (!id_div) {
        id_div = dojo.io.cookie.get('tab');
        if (!id_div) { id_div = tabs[0].id; }
    }
    showElement(id_div);
    $(id_div + '-tab').className = 'selected button';
    dojo.io.cookie.set('tab', id_div, 2, '/');
}

// Validate input and display #wait element while posting to cart
INTEGER = /^\d+$/;
DECIMAL = /^\d+(\.\d{0,3})?$/;
function postToCart(formid, unit){
    var form = $(formid);
    if (unit == 'item' && !INTEGER.test(form.quantity.value)){
        alert('The quantity should be a whole number.');
        return false;
    }else if (unit == 'kg' && !DECIMAL.test(form.quantity.value)){
        alert('The quantity should be a number with up to three decimal places.');
        return false;
    }
    var wait = $('wait');
    if (wait){
        var pos = dojo.html.getScrollOffset();
        wait.style.top = (pos[1] + 20) + 'px';
        showElement(wait);
    }else{ alert('#wait element not found in markup'); }
    form.submit();
}

function mailto(name, domain, subject, analytics_url) {
    if (analytics_url) { urchinTracker(analytics_url); }
    window.location.href = 'mailto:' + name + '@' + domain + '?SUBJECT=' + subject;
    return false;
}

function delete_cookie(cookie_name){
  document.cookie = cookie_name += "=cleared; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/";
  return 'dummy';
}
