var opacity_level = 0;

function show_basket()
{	
   AJAX('core/AJAX/php/core_interface.php','show_basket_small=1',show_basket_callback);
}
function show_basket_callback(r)
{
	var json = r.responseText;		
	json = eval( "(" + json + ")" );
	
	var basket_mini = document.getElementById("TOP_BASKET_DIV");
	
	if (json['how_many'] == undefined)
    zm1 = 0;  
  else
    zm1 = json['how_many'];
  
  if (json['sum'] == undefined)
    zm2 = 0.00;
  else
    zm2 = json['sum'];
	
	basket_mini.innerHTML = "<strong>Twój Koszyk</strong><br><br>Liczba produktów: <font color='#FFFFFF'>"+zm1+"</font><br>Wartość: <font color='#FFFFFF'>"+zm2 +" zł</font><br><br><a href='index.php?list_basket=1&view=basket'>Pokaż mój koszyk</a>";
}

function change_price(product_id,choice_id)
{	
   AJAX('core/AJAX/php/core_interface.php','product_ID='+product_id+"&choice_id="+choice_id,change_price_callback);
}
function change_price_callback(r)
{
  var json = r.responseText;	

  var tresc = document.getElementById("product_price");
  tresc.innerHTML = json;
}


function add_few_to_basket(product_id,quantity)
{	
   AJAX('core/AJAX/php/core_interface.php','add_few_to_basket_id='+product_id+"&quantity="+quantity,add_few_to_basket_callback);
}

function add_few_to_basket_callback(r)
{
  var json = r.responseText;

  if (json == 1)
  {
    show_basket();
    show_info('Dodano produkt/y do koszyka');
  }
  else
  {
    show_basket();
    show_info('Na stanie nie ma tak wielu produktów'); 
  }
  
  var quantity = document.getElementById("add_basket_textarea");
  quantity.value = 1;
}


function add_to_basket(product_id)
{	
   AJAX('core/AJAX/php/core_interface.php','add_basket_id='+product_id,add_to_basket_callback);
}
function add_to_basket_callback(r)
{
  var json = r.responseText;
  
  
  if (json == 1)
  {
    show_basket();
    show_info('Dodano produkt do koszyka');
  }
  else
  {
    show_basket();
    show_info('Na stanie nie ma już produktów'); 
  }
}


function delete_product_from_basket(id)
{
  show_info('Usunięto produkt/y do koszyka');
  AJAX('core/AJAX/php/core_interface.php','product_ID='+id,delete_product_from_basket_callback);
  
}
function delete_product_from_basket_callback(r)
{
	show_basket();
	show_basket2();
}


function modify_basket(quantity,id)
{
  AJAX('core/AJAX/php/core_interface.php','key='+id+"&modify_basket=1&QUANTITY="+quantity,modify_basket_callback);
}
function modify_basket_callback(r)
{
  var json = r.responseText;
  
  
  if (json == 1)
  {
    show_info('Zmieniono liczbę produktów w koszyku');
    show_basket();
    show_basket2();
  }
  else
  {
    show_info('Na stanie nie ma już produktów');
    show_basket();
    show_basket2(); 
  }
  
  
}




function show_info(komunikat){

	
  info = document.getElementById('info_box');
  
	if( info == undefined || info == null){
		scr_width = getPageSize()[0];
		scr_height = getPageSize()[1];
		pos_x = (scr_width - 300)/2;
		var info = mk_box('info_box','','');
		info.style.position = "fixed";
		info.style.bottom = parseInt(scr_height/2)  + "px";
		
    info.style.left = pos_x + "px";
		info.style.width = "350px";
		info.style.height = "60px";
		document.body.appendChild(info);
		}
	
	if(opacity_level == 10 ){
		if(document.all)
			info.style.filter = "alpha(opacity = "+opacity_level*10+")";
		else
			info.style.opacity = opacity_level/10;
	}
	
	info.innerHTML = komunikat;
	
	if(opacity_level == 10);
		setTimeout("hide_info()",2000);	
}

function hide_info(){
	
	info = document.getElementById("info_box");
	
	opacity_level--;
	
	if(document.all){
		info.style.filter = "alpha(opacity = "+opacity_level*5+")";
	}
	else
		info.style.opacity = opacity_level/5;
		
	
	if(opacity_level <= 0){
		opacity_level =10;
		return 0;
	}
	else
		setTimeout("hide_info()",50);

}

