/* Rad s HTTP 
vim:enc=utf8:fenc=utf8
 */

function getHTTPObject() {
	var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

var http = getHTTPObject();
var hp   = getHTTPObject();

if( !http ) {
	alert("Ne moľemo raditi sa http!");
}

var uPogonuSam = false;
var disp;
function Pisi( str ) {
	disp.innerHTML += "<li>" + str;
}


/*
 * Favoriti
 */
function ObrisiFavorita( but, vrsta, idA ) {
	src = but.src;
	but.src = "images/cekanje.gif";
	but.alt = "[ČEKAJ]";
	but.title = "Čekaj";
	but.disabled = true;
	if( confirm( "Doista želite obrisati?\nOK - da\nCancel - ne" ) ) {
		var redak = but.parentNode.parentNode;
		if( http && !uPogonuSam ) {
			http.open("POST", "classes/akcije5.php?akcija=ObrisiFavorita", true);
			http.onreadystatechange = function() {
				if( http.readyState == 4 ) {
					uPogonuSam = false;
					odg = http.responseText.split( "###" );
					if( odg[0] == "OK" ) {
						redak.parentNode.removeChild( redak );
						document.getElementById( "FavTab" ).innerHTML = odg[1];
						onload();
					} else {
						alert( "GREŠKA\n" + http.responseText );
					}
				}
			}
			uPogonuSam = true;
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
			http.send( "id="+idA+"&vrsta="+vrsta );
		}
	}
	but.src = src;
	but.alt = "[Obriši]";
	but.title = "Obriši";
	but.disabled = false;
}

// U p o z o r e n j a
// --------------------

function OpcijeUpozorenja( x, odabir ) {
	var gran = document.getElementById( "graniceUpozorenja" );
	var dani = document.getElementById( "daniUpozorenja" );

	if( odabir==0 ) { dani.style.display = "block"; } else { dani.style.display = "none"; }
	if( odabir==2 ) { gran.style.display = "block"; } else { gran.style.display = "none"; }
}

function ObrisiUpozorenje( but, idA, tablica ) {
	but.src = "images/cekanje.gif";
	if( confirm( "Obrisati brzu informaciju?\nOK - da\nCancel - ne" ) ) {
		var redak = but.parentNode.parentNode;
		if( http && !uPogonuSam ) {
			but.value = "Pričekajte, brišem";
			but.disabled = true;
			http.open("POST", "classes/akcije5.php?akcija=ObrisiUpozorenje", true);
			http.onreadystatechange = function() {
				if( http.readyState == 4 ) {
					uPogonuSam = false;
					rez = http.responseText.split( "###" );
					if( rez[0] == "OK" ) {
						document.getElementById( "divZaTablicuAlerta" ).innerHTML = rez[1];
					} else {
						alert( "Ništa nije obrisano!" );
					}
				}
			}
			uPogonuSam = true;
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
			http.send( "id="+idA+"&tablica="+tablica );
		}
	}
	but.src = "images/delete.png";
}

function DodajUpozorenje( tablica ) {
	var mzi = document.getElementById( "mjestoZaIzmjenu" );
	var fzi = document.getElementById( "formaUpozorenja" );
	var dani = document.getElementById( "daniUpozorenja" );
	var gran = document.getElementById( "graniceUpozorenja" );

	fzi.action = "DodajUpozorenje";
	fzi.tablica.value = tablica;
	fzi.idA.value = 0;
	mzi.firstChild.innerHTML = "Dodavanje brze informacije";
	mzi.style.display = "block";
	fzi.ticker.selectedIndex = 0;
	fzi.aktiviraSe.selectedIndex = 0;

	gran.style.display = "none";
	dani.style.display = "block";
	for( i=0; i<5; i++ ) {
		dani.childNodes[i].firstChild.checked = true;
	}
	for( i=5; i<7; i++ ) {
		dani.childNodes[i].firstChild.checked = false;
	}
	fzi.cema.checked = true;
	fzi.csms.checked = false;
	fzi.sati.value = "16";
}

function IzmjeniUpozorenje( but, idA, tablica ) {
	var redak = but.parentNode.parentNode;
	var daniDiv = document.getElementById( "daniUpozorenja" );
	var granice = document.getElementById( "graniceUpozorenja" );
	
	var ticker = redak.childNodes[0].title;
	var aktiviraSe = redak.childNodes[1].firstChild.innerHTML;
	var polje = redak.childNodes[1].lastChild.innerHTML;

	var mzi = document.getElementById( "mjestoZaIzmjenu" );
	var fzi = document.getElementById( "formaUpozorenja" );
	fzi.action = "IzmjeniUpozorenje";
	fzi.tablica.value = tablica;
	fzi.idA.value = idA;

	mzi.firstChild.innerHTML = "Izmjena brze informacije";
	mzi.style.display = "block";

	if( polje != "last" && polje != "" ) {
		ticker = ticker.split("-");
		banka = ticker[0];
		ticker = ticker[1];
		document.getElementById( "banka-" + banka ).selected = true;
	}

	document.getElementById( "ticker-" + ticker ).selected = true;

	if( aktiviraSe == "Dnevno" ) {
		fzi.aktiviraSe.selectedIndex = 0;
		daniDiv.style.display = "block";
		granice.style.display = "none";
		fzi.sati.value = Math.floor( redak.childNodes[2].childNodes[1].innerHTML );
		var dani = redak.childNodes[3].childNodes[1].innerHTML;
		dani = dani.toLowerCase();

		fzi.dan_2.checked = dani.indexOf( "pon" ) != -1 ? true : false;
		fzi.dan_3.checked = dani.indexOf( "uto" ) != -1 ? true : false;
		fzi.dan_4.checked = dani.indexOf( "sri" ) != -1 ? true : false;
		fzi.dan_5.checked = dani.indexOf( "čet" ) != -1 ? true : false;
		fzi.dan_6.checked = dani.indexOf( "pet" ) != -1 ? true : false;
		fzi.dan_7.checked = dani.indexOf( "sub" ) != -1 ? true : false;
		fzi.dan_1.checked = dani.indexOf( "ned" ) != -1 ? true : false;

	} else if( aktiviraSe == "Izvan granica" ) {
		if( polje == "last" || polje == "K" ) {
			fzi.polje.selectedIndex = 0;
		} else if( polje == "S" ) {
			fzi.polje.selectedIndex = 1;
		} else {
			fzi.polje.selectedIndex = 2;
		}
		fzi.aktiviraSe.selectedIndex = 2;
		daniDiv.style.display = "none";
		granice.style.display = "block";
		fzi.donjaGranica.value = redak.childNodes[2].childNodes[1].innerHTML;
		fzi.gornjaGranica.value = redak.childNodes[3].childNodes[1].innerHTML;
	} else {
		fzi.aktiviraSe.selectedIndex = 1;
		daniDiv.style.display = "none";
		granice.style.display = "none";
	}
	
	fzi.cema.checked = ( redak.childNodes[4].innerHTML.indexOf( "DA" ) != -1 ) ? true : false;
	fzi.csms.checked = ( redak.childNodes[5].innerHTML.indexOf( "DA" ) != -1 ) ? true : false;
	fzi.redak = redak;
}

function SpremiUpozorenje( b ) {
	// b = button Spremi koji smo pritisnuli
	b.value = "Pričekajte, spremam!";
	b.disabled = true;
	f = b.form;

	pom = "akcija=" + f.action + "&idA=" + f.idA.value + "&vrsta=" + f.vrsta.value + "&ticker=" + f.ticker.value + "&aktiviraSe=" + f.aktiviraSe.value + "&polje=" + f.polje.value;
	if( f.banka ) pom += "&banka=" + f.banka.value;
	pom += "&donjaGranica=" + f.donjaGranica.value + "&gornjaGranica=" + f.gornjaGranica.value;
  	pom += "&sati=" + f.sati.value + "&cema=" + ( f.cema.checked ? 1 : 0 ) + "&csms=" + ( f.csms.checked ? 1 : 0 );
	pom += "&tablica=" + f.tablica.value;

	var dani = "";

	if( f.dan_2.checked ) { pom += "&pon=1"; dani += "Pon "; }
	if( f.dan_3.checked ) { pom += "&uto=1"; dani += "Uto "; }
	if( f.dan_4.checked ) { pom += "&sri=1"; dani += "Sri "; }
	if( f.dan_5.checked ) { pom += "&cet=1"; dani += "Čet "; }
	if( f.dan_6.checked ) { pom += "&pet=1"; dani += "Pet "; }
	if( f.dan_7.checked ) { pom += "&sub=1"; dani += "Sub "; }
	if( f.dan_1.checked ) { pom += "&ned=1"; dani += "Ned "; }

	if( http && !uPogonuSam ) {
		http.open("POST", "classes/akcije5.php?akcija=SpremiUpozorenje", true);
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				rez = http.responseText.split( "###" );
				if( rez[0] == "OK" ) {
					document.getElementById( "divZaTablicuAlerta" ).innerHTML = rez[1];
				} else {
					alert( "Nije izmjenjeno!" );

				}
		b.value = "Spremi!";
		b.disabled = false;
			document.getElementById( "mjestoZaIzmjenu" ).style.display = "none";				
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
		http.send( pom );
	}

	b.value = "Pričekajte, spremam!";
	b.disabled = true;
}

function NamjestiSelect( s, t ) {
	pom = "";
	for( i = 0; i < s.options.length; i++ ) {
		if( s.options[i].value.split("###")[0] == t ) {
			s.options[i].selected = true;
		}
	}
}

function ProdajStavkuPortfelja( button ) {

	idPortfelja = button.form.idPortfelja.value;
	r = button.parentNode.parentNode;
	vrsta = r.cells[0].innerHTML;
	ticker = r.cells[1].firstChild.innerHTML;
	kolicina = r.cells[2].innerHTML;
	if( vrsta == "Valute" ) {
		jedCijena = r.cells[6].innerHTML;
	} else {
		jedCijena = r.cells[3].innerHTML;
	}
	kolicina = kolicina.replace( /\./, "" );
	kolicina = kolicina.replace( /,/, "." );
	jedCijena = jedCijena.replace( /\./, "" );
	jedCijena = jedCijena.replace( /,/, "." );

	curtop = GdjeSam( button );
	f = document.getElementById("formaPortfelja");
	f.style.display = "block";
	f.style.top = ( curtop-100 )+"px";

	f.firstChild.firstChild.innerHTML = "<b>Prodajte vrijednosnice</b>";
	f.kolicina.value = kolicina;
	f.jedCijena.value = jedCijena;
	f.hkol.value = kolicina;
	if( vrsta == 'Dionice' ) {
		f.vrsta[0].checked = true;
		var s = f.vrsta[0].parentNode.nextSibling.firstChild;
		NamjestiSelect( s, ticker );
	} else if( vrsta == 'Fondovi' ) {
		f.vrsta[1].checked = true;
		var s = f.vrsta[1].parentNode.nextSibling.firstChild;
		NamjestiSelect( s, ticker );
	} else {
		f.vrsta[2].checked = true;
		var s = f.vrsta[2].parentNode.nextSibling.firstChild;
		NamjestiSelect( s, ticker );
	}
	f.vrsta[0].disabled = true;
	f.vrsta[1].disabled = true;
	f.vrsta[2].disabled = true;
	f.vrsta[0].parentNode.nextSibling.firstChild.disabled = true;
	f.vrsta[1].parentNode.nextSibling.firstChild.disabled = true;
	f.vrsta[2].parentNode.nextSibling.firstChild.disabled = true;
	f.portfelj.value = idPortfelja;
}

function GdjeSam( b ) { // Y
	if ( b.offsetParent ) {
		curtop = b.offsetTop
		while ( b = b.offsetParent) {
			curtop += b.offsetTop
		}
	}
	return curtop;
}

function GdjeSamX( b ) {
	if ( b.offsetParent ) {
		curleft = b.offsetLeft
		while ( b = b.offsetParent) {
			curleft += b.offsetLeft
		}
	}
	return curleft;
}

function DodajStavkuPortfelja( b, idPortfelja ) {
	curtop = GdjeSam( b );

	f = document.getElementById("formaPortfelja");
	f.style.display = "block";
	f.style.top = ( curtop-100 )+"px";
	f.firstChild.firstChild.innerHTML = "<b>Dodajte vrijednosnice</b>";
	f.vrsta[0].disabled = false;
	f.vrsta[1].disabled = false;
	f.vrsta[2].disabled = false;
	f.vrsta[0].parentNode.nextSibling.firstChild.disabled = false;
	f.vrsta[1].parentNode.nextSibling.firstChild.disabled = false;
	f.vrsta[2].parentNode.nextSibling.firstChild.disabled = false;
	f.kolicina.value = "0.00";
	f.hkol.value = 9999999999;
	f.portfelj.value = idPortfelja;
}

function PonistiPromjenuStavkePortfelja( b, id ) {
	if( confirm( "Doista želite poništiti prethodnu promjenu?" ) ) {
		if( http && !uPogonuSam ) {
			b.value = "Pričekajte...";
			b.disabled = true;

			http.open("POST", "classes/akcije5.php?akcija=PonistiPromjenuStavkePortfelja", true);
			http.onreadystatechange = function() {
				if( http.readyState == 4 ) {
					uPogonuSam = false;
					b.value = "PONIŠTITE zadnju promjenu!";
					b.disabled = false;
					rez = http.responseText.split( "###" );
					
					if( rez[0] == "OK" ) {
						document.getElementById( "FormaPopisaPortfelja" ).innerHTML = rez[1];
						document.getElementById( "Portfelj_" + b.form.idPortfelja.value ).innerHTML = rez[2];

					} else {
						alert( http.responseText );
					}
				}
			}
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
			http.send( "id=" + id );
		}
	}
}

function DrugiDioTablicePortfelja( b,objekt ) {
	var t = b.form.firstChild;
	if( t.rows[0].cells[8].style.display == "none" ) {
		b.value = "Pogledajte STANJE portfelja";
		
		
		//Odredi naslov 
		objekt.innerHTML=' - ULAGANJA u portfelj';
		for( r = 0; r < t.rows.length - 1; r++ ) {
			for( s = 0; s < 15; s++ ) {
				if( t.rows[r].cells[s].className.indexOf( "stanje" ) != -1 ) {
					t.rows[r].cells[s].style.display = "none";
				} else if( t.rows[r].cells[s].className.indexOf( "dobit" ) != -1 ) {
					t.rows[r].cells[s].style.display = "";
				}
			}
		}
	} else {
		b.value = "Pogledajte ULAGANJA u portfelj";
		//Odredi naslov 
		objekt.innerHTML=' - STANJE portfelja';
		for( r = 0; r < t.rows.length - 1; r++ ) {
			for( s = 0; s < 15; s++ ) {
				if( t.rows[r].cells[s].className.indexOf( "stanje" ) != -1 ) {
					t.rows[r].cells[s].style.display = "";
				} else if( t.rows[r].cells[s].className.indexOf( "dobit" ) != -1 ) {
					t.rows[r].cells[s].style.display = "none";
				}
			}
		}
	}
}

function OtvoriPortfelj( b ) {
	var f = b.form;
	var ime = f.naziv.value;
	tb = f.lastChild.tBodies[0];
	if( puno( ime ) ) {
		if( http && !uPogonuSam ) {
			b.value = "Pričekajte, otvaram portfelj!";
			b.disabled = true;

			http.open("POST", "classes/akcije5.php?akcija=OtvoriPortfelj", true);
			http.onreadystatechange = function() {
				if( http.readyState == 4 ) {
					uPogonuSam = false;
					b.value = "Otvori!";
					b.disabled = false;
					b.form.firstChild.firstChild.style.display = "";
					rez = http.responseText.split( "###" );

					if( rez[0] == "OK" ) {
//						location = "http://www.kapital-plus.net/?Portfelji";

						// Dodajmo redak u tablicu popis portfelja

						b.form.brojPortfelja.value++;
						if( 1*b.form.brojPortfelja.value >= 1*b.form.maxPortfelja.value ) {
							b.disabled = true;
							b.previousSibling.disabled = true;
						}

						var pp = document.getElementById("PopisPortfelja").tBodies[0];						

						tr = pp.insertRow( 0 );
						td = tr.insertCell( 0 );
						td.className = "sorttd";
						td.innerHTML = "<a href='http:\/\/www.kapital-plus.net\/?Portfelji\/" + ime +"'>"+ ime +"</a>";
						td = tr.insertCell( 1 );
						td.innerHTML = rez[ 1 ];
						td.className = "sorttd";

						for( i = 2; i < 8; i++ ) {
							td = tr.insertCell( i );
							td.className = "broj sorttd";
							td.innerHTML = "0,00";
						}
						td = tr.insertCell( 8 );
						td.className = "sorttd";
						inp = document.createElement( "INPUT" );
						inp.type = "image";
						inp.src = "images/delete.png";
						inp.alt = "[X]";
						inp.title = "Obriši";
						inp.onclick = function() {
							ObrisiPortfelj( this, rez[2] );
						}
						td.appendChild( inp );						

						// Moramo dodati novi portfelj na vrh stranice ispod gornje tablice!!!

						div = document.createElement( "DIV" );
						div.id = "Portfelj_"+rez[2];
						div.innerHTML = rez[3];

						f.parentNode.insertBefore( div, f.nextSibling );
					} else {
						alert( http.responseText );
					}
				}
			}
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
			http.send( "naziv=" + ime );
		}
	} else {
		alert( "Niste upisali ime Vašeg novog portfelja!" );
	}
}

function ObrisiPortfelj( b, idPortfelja ) {
	b.src = "images/cekanje.gif";
	b.disabled = true;
	b.alt = "Pričekajte, brišem!";
	if( confirm( "Doista želite obrisati ovaj portfelj?" ) ) {
		if( http && !uPogonuSam ) {

			http.open("POST", "classes/akcije5.php?akcija=ObrisiPortfelj", true);
			http.onreadystatechange = function() {
				if( http.readyState == 4 ) {
					uPogonuSam = false;
					odg = http.responseText.split( "###" );

					if( odg[0] == "OK" ) {
						naziv = odg[1];

						// Brišem iz gornje tablice
						var tbl = document.getElementById( "PopisPortfelja" );
						if( tbl != null ) {
							for( i = 1; i < tbl.rows.length-1; i++ ) {
								if( tbl.rows[i].cells[0].innerHTML.indexOf( naziv ) != -1 ) {
									tbl.deleteRow( i );
									break;
								}
							}
							f = tbl.parentNode;
							f.brojPortfelja.value--;
							if( f.brojPortfelja.value <= f.maxPortfelja.value ) {
								f.naziv.disabled = false;
								f.naziv.value = "";
								f.otvori.disabled = false;
							}
						}

						a = document.getElementById( "Portfelj_" + idPortfelja );
						a.parentNode.removeChild( a );

					} else {
						alert( http.responseText );
					}
				}
			}
			http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
			http.send( "idPortfelja=" + idPortfelja );
		}
	}
	b.src = "images/delete.png";
	b.disabled = false;
	b.alt = "[X]";
}

function puno(str) {
	reg=/\S/;
	return reg.test(str);
}

function Izaberi( x ) {

	var vrsta, f = x.form;

	var classN = Array( "", "", "broj", "stanje broj", "stanje broj", "stanje", "stanje broj", "stanje broj",
			"dobit broj", "dobit broj", "dobit broj", "broj", "stanje broj", "dobit broj", "dobit broj" );

	if( f.vrsta[0].checked ) {
		ticker = f.vrsta[0].parentNode.nextSibling.firstChild.value;
		tick = ticker.split("###");
		ticker = tick[0];
		vrsta = "Dionice";
	} else if( f.vrsta[1].checked ) {
		ticker = f.vrsta[1].parentNode.nextSibling.firstChild.value;
		tick = ticker.split("###");
		ticker = tick[0];
		vrsta = "Fondovi";
	} else {
		ticker = f.vrsta[2].parentNode.nextSibling.firstChild.value;
		tick = ticker.split("###");
		ticker = tick[0];
		vrsta = "Valute";
	}

	var greska = "";
	if( !jeDatum( f.datum.value ) ) {
		greska += "Datum nije dobro upisan!\n";
	} 
	if( !jeBroj( f.kolicina.value ) ) {
		if( jeBrojZarez( f.kolicina.value ) ) {
			f.kolicina.value = f.kolicina.value.replace(".","").replace(",",".");
		} else {
			greska += "Količina nije dobro upisana! Mora biti broj.\n";
		}
	} 
	if( !jeBroj( f.jedCijena.value ) ) {
		if( jeBrojZarez( f.jedCijena.value ) ) {
			f.jedCijena.value = f.jedCijena.value.replace(".","").replace(",",".");
		} else {
			greska += "Jedinična cijena nije dobro upisana! Mora biti broj.\n";
		}
	} 
	if( !jeBroj( f.naknada.value ) ) {
		if( jeBrojZarez( f.naknada.value ) ) {
			f.naknada.value = f.naknada.value.replace(".","").replace(",",".");
		} else {
			greska += "Naknada nije dobro upisana! Mora biti broj.\n";
		}
	}

	if( greska.length > 0 ) {
		alert( "KRIVI UNOS\n\n" + greska + "\nDatum se piše u formatu dd.mm.gggg: 02.03.2007.\nBroj se piše s najviše jednom decimalnom točkom: 123.45\nili\ns više točaka koje odvajaju tisućice i jednim decimalnim zarezom" );
	} else {
		greska = "";
		if( f.kolicina.value-0 <= 0 ) {
			greska += "Količina mora biti veća od nule!\n";
		} else if( f.kolicina.value*1 > f.hkol.value*1 ) {
			greska += "Količina ne smije biti veća od količine kojom raspočažete: " + f.hkol.value*1 + "!\n";
		}
		if( f.jedCijena.value-0 <= 0 ) {
			greska += "Jedinična cijena mora biti veća od nule!\n";
		}
		if( greska.length-0 > 0 ) {
			alert( "KRIVI UNOS\n\n" + greska +"\n" );
		} else {
			if( f.firstChild.firstChild.innerHTML.indexOf("Prodajte") != -1 ) {
				kolicina = -f.kolicina.value*1;
			} else {
				kolicina = f.kolicina.value*1;
			}
			pom = "portfelj=" + f.portfelj.value + "&vrsta=" + vrsta + "&ticker=" + ticker + "&datum=" + f.datum.value + "&kolicina=" + kolicina + "&jedCijena=" + f.jedCijena.value*1 + "&naknada=" + f.naknada.value*1;
			if( http && !uPogonuSam ) {

				x.value = "Transakcija u tijeku!";
				x.disabled = true;


				http.open("POST", "classes/akcije5.php?akcija=PromjeniStavkuPortfelja", true);
				http.onreadystatechange = function() {
					if( http.readyState == 4 ) {
						uPogonuSam = false;
						odg = http.responseText.split( "###" );

						if( odg[0] == "OK" ) {
//							location.reload();
							x.form.style.display = "none";
							x.disabled = false;
							x.value = "OK";
							
							try
							{
								document.getElementById("FormaPopisaPortfelja").innerHTML = odg[1];
							}
							catch (e){}
							
							var pp = document.getElementById("Portfelj_" + f.portfelj.value );
							try {
								pp.innerHTML = odg[2];
							} catch ( e ) {
								location.reload();
							}

						} else {
							alert( "Nije ok" );
							alert( http.responseText );
							x.disabled = false;
							x.value = "OK";
							x.form.style.display = "none";
						}
					}
				}
				uPogonuSam = true;
				http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
				http.send( pom );
			}
		}
	}
}

function jeDan(str) {
	reg=/^0?[1-9]$|^[1-2][0-9]$|^3[01]$/;
	return reg.test(str);
}

function jeMj(str) {
	reg=/^0?[1-9]$|^1[0-2]$/;
	return reg.test(str);
}

function jeGod(str) {
	reg=/^19[0-9][0-9]$|^20[0-9][0-9]$/;
	return reg.test(str);
}
function jeDatum( x ) {
	a = x.split(".");
	if( a.length < 3 ) return false;

	return jeDan( a[0] ) && jeMj( a[1] ) && jeGod( a[2] );
}
function jeBroj( str ) {
	reg=/^\d*\.?\d*$/;
	return reg.test( str );
}
function jeBrojZarez( str ) {
	reg = /^\d{1,3}(.\d{3})*(,\d*)?$/;
	return reg.test( str );
}
function PrikazValute( b ) {
	f = b.form;
	if( http && !uPogonuSam ) {
		b.value = "P r i č e k a j t e . . .";
		b.disabled = "true";
		http.open("POST", "classes/akcije5.php?akcija=PrikazValute", true);
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				document.getElementById("PrikazValute").innerHTML = http.responseText;
				onload();
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
		http.send( "valuta=" + f.val.value + "&d=" + f.d.value + "&m=" + f.m.value + "&Y=" + f.Y.value );
	}
}
function PrikazBanke( b ) {
	f = b.form;
	if( http && !uPogonuSam ) {
		b.value = "P r i č e k a j t e . . .";
		b.disabled = "true";
		http.open("POST", "classes/akcije5.php?akcija=PrikazBanke", true);
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				document.getElementById("PrikazBanke").innerHTML = http.responseText;
				onload();
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
		http.send( "banka=" + f.ban.value + "&d=" + f.d.value + "&m=" + f.m.value + "&Y=" + f.Y.value );
	}
}

function DodajFavoritaUTablicu( th, vrsta, ticker ) {
	if( http && !uPogonuSam ) {
		http.open("POST", "classes/akcije5.php?akcija=DodajFavoritaUTablicu", true);
		src = th.src;
		th.src = "images/cekanje.gif";
		th.alt = "[CEKAJ]";
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				odg = http.responseText.split( "###" );
				if( odg[0] == "OK" ) {
					document.getElementById( "FavTab" ).innerHTML = odg[1];
					onload();
				} else {
					alert( http.responseText );
				}
				th.src = src;
				th.alt = "[T]";
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
		http.send( "ticker="+ticker+"&vrsta="+vrsta );
	}
}
function DodajFavoritaUGrafove( th, vrsta, ticker, dana ) {
	if( dana < 1 ) {
		alert( "Broj dana mora biti veći od nule" );
		return;
	}
	if( http && !uPogonuSam ) {
		http.open("POST", "classes/akcije5.php?akcija=DodajFavoritaUGrafove", true);
		src = th.src;
		th.src = "images/cekanje.gif";
		th.alt = "[CEKAJ]";
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				odg = http.responseText.split( "###" );
				if( odg[0] == "OK" ) {

					df = document.getElementById( "FZDF" );
					a = document.createElement( "DIV" );
					a.innerHTML = odg[1];
					df.parentNode.insertBefore( a, df.nextSibling );

				} else {
					alert( http.responseText );
				}
			}
		}
		th.src = src;
		th.alt = "[G]";
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
		http.send( "ticker="+ticker+"&vrsta="+vrsta+"&dana="+dana );
	}
}

