function prodInfo(sifra){
	window.open("product_info.php?s=" + sifra,"_blank","height=400,width=800,top=100,left=100,resizable=yes,scrollbars=yes");
}

function prodPicture(sifra){
	window.open("product_picture.php?s=" + sifra,"_blank","height=400,width=600,top=100,left=100,resizable=yes,scrollbars=yes");
}

function prodPicture_mo(sifra){
	window.open("mo_picture.php?s=" + sifra,"_blank","height=400,width=600,top=100,left=100,resizable=yes,scrollbars=yes");
}

function initProgres(){
	var objDivProgres = document.getElementById("divProgres");
	var le;
	objDivProgres.style.display = "none";
	le = (screen.availWidth / 2) - 310;
	objDivProgres.style.left = le + "px";
}

function korpaUbaci(roba_id){
	ajaxObjectArray[ajaxObjectArray.length] = new ajax();
	var ajaxIndex = ajaxObjectArray.length-1;
	var objDivProgres = document.getElementById("divProgres");
	objDivProgres.style.display = "block";
	var kolicina;
	var objKolicina = document.getElementById("txtKolicina" + roba_id);
	if (objKolicina.value == ""){
		kolicina = 1;
		objKolicina.value = 1;
	} else {
		kolicina = objKolicina.value;
		// sledeci korak povecava sadrzaj korpe ukoliko je podeseno u korpa_ajax.php da prihvata dodavanje unete kolicine na postojecu
		// umesto da se upisuje uneta kolicina u bazu
		// mislim da je to neprakticno da se napravi
		//objKolicina.value = objKolicina.value + kolicina;
	}
	ajaxObjectArray[ajaxIndex].queryString = 'ac=ins&roba=' + roba_id + '&kol=' + kolicina;
	ajaxObjectArray[ajaxIndex].requestFile = "korpa_ajax.php";
	ajaxObjectArray[ajaxIndex].onCompletion = function() { getKorpa(ajaxIndex); };	// Specify function that will be executed after file has been found					
	ajaxObjectArray[ajaxIndex].onError = function() { handleError(ajaxIndex); };
	ajaxObjectArray[ajaxIndex].onFail = function() { handleFail(); };
	ajaxObjectArray[ajaxIndex].initRequest();		// Execute AJAX function
}

function getKorpa(ajaxIndex){
	var retValue = eval("(" + ajaxObjectArray[ajaxIndex].response + ")");
	//postavljam progres bar
	var objDivProgres = document.getElementById("divProgres");
	
	if(retValue.isDone){
		var objDivKorpa = document.getElementById("korpaKolicina");
		objDivKorpa.innerHTML = retValue.kol;
		var objDivIznos = document.getElementById("korpaVrednost");
		objDivIznos.innerHTML = 'Vaša korpa ima ' + retValue.kol + ' proizvoda.<br />Vrednost: <div id="korpaIznos" class="divKorpaKol">' + retValue.iznos + '</div>';
	}

	ajaxObjectArray[ajaxIndex] = false; //raskidam vezu sa ajax zahtevom
	objDivProgres.style.display = "none";
	//alert(retValue.poruka);
}

function korpaIzbaci(roba_id){
	if(confirm("Da li ste sigurni da želite da obrišete izabrani artikal?")){
		ajaxObjectArray[ajaxObjectArray.length] = new ajax();
		var ajaxIndex = ajaxObjectArray.length-1;
		var objDivProgres = document.getElementById("divProgres");
		objDivProgres.style.display = "block";
	
		ajaxObjectArray[ajaxIndex].queryString = 'ac=del&roba=' + roba_id;
		ajaxObjectArray[ajaxIndex].requestFile = "korpa_ajax.php";
		ajaxObjectArray[ajaxIndex].onCompletion = function() { getKorpaDel(ajaxIndex); };	// Specify function that will be executed after file has been found					
		ajaxObjectArray[ajaxIndex].onError = function() { handleError(ajaxIndex); };
		ajaxObjectArray[ajaxIndex].onFail = function() { handleFail(); };
		ajaxObjectArray[ajaxIndex].initRequest();		// Execute AJAX function
	}
}

