
function dec2hex(n){
    n = parseInt(n); var c = 'ABCDEF';
    var b = n / 16; var r = n % 16; b = b-(r/16); 
    b = ((b>=0) && (b<=9)) ? b : c.charAt(b-10);    
    return ((r>=0) && (r<=9)) ? b+''+r : b+''+c.charAt(r-10);
}

// email obfuscator
jQuery(document).ready(function(){
    jQuery('a.email').each(function(){
        e = this.rel.replace('/','@');
        this.href = 'mailto:' + e;
        jQuery(this).text(e);
    });
});

function ShowWindow(url, width, height) 
{
    LeftPosition = (screen.width)  ? (screen.width-width)/2 : 100;
    TopPosition  = (screen.height) ? (screen.height-height)/2 : 100;
    
    var winPopup = window.open(url,'okienko','width='+width+',height='+height+',top='+TopPosition+',left='+LeftPosition+',resizable=0,scrollbars=yes,menubar=no');
    
    return false;
}

/**
 *  
 */
function AjaxRequest(obj, params, loadingZone, callback, callbackPromptCancel)
{
    if (typeof(loadingZone) == 'string') 
        var jloadingZone = jQuery('#'+loadingZone);
    else
    {
        var jloadingZone = jQuery(loadingZone);
        jloadingZone.hide();
    }
        
	addIcon('loading');
    
    if (!params) params = {'ptype':'ajax'};
    else params['ptype'] = 'ajax';

    if (obj.href != undefined)
        var url = obj.href;
    else
        var url = (obj.action != undefined) ? obj.action : obj;

    function addIcon(type)
    {
        if (typeof(loadingZone) == 'string')
            jloadingZone.html('<img class="'+type+'_icon" src="/gfx/trenerpl/dzien/'+type+'_icon.gif"/>');
        else
            jloadingZone.parent().append('<img class="'+type+'_icon" src="/gfx/trenerpl/dzien/'+type+'_icon.gif"/>');
    }
    
    function removeIcon(type)
    {
        if (typeof(loadingZone) == 'string')
            jloadingZone.html('');
        else
        {
           jloadingZone.parent().children('.'+type+'_icon').remove();
        }
    }

    function AjaxRequestCallback(response)
    {
        response = eval('('+response+')');
   
        if (!response)
        {
            return;
        }
        
        if (response.code == -1)
        {
            var txt = ((response['errorMsg']) ? '<div class="pb5 red justify">'+response['errorMsg']+'</div>' : '')+
                      '<div class="pb10">'+((response['msg']) ? response['msg'] : 'Żądana akcja wymaga zalogowania') + '</div>'+
                      '<div class="fl right pr5 w74 pb5">Login:</div><div class="fl pb5" id="lg"><input type="text" id="login" name="login" value="" /></div>'+
                      '<div class="clear"></div>'+
                      '<div class="fl right pr5 w74 pb5">Hasło:</div><div class="fl pb5"><input type="password" id="password" name="password" value="" /></div>'+
                      '<div class="clear"></div>';
            
            jQuery.prompt(txt,{
                                prefix: 'jqilogin_',
                                buttons: { Zaloguj: true,
                                           Anuluj: false },
                                submit  : function () 
                                          {
                                            params['login']    = jQuery('#login').attr('value');
                                            params['password'] = jQuery('#password').attr('value');
                                            
                                            return true;
                                          },
                                callback: function (clicked, msg)
                                          {
                                            if (clicked)
                                            {
                                                jQuery.post(url, params, AjaxRequestCallback);
                                            }
                                            else
                                            {
												removeIcon('loading');
                                                jQuery(loadingZone).show();
                                                
												if ( callbackPromptCancel )
													callbackPromptCancel(response, params);
                                            }
                                            
                                            return true;
                                          }
                              });
            
            jQuery("#login").focus();

            return;
        }
        
        removeIcon('loading');
        
        if (response.code == -2)
        {
            addIcon('error');
            setTimeout(function () {removeIcon('error');}, 5000);
        }            
        else
        {
            addIcon('ok');
            setTimeout(function () {removeIcon('ok');}, 5000);
        }
		
        if (callback)
            callback(response, params);
    }

    jQuery.post(url, params, AjaxRequestCallback);
    
    return false;
}

