
function bd_showpersonalmsg(id,iv)
{
    $(id).style.display = "block";
    $(iv).style.display = "none";

    if (typeof event != 'undefined')
    {
        event.returnValue=false;
    }
    
    return false;
};

function bd_feed_delete(id,md5,ask)
{
    var result = true;
    var extra  = '';

    // Delete video or photo
    if (ask == 1)
    {
        result = confirm("Are you sure you wish to delete this?");
    }

    if (result == true)
    {
        // Remove possible youtube embed to stop playing if user has played and deletes while it is still playing
        if (typeof $('myimage' + id) != 'undefined')
        {
            setTimeout("$('myimage" + id + "').innerHTML = '';", 1200);
        }
        
        // Slide
        if (typeof $('feed_' + id) != 'undefined')
        {
            new Effect.SlideUp('feed_' + id, {duration:1.2});
        }
        
        // Call DOM to initiate delete of element
        bd_loadxmldoc("/bodydot/feeds/deletepost?id=" + id + "&md5=" + md5,"","","");
    }

    if (typeof event != 'undefined')
    {
        event.returnValue=false;
    }
    
    return false;
};

function bd_get_cookie(fname)
{
    var bikky = document.cookie;
    var index = bikky.indexOf(fname + "=");

    if (index == -1) { return null; }
    
    index = bikky.indexOf("=", index) + 1;
    
    var endstr = bikky.indexOf(";", index);

    if (endstr == -1) endstr = bikky.length;
    {
        return unescape(bikky.substring(index, endstr));
    }
};

function bd_set_cookie(fname, fvalue, hours)
{
    var today  = new Date();
    var expiry = new Date(today.getTime() + hours * 60 * 60 * 1000);

    if (fvalue != null && fvalue != "")
    {
        document.cookie = fname + "=" + escape(fvalue) + "; expires=" + expiry.toGMTString();
    }
};

function bd_getiframe(fname)
{
    var IFrameObj = document.getElementsByName(fname)[0];
    
    if (IFrameObj)
    {
        if (IFrameObj.contentDocument)
        {
            // For NS6
            return IFrameObj.contentDocument;
        } 
        else if (IFrameObj.contentWindow) 
        {
            // For IE5.5 and IE6
            return IFrameObj.contentWindow.document;
        } 
        else if (IFrameObj.document) 
        {
            // For IE5
            return document.frames(fname).document;
        }
    }
};

function bd_getobj(fname,fwhich)
{
    if (!fwhich) { fwhich = document; }

    if (fwhich.getElementById) 
    {
        return fwhich.getElementById(fname);
    }
    else if (fwhich.all)
    {
        return fwhich.all[fname];
    }
    else if (fwhich.layers)
    {
        return fwhich.layers[fname];
    }
};

function bd_loadxmldoc(url,callback,objid,message)
{
    var req;
    
	req = false;
	
	if (objid && message)
	{
	   bd_getobj(objid).innerHTML = message;
    }
	
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest && !(window.ActiveXObject))
	{
    	try
		{
			req = new XMLHttpRequest();
        }
		catch (e)
		{
			req = false;
        }
    }
	else if (window.ActiveXObject)
	{
       	try
		{
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	}
		catch (e)
		{
        	try
			{
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	}
			catch (e)
			{
          		req = false;
        	}
		}
    }
    
	if (req)
	{
		req.onreadystatechange = function()
		{ 
            if (req.readyState == 4)
        	{
                if (req.status == 200)
        		{
        		    if (callback)
        		    {
                        callback (req.responseText,objid);
                    }
                    else
                    {
                        if (objid)
                        {
                            bd_getobj(objid).innerHTML = req.responseText;
                        }
                    }
                }
        		else
        		{
                    // Failed request        		    
                    // alert("There was a problem retrieving the XML data:\n" + req.statusText);
                }
            }
        };
		
		req.open("POST", url, true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send("");
	}
};

function bd_setselected(id,value)
{
    if (value == '') { return; }
    
	box = bd_getobj(id);
	
	if (box)
	{
    	if (box.options)
    	{
    		for (z = 0;z < box.options.length;z++)
    		{
    			if ((box.options[z].value == value) || ((box.options[z].value == '') && (box.options[z].text == value)))      
    			{
    				box.selectedIndex = z;
    				
    				return;
    			}
    		}
    	}
    	else 
        {
            box.value = value;
        }
    }
};

