function show_loader()
{
	$(document).ready(function()
	{
		$.get("../loader.php",
		function(data) { $("#loader").html(data); });
		$("#loader")
		.ajaxStart(function() { $(this).show(); })
		.ajaxComplete(function() { $(this).hide(); });
	});
}

function showcart(ccat, srt, tp, idd)
{
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					$.get("../shop/catalog.php",  { 'cat': ccat, 'sort_by': srt, 'typ': tp, 'id': idd },
					function(data) { $("#market_disp").html(data); });
				}
			}
		);
	});
}

function showorder(ccat, srt)
{
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					$.get("../shop/orderform.php",
					function(data) { $("#market_disp").html(data); });
				}
			}
		);
	});
}

function show_in(surl, clk)
{
	$(document).ready(function()
	{
		if (clk == 1)
		{
			$(document).scrollTo('#market_disp', 800,
				{ onAfter:function()
					{
						$.get(surl, { dummy: 0 },
						function(data) { $("#market_disp").html(data); });
					}
				}
			);
		}
		else
		{
			$.get(surl, { dummy: 0 },
			function(data) { $("#market_disp").html(data); });
		};
	});
}

function manage_cart()
{
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					$.get("../shop/cart_edit.php",  
					function(data) { $("#market_disp").html(data); });
				}
			}
		);
	});
}

function edit_cart(cmdd, ccat, idd, qnty)
{
	$(document).ready(function()
	{
		var myqnty = $("#qqnty"+qnty).val();
		$.get("../shop/cart.php", { action: cmdd, cat: ccat, id: idd, items: myqnty },  
		function(data) { $("#cart").html(data); });
	});
}

function mymail(cor_val)
{
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					var mysender = $("#ssender").val();
					var myemail = $("#emailer").val();
					var mysubject = $("#ssubject").val();
					var mymessage = $("#smessage").val();
					var myvalidator = $("#vvalidator").val();
					var mval = cor_val;
					$.post("email.php",
					{
						sender: mysender, email: myemail,
						subject: mysubject, message: mymessage ,
						validator: myvalidator, correct_val: mval
					}, 
					function(data) { $("#market_disp").html(data); });
				}
			}
		);
	});
}

function login(act)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					$.get("admin.php", { action: act }, 
					function(data) { $("#disp").html(data); });
				}
			}
		);
	});
}

function do_search()
{
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					var myq = $("#q_text").val();
					$.post("search.php", { q: myq }, 
					function(data) { $("#market_disp").html(data); });
				}
			}
		);
	});
}

function admin_db(comm)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					$.get("admin.php", { action: "admin", cmd: comm }, 
					function(data) { $("#disp").html(data); });
				}
			}
		);
	});
}

function manage_rest(ord, comm)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					$.get("admin.php", { action: ord, cmd: comm }, 
					function(data) { $("#disp").html(data);
					$(".a_txt").wysiwyg({
							css: { fontFamily: 'Tahoma', fontSize : '8pt' }, 
							controls:
							{
								separator04 		: { visible : false },
								insertHorizontalRule: { visible : false },
								justifyLeft   		: { visible : false },
								justifyCenter 		: { visible : false },
								justifyRight  		: { visible : false },
								justifyFull   		: { visible : false },
								separator01   		: { visible : false },
								indent				: { visible : false },
								outdent				: { visible : false },
								undo				: { visible : false },
								redo				: { visible : false },
								separator02 		: { visible : false },
								separator05 		: { visible : false },
								strikeThrough 		: { visible : false },
								createLink			: { visible : false },
								insertImage			: { visible : false }
							}
						});
					});
				}
			}
		);
	});
}

function manage_items(cate)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					$.post("admin.php", { action: "manage", cat: cate },
						function(data){ $("#disp").html(data),
						$(".a_txt").wysiwyg({
							css: { fontFamily: 'Tahoma', fontSize : '8pt' }, 
							controls:
							{
								separator04 		: { visible : false },
								insertHorizontalRule: { visible : false },
								justifyLeft   		: { visible : false },
								justifyCenter 		: { visible : false },
								justifyRight  		: { visible : false },
								justifyFull   		: { visible : false },
								separator01   		: { visible : false },
								indent				: { visible : false },
								outdent				: { visible : false },
								undo				: { visible : false },
								redo				: { visible : false },
								separator02 		: { visible : false },
								separator05 		: { visible : false },
								strikeThrough 		: { visible : false },
								createLink			: { visible : false },
								insertImage			: { visible : false }
							}
						});
					});
				}
			}
		);
	});
}

