var showCustomAll = false;
var showSkillAll = false;
var showHonorAll = false;
var showArenaAll = false;
var showBadgeAll = false;
var showMisAll = false;
var packageClass;
var onLoading = false;
$(document).ready(function(){
    var gameName = $("#hidGameName").attr("value");
	if(gameName.toLowerCase().indexOf("eu") == 0)
	{
		game.ajax.common.SetCurrency("EUR");
	}
	else
	{
		game.ajax.common.SetCurrency("USD");
	}
    $("#drpStart").change(function(){ChangeLevel();});
    $("#drpEnd").change(function(){ChangeLevel();});
	if(gameName.toLowerCase().indexOf("wow") < 0)
	{
		$("#moreCustom").click(function(){MakePakage("PackageCustom", '0', showCustomAll ? 30 : 0);showCustomAll = showCustomAll ? false : true ;});
	}
	else
	{
		$("#moreCustom").click(function(){MakePakage("PackageCustom", '15', showCustomAll ? 30 : 0); this.childNodes[0].src=showCustomAll ? "/image/more_.gif" : "/image/more.gif"; showCustomAll = showCustomAll ? false : true ; });
		$("#moreSkill").click(function(){MakePakage("PackageSkill", '16', showSkillAll ? 30 : 0); this.childNodes[0].src=showSkillAll ? "/image/more_.gif" : "/image/more.gif"; showSkillAll = showSkillAll ? false : true ;});
		$("#moreHonor").click(function(){MakePakage("PackageHonor", '1', showHonorAll ? 30 : 0); this.childNodes[0].src=showHonorAll ? "/image/more_.gif" : "/image/more.gif"; showHonorAll = showHonorAll ? false : true;});
		$("#moreArena").click(function(){MakePakage("PackageArena", '2', showArenaAll ? 30 : 0); this.childNodes[0].src=showArenaAll ? "/image/more_.gif" : "/image/more.gif"; showArenaAll = showArenaAll ? false : true;});
		$("#moreBadge").click(function(){MakePakage("PackageBadge", '22', showBadgeAll ? 30 : 0); this.childNodes[0].src=showBadgeAll ? "/image/more_.gif" : "/image/more.gif"; showBadgeAll = showBadgeAll ? false : true;});
		$("#moreMis").click(function(){MakePakage("PackageMis", '8', showMisAll ? 30 : 0); this.childNodes[0].src=showMisAll ? "/image/more_.gif" : "/image/more.gif"; showMisAll = showMisAll ? false : true;});
	}
    ReloadCurrency();
})

function ReloadCurrency()
{
	
	ChangeLevel();
	var gameName = $("#hidGameName").attr("value");
	if(gameName.toLowerCase().indexOf("wow") < 0)
	{
		MakePakage("PackageCustom", '0', 30);
		showCustomAll = false;
	}
	else
	{
		//$("#divCustom").css("display", "none");
	    //$("#divSkill").css("display", "none");
	    //$("#divHonor").css("display", "none");
	    //$("#divArena").css("display", "none");
	    //$("#divMis").css("display", "none");
		$("#moreCustom").click();
		$("#moreSkill").click();
		$("#moreHonor").click();
		$("#moreArena").click();
		$("#moreBadge").click();
		$("#moreMis").click();
		var showid = getQueryString("show");
		switch(showid)
		{
			case "1": $("#moreCustom").click(); showCustomAll = false; $("#divCustom").css("display", "block"); break;
			case "2": $("#moreSkill").click(); showSkillAll = false; $("#divSkill").css("display", "block"); break;
			case "3": $("#moreHonor").click(); showHonorAll = false; $("#divHonor").css("display", "block"); break;
			case "4": $("#moreArena").click(); showArenaAll = false; $("#divArena").css("display", "block"); break;
			case "6": $("#moreBadge").click(); showBadgeAll = false; $("#divBadge").css("display", "block"); break;
			case "5": $("#moreMis").click(); showMisAll = false; $("#divMis").css("display", "block"); break;
			default: $("#moreCustom").click(); $("#divCustom").css("display", "block"); break;
		}
	}
}

