var opacity_level = 0;

function show_basket()
{	
   show_loader2();
   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 = "<center><strong>Koszyk</strong></center><br>Liczba prod.: "+zm1+" <br>Wartość: "+zm2 +" zł";
}

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,size)
{	
   AJAX('core/AJAX/php/core_interface.php','add_few_to_basket_id='+product_id+"&quantity="+quantity+"&size="+size,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)
{
  show_basket();
  show_info('Dodano produkt do koszyka');
}


function delete_product_from_basket(id,size)
{
  show_loader();
  show_info('Usunięto produkt/y do koszyka');
  AJAX('core/AJAX/php/core_interface.php','delete=1&product_ID='+id+'&size='+size,delete_product_from_basket_callback);
  
}
function delete_product_from_basket_callback(r)
{
	show_basket();
	show_basket2();
}


function modify_basket(quantity,id,size)
{
  show_loader();
  show_info('Zmieniono liczbę produktów w koszyku');
  AJAX('core/AJAX/php/core_interface.php','key='+id+"&modify_basket=1&QUANTITY="+quantity+"&size="+size,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 = "40px";
		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()",1200);	
}

function hide_info(){
	
	info = document.getElementById("info_box");
	
	opacity_level--;
	
	if(document.all){
		info.style.filter = "alpha(opacity = "+opacity_level*10+")";
	}
	else
		info.style.opacity = opacity_level/10;
		
	
	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);
	
	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("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("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 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);
		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("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 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);
		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()
{	
   show_loader();
   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 + ")" );
	
  //alert(json['how_many']);
	
	 var nav_menu = document.getElementById('NAV_DIV');
	 nav_menu.innerHTML = 'Jesteś tutaj: » <a href="index.php">Strona główna</a> » <a href="javascript:show_basket2()">Zawartość koszyka</div>';

	
	
  var content = document.getElementById('basket_content');
	while(content.firstChild)
			content.removeChild(content.firstChild);
	
	if(json['how_many'] == 0){	
		content.innerHTML = "<div id='basket_empty'>Koszyk jest pusty</div>";
		return 0;
	}
	else
	{

	}


	var content_hdr = document.createElement("div");
	content_hdr.setAttribute("id","subcat_top_title_long");
	content_hdr.innerHTML = 'Zawartość koszyka';
	content.appendChild(content_hdr);	
	
	var content_border = document.createElement("div");
	content_border.setAttribute("id","TOP_SUB_C_DIV3");
	content.appendChild(content_border);
	
	var bassket = document.createElement("table");
	bassket.setAttribute("cellspacing","0px");
	bassket.setAttribute("id","basket");
	
	content_border.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);


	
	var i = 0;
	
	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.length - 2)
			product_basket.setAttribute("class","product_basket");
		else
			product_basket.setAttribute("class","last_product_basket");
		
		var size = json[i][3];
		
		var product_name = document.createElement("td");
		product_name.setAttribute("class","product_name_basket");
		product_name.innerHTML = json[i][0][2] + " rozmiar: " +size;
		
    //alert(json[i][0][3]);
		
		product_basket.appendChild(product_name);
	
		var price = document.createElement("td");
		price.setAttribute("class","product_price_basket");
		price.innerHTML = json[i][0][4] ;
	
		product_basket.appendChild(price);
	
		var qty = document.createElement("td");
		qty.setAttribute("class","qunatity_div_basket");

		
		product_basket.appendChild(qty);
	
	 var input_id = json[i][0][0]+"|"+size;
	
		var input = document.createElement("input");
		input.type = "text";
		input.setAttribute("name","quantity_input_basket");
		input.setAttribute("id",input_id);
		input.setAttribute("class","quantity_input_basket");
		input.value = json[i][1];
		input.size = "3";
		input.onblur = function() { modify_basket(this.value,this.id); };
	
		qty.appendChild(input);
	
		var price_sum = document.createElement("td"); 
		price_sum.setAttribute("class","price_sum_basket");
		price_sum.innerHTML = json[i][0][5];
	
		product_basket.appendChild(price_sum);
	
		var operations = document.createElement("td");
		operations.setAttribute("id", input_id);
		operations.setAttribute("class","del_basket");
		operations.innerHTML = "Usuń";
		operations.onclick = function(){delete_product_from_basket(this.id)};
		product_basket.appendChild(operations);
		 
		if(i == json.length - 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_tr.appendChild(last_td);	
		}
		
	}

	var sum_of_all = document.createElement("div");
	sum_of_all.setAttribute("id","sum_of_all");
	sum_of_all.innerHTML = "Suma: "+json[4]+" zł";
	content_border.appendChild(sum_of_all);
	
	var make_order_ = document.createElement("a");
	make_order_.setAttribute("id","make_order");
	make_order_.innerHTML = "<center><div id='make_order'></div></center>";
    make_order_.setAttribute('href', 'index.php?order=1');


	content_border.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 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;
}

function show_loader(){

	var content= document.getElementById('basket_content');
	while(content.firstChild)
		content.removeChild(content.firstChild);
		
	var loader = document.createElement("img");
	loader.src = "loader.gif";
	loader.style.marginTop = "15px";
	content.appendChild(loader);
}

function show_loader2(){

	var content= document.getElementById('TOP_BASKET_DIV');
	while(content.firstChild)
		content.removeChild(content.firstChild);
		
	var loader = document.createElement("img");
	loader.src = "loader.gif";
	loader.style.marginTop = "15px";
	content.appendChild(loader);
}

