function checkAll(id)
{
	inputs = $(id).getElementsByTagName('input');
	inputs = $A(inputs);

	inputs.each(function(input) {
		if (input.type == 'checkbox')
		{
			input.checked = true
		}
	});
	
	return false;
}


function uncheckAll(id)
{
	inputs = $(id).getElementsByTagName('input');
	inputs = $A(inputs);

	inputs.each(function(input) {
		if (input.type == 'checkbox')
		{
			input.checked = false
		}
	});
	
	return false;
}


function windowOpen(url, title)
{
    window.open(url, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, height=600, width=800'); 
    return false;    
}


function roller(id)
{
    var roller = $('profileRoller' + id);
    var element = $('profileData' + id);
    
    if (roller.className == 'roll')
    {
        element.hide();
        roller.className = 'unroll';
        roller.innerHTML = 'pokaż informacje o autorze';
        setCookie('hideProfile', 1);
    }
    else
    {
        element.show();
        roller.className = 'roll';
        roller.innerHTML = 'ukryj informacje o autorze';
        setCookie('hideProfile', '');
    }
    
    return false;
}

function rollerGroup(id)
{
    var roller = $('profileRoller' + id);
    var element = $('profileData' + id);
    
    if (roller.className == 'roll')
    {
        element.hide();
        roller.className = 'unroll';
        roller.innerHTML = 'pokaż informacje o grupie';
        setCookie('hideGroupProfile', 1);
    }
    else
    {
        element.show();
        roller.className = 'roll';
        roller.innerHTML = 'ukryj informacje o grupie';
        setCookie('hideGroupProfile', '');
    }
    
    return false;
}


function rollerGroupModeration(id)
{
    var roller = $('groupRoller' + id);
    var element = $('opt' + id);
    
    if (roller.className == 'moderationDownArrow')
    {
        element.hide();
        roller.className = 'moderationTopArrow';
        setCookie('hideGroupProfile'+id, 1);
    }
    else
    {
        element.show();
        roller.className = 'moderationDownArrow';
        setCookie('hideGroupProfile'+id, '');
    }
    
    return false;
}


function insertEmoticon(icon)
{
    insertAtCursor($('content'), icon);
}


function quotation(number)
{
    q = '[q]' + $('comment' + number).innerHTML +'[/q]';
    q = q.replace('<span class="quotation">', '[q]');
    q = q.replace('</span>', '[/q]');
    q = q.replace(/<br\s*\/?>/g, '');
    q = q.replace(/<img\s*src="[^"]*"\s*alt="([^"]*)"[^>]*>/g, "$1");
    insertAtCursor($('content'), q);
    return false;
}


function insertAtCursor(myField, myValue) {
    //IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);	
		
		myField.setSelectionRange(parseInt(startPos) + parseInt(myValue.length), parseInt(startPos) + parseInt(myValue.length));
	} 
	else {
		myField.value += myValue;
	}
	
	myField.focus();
}


function setCookie(name, value){
    document.cookie = name+"="+value+';path=/;';
}


function updateVipExpire(role)
{
    if (role == 'vip')
    {
       var date = new Date();
       var reg = /^\d\d\d\d-\d\d-\d\d$/;
              
       if (!$('vip_expire').value.match(reg)) {
           if (parseInt($('vip_expire').value)) {
             alert(parseInt($('vip_expire').value));
             date.setTime(date.getTime() + (parseInt($('vip_expire').value) * 1000));
           } else {
               date.setDate(date.getDate() + 90);
           }
       
           month = date.getMonth() + 1;
           month = month.toString();
           
           if (month.length == 1)
           {
               month = '0' + month;
           }
           $('vip_expire').value = date.getFullYear() + '-' + month + '-' + date.getDate();
       }
       
       $('vipExpireBlock').show();
    }
    else
    {
       $('vipExpireBlock').hide();
    }
}

function userTooltip(id, side)
{
    var html = '<div id="userTooltip' + id + '" class="profileTooltip"><div class="profileBlock ' + (side ? 'left' : 'right') + '"><div class="data"><img src="/img/loading50x50.gif" width="50" height=50" alt="trwa wczytywanie..." /></div></div></div>';
    
    new Ajax.Request(side ? '/user-tooltip-left' : '/user-tooltip-right',
  	{
		asynchronous: true,
   		method: 'post',
   		parameters: 'id=' + id,
   		onComplete: function(obj) 
   		{
   		    $('userTooltip' + id).innerHTML = obj.responseText;
   		}
   	});
   	
   	return html;
}
