// JavaScript Document
$(document).ready(function(){
	loadServer();
	loadTradeMethod();
});

$("#gameId").change(function(){
	loadServer();
	loadTradeMethod();
});

function loadServer(){
	document.getElementById("serverId").length=0;
	document.getElementById("serverId").options[0]=new Option("loading...", 0);
	$.ajax({
		type: "get",
		global: false,
		data: "gameId=" + $("#gameId").val() + "&rand=" + Math.random(),
		processData: false,
		url: "loadServer.php",
		dateType: "html",
		async: true,
		success: function (msg){
			serverSelectArr=msg.split("||");
			for ( i=0;i<=serverSelectArr.length-1;i++ ){
				serverArr=new Array();
				serverArr=serverSelectArr[i].split("|");
		
				document.getElementById("serverId").options[i]=new Option(serverArr[1], serverArr[0]);
			};
		}
	});
}

function loadTradeMethod(){
	document.getElementById("o_trade_method").length=0;
	document.getElementById("o_trade_method").options[0]=new Option("loading...", 0);
	$.ajax({
		type: "get",
		global: false,
		data: "gameId=" + $("#gameId").val() + "&rand=" + Math.random(),
		processData: false,
		url: "loadTradeMethod.php",
		dateType: "html",
		async: true,
		success: function (msg){
			productSelectArr=msg.split("||");
			for ( i=0;i<=productSelectArr.length-1;i++ ){
				productArr=new Array();
				productArr=productSelectArr[i].split("|");
		
				document.getElementById("o_trade_method").options[i]=new Option(productArr[1], productArr[0]);
			};
		}
	});
}

$("#serverId").change(function(){
	loadProduct();
});

function loadProduct(){
	document.getElementById("product2").length=0;
	document.getElementById("product2").options[0]=new Option("loading...", 0);
	$.ajax({
		type: "get",
		global: false,
		data: "serverId=" + $("#serverId").val() + "&rand=" + Math.random(),
		processData: false,
		url: "loadProduct.php",
		dateType: "html",
		async: true,
		success: function (msg){
			productSelectArr=msg.split("||");
			for ( i=0;i<=productSelectArr.length-1;i++ ){
				productArr=new Array();
				productArr=productSelectArr[i].split("|");
		
				document.getElementById("product2").options[i]=new Option(productArr[1], productArr[0]);
			};
		}
	});
}

$("#quickOrderForm").submit(function(){
	return quickOrderCheck();
});
function quickOrderCheck(){
	if ( $.trim($("#gameId").val())==0 ){
		alert("Please select your game!");
		$("#gameId").focus();
		return false;
	}else if ( $.trim($("#serverId").val())==0 ){
		alert("Please select your server!");
		$("#serverId").focus();
		return false;
	}else if ( $.trim($("#product2").val())==0 ){
		alert("Please select your product!");
		$("#product2").focus();
		return false;
	}else if ( $("#o_count2").val().search(/^[\d]+$/)==-1 ){
		alert("Please input quantity!");
		$("#product2").focus();
		return false;
	}else if ( $.trim($("#o_contact_name2").val())=="" ){
		alert("Please input full name!");
		$("#o_contact_name2").focus();
		return false;
	}else if ( $.trim($("#o_game_character2").val())=="" ){
		alert("Please input character!");
		$("#o_game_character2").focus();
		return false;
	}else if ( $("#o_email2").val().search(/^[\w\-\.]+[\@]{1}[\w\-]+([\.]{1}[\w]+)+$/)==-1 ){
		alert("Please input e-mail!");
		$("#o_email2").focus();
		return false;
	}else if ( $.trim($("#o_phone2").val())=="" ){
		alert("Please input phone number!");
		$("#o_phone2").focus();
		return false;	
	}else{
		if ( $.trim($("#o_coupon2").val())!='' ){
			if ( couponCheck($("#o_coupon2").val())==0 ){
				alert("The coupons can not be used!");
				$("#o_coupon2").val('');
				$("#o_coupon2").focus();
				return false;
			}
		}
	}
	var goUrl="quick.php?serverId=" + $("#serverId").val() + "&product=" + $("#product2").val();
	document.getElementById("quickOrderForm").action=goUrl;
}

function couponCheck(number){
	var data=$.ajax({
		type: "get",
		global: false,
		data: "number=" + number + "&rand=" + Math.random(),
		processData: false,
		url: "checkCoupon.php",
		dateType: "html",
		async: false
	}).responseText;
	return data;
}