function MakePakage(package, Class, maxCount)
{
	try
	{
	$("#moreCustom")[0].childNodes[0].src = "/image/more.gif";
	$("#moreSkill")[0].childNodes[0].src = "/image/more.gif";
	$("#moreHonor")[0].childNodes[0].src = "/image/more.gif";
	$("#moreArena")[0].childNodes[0].src = "/image/more.gif";
	$("#moreBadge")[0].childNodes[0].src = "/image/more.gif";
	$("#moreMis")[0].childNodes[0].src = "/image/more.gif";
	}catch(e){}
    var pack = $("#" + package);
    var child = pack[0].childNodes;
    if(maxCount == 0)
    { 
        pack.css("display","none");
    }
    else if(child.length > 1)
    {
		showCustomAll = true;
		showSkillAll = true;
		showHonorAll = true;
		showArenaAll = true;
		showBadgeAll = true;
		showMisAll = true;
		$("#PackageCustom").css("display","none");
		$("#PackageSkill").css("display","none");
		$("#PackageHonor").css("display","none");
		$("#PackageArena").css("display","none");
		$("#PackageBadge").css("display","none");
		$("#PackageMis").css("display","none");
	
       pack.css("display","block");  
    }
    else
    {
		showCustomAll = true;
		showSkillAll = true;
		showHonorAll = true;
		showArenaAll = true;
		showBadgeAll = true;
		showMisAll = true;
		$("#PackageCustom").css("display","none");
		$("#PackageSkill").css("display","none");
		$("#PackageHonor").css("display","none");
		$("#PackageArena").css("display","none");
		$("#PackageBadge").css("display","none");
		$("#PackageMis").css("display","none");
	
       if(onLoading)
       {
          alert("Is Loding! Please Wait!");
          return;
       }
       pack.css("display","block");
       onLoading = true;
       pack.html("<div style=\"width:100%;text-align:center; color:#000; border-bottom:#519ED2 solid 1px; padding:15px 0 15px 0;\">Loading....<img src=\"../image/loading.gif\" /></div>");
	   var gameId = $("#hidGameId").attr("value");
	   packageClass = package;
	    game.powerlevel.common.GetPackageslist(gameId, Class, MakeHtml);
	}
}

function MakeHtml(response)
{
	if(response == null || response.value == null)
	{
		return;
	}
	var html = "";
	var htmlTemplate = "<div style=\"width:634px; border-bottom:#519ED2 solid 1px; height:39px; position:relative; color: #1862A3; font-weight:bold;\" ><div style=\"position:absolute; top:7px; left:15px; width:228px; line-height:12px; word-break:break-all;\" align=\"left\"><a style=\"cursor:pointer;\" id=\"$aDetail$\" onclick=\"DisplayHidden(this.id,'$aID$')\">$PackName$</a></div><div style=\"position:absolute; top:13px; left:300px; width:90px; line-height:12px;\" align=\"left\">$RequestDays$</div><div style=\"position:absolute; top:13px; left:407px; width:50px; line-height:12px;\" align=\"left\">$PackPrice$</div><div style=\"position:absolute; top:8px; left:502px; width:115px; line-height:12px;\" align=\"left\"><a style=\"cursor:pointer;\" onclick=\"CheckOut('$cartName$','$cartMoneyCode$','$cartPrice$','$cartWebtime$','$cartStartLevel$','$cartEndLevel$','$cartLevelType$','$cartChooseType$')\"><img src=\"/image/BUY.gif\" /></a></div></div>";
	
	var arrayPack = response.value;
	for(var i = 0; i < arrayPack.length; i++)
	{
		var tempHtml = htmlTemplate;
		tempHtml = tempHtml.replace("$PackName$", arrayPack[i][1].replace("\\","").replace("new.gif","../adsimages/new.gif"));
		tempHtml = tempHtml.replace("$RequestDays$", arrayPack[i][7]);
		tempHtml = tempHtml.replace("$PackPrice$", arrayPack[i][3] + arrayPack[i][4]);
		tempHtml = tempHtml.replace("$cartName$", arrayPack[i][1].replace("\"","\\\"").replace("new.gif","../adsimages/new.gif"));
		tempHtml = tempHtml.replace("$cartMoneyCode$", arrayPack[i][2]);
		tempHtml = tempHtml.replace("$cartPrice$", arrayPack[i][4]);
		tempHtml = tempHtml.replace("$cartWebtime$", arrayPack[i][11]);
	    tempHtml = tempHtml.replace("$cartStartLevel$", arrayPack[i][5]);
		tempHtml = tempHtml.replace("$cartEndLevel$", arrayPack[i][6]);
		tempHtml = tempHtml.replace("$cartLevelType$", arrayPack[i][0]);
		tempHtml = tempHtml.replace("$cartChooseType$", arrayPack[i][9]);
		tempHtml = tempHtml.replace("$aDetail$", "aDetail_" + packageClass + "_" + i);
		tempHtml = tempHtml.replace("$aID$", arrayPack[i][10]);
		html = html + tempHtml;
	}
	var packRef = $("#" + packageClass);
	var headHtml = "<div style=\"height:20px; background:#519ED2; color:#FFFFFF; font-weight:bold; padding-top:10px; padding-left:20px;line-height:10px!important;\" align=\"left\"><div style=\"float:left; width:200px; padding-left:60px;\">Item Name</div><div style=\"float:left; width:90px; padding-left:20px;;\">Days</div><div style=\"float:left; width:70px; padding-left:20px;;\">Price</div><div style=\"float:left; width:150px; text-align:center;\">Buy</div></div>";
	packRef.html(headHtml + html);
	onLoading = false;
}

