function LoadPopUp(cat)
{
imagePopup('t1001924.jpg', cat, 'PopInfo');
}

function ProductDetail(productId, quantity, itemId)
{
    var targetDiv = "lightbox";
    var url = 'resource/product_detail.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        //{    method: 'POST', parameters: 'pr='+productId, asynchronous:false, evalScripts:true }
        { method: 'POST', parameters: 'pr='+productId, asynchronous:true, evalScripts:true }
    );
    var element = "";
    return false;

}

function shoppingCart()
{
    var targetDiv = "lightbox";
    var url = 'resource/cart.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        { method: 'POST'}
    );
    var element = "";
    return false;

}

function cartProccess(stepid)
{
    var targetDiv = "lightbox";
    var url = 'resource/cart_proccess.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        { method: 'POST', parameters: 'step='+stepid, asynchronous:true, evalScripts:true }
        //{ method: 'POST'}
    );
    var element = "";
    return false;

}

function OsvojiNagradu(nagid)
{
    var targetDiv = "lightbox";
    var url = 'resource/nagrada.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        { method: 'POST', parameters: 'nag='+nagid, asynchronous:true, evalScripts:true }
        //{ method: 'POST'}
    );
    var element = "";
    return false;

}

function orderDetail(productId, quantity, itemId)
{
    var targetDiv = "lightbox";
    var url = 'resource/order_detail.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        {    method: 'POST', parameters: 'or='+productId, asynchronous:false, evalScripts:true }
    );
    var element = "";
    return false;

}

function AdminOrder(productId, quantity, itemId)
{
    var targetDiv = "lightbox";
    var url = 'order_detail.php';

    $(targetDiv).innerHTML = '<img src="../resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        {    method: 'POST', parameters: 'or='+productId, asynchronous:false, evalScripts:true }
    );
    var element = "";
    return false;

}


function removeProduct(prodid)
{
    var targetDiv1 = "btm_content";
    //var targetDiv1 = "lightbox";
    var url = 'resource/cart_product.php';

    $(targetDiv1).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv1);

    var ajax = new Ajax.Updater(
        {success: targetDiv1},
        url,
        {    method: 'POST', parameters: 'pid='+prodid+'&action=delProduct' }
    );
    var o=document.getElementById('kolicina_' + prodid);
        if(o)
        {
            document.getElementById('kolicina_' + prodid).value = '';
        }
    var element = "";
    return false;

}

function DodajProduct(pid, vendor)
{
    var pname = document.getElementById('product_name_' + pid).value;
    var price = document.getElementById('product_price_' + pid).value;
    var qnty = document.getElementById('kolicina_' + pid).value;
    var sku = document.getElementById('product_sku_' + pid).value;
    var valuta = document.getElementById('product_valuta_' + pid).value;
    var unit = document.getElementById('product_unit_' + pid).value;

    if(!parseInt(qnty)){
    	alert('Molimo unesite kolicinu!');
		return false;
    }

    var targetDiv1 = "btm_content";
    var url = 'resource/cart_product.php';

    $(targetDiv1).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv1);

    var ajax = new Ajax.Updater(
        {success: targetDiv1},
        url,
        {    method: 'POST', parameters: 'pid='+pid+'&action=addProduct&product_name='+pname+'&product_price='+price+'&kolicina='+qnty+'&sku='+sku+'&val='+valuta+'&unit='+unit+'&vendor='+vendor }
    );
    var element = "";
    var targetDiv1 = "";
    return false;

}

function DodajProductPop(pid, vendor)
{
    var pname = document.getElementById('product_name_' + pid).value;
    var price = document.getElementById('product_price_' + pid).value;
    var qnty = document.getElementById('kolicina_pop_' + pid).value;
    var sku = document.getElementById('product_sku_' + pid).value;
    var valuta = document.getElementById('product_valuta_' + pid).value;
    var unit = document.getElementById('product_unit_' + pid).value;

    if(!parseInt(qnty)){
    	alert('Molimo unesite kolicinu!');
		return false;
    }

    document.getElementById('kolicina_' + pid).value = qnty;

    var targetDiv1 = "btm_content";
    var url = 'resource/cart_product.php';

    $(targetDiv1).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv1);

    var ajax = new Ajax.Updater(
        {success: targetDiv1, failure: targetDiv1 },
        url,
        {    method: 'POST', parameters: 'pid='+pid+'&action=addProduct&product_name='+pname+'&product_price='+price+'&kolicina='+qnty+'&sku='+sku+'&val='+valuta+'&unit='+unit+'&vendor='+vendor }
    );

    var element = "";
    var targetDiv1 = "";
    return false;

}

