// include global.js

var GROUP_BY_NONE = 1;
var GROUP_BY_PROD = 2;
var GROUP_BY_SITE = 3;

var SORT_BY_TIME_SAVED_DESC = 1;
var SORT_BY_TIME_SAVED_ASC = 2;
var SORT_BY_PRICE_ASC = 3;
var SORT_BY_PRICE_DESC = 4;
var SORT_BY_NAME_ASC = 5;
var SORT_BY_NAME_DESC = 6;
var SORT_BY_FAV = 7;
var SORT_BY_RATING = 9;
var SORT_BY_TITLE_ASC = 10;
var SORT_BY_TITLE_DESC = 11;

var editfavs, refreshlinks, sortedlinks, sortedysprods, sortedsites;
var ifcreatelist, ifdellinks, ifxmlhttpreq;

ifcreatelist = false;
ifdellinks = false;
ifxmlhttpreq = false;
editfavs = new Object();

function sign_up_temp() {
    document.getElementById('signuptemp').name = 'signuptemp';
    document.getElementById('my').submit();
}

function create_list() {
    if (document.getElementById('createlistarea').style.display == 'none') {
        // ifcreatelist = true;

        document.getElementById('createlistarea').innerHTML =
        '<table cellpadding="0" cellspacing="0" style="position: relative">' +
        '<tr>' +
        '<td>' +
        '<table id="createlisttable" cellpadding="0" cellspacing="0">' +
        '<tr>' +
        '<td align="right" style="font-weight: bold" nowrap>Name</td>' +
        '<td width="100%" style="padding-left: 10">' +
        '<input type="text" id="listname" maxlength="80" style="width: 100%">' +
        '</td>' +
        '</tr>' +
        '<tr><td style="padding-top: 3"></td></tr>' +
        '<tr>' +
        '<td colspan="2">' +
        '<center>' +
        '<input id="createlist" type="button" value="Create" onClick="javascript: createlist_on_click();">&nbsp; ' +
        '<input id="cancelcreatelist" type="button" value="Cancel" onClick="javascript: cancelcreatelist_on_click();">' +
        '</center>' +
        '</td>' +
        '</tr>' +
        '</table>' +
        '<img id="createlistbusy" style="display: none; position: absolute; left: 82; top: 50%">' +
        '</td>' +
        '</tr>' +
        '</table>';
        document.getElementById('createlistarea').style.display = '';
    }
}

