//Variables
var uniqueParamCounter = 0;
var uniqueCatCounter = 0;

arrayFind = function(ary, element){
	for(var i=0; i<ary.length; i++){
		if(ary[i] == element){
			return true;
		}
	}
	return false;
}

//Show or hide element
function show_hide(element_id,display_type){
	display=document.getElementById(element_id).style.display;
	if(display==display_type){
		display='none';
	}else{
		display=display_type;
	}
	document.getElementById(element_id).style.display=display;
	//alert(display);
}

//Calculate prices for admin panel
function admin_shop_calculate_prices(){
	promotion_price = document.getElementsByName('data[Product][promotion_price]');
	promotion_price = promotion_price[0];
	prod_price = document.getElementsByName('data[Product][price]');
	prod_price = prod_price[0];
	margin = document.getElementsByName('data[Product][margin]');
	margin = margin[0];
	nett = document.getElementsByName('data[Product][nett]');
	nett = nett[0];
	
	if(1*promotion_price.value < 1*prod_price.value){
		price=promotion_price.value;
	}else{
		price=prod_price.value;
	}
	
	nett.value = Math.round( price / (1.23*(1+(margin.value/100))) * 100) / 100;
}

function admin_shop_calculate_margin(){
	promotion_price = document.getElementsByName('data[Product][promotion_price]');
	promotion_price = promotion_price[0];
	prod_price = document.getElementsByName('data[Product][price]');
	prod_price = prod_price[0];
	margin = document.getElementsByName('data[Product][margin]');
	margin = margin[0];
	nett = document.getElementsByName('data[Product][nett]');
	nett = nett[0];
	
	if(1*promotion_price.value < 1*prod_price.value){
		price=promotion_price.value
	}else{
		price=prod_price.value
	}
	
	margin.value = Math.floor((100 * price) / ((nett.value * 1.23) -1) * 1000) / 1000;
}

function admin_shop_calculate_from_nett(){
	promotion_price = document.getElementsByName('data[Product][promotion_price]');
	promotion_price = promotion_price[0];
	prod_price = document.getElementsByName('data[Product][price]');
	prod_price = prod_price[0];
	margin = document.getElementsByName('data[Product][margin]');
	margin = margin[0];
	nett = document.getElementsByName('data[Product][nett]');
	nett = nett[0];
	
	if(1*promotion_price.value < 1*prod_price.value){
		price=promotion_price.value
	}else{
		price=prod_price.value
	}
	
	prod_price.value = Math.ceil(nett.value * 1.23 * (1 + margin.value/100));
	promotion_price.value = prod_price.value;
}

//Changes category in admin product view
function admin_goToCategory(){
	cid = document.getElementById('contextualMenu').value;
	location.href="/admin/kategorie/"+cid;
}
//Changes category in admin explorer view
function admin_explorer_goToCategory(){
	cid = document.getElementById('contextualMenu').value;
	location.href="/admin/produkty/explorer/"+cid;
}
//Changes category in admin parameters view
function admin_goToParameters(){
	cid = document.getElementById('contextualMenu').value;
	location.href="/admin/parametry/"+cid;
}

//Moves Row in table upp or down
function moveRow(row,direction){
	if(row.rowIndex-direction > 1 && row.rowIndex-direction < row.parentNode.rows.length-1){
		index1 = row.rowIndex;
		index2 = row.rowIndex-direction;
		//ZAPAMIĘTAJ WARTOŚCI value i name
			
		//WYKONAJ ZAMIANĘ
			
			var c1 = row.parentNode.rows[index1].cells;//alert(c1.length);
			var c2 = row.parentNode.rows[index2].cells;
			limit = c1.length-1;
			limit = c2.length-1;
			for(i=0;i<=limit;i++){
				row.parentNode.rows[index2].appendChild(c1[0]);//alert('c1:'+i+'/'+limit);
				
			}
			
			for(i=0;i<=limit;i++){
				row.parentNode.rows[index1].appendChild(c2[0]);//alert('c2:'+i+'/'+limit);
			}
		//PRZYWRÓĆ WARTOŚCI
			
		//PRZELICZ KOLEJNOŚC
			recalculateTableOrder(row.parentNode);		
	}
}

function recalculateTableOrder(table){
	for(i=0;i<table.rows.length-1;i++){
		row = table.rows[i];
		elements = row.getElementsByTagName('input');
		for(j=0;j<elements.length;j++){
			element = elements[j];
			if(element.name.indexOf('order') != -1) element.value = row.rowIndex/(table.rows.length-1);
		}
	}
}