function IzmeniAdresu(action, addressid)
{
    var targetDiv = "lightbox";
    var url = 'resource/user_address.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        {    method: 'POST', parameters: 'action='+action+'&address_id='+addressid }
    );
    var element = "";
    return false;
}

function IzmeniAdresuProc(artion, addid)
{
    //if!(action)){
    //	alert('Nemam akciju:' + artion);
	//	return false;
    //} else {
    //alert('Imam akciju:' + action);
	//	return false;
    //}

    if (artion == 'addaddress') {
    var address_type_name = document.getElementById('aaddress_type_name').value;
    var company = document.getElementById('acompany').value;
    var pib = document.getElementById('apib').value;
    var ime = document.getElementById('aime').value;
    var prezime = document.getElementById('aprezime').value;
    var ulica = document.getElementById('aulica').value;
    var city = document.getElementById('acity').value;
    var zip = document.getElementById('azip').value;
    var drzava = document.getElementById('adrzava').value;
    var ref = document.getElementById('aref').value;
    var tel = document.getElementById('atelefon').value;
    } else if (artion == 'editaddress') {
    var address_type_name = document.getElementById('address_type_name').value;
    var company = document.getElementById('company').value;
    var pib = document.getElementById('pib').value;
    var ime = document.getElementById('ime').value;
    var prezime = document.getElementById('prezime').value;
    var ulica = document.getElementById('ulica').value;
    var city = document.getElementById('city').value;
    var zip = document.getElementById('zip').value;
    var drzava = document.getElementById('drzava').value;
    var ref = document.getElementById('ref').value;
    var tel = document.getElementById('telefon').value;
    } else {
        alert('Nemam akciju:' + artion);
		return false;
    }

    var targetDiv = "lightbox";
    var url = 'resource/user_address_proccess.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        //{    method: 'POST' }
        {    method: 'POST', parameters: 'act1='+artion+'&id='+addid+'&type_name='+address_type_name+'&company='+company+'&pib='+pib+'&ime='+ime+'&prezime='+prezime+'&ulica='+ulica+'&city='+city+'&zip='+zip+'&drzava='+drzava+'&ref='+ref+'&tel='+tel, asynchronous:true, evalScripts:true }
    );
    var element = "";
    return false;
}



function IzvrsiPorudzbinu(pid)
{
    var adresa_id = document.getElementById('isporuka_adresa_id').value;
    var isporuka_id = document.getElementById('isporuka_id').value;
    var placanje_id = document.getElementById('placanje_id').value;
    var komentar = document.getElementById('comment').value;

    if(!parseInt(adresa_id)){
    	alert('Molimo izaberite adresu isporuke!');
        Neka(1);
		return false;
    }

    if(!parseInt(isporuka_id)){
    	alert('Molimo izaberite nacin isporuke!');
		return false;
    }

    if(!parseInt(placanje_id)){
    	alert('Molimo izaberite nacin placanja!');
		return false;
    }

    var targetDiv = "lightbox";
    var url = 'resource/porudzbina.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        {    method: 'POST', parameters: 'adresa='+adresa_id+'&isporuka='+isporuka_id+'&placanje='+placanje_id+'&comment='+komentar }
    );
    var element = "";
    return false;
}

function PopInfo(clanak)
{
    var targetDiv = "lightbox";
    var url = 'resource/pops.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        { method: 'POST', parameters: 'clanak='+clanak}
    );
    var element = "";
    return false;
}

function changePassword()
{
    var targetDiv = "lightbox";
    var url = 'resource/passchange.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        { method: 'POST'}
    );
    var element = "";
    return false;
}


