﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

$(document).ready(function(){
	//global vars
	
    var inputProductid = $("#productid");
	var inputQuantity = $("#quantity");
	var inputOrderid = $("#orderid");
	var loading = $("#loading");
	var messageList = $(".content > ul");
	
	//functions
	function updateShoutbox(){
		//just for the fade effect
		messageList.hide();
		loading.show();
		//send the post to shoutbox.php
		$.ajax({
			type: "POST", url: "orderproc.php", data: "action=update",
			complete: function(data){
				loading.hide();
				messageList.html(data.responseText);
				messageList.show(2);
			}
		});
	}
	//check if all fields are filled
	function checkForm(){
		if(inputQuantity.attr("value"))
			return true;
		else
			return false;
	}
	
	//Load for the first time the shoutbox data
	updateShoutbox();
	
	//on submit event
	$("#form").submit(function(){
		if(checkForm()){
			var quantity = inputQuantity.attr("value");
			var productid = inputProductid.attr("value");
			var orderid = inputOrderid.attr("value");
			//we deactivate submit button while sending
			$("#send").attr({ disabled:true, value:"Add to Order.." });
			$("#send").blur();
			//send the post to shoutbox.php
			$.ajax({
				type: "POST", url: "orderproc.php", data: 'action=insert&productid=' + productid + "&quantity=" + quantity + "&orderid=" + orderid,
				complete: function(data){
					messageList.html(data.responseText);
					updateShoutbox();
					//reactivate the send button
					$("#send").attr({ disabled:false, value:"Add to Order" });
				}
			 });
		}
		else alert("Please select quantity!");
		//we prevent the refresh of the page after submitting the form
		return false;
	});
});
