function trackCompanyView(cik) {
    try {
        var req = getXMLHTTP();
        req.open("GET", "http://www.form4oracle.com/tracking/company?cik=" + cik, true);
        req.send(null);
    } catch (e) {
        // Ignore...
    }
}
function trackInsiderView(cik) {
    try {
        var req = getXMLHTTP();
        req.open("GET", "http://www.form4oracle.com/tracking/insider?cik=" + cik, true);
        req.send(null);
    } catch (e) {
        // Ignore...
    }
}

/**
 * Window Management
 */
var WindowMgr = Class.create();


WindowMgr.prototype = {
    initialize: function() {
        this.cWins = new Array();
        this.idx = 0;
        this.xidx = 0;
        this.cascade = 0;
        this.rOffset = 180;
        this.lOffset = 20;
    },

    getTop: function() {
        return 120 + this.cascade * this.rOffset;
    },

    getLeft: function() {
        return 180 + this.cascade * this.lOffset;
    },

    onShow: function(eventName, win) {
        win.toFront();
        win.setDestroyOnClose();
        this.cascade++;
        this.idx++;
        this.xidx++;

        this.cWins.push(win);
    },

    onClose: function(eventName, win) {
        this.cascade--;
        this.idx--;
        this.xidx--;
        this.cWins.pop(win);
    }
}

function form4_showWindow(id, title) {
    var win = new Window({
        className: "mac_os_x",
        resizable: false,
        minimizable: false,
        maximizable: false,
        effectOptions: {duration: 0.5},
        top: (Position.cumulativeOffset)[0],
        left: (Position.cumulativeOffset)[1],
//        top:(120 + (cascade * rOffset)),
//        left:(180 + (cascade * lOffset)),
        width: 500,
        height: 0
    });
    win.setTitle(title);
    win.getContent().innerHTML = $(id).innerHTML;
    win.setDestroyOnClose();
    win.show();
    win.toFront();
    win.setCloseCallback(function() {cascade--;idx--;xidx--;cWins.pop(win); return true;});
    cascade++;
    idx++;
    xidx++;
    cWins.push(win);
}

function test(cik, fromDate, toDate) {
  var win = new Window({className: "mac_os_x", title: "Insider Transactions", top:70, left:100, width:700, height:300, url: "insider-txns-popup?cik=" + cik, showEffectOptions: {duration:1.5}});
  win.show();
}


function form4_toggleHelp(id) {
    Effect.toggle($('helpContent' + id), 'blind');
    Element.toggle('summaryLess' + id);
    Element.toggle('summaryMore' + id);
}
function form4_toggleAllSentimentRows(table) {
    if (Element.visible($('plusAll'))) {
        form4_showAllSentimentRows(table);
    } else {
        form4_hideAllSentimentRows(table);
    }
    Element.toggle($('plusAll'));
    Element.toggle($('minusAll'));
}
function form4_showAllSentimentRows(table) {
    var rows = $A(this.document.getElementsByClassName('hideable'),$(table));
    rows.each(function(row) {
        form4_showSentimentRow(row);
    })
}
function form4_hideAllSentimentRows(table) {
    var rows = $A(this.document.getElementsByClassName('hideable'),$(table));
    rows.each(function(row) {
        form4_hideSentimentRow(row);
    })
}
function form4_showSentimentRow(row) {
    Element.show(row);
    // Toggle plus/minus
    var classes = $A(Element.classNames(row));
    Element.hide('plus-' + classes[1]);
    Element.show('minus-' + classes[1]);
}
function form4_hideSentimentRow(row) {
    Element.hide(row);
    // Toggle plus/minus
    var classes = $A(Element.classNames(row));
    Element.show('plus-' + classes[1]);
    Element.hide('minus-' + classes[1]);
}
function form4_toggleSentimentRow(table, className) {
    var rows = $A(this.document.getElementsByClassName(className),$(table));
    // Toggle plus/minus of the first row
    var classes = $A(Element.classNames(rows[0]));
    Element.toggle('plus-' + classes[1]);
    Element.toggle('minus-' + classes[1]);

    rows.each(function(row) {
        Element.toggle(row);
    })
}

function form4_toggle(id) {
    Element.toggle(id);
}

function loadCompany() {
/*
    if (location.hostname != 'form4oracle.com') {
        $('transactions').style.color = '#fff';
        $('summary_by_title').style.color = '#fff';
        $('company_summary_group').style.color = '#efe';
    }
*/
    $('symbol').focus();
}

function loadInsider() {
    $('symbol').focus();
}

function playSound(url) {
    $("sound-file").innerHTML="<embed src='"+url+"' hidden=true autostart=true loop=false>";
}

function loadSearch() {
    if ($('forms_submit_id') != undefined)
        $('forms_submit_id').value = '';
    if ($('toggleState').value == '')
        $('toggleState').value = 'hide';
    form4_initToggleElements('search');
    form4_adustSearchHeight();
}

function form4_adustSearchHeight() {
    var h = Element.getDimensions('search_content').height;
    var height = h - 68;
    $('saved_searches_content').style.height = height + 'px';
    $('saved_searches_content').style.clip = "rect(0px 200px " + height + "px 0px)";
}

