<!--
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

var eid = "";
var rozlacz = 0;
var call_id = 0;
var src = '';
var dsc = '';
var old_price = '';
var test = 0;
var cTimer = '';
var kontakt_name = '';
var mojenumery = Array(4);
var gl_id = '';
var demo_call = '';

function getUrl( ) {
    var args = getUrl.arguments;
    
    if (document.getElementById) {
        var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
        }

    if (x) {
        x.onreadystatechange = function( ) {
            if (x.readyState == 4 && x.status == 200) {
                str = x.responseText;
                str = str.replace(/[+]/ig, "");
				//console.log(x.responseText);
				//console.log(args[1] + '(\'' + str + '\')');
                if (args[1] == 'start_call')
                    eval(args[1] + '(\'' + parseFloat(str) + '\', 1)'); 
                else {
                	eval(args[1] + '(\'' + str + '\')');
                    }
                }
            }
        }

    x.open("GET", args[0], true);
    x.send(null);
    }
    
var cTimer = 0;
function start_call( call , start ) {
    clearTimeout(cTimer);

    if( demo_call == 1 && start == 1 && call == 0){
    	alert("Połączenia demo można wykonywać jedynie na numery stacjonarne przez 2 minuty.");
    	clearTimeout(cTimer);
		document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
        getUrl(aloalo_url + '?fun=disconnect_call&con=function&par=' + call_id, 'void');
        
    }
    if (call)
        call_id = call;

    if (call_id > 1)
        getUrl(aloalo_url + '?fun=get_full_call_state&con=function&par=' + call_id, 'makeCall');
	else {
		document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
	}
    if (call_id)
        cTimer = setTimeout("start_call()", 4000);
    }
    
var fTimer = '';
function flash( id, kolor, czas, kolor2 ) {
    czas2 = czas;

    if (document.getElementById) {
        if (! document.getElementById(id)) {
            clearTimeout(fTimer);
            return '';
            }

        document.getElementById(id).style.color = kolor;
        fTimer = setTimeout('flash("' + id + '","' + kolor2 + '",' + czas2 + ',"' + kolor + '",' + czas + ')', czas);
        } else if (document.all) {
        if (! document.all[id]) {
            clearTimeout(fTimer);
            return '';
            }

        document.all[id].style.color = kolor;
        fTimer = setTimeout('flash("' + id + '","' + kolor2 + '",' + czas2 + ',"' + kolor + '",' + czas + ')', czas);
        }
    }
function makeCall( id ) {
	gl_id = id;
    if (id == 'connected') {
        document.getElementById('call_state').innerHTML = '<span id="stage3" style="color: red">Połączono</span>';
        clearTimeout(fTimer);
        flash('stage3', '#ff0000', 800, '#A8A6AB');
    } else if (id == 'new' || id == 'started') {
        document.getElementById('call_state').innerHTML = '<span id="stage2">aloalo łączy</span>';
        clearTimeout(fTimer);
        flash('stage2', '#ff0000', 400, '#A8A6AB');
        document.getElementById('call_button').src = '/grafika/rozlacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 1;
    } else if (id == 'stage2') {
        document.getElementById('call_state').innerHTML = '<span id="stage2">aloalo łączy</span>';
        clearTimeout(fTimer);
        flash('stage2', '#ff0000', 600, '#A8A6AB');
        document.getElementById('call_button').src = '/grafika/rozlacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 1;
    } else if (id == 'finished' || id == '' || id == '1') {
        clearTimeout(cTimer);
        document.getElementById('call_state').innerHTML = 'Zakończono połączenie';
        document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
        //alert(call_id);
        getUrl(aloalo_url + '?fun=disconnect_call&con=function&par=' + call_id, 'void');
        getSaldo();
    } else if (id == 'failed') {
        clearTimeout(cTimer);
        document.getElementById('call_state').innerHTML = 'Połączenie nieskuteczne. Łączyć ponownie';
        document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
    } else if (id == 'src-number-invalid' || id == 'dst-number-invalid') {
        clearTimeout(cTimer);
        document.getElementById('call_state').innerHTML = 'Błędny numer ';
        document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
    } else if (id == 'user-acc-locked') {
        clearTimeout(cTimer);
        document.getElementById('call_state').innerHTML = 'Konto zablokowane';
        document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
    } else if (id == 'price-not-found') {
        clearTimeout(cTimer);
        document.getElementById('call_state').innerHTML = 'Numer docelowy poza zasięgiem AloAlo';
        document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
    } else if (id == 'balance-too-low') {
        clearTimeout(cTimer);
        document.getElementById('call_state').innerHTML = 'Niewystarczające środki na koncie';
        document.getElementById('call_button').src = '/grafika/polacz'+(demo_call ? '2' : '')+'.gif';
        rozlacz = 0;
    } else {
		clearTimeout(cTimer);
    }
}
function getSaldo( ) {
	//if( !demo_call )
    //	getUrl(aloalo_url + '?fun=get_user_balance&con=function&PHPSESSID=' + document.aloalo.phpsessid.value, 'setSaldo');
    }
