$(document).ready(function(){
    if(document.getElementById("hidGameName"))
	{
		$("#drpStart").change(function(){ChangeLevel();});
		$("#drpEnd").change(function(){ChangeLevel();});
		$("#drpClass").change(function(){ChangeLevel();});
		if($("#hidGameName").attr("value").toLowerCase().indexOf("eu") == 0 || $("#hidGameName").attr("value").toLowerCase() == "metin2")
		{
			ChangeCurrencyNoRefresh("EUR", 'EUR');
			if($("#hidPackageClassID").attr("value") == 26)
			{
				LoadGearsClass();
			}
			else if($("#hidPackageClassID").attr("value") == 32)
			{
				LoadItemsClass(0);
			}
			else
			{
				ReloadCurrency();
			}
		}
		else
		{
			if($("#hidPackageClassID").attr("value") == 26)
			{
				LoadGearsClass();
			}
			else if($("#hidPackageClassID").attr("value") == 32)
			{
				LoadItemsClass(0);
			}
			else
			{
				ReloadCurrency();
			}
		}
	}
})

function CheckOut(name,moneycode,price,webtime,startlevel,endlevel,leveltype,choosetype)
{
	InfoHidden();
	var result = PowerLevel_Common.SaveForm($("#hidGameName").attr("value"), "", name, moneycode, price, webtime, startlevel, endlevel, leveltype, choosetype);
	Main("PL_CheckOut");
	window.location="confirmstep1.aspx";
}

