/** Klasa obslugiujaca zakladki*/
ToolsMenu = function(id) {
	this.objtab = document.getElementById(id);
    this.objtabcontent = document.getElementById(id+'content');
    this.objtabimportantinformation = document.getElementById(id+'importantinformation');
    //this.objtabcontent.style.display = 'block';
    this.setSelected = function (id) {
    	var tab = document.getElementById(id);
    	var tabcontent = document.getElementById(id+'content');
    	var tabimportantinformation = document.getElementById(id+'importantinformation');
        var obj = document.getElementById('logo');

        if (is(tab) && is(tabcontent) && is(obj)) {
            switch (id) {
        		case 'tab1':
        			obj.className='pk_logo';
        			break;
        		case 'tab2':
        			obj.className='webas_logo';
        			break;
        		case 'tab3':
        			obj.className='webmail_logo';
        			break;
        		case 'tab5':
        			obj.className='domeny_logo';
        			break;
                default:
                    obj.className='pk_logo';
                    break;
        	}
            if (tabcontent.style.display == 'none') {
            	tabcontent.style.display = 'inline';
            	tabimportantinformation.style.display = 'inline';
            }
            if (tab.className != 'selected') {
            	// switch selected tab to normal
            	this.objtab.className = '';
            	this.objtabcontent.style.display = 'none';
            	this.objtabimportantinformation.style.display = 'none';
            	// rewrite selected tab to the one selected
            	this.objtab = tab;
            	this.objtabcontent = tabcontent;
            	this.objtabimportantinformation = tabimportantinformation;
            	// set proper values
				this.objtab.className = 'selected';
            	this.objtabcontent.style.display = 'inline';
            	this.objtabimportantinformation.style.display = 'inline';
            }
        }
    }
}

function tabClass(obj, c){
    if(obj.parentNode.className != 'selected'){
        obj.parentNode.className = c;
    }
}

function changeLocation(address, newWindow) {
	if(newWindow){
        var oWindow = window.open(address, '_blank');
        if(oWindow.focus) {
            oWindow.focus();
        }        
    }else{
        if(is(window.location)) {
            window.location.href = address;
        } else {
            window.src = address;
        }        
    }
}

function openWindow(link, w, h){
    var oWindow = window.open(link+'&w='+w+'&h='+(h), null, 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+w+',height='+h+',left=200' ) ;
    if(oWindow.focus) {
        oWindow.focus();
    }
}

function switchCheckbox(id){
    var c = document.getElementById(id);
    if(is(c)){
        c.checked = !c.checked;
    }
}

function selectRadio(id){
    var c = document.getElementById(id);
    if(is(c) && !c.checked){
        c.checked = true;
    }
}

/**
 * Funkcja resizujaca okienko do wlasciwych rozmiarow.
 * @param {Object} width
 * @param {Object} height
 */
function setWindowSize(width,height) {
    if (window.outerWidth) {
		window.outerWidth = width;
		window.outerHeight = height;
	}	else if (window.resizeTo) {
		window.resizeTo(width,height);
	}	else {
		return;
	}
}

/**
 * otwieranie okna 
 * @param {Object} url
 * @param {Object} x
 * @param {Object} y
 * @param {Object} w
 * @param {Object} h
 * @param {Object} name
 */
function new_window(url,x,y,w,h,name) {
	parametry='scrollbars=1 ,location=no, status=yes,resizable=yes, toolbar=no, top=' + y + ', left=' + x + ', width=' + w + ', height='+h;
	window.open(url,name,parametry);
}

/**
 * trimowanie stringow
 * @param {Object} inputString
 */
function trim(inputString) {                                                                                     
    return inputString.replace(/^\s+|\s+$/g,"");                                                
 }

/**
 * zaokraglanie
 * @param {Object} x
 * @param {Object} places
 */
function custRound(x,places) {
    // Created 1997 by Brian Risk.  http://members.aol.com/brianrisk
    return (Math.round(x*Math.pow(10,places)))/Math.pow(10,places)
}

/**
 * pobranie funkcji onclick z obiektu
 * @param {Object} id
 */
