function loadingPicDis(flag,ctrlName) {
	ctrlName = ctrlName+"_loading";
	if($(ctrlName)) {
		if(flag) {
			// display loading pic
			$(ctrlName).style.display = "";
		} else {
			$(ctrlName).style.display = "none";
		}
	}


}

// fill select option
function fillOptionData(child,selectXML,selectValue) {
		
		var optionsXML = selectXML;
		if(optionsXML != null) {
			if (optionsXML.length == 0) {
				child.options.length = 1;
				child.options[0].value = "";
				child.options[0].text = "select";
				return;
			} else {
				child.options.length = optionsXML.length;
				for (var i = 0; i < optionsXML.length; i++) {
					var optionXML = optionsXML[i];
					var option = child.options[i];
					option.value = optionXML.getAttribute("VALUE");
					option.text = optionXML.getAttribute("LABEL");
					if(selectValue != null) {
						if (option.value == selectValue) {
							child.selectedIndex = i;
						}
					}

				}
			}

		} else {
				child.options.length = 1;
				child.options[0].value = "";
				child.options[0].text = "select";
				return;
		}

}

function LoadDataFromBack(xmlStr,willFillCtrlName) {
		//alert(xmlStr);
		loadingPicDis(true,willFillCtrlName);
	    if(willFillCtrlName != null) {
	    	moveOption($(willFillCtrlName));
	    }
		
	    var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><NCI><COMMAND NAME='buygoldselect'>";
	    xml += xmlStr;
	    xml=xml+"</COMMAND></NCI>";
	    var returnText = XmlHttpPostForCms("/plat/xmlhttp", xml);
	    if(willFillCtrlName != null) {
			if(returnText != null) {
		     	var xmlDoc = toXmlDoc(returnText);
				var optionsXML = xmlDoc.documentElement.getElementsByTagName("OPTION");
				if(optionsXML != null) {
					fillOptionData($(willFillCtrlName),optionsXML,null);
				}
				delete(optionsXML);
				delete(xmlDoc);
			}
	    }
		loadingPicDis(false,willFillCtrlName);
		return returnText;
}

function DisableCtrls(flag) {
	var disableCtrls = "selectGame,selectServerIndex,selectServer,selectQuantity,selectBSort";
	var ctrls = disableCtrls.split(",");
	if(ctrls != null && ctrls.length > 0) {
		for(var i=0;i<ctrls.length;i++) {
			$(ctrls[i]).disabled = flag;
		}
	}
	delete(ctrls);
}


function resetBSort(){
	 
	 var bsortObj = $("selectBSort");
	 moveOption(bsortObj);
	 
	 bsortObj.options.length = 3
	 bsortObj.options[0].value = "USD";
	 bsortObj.options[0].text = "USD";
	 bsortObj.options[0].selectedIndex = 0;
	 
	 bsortObj.options[1].value = "EUR";
	 bsortObj.options[1].text = "EUR";
	 
	 bsortObj.options[2].value = "GBP";
	 bsortObj.options[2].text = "GBP";
}

function convertEmptyToNull(value) {
	var result = value;
	if(value == null || value == "" || value.length == 0) {
		result = "null";
	}
	return result;
}

function FillServerIndex() {
	// serverindex
	var gameId = convertEmptyToNull($("selectGame").value);
	var indexObj = $("selectServerIndex");
	moveOption(indexObj);
	if(gameId != "null" ) {
		var sorts = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";
		var sortArray = sorts.split(",");
		indexObj.options.length = sortArray.length;
		for(var i=0;i<sortArray.length;i++) {
			indexObj.options[i].value = sortArray[i];
			indexObj.options[i].text = sortArray[i];
		}
		indexObj.selectedIndex = 0;
	} else {
		fillOptionData(indexObj,null,null);
	}
}

function FillServer() {
	// server
	var gameId = convertEmptyToNull($("selectGame").value);
	var serverIndex = convertEmptyToNull($("selectServerIndex").value);
	var selectServerXml = "<flag>selectServer</flag><gameid>"+gameId+"</gameid><serverindex>"+serverIndex+"</serverindex>";
	LoadDataFromBack(selectServerXml,"selectServer");
}

