var playme_shop = new Object();


/*********************************************************/
/*Admin functionality
/*********************************************************/
playme_shop.admin=new Object();

playme_shop.admin.add_category=function(form_el)
{

	var name=form_el.category_name.value.toString();
	var that=this;
	js_tools.make_ajax_request({url:"_shop/ajax.php?section=admin&do=add_category&token=" + playme_shop.TOKEN,
		data:
		{
			name:name
		},
		callback:function()
		{
			that.fetch_category_list();
		}
	});

}

playme_shop.admin.remove_category=function(category_id)
{
	var that =this;
	js_tools.make_ajax_request({url:"_shop/ajax.php?section=admin&do=remove_category&token=" + playme_shop.TOKEN,
	data:
	{
		id:category_id
	},
	callback:function()
	{
		that.fetch_category_list();
	}
	});

}
playme_shop.admin.fetch_category_list=function()
{
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=fetch_category_list&token=' + playme_shop.TOKEN,
			callback:function(response)
			{ 
				getElement('category_list_wrap').innerHTML=response;			
			}
		});
} 

playme_shop.admin.add_product=function(form_el)
{
	var that=this;
	var prod_name=form_el.product_name.value.toString();
	
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=add_product&token=' + playme_shop.TOKEN,
				data:
				{
					name:prod_name
				},
				callback:function()
				{
					that.product_list.fetch({});
				}
	});		
}

playme_shop.admin.product_list=new Object();

playme_shop.admin.product_list.order_options=new Object();
playme_shop.admin.product_list.order_options.orderby='id';
playme_shop.admin.product_list.order_options.asc='false';
playme_shop.admin.product_list.order_options.page=1;

playme_shop.admin.product_list.fetch=function(args)
{
	var that=this;
	if(args)
	{
		if(args.orderby)
		{
			this.order_options.orderby=args.orderby;
		}
		if(typeof args.asc != 'undefined')
		{
			if(args.asc) this.order_options.asc='true';
			else this.order_options.asc='false';
		}
	
		if(args.page) 
		{
			this.order_options.page=args.page;
		}
	}
		
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=fetch_product_list&token=' + playme_shop.TOKEN,
				data:
				{
					orderby:that.order_options.orderby,
					asc:that.order_options.asc,
					page:that.order_options.page
				},
				callback:function(response)
				{
					getElement('product_list_wrap').innerHTML=response;
				}});
	
}

playme_shop.admin.update_product_image=function(prod_id)
{
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=get_product_image&product_id=' + prod_id + "&token=" + playme_shop.TOKEN,
				callback:function(response)
				{
					getElement('product_image').innerHTML=response;
				}});
}

playme_shop.admin.product_properties=new Object();

playme_shop.admin.product_properties.properties=new Array();

playme_shop.admin.product_properties.add_property=function(form_el)
{	
	var property=new Object();
	property.name=form_el.product_property.value.toString();
	property.alternatives=new Array();

	this.properties.push(property);
	this.draw_properties();
}

playme_shop.admin.product_properties.add_child=function(form_el,property)
{	
	if(!this.properties[property]) return false;
	
	this.properties[property].alternatives.push(form_el.child_name.value.toString());
	this.draw_properties();
}

playme_shop.admin.product_properties.remove=function(property)
{	
	if(!this.properties[property])
	{
		return false;	
	}
	else
	{
		this.properties.splice(property,1);
	}
	this.draw_properties();
}

playme_shop.admin.product_properties.remove_alternative=function(property,alt)
{	
	if(!this.properties[property])
	{
		return false;	
	}
	else
	{
		if(!this.properties[property].alternatives || !this.properties[property].alternatives[alt])
		{
			return false;
		}
		else
		{
			this.properties[property].alternatives.splice(alt,1);
		}
	}
	this.draw_properties();
}

