/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 3.5.3
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-2006 Jelsoft Enterprises Ltd. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/

var sdtag_posting = 0;
var sdtag_errors_shown = false;
var tagFormShown = false;

function setAddTagForm(threadid)
{
    sdfetch_object("sdtag_form").threadid.value = threadid;
}

function showAddTagForm(e, threadid, slickdealid)
{
    if (tagFormShown == false)
    {
        tagFormShown = true;
        showDiv(e,'addsdtag_div');
        formobj = sdfetch_object("sdtag_form");
        formobj.addsdtag_tagid.value = '';
        formobj.addsdtag_catid.value = '';
        formobj.addsdtag_actag.value = '';
        formobj.threadid.value = threadid;
        formobj.addsdtag_txt.focus();
    }
    else
    {
        tagFormShown = false;
        closeDiv(e, 'addsdtag_div');
    }
//  automenu = sdfetch_object("addsdtag_menu")
//  cloud = sdfetch_object("addsdtag_div");
//  automenu.style.position = "absolute";
//  automenu.style.top = "10px";
//  automenu.style.left = "0px";
//alert(automenu.style.top);
}

function sdtag_prepare_submit(formobj)
{
    if (!AJAX_Compatible)
    {
        // not AJAX compatible do not attempt to use AJAX
        return sdtag_check_data(formobj);
    }
    else if (sdtag_check_data(formobj))
    {
        var test_ajax = new vB_AJAX_Handler(true);
        if (!test_ajax.init() || (typeof vb_disable_ajax != 'undefined' && vb_disable_ajax > 0))
        {
            // couldn't initialize, return true to allow click to go through
            return true;
        }

        if (is_ie && userAgent.indexOf('msie 5.') != -1)
        {
            // IE 5 has problems with non-ASCII characters being returned by
            // AJAX. Don't universally disable it, but if we're going to be sending
            // non-ASCII, let's not use AJAX.
            if (PHP.urlencode(formobj.tag.value).indexOf('%u') != -1)
            {
                return true;
            }
        }

        if (sdtag_posting == 1)
        {
            return false;
        }
        else
        {
            sdtag_posting = 1;
            setTimeout("sdtag_posting = 0", 1000);
        }
		
        if (typeof formobj == "string")
        {
            var submitstring = formobj;
        }
        else if (typeof formobj == "object")
        {
            var submitstring = 'do=tagthread&ajax=1';

            for (i = 0; i < formobj.elements.length; i++)
            {
                var obj = formobj.elements[i];
	
                if (obj.name && !obj.disabled)
                {
                    switch (obj.type)
                    {
                        case 'text':
                        case 'textarea':
                        case 'hidden':
                            submitstring += '&' + obj.name + '=' + PHP.urlencode(obj.value);
                            break;
                        case 'checkbox':
                        case 'radio':
                            submitstring += obj.checked ? '&' + obj.name + '=' + PHP.urlencode(obj.value) : '';
                            break;
                        case 'select':
                            submitstring += '&' + obj.name + '=' + PHP.urlencode(obj.options[obj.selectedIndex].value);
                            break;
                    }
                }
            }
        }

        document.body.style.cursor = 'wait';

        sdtag_ajax_post(submitstring);
        return false;
    }
    else
    {
        return false;
    }
}

/**
* Works with form data to decide what to do
*
* @param	object	<form> object containing quick reply
*
* @return	boolean
*/
function sdtag_check_data(formobj)
{
    if (typeof formobj == "object")
    {
        re = new RegExp("^[a-zA-Z0-9.&,\\-\\s]{0,50}$", 'i');
        if (formobj.addsdtag_txt != null)
            tagval = formobj.addsdtag_txt.value
        else
        {
            var tagin = formobj.filtersdtag_txt;
            if (tagin.value=="Tag" || tagin.value=="Search Tag")
                tagin.value='';
            tagval = tagin.value;
        }
        var tagvalid = ((tagval.match(re) != null && tagval != '') || formobj.addsdtag_catid.value) ? true : false;
        if (!tagvalid)
            sdtag_show_errors("Invalid tag.");
        else
            sdtag_hide_errors();
        return tagvalid;
    }
    return true;
}

/**
* Sends quick reply data to newreply.php via AJAX
*
* @param	string	String representing form data ('x=1&y=2&z=3' etc.)
*/
function sdtag_ajax_post(submitstring)
{
    xml = new vB_AJAX_Handler(true);
    xml.onreadystatechange(sdtag_do_ajax_post);
    xml.send('/forums/sdtag_ajax.php', submitstring);
}

/**
* Handles quick reply data when AJAX says qr_ajax_post() is complete
*/
function sdtag_do_ajax_post()
{
    if (xml.handler.readyState == 4 && xml.handler.status == 200 && xml.handler.responseText)
    {
        document.body.style.cursor = 'auto';
        if (xml.handler.responseText.match(/^<!-- sdtag ok -->/))
        {
            lp = fetch_object('sdtagbits_' + sdfetch_object("sdtag_form").threadid.value);
            lp.innerHTML = xml.handler.responseText;
            fetch_object('addsdtag_txt').value = "";
            fetch_object('addsdtag_tagid').value = "";
            fetch_object('addsdtag_catid').value = "";
            fetch_object('addsdtag_actag').value = "";
            fetch_object('addsdtag_menu').style.display = "none";
            sdtag_hide_errors();

            if (is_ie)
            {
                // in my tests, FF freaks out if I do this abort call here...
                // however, it seems to be necessary (in local tests) for IE
                xml.handler.abort();
            }

            // unfocus the qr_submit button to prevent a space from resubmitting
            if (fetch_object('sdtag_submit'))
            {
                fetch_object('sdtag_submit').blur();
            }
        }
        else
        {
            if (!is_saf)
            {
                // this is the nice error handler, of which Safari makes a mess
                if (xml.handler.responseText.match(/^<!-- sdtag error -->/))
                {
                    sdtag_show_errors(xml.handler.responseText);
                    if (is_ie)
                    {
                        xml.handler.abort();
                    }
                    return;
                }
            }

            // this is the not so nice error handler, which is a fallback in case the previous one doesn't work
            if (is_ie)
            {
                xml.handler.abort();
            }

            fetch_object('sdtag_form').submit();
        }
    }
}

/**
* Un-hides the quick reply errors element
*
* @param	string	Error(s) to show
*
* @return	boolean	false
*/
function sdtag_show_errors(errortext)
{
    sdtag_errors_shown = true;
    fetch_object("sdtag_err").innerHTML = errortext;

    return false;
}

/**
* Hides the quick reply errors element
*
* @return	boolean	false
*/
function sdtag_hide_errors()
{
    if (sdtag_errors_shown)
    {
        sdtag_errors_shown = true;
        fetch_object("sdtag_err").innerHTML = "";

        return false;
    }
}

function sdtag_confirmdel(tag)
{
    return confirm('Are you sure you want to delete '+tag+'?\nClick OK to delete, or cancel to hide this prompt.');
}
/*======================================================================*\
|| ####################################################################
|| # Downloaded: 05:54, Mon Feb 13th 2006
|| # CVS: $RCSfile: vbulletin_quick_reply.js,v $ - $Revision: 1.19.2.1 $
|| ####################################################################
\*======================================================================*/