function bd_set_radiocheckbox(name,id,type)
{
    var radios = document.getElementsByTagName('input');

    for (var i = 0; i < radios.length; i++) 
    {
        var input = radios[i];
           
        if (input.type == type && input.name == name) 
        {               
            if (input.value == id)
            {
                input.checked = true;
            }
            else
            {
                input.checked = false;
            }
        }
    }
};

function bd_addcommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) 
    {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
};

function bd_geturlvalue(name,defaultvalue)
{  
    name        = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS  = "[\\?&]"+name+"=([^&#]*)";
    var regex   = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    
    if (results == null)
    {   
        return defaultvalue;
    }
    else   
    {
        return results[1];
    }
};

function bd_delpv(divparent,divnum,ask)
{
    var result = true;
    var extra  = '';
    
    // Delete video or photo
    if (ask != 1)
    {
        result = confirm("Are you sure you wish to delete this?");
        extra  = '&deletemysql=1';
    }
    
    if (result == true)
    {
        // Delete element
        if (divparent == 'bd_failedboxes')
        {
            // Deleting failed elements slide up
            new Effect.SlideUp('image_' + divnum, {duration:0.7});
        }
        else
        {
            var d = document.getElementById(divparent);
            var olddiv = document.getElementById('image_' + divnum);
            d.removeChild(olddiv);
        }
        
        // Call DOM to initiate delete of element
        bd_loadxmldoc("/bodydot/actions/deletecontent?id=" + divnum + extra,"","","");
    }
    
    if (typeof event != 'undefined')
    {
        event.returnValue=false;
    }
    
    return false;
};

function bd_deleteemail(id)
{
    // Slide up effect
    new Effect.SlideUp('emh_' + id, {duration:0.7});

    // Delete it
    bd_loadxmldoc('/bodydot/emails/delete?id=' + id);
    
    if (typeof event != 'undefined')
    {
        event.returnValue=false;
    }
    
    return false;
};

function bd_deleteallcheckboxed()
{
    var textareas = document.getElementsByTagName('input');

    for (var i = 0; i < textareas.length; i++)
    {
        var input = textareas[i];

        if ((input.id.search('chkbx_') != -1) && (input.checked == true))
        {
            bd_deleteemail(input.value);
        }
    }
};

function bd_selectallcheckboxed(x)
{
    var textareas = document.getElementsByTagName('input');

    for (var i = 0; i < textareas.length; i++)
    {
        var input = textareas[i];

        if (input.id.search('chkbx_') != -1)
        {
            input.checked = x;
        }
    }
};

function bd_set_checked_radiobutton(radioObj, newValue)
{
    // bd_set_checked_radiobutton(document.myform.radiobutton, "a2");
	if (!radioObj)
	{
		return;
	}
	
	var radioLength = radioObj.length;
		
	if (radioLength == undefined)
    {
		radioObj.checked = (radioObj.value == newValue.toString());
		
		return;
	}

	for (var i = 0; i < radioLength; i++)
    {
		radioObj[i].checked = false;

		if (radioObj[i].value == newValue.toString())
        {
			radioObj[i].checked = true;
		}
	}
};

function bd_set_checkedvalue(objname,value)
{
    var a = bd_getobj(objname);
    
    if (a != undefined)
    {
        a.checked = value;
    }
};

function bd_get_checkedvalue(radioObj)
{
	if (!radioObj)
	{
		return "";
	}
	
	var radioLength = radioObj.length;
	
	if (radioLength == undefined)
	{
		if (radioObj.checked)
		{
			return radioObj.value;
		}
		else
		{
			return "";
		}
	}
	
	for (var i = 0; i < radioLength; i++)
    {
		if (radioObj[i].checked)
        {
			return radioObj[i].value;
		}
	}
	
	return "";
};

function bd_set_radiocheckbox(name,id,type)
{
    var radios = document.getElementsByTagName('input');

    for (var i = 0; i < radios.length; i++) 
    {
        var input = radios[ i ];

        if (input.type == type && input.name == name) 
        {               
            if (input.value == id)
            {
                input.checked = true;
            }
            else
            {
                input.checked = false;
            }
        }
    }
};