function getPageSize()
{
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement.clientWidth && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;//document.documentElement.clientWidth;
        windowHeight =document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.offsetWidth;
        windowHeight = document.body.offsetHeight;//window.screen.height
    }   
    arrayPageSize = new Array(windowWidth,windowHeight)
    return arrayPageSize;
}

function mk_box(_id,_class,_name){
	
	var box = document.createElement("div");
	box.setAttribute("id",_id);
	box.setAttribute("name",_name);
	box.setAttribute("class",_class);
		
	return box;
}

function display_promotions_main()
{
	AJAX('core/AJAX/php/core_interface.php','display_main=1',display_promotions_main_callback);
}

function display_promotions_main_callback(r)
{
var json = r.responseText;		
	json = eval( "(" + json + ")" );

	var promotions_main_page2 = document.getElementById("TOP_SUB_C_DIV2");
	
	if (promotions_main_page2 == null)
	{
    return 0;
  }
	
		while(promotions_main_page2.firstChild)
			promotions_main_page2.removeChild(promotions_main_page2.firstChild);
	
	var promotions_main_page = document.createElement("div");
	promotions_main_page.setAttribute("id","TOP_SUB_C_DIV3");
	promotions_main_page2.appendChild(promotions_main_page);
	
	if ((json['ile'] == 0) || (json['ile'] == undefined))
	{
    var promotions_main_page5 = document.createElement("div");
	  promotions_main_page5.setAttribute("id","TOP_SUB_C_DIV4");
    promotions_main_page5.innerHTML = 'Aktualnie brak promocji..';
    promotions_main_page.appendChild(promotions_main_page5);
    return 0;
  
  }
		
	var table2 = document.createElement("table");
	table2.setAttribute("id","promotions_table");
	var table = document.createElement("tbody");
				
		promotions_main_page.appendChild(table2);
		table2.appendChild(table);
		
		var name_tr = document.createElement("tr");
		table.appendChild(name_tr);
		var name_td = document.createElement("td");
		name_td.setAttribute("id","promotions_td_link");
		name_td.setAttribute("colspan",2);
		
		var newlink = document.createElement('a');
		newlink.setAttribute("id","promotions_link");
		newlink.innerHTML = json[0][1];
	  newlink.setAttribute('href', 'index.php?product_id='+json[0][0]);
		name_tr.appendChild(name_td);
		name_td.appendChild(newlink);

		var name_td2 = document.createElement("td");
		name_td2.setAttribute("rowspan",3);
		var promotions_photo = document.createElement("img");
		promotions_photo.setAttribute("id","promotions_photo");
		promotions_photo.src = "files/100/"+json[0][3];
		name_tr.appendChild(name_td2);
		name_td2.appendChild(promotions_photo);
	
		var name_tr3 = document.createElement("tr");
		table.appendChild(name_tr3);
		var name_td4 = document.createElement("td");
		name_tr3.appendChild(name_td4);
		name_td4.setAttribute("colspan",2);
		var promotions_price = document.createElement("div");
		promotions_price.setAttribute("id","div_promotions_price");
		promotions_price.innerHTML = json[0][2]+' zł';
		name_td4.appendChild(promotions_price);
		
		var name_tr4 = document.createElement("tr");
		table.appendChild(name_tr4);
		
		var name_td6 = document.createElement("td");
		name_tr4.appendChild(name_td6);
		name_td6.setAttribute("width",35);
		
		var name_td5 = document.createElement("td");
		name_tr4.appendChild(name_td5);
		var promotions_next = document.createElement("div");
		promotions_next.setAttribute("id","div_photo_next");
		promotions_next.onclick = function() {next_photo(json[0][0]);};
		name_td5.appendChild(promotions_next);
}

function next_photo(id)
{
	show_promotions_alpha();
	AJAX('core/AJAX/php/core_interface.php','next_photo=1&id='+id,next_photo_callback);
}

function previous_photo(id)
{
	show_promotions_alpha();
	AJAX('core/AJAX/php/core_interface.php','previous_photo=1&id='+id,previous_photo_callback);
}