function setSaldo( price ) {
    //document.getElementById('balance1').innerHTML = price;
    }
var pTimer = "";
function getCallPrice( price ) {
    if (price == 'stop') {
        clearTimeout(pTimer);
        return;
    }

    if (rozlacz)
        return;

    if (price) {
        old_price = price;
        document.getElementById('call_price').innerHTML = price;
        } else {
        pTimer = setTimeout("getCallPrice()", 5000);

        if (document.aloalo.src_num.value.replace(/[^0-9]/ig,
                                                  "") > 0 && document.aloalo.dsc_num.value.replace(/[^0-9]/ig,
                                                                                                   "") > 0
            && (document.aloalo.src_num.value.replace(/[^0-9]/ig,
                                                      "") != src || document.aloalo.dsc_num.value.replace(/[^0-9]/ig,
                                                                                                          "") != dsc)) {
            src = document.aloalo.src_num.value.replace(/[^0-9]/ig, "");
            dsc = document.aloalo.dsc_num.value.replace(/[^0-9]/ig, "");
            //test = test + 1;
            //document.getElementById('test').innerHTML = test;

            getUrl(aloalo_url + '?fun=minute_brutto_price_for2&con=function&par= '
                       + formatNumberVal(document.aloalo.dsc_num.value) + ',' + formatNumberVal(document.aloalo.src_num.value),
                   'getCallPrice')
            } else if (old_price) {
            return old_price;
            } else
            getCallPrice('...');
        }
    }
function aloalo_submit( ) {
	if( gl_id == 'new' && rozlacz == 1 ){
		return;
	}
    if (rozlacz == 1) {
        makeCall('finished');
        return;
    }
	if( prepareSave(document.aloalo.dsc_num.value) && prepareSave(document.aloalo.dsc_num.value)!= 48 && prepareSave(document.aloalo.src_num.value) && prepareSave(document.aloalo.src_num.value) != 48 ) {
	    document.getElementById('call_button').src = '/grafika/rozlacz.gif';
	    rozlacz = 1;
		gl_id = "";
	    formatNumber(document.aloalo.dsc_num);
	    getUrl(aloalo_url + '?PHPSESSID=' + document.aloalo.phpsessid.value + '&con=call&dsc_num='
	               + document.aloalo.dsc_num.value.replace(/[^0-9p]/ig,
	                                                       "") + '&src_num=' + document.aloalo.src_num.value.replace(
	                                                                               /[^0-9p]/ig, ""),
	           'start_call');
	}
}

function demo_submit( ) {
	demo_call =1;
	if( gl_id == 'new' && rozlacz == 1 ){
		return;
	}
    if (rozlacz == 1) {
        makeCall('finished');
        return;
    }
	if( prepareSave(document.aloalo.dsc_num.value) && prepareSave(document.aloalo.dsc_num.value)!= 48 && prepareSave(document.aloalo.src_num.value) && prepareSave(document.aloalo.src_num.value) != 48 ) {
	    document.getElementById('call_button').src = '/grafika/rozlacz2.gif';
	    rozlacz = 1;
		gl_id = "";
	    formatNumber(document.aloalo.dsc_num);
	    formatNumber(document.aloalo.src_num);
	    getUrl(aloalo_url + '?con=demo_call&dsc_num='+ document.aloalo.dsc_num.value.replace(/[^0-9p]/ig,"") + '&src_num=' + document.aloalo.src_num.value.replace(/[^0-9p]/ig, ""),
	           'start_call');
	}
}

function formatNumber( obj ) {
    str = obj.value;

    if (str)
        str = str.replace(/([^0-9p]+)/ig, "");
	

	if( str.length == 12 )
		str = str.replace(/^480/ig, "48");

	if( str.length == 10 )
		str = str.replace(/^0/ig, "");

	if( str.length == 9 && !str.match("^49") )
		str = '48'+str;

	str = str.replace(/^00/ig, "");
	if( str.length <= 10 ){
		str = str.replace(/^0/ig, "48");
	}
    str = str.replace(/^([0-9p]{2})/ig, "$1 ");

    obj.value = '+' + str;
    }

function formatNumberVal( value ) {
    str = value;

    if (str)
        str = str.replace(/([^0-9p]+)/ig, "");
	

	if( str.length == 12 )
		str = str.replace(/^480/ig, "48");

	if( str.length == 10 )
		str = str.replace(/^0/ig, "");

	if( str.length == 9 && !str.match("^49"))
		str = '48'+str;

	str = str.replace(/^00/ig, "");
	if( str.length <= 10 ){
		str = str.replace(/^0/ig, "48");
	}
	return str;
    }
