$(document).ready(function () {
		$('#gameId').change(function () {
		    $('#price').val('');
		    $('#productQuantity').val('');
				var gid = $(this).val();
				$.ajax({
				    type: 'POST', 
						timeout: 10000, 
						url: 'ajax/fit.gold.server.ajax.php', 
						data: {'gid':gid}, 
						dataType: 'html', 
						error: function (a, b) { 
						    alert('Sorry, request timeout, click confirm to reload this page.');
								window.location.reload(document.URL);
						},
						beforeSend: function () { $('#serverId').attr('disabled', true); }, 
						success: function (data) { 
								$('#serverId').attr('disabled', false);
						    $('#serverId').html(data); 
						}
				});
				$.post('ajax/fit.gold.get.unit.ajax.php', {'gid':gid}, function (data) {$('#fitGoldUnit').html(data);});
		});
		
    $('#serverId').change(function () {
		    $('#price').val('');
		    $('#productQuantity').val('');
				var gid = $('#gameId').val();
				var serverId = $(this).val();
				$.ajax({
				    type: 'POST', 
						timeout: 10000, 
						url: 'ajax/fit.gold.index.package.ajax.php', 
						data: {'gid':gid, 'serverId':serverId}, 
						dataType: 'html', 
						error: function (a, b) { 
						    alert('Sorry, request timeout, click confirm to reload this page.');
								window.location.reload(document.URL);
						},
						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();
				gid = parseInt(gid);
				if(isNaN(gid) || gid == 0){
				    alert('Please choose game');
						$('#gameId').focus();
						return false;
				}
				var serverId = $('#serverId').val();
				serverId = parseInt(serverId);
				if(isNaN(serverId) || serverId == 0){
				    alert('Please choose server');
						$('#serverId').focus();
						return false;
				}
				$.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();
				gid = parseInt(gid);
				if(isNaN(gid) || gid == 0){
				    alert('Please choose game');
						$('#gameId').focus();
						return false;
				}
				var serverId = $('#serverId').val();
				serverId = parseInt(serverId);
				if(isNaN(serverId) || serverId == 0){
				    alert('Please choose server');
						$('#serverId').focus();
						return false;
				}
				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;
				}
		});
});