function next_photo_callback(r)
{
	var json = r.responseText;		
	json = eval( "(" + json + ")" );

	var promotions_main_page2 = document.getElementById("TOP_SUB_C_DIV2");
	
		while(promotions_main_page2.firstChild)
			promotions_main_page2.removeChild(promotions_main_page2.firstChild);
	
	var promotions_main_page = document.createElement("div");
	promotions_main_page.setAttribute("id","TOP_SUB_C_DIV3");
	promotions_main_page2.appendChild(promotions_main_page);
	
	var table2 = document.createElement("table");
	var table = document.createElement("tbody");
				
		promotions_main_page.appendChild(table2);
		table2.appendChild(table);
		
		var name_tr = document.createElement("tr");
		table.appendChild(name_tr);
		var name_td = document.createElement("td");
		name_td.setAttribute("id","promotions_td_link");
		name_td.setAttribute("colspan",2);
		
		var newlink = document.createElement('a');
		newlink.setAttribute("id","promotions_link");
		newlink.innerHTML = json[0][1];
	    newlink.setAttribute('href', 'index.php?product_id='+json[0][0]);
		name_tr.appendChild(name_td);
		name_td.appendChild(newlink);

		var name_td2 = document.createElement("td");
		name_td2.setAttribute("rowspan",3);
		var promotions_photo = document.createElement("img");
		promotions_photo.setAttribute("id","promotions_photo");
		promotions_photo.src = "files/100/"+json[0][3];
		name_tr.appendChild(name_td2);
		name_td2.appendChild(promotions_photo);
	
		var name_tr3 = document.createElement("tr");
		table.appendChild(name_tr3);
		var name_td4 = document.createElement("td");
		name_tr3.appendChild(name_td4);
		name_td4.setAttribute("colspan",2);
		var promotions_price = document.createElement("div");
		promotions_price.setAttribute("id","div_promotions_price");
		promotions_price.innerHTML = json[0][2]+' zł';
		name_td4.appendChild(promotions_price);
		
		var name_tr4 = document.createElement("tr");
		table.appendChild(name_tr4);
		
		var name_td6 = document.createElement("td");
		name_tr4.appendChild(name_td6);
		name_td6.setAttribute("align","right");
		var promotions_previous = document.createElement("div");
		promotions_previous.setAttribute("id","div_photo_previous");
		promotions_previous.onclick = function() {previous_photo(json[0][0]);};	
		name_td6.appendChild(promotions_previous);
		
		var name_td5 = document.createElement("td");
		name_tr4.appendChild(name_td5);
		name_td5.setAttribute("align","left");
		var promotions_next = document.createElement("div");
		promotions_next.setAttribute("id","div_photo_next");
		promotions_next.onclick = function() {next_photo(json[0][0]);};
		name_td5.appendChild(promotions_next);
		
		if (json['koniec']==1)
		{
			var promotions_next = document.getElementById("div_photo_next");
			name_td5.removeChild(promotions_next);
		}
}

function previous_photo_callback(r)
{
var json = r.responseText;		
	json = eval( "(" + json + ")" );

	var promotions_main_page2 = document.getElementById("TOP_SUB_C_DIV2");
	
		while(promotions_main_page2.firstChild)
			promotions_main_page2.removeChild(promotions_main_page2.firstChild);
	
	var promotions_main_page = document.createElement("div");
	promotions_main_page.setAttribute("id","TOP_SUB_C_DIV3");
	promotions_main_page2.appendChild(promotions_main_page);
	
	var table2 = document.createElement("table");
	var table = document.createElement("tbody");
				
		promotions_main_page.appendChild(table2);
		table2.appendChild(table);
		
		var name_tr = document.createElement("tr");
		table.appendChild(name_tr);
		var name_td = document.createElement("td");
		name_td.setAttribute("id","promotions_td_link");
		name_td.setAttribute("colspan",2);
		
		var newlink = document.createElement('a');
		newlink.setAttribute("id","promotions_link");
		newlink.innerHTML = json[0][1];
	    newlink.setAttribute('href', 'index.php?product_id='+json[0][0]);
		name_tr.appendChild(name_td);
		name_td.appendChild(newlink);

		var name_td2 = document.createElement("td");
		name_td2.setAttribute("rowspan",3);
		var promotions_photo = document.createElement("img");
		promotions_photo.setAttribute("id","promotions_photo");
		promotions_photo.src = "files/100/"+json[0][3];
		name_tr.appendChild(name_td2);
		name_td2.appendChild(promotions_photo);
	
		var name_tr3 = document.createElement("tr");
		table.appendChild(name_tr3);
		var name_td4 = document.createElement("td");
		name_tr3.appendChild(name_td4);
		name_td4.setAttribute("colspan",2);
		var promotions_price = document.createElement("div");
		promotions_price.setAttribute("id","div_promotions_price");
		promotions_price.innerHTML = json[0][2]+' zł';
		name_td4.appendChild(promotions_price);
		
		var name_tr4 = document.createElement("tr");
		table.appendChild(name_tr4);
		
		var name_td6 = document.createElement("td");
		name_tr4.appendChild(name_td6);
		name_td6.setAttribute("align","right");
		var promotions_previous = document.createElement("div");
		promotions_previous.setAttribute("id","div_photo_previous");
		promotions_previous.onclick = function() {previous_photo(json[0][0]);};	
		name_td6.appendChild(promotions_previous);
		
		var name_td5 = document.createElement("td");
		name_tr4.appendChild(name_td5);
		name_td5.setAttribute("align","left");
		var promotions_next = document.createElement("div");
		promotions_next.setAttribute("id","div_photo_next");
		promotions_next.onclick = function() {next_photo(json[0][0]);};
		name_td5.appendChild(promotions_next);
		
		if (json['koniec']==1)
		{
			var promotions_next = document.getElementById("div_photo_previous");
			name_td6.removeChild(promotions_next);
		}
}