function ChangeLevel()
{
	var drpStart = $("#drpStart");
	var drpEnd = $("#drpEnd");
	var hidGameId = $("#hidGameId");
	var hidSubmit = $("#hidSubmit");
	var labPrice = $("#labPrice");
	var labDays = $("#labDays");
	var moneyCode = $("#labMoneyCode");
	labPrice.attr("value", "");
	labDays.attr("value", "");
	if(drpStart[0].value =="All" || drpEnd[0].value == "All")
	{
		return;
	}
	var start = Number(drpStart[0].value);
	var end = Number(drpEnd[0].value);
	if(start >= end)
	{
		alert("Current Level must less than Desired Level.");
		return;
	}
	var result = game.powerlevel.custom.GetPrice(hidGameId.attr("value"), start, end).value;
	if(result != null && result != "")
	{
		var result1 = result.split("|");
		labDays.attr("value", result1[6].replace("&nbsp;", " "));	
		labPrice.attr("value", result1[1]);
		moneyCode.html(result1[2]);
		hidSubmit.attr("value","Level " + start + " to " + end + "|" + result1[2] + "|" + result1[1] + "|" + result1[0] + "|" +result1[4] + "|" + result1[5]);
	}				
}

function CustomLevelSubmit()
{
	var drpStart = $("#drpStart");
	var drpEnd = $("#drpEnd");
	var labPrice = $("#labPrice");				
	if(drpStart[0].value == "All" || drpEnd[0].value == "All")
	{
		alert("Please select Current Level and Desired Level.");
		return;
	}
	if(labPrice.attr("value") == "")
	{
		alert("Price can't be empty.");
		return;
	}
	var start = Number(drpStart.value);
	var end= Number(drpEnd.value);
	if(start >= end)
	{
		alert("Current Level must less than Desired Level.");
		return;
	}															
	var result = $("#hidSubmit").attr("value").split("|");
	CheckOut(result[0],result[1],result[2],result[3],result[4],result[5],"0","0");
}

function CheckOut(name,moneycode,price,webtime,startlevel,endlevel,leveltype,choosetype)
{
	
	InfoHidden();
	var result = game.powerlevel.common.SaveForm($("#hidGameName").attr("value"), "", name, moneycode, price, webtime, startlevel, endlevel, leveltype, choosetype, "0");
	Main("PL_CheckOut");
	window.location="confirm_step1.html";
}

function SwitchPackage(package)
{
	$("#divCustom").css("display", "none");
	$("#divSkill").css("display", "none");
	$("#divHonor").css("display", "none");
	$("#divArena").css("display", "none");
	$("#divBadge").css("display","none");
	$("#divMis").css("display", "none");
	$(package).css("display", "block");
	switch(package)
	{
		case "#divLevel": showCustomAll = true;$("#divCustom").css("display", "block"); $("#moreCustom").click(); break;
		case "#divSkill": showSkillAll = true; $("#moreSkill").click(); break;
		case "#divHonor": showHonorAll = true; $("#moreHonor").click(); break;
		case "#divArena": showArenaAll = true; $("#moreArena").click(); break;
		case "#divBadge": showBadgeAll = true; $("#moreBadge").click(); break;
		case "#divMis": showMisAll = true; $("#moreMis").click(); break;
		default: break;
	}
}