function save_item(cate)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					var mymanu = $("#manu").val();
					var mymate = $("#mate").val();
					var mysiz = $("#siz").val();
					var mywei = $("#wei").val();
					var mypric = $("#pric").val();
					var mydesc = $("#desc").val();
					var mycomm = $("#comm").val();
					var myquant = $("#quant").val();
					var mycode = $("#code").val();
					var myavail = $("#avail").is(":checked");
					$.post("admin.php",
					{
						action: "manage", cmd: "add", cat: cate,
						manufact: mymanu, material: mymate, size: mysiz,
						weight: mywei, price: mypric, descr: mydesc,
						comms: mycomm, quant: myquant, code: mycode,
						avail: myavail
					}, 
					function(data) { $("#disp").html(data),
					$(".a_txt").wysiwyg ( {
							css: { fontFamily: 'Tahoma', fontSize : '8pt' }, 
							controls:
							{
								separator04 		: { visible : false },
								insertHorizontalRule: { visible : false },
								justifyLeft   		: { visible : false },
								justifyCenter 		: { visible : false },
								justifyRight  		: { visible : false },
								justifyFull   		: { visible : false },
								separator01   		: { visible : false },
								indent				: { visible : false },
								outdent				: { visible : false },
								undo				: { visible : false },
								redo				: { visible : false },
								separator02 		: { visible : false },
								separator05 		: { visible : false },
								strikeThrough 		: { visible : false },
								createLink			: { visible : false },
								insertImage			: { visible : false }
							}
						});
					});
				}
			}
		);
	});
}

function save_main()
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					var mytitle = $("#title").val();
					var mytext = $("#main_text").val();
					var mydate = $("#date").val();
					var myprice = $("#price").val();
					$.post("admin.php", { action: "manage_main", cmd: "add", title: mytitle, text: mytext, date: mydate, price: myprice }, 
					function(data) { $("#disp").html(data),
					$(".a_txt").wysiwyg ( {
							css: { fontFamily: 'Tahoma', fontSize : '8pt' }, 
							controls:
							{
								separator04 		: { visible : false },
								insertHorizontalRule: { visible : false },
								justifyLeft   		: { visible : false },
								justifyCenter 		: { visible : false },
								justifyRight  		: { visible : false },
								justifyFull   		: { visible : false },
								separator01   		: { visible : false },
								indent				: { visible : false },
								outdent				: { visible : false },
								undo				: { visible : false },
								redo				: { visible : false },
								separator02 		: { visible : false },
								separator05 		: { visible : false },
								strikeThrough 		: { visible : false },
								createLink			: { visible : false },
								insertImage			: { visible : false }
							}
						});
					});
				}
			}
		);
	});
}

function edit_item(cate, idd)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					var mymanu = $("#emanu"+idd).val();
					var mymate = $("#emate"+idd).val();
					var mysiz = $("#esiz"+idd).val();
					var mywei = $("#ewei"+idd).val();
					var mypric = $("#epric"+idd).val();
					var mydesc = $("#edesc"+idd).val();
					var mycomm = $("#comm"+idd).val();
					var myquant = $("#quant"+idd).val();
					var mycode = $("#code"+idd).val();
					var myavail = $("#avail"+idd).is(":checked");
					$.post("admin.php", { action: "manage", cmd: "edit", cat: cate, id: idd, manufact: mymanu, material: mymate, size: mysiz, weight: mywei, price: mypric, descr: mydesc, comms: mycomm, quant: myquant, code: mycode, avail: myavail }, 
					function(data) { $("#disp").html(data),
					$(".a_txt").wysiwyg( {
							css: { fontFamily: 'Tahoma', fontSize : '8pt' }, 
							controls:
							{
								separator04 		: { visible : false },
								insertHorizontalRule: { visible : false },
								justifyLeft   		: { visible : false },
								justifyCenter 		: { visible : false },
								justifyRight  		: { visible : false },
								justifyFull   		: { visible : false },
								separator01   		: { visible : false },
								indent				: { visible : false },
								outdent				: { visible : false },
								undo				: { visible : false },
								redo				: { visible : false },
								separator02 		: { visible : false },
								separator03 		: { visible : false },
								separator05 		: { visible : false },
								strikeThrough 		: { visible : false },
								createLink			: { visible : false },
								insertImage			: { visible : false }
							}
						});
					});
				}
			}
		);
	});
}

