function AsyncUpdateEvent()
{
	switch(xmlHttp.readyState){
		case 0:
		if (document.getElementById(idPolja)) document.getElementById(idPolja).innerHTML = uninitialized;
		else xmlHttp.abort();
		break;
		case 1:
		if (document.getElementById(idPolja)) document.getElementById(idPolja).innerHTML = loading;
		else xmlHttp.abort();
		break;
		case 2:
		if (document.getElementById(idPolja)) document.getElementById(idPolja).innerHTML = loaded;
		else xmlHttp.abort();
		break;
		case 3:
		if (document.getElementById(idPolja)) document.getElementById(idPolja).innerHTML = interactive;
		else xmlHttp.abort();
		break;
		case 4:
        arg = xmlHttp.responseText.split("||");
		if (document.getElementById(idPolja) != null) document.getElementById(idPolja).innerHTML = arg[0];
        if (document.getElementById('datum')!=null && arg[1]!= undefined) document.getElementById('datum').innerHTML = arg[1];
        if (document.getElementById('navigacija')!=null && arg[2]!=undefined) document.getElementById('navigacija').innerHTML = arg[2];
		break;
	}
}
function berzabox_favoriti(fraza)
{
	idPolja = "berza-favoriti";
    var sort = document.getElementById('sort').value;
    string = "sort="+sort;
	if (xmlHttp){
		xmlHttp.abort();
		xmlHttp.open("POST","skripte/berza-favoriti.php",true);
		xmlHttp.onreadystatechange = AsyncUpdateEvent;
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send(string);
	}
}
function brza_pretraga(oEvent,svi_rezultati)
{
    oEvent = oEvent || window.event;
	var inputField = oEvent.target || oEvent.srcElement;
    var fraza=inputField.value;
    k = svi_rezultati;
    //var broj_redova = document.getElementById('broj_redova').value;
    var sort = document.getElementById('sort').value;
    idPolja = "berza-favoriti";
	if (fraza != '') string = "fraza="+fraza+"&sort="+sort;
    else  string = "sort="+sort;
    clearTimeout(delay);
    delay = setTimeout("trazi(string,k)",500);
}
function sort(svi_rezultati)
{
    //var broj_redova = document.getElementById('broj_redova').value;
    var fraza = document.getElementById('brza_pretraga_tickers').value;
    var sort =  document.getElementById('sort').value; 
    idPolja = "berza-favoriti";
    k = svi_rezultati;
	if (fraza != '') string = "fraza="+fraza+"&sort="+sort;
    else  string = "sort="+sort;
    trazi(string,k);
}
function trazi(string,s)
{
    if (xmlHttp){
		xmlHttp.abort();
        if (s==0) xmlHttp.open("POST","skripte/berza-favoriti.php",true);
        else xmlHttp.open("POST","skripte/sve-hartije.php",true);
		xmlHttp.onreadystatechange = AsyncUpdateEvent;
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp.send(string);
	}
}
function AsyncUpdateEventDodajPortf()
{
    if(xmlHttp_dodajfav.readyState ==4){
        arg = xmlHttp_dodajfav.responseText.split("||");
        var polje;
        var i = 0;
        var red = document.createElement('tr');
        while(arg[i] != undefined){
			polje = document.createElement('td');
			polje.innerHTML = arg[i];
			red.appendChild(polje);
			i = i+1;
		}
		document.getElementById('listafavorita').appendChild(red);
	}
}
function portfolio_dodaj()
{
    var hartija = document.getElementById('novifav').value;
    xmlHttp_dodajfav = new createXMLHttp();
    if (xmlHttp_dodajfav){
		xmlHttp_dodajfav.abort();
		xmlHttp_dodajfav.open("GET","skripte/portfolio_dodaj.php?hartija="+hartija,true);
		xmlHttp_dodajfav.onreadystatechange = AsyncUpdateEventDodajPortf;
        xmlHttp_dodajfav.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp_dodajfav.send(null);
	}
}
function AsyncUpdateEventPortfolioIzbrisi()
{
    if(xmlHttp_izbrisifav.readyState == 4){
        if (xmlHttp_izbrisifav.responseText != 'false') {
            arg = xmlHttp_izbrisifav.responseText.split("||");
            document.getElementById('kupovnavrijednost').value = arg[0];
            document.getElementById('sadasnjavrijednost').value = arg[1];
            inputField.parentNode.parentNode.style.display = 'none';
            var dg = document.getElementById('dobitak_gubitak');
            var dgp = document.getElementById('procenat_dobitak_gubitak');
            dg.value = arg[2];
            dgp.value = arg[3]+" %";
            if (Number(arg[3])>=0){
            	dg.style.color = '#339933';
            	dgp.style.color = '#339933';
            }
            else{
				dg.style.color = '#CC3300';
            	dgp.style.color = '#CC3300';
			}
        }
	}
}
function portfolio_izbrisi(oEvent,hartija,ident)
{
    if (!confirm('Da li zelite da izbrisete hartiju : '+hartija))  return;
    oEvent = oEvent || window.event;
	inputField = oEvent.target || oEvent.srcElement;
    xmlHttp_izbrisifav = new createXMLHttp(); 
    if (xmlHttp_izbrisifav){
		xmlHttp_izbrisifav.abort();
		xmlHttp_izbrisifav.open("GET","skripte/portfolio_izbrisi.php?hartija="+hartija+"&ident="+ident,true);
		xmlHttp_izbrisifav.onreadystatechange = AsyncUpdateEventPortfolioIzbrisi;
        xmlHttp_izbrisifav.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp_izbrisifav.send(null);
	}
}
function AsyncUpdateEventUpdt()
{
    if(xmlHttp_brakc.readyState == 4){ 
        if (xmlHttp_brakc.responseText != 'false') {
            var arg =  xmlHttp_brakc.responseText.split("||");
            var polje = link.parentNode.parentNode.lastChild.previousSibling;
            polje.innerHTML = arg[0];
            polje = polje.previousSibling;
            polje.innerHTML = arg[1];
            polje = polje.previousSibling;
            polje.innerHTML = arg[2];
            polje = polje.previousSibling;
            polje.innerHTML = arg[3];
            polje = polje.previousSibling;
            polje.innerHTML = arg[4];
            document.getElementById('kupovnavrijednost').value = arg[5];
            document.getElementById('sadasnjavrijednost').value = arg[6];
            var dg = document.getElementById('dobitak_gubitak');
            var dgp = document.getElementById('procenat_dobitak_gubitak');
            dg.value = arg[7];
            dgp.value = arg[8]+" %";
            if (Number(arg[8])>=0){
            	dg.style.color = '#339933';
            	dgp.style.color = '#339933';
            }
            else{
				dg.style.color = '#CC3300';
            	dgp.style.color = '#CC3300';
			}
        }
	}
}
function portfolio_update(oEvent,hartija,ident)
{
    oEvent = oEvent || window.event;
	link = oEvent.target || oEvent.srcElement;
    var vrijednost = 0;
    var vrijednost1 = 0;
    var vrijednost2 = 0;
    var vrijednost3 = 0;
    var field = link.parentNode.previousSibling;
    field = field.previousSibling;
    field = field.previousSibling;
    field = field.previousSibling;
    field = field.previousSibling;
    field = field.previousSibling;
    vrijednost3 = field.firstChild.value;
    field = field.previousSibling;
    vrijednost2 = field.firstChild.value;
    field = field.previousSibling;
    vrijednost1 =  field.firstChild.value;
    field = field.previousSibling;
    vrijednost =  field.firstChild.value;
    if (isNaN(vrijednost)==true || vrijednost == '') vrijednost = 0;
    if (isNaN(vrijednost1)==true || vrijednost1 == '') vrijednost1 = 0;
    if (isNaN(vrijednost2)==true || vrijednost2 == '') vrijednost2 = 0;
    if (isNaN(vrijednost3)==true || vrijednost3 == '') vrijednost3 = 0;
    xmlHttp_brakc = new createXMLHttp(); 
    if (xmlHttp_brakc){
		xmlHttp_brakc.abort();
		xmlHttp_brakc.open("GET","skripte/portfolio_update.php?hartija="+hartija+"&nova_vrijednost="+vrijednost+"&nova_vrijednost1="+vrijednost1+"&nova_vrijednost2="+vrijednost2+"&nova_vrijednost3="+vrijednost3+"&ident="+ident,true);
		xmlHttp_brakc.onreadystatechange = AsyncUpdateEventUpdt;
        xmlHttp_brakc.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlHttp_brakc.send(null);
	}
}