function form4_initToggleElements(element) {
    if ($('toggleState').value == 'show') {
        var togglables = $A(this.document.getElementsByClassName('toggleable'),$(element));
        togglables.each(function(togglable) {
            Element.toggle(togglable);
        });

        Element.hide('toggle_show');
        Element.show('toggle_hide');
    }
    form4_adustSearchHeight();
}

function form4_toggleElements(element) {
    // set state
    if ($('toggleState').value == 'hide') {
        $('toggleState').value = 'show';
    } else {
        $('toggleState').value = 'hide';
    }

    Element.toggle('toggle_hide');
    Element.toggle('toggle_show');

    var togglables = $A(this.document.getElementsByClassName('toggleable'),$(element));
    togglables.each(function(togglable) {
        Element.toggle(togglable);
    })
    form4_adustSearchHeight();

}

function form4_checkAll(form) {
    var checked = false;
    if (Element.classNames('checkAll') == 'checked') {
        checked = true;
    }
    var checkboxes = Form.getInputs(form, 'checkbox');
    checkboxes.each(function(checkbox){
        if (checked) {
            checkbox.checked = false;
            Element.removeClassName('checkAll', 'checked');
            Element.addClassName('checkAll', 'unchecked');
            $('checkAll').src = '/skins/standards/images/icon_all_check.png';
        } else {
            checkbox.checked = true;
            Element.removeClassName('checkAll', 'unchecked');
            Element.addClassName('checkAll', 'checked');
            $('checkAll').src = '/skins/standards/images/icon_all_uncheck.png';
        }
    });
}

function form4_clearForm(form) {
    form = $(form);
    for (i=0; i<form.elements.length; i++) {
        with(form.elements[i]) {
            if (type == "text" || type == "textarea" || type == "password") {
                value = "";
            } else if (type.indexOf("select") > -1) {   
                for (o=0; o<options.length; o++) {
                    if (type == "select-multiple") {
                        options[o].selected = false;
                    } else {
                        selectedIndex = 0;
                    }
                }   
            }
        }// end with
    }//end for
}

function form4_getUrlParam(name) {
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var tmpURL = window.location.href;
    var results = regex.exec( tmpURL );
    if( results == null )
        return "";
    else
        return results[1];
}

function openInOpener(location) {
    if (window.opener && !window.opener.closed) {
        window.opener.location = location;
        window.opener.focus();
     } else if (window.targetWin && window.targetWin.open && !window.targetWin.closed) {
        window.targetWin.location = location;
        window.targetWin.focus();
     } else {
         targetWin = window.open(location, "targetWin");
         targetWin.focus();
     }
}

function alertFrequencyOnChange() {
    var value = document.forms[0].frequency[document.forms[0].frequency.selectedIndex].value;
    if (value == 1) {
        document.forms[0].emailAlert.checked = false;        
        document.forms[0].smsAlert.checked = false;
    }
}

/**
 * Company Pages
 */
function showInsiderWindow(id, posElement) {
  var win = new Window({
      className: "mac_os_x",
      title: "Insider Profile",
      resizable: false,
      minimizable: false,
      maximizable: false,
      effectOptions: {duration: 0.2},
      left: Element.cumulativeScrollOffset($(posElement))[0] + 100,
      top: Element.cumulativeScrollOffset($(posElement))[1] + 100,
      width: 500,
      height: 0
  });
  win.getContent().innerHTML = $(id).innerHTML;
  win.show();
}

function showTxnWindow(baseURL, targetElement, companyId, insiderCik, fromTimeId, toTimeId, compare) {
    if (compare) {
        var span = $('insider-' + insiderCik).rowSpan;
        $('insider-' + insiderCik).rowSpan = span + 1;
    }
    var url = "/" + baseURL + "/insider-txns-popup?companyId=" + companyId +
           "&insiderCik=" + insiderCik +
           "&fromTimeId=" + fromTimeId +
           "&toTimeId=" + toTimeId +
            "&a=" + Math.random();

    new Ajax.Updater(targetElement, url);
}

function hideTxnWindow(insiderCik, compare) {
    if (compare) {
        var span = $('insider-' + insiderCik).rowSpan;
        $('insider-' + insiderCik).rowSpan = span - 1;
    }
}

function reportError(request) {
    $F('zipResult') = "Error";
}

function showHelp(id) {
    //Dialog.info($(id).innerHTML, {width:250, height:100});
    var win = new Window({
        className: "mac_os_x",
        title: "Insider Transactions",
        resizable: false,
        minimizable: false,
        maximizable: false,
        effectOptions: {duration: 0.2},
        left: Element.cumulativeScrollOffset($(id))[0] + 400,
        top: Element.cumulativeScrollOffset($(id))[1] + 300,
        width: 200,
        height: 70,
        destroyOnClose: true
    });
    win.getContent().innerHTML = $(id).innerHTML;
    win.show();
}

/**
 * Utilities
 */
function form4_unescape() {
    var elements = $A(this.document.getElementsByClassName('unescape'));
    elements.each(function(e) {
        e.innerHTML = e.innerHTML.unescapeHTML();
    })
}