function edit_main(idd)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					var mytitle = $("#etitle"+idd).val();
					var mymain = $("#emain_text"+idd).val();
					var mydate = $("#edate"+idd).val();
					var myprice = $("#eprice"+idd).val();
					$.post("admin.php",
					{
						action: "manage_main", cmd: "edit",
						id: idd, title: mytitle, text: mymain,
						date: mydate, price: myprice
					}, 
					function(data) { $("#disp").html(data),
					$(".a_txt").wysiwyg( {
							css: { fontFamily: 'Tahoma', fontSize : '8pt' }, 
							controls:
							{
								separator04 		: { visible : false },
								insertHorizontalRule: { visible : false },
								justifyLeft   		: { visible : false },
								justifyCenter 		: { visible : false },
								justifyRight  		: { visible : false },
								justifyFull   		: { visible : false },
								separator01   		: { visible : false },
								indent				: { visible : false },
								outdent				: { visible : false },
								undo				: { visible : false },
								redo				: { visible : false },
								separator02 		: { visible : false },
								separator03 		: { visible : false },
								separator05 		: { visible : false },
								strikeThrough 		: { visible : false },
								createLink			: { visible : false },
								insertImage			: { visible : false }
							}
						});
					});
				}
			}
		);
	});
}

function save_state(idd, ind, dat)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					$.get("admin.php",
					{
						action: "manage_orders", cmd: "edit",
						id: idd, value: ind, date: dat
					}, 
					function(data) { $("#disp").html(data) });
				}
			}
		);
	});
}

function edit_state(idd, st, dat)
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					if (st == 'false') { st = 'true'; } else { st = 'false'; dat = ''; };
					$.get("admin.php",
					{
						
						action: "manage_orders", cmd: "edit",
						id: idd, state: st, date: dat
					}, 
					function(data) { $("#disp").html(data) });
				}
			}
		);
	});
}

function validate_params()
{
	$(document).ready(function()
	{
		$(document).scrollTo({top:'0px', left:'0px'}, 800,
			{ onAfter:function()
				{
					var myuser = $("#username").val();
					var mypass = $("#password").val();
					$.post("admin.php", { action: "check", user: myuser, pass: mypass }, 
					function(data) {  $("#disp").html(data); });
				}
			}
		);
	});
}

function validate_trnid()
{
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					var mypass = $("#trnid").val();
					$.post("/order_check.php", { action: "check", trn: mypass }, 
					function(data) {  $("#market_disp").html(data); });
				}
			}
		);
	});
}

function refresh_trnid(trnind)
{
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					$.post("/order_check.php", { action: "check", trn: trnind }, 
					function(data) {  $("#market_disp").html(data); });
				}
			}
		);
	});
}

function delete_url(message)
{
	confirmed = window.confirm(message);
	if (confirmed) { admin_db('drop'); };
}

function delete_sql(message, fl)
{
	confirmed = window.confirm(message);
	if (confirmed)
	{
		$(document).ready(function()
		{
			$(document).scrollTo({top:'0px', left:'0px'}, 800,
				{ onAfter:function()
					{
						$.get("admin.php", { action: "admin", cmd: "rem", file: fl }, 
						function(data) { $("#disp").html(data); });
					}
				}
			);
		});
	}
}

function rem_category(cate, idd, message)
{
	confirmed = window.confirm(message);
	if (confirmed)
	{
		$(document).ready(function()
		{
			$(document).scrollTo({top:'0px', left:'0px'}, 800,
				{ onAfter:function()
					{
						$.post("admin.php", { action: "manage", cmd: "del", cat: cate, id: idd }, 
						function(data)
						{
							$("#disp").html(data),
							$(".a_txt").wysiwyg( {
								css: { fontFamily: 'Tahoma', fontSize : '8pt' }, 
								controls:
								{
									separator04 		: { visible : false },
									insertOrderedList	: { visible : false },
									insertUnorderedList	: { visible : false },
									insertHorizontalRule: { visible : false },
									justifyLeft   		: { visible : false },
									justifyCenter 		: { visible : false },
									justifyRight  		: { visible : false },
									justifyFull   		: { visible : false },
									separator01   		: { visible : false },
									indent				: { visible : false },
									outdent				: { visible : false },
									undo				: { visible : false },
									redo				: { visible : false },
									separator02 		: { visible : false },
									separator05 		: { visible : false },
									strikeThrough 		: { visible : false },
									createLink			: { visible : false },
									insertImage			: { visible : false }
								}
							});
						});
					}
				}
			);
		});
	}
}