function bd_clipboard(name)
{
	var inname = bd_getobj(name);

	// Copy to clipboard
    Copied = inname.createTextRange();
    Copied.execCommand("Copy");

	// Select text in case he wants to do it himself
	inname.focus();
	inname.select();
};

function bd_expanddescription(a)
{
    bd_getobj('bd_moredesc_' + a).style.display = "block";
    bd_getobj('bd_moretag_' + a).style.display  = "none";
};

var comment_slide_dir = 1;
var comment_captchaloaded = false;

function bd_expandleavecomment()
{ 
    // Expand leave comment boxes
    if (comment_slide_dir  == 1)
    {
        new Effect.SlideDown('bd_leavecommentbox0', {duration:0.7});

        // Load captcha now rather than at page load to ease load on server
        if (comment_captchaloaded == false)
        {
            bd_getobj('captcha').src = "/captcha/image?x=170&y=35&p0=20&p1=24";
            
            comment_captchaloaded = true;
        }
        
        // When it expands, set focus to textarea
        setTimeout("bd_getobj('textarea').focus();",750);
    }
    else
    {
        // Close 'er up
        new Effect.SlideUp('bd_leavecommentbox0', {duration:0.7});
    }
    
    comment_slide_dir = 1 - comment_slide_dir;
        
    return false;
};

var bd_localemailsenddir = 1;

function bd_localemailsendshow()
{
    if (bd_localemailsenddir == 1)
    {
        new Effect.SlideDown('bd_emailbox', {duration:0.7});
    }
    else
    {
        // Close 'er up
        new Effect.SlideUp('bd_emailbox', {duration:0.7});
    }
    
    bd_localemailsenddir = 1 - bd_localemailsenddir;
};

// private_get_comments
function cm_del(type,gid,id,ask,target)
{
    if (ask == true)
    {
        var cn;
        
        if ((cn = confirm('Are you sure you wish to delete this?')) == true)
        {
            if (typeof event != 'undefined')
            {
                event.returnValue = cn;
            }

            return false;
        }
    }
    
    // Slide up the comment
    new Effect.SlideUp('c' + id, {duration:0.75});
    
    if (typeof target == 'undefined')
    {
        target = 'delete';
    }

    // Delete the comment
    // alert('/bodydot/comments/comment' + type + target + '?gid=' + gid + '&id=' + id);
    bd_loadxmldoc('/bodydot/comments/comment' + type + target + '?gid=' + gid + '&id=' + id,'','');
    
    if (typeof event != 'undefined')
    {
        event.returnValue = false;
    }

    return false;
}

// shallow feed comments
function bd_shallowcomment_show(responseText,objid)
{
    if (objid)
    {
        if (bd_getobj(objid))
        {
            bd_getobj(objid).innerHTML = responseText;
        }
    }
}

// Session keep alive
var bd_pingtimeout = 300000; // 5 mins

function bd_pingserver()
{
    bd_loadxmldoc('/images/js/ping.php?' + Math.random());
    setTimeout('bd_pingserver()', bd_pingtimeout);
}

setTimeout('bd_pingserver()', bd_pingtimeout);

// Youtube play
function bd_youtubeembed(id,youtubeid)
{
    swfobject.embedSWF("http://www.youtube.com/v/" + youtubeid + "?autoplay=1", 'myyt' + id, "100%", "100%", "8", null, null, null, null);
}

function bd_youtubeplay(id,youtubeid)
{
    new Effect.Morph('myimage' + id, { style: 'height:340px;',duration: 1.2 } );
    new Effect.Morph('myimage' + id, { style: 'width:480px;',duration: 1.2 } );

    new Effect.Morph('imgyt' + id, { style: 'height:340px;',duration: 1.2 } );
    new Effect.Morph('imgyt' + id, { style: 'width:480px;',duration: 1.2 } );
    new Effect.Fade('imgyt' + id, { duration: 1.4 });
    
    setTimeout("bd_youtubeembed('" + id + "','" + youtubeid + "')", 1000);
}