function getKorpaDel(ajaxIndex){
	var retValue = eval("(" + ajaxObjectArray[ajaxIndex].response + ")");
	//postavljam progres bar
	var objDivProgres = document.getElementById("divProgres");
	
	ajaxObjectArray[ajaxIndex] = false; //raskidam vezu sa ajax zahtevom
	objDivProgres.style.display = "none";
	if(retValue.isDone){
		window.location.href = "korpa_pregled.php";
	} else {
		window.location.href = "products.php";
	}
}

function korpaPromeni(roba_id){
	ajaxObjectArray[ajaxObjectArray.length] = new ajax();
	var ajaxIndex = ajaxObjectArray.length-1;
	var objKolicina = document.getElementById("txtKol" + roba_id);
	var objDivProgres = document.getElementById("divProgres");
	objDivProgres.style.display = "block";

	ajaxObjectArray[ajaxIndex].queryString = 'ac=edt&roba=' + roba_id + '&kol=' + objKolicina.value;
	ajaxObjectArray[ajaxIndex].requestFile = "korpa_ajax.php";
	ajaxObjectArray[ajaxIndex].onCompletion = function() { getKorpaEdit(ajaxIndex); };	// Specify function that will be executed after file has been found					
	ajaxObjectArray[ajaxIndex].onError = function() { handleError(ajaxIndex); };
	ajaxObjectArray[ajaxIndex].onFail = function() { handleFail(); };
	ajaxObjectArray[ajaxIndex].initRequest();		// Execute AJAX function
}

function getKorpaEdit(ajaxIndex){
	var retValue = eval("(" + ajaxObjectArray[ajaxIndex].response + ")");
	//postavljam progres bar
	var objDivProgres = document.getElementById("divProgres");
	
	ajaxObjectArray[ajaxIndex] = false; //raskidam vezu sa ajax zahtevom
	objDivProgres.style.display = "none";
	//alert(retValue.poruka);
	window.location.href = "korpa_pregled.php";
}

function snimi(){
	ajaxObjectArray[ajaxObjectArray.length] = new ajax();
	var ajaxIndex = ajaxObjectArray.length-1;
	var objDivProgres = document.getElementById("divProgres");
	objDivProgres.style.display = "block";

	ajaxObjectArray[ajaxIndex].queryString = 'ac=sav';
	ajaxObjectArray[ajaxIndex].requestFile = "korpa_ajax.php";
	ajaxObjectArray[ajaxIndex].onCompletion = function() { snimljeno(ajaxIndex); };	// Specify function that will be executed after file has been found					
	ajaxObjectArray[ajaxIndex].onError = function() { handleError(ajaxIndex); };
	ajaxObjectArray[ajaxIndex].onFail = function() { handleFail(); };
	ajaxObjectArray[ajaxIndex].initRequest();		// Execute AJAX function
}

function snimljeno(ajaxIndex){
	var retValue = eval("(" + ajaxObjectArray[ajaxIndex].response + ")");
	//postavljam progres bar
	var objDivProgres = document.getElementById("divProgres");
	ajaxObjectArray[ajaxIndex] = false; //raskidam vezu sa ajax zahtevom
	objDivProgres.style.display = "none";
	if(retValue.isDone){
		alert(retValue.poruka);
		window.location.href = "user_info.php";
	}
}

function naruci(){
	return confirm('Ovo je nepovratni postupak.\nDa posaljem narudzbinu?');
}

// formira objekat sa vrednostima za roba_id i kolicinu
function prodKol(robaID,kolicina){
	this.rid = robaID;
	this.kol = kolicina;
}

// postavljam vrednosti za kolicinu u text boxove za odredjenu robu
function podesiKolicine(){
	var i, k;
	var roba, kolic;
	var objKolicina;
	
	if(robaKorpa.length > 0){
		k = robaKorpa.length;
		for (i = 0; i < k; i++){
			roba = robaKorpa[i].rid;
			kolic = robaKorpa[i].kol;
			if(document.getElementById("txtKolicina" + roba)){
				objKolicina = document.getElementById("txtKolicina" + roba);
				objKolicina.value = kolic;	
			}
		}
	}
}