function rem_order(ord, idd, message)
{
	confirmed = window.confirm(message);
	if (confirmed)
	{
		$(document).ready(function()
		{
			$(document).scrollTo({top:'0px', left:'0px'}, 800,
				{ onAfter:function()
					{
						$.get("admin.php", { action: ord, cmd: "del", id: idd }, 
						function(data) { $("#disp").html(data) });
					}
				}
			);
		});
	}
}

function edit_category(categoryID)
{
	$(document).ready(function()
	{
		$('#edit'+categoryID).slideToggle();
	});
}

function add_category(categoryID)
{
	$(document).ready(function() { $('#add'+categoryID).slideToggle(); });
}

function show_cat(categoryID)
{
	$(document).ready(function() { $('#edit'+categoryID).toggle(); });
}

function show_mnu(idd)
{
	$(document).ready(function() { $('#'+idd).slideToggle('fast'); });
}

function mail_valid(summary)
{
	$(document).ready(function() { $("#vvalidator").val(summary); });
}

function add_image(cate, idd)
{
	$.ajaxFileUpload
	(
		{
			url: 'img_add.php?action=manage&cmd=edit&cat='+cate+'&id='+idd,
			secureuri: false,
			fileElementId: 'photo'+idd,
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '') { alert(data.error); }
					else { alert(data.msg); };
				};
				manage_items(cate);
			},
			error: function (data, status, e) { alert(e); }
		}
	)
	return false;
}

function add_image_main(idd)
{
	$.ajaxFileUpload
	(
		{
			url: 'img_add.php?action=manage_main&cmd=img&id='+idd,
			secureuri: false,
			fileElementId: 'photo'+idd,
			dataType: 'json',
			success: function (data, status)
			{
				if (typeof(data.error) != 'undefined')
				{
					if(data.error != '') { alert(data.error); }
					else { alert(data.msg); };
				};
				manage_rest('manage_main');
			},
			error: function (data, status, e) { alert(e); }
		}
	)
	return false;
}


function add_backup()
{
	$.ajaxFileUpload
	(
		{
			url: 'img_add.php?action=admin&cmd=makerestore',
			secureuri: false,
			fileElementId: 'backup_file',
			dataType: 'json',
			success: function (data, status)
			{
				if (typeof(data.error) != 'undefined')
				{
					if (data.error != '') { alert(data.error); }
					else { alert(data.msg); };
				};
				admin_db('view');
			},
			error: function (data, status, e) { alert(e); }
		}
	)
	return false;
}

function save_order($dat)
{
	$(document).ready(function()
	{
		var myfirstname = $("#first_name").val();
		var mylastname = $("#last_name").val();
		var myaddress1 = $("#address1").val();
		var myaddress2 = $("#address2").val();
		var mycity = $("#city").val();
		var mystate = $("#state").val();
		var myzip = $("#zip").val();
		var myemail = $("#email").val();
		var myphone2 = $("#night_phone_b").val();
		var myafm = $("#afm").val();
		var mycmp = $("#cmp").val();
		var mydoy = $("#doy").val();
		var mypaytype = $("#pay_type").val();
		var myinv = $("#invoice").val();
		var myship = $("#ship").val();
		var myitemn = $("#item_number").val();
		var myqnty = $("#quantity").val();
		var myiname = $("#item_name").val();
		var myamnt = $("#amount").val();
		var mycheck = $("#check_type").val();
		$.post("/shop/save.php",
		{
			"fname": myfirstname, "lname": mylastname,
			"addr1": myaddress1, "addr2": myaddress2,
			"ct": mycity, "stat": mystate, "zp": myzip,
			"ml": myemail, "phn2": myphone2, "afm": myafm, "cmp": mycmp,
			"doy": mydoy, "paytype": mypaytype, "invoice": myinv, "shipping": myship,
			"item_n": myitemn, "quantity": myqnty, "item_name": myiname,
			"amount": myamnt, "check_type": mycheck, "paytype": mypaytype, "date": $dat
		});
	});
}

