
function openWindow(url, name, left, top, width, height)
{
	var obj=window.open(url, name, "width="+width+",height="+height+",status=no,resizable=yes,top="+top+",left="+left+",scrollbars=yes,dependent=yes,alwaysRaised=yes");
	obj.focus();
}

function openWindowDescription(url, name, left, top, width, height)
{
	var obj=window.open(url, name, "width="+width+",height="+height+",status=no,resizable=no,top="+top+",left="+left+",scrollbars=auto,toolbar=no,menubar=no,dependent=yes,alwaysRaised=yes");
	obj.focus();
}

function openWindowSubmit(url, name, left, top, width, height)
{
        document.plcomform.submit();
	var obj=window.open(url, name, "width="+width+",height="+height+",status=no,resizable=yes,top="+top+",left="+left+",scrollbars=yes,dependent=yes,alwaysRaised=yes");
	obj.focus();
}


function changeOpenerUrlClose(url)
{
	opener.location.href=url;
	self.close();
}


function plMenuComboChange(id, url)
{
	if (id.value > 0)
	{
	     location.href=url+id.value;
	}
}


function plcomInit()
{
        plcomChangeItem(-1);
        plcomChangeTransport();
        plcomPreparePriceGroups(0);
}


function plcomErrorCooler()
{
	var proc_id = document.plcomform.elements["plcoma["+plcomProcessorInRow+"][item_id]"].value;
        var cooler_id = document.plcomform.elements["plcoma["+plcomCoolerInRow+"][item_id]"].value;
        var needed = plcomProcCooler[proc_id];

        document.getElementById("pl2comerror1tr0").style.display = "none";
        document.getElementById("pl2comerror1tr0").style.visibility = "hidden";
        document.getElementById("pl2comerror1td0").innerText = " "
        /*
	if ((needed > 0) && (cooler_id == 0))
        {
        	// add cooler
                document.getElementById("pl2comerror1tr0").style.display = "";
                document.getElementById("pl2comerror1tr0").style.visibility = "visible";
                document.getElementById("pl2comerror1td0").innerText = " Do tego procesora potrzebny jest wentylator!"
        }
        if ((needed == 0) && (cooler_id > 0))
        {
        	// remove cooler
                document.getElementById("pl2comerror1tr0").style.display = "";
                document.getElementById("pl2comerror1tr0").style.visibility = "visible";
                document.getElementById("pl2comerror1td0").innerText = " Ten procesor nie potrzebuje wentylatora!"
        }
        */
}

function plcomErrorTransport()
{
        /*
	var no_error = isNumeric(document.plcomform.elements["plcoma[transport]"].value);

        document.getElementById("pl2comerror1tr1").style.display = "none";
        document.getElementById("pl2comerror1tr1").style.visibility = "hidden";
        document.getElementById("pl2comerror1td1").innerText = " "
        if (!no_error)
        {
        	// add cooler
                document.getElementById("pl2comerror1tr1").style.display = "";
                document.getElementById("pl2comerror1tr1").style.visibility = "visible";
                document.getElementById("pl2comerror1td1").innerText = " Pole to musi miec wartosc numeryczna."
        }
        */
}

function plcomErrorPayment()
{
        var no_error = isNumeric(document.plcomform.elements["plcoma[pay_init]"].value);

        document.getElementById("pl2comerror1tr2").style.display = "none";
        document.getElementById("pl2comerror1tr2").style.visibility = "hidden";
        document.getElementById("pl2comerror1td2").innerText = " "
        if (!no_error)
        {
        	// add cooler
                document.getElementById("pl2comerror1tr2").style.display = "";
                document.getElementById("pl2comerror1tr2").style.visibility = "visible";
                document.getElementById("pl2comerror1td2").innerText = "Pole 'wartosc poczatkowa' musi miec wartosc numeryczna."
        }
}

function preparePrice(price)
{
        var sprice = price.toString();
        var place = sprice.indexOf(".");
        if (place < 0) sprice += ".00";
        else
        {
        	if ((sprice.length - place) > 3) sprice = sprice.slice(0, (-(sprice.length - place - 3)));
                if ((sprice.length - place) == 2) sprice += "0";
        }
        return sprice;
}

function isNumeric(string) {
        if (string.search(/^[0-9.]*$/) != -1) return true;
        else return false;
}

function plcomChangeTransport()
{
        /*
	var price = document.plcomform.elements["plcoma[transport]"].value;

        if (!isNumeric(price)) price = 0;

        if (price < 10) price = 0;

        price = Math.floor(plcomTransportPerKm * price);

        document.plcomform.elements["plcoma[trans_price]"].value = preparePrice(price);
        document.plcomform.elements["plcoma[sum_price]"].value = preparePrice(parseFloat(document.plcomform.elements["plcoma[com_price]"].value) + price);

        plcomErrorTransport();
        */
        
        //plcomChangePayment();
}