function confirmAddFriend(obj, id, nick)
{
    if ( confirm("Czy na pewno dodać znajomego "+nick+"?") )
    {
        return AjaxRequest(obj, {'add_friend':'1', 'du_id':id}, obj);
    }
    
    return false;
}

/*****************************************************************************/

function ShowWindowR(url, width, height) {
                     LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
                     TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
        var Win = window.open(url,'okienko','width='+width+',height='+height+',top='+TopPosition+',left='+LeftPosition+',resizable=yes,scrollbars=yes,menubar=no,top=150,left=150' );
Win.focus();
}
function setCheckboxesRange(the_form, do_check, basename)
{
		if(!document.forms[the_form].elements[basename].length)
			document.forms[the_form].elements[basename].checked = do_check;
		else
        for(i=0;i<document.forms[the_form].elements[basename].length;i++) 
            document.forms[the_form].elements[basename][i].checked = do_check;
        

    return true;
}

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);
} else {
myField.value += myValue;
}
   return true;
}


function requireChecked(the_form, basename , co )
{
		var ile=0;
        for(i=0;i<document.forms[the_form].elements[basename].length;i++) 
        {
//	        alert(basename+"="+i);
            if(document.forms[the_form].elements[basename][i].checked) ile++ ;
    	}
	if(ile==0)
	{
		alert("Wybierz odpowiedź do : "+co);
		return false;
	}
    return true;
}

function checkSonda(the_form)
{
	for(i=0;i<same_nazwy.length;i++) 
	{
		if(same_nazwy[i]!='undefined');
		if(document.forms[the_form].elements[same_nazwy[i]][0].type=='radio' || document.forms[the_form].elements[same_nazwy[i]][0].type=='checkbox' )
		{
			co = requireChecked(the_form, same_nazwy[i], nazwy[same_nazwy[i]])	
			if(co==false)
				return false;
		}
	}
	return true;
}

function writeobject()
{
document.writeln('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="203" height="73" id="vitalia_logo_zielone_indywidualna" align="middle">');
document.writeln('<param name="allowScriptAccess" value="sameDomain" />');
document.writeln('<param name="movie" value="http://files.vitalia.pl/themes/default/vitalia_logo_zielone_indywidualna_new3.swf?clickTag=%2F?mid=4%26fid=4" />');
document.writeln('<param name="quality" value="high" />');
document.writeln('<param name="bgcolor" value="#ffffff" />');
document.writeln('<embed src="http://files.vitalia.pl/themes/default/vitalia_logo_zielone_indywidualna_new3.swf?clickTag=%2F?mid=4%26fid=4" quality="high" bgcolor="#ffffff" width="203" height="73" name="vitalia_logo_zielone_indywidualna" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
document.writeln('</object>');

}

function writeobject_kamis()
{
document.writeln('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="160" height="200" id="160x200" align="middle">');
document.writeln('<param name="allowScriptAccess" value="sameDomain" />');
document.writeln('<param name="movie" value="http://files.vitalia.pl/gfx/kamis/160x200.swf?CLICKTAG=http%3A%2F%2Fwww.vitalia.pl%2Findex.php%2Fmid%2F39%2Ffid%2F221%2Fodchudzanie%2Fdiety%2Fpp_banery_id%2F39%2Fpartner_id%2F32408%2Fpb_id%2F489%2F%26url%3Dhttp%3A%2F%2Fkamis.pl" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />');
document.writeln('<embed src="http://files.vitalia.pl/gfx/kamis/160x200.swf?CLICKTAG=http%3A%2F%2Fwww.vitalia.pl%2Findex.php%2Fmid%2F39%2Ffid%2F221%2Fodchudzanie%2Fdiety%2Fpp_banery_id%2F39%2Fpartner_id%2F32408%2Fpb_id%2F489%2F%26url%3Dhttp%3A%2F%2Fkamis.pl" quality="high" bgcolor="#ffffff" width="160" height="200" name="160x200" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
document.writeln('</object>');
}

function safemail(name, domain, display) {
  displayed=(typeof(display)=="undefined") ? name+"@"+domain : display;
  document.write('<a href=mailto:' + name + '@' + domain + '>' + displayed + '</a>');
}