// bt - button koji kliknem
// id - id Favorita kojega mijenjam
// dana - vidim iz grafa
function SpremiBrojDanaGrafa( bt, id, vrsta, ticker ) {
	dana = bt.previousSibling.previousSibling.value;
	if( dana < 1 ) {
		alert( "Broj dana mora biti veći od nule" );
		return;
	}

	if( http && !uPogonuSam ) {
		bt.value = "Pričekajte";
		bt.disabled = "true";
		http.open("GET", "classes/akcije5.php?akcija=SpremiBrojDanaGrafa&id="+id+"&dana="+dana+"&vrsta="+vrsta+"&ticker="+ticker, true);
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				odg = http.responseText.split( "###" );
				if( odg[0] == "OK" ) {
					df = bt.parentNode.parentNode;
					df.innerHTML = odg[1];
				} else {
					alert( http.responseText );
				}
			}
		}
		bt.value = "Promijeni";
		bt.disabled = "false";
		uPogonuSam = true;
		http.send( null );
	}
}
// Hover koji se pojavljuje u tablicama
// ---
function PokaziHover( th, str ) {
	hvr = document.getElementById( "hover" );
	hvr.style.top = ( GdjeSam(th)-30 ) + "px";

	if ( th.offsetParent ) {
		curleft = th.offsetLeft
		while ( th = th.offsetParent) {
			curleft += th.offsetLeft
		}
	}
	hvr.style.left = curleft + "px";

	hvr.style.display = "";
	hvr.innerHTML = str;
}
function SakrijHover( th, str ) {
	hvr.style.display = "none";
}