function getOnClick(id){
    var o=document.getElementById(id);
    return o!=null?o.onclick:null
}


/**
 * Funkcje liczace absolutne polozenie elementu
 * @param {Object} obj
 */
getLeft = function(obj){
    var o =  isString(obj)  ? document.getElementById( obj ) : obj;
    var val = 0;
    while ( isString(o.nodeName) &&  o.nodeName.toLowerCase() != "body" ) {
        
        val += parseInt( o.offsetLeft );
        o = o.offsetParent;
    }
    return val;
}

getTop = function(obj){
    var o = isString(obj)  ? document.getElementById( obj ) : obj;
    var val = 0;
    while ( isString(o.nodeName) && o.nodeName.toLowerCase() != "body" ) {
        val += parseInt( o.offsetTop );
        o = o.offsetParent;
    }
    return val;
}

/**
 * zaznaczanie opcji w select
 * @param {Object} s obiekt Select
 * @param {Object} v wartosc do zaznaczenia
 */
function setSelect(s,v){for (var i=0;i<s.options.length;i++){if(s.options[i].value==v){s.selectedIndex=i;return;}}}
/**
 * przelacznik checkbox w formularzu
 * @param string/object f formularz
 * @param string on flaga on/off
 */
function selectAll(f,on){	
	if (typeof(f)=="undefined" || typeof(f.elements)=="undefined"){f=eval('document.'+f)}
	for (var i=0,j=f.length;i<j;i++){
        if (f.elements[i].type=='checkbox' && (arguments.length==2||f.elements[i].name==arguments[2])){
            f.elements[i].checked=(on=="on"?true:false)
        }
    }
}
/**
 * testowanie czy cokolwiek zaznaczono w formularzu
 * @param {Object} f
 */
function isChecked(f){	
	for (var i=0,l=f.length;i<l;i++){
		if (f.elements[i].type=="checkbox") {
			if (arguments.length==1 || (arguments.length>1 && f.elements[i].name==arguments[1])) {
				if (f.elements[i].checked){
					return true
				}
			}
		}
	}
	return false;
}

/**
 * testowanie czy cokolwiek zaznaczono w formularzu
 * @param {Object} f
 */
function isCheckedByClass(f){	
	for (var i=0,l=f.length;i<l;i++){
		if (f.elements[i].type=="checkbox") {
			if (arguments.length==1 || (arguments.length>1 && f.elements[i].className==arguments[1])) {
				if (f.elements[i].checked){
					return true
				}
			}
		}
	}
	return false;
}

/**
 * testowanie czy checkbox o zadanym id jest zazanaczony
 * @param {Object} id
 */
function checkField(id) {
	var field=document.getElementById(id);
	field.checked=(field.type!='radio'&&field.checked ? false : true);
}

/**
 * czyszczenie zaznaczenie checkboxow
 * @param {Object} f
 * @param {Object} n
 */
function clearChecked(f,n) {
	for (var i=0,l=f.length;i<l;i++){
		if (f.elements[i].type=="checkbox") {
			if ( (arguments.length==2 && f.elements[i].name!=n) ||  (arguments.length==3 && f.elements[i].name==n)){
				f.elements[i].checked=false;				
			}
		}
	}	
}

/**
 * czyszczenie checkboxow wg klas
 * @param {Object} f
 * @param {Object} classname
 */
function clearCheckedByClass(f,classname) {
	for (var i=0,l=f.length;i<l;i++){
		if (f.elements[i].type=="checkbox") {
			if ( (arguments.length==2 && f.elements[i].className!=classname) || (arguments.length==3 && f.elements[i].className==classname)){
				f.elements[i].checked=false;				
			}
		}
	}	
}
/**
 * pobieranie pola formularza po nazwie (niestandardowej)
 * @param {Object} container
 * @param {Object} tag
 * @param {Object} name
 */
