function getAjaxObject() {
    var xmlHttp;

    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch(e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}

// Takes an action name and returns something of the form: /action/<action_name>?key1=value1&key2=value2...
function getActionUrl(action_name, params) {
    var url = '/action/' + action_name + '?';
    for (var word in params) {
        url += word + '=' + params[word] + '&';
    }
    return url;
}

function isAjaxSuccess(ajax) {
    if (ajax.readyState == 4) return true;
    return false;
}

function loadMore(start_num, tourstyle, action, list_type, sort_by) {
    var ajax = getAjaxObject();
    ajax.onreadystatechange = function() {
        if (isAjaxSuccess(ajax)) {
            var id = '#loadMore-' + list_type + '-' + start_num;
            $(id).replaceWith(ajax.responseText);
        }
    }

    var params = new Array();
    params['start_num'] = start_num;
    params['list_type'] = list_type;
    params['sort_by'] = sort_by;
    params['favs_type'] = list_type;

    tourstyle = tourstyle.replace(/\//g, '');
    if (tourstyle != '') {
        tourstyle = '/' + tourstyle;
    }

    ajax.open("GET", tourstyle + getActionUrl(action, params));
    ajax.send(null);
}

function lostPassword(action) {
    var email = document.getElementById('lostPasswordEmail').value;

    var ajax = getAjaxObject();
    ajax.onreadystatechange = function() {
        if (isAjaxSuccess(ajax)) {
            var success = parseInt(ajax.responseText);

            if (success == 1) {
                alert("We've sent you an e-mail with your username and password.");
            }
            else {
                alert("Sorry, we couldn't find your account.  Double-check your e-mail address and try again.");
            }
        }
    }

    var params = new Array();
    params['email'] = email;

    ajax.open('GET', getActionUrl('lost-password', params));
    ajax.send(null);
}

function adjustFavorite(member, not_member_link, id, type, xsum, add_or_remove) {
    if (member == '0') {
        window.location = not_member_link;
        return;
    }

    var ajax = getAjaxObject();
    ajax.onreadystatechange = function() {
        if (isAjaxSuccess(ajax)) {
            var success = parseInt(ajax.responseText);
            var obj = document.getElementById('fav' + type + ':' + id);

            if (success == 1) {
                if (parseInt(add_or_remove) == 1) {

                    obj.className = obj.className.replace(/favAddButton/, 'favRemButton');
                    obj.className = obj.className.replace(/Add/gi, 'Remove');
                    obj.innerHTML = obj.innerHTML.replace(/Add/i, 'Remove');
                    obj.innerHTML = obj.innerHTML.replace(/to/i, 'from');


                    var txt = obj.href;
                    obj.href = txt.replace(/'1'\)$/, "'0')");
                    alert('Added to favorites!');
                }
                else {

                    obj.className = obj.className.replace(/favRemButton/, 'favAddButton');    
                    obj.className = obj.className.replace(/Remove/g, 'Add');
                    obj.innerHTML = obj.innerHTML.replace(/Remove/i, 'Add');
                    obj.innerHTML = obj.innerHTML.replace(/from/i, 'to');
                    var txt = obj.href;
                    obj.href = txt.replace(/'0'\);?$/, "'1')");
                    alert('Removed from favorites.');
                }
            }
            else {
                alert('Done.');
            }
        }
    }

    var params = new Array();
    params['id'] = id;
    params['type'] = type;
    params['xsum'] = xsum;
    params['add'] = add_or_remove;

    ajax.open("GET", getActionUrl('adjust_favorite', params));
    ajax.send(null);
}