function imagePopup(imageSrc, productId, popupType)
{
	if (imageSrc.indexOf("www.youtube.com") > -1)
	{
		imageSrc = escape(imageSrc);
	}

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	var objOverlay = document.getElementById("overlay");
	objOverlay.onclick = function() { hideLightbox(); return false; }
	objOverlay.style.display = 'block';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
 	objOverlay.style.height = arrayPageSize[1] + 'px';

	var objLightbox = document.getElementById("lightbox");
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';

	// preload image
	imgPreload = new Image();
	imgPreload.onload = function()
	{
		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport

		var width = imgPreload.width;
		var height = imgPreload.height;

		if (width < 400)
		{
			width = 400;
		}

		if (height < 300)
		{
			height = 300;
		}

		switch (popupType)
		{
			case "galleryPopup":
				var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 400 - height) / 2); // bilo 35
				var lightboxLeft = ((arrayPageSize[0] - 170 - width) / 2);
				break;
			case "advancedSearch":
				var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 400 - height) / 2); // bilo 35
				var lightboxLeft = ((arrayPageSize[0] - 50 - width) / 2);
				break;
			case "imageLarge":
				var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 400 - height) / 2); // bilo 35
				var lightboxLeft = ((arrayPageSize[0] - 50 - width) / 2);
				break;
			case "ProductDetail":
			case "shoppingCartHistory":
            case "orderArchive":
            case "cartProccess":
            case "changePass":
            case "IzmeniAdresus":
            case "DodajAdresu":
            case "AdminOrder":
            case "OsvojiNagradu":
				//var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - height - 80) / 2); // bilo 35
                var lightboxTop = arrayPageScroll[1] + 80; // bilo 35
                var lightboxLeft = ((arrayPageSize[0] - width - 200) / 2);
				break;
            case "addProduct":
            case "PopInfo":
				var lightboxTop = arrayPageScroll[1] + 80; // bilo 35
                var lightboxLeft = ((arrayPageSize[0] - 600) / 2);
				break;
            case "delProduct":
                var lightboxTop = 0;
                var lightboxLeft = 0;
                break;
		}

		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";

		// Hide select boxes as they will 'peek' through the image in IE
		selects = document.getElementsByTagName("select");

		for (i = 0; i != selects.length; i++)
		{
			selects[i].style.visibility = "hidden";
        }

		objLightbox.style.display = 'block';

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');

		switch (popupType)
		{
			case "galleryPopup":
				// ***** ajax gallery popup ***** //
				galleryPopup(productId, imageSrc);
				// ***** /ajax gallery popup ***** //

				break;
			case "advancedSearch":
				// ***** ajax advanced search popup ***** //
				advancedSearchPopup();
				// ***** /ajax advanced search popup ***** //

				break;
			case "imageLarge":
				// ***** ajax advanced search popup ***** //
				imageLarge(imageSrc, productId);
				// ***** /ajax advanced search popup ***** //

				break;

            case "addProduct":
				// ***** Dodajem proizvod u korpu ***** //
				var action = "addProduct";
                hideLightbox();
                var qnty = document.getElementById('kolicina_' + productId).value;
                if(!parseInt(qnty)){
				alert('Molimo unesite kolicinu!');
				return false;
			    }
                DodajProduct(productId);
				break;

            case "delProduct":
                // ***** Brisem proizvod iz korpe ***** //
	            var action = "delProduct";
                hideLightbox();
                removeProduct(productId);
				break;

			case "ProductDetail":
				// ***** ajax shopping cart popup ***** //
				if (productId.indexOf("|") > -1)
				{
					var temp = productId.split("|");
					ProductDetail(temp[0], temp[1], temp[2]);
				}
				else
				{
					ProductDetail(productId);
				}
				// ***** /ajax shopping cart popup ***** //

				break;
			case "shoppingCartHistory":
				shoppingCart();
				break;

            case "changePass":
                changePassword();
				break;

            case "orderArchive":
				orderDetail(productId);
				break;

            case "AdminOrder":
				AdminOrder(productId);
				break;

            case "IzmeniAdresus":
                var akcija = "edit";
                IzmeniAdresu(akcija, productId);
				break;

            case "DodajAdresu":
                var akcija = "add";
                IzmeniAdresu(akcija, productId);
				break;

            case "cartProccess":
                cartProccess(productId);
				break;

            case "PopInfo":
                PopInfo(productId);
				break;

            case "OsvojiNagradu":
                OsvojiNagradu(productId);
				break;
		}

		window.setTimeout("imgPreload.src = null", 100);

		return false;
	}

	if (imageSrc.indexOf("www.youtube.com") > -1)
	{
		imgPreload.src = "blank.gif";
	}
	else
	{
		imageSrc = (imageSrc == "")? "blank.gif" : imageSrc;
		imgPreload.src = imageSrc;
	}

	return false;
}

function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");

	for (i = 0; i != selects.length; i++)
	{
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';

	window.clearTimeout();
}