function getFieldByName(form,name) {
	for ( var i=0,l=form.elements.length;i<l;i++) {
		if (form.elements[i].name == name) {
			return form.elements[i];
		}
	}
	return null;

}
function displayReverse(container,tag,name) {
	var lista=container.getElementsByTagName(tag);
	if (isArray(lista)) {
		for(var i=1,l=lista.length;i<l;i++){			
			if (lista[i].getAttribute('name')==name){
				lista[i].style.display=(lista[i].style.display=='none'?'':'none');
			}
		}		
	}
}

/**
 * doklejenie kawalka do akcji formularza
 * @param {Objec}
 * @param {Object} str
 */
function addActionString(form,str){
    if (form.action=='' || form.action=='?'){
        form.action='?'+str;
    } else {
        form.action+='&'+str;
    }
}

function go(url){
    document.location.href=url;
}

/**
 * warunkowo przejscie na url
 */
function goif(msg,url){
    if (window.confirm(msg)===true) {
       go(url);
    }
}

/**
 * Funkcja wyświetla lub chowa warstwę w zależności
 * od stanu checboxa
 * @param object obj obiekt checkbox'a
 * @param string id identyfikator ukrywanej warstwy 
 */
function switchCheckboxAction(obj,id) {
    e=document.getElementById(id);
    if (obj.checked) {
        // IEFIX: poprawnie tu powinno być e.style.display='table'
        e.style.display=''
    } else {
        e.style.display='none'
    }
}

/**
 * otwieranie okna na podglad faktur
 * @param {Object} url
 * @param {Object} x
 * @param {Object} y
 * @param {Object} w
 * @param {Object} h
 * @param {Object} name
 */
function faktura(url,x,y,w,h,name) {
	parametry='scrollbars=1 ,location=no, status=yes,resizable=yes,menubar=yes, toolbar=no, top=' + y + ', left=' + x + ', width=' + w + ', height='+h;
	window.open(url,name,parametry);
}

/**
 * Wysyla wybrane adresy email do usuniecia
 * @param {Event} e 
 * @param {FormElement} f
 */
function deleteSelectedEmails(e, f, q){
    var isFound = false;
    for(var i = 0; i < f.elements.length; i++){
        if(f.elements[i].nodeName.toLowerCase() == 'input' && f.elements[i].type.toLowerCase() == 'checkbox' && f.elements[i].checked){
            isFound = true;
            break;
        }
    }

    //jkelsi zaden czekboks nei zaznaczony to koncze dzialanie
    if(!isFound){
        return false;
    }
    
    if(window.confirm(q)){
        f.submit();
        return true;
    }else{
        return false;
    }
    
}

function selectAllEmails(e, c, f){
    var selectedCounter = 0;
    var unselectedCounter = 0;
    for(var i = 0; i < f.elements.length; i++){
        if(f.elements[i].nodeName.toLowerCase() == 'input' && f.elements[i].type.toLowerCase() == 'checkbox' ){
            if(f.elements[i].checked){
                selectedCounter++;
            }else{
                unselectedCounter++;
            }
        }
    }
    
    if(selectedCounter==0 && unselectedCounter==0){
        return false;
    }
    
    var check = true;
    if(selectedCounter > unselectedCounter){
        check = false;
    }

    for(var i = 0; i < f.elements.length; i++){
        if(f.elements[i].nodeName.toLowerCase() == 'input' && f.elements[i].type.toLowerCase() == 'checkbox' ){
            f.elements[i].checked = check;
        }
    }
    c.checked = check;
    return true;
    
}
/**
 * Zatrzymuje babelkowanie czyli propagowanie JS zdarzen 
 * @param {Event} event
 */
function stopEventPropagation(event){
    if (event.stopPropagation) {
        event.stopPropagation();
    } else {
        //IE
        event.cancelBubble = true;
    }
}


/**
 * Ustawia ciastko w przegladarce
 * @param {String} name
 * @param {String} value
 * @param {int} days
 */
function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/**
 * Pobiera wartosc ciastka.
 * @param {String} name
 */
function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

/**
 * Kasuje ciastko.
 * @param {String} name
 */
function deleteCookie(name) {
	createCookie(name,"",-1);
}

