var currentSelectedTag = -1;
var isEditShowing = false;
var currentEditWindowRow;

var oldDescription;
var oldAmount;
var oldBusinessExpense;
var oldGstExpense;
var oldTags;


function hide(element)
{
    element.style.display='none';
}
function show(element)
{
    element.style.display='';
}

function flashSave()
{
    return;
    new Effect.Pulsate('save-div', {duration:2.0})
}

function showRow(row)
{
    $(row + ':edit').style.display = 'inline';
    //            $(row+':row').style.background='#faf1c3';
}
function hideRow(row)
{
    $(row + ':edit').style.display = 'none';
    //            $(row+':row').style.backgroundColor=background;
    //            $(row+':row').style.borderColor=border;

}


function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft,curtop];
}

function showNewRuleDiv()
{
    new Effect.Appear($('new-rule-div'), {duration:0.2 })
}

function closeNewRuleDiv()
{
    new Effect.Fade($('new-rule-div'), {duration:0.2 })
}

function showStatus(component)
{
    var xy = findPos(component);
    $('status').style.left = (xy[0] + 20) + 'px';
    $('status').style.top = xy[1] + 'px';
    new Effect.Appear($('status'), {duration:0.2 })
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}



function positionAt(componentToPosition, componentWhoProvidesPosition, align, xOffset, yOffset)
{
    var xy = findPos(componentWhoProvidesPosition);
    if (align == 'LEFT')
    {
        componentToPosition.style.left = '250px';
    } else
    {
        componentToPosition.style.left = (xy[0] + 20 + xOffset) + 'px';
    }
    scrollXY= getScrollXY();

    componentToPosition.style.top = (xy[1]+yOffset) + 'px';
    new Effect.Appear(componentToPosition, {duration:0.2 })
}

function hideStatus()
{
    new Effect.Fade($('status'), {duration:0.1 })
}

function addTag(currentTagComponent, tag)
{
    var value = currentTagComponent.value;
    var endIndex = value.lastIndexOf(",") == -1 ? 0 : value.lastIndexOf(',') + 1;
    value = value.substring(0, endIndex);
    value += tag;
    currentTagComponent.value = value;

}

function toggleStar(obj)
{
    var src = obj.lastChild.src;
    if (src.indexOf('on') > -1)
        src = src.replace(/on/, 'off');
    else src = src.replace(/off/, 'on');
    obj.lastChild.src = src;
}


function showBox() {
    $('box').show();
    center('box');
    return false;
}

function hideBox() {
    $('box').hide();
    return false;
}


function center(element) {
    try {
        element = $(element);
    } catch(e) {
        return;
    }

    var my_width = 0;
    var my_height = 0;

    if (typeof( window.innerWidth ) == 'number') {
        my_width = window.innerWidth;
        my_height = window.innerHeight;
    } else if (document.documentElement &&
               ( document.documentElement.clientWidth ||
                 document.documentElement.clientHeight )) {
        my_width = document.documentElement.clientWidth;
        my_height = document.documentElement.clientHeight;
    }
    else if (document.body &&
             ( document.body.clientWidth || document.body.clientHeight )) {
            my_width = document.body.clientWidth;
            my_height = document.body.clientHeight;
        }

    element.style.position = 'absolute';
    element.style.zIndex = 99;

    var scrollY = 0;

    if (document.documentElement && document.documentElement.scrollTop) {
        scrollY = document.documentElement.scrollTop;
    } else if (document.body && document.body.scrollTop) {
        scrollY = document.body.scrollTop;
    } else if (window.pageYOffset) {
        scrollY = window.pageYOffset;
    } else if (window.scrollY) {
        scrollY = window.scrollY;
    }

    var elementDimensions = Element.getDimensions(element);

    var setX = ( my_width - elementDimensions.width  ) / 2;
    var setY = ( my_height - elementDimensions.height ) / 2 + scrollY;

    setX = ( setX < 0 ) ? 0 : setX;
    setY = ( setY < 0 ) ? 0 : setY;

    element.style.left = setX + "px";
    element.style.top = setY + "px";

    element.style.display = 'block';
}

function getPageSize() {
    var de = document.documentElement;
    var w = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
    var h = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight
    arrayPageSize = new Array(w, h)
    return arrayPageSize;
}