function addTableRow(table,index){
	index += 1; 
	table.insertRow(index);
	newId = table.rows.length-4;
	table.rows[index].innerHTML = ''+
			'<td><img src="/img/icons/up_24.png" alt="" onclick="moveRow(this.parentNode.parentNode,1)" style="margin:1px;"/><img src="/img/icons/down_24.png" alt="" onclick="moveRow(this.parentNode.parentNode,-1)" style="margin:1px;"/><br/><img src="/img/icons/add_24.png" alt="" onclick="addTableRow(this.parentNode.parentNode.parentNode,this.parentNode.parentNode.rowIndex)" style="margin:1px;"/><img src="/img/icons/remove_24.png" alt="" onclick="deleteTableRow(this.parentNode.parentNode.parentNode,this.parentNode.parentNode.rowIndex)" style="margin:1px;"/></td>'+
			'<td><div class="input text"><input name="data[Parameter]['+newId+'][name]" type="text" value="" /></div></td>'+
			'<td><div class="input text"><input name="data[Parameter]['+newId+'][order]" type="hidden" value="" /></div>	<div class="input text"><input name="data[Parameter]['+newId+'][product_id]" type="hidden" value="'+productId+'" /></div>	<textarea name="data[Parameter]['+newId+'][value]" rows="2" style="width:95%;font-size:11px;" ></textarea>	</td>';
	recalculateTableOrder(table);
}

function deleteTableRow(table,index){
	table.deleteRow(index);
	recalculateTableOrder(table);
}

//Sprawdzenie czy cena od<ceny do w wyszukiwaniu
function advSearch_checkPrices(){
	min = document.getElementById("SearchFrom").value;
	max = document.getElementById("SearchTo").value;
	if (min > max && min.length>0 && max.length>0){
		alert('Cena minimalna powinna być mniejsza od maksymalnej!');
		return false;
	}else{
		return true;
	}
}

function filterProducers(name){
	if(name.length > 0){
		document.filter.submit();
	}else{
		location.href = '/';
	}
};

function Explorer(fieldId){
	this.value = 0;
	this.fieldId = '0';
	this.show = function(fieldId){
		w = window.open('/admin/produkty/explorer/');
		this.fieldId = fieldId;
	}
	this.set = function(id){
		this.value = id;
		v = document.getElementById(this.fieldId);
		if(v.value.length == 0)
			v.value += this.value
		else
			v.value += ',' + this.value
	}
}

function configuratorRecalculate(){
	elements = document.getElementsByTagName('select');
	totalPrice = 0;
	for(i=0;i<elements.length;i++){
		element = elements[i];
		if(element.selectedIndex){
			value = element.options[element.selectedIndex].text;
			totalPrice = totalPrice + value.substring(value.lastIndexOf(';')+1,value.lastIndexOf(' zł')) * 1;
		}
	}
	totalPrice = Math.ceil(totalPrice);
	document.getElementById('configuratorPrice').innerHTML = 'Cena: ' + totalPrice + ' zł';
}

function configuratorMakeString(){
	configuratorRecalculate();
	elements = document.getElementsByTagName('select');
	string = '';
	for(i=1;i<elements.length;i++){
		element = elements[i];
		string = string + element.options[element.selectedIndex].value + ',';
	}
	return string;
}
function configuratorNarrowDown(id){
	
	mainboardList = document.getElementById('mainboard');
	
	// Jeśli id jest puste to czyścimy ograniczenia
	if(id == 0){
		for(i=0;i<mainboardList.options.length;i++){
			mainboardList.options[i].disabled = false;
		}
		return;
	}
	comatibileMainboards = p[id];
	
	for(i=0;i<mainboardList.options.length;i++){
		mainboardList.options[i].disabled = (!arrayFind(comatibileMainboards,mainboardList.options[i].value));
	}
	if(mainboardList.options[mainboardList.selectedIndex].disabled)mainboardList.selectedIndex = null;
}
function configuratorSendMail(){
	location.href = '/konfigurator/mail/' + configuratorMakeString();
}
function configuratorPrint(){
	location.href = '/konfigurator/drukuj/' + configuratorMakeString();
}
function pdfPrint(){
	location.href = '/konfigurator/pdf/' + configuratorMakeString() + '/true/Tm93eSB6ZXN0YXc=/Zestaw.pdf';
}
function configuratorAddToCart(){
	location.href = '/konfigurator/dodaj/' + document.getElementById('cartQuantity').value + '/' + configuratorMakeString();
}
function mailingSendMail(){
	elements = document.getElementsByTagName('select');
	string = '';
	for(i=0;i<elements.length;i++){
		element = elements[i];
		string = string + element.options[element.selectedIndex].value + ',';
	}
	location.href = '/admin/mailing/przygotuj/' + string;
}