function orderAuthChange(e, obj){
    var rg = /^([^@]*)@([^@]*)$/mi;
    var found = obj.value.match(rg);
    var auth_id_opcja = document.getElementById('auth_id_opcja');
    var auth_typ_uslugi = document.getElementById('auth_typ_uslugi');

    if(is(auth_id_opcja) && is(auth_typ_uslugi) && is(found) &&found.length == 3){
        auth_id_opcja.value = found[1];
        auth_typ_uslugi.value = found[2];
    }
}

/**
 * Funkcja pokazujaca i ukrywajaca formularz do odzyskiwania hasla
 * @param {Object} show
 */
function showRecoverPassForm(show){
    var tab1 = document.getElementById('tab1content');
    var tab4 = document.getElementById('tab4content');
    if(is(tab1) && is(tab4)){
        if(show){
            tab1.style.display = 'none';
            tab4.style.display = ''; 
        }else{
            tab1.style.display = '';
            tab4.style.display = 'none';            
        }
    }
}

function hideRecoverPassForm(){
    var tab4 = document.getElementById('tab4content');
    if(is(tab4)){
        tab4.style.display = 'none'; 
    }
}

/**
 * Funkcja uzywana tylko na czas testow!
 */
function rRecoverChange(){
    var rr1 = document.getElementById('rr1');
    var rr2 = document.getElementById('rr2');
    var rr1form = document.getElementById('recoverform');
    var rr2form = document.getElementById('recoverform_test');
    if(is(rr1) && is(rr2) && is(rr1form) && is(rr2form)){
        if(rr1.checked){
            rr1form.style.display = '';
            rr2form.style.display = 'none';
        }else{
            rr1form.style.display = 'none';
            rr2form.style.display = '';
        }
    }
}

/**
 * Funkcja dynamicznie przydziela obslude zdarzen tabom.
 * Wywolywana jest po zaladowaniu dokumentu (onload)
 */
function assignTabEvent(){
    var lista = document.getElementById('tabArray');
    if(!is(lista)){
        return false;
    }
    var rgxp = /^selected/i;
    for(var i = 0; i < lista.childNodes.length; i++){
        //szukam wszystkich elementow a umieszczonych bezposrednio w elementach listy tabArray
        '//takim znalezionym elementom generuje dynamicznie obsluge onmouseover i onmouseout'
        if(lista.childNodes[i].nodeType == 1 && lista.childNodes[i].nodeName.toLowerCase() == 'li' && is(lista.childNodes[i].firstChild) && lista.childNodes[i].firstChild.nodeName.toLowerCase() == 'a'){
            if(lista.childNodes[i].className.toLowerCase() != 'selected'){
                lista.childNodes[i].firstChild.onmouseover = function(){
                    tabClass(this, 'hover');
                }
                lista.childNodes[i].firstChild.onmouseout = function(){
                    tabClass(this,  '');
                }
            }
        }
    }
}

/**
 * Funkcja na podstawie przekazanego id elementu, wyluskuje identyfikator grupy tabow.
 * Tab sklada sie z 3 elementow:
 * tabsXXleft, tabsXXmiddle oraz tabsXXright
 * Identyfikatorem grupowym bedzie czesc wspolna czyli: tabsXX
 * gdzie XX to jakas liczba
 * 
 * @param {String} id identyfikator elementu
 */
function getIdTab(id){
    var rgxp = /^tabs(\d)*/i;
    var found = null;
    found = id.toLowerCase().match(rgxp);
    if(is(found)){
        return found[0];
    }else{
        return false;
    }
}
/**
 * Metoda wysylajaca zadanie ustawieniaa adresu email jako podstawowego
 * @param {Object} email_id
 */
function setPrimaryAtuhEmail(obj, email){
    if(document.forms['primary_email_form'].elements['primary_email'].value != obj.value){
        if(window.confirm('Czy chcesz ustawić adres '+email+' jako domyślny?')){
            document.forms['primary_email_form'].elements['primary_email'].value = obj.value;
            document.forms['primary_email_form'].submit();
        }        
    }
}