function show_promotions_alpha()
{
	info = document.getElementById('TOP_SUB_C_DIV3');

	opacity_level++;
	
	if(document.all){
		info.style.filter = "alpha(opacity = "+opacity_level*10+")";
	}
	else
		info.style.opacity = opacity_level/10;
		
	
	if(opacity_level >= 10){
		opacity_level = 0;
		return 0;
	}
	else
		setTimeout("show_promotions_alpha()",100);
}	


function show_basket2()
{	
   AJAX('core/AJAX/php/core_interface.php','show_basket_big=1',show_basket2_callback);
}

function show_basket2_callback(r){

	var json = r.responseText;		
	json = eval( "(" + json + ")" );
	
	var nav_menu = document.getElementById('NAV_DIV');
	 nav_menu.innerHTML = 'Jesteś tutaj:  <a href="index.php">» Marino-JG.pl</a> » <a href="javascript:show_basket2()">Zawartość koszyka</div>';
	
	 var content = document.getElementById('basket_content');
	 while(content.firstChild)
			content.removeChild(content.firstChild);
	
	var top_header = document.createElement("div");
	top_header.setAttribute("id","subcat_top_title");
	top_header.innerHTML="Zawartość koszyka";
	content.appendChild(top_header);
	
	
	
	if((json['how_many'] == 0)||(json['how_many'] == undefined)){	
		content.innerHTML = '<div id="basket_empty">Koszyk jest pusty</div>';
		return 0;
	}
	
	var bassket = document.createElement("table");
	bassket.setAttribute("cellspacing","0px");
	bassket.setAttribute("id","basket");
	
	content.appendChild(bassket);
	
  var titles_tb = document.createElement("tbody");
	var titles = document.createElement("tr");
	titles.setAttribute("class","titles");
	
	bassket.appendChild(titles_tb);
	titles_tb.appendChild(titles);
	
	var product_name_title = document.createElement("td");
	product_name_title.setAttribute("class","product_name_title");
	product_name_title.innerHTML = 'Nazwa';
	
	var product_price_title = document.createElement("td");
	product_price_title.setAttribute("class","product_price_title");
	product_price_title.innerHTML = "Cena";
	
	var product_qty_title = document.createElement("td");
	product_qty_title.setAttribute("class","product_qty_title");
	product_qty_title.innerHTML = "Ilość";
	
	var product_price_sum_title = document.createElement("td");
	product_price_sum_title.setAttribute("class","product_price_sum_title");
	product_price_sum_title.innerHTML = "Wartość";
	
	var product_operation_title = document.createElement("td");
	product_operation_title.setAttribute("class","product_operation_title");
	product_operation_title.innerHTML = "Operacje";
		
	titles.appendChild(product_name_title);
	titles.appendChild(product_price_title);
	titles.appendChild(product_qty_title);
	titles.appendChild(product_price_sum_title);
	titles.appendChild(product_operation_title);
	

	
	
	//TUTAJ PRODUKTY WKLEI
	for(i = 0; i < json['how_many']; i++){
	  var product_basket_tb = document.createElement("tbody");
		bassket.appendChild(product_basket_tb);
	
	  var product_basket = document.createElement("tr");
		product_basket_tb.appendChild(product_basket);
		
		//if(i != json['how_many']-2)
		
    product_basket.setAttribute("class","product_basket");
	
  //	else
		//	setClass(product_basket,"last_product_basket");

		var product_name = document.createElement("td");
		product_name.setAttribute("class","product_name_basket");
		product_name.innerHTML = json[i][0][1];
		
		product_basket.appendChild(product_name);
	
		var price = document.createElement("td");
		price.setAttribute("class","product_price_basket");
		price.innerHTML = json[i][0][2] ;
	
		product_basket.appendChild(price);
	
		var qty = document.createElement("td");
		qty.setAttribute("class","qunatity_div_basket");

		
		product_basket.appendChild(qty);
	
		var input_field = document.createElement("input");
		input_field.type = "text";
	//	input.setAttribute("name","quantity_input_basket");
		id = json[i][0][0];
    input_field.setAttribute("id", id);
  	input_field.setAttribute("class","quantity_input_basket");
		input_field.value = json[i][1];
		input_field.size = "2";
	  input_field.onchange =  function(){modify_basket(this.value,this.id)};
	
		qty.appendChild(input_field);
	
		var price_sum = document.createElement("td");
    price_sum.setAttribute("class","price_sum_basket"); 
		price_sum.innerHTML = json[i][2];
	
		product_basket.appendChild(price_sum);
	
		var operations = document.createElement("td");
		operations.setAttribute("id", json[i][0][0]);
	  operations.setAttribute("class","del_basket");
		operations.innerHTML = "Usuń";
		operations.onclick = function(){delete_product_from_basket(this.id)};
		
		product_basket.appendChild(operations);
		
		if(i == json['how_many']-2){
		
			var last_product_basket_tb = document.createElement("tbody");
		  bassket.appendChild(last_product_basket_tb);
      
            var last_tr = document.createElement("tr");
			last_tr.setAttribute("id","last_tr");
			last_product_basket_tb.appendChild(last_tr);
			
			var last_td = document.createElement("td");
			last_td.setAttribute("id","last_td");
			last_td.setAttribute("colspan","5");
			last_tr.appendChild(last_td);
	
			
		}

	}
	

	

	
	var sum_of_all = document.createElement("div");
	sum_of_all.setAttribute("id","sum_of_all");
	sum_of_all.innerHTML = "Wartość zamówienia: "+json['sum'] + " zł";
	
	content.appendChild(sum_of_all);
	
	var make_order_ = document.createElement("div");
	//make_order_.setAttribute("id","make_order");
	make_order_.innerHTML = "<a href='index.php?order=1'><div id='make_order'>Złóż zamówienie</div></a>";
	
	
	content.appendChild(make_order_);
	
}