function prepareStartValue(a){
	a = a.replace(/^\s+|\s+$/, '');
	if(a == "48")
		a = "";
	a = a.replace(/^([0-9]{2})/ig, "$1 ");
	return a;
}
    
function loadKontakty( ) {
    filter = document.groups.grupa.value;
    var kon = '';
    
    if (kontakt_name) {
        for (i = 1; i < kontakt_name.length; i++) {
                if (kontakt_id[i] > 0 && (kontakt_grupa[i] == filter && filter > 0 || filter <= 0))
                    kon = kon + '<tr><td class="td_okienko_l">'
                    		  + '<div class="div_okiel">' + kontakt_name[i]
                              + '</div></td><td class="iko_tele">'
                    		  + '<a id="ko_'+i+'" name="ko_'+i+'" href="javascript: kontakty_edit('+i+')"><img border=0 alt="Edytuj kontakt" align="absmiddle" src="grafika/edit.gif"></a> '
                    		  + '<a href="javascript: kontakty_delete('+kontakt_id[i]+')"><img border=0 alt="Usuń kontakt" align="absmiddle" src="grafika/remove.gif"></a>'
                    		  + '</div></td><td nowarp class="td_okienko"><div class="div_okie" style="cursor:pointer;" onClick="setNumber(\''
                              + kontakt_work[i] + '\')">' + (prepareStartValue(kontakt_work[i]) ? '+' + prepareStartValue(kontakt_work[i]) : "&nbsp;")
                              + '</div></td><td nowarp class="td_okienko"><div class="div_okie" style="cursor:pointer;" onClick="setNumber(\'' + kontakt_dom[i]
                              + '\')">'
                              + (prepareStartValue(kontakt_dom[i]) ? '+' + prepareStartValue(kontakt_dom[i])
                                    : "&nbsp;") + '</div></td><td nowarp class="td_okienko"><div class="div_okie" style="cursor:pointer;" onClick="setNumber(\''
                              + kontakt_mobile[i] + '\')">' + ( prepareStartValue(kontakt_mobile[i])
                                                                ? '+' + prepareStartValue(kontakt_mobile[i]) : "&nbsp;") + '</div></td></tr>';
            }
        }

    document.getElementById('kontakty').innerHTML
        = '<table width="525" border="0" cellspacing="0" cellpadding="0" align="left"><!--<tr><td class="nagl_n">'+imie_i_nazwisko+'</td><td>&nbsp;</td><td class="nagl_n">Praca</td><td class="nagl_n">Dom</td><td class="nagl_n">'+komorka+'</td></tr>-->' + kon
              + '</table>';
    } 
function setNumber( a ) {
    document.aloalo.dsc_num.value = '+' + a;
    getCallPrice();
    }

function setSrc( radio, val, valobj ) {
    for (i = radio.length - 1; i > - 1; i--) {
        if (radio[i].checked) {
            myOption = i;
            i = - 1;
            }
        }

    if (radio[myOption].value = val)
        document.aloalo.src_num.value = valobj;
}

function dodajGrupe(){
	hideLay();
	url = "/tpl/html/grupy.html";
	document.getElementById('tta').style.display="block";
	
	document.getElementById('tta').innerHTML = "<iframe id='tti' style='background: #ffffff;border: solid 1px #cccccc' width=406 height=270 scrolling=no frameborder=0 src='"+url+"'></iframe>";	
	
	pos = getAnchorPosition("gr_e");
	
	document.getElementById('tta').style.left = (document.body.scrollWidth/2-document.getElementById('tti').width/2)+'px';
	document.getElementById('tta').style.top = (pos.y-document.getElementById('tti').height-3)+'px';
}