// proveravam da li je korisnik logovan
function check_user(){
	if(document.getElementById("hdnKupac")){
		if(document.getElementById("hdnWarning")){
			alert("Ne mozete realizovati narudzbinu dok ne sredite kolicine.");
		} else {
			document.frmKorpa.submit();
		}
	} else {
		if(confirm("Morate se ulogovati da biste nastavili sa kupovinom")){
			window.location.href = "login.php?page=nalog";
		}
	}
}

// brisem sadrzaj korpe
function isprazni(){
	if(confirm("Ovim postupkom brisete ceo sadrzaj korpe.\nDa nastavimo?")){
		window.location.href = "products.php?act=del";
	}
}

// nastavljam kupovinu
function dodaj(){
	window.location.href = "products.php";
}

// prikaz izbora kategorija
function show_products(){
	var objMenu;
	
	objMenu = document.getElementById("prodMenu");
	
	if(objMenu.style.display == "none"){
		objMenu.style.display = "block";		
	} else {
		objMenu.style.display = "none";		
	}
}

// trebalo bi da snimi stavku kada se stisne enter
function save_item(oEvent, roba_id){
	if(oEvent.keyCode == 13){
		korpaPromeni(roba_id);
	}
	//ispis = "Tip: " + oEvent.type + ", target: " + (oEvent.target || oEvent.srcElement).id + ", taster: " + oEvent.keyCode + ".";
	//alert(ispis);
}

// ubacuje u korpu kod prikaza informacija o artiklu
function infoKorpaUbaci(roba_id){
	ajaxObjectArray[ajaxObjectArray.length] = new ajax();
	var ajaxIndex = ajaxObjectArray.length-1;
	var objDivProgres = document.getElementById("divProgres");
	objDivProgres.style.display = "block";
	var kolicina;
	var objKolicina = document.getElementById("txtKolicina" + roba_id);
	if (objKolicina.value == ""){
		kolicina = 1;
		objKolicina.value = 1;
	} else {
		kolicina = objKolicina.value;
		// sledeci korak povecava sadrzaj korpe ukoliko je podeseno u korpa_ajax.php da prihvata dodavanje unete kolicine na postojecu
		// umesto da se upisuje uneta kolicina u bazu
		// mislim da je to neprakticno da se napravi
		//objKolicina.value = objKolicina.value + kolicina;
	}
	ajaxObjectArray[ajaxIndex].queryString = 'ac=ins&roba=' + roba_id + '&kol=' + kolicina;
	ajaxObjectArray[ajaxIndex].requestFile = "korpa_ajax.php";
	ajaxObjectArray[ajaxIndex].onCompletion = function() { infoGetKorpa(ajaxIndex); };	// Specify function that will be executed after file has been found					
	ajaxObjectArray[ajaxIndex].onError = function() { handleError(ajaxIndex); };
	ajaxObjectArray[ajaxIndex].onFail = function() { handleFail(); };
	ajaxObjectArray[ajaxIndex].initRequest();		// Execute AJAX function
}

function infoGetKorpa(ajaxIndex){
	var retValue = eval("(" + ajaxObjectArray[ajaxIndex].response + ")");
	//postavljam progres bar
	var objDivProgres = document.getElementById("divProgres");

	ajaxObjectArray[ajaxIndex] = false; //raskidam vezu sa ajax zahtevom
	objDivProgres.style.display = "none";

	if(retValue.isDone){
		window.location.href = "korpa_pregled.php";
	}
	//alert(retValue.poruka);
}

// trebalo bi da snimi stavku kada se stisne enter
function save_info_item(oEvent, roba_id){
	if(oEvent.keyCode == 13){
		infoKorpaUbaci(roba_id);
	}
}

// trebalo bi da snimi stavku kada se stisne enter
function add_item(oEvent, roba_id){
	if(oEvent.keyCode == 13){
		korpaUbaci(roba_id);
	}
}

