$(document).ready(function () {
    $('#serverId').change(function () {
		    var serverId = $(this).val();
				var gid = $('#gameId').val();
				$.ajax({
						timeout: 10000, 
				    type: 'POST', 
						url: '../ajax/fit.gold.package.ajax.php', 
						data: {'gid':gid, 'serverId':serverId}, 
						dataType: 'html', 
						error: function (a, b) { alert(b); },
						beforeSend: function () { $('#fitPackStore').html('Please wait, loading...'); }, 
						success: function (data) { $('#fitPackStore').html(data); }
				});
		});
		
		$('#calculate').mouseover(function(){ $(this).css('cursor', 'pointer'); });
		$('#calculate').click(function () {
				var qtn = parseFloat($('#productQuantity').val());
				if(isNaN(qtn)) {
				    alert('The Quantity must be a number');
						$('#productQuantity').focus();
						return false;
				}
				var gid = $('#gameId').val();
				var serverId = $('#serverId').val();
				$.ajax({
				    timeout: 10000,
						type: 'POST',
						url: '../ajax/fit.gold.retail.ajax.php', 
						data: {'gid':gid, 'serverId':serverId, 'qtn':qtn}, 
						dataType: 'json',
						error: function (a, b) {
						    alert('Sorry, request timeout, click confirm to reload this page.');
								window.location.reload(document.URL);
						},
						beforeSend: function () { 
						    
						},
						success: function (data) { $('#price').val(data.toFixed(2)); }
				});
		});
		
    $('#gold').submit(function () {
				var qtn = parseFloat($('#productQuantity').val());
				if(isNaN(qtn)) {
				    alert('The Quantity must be a number');
						$('#productQuantity').focus();
						return false;
				}
				var gid = $('#gameId').val();
				var serverId = $('#serverId').val();
				var flag = false;
				$.ajax({
				    async: false,
						timeout: 10000,
						type: 'POST',
						url: '../ajax/fit.gold.retail.ajax.php', 
						data: {'gid':gid, 'serverId':serverId, 'qtn':qtn}, 
						dataType: 'json',
						error: function (a, b) {
						    alert('Sorry, request timeout, click confirm to reload this page.');
								window.location.reload(document.URL);
						},
						beforeSend: function () { 
						    
						},
						success: function (data) { $('#price').val(data.toFixed(2)); flag = true; }
				});
				if(!flag){
				    alert('Retry after 1 seconds.');
						return false;
				}
				
		    if ($('#productQuantity').val() < 1) {
				    alert('Quantity must be more than 0.');
						$('#productQuantity').focus();
						return false;
				}
				if ($('#price').val() < 1) {
				    alert('Price must be more than 0.');
						$('#productQuantity').focus();
						return false;
				}
				if ($('#role').val() == '') {
				    alert('Character Name must be filled.');
						$('#role').focus();
						return false;
				}
		});
});