function ReloadCurrency()
{
	var gameId = $("#hidGameId").attr("value");
	var packageClass = $("#hidPackageClassID").attr("value");
	PowerLevel_Common.GetPackageslist(gameId, packageClass, MakeHtml);
}

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 = PowerLevel_Common.GetLevelPrice(hidGameId.attr("value"), start, end).value;
	if(result != null && result != "")
	{
		var result1 = result.split("|");
		labDays.html(result1[6].replace("&nbsp;", " "));	
		labPrice.html(result1[3] + 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 MakeHtml(response)
{
	if(response == null || response.value == null)
	{
		return;
	}
	var html = "";
	var htmlPieces = "";
/*	
	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=\"/images/BUY.gif\" /></a></div></div>";
	*/
	var htmlTemplate = "<tr  onMouseOver=\"this.className='bj2';\" onMouseOut=\"this.className='bj';\"><td align=\"left\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\" ><a style=\"cursor:pointer;\" id=\"$aDetail$\" onclick=\"DisplayHidden(this.id,'$aID$',true)\" onmouseover=\"DisplayHidden(this.id,'$aID$',false)\" onmouseout=\"InfoHidden(false)\">$PackName$</a></td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  >$RequestDays$</td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  >$PackPrice$</td><td align=\"center\" valign=\"middle\" style=\" border-bottom:1px solid #660105;\"  ><input type=\"button\" name=\"button4\" id=\"button4\" value=\"check out\" onclick=\"CheckOut('$cartName$','$cartMoneyCode$','$cartPrice$','$cartWebtime$','$cartStartLevel$','$cartEndLevel$','$cartLevelType$','$cartChooseType$')\" /></td></tr>";
/*	var htmlTemplate = "<div class=\"bg2\"><div class=\"bg2_1\" align=\"center\"> <a style=\"cursor:pointer;\" id=\"$aDetail$\" onclick=\"DisplayHidden(this.id,'$aID$')\">$PackName$</a></div><div class=\"bg2_1\" style=\"width:86px;\" align=\"center\">$RequestDays$</div><div class=\"bg2_1\" style=\"width:82px;\" align=\"center\">$PackPrice$</div><div class=\"bg2_2\" style=\"width:120px;\" align=\"center\"><input type=\"button\" name=\"button4\" id=\"button4\" value=\"check out\" onclick=\"CheckOut('$cartName$','$cartMoneyCode$','$cartPrice$','$cartWebtime$','$cartStartLevel$','$cartEndLevel$','$cartLevelType$','$cartChooseType$')\" /></div></div>";*/
	var htmlTemplatePieces = "<tr onMouseOver=\"this.className='bj2';\" onMouseOut=\"this.className='bj';\"><td align=\"left\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\" ><a style=\"cursor:pointer;\" id=\"$aDetail$\" onmouseover=\"DisplayHidden(this.id,'$aID$')\" onmouseout=\"InfoHidden()\">$PackName$</a></td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  >$Pieces$</td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  >$RequestDays$</td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  >$PackPrice$</td><td align=\"center\" valign=\"middle\" style=\" border-bottom:1px solid #660105;\"  ><input type=\"button\" name=\"button4\" id=\"button4\" value=\"check out\" onclick=\"CheckOut('$cartName$','$cartMoneyCode$','$cartPrice$','$cartWebtime$','$cartStartLevel$','$cartEndLevel$','$cartLevelType$','$cartChooseType$')\" /></td></tr>";
	
	var arrayPack = response.value;
	for(var i = 0; i < arrayPack.length; i++)
	{
		var tempHtml;
		if(arrayPack[i][12] == "")
		{
			tempHtml = htmlTemplate;
		}
		else
		{
			tempHtml = htmlTemplatePieces;
			tempHtml = tempHtml.replace("$Pieces$", arrayPack[i][12]);
		}
		tempHtml = tempHtml.replace(/\$PackName\$/g, 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_1" + "_" + i);
		tempHtml = tempHtml.replace(/\$aID\$/g, arrayPack[i][10]);
		if(arrayPack[i][12] == "")
		{
			html = html + tempHtml;
		}
		else
		{
			htmlPieces = htmlPieces + tempHtml;
		}
	}
	var packRef = $("#Package");
	var headHtml = "<tr  class=\"lv\"><td width=\"55%\" align=\"left\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\" >Items</td><td width=\"16%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Days</td><td width=\"13%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Price</td><td width=\"16%\" align=\"center\" valign=\"middle\" style=\" border-bottom:1px solid #660105;\"  >Checkout</td></tr>";
	/*var headHtml = "<div class=\"bg1\"><div class=\"bg1_1\" align=\"center\">Items</div><div class=\"bg1_1\" style=\"width:85px;\" align=\"center\">Days</div><div class=\"bg1_1\" style=\"width:80px;\" align=\"center\">Price</div><div class=\"bg1_1\" style=\"width:118px; border-right:0px;\" align=\"center\">CheckOut</div></div>";*/
	var headHtmlPieces = "<tr  class=\"lv\"><td width=\"33%\" align=\"left\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\" >Items</td><td width=\"22%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Pieces</td><td width=\"16%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Days</td><td width=\"13%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Price</td><td width=\"16%\" align=\"center\" valign=\"middle\" style=\" border-bottom:1px solid #660105;\"  >Checkout</td></tr>";
	if(html != "")
	{
		$("#Package").html(headHtml + html);
		$("#divPackage").css("display", "block");
	}
	if(htmlPieces != "")
	{
	    $("#PackagePieces").html(headHtmlPieces + htmlPieces);
		$("#divPieces").css("display", "block");
	}
}

function MakeItemHtml(response)
{
	if(response == null || response.value == null)
	{
		return;
	}
	var htmlPieces = "";
	var htmlTemplatePieces = "<tr onMouseOver=\"this.className='bj2';\" onMouseOut=\"this.className='bj';\"><td align=\"left\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\" ><a style=\"cursor:pointer;\" id=\"$aDetail$\" onmouseover=\"DisplayHidden(this.id,'$aID$')\" onmouseout=\"InfoHidden()\">$PackName$</a></td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  ><a href=\"http://www.aionarmory.com/item.aspx?id=$ItemID$\" target=\"_blank\"><img style=\"width:40px; height:40px; border:0px;\" src=\"$ItemPic$\" /></a></td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  >$RequestDays$</td><td align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105; color:#FFFF00\"  >$PackPrice$</td><td align=\"center\" valign=\"middle\" style=\" border-bottom:1px solid #660105;\"  ><input type=\"button\" name=\"button4\" id=\"button4\" value=\"check out\" onclick=\"CheckOut('$cartName$','$cartMoneyCode$','$cartPrice$','$cartWebtime$','$cartStartLevel$','$cartEndLevel$','$cartLevelType$','$cartChooseType$')\" /></td></tr>";
	
	var arrayPack = response.value;
	for(var i = 0; i < arrayPack.length; i++)
	{
		var tempHtml;
		tempHtml = htmlTemplatePieces;
		tempHtml = tempHtml.replace("$ItemID$", arrayPack[i][13]);
		tempHtml = tempHtml.replace("$ItemPic$", "http://www.g4pitem.com/" + arrayPack[i][14]);
		tempHtml = tempHtml.replace(/\$PackName\$/g, 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_1" + "_" + i);
		tempHtml = tempHtml.replace(/\$aID\$/g, arrayPack[i][10]);
		htmlPieces = htmlPieces + tempHtml;
	}
	var packRef = $("#Package");
	var headHtmlPieces = "<tr  class=\"lv\"><td width=\"33%\" align=\"left\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\" >Items</td><td width=\"22%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Pieces</td><td width=\"16%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Days</td><td width=\"13%\" align=\"center\" valign=\"middle\" style=\" border-right:1px solid #660105; border-bottom:1px solid #660105;\"  >Price</td><td width=\"16%\" align=\"center\" valign=\"middle\" style=\" border-bottom:1px solid #660105;\"  >Checkout</td></tr>";
	if(htmlPieces != "")
	{
	    $("#PackagePieces").html(headHtmlPieces + htmlPieces);
		$("#divPieces").css("display", "block");
	}
}

var currentGears = 4;
function GetGears(gears)
{
	currentGears = gears;
	var gameId = $("#hidGameId").attr("value");
	var packageClass = $("#hidPackageClassID").attr("value");
	PowerLevel_Common.GetPackageslistByGearsClass(gameId, packageClass, currentGears, MakeHtml);
}
function LoadGearsClass()
{
	var result = PowerLevel_Common.GetGearsClass();
	if(result != null)
	{
		var html = "<ul class=\"gearsclass\">";
		var tb = result.value;
		for(var i = 0; i < tb.Rows.length; i ++)
		{
			html = html + "<li><a onclick=\"GetGears(" + tb.Rows[i]["ClassID"] + ");\" style=\"cursor:pointer;\">" + tb.Rows[i]["ClassName"] + "</a></li>";
		}
		html = html + "</ul><div style=\"clear:both;\"></div>";
	}
	$("#gearsClass").html(html);
	var gameId = $("#hidGameId").attr("value");
	var packageClass = $("#hidPackageClassID").attr("value");
	PowerLevel_Common.GetPackageslistByGearsClass(gameId, packageClass, currentGears, MakeHtml);
}
function SearchGears()
{
	var gameId = $("#hidGameId").attr("value");
	var key = $("#txtGears").attr("value");
	var packageClass = $("#hidPackageClassID").attr("value");
	if(key != null && key != undefined)
	{
		PowerLevel_Common.SearchGears(gameId, packageClass, key, MakeHtml);
	}
}
function LoadItemsClass(parentClass)
{
	var result = PowerLevel_Common.GetItemsClass(parentClass);
	if(result != null && result.value.Rows.length > 0)
	{
		var html = "<ul class=\"gearsclass\">";
		var tb = result.value;
		if(parentClass != 0)
		{
			html = html + "<li><a onclick=\"LoadItemsClass(0);SetColor(this);\" style=\"cursor:pointer;\">RootClass</a></li>";			
		}
		for(var i = 0; i < tb.Rows.length; i ++)
		{
			html = html + "<li><a onclick=\"LoadItemsClass(" + tb.Rows[i]["ItemClassID"] + ");SetColor(this);\" style=\"cursor:pointer;\">" + tb.Rows[i]["ItemClassName"] + "</a></li>";
		}
		html = html + "</ul><div style=\"clear:both;\"></div>";
	}
	$("#gearsClass").html(html);
	var gameId = $("#hidGameId").attr("value");
	var packageClass = $("#hidPackageClassID").attr("value");
	PowerLevel_Common.GetPackageslistByItemClass(gameId, packageClass, parentClass, MakeItemHtml);
}
function SearchItems()
{
	var gameId = $("#hidGameId").attr("value");
	var key = $("#txtGears").attr("value");
	var packageClass = $("#hidPackageClassID").attr("value");
	if(key != null && key != undefined)
	{
		PowerLevel_Common.SearchItems(gameId, packageClass, key, MakeItemHtml);
	}
}
function LoadJS(id, fileUrl ) 
{ 
    var scriptTag = document.getElementById(id); 
    var oHead = document.getElementsByTagName('HEAD').item(0); 
    var oScript= document.createElement("script"); 
    if (scriptTag)
	oHead.removeChild(scriptTag); 
    oScript.id = id; 
    oScript.type = "text/javascript"; 
    oScript.src=fileUrl ; 
    oHead.appendChild(oScript); 
}
var currentNode = null;
function SetColor(ref)
{
	var node = document.getElementById("gearsClass").childNodes[0];
	if(node.nodeType == 3)
	{
		node = document.getElementById("gearsClass").childNodes[1];
	}
	for(var i = 0; i < node.childNodes.length; i ++)
	{
		if(node.childNodes[i].nodeType == 1 && node.childNodes[i] != currentNode)
		{
			node.childNodes[i].childNodes[0].style.color = "#FF9900";
		}
	}
	ref.style.color = "#357EC1";
}
function ClearColor(ref)
{
	var node = document.getElementById("gearsClass").childNodes[0];
	if(node.nodeType == 3)
	{
		node = document.getElementById("gearsClass").childNodes[1];
	}
	for(var i = 0; i < node.childNodes.length; i ++)
	{
		if(node.childNodes[i].nodeType == 1 && node.childNodes[i] != currentNode)
		{
			node.childNodes[i].style.backgroundColor = "#660105";
		}
	}
}