function FillQuantity() {
	// quantity
	 var deploykey = convertEmptyToNull($("selectServer").value);
	 var quantityObj = $("selectQuantity");
	 moveOption(quantityObj);
	 if(deploykey == null || deploykey == "null") {
	 	fillOptionData(quantityObj,null,null);
	 } else {
		 quantityObj.options.length = 1
		 quantityObj.options[0].value = "200";
		 quantityObj.options[0].text = "200";
		 quantityObj.options[0].selectedIndex = 0;
	 }

}

function FillPrice() {
	// price
	var bsort = convertEmptyToNull($("selectBSort").value);
	var dollarPrice = 0.01;

	var moneyType = "$";
	if(bsort == "EUR") {
		moneyType = "€";
	} else if(bsort == "GBP") {
		moneyType = "￡";
	}
	$("displayMoney").innerHTML = moneyType + dollarPrice;
}


function changAction(action) {
	DisableCtrls(true);
	if(action == "FillServerIndex") {
		FillServerIndex();
		FillServer();
		FillQuantity();
		resetBSort();
		FillPrice();
		// bsortreset
		
	} else if(action == "FillServer") {
		FillServer();
		FillQuantity();
		resetBSort();
		FillPrice();
		// bsortreset
	} else if(action == "FillQuantity") {
		FillQuantity();
		resetBSort();
		FillPrice();
		// bsortreset
	} else if(action == "FillPrice") {
		FillPrice();
	}
	
	
	DisableCtrls(false);
}



 
// pay
function gotoPay() {
	if(window.event.srcElement.disabled == true ) {
		return false;
	}
	
	if($("selectQuantity").value == "") {
		alert("please, choose quantity!");
		return false
	} else if($("selectServer").value == "") {
		alert("please, choose server!");
		return false
	} else if($("contractMethod").value = 5) {
		// MSN
		var isEmail = $("contractDetail").value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
		if(isEmail == -1) {
			alert("please, input available email!");
			return false
		}
	}
	
	window.event.srcElement.disabled = true;
	disabledCurrPage(true);
	$("check_out_img").width = 16;
	$("check_out_img").height = 16;
	$("check_out_img").src = "/wow-images/loading.gif";
	
	// default is paypal
	var choosePay = "5";
	var payments = document.getElementsByName("vs_payment");
	if(payments != null && payments.length > 0) {
		for(var i=0;i<payments.length;i++) {
			if(payments[i].checked == true) {
				choosePay = payments[i].value;
			}
		}
	}
   
    
    // trans data to back
	    var xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><NDI><COMMAND NAME='ajaxbuygoldpromotion'>";
	    xml += reverseDocumentToXml();
	    xml += "<payment>"+choosePay+"</payment>";
	    xml=xml+"</COMMAND></NDI>";
	    var returnText = XmlHttpPostForCms("/plat/xmlhttp", xml);
	    //alert("returnText:"+returnText);
		if(returnText != null) {
			//$("helloworld").value = returnText;
		    var xmlDoc = toXmlDoc(returnText);
		    var optionsXML = null;
			if(getOs() == "Firefox" || getOs() == "Safari" ) {
				optionsXML = xmlDoc.documentElement.getElementsByTagName("OPTION");
			} else if(getOs() == "MSIE" ) {
				optionsXML = xmlDoc.documentElement.selectNodes("OPTION");
			}
			if(optionsXML != null) {
				// build form
				createPayFormAndSubmit(optionsXML);
				
				// auto location page
				window.setTimeout("window.location.href = '/buy-promotion-wow-gold.html';","2000");
			}
			delete(optionsXML);
			delete(xmlDoc);
		}
		
}

function FillGame() {
	// Fill Game
	var selectGameXml = "<flag>FillGame</flag>";
	LoadDataFromBack(selectGameXml,"selectGame");
}

function buyGold_onload(gameId) {
	FillGame();
	if(gameId) {
		autoChangeSelectDefault($("selectGame"),gameId);
	}
	changAction('FillServerIndex');
}