function createlist_on_click() {
    var newlistname, xmlhttpreq, timeout;
    var iftimeout;

    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    try {
        newlistname = document.getElementById('listname').value;
        if (newlistname == '') throw 'Please enter a list name.';
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('List \'' + newlistname + '\' cannot be created because ' +
                  'there was an error communicating with the server. Please ' +
                  'try again later.');

            document.getElementById('createlisttable').style.background = '#FFFFFF';
            document.getElementById('createlisttable').style.filter = 'alpha(opacity=100)';
            document.getElementById('createlisttable').style.opacity = 1;
            document.getElementById('listname').disabled = false;
            // document.getElementById('listnote').disabled = false;
            document.getElementById('createlist').disabled = false;
            document.getElementById('cancelcreatelist').disabled = false;
            document.getElementById('createlistbusy').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/createlist', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('List \'' + newlistname + '\' cannot be created ' +
                           'because there was an error communicating with ' +
                           'the server. Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = '/my?listid=' + result['listid'];

                return;
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById('createlisttable').style.background = '#FFFFFF';
            document.getElementById('createlisttable').style.filter = 'alpha(opacity=100)';
            document.getElementById('createlisttable').style.opacity = 1;
            document.getElementById('listname').disabled = false;
            // document.getElementById('listnote').disabled = false;
            document.getElementById('createlist').disabled = false;
            document.getElementById('cancelcreatelist').disabled = false;
            document.getElementById('createlistbusy').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpreq.send('listname=' + encodeURIComponent(newlistname));
                        // '&note=' +
                        // encodeURIComponent(document.getElementById('listnote').value));

        document.getElementById('createlisttable').style.background = '#FFFFFF';
        document.getElementById('createlisttable').style.filter = 'alpha(opacity=30)';
        document.getElementById('createlisttable').style.opacity = 0.3;
        document.getElementById('listname').disabled = true;
        // document.getElementById('listnote').disabled = true;
        document.getElementById('createlist').disabled = true;
        document.getElementById('cancelcreatelist').disabled = true;
        document.getElementById('createlistbusy').src = '/image/busy.gif';
        document.getElementById('createlistbusy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function cancelcreatelist_on_click() {
    document.getElementById('createlistarea').style.display = 'none';
    // document.getElementById('listname').value = '';
    // document.getElementById('listnote').value = '';

    // ifcreatelist = false;
}

function show_lists() {
    var s;

    s = '';
    for (i in lists) {
        if (listid !== undefined && i.substring(1) == listid) {
            s += '<div class="selected-item">';
        }
        else s += '<div class="item">';
        s += ('<a href="/my?listid=' + i.substring(1) + '">' +
              lists[i]['listname'] +
              '</a> ' +
              '<span class="count">(' + lists[i]["nlink"] + ')</span>' +
              '</div>');
    }

    return s;
}

function delete_list() {
    var xmlhttpreq, timeout;
    var iftimeout;

    if (confirm('Are you sure you want to delete list \'' + listname + '\' ' +
                'and the links in it?') == false) return;
    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    try {
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('List \'' + listname + '\' cannot be deleted because ' +
                  'there was an error communicating with the server. Please ' +
                  'try again later.');

            document.getElementById('busy').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/deletelist', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('List \'' + listname + '\' cannot be deleted ' +
                           'because there was an error communicating with ' +
                           'the server. Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = '/my';

                return;
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById('busy').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpreq.send('listid=' + listid);

        document.getElementById('busyarea').innerHTML =
        '<img id="busy" src="/image/busy.gif" style="position: absolute; left: 50%; top: 50%">';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function select_all_links() {
    var ysprodlinks, sitelinks;
    var i, j;

    if (ysprods !== undefined) {
        for (i in ysprods) {
            ysprodlinks = ysprods[i]['links'];
            for (j in ysprodlinks) {
                document.getElementById('sellink' + j.substring(1)).checked = true;
            }
        }
    }
    if (sites !== undefined) {
        for (i in sites) {
            sitelinks = sites[i]['links'];
            for (j in sitelinks) {
                document.getElementById('sellink' + j.substring(1)).checked = true;
            }
        }
    }
    if (links !== undefined) {
        for (i in links) {
            document.getElementById('sellink' + i.substring(1)).checked = true;
        }
    }
}

function unselect_all_links() {
    var ysprodlinks, sitelinks;
    var i, j;

    if (ysprods !== undefined) {
        for (i in ysprods) {
            ysprodlinks = ysprods[i]['links'];
            for (j in ysprodlinks) {
                document.getElementById('sellink' + j.substring(1)).checked = false;
            }
        }
    }
    if (sites !== undefined) {
        for (i in sites) {
            sitelinks = sites[i]['links'];
            for (j in sitelinks) {
                document.getElementById('sellink' + j.substring(1)).checked = false;
            }
        }
    }
    if (links !== undefined) {
        for (i in links) {
            document.getElementById('sellink' + i.substring(1)).checked = false;
        }
    }
}

function copy_links() {
    var s;

    if (document.getElementById('copymovedellinksarea').style.display == 'none') {
        if (get_if_selected_links() == false) {
            alert('Please select the links to be copied first.');

            return;
        }

        s = '<br>' +
            '<table width="100%" cellpadding="0" cellspacing="0" style="position: relative">' +
            '<tr>' +
            '<td>' +
            '<center>' +
            '<table id="copylinkstable" cellpadding="0" cellspacing="0">' +
            '<tr>' +
            '<td>' +
            'Select the lists you want to copy to and add them to the right:' +
            '<div style="margin-top: 3"></div>' +
            '</td>' +
            '</tr>' +
            '<tr>' +
            '<td>' +
            '<center>' +
            '<table cellpadding="0" cellspacing="0">' +
            '<tr valign="middle">' +
            '<td>' +
            '<select id="unsellistids" size="4" style="width: 200; color: #808080" onChange="javascript: unsellistids_on_change();">';
        for (i in lists) {
            if (listid === undefined || i.substring(1) != listid) {
                s += ('<option value="' + i.substring(1) + '">' + lists[i]['listname'] + '</option>');
            }
        }
        s += ('</select>' +
              '</td>' +
              '<td style="padding-left: 10">' +
              '<input id="addlist" type="button" value="Add &#x25BA;" style="width: 80" disabled onClick="javascript: addlist_on_click();">' +
              '<div style="margin-top: 5"></div>' +
              '<input id="removelist" type="button" value="&#x25C4; Remove" style="width: 80" disabled onClick="javascript: removelist_on_click();">' +
              '</td>' +
              '<td style="padding-left: 10">' +
              '<select id="sellistids" size="4" style="width: 200; color: #000000" onChange="javascript: sellistids_on_change();">' +
              '</select>' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '<br>' +
              '<input id="copylinks" type="button" value="Copy" onClick="javascript: copylinks_on_click();">&nbsp; ' +
              '<input id="cancelcopylinks" type="button" value="Cancel" onClick="javascript: cancelcopylinks_on_click();">' +
              '</center>' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '</center>' +
              '<img id="copylinksbusy" style="display: none; position: absolute; left: 50%; top: 50%">' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '<br>');
        document.getElementById('copymovedellinksarea').innerHTML = s;
        document.getElementById('copymovedellinksarea').style.display = '';
    }
}

function copylinks_on_click() {
    var sellinks, xmlhttpreq, timeout;
    var iftimeout;
    var i, s;

    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    try {
        sellinks = get_selected_links();
        if (sellinks.length <= 0) {
            throw 'Please select the links to be copied first.';
        }
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('The selected links cannot be copied because there was an' +
                  'error communicating with the server. Please try again ' +
                  'later.');

            document.getElementById('copymovedellinksarea').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/copylinks', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('The selected links cannot be copied because ' +
                           'there was an error communicating with the ' +
                           'server. Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = url + '&sort=' + sort_top.options[sort_top.selectedIndex].value;
                // location.reload();

                return;
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById('copymovedellinksarea').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        s = '';
        for (n = document.getElementById('sellistids').options.length, i = 0; i < n; i++) {
            if (s == '') {
                s = 'listids[]=' + document.getElementById('sellistids').options[i].value;
            }
            else {
                s += ('&listids[]=' + document.getElementById('sellistids').options[i].value);
            }
        }
        for (i in sellinks) {
            if (s == '') s = 'linkids[]=' + sellinks[i];
            else s += ('&linkids[]=' + sellinks[i]);
        }
        xmlhttpreq.send(s);

        document.getElementById('copylinkstable').style.background = '#FFFFFF';
        document.getElementById('copylinkstable').style.filter = 'alpha(opacity=30)';
        document.getElementById('copylinkstable').style.opacity = 0.3;
        document.getElementById('sellistids').disabled = true;
        document.getElementById('unsellistids').disabled = true;
        document.getElementById('addlist').disabled = true;
        document.getElementById('removelist').disabled = true;
        document.getElementById('copylinks').disabled = true;
        document.getElementById('cancelcopylinks').disabled = true;
        document.getElementById('copylinksbusy').src = '/image/busy.gif';
        document.getElementById('copylinksbusy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function cancelcopylinks_on_click() {
    document.getElementById('copymovedellinksarea').style.display = 'none';
}

function move_links() {
    var s;

    if (document.getElementById('copymovedellinksarea').style.display == 'none') {
        if (get_if_selected_links() == false) {
            alert('Please select the links to be moved first.');

            return;
        }

        s = '<br>' +
            '<table width="100%" cellpadding="0" cellspacing="0" style="position: relative">' +
            '<tr>' +
            '<td>' +
            '<center>' +
            '<table id="movelinkstable" cellpadding="0" cellspacing="0">' +
            '<tr>' +
            '<td>' +
            'Select the lists you want to move to and add them to the right:' +
            '<div style="margin-top: 3"></div>' +
            '</td>' +
            '</tr>' +
            '<tr>' +
            '<td>' +
            '<center>' +
            '<table cellpadding="0" cellspacing="0">' +
            '<tr valign="middle">' +
            '<td>' +
            '<select id="unsellistids" size="4" style="width: 200; color: #808080" onChange="javascript: unsellistids_on_change();">';
        for (i in lists) {
            if (listid === undefined || i.substring(1) != listid) {
                s += ('<option value="' + i.substring(1) + '">' + lists[i]['listname'] + '</option>');
            }
        }
        s += ('</select>' +
              '</td>' +
              '<td style="padding-left: 10">' +
              '<input id="addlist" type="button" value="Add &#x25BA;" style="width: 80" disabled onClick="javascript: addlist_on_click();">' +
              '<div style="margin-top: 5"></div>' +
              '<input id="removelist" type="button" value="&#x25C4; Remove" style="width: 80" disabled onClick="javascript: removelist_on_click();">' +
              '</td>' +
              '<td style="padding-left: 10">' +
              '<select id="sellistids" size="4" style="width: 200; color: #000000" onChange="javascript: sellistids_on_change();">' +
              '</select>' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '<br>' +
              '<input id="movelinks" type="button" value="Move" onClick="javascript: movelinks_on_click();">&nbsp; ' +
              '<input id="cancelmovelinks" type="button" value="Cancel" onClick="javascript: cancelmovelinks_on_click();">' +
              '</center>' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '</center>' +
              '<img id="movelinksbusy" style="display: none; position: absolute; left: 50%; top: 50%">' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '<br>');
        document.getElementById('copymovedellinksarea').innerHTML = s;
        document.getElementById('copymovedellinksarea').style.display = '';
    }
}

function movelinks_on_click() {
    var sellinks, xmlhttpreq, timeout;
    var iftimeout;
    var i, s;

    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    try {
        sellinks = get_selected_links();
        if (sellinks.length <= 0) {
            throw 'Please select the links to be moved first.';
        }
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('The selected links cannot be moved because there was an' +
                  'error communicating with the server. Please try again ' +
                  'later.');

            document.getElementById('copymovedellinksarea').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/movelinks', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('The selected links cannot be moved because there ' +
                           'was an error communicating with the server. ' +
                           'Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = url + '&sort=' + sort_top.options[sort_top.selectedIndex].value;
                // location.reload();

                return;
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById('copymovedellinksarea').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        if (listid !== undefined) s = 'listid=' + listid;
        else s = '';
        for (n = document.getElementById('sellistids').options.length, i = 0; i < n; i++) {
            if (s == '') {
                s = 'listids[]=' + document.getElementById('sellistids').options[i].value;
            }
            else {
                s += ('&listids[]=' + document.getElementById('sellistids').options[i].value);
            }
        }
        for (i in sellinks) {
            if (s == '') s = 'linkids[]=' + sellinks[i];
            else s += ('&linkids[]=' + sellinks[i]);
        }
        xmlhttpreq.send(s);

        document.getElementById('movelinkstable').style.background = '#FFFFFF';
        document.getElementById('movelinkstable').style.filter = 'alpha(opacity=30)';
        document.getElementById('movelinkstable').style.opacity = 0.3;
        document.getElementById('sellistids').disabled = true;
        document.getElementById('unsellistids').disabled = true;
        document.getElementById('addlist').disabled = true;
        document.getElementById('removelist').disabled = true;
        document.getElementById('movelinks').disabled = true;
        document.getElementById('cancelmovelinks').disabled = true;
        document.getElementById('movelinksbusy').src = '/image/busy.gif';
        document.getElementById('movelinksbusy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function cancelmovelinks_on_click() {
    document.getElementById('copymovedellinksarea').style.display = 'none';
}

function delete_links() {
    var s;

    if (document.getElementById('copymovedellinksarea').style.display == 'none') {
        // ifdellinks = true;

        if (get_if_selected_links() == false) {
            alert('Please select the links to be deleted first.');

            // ifdellinks = false;

            return;
        }

        s = '<br>' +
            '<table width="100%" cellpadding="0" cellspacing="0" style="position: relative">' +
            '<tr>' +
            '<td>' +
            '<center>' +
            '<table id="dellinkstable" cellpadding="0" cellspacing="0">' +
            '<tr>' +
            '<td>' +
            '<center>' +
            'Are you sure you want to delete the selected links?' +
            '<br><br>';
        if (listid === undefined) {
            s += '<input id="dellinks" type="button" value="Delete" onClick="javascript: dellinks_on_click(false);">&nbsp; ';
        }
        else {
            s += ('<input id="dellinkslist" type="button" value="Delete from This List" onClick="javascript: dellinks_on_click(true);">&nbsp; ' +
                  '<input id="dellinksall" type="button" value="Delete from All My Shopping Notes" onClick="javascript: dellinks_on_click(false);">&nbsp; ');
        }
        s += ('<input id="canceldellinks" type="button" value="Cancel" onClick="javascript: canceldellinks_on_click();">' +
              '</center>' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '</center>' +
              '<img id="dellinksbusy" style="display: none; position: absolute; left: 50%; top: 50%">' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '<br>');
        document.getElementById('copymovedellinksarea').innerHTML = s;
        document.getElementById('copymovedellinksarea').style.display = '';
    }
}

function dellinks_on_click(ifdelfromlist) {
    var sellinks, xmlhttpreq, timeout;
    var iftimeout;
    var i, s;

    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    try {
        sellinks = get_selected_links();
        if (sellinks.length <= 0) {
            throw 'Please select the links to be deleted first.';
        }
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('The selected links cannot be deleted because there was an' +
                  'error communicating with the server. Please try again ' +
                  'later.');

            document.getElementById('copymovedellinksarea').style.display = 'none';
            // ifdellinks = false;

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/deletelinks', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('The selected links cannot be deleted because ' +
                           'there was an error communicating with the ' +
                           'server. Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = url + '&sort=' + sort_top.options[sort_top.selectedIndex].value;
                // location.reload();

                return;
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById('copymovedellinksarea').style.display = 'none';
            // ifdellinks = false;

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        if (ifdelfromlist == true) s = 'listid=' + listid;
        else s = '';
        for (i in sellinks) {
            if (s == '') s = 'linkids[]=' + sellinks[i];
            else s += ('&linkids[]=' + sellinks[i]);
        }
        xmlhttpreq.send(s);

        document.getElementById('dellinkstable').style.background = '#FFFFFF';
        document.getElementById('dellinkstable').style.filter = 'alpha(opacity=30)';
        document.getElementById('dellinkstable').style.opacity = 0.3;
        if (listid === undefined) {
            document.getElementById('dellinks').disabled = true;
        }
        else {
            document.getElementById('dellinkslist').disabled = true;
            document.getElementById('dellinksall').disabled = true;
        }
        document.getElementById('canceldellinks').disabled = true;
        document.getElementById('dellinksbusy').src = '/image/busy.gif';
        document.getElementById('dellinksbusy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function canceldellinks_on_click() {
    document.getElementById('copymovedellinksarea').style.display = 'none';

    // ifdellinks = false;
}

function refresh_links() {
    var sellinks, xmlhttpreq, timeout, linkid, refreshtime;
    var iftimeout;
    var i, s, t;

    if (get_if_selected_links() == false) {
        alert('Please select the links to be refreshed first.');

        return;
    }
    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    try {
        sellinks = get_selected_links();
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(refresh_links_timeout, AJAX_TIMEOUT);
        xmlhttpreq.open('POST', '/ajax/refreshlinks', true);
        xmlhttpreq.onreadystatechange = refresh_links_cont;
        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        s = '';
        for (i in sellinks) {
            if (s == '') s = 'linkids[]=' + sellinks[i];
            else s += ('&linkids[]=' + sellinks[i]);
        }
        xmlhttpreq.send(s);

        for (i in sellinks) {
            linkid = sellinks[i];
            t = 'link' + linkid;

            if (document.getElementById('editlinkalert' + linkid + 'area').style.display != 'none') {
                document.getElementById('editlinkalert' + linkid + 'area').style.display = 'none';
            }
            document.getElementById('sellink' + linkid).disabled = true;
            document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
            document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=30)';
            document.getElementById('sellink' + linkid).style.opacity = 0.3;
            if (document.getElementById(t + 'col2area') !== null) {
                document.getElementById(t + 'col2area').style.background = '#FFFFFF';
                document.getElementById(t + 'col2area').style.filter = 'alpha(opacity=30)';
                document.getElementById(t + 'col2area').style.opacity = 0.3;
            }
            document.getElementById(t + 'col3area').style.background = '#FFFFFF';
            document.getElementById(t + 'col3area').style.filter = 'alpha(opacity=30)';
            document.getElementById(t + 'col3area').style.opacity = 0.3;
            document.getElementById(t + 'col4area').style.background = '#FFFFFF';
            document.getElementById(t + 'col4area').style.filter = 'alpha(opacity=30)';
            document.getElementById(t + 'col4area').style.opacity = 0.3;
            document.getElementById(t + 'busy').src = '/image/busy.gif';
            document.getElementById(t + 'busy').style.display = '';
        }
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }

    function refresh_links_timeout() {
        var linkid;
        var i, t;

        iftimeout = true;

        xmlhttpreq.abort();

        alert('The selected links cannot be refreshed because some links ' +
              'cannot be reached or there was an error communicating with ' +
              'the server. Please try again later.');

        for (i in sellinks) {
            linkid = sellinks[i];
            t = 'link' + linkid;

            document.getElementById('sellink' + linkid).disabled = false;
            document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
            document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=100)';
            document.getElementById('sellink' + linkid).style.opacity = 1;
            if (document.getElementById(t + 'col2area') !== null) {
                document.getElementById(t + 'col2area').style.background = '#FFFFFF';
                document.getElementById(t + 'col2area').style.filter = 'alpha(opacity=100)';
                document.getElementById(t + 'col2area').style.opacity = 1;
            }
            document.getElementById(t + 'col3area').style.background = '#FFFFFF';
            document.getElementById(t + 'col3area').style.filter = 'alpha(opacity=100)';
            document.getElementById(t + 'col3area').style.opacity = 1;
            document.getElementById(t + 'col4area').style.background = '#FFFFFF';
            document.getElementById(t + 'col4area').style.filter = 'alpha(opacity=100)';
            document.getElementById(t + 'col4area').style.opacity = 1;
            document.getElementById(t + 'busy').style.display = 'none';
        }

        ifxmlhttpreq = false;
    }

    function refresh_links_cont() {
        var result, linkid, link, newlink;
        var i, t;

        if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

        clearTimeout(timeout);
        try {
            if (xmlhttpreq.status != 200) {
                throw ('The selected links cannot be refreshed because ' +
                       'there was an error communicating with the server. ' +
                       'Please try again later.');
            }
            eval('result = ' + xmlhttpreq.responseText + ';');
            if (result['errormsg'] !== null) throw result['errormsg'];
            // if (result['refreshtime'] !== null) refreshtime = result['refreshtime'];
            now = result['now'];
            for (i in result['links']) {
                linkid = i.substring(1);
                link = find_link(linkid);
                newlink = result['links'][i];
                t = 'link' + linkid;

                link['errormsg'] = newlink['errormsg'];
                if (newlink['errormsg'] === null) {
                    link['title'] = newlink['title'];
                    link['lastrefreshtime'] = now;
                    if (newlink['htmlpriceavailabilitytext'] !== link['htmlpriceavailabilitytext']) {
                        link['oldhtmlpriceavailabilitytext'] = link['htmlpriceavailabilitytext'];
                        link['htmlpriceavailabilitytext'] = newlink['htmlpriceavailabilitytext'];
                        if (group == GROUP_BY_NONE) {
                            link['oldcurrency'] = link['currency'];
                            link['currency'] = newlink['currency'];
                            link['oldprice'] = link['price'];
                            link['price'] = newlink['price'];
                            link['oldavailability'] = link['availability'];
                            link['availability'] = newlink['availability'];
                        }
                    }
                }

                document.getElementById(t + 'title').innerHTML = link['title'];
                document.getElementById(t + 'col3area').innerHTML = show_link_col_3(linkid, link);
                document.getElementById(t + 'priceavailabilityalertarea').innerHTML = show_price_availability_alert(linkid, link);
                document.getElementById('sellink' + linkid).disabled = false;
                document.getElementById('sellink' + linkid).checked = false;
                document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
                document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=100)';
                document.getElementById('sellink' + linkid).style.opacity = 1;
                if (document.getElementById(t + 'col2area') !== null) {
                    document.getElementById(t + 'col2area').style.background = '#FFFFFF';
                    document.getElementById(t + 'col2area').style.filter = 'alpha(opacity=100)';
                    document.getElementById(t + 'col2area').style.opacity = 1;
                }
                document.getElementById(t + 'col3area').style.background = '#FFFFFF';
                document.getElementById(t + 'col3area').style.filter = 'alpha(opacity=100)';
                document.getElementById(t + 'col3area').style.opacity = 1;
                document.getElementById(t + 'col4area').style.background = '#FFFFFF';
                document.getElementById(t + 'col4area').style.filter = 'alpha(opacity=100)';
                document.getElementById(t + 'col4area').style.opacity = 1;
                document.getElementById(t + 'busy').style.display = 'none';
            }
            if (result['reqid'] === null) {
                sort_data();
                document.getElementById('dataarea').innerHTML = show_data();
            }
            else {
                timeout = setTimeout(refresh_links_timeout, AJAX_TIMEOUT);
                xmlhttpreq.open('POST', '/ajax/refreshlinks', true);
                xmlhttpreq.onreadystatechange = refresh_links_cont;
                xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                xmlhttpreq.send('reqid=' + result['reqid']);
            }
        }
        catch (ex) {
            if (ex == ('You have exceeded the maximum number of refreshes ' +
                       'you are allowed to set.')) {
                if (confirm('You have exceeded the maximum number of ' +
                            'refreshes you are allowed to set. Would you like ' +
                            'to learn how to increase this limit?') == true) {
                    location = '/help';
                }
            }
            else alert(ex);

            for (i in sellinks) {
                linkid = sellinks[i];
                t = 'link' + linkid;

                document.getElementById('sellink' + linkid).disabled = false;
                document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
                document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=100)';
                document.getElementById('sellink' + linkid).style.opacity = 1;
                if (document.getElementById(t + 'col2area') !== null) {
                    document.getElementById(t + 'col2area').style.background = '#FFFFFF';
                    document.getElementById(t + 'col2area').style.filter = 'alpha(opacity=100)';
                    document.getElementById(t + 'col2area').style.opacity = 1;
                }
                document.getElementById(t + 'col3area').style.background = '#FFFFFF';
                document.getElementById(t + 'col3area').style.filter = 'alpha(opacity=100)';
                document.getElementById(t + 'col3area').style.opacity = 1;
                document.getElementById(t + 'col4area').style.background = '#FFFFFF';
                document.getElementById(t + 'col4area').style.filter = 'alpha(opacity=100)';
                document.getElementById(t + 'col4area').style.opacity = 1;
                document.getElementById(t + 'busy').style.display = 'none';
            }
        }

        ifxmlhttpreq = false;
    }
}

function sellistids_on_change() {
    if (document.getElementById('sellistids').selectedIndex == -1) {
        document.getElementById('removelist').disabled = true;
    }
    else document.getElementById('removelist').disabled = false;
}

function unsellistids_on_change() {
    if (document.getElementById('unsellistids').selectedIndex == -1) {
        document.getElementById('addlist').disabled = true;
    }
    else document.getElementById('addlist').disabled = false;
}

function addlist_on_click() {
    var sellistids = document.getElementById('sellistids');
    var unsellistids = document.getElementById('unsellistids');
    var option;
    var s, n, i, j;

    i = unsellistids.selectedIndex;
    if (i != -1) {
        s = unsellistids.options[i].text;
        option = document.createElement('option');
        option.value = unsellistids.options[i].value;
        option.text = s;
        s = s.toLowerCase();
        for (n = sellistids.options.length, j = 0; j < n; j++) {
            if (s <= sellistids.options[j].text.toLowerCase()) break;
        }
        try {
            sellistids.add(option, j);
        }
        catch (ex) {
            sellistids.add(option, sellistids.options[j]);
        }
        unsellistids.remove(i);
        document.getElementById('addlist').disabled = true;
    }
}

function removelist_on_click() {
    var sellistids = document.getElementById('sellistids');
    var unsellistids = document.getElementById('unsellistids');
    var option;
    var s, n, i, j;

    i = sellistids.selectedIndex;
    if (i != -1) {
        s = sellistids.options[i].text;
        option = document.createElement('option');
        option.value = sellistids.options[i].value;
        option.text = s;
        s = s.toLowerCase();
        for (n = unsellistids.options.length, j = 0; j < n; j++) {
            if (s <= unsellistids.options[j].text.toLowerCase()) break;
        }
        try {
            unsellistids.add(option, j);
        }
        catch (ex) {
            unsellistids.add(option, unsellistids.options[j]);
        }
        sellistids.remove(i);
        document.getElementById('removelist').disabled = true;
    }
}

// selection-sort
function sort_data() {
    var link, linkid, ysprod, yscatalogid, site, siteid, savetime;
    var currency, price, fav, rating, lcprodname, lcsitename;
    var n, i;

    if (group == GROUP_BY_NONE) {
        if (links !== undefined) {
            sortedlinks = new Array();
            n = 0;
            for (i in links) {
                links[i]['ifsorted'] = false;

                n++;
            }
            if (sort == SORT_BY_TIME_SAVED_DESC) {
                for (; n > 0; n--) {
                    savetime = null;
                    for (i in links) {
                        link = links[i];
                        if (link['ifsorted'] == false &&
                            (savetime === null ||
                             link['savetime'] < savetime)) {
                            linkid = i;
                            savetime = link['savetime'];
                        }
                    }
                    sortedlinks.unshift(linkid);
                    links[linkid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_TIME_SAVED_ASC) {
                for (; n > 0; n--) {
                    savetime = null;
                    for (i in links) {
                        link = links[i];
                        if (link['ifsorted'] == false &&
                            (savetime === null ||
                             link['savetime'] > savetime)) {
                            linkid = i;
                            savetime = link['savetime'];
                        }
                    }
                    sortedlinks.unshift(linkid);
                    links[linkid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_PRICE_ASC) {
                for (i in links) {
                    if (links[i]['price'] === null) {
                        sortedlinks.push(i);
                        links[i]['ifsorted'] = true;

                        n--;
                    }
                }
                for (; n > 0; n--) {
                    currency = null;
                    price = null;
                    for (i in links) {
                        link = links[i];
                        if (link['ifsorted'] == false &&
                            (price === null ||
                             (link['currency'] !== null && // link['price'] == 0
                              (currencies[link['currency']] > currencies[currency] ||
                               (currencies[link['currency']] == currencies[currency] &&
                                link['price'] > price))))) {
                            linkid = i;
                            currency = link['currency'];
                            price = link['price'];
                        }
                    }
                    sortedlinks.unshift(linkid);
                    links[linkid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_PRICE_DESC) {
                for (i in links) {
                    if (links[i]['price'] === null) {
                        sortedlinks.push(i);
                        links[i]['ifsorted'] = true;

                        n--;
                    }
                }
                for (; n > 0; n--) {
                    currency = null;
                    price = null;
                    for (i in links) {
                        link = links[i];
                        if (link['ifsorted'] == false &&
                            (price === null ||
                             (link['currency'] !== null && // link['price'] == 0
                              (currencies[link['currency']] > currencies[currency] ||
                               (currencies[link['currency']] == currencies[currency] &&
                                link['price'] < price))))) {
                            linkid = i;
                            currency = link['currency'];
                            price = link['price'];
                        }
                    }
                    sortedlinks.unshift(linkid);
                    links[linkid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_TITLE_ASC) {
                for (; n > 0; n--) {
                    var lctitle = null;
                    for (i in links) {
                        var link = links[i];
                        var _lctitle = link['title'].toLowerCase();
                        var notsorted = (link['ifsorted'] == false);
                        var nolctitle = (lctitle === null);

                        if (notsorted && (nolctitle || _lctitle > lctitle)) {
                            linkid = i;
                            lctitle = _lctitle;
                        }
                    }

                    sortedlinks.unshift(linkid);
                    links[linkid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_TITLE_DESC) {
                for (; n > 0; n--) {
                    var lctitle = null;
                    for (i in links) {
                        var link = links[i];
                        var _lctitle = link['title'].toLowerCase();
                        var notsorted = (link['ifsorted'] == false);
                        var nolctitle = (lctitle === null);

                        if (notsorted && (nolctitle || _lctitle < lctitle)) {
                            linkid = i;
                            lctitle = _lctitle;
                        }
                    }

                    sortedlinks.unshift(linkid);
                    links[linkid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_FAV) {
                for (; n > 0; n--) {
                    fav = null;
                    for (i in links) {
                        link = links[i];
                        if (link['ifsorted'] == false &&
                            (fav === null || link['fav'] < fav)) {
                            linkid = i;
                            fav = link['fav'];
                        }
                    }
                    sortedlinks.unshift(linkid);
                    links[linkid]['ifsorted'] = true;
                }
            }
        }
    }
    else if (group == GROUP_BY_PROD) {
        if (links !== undefined) {
            sortedlinks = new Array();
            for (i in links) sortedlinks.push(i);
        }
        if (ysprods !== undefined) {
            sortedysprods = new Array();
            n = 0;
            for (i in ysprods) {
                ysprods[i]['ifsorted'] = false;

                n++;
            }
            if (sort == SORT_BY_NAME_ASC) {
                for (; n > 0; n--) {
                    lcprodname = null;
                    for (i in ysprods) {
                        ysprod = ysprods[i];
                        if (ysprod['ifsorted'] == false &&
                            (lcprodname === null ||
                             ysprod['prodname'].toLowerCase() > lcprodname)) {
                            yscatalogid = i;
                            lcprodname = ysprod['prodname'].toLowerCase();
                        }
                    }
                    sortedysprods.unshift(yscatalogid);
                    ysprods[yscatalogid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_NAME_DESC) {
                for (; n > 0; n--) {
                    lcprodname = null;
                    for (i in ysprods) {
                        ysprod = ysprods[i];
                        if (ysprod['ifsorted'] == false &&
                            (lcprodname === null ||
                             ysprod['prodname'].toLowerCase() < lcprodname)) {
                            yscatalogid = i;
                            lcprodname = ysprod['prodname'].toLowerCase();
                        }
                    }
                    sortedysprods.unshift(yscatalogid);
                    ysprods[yscatalogid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_FAV) {
                for (; n > 0; n--) {
                    fav = null;
                    for (i in ysprods) {
                        ysprod = ysprods[i];
                        if (ysprod['ifsorted'] == false &&
                            (fav === null || ysprod['fav'] < fav)) {
                            yscatalogid = i;
                            fav = ysprod['fav'];
                        }
                    }
                    sortedysprods.unshift(yscatalogid);
                    ysprods[yscatalogid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_RATING) {
                for (; n > 0; n--) {
                    rating = null;
                    for (i in ysprods) {
                        ysprod = ysprods[i];
                        if (ysprod['ifsorted'] == false &&
                            (rating === null || ysprod['rating'] < rating)) {
                            yscatalogid = i;
                            rating = ysprod['rating'];
                        }
                    }
                    sortedysprods.unshift(yscatalogid);
                    ysprods[yscatalogid]['ifsorted'] = true;
                }
            }
        }
    }
    else if (group == GROUP_BY_SITE) {
        if (links !== undefined) {
            sortedlinks = new Array();
            for (i in links) sortedlinks.push(i);
        }
        if (sites !== undefined) {
            sortedsites = new Array();
            n = 0;
            for (i in sites) {
                sites[i]['ifsorted'] = false;

                n++;
            }
            if (sort == SORT_BY_NAME_ASC) {
                for (; n > 0; n--) {
                    lcsitename = null;
                    for (i in sites) {
                        site = sites[i];
                        if (site['ifsorted'] == false &&
                            (lcsitename === null ||
                             site['sitename'].toLowerCase() > lcsitename)) {
                            siteid = i;
                            lcsitename = site['sitename'].toLowerCase();
                        }
                    }
                    sortedsites.unshift(siteid);
                    sites[siteid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_NAME_DESC) {
                for (; n > 0; n--) {
                    lcsitename = null;
                    for (i in sites) {
                        site = sites[i];
                        if (site['ifsorted'] == false &&
                            (lcsitename === null ||
                             site['sitename'].toLowerCase() < lcsitename)) {
                            siteid = i;
                            lcsitename = site['sitename'].toLowerCase();
                        }
                    }
                    sortedsites.unshift(siteid);
                    sites[siteid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_FAV) {
                for (; n > 0; n--) {
                    fav = null;
                    for (i in sites) {
                        site = sites[i];
                        if (site['ifsorted'] == false &&
                            (fav === null || site['fav'] < fav)) {
                            siteid = i;
                            fav = site['fav'];
                        }
                    }
                    sortedsites.unshift(siteid);
                    sites[siteid]['ifsorted'] = true;
                }
            }
            else if (sort == SORT_BY_RATING) {
                for (; n > 0; n--) {
                    rating = null;
                    for (i in sites) {
                        site = sites[i];
                        if (site['ifsorted'] == false &&
                            (rating === null || site['rating'] < rating)) {
                            siteid = i;
                            rating = site['rating'];
                        }
                    }
                    sortedsites.unshift(siteid);
                    sites[siteid]['ifsorted'] = true;
                }
            }
        }
    }
}

function show_data() {
    var link, linkid, ysprod, yscatalogid, ysprodrating, ysprodfav, ysprodlinks;
    var site, siteid, ysmerchantrating, sitefav, sitelinks;
    var iffirst;
    var i, s, j;

    iffirst = true;

    s = '';
    /*
    if (ysprods !== undefined) {
        s += ('<table width="100%" border="0" cellpadding="0" cellspacing="0">' +
              '<tr>' +
              '<td></td>' +
              '<td width="80">' +
              '<img src="/image/1x1.gif" width="80" height="1">' +
              '</td>' +
              '<td width="100%"></td>' +
              '</tr>');
        for (i in sortedysprods) {
            yscatalogid = sortedysprods[i];
            ysprod = ysprods[yscatalogid];
            yscatalogid = yscatalogid.substring(1);
            if (iffirst == true) iffirst = false;
            else s += '<tr><td>&nbsp;</td></tr>';
            s += ('<tr valign="top">' +
                  '<td style="padding-right: 5">' +
                  '<a href="javascript: expandcollapseysprod_on_click(' + yscatalogid + ');">' +
                  '<img id="expandcollapseysprod' + yscatalogid + '" ');
            if (ysprod['ifexpanded'] == true) s += 'src="/image/minus.gif" ';
            else s += 'src="/image/plus.gif" ';
            s += ('border="0">' +
                  '</a>' +
                  '</td>' +
                  '<td>');
            if (ysprod['imageurl'] !== null) {
                if (ysprod['url'] !== null) {
                    s += ('<a href="' + ysprod['url'] + '">');
                }
                s += ('<img src="' + ysprod['imageurl'] + '" border="0">');
                if (ysprod['url'] !== null) s += '</a>';
            }
            s += ('</td>' +
                  '<td style="padding-left: 10">' +
                  '<span class="product">');
            if (ysprod['url'] !== null) {
                s += ('<a href="' + ysprod['url'] + '">');
            }
            s += ysprod['prodname'];
            if (ysprod['url'] !== null) {
                s += ('</a>' +
                      '</span>' +
                      ' <a href="' + ysprod['url'] + '" target="_blank" title="Open link in new window">' +
                      '<img src="/image/nw2prod.gif" border="0">' +
                      '</a>');
            }
            else s += '</span>';
            if (ysprod['rating'] !== null) {
                ysprodrating = Math.round(ysprod['rating'] * 2) / 2;
                s += ' <a';
                if (ysprod['ratingurl'] !== null) {
                    s += (' href="' + ysprod['ratingurl'] + '"');
                }
                s += (' title="Yahoo! Shopping Product Rating: ' + ysprodrating + ' stars">' +
                      '<img src="/image/pr_' + ysprodrating + '.gif" border="0">' +
                      '</a>');
            }
            s += (' <span class="count">(' + ysprod['nlink'] + ')</span>');
            ysprodfav = ysprod['fav'];
            if (ysprodfav > 0) {
                s += (' <span style="white-space: nowrap">' +
                      '<img src="/image/heart.jpg">');
                for (i = 1; i < ysprodfav; i++) {
                    s += '<img src="/image/heart.jpg" style="margin-left: 2">';
                }
                s += '</span>';
            }
            s += (' <span class="command">' +
                  ' -' +
                  ' <a href="javascript: editysprod_on_click(' + yscatalogid + ');" title="Click to mark as favorite or edit note or price">' +
                  'Edit' +
                  '</a>' +
                  '</span>');
            if (ysprod['attrtext'] !== null) {
                s += ('<br>' +
                      ysprod['attrtext']);
            }
            if (ysprod['note'] !== null) {
                s += ('<br>' +
                      '<table border="0" cellpadding="0" cellspacing="0">' +
                      '<tr valign="baseline">' +
                      '<td><b>Note:</b>&nbsp;</td>' +
                      '<td>' +
                      text_to_html_new_line_spaces(ysprod['note']) +
                      '</td>' +
                      '</tr>' +
                      '</table>');
            }
            s += ('</td>' +
                  '</tr>' +
                  '<tr>' +
                  '<td></td>' +
                  '<td colspan="2">' +
                  '<table id="ysprodlinks' + yscatalogid + '" width="100%" border="0" cellpadding="0" cellspacing="0">');
            ysprodlinks = ysprod['links'];
            for (j in ysprodlinks) {
                link = ysprodlinks[j];
                linkid = j.substring(1);
                s += ('<tr><td>&nbsp;</td></tr>' +
                      '<tr valign="top">' +
                      '<td>' +
                      '<input id="sellink' + linkid + '" type="checkbox">' +
                      '</td>' +
                      '<td style="position: relative; padding-left: 10">' +
                      '<div style="position: relative">' +
                      '<table id="link' + linkid + 'col3area" width="100%" border="0" cellpadding="0" cellspacing="0">' +
                      '<tr>' +
                      '<td>' +
                      show_link_col_3(linkid, link) +
                      '</td>' +
                      '</tr>' +
                      '</table>' +
                      '<img id="link' + linkid + 'busy" style="display: none; position: absolute; left: 50%; top: 50%">' +
                      '</div>' +
                      '</td>' +
                      '<td width="40%" style="padding-left: 10">' +
                      '<div id="link' + linkid + 'col4area" style="width: 100%">' +
                      show_link_col_4(linkid, link) +
                      '</div>' +
                      '</td>' +
                      '</tr>' +
                      '<tr>' +
                      '<td></td>' +
                      '<td id="editlinkalert' + linkid + 'area" colspan="2" style="display: none; position: relative; padding: 5 10 0 20"></td>' +
                      '</tr>');
            }
            s += ('</table>' +
                  '</td>' +
                  '</tr>');
        }
        s += '</table>';
    }
    */

    if (sites !== undefined) {
        s += '<table width="100%" cellpadding="0" cellspacing="0">';
        for (i in sortedsites) {
            siteid = sortedsites[i];
            site = sites[siteid];
            siteid = siteid.substring(1);
            if (iffirst == true) iffirst = false;
            else s += '<tr><td><br></td></tr>';
            s += ('<tr valign="top">' +
                  '<td style="padding-right: 5">' +
                  '<a href="javascript: expandcollapsesite_on_click(' + siteid + ');">' +
                  '<img id="expandcollapsesite' + siteid + '" ');
            if (site['ifexpanded'] == true) s += 'src="/image/minus.gif" ';
            else s += 'src="/image/plus.gif" ';
            s += ('border="0">' +
                  '</a>' +
                  '</td>' +
                  '<td width="100%">' +
                  '<a class="site" href="' + site['siteurl'] + '">' +
                  site['sitename'] +
                  '</a> ' +
                  '<a href="' + site['siteurl'] + '" target="_blank" title="Open link in new window">' +
                  '<img src="/image/nw2site.gif" border="0">' +
                  '</a> ' +
            /*
            if (site['ysmerchantrating'] !== null) {
                ysmerchantrating = Math.round(site['ysmerchantrating'] * 2) / 2;
                s += ' <a';
                if (site['ysmerchantrating'] !== null) {
                    s += (' href="' + site['ysmerchantratingurl'] + '"');
                }
                s += (' title="Yahoo! Shopping Merchant Rating: ' + ysmerchantrating + ' stars">' +
                      '<img src="/image/sr_' + ysmerchantrating + '.gif" border="0">' +
                      '</a>');
            }
            */
                  ' <span class="count">(' + site['nlink'] + ')</span>');
            sitefav = site['fav'];
            if (sitefav > 0) {
                s += (' <span style="white-space: nowrap">' +
                      '<img src="/image/heart.jpg">');
                for (i = 1; i < sitefav; i++) {
                    s += '<img src="/image/heart.jpg" style="margin-left: 2">';
                }
                s += '</span>';
            }
            /*
            s += (' <span class="command">' +
                  ' -' +
                  ' <a href="javascript: editsite_on_click(' + siteid + ');" title="Click to mark as favorite or edit note or price">' +
                  'Edit' +
                  '</a>' +
                  '</span>');
            if (site['note'] !== null) {
                s += ('<br>' +
                      '<table border="0" cellpadding="0" cellspacing="0">' +
                      '<tr valign="baseline">' +
                      '<td><b>Note:</b>&nbsp;</td>' +
                      '<td>' +
                      text_to_html_new_line_spaces(site['note']) +
                      '</td>' +
                      '</tr>' +
                      '</table>');
            }
            */
            s += ('</td>' +
                  '</tr>' +
                  '<tr>' +
                  '<td></td>' +
                  '<td>' +
                  '<table id="sitelinks' + siteid + '" width="100%" cellpadding="0" cellspacing="0">' +
                  '<tr>' +
                  '<td></td>' +
                  '<td width="80">' +
                  '<img src="/image/1x1.gif" width="80" height="1">' +
                  '</td>' +
                  '<td width="60%">' +
                  '<td width="40%">' +
                  '</tr>');
            sitelinks = site['links'];
            for (j in sitelinks) {
                link = sitelinks[j];
                linkid = j.substring(1);
                s += ('<tr><td><br></td></tr>' +
                      '<tr valign="top">' +
                      '<td>' +
                      '<input id="sellink' + linkid + '" type="checkbox">' +
                      '</td>' +
                      '<td>' +
                      '<div id="link' + linkid + 'col2area">' +
                      show_link_col_2(link) +
                      '</div>' +
                      '</td>' +
                      '<td style="position: relative; padding-left: 10">' +
                      '<div style="position: relative">' +
                      '<div id="link' + linkid + 'col3area" style="width: 100%">' +
                      show_link_col_3(linkid, link) +
                      '</div>' +
                      '<img id="link' + linkid + 'busy" style="display: none; position: absolute; left: 50%; top: 50%">' +
                      '</div>' +
                      '</td>' +
                      '<td style="padding-left: 10">' +
                      '<div id="link' + linkid + 'col4area" style="width: 100%">' +
                      show_link_col_4(linkid, link) +
                      '</div>' +
                      '</td>' +
                      '</tr>' +
                      '<tr>' +
                      '<td colspan="2"></td>' +
                      '<td id="editlinkalert' + linkid + 'area" colspan="2" style="display: none; position: relative; padding: 5 10 0 20"></td>' +
                      '</tr>');
            }
            s += ('</table>' +
                  '</td>' +
                  '</tr>');
        }
        s += '</table>';
    }

    if (links !== undefined) {
        if (ysprods !== undefined || sites !== undefined) {
            s += '<tr><td><br></td></tr>';
        }
        s += ('<table width="100%" cellpadding="0" cellspacing="0">' +
              '<tr>' +
              '<td></td>' +
              '<td width="80">' +
              '<img src="/image/1x1.gif" width="80" height="1">' +
              '</td>' +
              '<td width="60%">' +
              '<td width="40%">' +
              '</tr>');
        for (i in sortedlinks) {
            linkid = sortedlinks[i];
            link = links[linkid];
            linkid = linkid.substring(1);
            if (iffirst == true) iffirst = false;
            else s += '<tr><td><br></td></tr>';
            s += ('<tr valign="top">' +
                  '<td>' +
                  '<input id="sellink' + linkid + '" type="checkbox">' +
                  '</td>' +
                  '<td>' +
                  '<div id="link' + linkid + 'col2area">' +
                  show_link_col_2(link) +
                  '</div>' +
                  '</td>' +
                  '<td style="position: relative; padding-left: 10">' +
                  '<div style="position: relative">' +
                  // '<table id="link' + linkid + 'col3table" width="100%" border="0" cellpadding="0" cellspacing="0">' +
                  // '<tr>' +
                  // '<td>' +
                  '<div id="link' + linkid + 'col3area" style="width: 100%">' +
                  show_link_col_3(linkid, link) +
                  '</div>' +
                  // '</td>' +
                  // '</tr>' +
                  // '</table>' +
                  '<img id="link' + linkid + 'busy" style="display: none; position: absolute; left: 50%; top: 50%">' +
                  '</div>' +
                  '</td>' +
                  '<td style="padding-left: 10">' +
                  '<div id="link' + linkid + 'col4area" style="width: 100%">' +
                  show_link_col_4(linkid, link) +
                  '</div>' +
                  '</td>' +
                  '</tr>' +
                  '<tr>' +
                  '<td colspan="2"></td>' +
                  '<td id="editlinkalert' + linkid + 'area" colspan="2" style="display: none; position: relative; padding: 5 10 0 20"></td>' +
                  '</tr>');
        }
        s += '</table>';
    }

    return s;
}

function show_link_col_2(link) {
    if (link['thumburl'] !== null) {
        return ('<a href="' + link['url'] + '">' +
                '<img src="' + link['thumburl'] + '" border="0">' +
                '</a>');
    }
    else return '';
}

function show_link_col_3(linkid, link) {
    var fav, ysprodrating, ysprodfav;
    var i, s;

    s = '<a id="link' + linkid + 'title" href="' + link['url'] + '">' + link['title'] + '</a> ' +
        '<a href="' + link['url'] + '" target="_blank" title="Open link in new window">' +
        '<img src="/image/nw2link.gif" border="0">' +
        '</a>&nbsp; ' +
        '<a class="interval" title="Saved at ' + time_to_str(link['savetime']) + ', refreshed at ' + time_to_str(link['lastrefreshtime']) + '">';
    if (link['savetime'] == link['lastrefreshtime']) {
        s += ('saved and refreshed ' + interval_to_str(now - link['savetime']) + ' ago');
    }
    else {
        s += ('saved ' + interval_to_str(now - link['savetime']) + ' ago, refreshed ' + interval_to_str(now - link['lastrefreshtime']) + ' ago');
    }
    s += '</a>';
    fav = link['fav'];
    if (fav > 0) {
        s += ('&nbsp; <span style="white-space: nowrap">' +
              '<img src="/image/heart.jpg">');
        for (i = 1; i < fav; i++) {
            s += '<img src="/image/heart.jpg" style="margin-left: 2">';
        }
        s += '</span>';
    }
    s += (' <span class="command">' +
          '- ' +
          '<a href="javascript: edit_link(' + linkid + ');" title="Mark as favorite or edit note or price">' +
          'Edit' +
          '</a>' +
          '</span>');
    if (link['note'] !== null) {
        s += ('<br>' +
              '<table cellpadding="0" cellspacing="0">' +
              '<tr valign="baseline">' +
              '<td><b>Note:</b>&nbsp;</td>' +
              '<td>' + text_to_html_new_line_spaces(link['note']) + '</td>' +
              '</tr>' +
              '</table>');
    }
    // s += ('<div id="editlinkarea' + linkid + '" style="display: none; position: relative; padding: 3 0 0 0"></div>');
    /*
    if (link['yscatalogid'] !== undefined && link['yscatalogid'] !== null) {
        s += ('<div style="margin-top: 3"></div>' +
              '<span class="product">' +
              '<a href="/my?yscatalogid=' + link['yscatalogid'] + '">' +
              link['ysprodname'] +
              '</a>' +
              '</span>');
        if (link['ysprodrating'] !== null) {
            ysprodrating = Math.round(link['ysprodrating'] * 2) / 2;
            s += ' <a';
            if (link['ysprodratingurl'] !== null) {
                s += (' href="' + link['ysprodratingurl'] + '"');
            }
            s += (' title="Yahoo! Shopping Product Rating: ' + ysprodrating + ' stars">' +
                  '<img src="/image/pr_' + ysprodrating + '.gif" border="0">' +
                  '</a>');
        }
        s += (' <span class="count">(' + link['ysprodnlink'] + ')</span>');
        ysprodfav = link['ysprodfav'];
        if (ysprodfav > 0) {
            s += (' <span style="white-space: nowrap">' +
                  '<img src="/image/heart.jpg">');
            for (i = 1; i < ysprodfav; i++) {
                s += '<img src="/image/heart.jpg" style="margin-left: 2">';
            }
            s += '</span>';
        }
    }
    */
    // s += '<img id="link' + linkid + 'busy" src="http://www.shoppingnotes.com/image/busy.gif" style="display: none; position: absolute; left: 50%; top: 50%">';

    return s;
}

function show_link_col_4(linkid, link) {
    var ysmerchantrating, sitefav;
    var i, s;

    s = ('<span id="link' + linkid + 'priceavailabilityalertarea">' +
         show_price_availability_alert(linkid, link) +
         '</span>');
    if (link['siteid'] !== undefined && link['siteid'] !== null) {
        s += ('<a class="site" href="/my?siteid=' + link['siteid'] + '">' +
              link['sitename'] +
              '</a>');
        if (link['ysmerchantrating'] !== null) {
            ysmerchantrating = Math.round(link['ysmerchantrating'] * 2) / 2;
            s += ' <a';
            if (link['ysmerchantratingurl'] !== null) {
                s += (' href="' + link['ysmerchantratingurl'] + '"');
            }
            s += (' title="Yahoo! Shopping Merchant Rating: ' + ysmerchantrating + 'stars">' +
                  '<img src="/image/sr_' + ysmerchantrating + '.gif" border="0">' +
                  '</a>');
        }
        s += (' <span class="count">(' + link['sitenlink'] + ')</span>');
        sitefav = link['sitefav'];
        if (sitefav > 0) {
            s += (' <span style="white-space: nowrap">' +
                  '<img src="/image/heart.jpg">');
            for (i = 1; i < sitefav; i++) {
                s += '<img src="/image/heart.jpg" style="margin-left: 2">';
            }
            s += '</span>';
        }
    }

    return s;
}

function show_price_availability_alert(linkid, link) {
    var s;

    if (link['errormsg'] !== undefined && link['errormsg'] !== null) {
        s = '<table cellpadding="0" cellspacing="0" style="color: #FF0000">' +
            '<tr valign="baseline">' +
            '<td><b>Warning:</b>&nbsp;</td>' +
            '<td>' + link['errormsg'] + '</td>' +
            '</tr>' +
            '</table>';
    }
    else s = '';
    if (link['oldhtmlpriceavailabilitytext'] !== undefined) {
        s += ('<table cellpadding="0" cellspacing="0">' +
              '<tr valign="baseline">' +
              '<td>' +
              '<span style="color: #FF0000; font-weight: bold">New:</span>&nbsp;' +
              '</td>' +
              '<td>');
    }
    if (link['htmlpriceavailabilitytext'] !== null) {
        if (link['price'] !== null && link['alertprice'] !== null &&
            link['alertcurrency'] !== null &&
            (link['currency'] === null ||
             (link['currency'] == link['alertcurrency'] &&
              link['price'] <= link['alertprice']))) {
            s += ('<a title="Current price has dropped to or below the alert price" style="color: #FF0000">' +
                  link['htmlpriceavailabilitytext'] +
                  '</a>');
        }
        else if (link['oldhtmlpriceavailabilitytext'] !== undefined) {
            s += ('<span style="color: #FF0000">' +
                  link['htmlpriceavailabilitytext'] +
                  '</span>');
        }
        else s += link['htmlpriceavailabilitytext'];
        s += ' ';
        if (link['alertprice'] === null) {
            s += ('<span class="command">' +
                  '- ');
        }
    }
    else if (link['alertprice'] === null) s += '<span class="command">';
    if (link['alertprice'] === null) {
        s += ('<a href="javascript: edit_alert(' + linkid + ');" title="Set alert on price changes">' +
              'Set Alert' +
              '</a>' +
              '</span>');
    }
    if (link['oldhtmlpriceavailabilitytext'] !== undefined) {
        s += ('</td>' +
              '</tr>' +
              '</table>' +
              '<table cellpadding="0" cellspacing="0">' +
              '<tr valign="baseline">' +
              '<td><b>Old:</b>&nbsp;</td>' +
              '<td>');
        if (link['oldhtmlpriceavailabilitytext'] !== null) {
            s += (link['oldhtmlpriceavailabilitytext'] + ' ');
        }
        s += ('<span class="command">' +
              '- ' +
              '<a href="javascript: show_old_price(' + linkid + ');" title="Show the old price instead of the new price">' +
              'Show Old Price' +
              '</a>' +
              '</span>'+
              '</td>' +
              '</tr>' +
              '</table>');
    }
    if (link['alertprice'] !== null) {
        s += ('<table cellpadding="0" cellspacing="0" style="margin-top: 3">' +
              '<tr valign="baseline">' +
              '<td nowrap>' +
              '<img src="/image/bell4.gif" align="left"> Alert:&nbsp;' +
              '</td>' +
              '<td>');
        if (now < link['alertexpiretime']) s += '<a title="Expires ';
        else s += '<a title="Expired ';
        s += ('at ' + time_to_str(link['alertexpiretime']) + '">' +
              '<span style="color: #000000">');
        if (link['alertcurrency'] === null) s += 'any price change';
        else {
            if (link['alertcurrency'] == 'USD') s += '$';
            else if (link['alertcurrency'] == 'EUR') s += '&euro;';
            else if (link['alertcurrency'] == 'GBP') s += '&pound;';
            s += commify(link['alertprice'].toString());
        }
        if (now >= link['alertexpiretime'] - ALERT_EXPIRING_PERIOD) {
            s += (',' +
                  '</span> ' +
                  '<span style="color: #FF0000">');
            if (now >= link['alertexpiretime']) s += 'expired';
            else s += ('expiring in ' + interval_to_str(link['alertexpiretime'] - now));
        }
        s += ('</span>' +
              '</a> ' +
              '<span class="command">' +
              '- ' +
              '<a href="javascript: edit_alert(' + linkid + ');" title="Edit alert">' +
              'Edit' +
              '</a>&nbsp; ' +
              '<a href="javascript: renew_alert(' + linkid + ');" title="Renew alert for another 30 days">' +
              'Renew' +
              '</a>' +
              '</span>' +
              '</td>' +
              '</tr>' +
              '</table>');
    }
    s += '<div style="margin-top: 3"></div>';

    return s;
}

function expandcollapseysprod_on_click(yscatalogid) {
    if (ysprods['_' + yscatalogid]['ifexpanded'] == true) {
        document.getElementById('ysprodlinks' + yscatalogid).style.display = 'none';
        document.getElementById('expandcollapseysprod' + yscatalogid).src = '/image/plus.gif';

        ysprods['_' + yscatalogid]['ifexpanded'] = false;
    }
    else {
        document.getElementById('ysprodlinks' + yscatalogid).style.display = '';
        document.getElementById('expandcollapseysprod' + yscatalogid).src = '/image/minus.gif';

        ysprods['_' + yscatalogid]['ifexpanded'] = true;
    }
}

function expandcollapsesite_on_click(siteid) {
    if (sites['_' + siteid]['ifexpanded'] == true) {
        document.getElementById('sitelinks' + siteid).style.display = 'none';
        document.getElementById('expandcollapsesite' + siteid).src = '/image/plus.gif';

        sites['_' + siteid]['ifexpanded'] = false;
    }
    else {
        document.getElementById('sitelinks' + siteid).style.display = '';
        document.getElementById('expandcollapsesite' + siteid).src = '/image/minus.gif';

        sites['_' + siteid]['ifexpanded'] = true;
    }
}

function edit_link(linkid) {
    var link, fav;
    var i, s, t;

    link = find_link(linkid);
    if (document.getElementById('editlinkalert' + linkid + 'area').style.display == 'none') {
        // link['ifedit'] = true;
        fav = link['fav'];
        editfavs['editlink' + linkid] = fav;
        t = 'editlink' + linkid + 'heart';

        s = '<table cellpadding="0" cellspacing="0">' +
            '<tr>' +
            '<td style="position: relative">' +
            '<div style="position: relative">' +
            '<table id="editlink' + linkid + 'table" cellpadding="0" cellspacing="0">' +
            '<tr valign="top">' +
            '<td align="right" style="font-weight: bold" nowrap>Favorite</td>' +
            '<td style="padding-left: 10">' +
            '<span style="white-space: nowrap">';
        if (fav > 0) {
            s += ('<img id="' + t + '1" src="/image/heart.jpg"');
        }
        else s += ('<img id="' + t + '1" src="/image/eheart.jpg"');
        s += ' onClick="javascript: heart_on_click(\'editlink' + linkid + '\', 1);" onMouseOver="javascript: heart_on_mouse_over(\'editlink' + linkid + '\', 1);" onMouseOut="javascript: heart_on_mouse_out(\'editlink' + linkid + '\', 1);">';
        for (i = 2; i <= 3; i++) {
            if (fav >= i) {
                s += ('<img id="' + t + i + '" src="/image/heart.jpg"');
            }
            else s += ('<img id="' + t + i + '" src="/image/eheart.jpg"');
            s += (' style="margin-left: 2" onClick="javascript: heart_on_click(\'editlink' + linkid + '\', ' + i + ');" onMouseOver="javascript: heart_on_mouse_over(\'editlink' + linkid + '\', ' + i + ');" onMouseOut="javascript: heart_on_mouse_out(\'editlink' + linkid + '\', ' + i + ');">');
        }
        s += ('</span>&nbsp; ' +
              '<a class="command" href="javascript: clear_favorites(\'editlink' + linkid + '\');">Clear</a>' +
              '</td>' +
              '</tr>' +
              '<tr><td style="padding-top: 3"></td></tr>' +
              '<tr valign="top">' +
              '<td align="right" style="font-weight: bold" nowrap>Note</td>' +
              '<td style="padding-left: 10">' +
              '<textarea id="editlink' + linkid + 'note" rows="3" cols="80">\n');
        if (link['note'] !== null) s += link['note'];
        s += ('</textarea>' +
              '</td>' +
              '</tr>' +
              '<tr><td style="padding-top: 3"></td></tr>' +
              '<tr valign="top">' +
              '<td align="right" style="font-weight: bold" nowrap>Price &amp;<br>Availability</td>' +
              '<td style="padding-left: 10">' +
              '<textarea id="editlink' + linkid + 'priceavailabilitytext" rows="3" cols="80">\n');
        if (link['htmlpriceavailabilitytext'] !== null) {
            s += link['htmlpriceavailabilitytext'].replace(/(?:<br>|<br \/>)/gi, '\n').replace(/(?:<b>|<\/b>)/gi, '').replace(/&nbsp;/g, ' ');
        }
        s += ('</textarea>' +
              '</td>' +
              '</tr>' +
              '<tr><td style="padding-top: 3"></td></tr>' +
              '<tr>' +
              '<td colspan="2">' +
              '<center>' +
              '<input id="saveeditlink' + linkid + '" type="button" value="Save" onClick="javascript: saveeditlink_on_click(' + linkid + ');">&nbsp; ' +
              '<input id="canceleditlink' + linkid + '" type="button" value="Cancel" onClick="javascript: canceleditlink_on_click(' + linkid + ');">' +
              '</center>' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '<img id="editlink' + linkid + 'busy" style="display: none; position: absolute; left: 50%; top: 50%">' +
              '</div>' +
              '</td>' +
              '</tr>' +
              '</table>');
        document.getElementById('editlinkalert' + linkid + 'area').innerHTML = s;
        document.getElementById('editlinkalert' + linkid + 'area').style.display = '';
    }
}

function saveeditlink_on_click(linkid) {
    var link, fav, xmlhttpreq, timeout;
    var iftimeout;
    var i, s, t;

    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    link = find_link(linkid);
    // if (link === null) return;
    fav = editfavs['editlink' + linkid];
    s = 'editlink' + linkid;
    t = s + 'heart';

    try {
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('The edited link cannot be saved because there was an error ' +
                  'communicating with the server. Please try again later.');

            document.getElementById(s + 'table').style.background = '#FFFFFF';
            document.getElementById(s + 'table').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'table').style.opacity = 1;
            for (i = 1; i <= fav; i++) {
                document.getElementById(t + i).src = '/image/heart.jpg';
            }
            for (; i <= 3; i++) {
                document.getElementById(t + i).src = '/image/eheart.jpg';
            }
            document.getElementById(s + 'note').disabled = false;
            document.getElementById(s + 'priceavailabilitytext').disabled = false;
            document.getElementById('saveeditlink' + linkid).disabled = false;
            document.getElementById('canceleditlink' + linkid).disabled = false;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/editlink', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('The edited link cannot be saved because there ' +
                           'was an error communicating with the server. ' +
                           'Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = url + '&sort=' + sort_top.options[sort_top.selectedIndex].value;
                // location.reload();

                return;
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById(s + 'table').style.background = '#FFFFFF';
            document.getElementById(s + 'table').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'table').style.opacity = 1;
            for (i = 1; i <= fav; i++) {
                document.getElementById(t + i).src = '/image/heart.jpg';
            }
            for (; i <= 3; i++) {
                document.getElementById(t + i).src = '/image/eheart.jpg';
            }
            document.getElementById(s + 'note').disabled = false;
            document.getElementById(s + 'priceavailabilitytext').disabled = false;
            document.getElementById('saveeditlink' + linkid).disabled = false;
            document.getElementById('canceleditlink' + linkid).disabled = false;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpreq.send('linkid=' + linkid + '&fav=' + fav +
                        '&note=' +
                        encodeURIComponent(document.getElementById(s + 'note').value) +
                        '&priceavailabilitytext=' +
                        encodeURIComponent(document.getElementById(s + 'priceavailabilitytext').value));

        document.getElementById(s + 'table').style.background = '#FFFFFF';
        document.getElementById(s + 'table').style.filter = 'alpha(opacity=30)';
        document.getElementById(s + 'table').style.opacity = 0.3;
        for (i = 1; i <= fav; i++) {
            document.getElementById(t + i).src = '/image/dheart.jpg';
        }
        for (; i <= 3; i++) {
            document.getElementById(t + i).src = '/image/eheart.jpg';
        }
        document.getElementById(s + 'note').disabled = true;
        document.getElementById(s + 'priceavailabilitytext').disabled = true;
        document.getElementById('saveeditlink' + linkid).disabled = true;
        document.getElementById('canceleditlink' + linkid).disabled = true;
        document.getElementById(s + 'busy').src = '/image/busy.gif';
        document.getElementById(s + 'busy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function canceleditlink_on_click(linkid) {
    document.getElementById('editlinkalert' + linkid + 'area').style.display = 'none';
}

function heart_on_click(id, i) {
    editfavs[id] = i;
}

function heart_on_mouse_over(id, i) {
    var fav;
    var i, j;

    fav = editfavs[id];
    if (i > fav) {
        for (j = fav + 1; j <= i; j++) {
            document.getElementById(id + 'heart' + j).src = '/image/heart.jpg';
        }
    }
    else if (i < fav) {
        for (j = i + 1; j <= fav; j++) {
            document.getElementById(id + 'heart' + j).src = '/image/eheart.jpg';
        }
    }
}

function heart_on_mouse_out(id, i) {
    var fav;
    var i, j;

    fav = editfavs[id];
    if (i > fav) {
        for (j = fav + 1; j <= i; j++) {
            document.getElementById(id + 'heart' + j).src = '/image/eheart.jpg';
        }
    }
    else if (i < fav) {
        for (j = i + 1; j <= fav; j++) {
            document.getElementById(id + 'heart' + j).src = '/image/heart.jpg';
        }
    }
}

function clear_favorites(id) {
    heart_on_mouse_over(id, 0);
    editfavs[id] = 0;
}

function edit_alert(linkid) {
    var link;
    var s;

    link = find_link(linkid);
    if (document.getElementById('editlinkalert' + linkid + 'area').style.display == 'none') {
        s = '<table cellpadding="0" cellspacing="0">' +
            '<tr>' +
            '<td style="position: relative">' +
            '<div style="position: relative">' +
            '<table id="editalert' + linkid + 'table" cellpadding="0" cellspacing="0">' +
            '<tr valign="baseline">' +
            '<td align="right" style="font-weight: bold" nowrap>Alert Price</td>' +
            '<td style="padding-left: 10">' +
            '<input type="radio" id="editalert' + linkid + 'alert2" name="editalert' + linkid + 'alert"';
        if (link['alertprice'] === null || link['alertcurrency'] === null) {
            s += ' checked';
        }
        s += ('>Any price change' +
              '<br>' +
              '<input type="radio" id="editalert' + linkid + 'alert3" name="editalert' + linkid + 'alert"');
        if (link['alertprice'] !== null && link['alertcurrency'] !== null) {
            s += ' checked';
        }
        s += '> ';
        if (link['currency'] === null) s += '$';
        else if (link['currency'] == 'EUR') s += '&euro;';
        else if (link['currency'] == 'GBP') s += '&pound;';
        else s += '$';
        s += (' <input type="text" id="editalert' + linkid + 'alertprice" ');
        if (link['alertprice'] !== null && link['alertcurrency'] !== null) {
            s += ('value="' + link['alertprice'] + '" ');
        }
        s += ('size="10" maxlength="10" onFocus="javascript: document.getElementById(\'editalert' + linkid + 'alert3\').checked = true;"> ' +
              '(An alert will be sent when the price drops to or below this price)' +
              '</td>' +
              '</tr>' +
              '<tr><td style="padding-top: 3"></td></tr>' +
              '<tr valign="baseline">' +
              '<td align="right" style="font-weight: bold" nowrap>Duration</td>' +
              '<td width="100%" style="padding-left: 10">' +
              '<input type="text" id="editalert' + linkid + 'duration" value="' + ALERT_DEF_LIFETIME_DAY + '" size="2" maxlength="2">' +
              ' day(s)' +
              '</td>' +
              '</tr>' +
              /*
              '<tr valign="top">' +
              '<td></td>' +
              '<td width="100%" style="padding-left: 10">' +
              // '<span id="editalert' + linkid + 'sendalerttofriends" class="command" style="padding-top: 3">' +
              // '<a href="javascript: send_alert_to_friends(' + linkid + ');">' +
              // 'Send Alert to Friends' +
              // '</a>' +
              // '</span>' +
              '</td>' +
              '</tr>' +
              '<tr><td style="padding-top: 3"></td></tr>' +
              '<tr valign="baseline">' +
              '<td align="right" nowrap>' +
              '<div id="editalert' + linkid + 'sendtocol1area"></div>' +
              '</td>' +
              '<td width="100%" style="padding-left: 10">' +
              '<div id="editalert' + linkid + 'sendtocol2area" style="display: none"></div>' +
              '</td>' +
              '</tr>' +
              */
              '<tr><td style="padding-top: 3"></td></tr>' +
              '<tr>' +
              '<td colspan="2">' +
              '<center>' +
              '<input id="saveeditalert' + linkid + '" type="button" value="Save" onClick="javascript: saveeditalert_on_click(' + linkid + ');">&nbsp; ' +
              '<input id="deletealert' + linkid + '" type="button" value="Delete" onClick="javascript: deletealert_on_click(' + linkid + ');">&nbsp; ' +
              '<input id="canceleditalert' + linkid + '" type="button" value="Cancel" onClick="javascript: canceleditalert_on_click(' + linkid + ');">' +
              '</center>' +
              '</td>' +
              '</tr>' +
              '</table>' +
              '<img id="editalert' + linkid + 'busy" style="display: none; position: absolute; left: 50%; top: 50%">' +
              '</div>' +
              '</td>' +
              '</tr>' +
              '</table>');
        document.getElementById('editlinkalert' + linkid + 'area').innerHTML = s;
        document.getElementById('editlinkalert' + linkid + 'area').style.display = '';
    }
}

function send_alert_to_friends(linkid) {
    document.getElementById('editalert' + linkid + 'sendtocol2area').innerHTML =
    // 'Enter or select the email addresses you want to also send this alert to' +
    // '<br>' +
    '<input id="editalert' + linkid + 'email" type="text" style="width: 300"> ' +
    '(Separate email addresses by comma)' +
    '<iframe id="editalert' + linkid + 'selcontacts" name="editalert' + linkid + 'selcontacts" src="/selectcontacts?emailid=editalert' + linkid + 'email" width="300" height="80" marginwidth="0" marginheight="0" style="display: none; margin-top: 3" onLoad="javascript: selectcontacts_on_load(' + linkid +');">';
}

function selectcontacts_on_load(linkid) {
    var selcontactsdocument;
    var s;

    s = 'editalert' + linkid;
    try {
        selcontactsdocument = frames(s + 'selcontacts').document;
    }
    catch (ex) {
        selcontactsdocument = document.getElementById(s + 'selcontacts').contentDocument;
    }

    if (selcontactsdocument.getElementById('errormsg').value != '') {
        alert(selcontactsdocument.getElementById('errormsg').value);

        return;
    }

    document.getElementById(s + 'sendalerttofriends').style.display = 'none';
    document.getElementById(s + 'sendtocol1area').innerHTML = '<b>Send To</b>';
    document.getElementById(s + 'sendtocol2area').style.display = '';
    if (selcontactsdocument.getElementById('ncontact').value > 0) {
        document.getElementById(s + 'selcontacts').style.display = '';
    }
    else {
        document.getElementById(s + 'selcontacts').style.display = 'none';
    }
}

function saveeditalert_on_click(linkid) {
    var link, xmlhttpreq, timeout;
    var iftimeout;
    var s, t;

    t = 'editalert' + linkid;
    if (document.getElementById(t + 'alert3').checked == true &&
        document.getElementById(t + 'alertprice').value.match(/^\s*(?:\d+(?:\.\d{1,2}|)|\.\d{1,2})\s*$/) === null) {
        alert('Please enter a valid alert price.');

        return;
    }
    if (document.getElementById(t + 'duration').value.match(/^\d{1,2}$/) === null) {
        alert('Please enter a valid number for alert duration.');

        return;
    }
    if (document.getElementById(t + 'duration').value > ALERT_DEF_LIFETIME_DAY) {
        alert('You cannot set an alert for more than ' + ALERT_DEF_LIFETIME_DAY + ' days.');

        return;
    }
    if (document.getElementById(t + 'duration').value < 1) {
        alert('You cannot set an alert for less than 1 day.');

        return;
    }
    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    link = find_link(linkid);

    try {
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('The edited alert cannot be saved because there was an error ' +
                  'communicating with the server. Please try again later.');

            document.getElementById(t + 'table').style.background = '#FFFFFF';
            document.getElementById(t + 'table').style.filter = 'alpha(opacity=100)';
            document.getElementById(t + 'table').style.opacity = 1;
            document.getElementById(t + 'alert2').disabled = false;
            document.getElementById(t + 'alert3').disabled = false;
            document.getElementById(t + 'alertprice').disabled = false;
            if (document.getElementById(t + 'email') !== null) {
                document.getElementById(t + 'email').disabled = false;
            }
            document.getElementById('saveeditalert' + linkid).disabled = false;
            document.getElementById('deletealert' + linkid).disabled = false;
            document.getElementById('canceleditalert' + linkid).disabled = false;
            document.getElementById(t + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/editalert', true);

        xmlhttpreq.onreadystatechange = function() {
            var result, refreshtime;
            var t;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                t = 'editalert' + linkid;

                if (xmlhttpreq.status != 200)  {
                    throw ('The edited alert cannot be saved because there ' +
                           'was an error communicating with the server. ' +
                           'Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = url + '&sort=' + sort_top.options[sort_top.selectedIndex].value;
                // location.reload();
                /*
                if (result['links'] !== undefined) {
                    newlink = result['links'][linkid];

                    link['errormsg'] = newlink['errormsg'];
                    if (newlink['errormsg'] === null) {
                        link['title'] = newlink['title'];
                        link['lastrefreshtime'] = result['refreshtime'];
                        if (newlink['htmlpriceavailabilitytext'] !== link['htmlpriceavailabilitytext']) {
                            link['oldhtmlpriceavailabilitytext'] = link['htmlpriceavailabilitytext'];
                            link['htmlpriceavailabilitytext'] = newlink['htmlpriceavailabilitytext'];
                            if (group == GROUP_BY_NONE) {
                                link['oldcurrency'] = link['currency'];
                                link['currency'] = newlink['currency'];
                                link['oldprice'] = link['price'];
                                link['price'] = newlink['price'];
                                link['oldavailability'] = link['availability'];
                                link['availability'] = newlink['availability'];
                            }
                        }
                    }
                }
                if (document.getElementById(t + 'havealertprice1').checked == true) {
                    link['alertcurrency'] = null;
                    link['alertprice'] = 0.0;
                }
                if (document.getElementById(t + 'havealertprice2').checked == true) {
                    link['alertcurrency'] = 'USD';
                    link['alertprice'] = document.getElementById(t + 'alertprice').value;
                }
                link['alertexpiretime'] = result['alertexpiretime'];
                if (result['links'] !== undefined) {
                    document.getElementById('link' + linkid + 'col3area').innerHTML = show_link_col_3(linkid, link);
                }
                document.getElementById('link' + linkid + 'priceavailabilityalertarea').innerHTML = show_price_availability_alert(linkid, link);
                document.getElementById('editlinkalert' + linkid + 'area').style.display = 'none';

                ifxmlhttpreq = false;
                */

                return;
            }
            catch (ex) {
                if (ex == ('You have exceeded the maximum number of alerts ' +
                           'you are allowed to set.')) {
                    if (confirm('You have exceeded the maximum number of ' +
                                'alerts you are allowed to set. Would you like ' +
                                'to learn how to increase this limit?') == true) {
                        location = '/help';
                    }
                }
                else alert(ex);
            }

            document.getElementById(t + 'table').style.background = '#FFFFFF';
            document.getElementById(t + 'table').style.filter = 'alpha(opacity=100)';
            document.getElementById(t + 'table').style.opacity = 1;
            document.getElementById(t + 'alert2').disabled = false;
            document.getElementById(t + 'alert3').disabled = false;
            document.getElementById(t + 'alertprice').disabled = false;
            if (document.getElementById(t + 'email') !== null) {
                document.getElementById(t + 'email').disabled = false;
            }
            document.getElementById('saveeditalert' + linkid).disabled = false;
            document.getElementById('deletealert' + linkid).disabled = false;
            document.getElementById('canceleditalert' + linkid).disabled = false;
            document.getElementById(t + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        s = 'linkid=' + linkid + '&alertcurrency=';
        if (document.getElementById(t + 'alert2').checked == true) {
            s += '&alertprice=0.0';
        }
        if (document.getElementById(t + 'alert3').checked == true) {
            if (link['currency'] === null) s += 'USD';
            else s += link['currency'];
            s += ('&alertprice=' + document.getElementById(t + 'alertprice').value);
        }
        s += ('&duration=' + document.getElementById(t + 'duration').value);
        if (document.getElementById(t + 'email') !== null) {
            s += ('&email=' + encodeURIComponent(email));
            // emails = document.getElementById(t + 'email').value.split(/[,\s]/);
            // for (n = emails.length, i = 0; i < n; i++) {
            //     s += ('&emails[]=' + encodeURIComponent(emails[i]));
            // }
        }
        xmlhttpreq.send(s);

        document.getElementById(t + 'table').style.background = '#FFFFFF';
        document.getElementById(t + 'table').style.filter = 'alpha(opacity=30)';
        document.getElementById(t + 'table').style.opacity = 0.3;
        document.getElementById(t + 'alert2').disabled = true;
        document.getElementById(t + 'alert3').disabled = true;
        document.getElementById(t + 'alertprice').disabled = true;
        if (document.getElementById(t + 'email') !== null) {
            document.getElementById(t + 'email').disabled = true;
        }
        document.getElementById('saveeditalert' + linkid).disabled = true;
        document.getElementById('deletealert' + linkid).disabled = true;
        document.getElementById('canceleditalert' + linkid).disabled = true;
        document.getElementById(t + 'busy').src = '/image/busy.gif';
        document.getElementById(t + 'busy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function deletealert_on_click(linkid){
    var xmlhttpreq, timeout;
    var iftimeout;
    var s;

    if (confirm('Are you sure you want to delete this alert?') == false) return;
    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    s = 'editalert' + linkid;

    try {
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('This alert cannot be deleted because there was an error ' +
                  'communicating with the server. Please try again later.');

            document.getElementById(s + 'table').style.background = '#FFFFFF';
            document.getElementById(s + 'table').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'table').style.opacity = 1;
            document.getElementById(s + 'alert2').disabled = false;
            document.getElementById(s + 'alert3').disabled = false;
            document.getElementById(s + 'alertprice').disabled = false;
            if (document.getElementById(s + 'email') !== null) {
                document.getElementById(s + 'email').disabled = false;
            }
            document.getElementById('saveeditalert' + linkid).disabled = false;
            document.getElementById('deletealert' + linkid).disabled = false;
            document.getElementById('canceleditalert' + linkid).disabled = false;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/deletealert', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('This alert cannot be deleted because there was ' +
                           'an error communicating with the server. Please ' +
                           'try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                location = url + '&sort=' + sort_top.options[sort_top.selectedIndex].value;
                // location.reload();
                /*
                link = find_link(linkid);
                link['alertcurrency'] = null;
                link['alertprice'] = null;
                link['alertexpiretime'] = null;
                document.getElementById('editlinkalert' + linkid + 'area').style.display = 'none';
                document.getElementById('link' + linkid + 'priceavailabilityalertarea').innerHTML =
                show_price_availability_alert(linkid, link);

                ifxmlhttpreq = false;
                */

                return;
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById(s + 'table').style.background = '#FFFFFF';
            document.getElementById(s + 'table').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'table').style.opacity = 1;
            document.getElementById(s + 'alert2').disabled = false;
            document.getElementById(s + 'alert3').disabled = false;
            document.getElementById(s + 'alertprice').disabled = false;
            if (document.getElementById(s + 'email') !== null) {
                document.getElementById(s + 'email').disabled = false;
            }
            document.getElementById('saveeditalert' + linkid).disabled = false;
            document.getElementById('deletealert' + linkid).disabled = false;
            document.getElementById('canceleditalert' + linkid).disabled = false;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpreq.send('linkid=' + linkid);

        document.getElementById(s + 'table').style.background = '#FFFFFF';
        document.getElementById(s + 'table').style.filter = 'alpha(opacity=30)';
        document.getElementById(s + 'table').style.opacity = 0.3;
        document.getElementById(s + 'alert2').disabled = true;
        document.getElementById(s + 'alert3').disabled = true;
        document.getElementById(s + 'alertprice').disabled = true;
        if (document.getElementById(s + 'email') !== null) {
            document.getElementById(s + 'email').disabled = true;
        }
        document.getElementById('saveeditalert' + linkid).disabled = true;
        document.getElementById('deletealert' + linkid).disabled = true;
        document.getElementById('canceleditalert' + linkid).disabled = true;
        document.getElementById(s + 'busy').src = '/image/busy.gif';
        document.getElementById(s + 'busy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function canceleditalert_on_click(linkid) {
    document.getElementById('editlinkalert' + linkid + 'area').style.display = 'none';
}

function renew_alert(linkid){
    var xmlhttpreq, timeout;
    var iftimeout;
    var s;

    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    s = 'link' + linkid;

    try {
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('This alert cannot be renewed because there was an error ' +
                  'communicating with the server. Please try again later.');

            document.getElementById('sellink' + linkid).disabled = false;
            document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
            document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=100)';
            document.getElementById('sellink' + linkid).style.opacity = 1;
            if (document.getElementById(s + 'col2area') !== null) {
                document.getElementById(s + 'col2area').style.background = '#FFFFFF';
                document.getElementById(s + 'col2area').style.filter = 'alpha(opacity=100)';
                document.getElementById(s + 'col2area').style.opacity = 1;
            }
            document.getElementById(s + 'col3area').style.background = '#FFFFFF';
            document.getElementById(s + 'col3area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col3area').style.opacity = 1;
            document.getElementById(s + 'col4area').style.background = '#FFFFFF';
            document.getElementById(s + 'col4area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col4area').style.opacity = 1;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/renewalert', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('This alert cannot be renewed because there was ' +
                           'an error communicating with the server. Please ' +
                           'try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                link = find_link(linkid);
                link['alertexpiretime'] = result['alertexpiretime'];
                document.getElementById(s + 'priceavailabilityalertarea').innerHTML = show_price_availability_alert(linkid, link);
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById('sellink' + linkid).disabled = false;
            document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
            document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=100)';
            document.getElementById('sellink' + linkid).style.opacity = 1;
            if (document.getElementById(s + 'col2area') !== null) {
                document.getElementById(s + 'col2area').style.background = '#FFFFFF';
                document.getElementById(s + 'col2area').style.filter = 'alpha(opacity=100)';
                document.getElementById(s + 'col2area').style.opacity = 1;
            }
            document.getElementById(s + 'col3area').style.background = '#FFFFFF';
            document.getElementById(s + 'col3area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col3area').style.opacity = 1;
            document.getElementById(s + 'col4area').style.background = '#FFFFFF';
            document.getElementById(s + 'col4area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col4area').style.opacity = 1;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpreq.send('linkid=' + linkid);

        if (document.getElementById('editlinkalert' + linkid + 'area').style.display != 'none') {
            document.getElementById('editlinkalert' + linkid + 'area').style.display = 'none';
        }
        document.getElementById('sellink' + linkid).disabled = true;
        document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
        document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=30)';
        document.getElementById('sellink' + linkid).style.opacity = 0.3;
        if (document.getElementById(s + 'col2area') !== null) {
            document.getElementById(s + 'col2area').style.background = '#FFFFFF';
            document.getElementById(s + 'col2area').style.filter = 'alpha(opacity=30)';
            document.getElementById(s + 'col2area').style.opacity = 0.3;
        }
        document.getElementById(s + 'col3area').style.background = '#FFFFFF';
        document.getElementById(s + 'col3area').style.filter = 'alpha(opacity=30)';
        document.getElementById(s + 'col3area').style.opacity = 0.3;
        document.getElementById(s + 'col4area').style.background = '#FFFFFF';
        document.getElementById(s + 'col4area').style.filter = 'alpha(opacity=30)';
        document.getElementById(s + 'col4area').style.opacity = 0.3;
        document.getElementById(s + 'busy').src = '/image/busy.gif';
        document.getElementById(s + 'busy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function show_old_price(linkid){
    var xmlhttpreq, timeout;
    var iftimeout;
    var s;

    if (ifxmlhttpreq == true) {
        alert('Another command is being processed. Please try again later.');

        return;
    }
    ifxmlhttpreq = true;

    s = 'link' + linkid;

    try {
        xmlhttpreq = get_xml_http_request();
        if (xmlhttpreq === null) {
            throw ('Your browser doesn\'t support this function. Please use ' +
                   'a newer version Internet Explorer or FireFox browser.');
        }
        iftimeout = false;

        timeout = setTimeout(function() {
            iftimeout = true;

            xmlhttpreq.abort();

            alert('Cannot change to show the old price because there was an ' +
                  'error communicating with the server. Please try again later.');

            document.getElementById('sellink' + linkid).disabled = false;
            document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
            document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=100)';
            document.getElementById('sellink' + linkid).style.opacity = 1;
            if (document.getElementById(s + 'col2area') !== null) {
                document.getElementById(s + 'col2area').style.background = '#FFFFFF';
                document.getElementById(s + 'col2area').style.filter = 'alpha(opacity=100)';
                document.getElementById(s + 'col2area').style.opacity = 1;
            }
            document.getElementById(s + 'col3area').style.background = '#FFFFFF';
            document.getElementById(s + 'col3area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col3area').style.opacity = 1;
            document.getElementById(s + 'col4area').style.background = '#FFFFFF';
            document.getElementById(s + 'col4area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col4area').style.opacity = 1;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }, AJAX_TIMEOUT);

        xmlhttpreq.open('POST', '/ajax/showoldprice', true);

        xmlhttpreq.onreadystatechange = function() {
            var result;

            if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

            clearTimeout(timeout);
            try {
                if (xmlhttpreq.status != 200)  {
                    throw ('Cannot change to show the old price because ' +
                           'there was an error communicating with the ' +
                           'server. Please try again later.');
                }
                eval('result = ' + xmlhttpreq.responseText + ';');
                if (result['errormsg'] !== null) throw result['errormsg'];
                link = find_link(linkid);
                link['htmlpriceavailabilitytext'] = link['oldhtmlpriceavailabilitytext'];
                delete link['oldhtmlpriceavailabilitytext'];
                if (group == GROUP_BY_NONE) {
                    link['currency'] = link['oldcurrency'];
                    delete link['oldcurrency'];
                    link['price'] = link['oldprice'];
                    delete link['oldprice'];
                    link['availability'] = link['oldavailability'];
                    delete link['oldavailability'];
                }
                document.getElementById(s + 'priceavailabilityalertarea').innerHTML = show_price_availability_alert(linkid, link);
            }
            catch (ex) {
                alert(ex);
            }

            document.getElementById('sellink' + linkid).disabled = false;
            document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
            document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=100)';
            document.getElementById('sellink' + linkid).style.opacity = 1;
            if (document.getElementById(s + 'col2area') !== null) {
                document.getElementById(s + 'col2area').style.background = '#FFFFFF';
                document.getElementById(s + 'col2area').style.filter = 'alpha(opacity=100)';
                document.getElementById(s + 'col2area').style.opacity = 1;
            }
            document.getElementById(s + 'col3area').style.background = '#FFFFFF';
            document.getElementById(s + 'col3area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col3area').style.opacity = 1;
            document.getElementById(s + 'col4area').style.background = '#FFFFFF';
            document.getElementById(s + 'col4area').style.filter = 'alpha(opacity=100)';
            document.getElementById(s + 'col4area').style.opacity = 1;
            document.getElementById(s + 'busy').style.display = 'none';

            ifxmlhttpreq = false;
        }

        xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xmlhttpreq.send('linkid=' + linkid);

        if (document.getElementById('editlinkalert' + linkid + 'area').style.display != 'none') {
            document.getElementById('editlinkalert' + linkid + 'area').style.display = 'none';
        }
        document.getElementById('sellink' + linkid).disabled = true;
        document.getElementById('sellink' + linkid).style.background = '#FFFFFF';
        document.getElementById('sellink' + linkid).style.filter = 'alpha(opacity=30)';
        document.getElementById('sellink' + linkid).style.opacity = 0.3;
        if (document.getElementById(s + 'col2area') !== null) {
            document.getElementById(s + 'col2area').style.background = '#FFFFFF';
            document.getElementById(s + 'col2area').style.filter = 'alpha(opacity=30)';
            document.getElementById(s + 'col2area').style.opacity = 0.3;
        }
        document.getElementById(s + 'col3area').style.background = '#FFFFFF';
        document.getElementById(s + 'col3area').style.filter = 'alpha(opacity=30)';
        document.getElementById(s + 'col3area').style.opacity = 0.3;
        document.getElementById(s + 'col4area').style.background = '#FFFFFF';
        document.getElementById(s + 'col4area').style.filter = 'alpha(opacity=30)';
        document.getElementById(s + 'col4area').style.opacity = 0.3;
        document.getElementById(s + 'busy').src = '/image/busy.gif';
        document.getElementById(s + 'busy').style.display = '';
    }
    catch (ex) {
        if (iftimeout !== undefined) clearTimeout(timeout);

        alert(ex);

        ifxmlhttpreq = false;
    }
}

function find_link(linkid) {
    var i;

    linkid = '_' + linkid;
    if (ysprods !== undefined) {
        for (i in ysprods) {
            if (ysprods[i]['links'][linkid] !== undefined) {
                return ysprods[i]['links'][linkid];
            }
        }
    }
    if (sites !== undefined) {
        for (i in sites) {
            if (sites[i]['links'][linkid] !== undefined) {
                return sites[i]['links'][linkid];
            }
        }
    }
    if (links !== undefined && links[linkid] !== undefined) {
        return links[linkid];
    }

    return null;
}

function get_if_selected_links() {
    var ysprodlinks, sitelinks;
    var i, j;

    if (ysprods !== undefined) {
        for (i in ysprods) {
            ysprodlinks = ysprods[i]['links'];
            for (j in ysprodlinks) {
                if (document.getElementById('sellink' + j.substring(1)).checked == true) {
                    return true;
                }
            }
        }
    }
    if (sites !== undefined) {
        for (i in sites) {
            sitelinks = sites[i]['links'];
            for (j in sitelinks) {
                if (document.getElementById('sellink' + j.substring(1)).checked == true) {
                    return true;
                }
            }
        }
    }
    if (links !== undefined) {
        for (i in links) {
            if (document.getElementById('sellink' + i.substring(1)).checked == true) {
                return true;
            }
        }
    }

    return false;
}

function get_selected_links() {
    var sellinks, ysprodlinks, sitelinks;
    var i, j;

    sellinks = new Array();
    if (ysprods !== undefined) {
        for (i in ysprods) {
            ysprodlinks = ysprods[i]['links'];
            for (j in ysprodlinks) {
                if (document.getElementById('sellink' + j.substring(1)).checked == true) {
                    sellinks.push(j.substring(1));
                }
            }
        }
    }
    if (sites !== undefined) {
        for (i in sites) {
            sitelinks = sites[i]['links'];
            for (j in sitelinks) {
                if (document.getElementById('sellink' + j.substring(1)).checked == true) {
                    sellinks.push(j.substring(1));
                }
            }
        }
    }
    if (links !== undefined) {
        for (i in links) {
            if (document.getElementById('sellink' + i.substring(1)).checked == true) {
                sellinks.push(i.substring(1));
            }
        }
    }

    return sellinks;
}

// Edit list name
function edit_list() {
    document.getElementById("editlistarea").style.display = "";

    var editlistname = document.getElementById("editlistname");
    editlistname.value = listname;
    editlistname.select();
}


if (document.getElementById("canceleditlist") != null) {
    document.getElementById("canceleditlist").onclick = function() {
        document.getElementById("editlistarea").style.display = "none";
    }
}

if (document.getElementById("editlist") != null) {
    document.getElementById("editlist").onclick = function() {
        var newlistname, xmlhttpreq, timeout;
        var iftimeout;

        if (ifxmlhttpreq == true) {
            alert("Another command is being processed. Please try again later.");
            return;
        }

        ifxmlhttpreq = true;

        try {
            newlistname = document.getElementById("editlistname").value;
            if (newlistname == '') throw 'Please enter a list name.';
            xmlhttpreq = get_xml_http_request();
            if (xmlhttpreq === null) {
                throw ('Your browser does not support this function. Please use ' +
                       'a newer version Internet Explorer or FireFox browser.');
            }
            iftimeout = false;

            timeout = setTimeout(function() {
                iftimeout = true;

                xmlhttpreq.abort();

                alert('There was an error communicating with the server. Please ' +
                      'try again later.');

                document.getElementById('editlisttable').style.background = '#FFFFFF';
                document.getElementById('editlisttable').style.filter = 'alpha(opacity=100)';
                document.getElementById('editlisttable').style.opacity = 1;
                document.getElementById('editlistname').disabled = false;
                document.getElementById('editlist').disabled = false;
                document.getElementById('canceleditlist').disabled = false;
                document.getElementById('editlistbusy').style.display = 'none';

                ifxmlhttpreq = false;
            }, AJAX_TIMEOUT);

            xmlhttpreq.open('POST', '/ajax/editlist', true);

            xmlhttpreq.onreadystatechange = function() {
                var result;

                if (xmlhttpreq.readyState != 4 || iftimeout == true) return;

                clearTimeout(timeout);
                try {
                    if (xmlhttpreq.status != 200)  {
                        throw('There was an error communicating with ' +
                              'the server. Please try again later.');
                    }
                    eval('result = ' + xmlhttpreq.responseText + ';');
                    if (result['errormsg'] !== null) throw result['errormsg'];
                    location = '/my?listid=' + result['listid'];

                    return;
                } catch(ex) {
                    alert(ex);
                }

                document.getElementById('editlisttable').style.background = '#FFFFFF';
                document.getElementById('editlisttable').style.filter = 'alpha(opacity=100)';
                document.getElementById('editlisttable').style.opacity = 1;
                document.getElementById('editlistname').disabled = false;
                document.getElementById('editlist').disabled = false;
                document.getElementById('canceleditlist').disabled = false;
                document.getElementById('editlistbusy').style.display = 'none';

                ifxmlhttpreq = false;
            }

            xmlhttpreq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlhttpreq.send('listid=' + listid + '&listname=' + encodeURIComponent(newlistname));

            document.getElementById('editlisttable').style.background = '#FFFFFF';
            document.getElementById('editlisttable').style.filter = 'alpha(opacity=30)';
            document.getElementById('editlisttable').style.opacity = 0.3;
            document.getElementById('editlistname').disabled = true;
            document.getElementById('editlist').disabled = true;
            document.getElementById('canceleditlist').disabled = true;
            document.getElementById('editlistbusy').style.display = '';
        } catch(ex) {
            if (iftimeout !== undefined) clearTimeout(timeout);

            alert(ex);

            ifxmlhttpreq = false;
        }
    }
}

var sort_top = document.getElementById('sort_top');
var sort_bottom = document.getElementById('sort_bottom');

function find_sort_selectedindex() {
    var sort_list = (sort_top == null)? sort_bottom : sort_top;

    if (sort_list != null) {
        for (var i = 0; i < sort_list.options.length; i++) {
            if (sort_list.options[i].value == sort) return i;
        }
    }

    return -1;
}

if (sort_top != null) {
    sort_top.onchange = function() {
        if (sort_bottom != null) {
            sort_bottom.selectedIndex = sort_top.selectedIndex;
        }

        sort = sort_top.options[sort_top.selectedIndex].value;
        sort_data();
        document.getElementById('dataarea').innerHTML = show_data();
    }
}

if (sort_bottom != null) {
    sort_bottom.onchange = function() {
        if (sort_top != null) {
            sort_top.selectedIndex = sort_bottom.selectedIndex;
        }

        sort = sort_bottom.options[sort_bottom.selectedIndex].value;
        sort_data();
        document.getElementById('dataarea').innerHTML = show_data();
    }
}