playme_shop.admin.product_properties.draw_properties=function()
{
	var out="";
	for(var i=0;i<this.properties.length;i++)
	{
		out= out + "<li>" +
		this.properties[i].name + " | <a href='#' onclick='playme_shop.admin.product_properties.remove(" + i + ");return false;'>Radera</a><ul>";
		
		for(var n=0;n<this.properties[i].alternatives.length;n++)
		{
			out = out + "<li>" + this.properties[i].alternatives[n] + 
			" | <a href='#' onclick='playme_shop.admin.product_properties.remove_alternative(" + i + "," + n + ");return false;'>Radera</a>" +
			"</li>";
		}
		out = out + "<li>" +
		"<form action='#' onsubmit='playme_shop.admin.product_properties.add_child(this," + i + ");return false;'>" +
		"<input type='text' name='child_name' value='Alternativ' /><input type='submit' value='Lägg till' />" +
		"</form>" +
		"</ul></li>";
	}
	getElement('product_properties').innerHTML=out;
}

playme_shop.admin.save_product=function(form_element,product_id)
{
	var product_properties=JSON.stringify(playme_shop.admin.product_properties.properties);
	var product_name=form_element.product_name.value.toString();
	var product_category=form_element.product_category.value.toString();
	var price_euro=form_element.price_euro.value.toString();
	var price_cents=form_element.price_cents.value.toString();	
	var product_description=form_element.product_description.value.toString();	
	var new_product=0;
	var frontpage=0;
	var special_deal=0;
	var recommend=0;
	var in_stock=0;
	/*if(form_element.new_product.checked){new_product=1;}*/
	if(form_element.frontpage.checked){frontpage=1;}
	if(form_element.special_deal.checked){special_deal=1;}
	if(form_element.in_stock.checked){in_stock=1;}
	if(form_element.recommend.checked){recommend=1;}
	
	js_tools.make_ajax_request({url:"_shop/ajax.php?section=admin&do=save_product_data&id=" + product_id + "&token=" + playme_shop.TOKEN,
				data:
				{
					product_name:product_name,
					product_category:product_category,
					price_euro:price_euro,
					price_cents:price_cents,
					product_description:product_description,
					product_properties:product_properties,
					frontpage:frontpage,
					new_product:0,
					special_deal:special_deal,
					recommend:recommend,
					in_stock:in_stock
				}});
}

playme_shop.admin.save_order=function(form_el,order_id)
{

	var status=form_el.order_status.value.toString();
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=save_order&order_id=' + order_id + '&status=' + status + "&token=" + playme_shop.TOKEN});
}

playme_shop.admin.order_list=new Object();

playme_shop.admin.order_list.order_options=new Object();
playme_shop.admin.order_list.order_options.orderby='id';
playme_shop.admin.order_list.order_options.asc='false';
playme_shop.admin.order_list.order_options.page=1;

playme_shop.admin.order_list.fetch=function(args)
{
	var that=this;
	if(args)
	{
		if(args.orderby)
		{
			this.order_options.orderby=args.orderby;
		}
		if(typeof args.asc != 'undefined')
		{
			if(args.asc) this.order_options.asc='true';
			else this.order_options.asc='false';
		}
	
		if(args.page)
		{
			this.order_options.page=args.page;
		}
	}
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=fetch_order_list&token=' + playme_shop.TOKEN,
				data:
				{
					orderby:that.order_options.orderby,
					asc:that.order_options.asc,
					page:that.order_options.page
				},
				callback:function(response)
				{
					getElement('order_list_wrap').innerHTML=response;
				}});
	
}

playme_shop.admin.remove_product=function(product_id)
{
	var that=this;
	if(!confirm("Vill du verkligen ta bort produkten?")) return false;
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=remove_product&product_id=' + product_id + "&token=" + playme_shop.TOKEN,
		callback:function()
		{
			that.product_list.fetch();
		}
	});
}

playme_shop.admin.remove_order=function(order_id)
{
	var that=this;
	if(!confirm("Vill du verkligen ta bort beställningen?")) return false;
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=remove_order&order_id=' + order_id + "&token=" + playme_shop.TOKEN,
		callback:function()
		{
			that.order_list.fetch();
		}
	});
}


playme_shop.admin.save_agreement=function(form_el)
{
	var that=this;
	var text=form_el.agreement_text.value.toString();

	js_tools.make_ajax_request({url:'_shop/ajax.php?section=admin&do=save_agreement&token=' + playme_shop.TOKEN,
					data:{text:text},post:true});
}
/************************************************************************/
/*Shop cart
/*************************************************************************/