function MjeseciUlaganjaFonda( tip, ticker ) {
	if( http && !uPogonuSam ) {
		http.open("POST", "classes/akcije5.php?akcija=MjeseciUlaganjaFonda", true);
		http.onreadystatechange = function() {
			if( http.readyState == 4 ) {
				uPogonuSam = false;
				document.getElementById( "MjeseciUlaganja" ).innerHTML = http.responseText;
			}
		}
		uPogonuSam = true;
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
		http.send( "ticker="+ticker+"&tip="+tip );
	}
}

function UlaganjeFonda( but, ticker, tip ) {
	but.disabled = true;
	but.value = "Pričekajte";
//	var http   = getHTTPObject();

	tip = ( but.parentNode.previousSibling[ but.parentNode.previousSibling.selectedIndex ].value);

	if( http ) {
		http.open("POST", "classes/akcije5.php?akcija=UlaganjaFonda", true);
		http.onreadystatechange = function() {
//			alert( "RS " + http.readyState );
			if( http.readyState == 4 ) {
				document.getElementById( "UlaganjaFonda" ).innerHTML = http.responseText;
			} 
		}
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");		
		http.send( "ticker="+ticker+"&tip="+tip+"&mjeGod="+but.form.mjesec.value );		
	}
	but.value = ">";
	but.disabled = false;
}

