﻿$(function(){ 

	var game_select    =  $('#fr_game');
	var server_select  =  $('#fr_server');
	var product_select =  $('#fr_product');
	var money_choose   =  $('input[name=moneytype]');
	var fastorderbt    =  $('#fastorderbt');
	var price		   =  $('#price');
	var selccode	   =  $('#selccode');
	$.get('/inc/ajax.fastorder.asp?action=GetGameList', function(data) {
		if(data!=''){ 
			game_select.append($(data));
			game_select.removeAttr('disabled');
			game_select.children('option').last().attr('selected',true);//alert(game_select.children('option').last().val())
			game_select.change();
			
		}
	})
	
	game_select.change(function(){
		server_select.children('option:gt(0)').remove();
		$.get('/inc/ajax.fastorder.asp?action=GetServerList&GameID='+$(this).val(), function(data) {
			if(data!=''){ 
				server_select.append($(data));
				server_select.removeAttr('disabled');
				server_select.children('option').last().attr('selected',true);
				server_select.change();
				product_select.attr('disabled','disabled');
			}	
		})	
	})
	server_select.change(function(){
		product_select.children('option:gt(0)').remove();
		$(price).val(0);
		$.get('/inc/ajax.fastorder.asp?action=GetProductList&ServerID='+$(this).val()+'&CurrencyID='+$('input[name=moneytype]:checked').val(), function(data) {
			if(data!=''){ 
				product_select.append($(data));
				product_select.removeAttr('disabled');
			}
		})	
	})
	product_select.change(function(){ 
		setPrice();
	})
	money_choose.click(function(){
		if ( !($('#fr_product').val()>0) ){return true;}
		var value = product_select.val();
		setCookie("currencyID",1+Number($(this).val()));
		product_select.children('option:gt(0)').remove();
		$.get('/inc/ajax.fastorder.asp?action=GetProductList&ServerID='+server_select.val()+'&CurrencyID='+$(this).val(), function(data) {
			if(data!=''){ 
				product_select.append($(data));
				product_select.children('option[value='+value+']').attr('selected',true);
				setPrice();
			}
		})	

	})
	fastorderbt.click(function(){
		if($('#fr_product').children('').length>1)					   
			location.href = '/shoppingcart.asp?pid='+product_select.val();
	})

	function setPrice(){ 
		var re = /.*\((.*?)([A-Z]{3})\)$/;
		var text = product_select.children('option:selected').text()
		re.test( String(text) );
		$(price).val( RegExp.$1 );
		$(selccode).html( RegExp.$2 );
	}

})