function plcomChangePayment()
{
        var sum = document.plcomform.elements["plcoma[sum_price]"].value;
        var init = document.plcomform.elements["plcoma[pay_init]"].value;
        var months = document.plcomform.elements["plcoma[pay_months]"].value;
        var perMonth = 0

        if (!isNumeric(init)) init = 0;
	
	if (init >= sum) 
	{
		document.plcomform.elements["plcoma[pay_init]"].value = sum;
		init = sum;
	}

        if (months > 0)
        {
		if (document.plcomform.elements["plcoma[pay_init]"].value >= document.plcomform.elements["plcoma[sum_price]"].value) perMonth = 0;
		else perMonth = (((sum - init) + ((sum - init) * plcomPaymentPercentage * months)) / months);
        }
        else perMonth = 0;

        document.plcomform.elements["plcoma[pay_per_month]"].value = preparePrice(perMonth);
        plcomErrorPayment();
}

function plcomPreparePriceGroups(newValue)
{
	var sel = 0;
	for (var i = 0; i < plcomKeys.length; i++)
   {
		idx = plcomKeys[i];

		if (plcomEmptyKey[idx] == 0)
		{
        if (document.plcomform.elements["plcoma["+idx+"][price_gr]"].type == 'text')
        {
	        if (newValue)
	        {
	        	   document.plcomform.elements["plcoma["+idx+"][price_gr]"].value = document.plcomform.elements["plcoma["+plcomKeys[0]+"][price_gr]"].value;
	        }

			  switch (document.plcomform.elements["plcoma["+idx+"][price_gr]"].value)
	        {
					case 'B': document.plcomform.elements["plcoma["+idx+"][price_gr]"].style.backgroundColor = '#5AA2D4'; break;
	        		case 'C': document.plcomform.elements["plcoma["+idx+"][price_gr]"].style.backgroundColor = '#D45ABE'; break;
	        		case 'D': document.plcomform.elements["plcoma["+idx+"][price_gr]"].style.backgroundColor = '#45B347'; break;
	        		case 'A': document.plcomform.elements["plcoma["+idx+"][price_gr]"].style.backgroundColor = '#F60A0A'; break;
	        }

           if (document.plcomform.elements["plcoma["+idx+"][price_ed]"].value == 1) document.plcomform.elements["plcoma["+idx+"][price_gr]"].style.color = 'yellow';

        }
		}
	}
   plcomPreparePrices();
}

function plcomPriceEdited(key)
{
	if (document.plcomform.elements["plcoma["+key+"][item_id]"].selectedIndex == 0)
	{
		document.plcomform.elements["plcoma["+key+"][price_ed]"].value = 0;
		document.plcomform.elements["plcoma["+key+"][price]"].value = '0.00';
	}
	else
	{
		document.plcomform.elements["plcoma["+key+"][price_ed]"].value = 1;
		if (!isNumeric(document.plcomform.elements["plcoma["+key+"][price]"].value)) document.plcomform.elements["plcoma["+key+"][price]"].value = 0;
		document.plcomform.elements["plcoma["+key+"][price]"].value = preparePrice(document.plcomform.elements["plcoma["+key+"][price]"].value);
		// zmiana koloru
		document.plcomform.elements["plcoma["+key+"][price_gr]"].style.color = 'yellow';
   }
	plcomPreparePrices();
}

function plcomPreparePrices()
{
        var price = 0;
        var com_price = 0;
        var sel = 0;
        var q = 1;
        var idx = 0;

        var tmp_price = 0;
		  var mode = 0;
		  var vat = 1.22;

		  for (var i = 0; i < plcomKeys.length; i++)
        {
        	       idx = plcomKeys[i];

					 sel = document.plcomform.elements["plcoma["+idx+"][item_id]"].value;
	             q = document.plcomform.elements["plcoma["+idx+"][quantity]"].value;

                if (plcomEmptyKey[idx])
                {
							if (isNumeric(document.plcomform.elements["plcoma["+idx+"][price]"].value))
							{
								tmp_price = document.plcomform.elements["plcoma["+idx+"][price]"].value;
       					}
                     else
							{
								tmp_price = 0;
       					}
                }
                else
                {
						 mode = document.plcomform.elements["plpid"].value;

						 if (document.plcomform.elements["plcoma["+idx+"][price_ed]"].value == 0)
						 {
							 switch (document.plcomform.elements["plcoma["+idx+"][price_gr]"].value)
							 {
							 	case 'A': tmp_price = (mode == 0)?vat * plcomPricesA[sel]:plcomPricesA[sel]; break;
							 	case 'B': tmp_price = (mode == 0)?vat * plcomPricesB[sel]:plcomPricesB[sel]; break;
							 	case 'C': tmp_price = (mode == 0)?vat * plcomPricesC[sel]:plcomPricesC[sel]; break;
							 	case 'D': tmp_price = (mode == 0)?plcomPricesD[sel]:plcomPricesD[sel] / vat; break;
						    }
					  	 }
					  	 else
						 {
						 	tmp_price = document.plcomform.elements["plcoma["+idx+"][price]"].value;
						 }

                }

                if (plcomMainKey[i]) price = price + (q * tmp_price);
                com_price = com_price + (q * tmp_price);

                if (!price) price = 0;
                if (!tmp_price) tmp_price = 0;
                if (!com_price) com_price = 0;

                if (document.plcomform.elements["plcoma["+idx+"][price_ed]"].value == 0) document.plcomform.elements["plcoma["+idx+"][price]"].value = preparePrice(tmp_price * q);

        }
        document.plcomform.elements["plcoma[all_price]"].value = preparePrice(price);
        document.plcomform.elements["plcoma[com_price]"].value = preparePrice(com_price);
        //document.plcomform.elements["plcoma[sum_price]"].value = preparePrice(parseFloat(document.plcomform.elements["plcoma[trans_price]"].value) + com_price);
        document.plcomform.elements["plcoma[sum_price]"].value = preparePrice(com_price);
}