function process_pay()
{
	$(document).ready(function()
	{
		$(document).scrollTo('#payprocess', 800,
			{ onAfter:function()
				{
					var myfirstname = $("#firstname").val();
					var myitem_name = $("#item_name").val();
					var mylastname = $("#lastname").val();
					var myitem_number = $("#item_number").val();
					var myaddress1 = $("#address1").val();
					var myaddress2 = $("#address2").val();
					var mycity = $("#city").val();
					var myamount = $("#amount").val();
					var mystate = $("#state").val();
					var myquantity = $("#quantity").val();
					var myzip = $("#zip").val();
					var myshipping_amount = $("#shipping_amount").val();
					var myptype = $("#pay_type").val();
					var myemail = $("#payer_email").val();
					var myphone2 = $("#phone2").val();
					var mytyp = $("#check_type").val();
					var myafm = $("#afm").val();
					var mycmp = $("#cmp").val();
					var mydoy = $("#doy").val();
					var myinv = $("#invoice").val();
					$.post("../shop/process.php",
					{
						"firstname": myfirstname, "item_name": myitem_name, "lastname": mylastname,
						"item_number": myitem_number, "address1": myaddress1, "address2": myaddress2,
						"city": mycity, "amount": myamount, "state": mystate,
						"quantity": myquantity, "zip": myzip, "shipping_amount": myshipping_amount,
						"email": myemail, "phone2": myphone2, "typ": mytyp, "afm": myafm, "cmp": mycmp,
						"paytype": myptype, "checktype": mytyp, "doy": mydoy, "invoice": myinv
					}, 
					function(data) { $("#market_disp").html(data); });
				}
			}
		);
	});
}

function backorder()
{
	$(document).ready(function()
	{
		$(document).scrollTo('#payprocess', 800);
		var myfirstname = $("#first_name").val();
		var mylastname = $("#last_name").val();
		var myaddress1 = $("#address1").val();
		var myaddress2 = $("#address1").val();
		var mycity = $("#city").val();
		var mystate = $("#state").val();
		var myzip = $("#zip").val();
		var myemail = $("#email").val();
		var myphone2 = $("#night_phone_b").val();
		var myafm = $("#afm").val();
		var mycmp = $("#cmp").val();
		var mydoy = $("#doy").val();
		var myinv = $("#invoice").val();
		$.post("../shop/orderform.php",
		{
			"fname": myfirstname, "lname": mylastname,
			"addr1": myaddress1, "addr2": myaddress2,
			"ct": mycity, "stat": mystate, "zp": myzip,
			"ml": myemail, "phn2": myphone2, "afm": myafm,
			"cmp": mycmp, "doy": mydoy, "invoice": myinv
		},  
		function(data) { $("#market_disp").html(data); });

	});
}

function show_mes(message, typ)
{
	
	var imag; var cls;
	if (typ == 0) { cls = "error-div"; }
	else if (typ == 1) { cls = "success-div"; };
	$(document).ready(function()
	{
		$(document).scrollTo('#market_disp', 800,
			{ onAfter:function()
				{
					$.fancybox('<div class="' + cls + '" style="width: auto; display: block; margin: 0px;">' + message + '</div>',
						{
							//'autoDimensions'	: 'true',
							'titleShow'			: 'false',
							'titlePosition' 	: 'outside',
							'width'         	: 'auto',
							'height'        	: 'auto',
							'transitionIn'		: 'none',
							'transitionOut'		: 'none'
						});
				}
			}
		);
	});
}

function extract_phone(id, msg, tc)
{
	var mg = 0; var tmp; var t = $("#pay_type").val();
	if (t == 2) { var s = 0; } else {s = 0;};
	if (id>1 & id<15)
	{ tmp = mg = 15 * s; }
	else if (id>15 & id<20)
	{ tmp = mg = 15 * s; }
	else if (id>20 & id<24)
	{ tmp = mg = 15 * s; }
	else if (id>24 & id<29)
	{ tmp = mg = 15 * s; }
	else if (id>29 & id<37)
	{ tmp = mg = 15 * s; }
	else if (id>37 & id<45)
	{ tmp = mg = 15 * s; }
	else if (id>45 & id<50)
	{ tmp = mg = 15 * s; }
	else if (id>50 & id<56)
	{ tmp = mg = 15 * s; }
	else if (id>56)
	{ tmp = mg = 15 * s; }
	else { tmp = msg; mg = 0; };
	
	var resultadd = parseInt(mg) + parseFloat(tc);
	$(document).ready(function()
	{
		mg = tmp;
		$("#shipping_amount_d").html(mg);
		$("#shipping_amount").val(mg);
		$("#final").html(resultadd + ' &euro;');
	});
}

function btoggle(id)
{
	$(document).ready(function()
	{
		switch (id)
		{
			case 0: $("#afm_c").hide(); $("#to_css").hide(); $("#cmp_c").hide(); $("#doy_c").hide(); break;
			case 1: $("#afm_c").show(); $("#to_css").show(); $("#cmp_c").show(); $("#doy_c").show(); break;
		};
	});
}
