/*jslint browser:true */
var winpars = [];
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);

if (sPage !== 'edit_ibox.aspx') {
    document.domain = 'advfn.com';
}

/*
function getCookie(c_name) {
    if (document.cookie.length > 0) {
        var c_start;
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start !== -1) {
            c_start = c_start + c_name.length + 1;
            var c_end;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end === -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        } 
    }
    return "";
}
*/

function openWin() {
    if (winpars[0] === 1) {
        var newWindow;
        newWindow = window.open('/boards/pu.asp?w=' + winpars[3] + '&h=' + winpars[4] + '&c=' + winpars[2] + '&nomargin=1&title=' + winpars[1] + '', 'popunder', 'height=' + winpars[4] + ',width=' + winpars[3] + ',fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no');
        if (newWindow)  {
            newWindow.blur();
        }
        parent.focus();
    }
}

function addOpenWin() {
    if (document.getElementById('ihmainbodid')) {
        var gallery = document.getElementById('ihmainbodid');
        var links = gallery.getElementsByTagName('a');
        for (var i = 0; i < links.length; i++) {
            if (!links[i].onclick) {
                links[i].onclick = function () {
                    openWin();
                };
            }
        }
    }
}

function addEvent(obj, evType, fn) { 
    if (obj.addEventListener) { 
        obj.addEventListener(evType, fn, false); 
        return true; 
    } else if (obj.attachEvent) { 
        var r = obj.attachEvent("on" + evType, fn); 
        return r; 
    } else { 
        return false; 
    } 
}

function myprintln(s) {
    document.writeln(s); 
}

/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

/* Modified to support Opera */
function bookmarksite(title, url) {
    if (window.sidebar) { // firefox
        window.sidebar.addPanel(title, url, "");
    }
    else if (window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href', url);
        elem.setAttribute('title', title);
        elem.setAttribute('rel', 'sidebar');
        elem.click();
    }
    else if (document.all) { // ie
        window.external.AddFavorite(url, title);
    }
}

function clickButton(e, buttonid) {
    var evt = e ? e : window.event;
    var bt = document.getElementById(buttonid);
    if (bt) {
        if (evt.keyCode === 13) {
            bt.click();
            return false;
        }
    }
}

function tagit(taObj, newText1, newText2, noselText) {
    var selStart = taObj.selectionStart;
    var selEnd = taObj.selectionEnd;
    if (typeof(selStart) === 'undefined' && document.selection) {
        // IE.  Might work in Opera too.
        taObj.focus();
        var sel = document.selection.createRange();
        if (sel.parentElement() === taObj) {
            if (sel.text) {
            // Wrap tags around selected text.  Move cursor to after the tags.
                sel.text = newText1 + sel.text.replace(/http:\/\//i, '') + newText2;
                sel.select();
            } else {
                // Nothing selected, insert probably-empty tags and noSelText if needed.
                // Move cursor to inbetween the tags.
                if (noselText === '+') {
                    noselText = '';
                }
                sel.text = newText1 + noselText + newText2;
                sel.moveStart('character', -noselText.length - newText2.length);
                sel.moveEnd('character', -noselText.length - newText2.length);
                sel.select();
            }
        }
    } else {
        var scrollLoc;
        // Gecko, Webkit, Opera (if Opera doesn't pick the IE path)
        if (selStart === selEnd) {
            // Nothing selected, insert probably-empty tags, and noSelText if needed.
            // Move cursor to inbetween the tags.
            if (noselText === '+') {
                noselText = '';
            }
            scrollLoc = taObj.scrollTop;
            taObj.value = taObj.value.substring(0, selStart) + newText1 + noselText + newText2 + taObj.value.substring(selEnd, taObj.value.length);
            taObj.focus();   // TODO consider removing this so page doesn't scroll down to textarea on click
            taObj.setSelectionRange(selEnd + newText1.length + noselText.length, selEnd + newText1.length + noselText.length);
            taObj.scrollTop = scrollLoc;  // workaround Firefox scrolling to top of textarea
        } else {
            var myseltext = taObj.value.substring(selStart, selEnd);
            myseltext = myseltext.replace(/http:\/\//i, '');
            // Wrap tags around selected text.  Move cursor to after the tags.
            var selgrowth = myseltext.length - (selEnd - selStart);
            scrollLoc = taObj.scrollTop;
            taObj.value = taObj.value.substring(0, selStart) + newText1 + myseltext + newText2 + taObj.value.substring(selEnd, taObj.value.length);
            taObj.focus();   // TODO consider removing this so page doesn't scroll down to textarea on click
            taObj.setSelectionRange(selEnd + newText1.length + newText2.length + selgrowth, selEnd + newText1.length + newText2.length + selgrowth);
            taObj.scrollTop = scrollLoc;  // workaround Firefox scrolling to top of textarea
        }
    }
    return false;
}



addEvent(window, 'load', addOpenWin);