function plcomPrepareDesc()
{
        var sel = 0;
        var idx = 0;
       	var mhref = 0;

        for (var i = 0; i < plcomKeys.length; i++)
        {
        	idx = plcomKeys[i];
         if (!plcomEmptyKey[idx])
         {
                sel = document.plcomform.elements["plcoma["+idx+"][item_id]"].value;

                if (plcomDesc[sel])
                {
                        document.getElementById("pl2desc"+idx).style.display = "";
                        document.getElementById("pl2desc"+idx).style.visibility = "visible";
                        mhref = "javascript:openWindowDescription(\""+plcomDescUrl+sel+"\", \"opis\", \""+plcomDescLeft+"\", \""+plcomDescTop+"\", \""+plcomDescWidth+"\", \""+plcomDescHeight+"\")";
                        document.getElementById("pl2more"+idx).href = mhref;
                }
                else
                {
                        document.getElementById("pl2desc"+idx).style.display = "none";
                        document.getElementById("pl2desc"+idx).style.visibility = "hidden";
                        //document.getElementById("pl2desc"+idx).innerText = " ";
                }
          }
        }
}


function plcomChangeCategory()
{
        var sel = 0;
        sel = document.plcomform.elements["pliid"].value;
        if (sel) document.plcomform.submit();
}


function plcomPrintComputer($op)
{
        document.plcomform.elements["plop"].value = $op;
        document.plcomform.submit();
}

function plcomSendComputer($op)
{
        document.plcomform.elements["plop"].value = $op;
        document.plcomform.submit();
}

function plcomChangePriceEmpty()
{
        plcomPreparePrices();
        //plcomChangePayment();
}

function plcomChangeQuantity(key)
{
        var sel = document.plcomform.elements["plcoma["+key+"][quantity]"].value;
        if (!isNumeric(sel)) document.plcomform.elements["plcoma["+key+"][quantity]"].value = 1;

        plcomPreparePrices();
}


function plcomChangePriceGroup(key)
{
        var sel = document.plcomform.elements["plcoma["+key+"][price_gr]"].value;
        switch (sel)
        {
        		case 'A': document.plcomform.elements["plcoma["+key+"][price_gr]"].value = 'B'; document.plcomform.elements["plcoma["+key+"][price_gr]"].style.backgroundColor = '#5AA2D4'; break;
        		case 'B': document.plcomform.elements["plcoma["+key+"][price_gr]"].value = 'C'; document.plcomform.elements["plcoma["+key+"][price_gr]"].style.backgroundColor = '#D45ABE'; break;
        		case 'C': document.plcomform.elements["plcoma["+key+"][price_gr]"].value = 'D'; document.plcomform.elements["plcoma["+key+"][price_gr]"].style.backgroundColor = '#45B347'; break;
        		case 'D': document.plcomform.elements["plcoma["+key+"][price_gr]"].value = 'A'; document.plcomform.elements["plcoma["+key+"][price_gr]"].style.backgroundColor = '#F60A0A'; break;
        }
        plcomPreparePrices();
}

function plcomChangeProfit()
{
        plcomPreparePrices();
}

function plcomChangeItem(key)
{
        if (key > -1) 
		  {
			  	document.plcomform.elements["plcoma["+key+"][price_ed]"].value = 0;
			  	document.plcomform.elements["plcoma["+key+"][price_gr]"].style.color = '';
		  }

		  plcomPreparePrices();

        // show desc
        plcomPrepareDesc();

        //errors
        if (key < 0)
        {
                plcomErrorCooler();
        }
        else
        {
        	if (key == plcomCoolerInRow)
        	{
                        plcomErrorCooler();
        	}
        	if (key == plcomProcessorInRow)
        	{
                        plcomErrorCooler();
        	}
        }
        
        //plcomChangePayment();
        if (key > -1)
        {
                var txth = document.plcomform.elements["plcoma["+key+"][item_id]"].options[document.plcomform.elements["plcoma["+key+"][item_id]"].selectedIndex].text;
					 defaultStatus = "Ostatnio wybrany element: " + txth;
			}
}