playme_shop.cart=new Object();

playme_shop.cart.add_product=function(form_el)
{
	var that=this;
	var product_id=form_el.product_id.value.toString();
	var selects=form_el.getElementsByTagName('select');
	var properties=Array();
	var errors="";
	for(var i=0;i<selects.length;i++)
	{
		var property=new Object();		
		property.name=selects[i].name;
		if(selects[i].value.toString()=='false')
		{
			errors += property.name +", ";
		}
		else
		{
			property.alternative=selects[i].value.toString();
		}
		properties.push(property);
	}
	
	if(errors.length!=0)
	{
		errors=errors.replace(/, $/,'');
		alert("Du måste ange följande alternativ: " + errors);
		return false;
	}
	
	var properties=JSON.stringify(properties);
	
	
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=cart&do=add&product_id=' + product_id + "&token=" + playme_shop.TOKEN,
		data:
		{
			properties:properties
		},
		callback:function(response)
		{
			that.update_mini_cart();
		}});
			
}
playme_shop.cart.remove_product=function(cart_id)
{
	var that=this;
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=cart&do=remove&cart_id=' + cart_id + "&token=" + playme_shop.TOKEN,
		callback:function(response)
		{
			that.update_mini_cart();
		}});
}

playme_shop.cart.remove_product_family=function(group_id)
{
	var that=this;
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=cart&do=remove_product_family&group_id=' + group_id + "&token=" + playme_shop.TOKEN,
		callback:function(response)
		{
			that.update_mini_cart();
			that.update_page();
		}});
}

playme_shop.cart.change_product_count=function(group_id,select_el)
{
	var count=select_el.value.toString();
	var that=this;
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=cart&do=change_product_count&group_id=' + group_id + '&count=' + count + "&token=" + playme_shop.TOKEN,
		callback:function(response)
		{
			that.update_mini_cart();
			that.update_page();
		}});
}

playme_shop.cart.update_mini_cart=function()
{	
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=cart&do=get_small_cart&token=' + playme_shop.TOKEN,
		callback:function(response)
		{
			getElement('cart_product_count').innerHTML=response;

		}});
}

playme_shop.cart.update_page=function()
{	
	js_tools.make_ajax_request({url:'_shop/ajax.php?section=cart&do=get_cart_page&token=' + playme_shop.TOKEN,
		callback:function(response)
		{
			getElement('right_container').innerHTML=response;

		}});
}


playme_shop.validate_details_form = function(form_element)
{
		var firstname=form_element.fname.value.replace(/^\s*/g,'').replace(/\s*$/g,'');
		var lastname=form_element.lname.value.replace(/^\s*/g,'').replace(/\s*$/g,'');;
		var address=form_element.address.value.replace(/^\s*/g,'').replace(/\s*$/g,'');;
		var postaddress=form_element.postaddress.value.replace(/^\s*/g,'').replace(/\s*$/g,'');;
		var country=form_element.country.value.replace(/^\s*/g,'').replace(/\s*$/g,'');;
		var email=form_element.email.value.replace(/^\s*/g,'').replace(/\s*$/g,'');;
		var tel=form_element.tel.value.replace(/^\s*/g,'').replace(/\s*$/g,'');;
		if(firstname.length==0 || lastname.length==0 || address.length==0 || postaddress.length==0 ||  country.length==0 || email.length==0)
		{
			alert('Följande fält måste vara fyllda: Förnamn,Efternamn,Adress,Postadress,Ort,Land och E-post');
			return false;
		}
		if(!email.match(/^[^\s]+@[^\s]+.[a-zA-Z0-9.]+$/i))
		{
			alert('Felaktig e-mail-adress!');
			return false;
		}//end of if
		return true;
}

playme_shop.keep_alive=function()
{
	setInterval(function(){js_tools.make_ajax_request({url:'_shop/ajax.php?section=keep_alive&token=' + playme_shop.TOKEN});},120000);
}