function prepareSave(a){
	a = a.replace(/([^0-9p]+)/ig, "");
    return a;
}
function kontakty_dodaj(){
	hideLay();
	if( document.dodaj.name.value ){
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
		if(x){
			x.onreadystatechange = function(){
			if (x.readyState == 4 && x.status == 200){
				i = kontakt_name.length;
				kontakt_name[i] = document.dodaj.name.value;
				kontakt_id[i] = x.responseText;
				kontakt_dom[i] = prepareSave(document.dodaj.dom.value);
				kontakt_work[i] = prepareSave(document.dodaj.work.value);
				kontakt_mobile[i] = prepareSave(document.dodaj.mobile.value);
				kontakt_grupa[i] = document.groups.grupa.value;
				document.dodaj.mobile.value = "+48 ";
				document.dodaj.work.value = "+48 ";
				document.dodaj.dom.value = "+48 ";
				document.dodaj.name.value = "";
				loadKontakty();		
			}}
			
		}
		formatNumber(document.dodaj.work);
		formatNumber(document.dodaj.mobile);
		formatNumber(document.dodaj.dom);
		x.open("GET", aloalo_url+"?con=kontakty_add&name="+encodeURI(document.dodaj.name.value)+"&mobile="+prepareSave(document.dodaj.mobile.value)+"&dom="+prepareSave(document.dodaj.dom.value)+"&work="+prepareSave(document.dodaj.work.value)+"&grupa="+document.groups.grupa.value+"&PHPSESSID="+document.aloalo.phpsessid.value, true);
		x.send(null);
	}
}
function hideLay(){
	document.getElementById("tta").style.display = "none";
}
function kontakty_delete(id){
	hideLay();
	if (document.getElementById) {
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}
	if(x){
		x.onreadystatechange = function(){
		if (x.readyState == 4 && x.status == 200){
			for(i=0;i<kontakt_name.length;i++){
				if( kontakt_id[i] == x.responseText){
					kontakt_id[i] = "";
				}
			}
			loadKontakty();					
		}}
	}
	x.open("GET", aloalo_url+"?con=kontakty_delete&id="+id+"&PHPSESSID="+document.aloalo.phpsessid.value, true);
	x.send(null);
}

function kontakty_edit(index){
	hideLay();
	eid = index;
	url = "/tpl/html/kontakty_edytuj.html";
	document.getElementById('tta').style.display="block";
	document.getElementById('tta').innerHTML = "<iframe id='tti' style='background: #ffffff;border: solid 1px #cccccc' width=406 height=270 scrolling=no frameborder=0 src='"+url+"'></iframe>";	
	

	pos = getAnchorPosition("ko_"+index);
	
	document.getElementById('tta').style.left = Math.round(document.body.scrollWidth/2-document.getElementById('tti').width/2)+'px';

	document.getElementById('tta').style.top = (pos.y-document.getElementById('tti').height-document.getElementById('kontakty').scrollTop-3)+'px';

}	

function edytujMojeNumery(){
	hideLay();
	url = "/tpl/html/mojenumery.html";
	
	document.getElementById('tta').style.display="block";
	document.getElementById('tta').innerHTML = "<iframe id='tti' style='background: #ffffff;border: solid 1px #cccccc' width=406 height=270 scrolling=no frameborder=0 src='"+url+"'></iframe>";	
	

	pos = getAnchorPosition("mr_e");
	
	document.getElementById('tta').style.left = (document.body.scrollWidth/2-document.getElementById('tti').width/2)+'px';
	document.getElementById('tta').style.top = (pos.y+20)+'px';		
}
function loadSpecial(){
	ret = '';
	for(i=1;i<=7;i++){
		if( special[i] && special_name[i]){
			ret = ret + '<a id="sp_'+i+'" name="sp_'+i+'" href="javascript: void(0);" class="tt"><input onclick="setNumber(\''+special[i]+'\');aloalo_submit();" type="button" class="input_znaj" value="'+special_name[i]+'" /><dfn>+'+special[i]+'</dfn></a>';   
			if( i == 1 ){
				ret = ret + '&nbsp;<a href="javascript:edytujSpecial(1);"><img border=0 src="grafika/edit.gif"></a>';
			}
		} else
			ret = ret + '<a id="sp_'+i+'" name="sp_'+i+'" href="javascript: void(0);" class="tt"><input onclick="edytujSpecial(1);" type="button" class="input_znaj" value="..." /><dfn>Zdefinuj numer</dfn></a>';     
	}
	document.getElementById('special').innerHTML = ret;
}
function edytujSpecial(index){
	hideLay();
	url = "/tpl/html/special.html";
	
	document.getElementById('tta').style.display="block";
	document.getElementById('tta').innerHTML = "<iframe id='tti' style='background: #ffffff;border: solid 1px #cccccc' width=406 height=270 scrolling=no frameborder=0 src='"+url+"'></iframe>";	
	

	pos = getAnchorPosition("sp_"+index);
	
	document.getElementById('tta').style.left = (document.body.scrollWidth/2-document.getElementById('tti').width/2)+'px';
	document.getElementById('tta').style.top = (pos.y+20)+'px';
}

function checkSession(){
		if (document.getElementById) {
			var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
		}
		if(x){
			x.onreadystatechange = function(){
			if (x.readyState == 4 && x.status == 200){
				//alert("'"+x.responseText+"'");
				if( document.aloalo.phpsessid.value != x.responseText ){
					location.href=aloalo_url_main+"?autologowanie=1&PHPSESSID="+document.aloalo.phpsessid.value;
				}
			}}
			
		}
		x.open("GET", aloalo_url_main+"?body=check_session&PHPSESSID="+document.aloalo.phpsessid.value, true);
		x.send(null);
	
	setTimeout('checkSession()', 1000*60*5);
}
-->    