function getPageSize()
{
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	}
	else if (document.body.scrollHeight > document.body.offsetHeight)
	{ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	}
	else
	{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;

	if (self.innerHeight)
	{ // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
	{ // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{ // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight)
	{
		pageHeight = windowHeight;
	}
	else
	{
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth)
	{
		pageWidth = windowWidth;
	}
	else
	{
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);

	return arrayPageSize;
}

function getPageScroll()
{
	var yScroll;

	if (self.pageYOffset)
	{
		yScroll = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{ // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	}
	else if (document.body)
	{ // all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)

	return arrayPageScroll;
}

function change(id){

    switch (id)
    {
    case 2:
        document.getElementById(2).style.display = "";
        document.getElementById(1).style.display = "none";
        break;
    default:
        document.getElementById(1).style.display = "";
        document.getElementById(2).style.display = "none";
    }

}

function DetaljiKorpe(){

   if (document.getElementById('korpa').style.display == "none"){
        document.getElementById('korpa').style.display = "";
   }else{
        document.getElementById('korpa').style.display = "none";
   }
}

function PokaziVideo(){

   if (document.getElementById('video').style.display == "none"){
        document.getElementById('video').style.display = "";
   }else{
        document.getElementById('video').style.display = "none";
   }
}
function Show(){
     ID = document.getElementById(2);

     if(ID.style.display == ""){
          document.getElementById(2).style.display = "none";
          document.getElementById(3).style.display = "none";
          document.getElementById(4).style.display = "none";
          document.getElementById(5).style.display = "none";
     }else{
          document.getElementById(2).style.display = "";
          document.getElementById(3).style.display = "";
          document.getElementById(4).style.display = "";
          document.getElementById(5).style.display = "";
     }
}

function Neka(nextid){

    switch (nextid)
    {
    case 1:
                document.getElementById('korpa').style.display = "none";
                document.getElementById(1).style.display = "";
                document.getElementById('1a').style.backgroundColor = "#E0E0E0";
                //document.getElementById('1b').style.display = "none";
                document.getElementById(2).style.display = "none";
                //document.getElementById('2b').style.display = "";
                break;
    case 2:
                document.getElementById('korpa').style.display = "none";
                document.getElementById(1).style.display = "none";
                //document.getElementById('1b').style.display = "";
                document.getElementById(2).style.display = "";
                document.getElementById('2a').style.backgroundColor="#E0E0E0";
                //document.getElementById('2b').style.display = "none";
                break;
    default:
	   break;
    }
}


function changeBoxStatus(id1){
    switch (id1)
    {
    case 22:
    document.getElementById('radiovred').value = "fizicko";
    document.getElementById(22).style.display = "";
    document.getElementById(21).style.display = "none";
    break;
    default:
    document.getElementById('radiovred').value = "pravno";
    document.getElementById(21).style.display = "";
    document.getElementById(22).style.display = "none";
    }
}


function IsporukaProccess(stepid)
{
    var adresa_id = document.getElementById('isporuka_adresa_id').value;
    var isporuka_id = document.getElementById('isporuka_id').value;
    var placanje_id = document.getElementById('placanje_id').value;

    var targetDiv = "lightbox";
    var url = 'resource/cart_proccess.php';

    $(targetDiv).innerHTML = '<img src="resource/ajax-loader.gif" />';

    var element = $(targetDiv);

    var ajax = new Ajax.Updater(
        {success: targetDiv},
        url,
        { method: 'POST', parameters: 'step='+stepid+'&adresa='+adresa_id+'&isporuka='+isporuka_id+'&placanje='+placanje_id, asynchronous:true, evalScripts:true }
        //{ method: 'POST', parameters: 'step='+stepid+'&adresa='+adresa_id+'&isporuka='+isporuka_id+'&placanje='+placanje_id }
        //{ method: 'POST'}
    );
    var element = "";
    return false;

}

function SwitchAddress(id, count, sifra){
        count1 = count +1;
        for (i = 0; i != count1; i++)
	    {
            if (i == id){
		    document.getElementById('address'+i).style.display = "";
            document.getElementById('isporuka_adresa_id').value=sifra;
            }else{
            document.getElementById('address'+i).style.display = "none";
            }
	    }
}

function DisplayAddress(id, count){
        for (i=1; i <= count; i++)
	    {
            if (i == id){
		    document.getElementById('address'+i).style.display = "";
            } else {
            document.getElementById('address'+i).style.display = "none";
            }
	    }
}


function callInProgress (xmlhttp) {
switch (xmlhttp.readyState) {
case 1: case 2: case 3:
return true;
break;
// Case 4 and 0
default:
return false;
break;
}
}
function showFailureMessage() {
alert('Trenutni problem u komunikaciji. Probajte ponovo.');
}
// Register global responders that will occur on all AJAX requests
Ajax.Responders.register({
onCreate: function(request) {
request['timeoutId'] = window.setTimeout(
function() {
// If we have hit the timeout and the AJAX request is active, abort it and let the user know
if (callInProgress(request.transport)) {
request.transport.abort();
showFailureMessage();
// Run the onFailure method if we set one up when creating the AJAX object
if (request.options['onFailure']) {
request.options['onFailure'](request.transport, request.json);
}
}
},
14000 // Five seconds
);
},
onComplete: function(request) {
// Clear the timeout, the request completed ok
window.clearTimeout(request['timeoutId']);
}
});