function subscribe(email)
{	
	if (validate_email(email))
		AJAX('core/AJAX/php/core_interface.php','subscribe=1&email='+email,subscribe_callback);
	else
	{
		var mail = document.getElementById('subscribe_mail2');
		mail.value = '';
		show_info('Błędnie wpisany adres e-mail');
	}
}
function subscribe_callback(r)
{
	var json = r.responseText;	
//	alert(json);
	
//	if (json=='1')
//	{
		show_info('Pomyślnie dodano adres e-mail do bazy');
/*
	}
	else
	{
		show_info('Adres e-mail nie został dodany do bazy');
	}
	*/
	var mail = document.getElementById('subscribe_mail2');
	mail.value = '';
}

function dissubscribe(email)
{	
	if (validate_email(email))
		AJAX('core/AJAX/php/core_interface.php','dissubscribe=1&email='+email,dissubscribe_callback);
	else
	{
		var mail = document.getElementById('subscribe_mail2');
		mail.value = '';
		show_info('Błędnie wpisany adres e-mail');
	}
}
function dissubscribe_callback(r)
{
	var json = r.responseText;	
//	alert(json);
	
//	if (json=='1')
//	{
		show_info('Pomyślnie usunięto adres e-mail');
/*
	}
	else
	{
		show_info('Adres e-mail nie został dodany do bazy');
	}
	*/
	var mail = document.getElementById('subscribe_mail2');
	mail.value = '';
}
function validate_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   }
   else
		return true;
}