function trim(str){
return  str.replace(/(^\s*)|(\s*$)/g,"");
}
function itemTable(){
var objTable=document.getElementById("tokenInfoBox").getElementsByTagName("table");
for(var i=0;i<objTable.length;i++){
objTable[i].onmouseover=function(){
this.style.background="#1b1b1b";
}
objTable[i].onmouseout=function(){
this.style.background="";
}
}
}

function updateEquipRarity(value){
$("#selectBar").html(value);
}

$(document).ready(function(){
init();
itemTable();
loadingItemEquipPack();
$("#searchItemEquip").click(function(){
searchingItemEquipPack();
});
$("#ItemLevel").click(function(){
sortEquip("ItemLevel");
});
$("#ItemPrice").click(function(){
sortEquip("ItemPrice");
});
});

var showFlag=false;
function init(){
$(".select_Three a").click(function(){ 
this.blur();
});
$(".select_Three a").focus(function(){ 
 this.blur();
});

$("#selectSourceArrow").click(function(){ 
          $("#selectSourceText").toggle("slow");
          if($("#selectSourceText").css("display")=="block"){
                 showFlag=true;
          }else{
                showFlag=false;
          }
          if($("#selectClassText").css("display")=="block"){
             $("#selectClassText").toggle("slow");
          }
          if($("#selectSlotText").css("display")=="block"){
             $("#selectSlotText").toggle("slow");
          }
          if($("#selectLevelText").css("display")=="block"){
             $("#selectLevelText").toggle("slow");
          }
      $("#selectSourceList li").each(function(){
           $(this).click(function(){
             $(this).addClass("current").siblings().removeClass("current");
             $("#selectSourceBar").html($(this).text());
             $("#packName").val($(this).text());
             $("#selectSourceText").hide();
            });
      });
});
$("#selectClassArrow").click(function(){ 
          $("#selectClassText").toggle("slow");
          if($("#selectClassText").css("display")=="block"){
           showFlag=true;
          }else{
           showFlag=false;
          }
          if($("#selectSourceText").css("display")=="block"){
             $("#selectSourceText").toggle("slow");
          }
          if($("#selectSlotText").css("display")=="block"){
             $("#selectSlotText").toggle("slow");
          }
          if($("#selectLevelText").css("display")=="block"){
             $("#selectLevelText").toggle("slow");
          }
       $("#selectClassList li").each(function(){
          $(this).click(function(){
             $(this).addClass("current").siblings().removeClass("current");
             $("#selectClassBar").html($(this).text());
             $("#itemClass").val($(this).text());
             $("#selectClassText").hide();
          });
       });
});
$("#selectSlotArrow").click(function(){ 
          $("#selectSlotText").toggle("slow");
          if($("#selectSlotText").css("display")=="block"){
            showFlag=true;
           }else{
            showFlag=false;
           }
          if($("#selectSourceText").css("display")=="block"){
             $("#selectSourceText").toggle("slow");
          }
          if($("#selectClassText").css("display")=="block"){
             $("#selectClassText").toggle("slow");
          }
           if($("#selectLevelText").css("display")=="block"){
             $("#selectLevelText").toggle("slow");
          }
          $("#selectSlotList li").each(function(){
          $(this).click(function(){
             $(this).addClass("current").siblings().removeClass("current");
             $("#selectSlotBar").html($(this).text());
             $("#itemSlot").val($(this).text());
             $("#selectSlotText").hide();
          });
       });
});

$("#selectLevelArrow").click(function(){ 
          $("#selectLevelText").toggle("slow");
          if($("#selectLevelText").css("display")=="block"){
                 showFlag=true;
          }else{
                showFlag=false;
          }
          if($("#selectSourceText").css("display")=="block"){
             $("#selectSourceText").toggle("slow");
          }
          if($("#selectClassText").css("display")=="block"){
             $("#selectClassText").toggle("slow");
          }
          if($("#selectSlotText").css("display")=="block"){
             $("#selectSlotText").toggle("slow");
          }
      $("#selectLevelList li").each(function(){
           $(this).click(function(){
             $(this).addClass("current").siblings().removeClass("current");
             $("#selectLevelBar").html($(this).text());
             $("#packName").val($(this).text());
             $("#selectLevelText").hide();
            });
      });
});
}

$(document).click(function(){
     if(showFlag){
          if($("#selectSourceText").css("display")=="block"){
                 $("#selectSourceText").hide();
          }
          if($("#selectClassText").css("display")=="block"){
                 $("#selectClassText").hide();
          }
         if($("#selectSlotText").css("display")=="block"){
                 $("#selectSlotText").hide();
          }
          if($("#selectLevelText").css("display")=="block"){
                 $("#selectLevelText").hide();
          }
     }
});

function  loadingItemEquipPack(){
$("#tokenInfoBox").hide();
$("#loadeffect").show("slow");
var  searchUrl="Token_View/loadingToken.jsp";
$.ajax({
		type: "POST",
		url: searchUrl,
		success: function(msg){	
	      var equipContent=msg.split("+");
	      $("#btn_First_page_content").html(equipContent[1]);
	      $("#btn_Last_page_content").html(equipContent[1]);
	      $("#loadeffect").hide("slow");
	      $("#tokenInfoBox").html(equipContent[0]);
	      $("#tokenInfoBox").fadeIn("slow");
		}
	});
}


function  searchingItemEquipPack(){
$("#tokenInfoBox").hide();
$("#loadeffect").show("slow");
var  currentPage="1";
var  loadURL="Token_View/loadingToken.jsp?currentPage="+currentPage;
var  equipPackageName=trim($("#selectSourceBar").text());
if(equipPackageName!="All"){
loadURL+="&equipPackageName="+equipPackageName;
}
var  equipEquipClass=trim($("#selectClassBar").text());
if(equipEquipClass!="All"){
loadURL+="&equipEquipClass="+equipEquipClass;
}
var  equipEquipSlot=trim($("#selectSlotBar").text());
if(equipEquipSlot!="All"){
loadURL+="&equipEquipSlot="+equipEquipSlot;
}

var  equipName=trim($("#equipEquipName").val());
if(equipName!=""){
loadURL+="&equipName="+equipName;
}
var  itemLevel=trim($("#selectLevelBar").text());
if(itemLevel!="All"){
loadURL+="&itemLevel="+itemLevel;
}
$.ajax({
		type: "POST",
		url: loadURL,
		success: function(msg){
	      var equipContent=msg.split("+");
	      $("#btn_First_page_content").html(equipContent[1]);
	      $("#btn_Last_page_content").html(equipContent[1]);
	      $("#loadeffect").hide("slow");
	      $("#tokenInfoBox").html(equipContent[0]);
	      $("#tokenInfoBox").fadeIn("slow");
		}
	});
}

function changePage(page){
$("#tokenInfoBox").hide();
$("#loadeffect").show("slow");
var  currentPage=page;
var  loadURL="Token_View/loadingToken.jsp?currentPage="+currentPage;
var  equipPackageName=trim($("#selectSourceBar").text());
if(equipPackageName!="All"){
loadURL+="&equipPackageName="+equipPackageName;
}
var  equipEquipClass=trim($("#selectClassBar").text());
if(equipEquipClass!="All"){
loadURL+="&equipEquipClass="+equipEquipClass;
}
var  equipEquipSlot=trim($("#selectSlotBar").text());
if(equipEquipSlot!="All"){
loadURL+="&equipEquipSlot="+equipEquipSlot;
}
var  equipName=trim($("#equipEquipName").val());
if(equipName!=""){
loadURL+="&equipName="+equipName;
}
var  itemLevel=trim($("#selectLevelBar").text());
if(itemLevel!="All"){
loadURL+="&itemLevel="+itemLevel;
}
var  orderBy=trim($("#orderBy").val());
if(orderBy!=""&&orderBy!=null){
loadURL+="&orderBy="+orderBy;
}
$.ajax({
		type: "POST",
		url: loadURL,
		success: function(msg){	
	      var equipContent=msg.split("+");
	      $("#btn_First_page_content").html(equipContent[1]);
	      $("#btn_Last_page_content").html(equipContent[1]);
	      $("#loadeffect").hide("slow");
	      $("#tokenInfoBox").html(equipContent[0]);
	      $("#tokenInfoBox").fadeIn("slow");
		}
	});

}
function  sortEquip(orderBy){
$("#tokenInfoBox").hide();
$("#loadeffect").show("slow");
var  currentPage="1";
var  loadURL="Token_View/loadingToken.jsp?currentPage="+currentPage;
var  equipPackageName=trim($("#selectSourceBar").text());
if(equipPackageName!="All"){
loadURL+="&equipPackageName="+equipPackageName;
}
var  equipEquipClass=trim($("#selectClassBar").text());
if(equipEquipClass!="All"){
loadURL+="&equipEquipClass="+equipEquipClass;
}
var  equipEquipSlot=trim($("#selectSlotBar").text());
if(equipEquipSlot!="All"){
loadURL+="&equipEquipSlot="+equipEquipSlot;
}
var  equipName=trim($("#equipEquipName").val());
if(equipName!=""){
loadURL+="&equipName="+equipName;
}
var  itemLevel=trim($("#selectLevelBar").text());
if(itemLevel!="All"){
loadURL+="&itemLevel="+itemLevel;
}
if(orderBy!=""&&orderBy!=null){
if($("#"+orderBy).attr("class")=="current"){
$("#"+orderBy).attr("class","");
orderBy=orderBy+" desc";
}else if($("#"+orderBy).attr("class")==""){
$("#"+orderBy).attr("class","current")
orderBy=orderBy+" asc";
}
loadURL+="&orderBy="+orderBy;
$("#orderBy").val(orderBy);
}
$.ajax({
		type: "POST",
		url: loadURL,
		success: function(msg){	
	      var equipContent=msg.split("+");
	      $("#btn_First_page_content").html(equipContent[1]);
	      $("#btn_Last_page_content").html(equipContent[1]);
	      $("#loadeffect").hide("slow");
	      $("#tokenInfoBox").html(equipContent[0]);
	      $("#tokenInfoBox").fadeIn("slow");
		}
	});
}


function  addToCart(equipId){
$("#shopping_cart").css("display","block");
var  loadCartUrl="Token_View/addToCart.jsp?equipEquipId="+equipId;
$("#butCartList").fadeOut("slow");
$.ajax({
		type: "POST",
		url: loadCartUrl,
		success: function(msg){	
		   if(msg.indexOf("This item already exists")!=-1){
	          alert(msg);
	        }else{
	        $("#butCartList").html(msg);
	        }
	        $("#butCartList").toggle("slow");
	       
		}
	});
}

function reducingNumber(equipId,number){
if(parseInt(number)==1||parseInt(number)<=0){
alert("The number of items can not be reduced!");
}else{
$("#butCartList").fadeOut("slow");
var  loadreduceUrl="Token_View/reduceNumber.jsp?equipEquipId="+equipId+"&currentNumber="+number;
$.ajax({
		type: "POST",
		url: loadreduceUrl,
		success: function(msg){	
	        $("#butCartList").html(msg);
	        $("#butCartList").toggle("slow");
		}
	});
}
}
function addingNumber(equipId,number){
if(parseInt(number)>=90){
alert("This item can no longer increase the number of!");
}else{
$("#butCartList").fadeOut("slow");
var  loadreduceUrl="Token_View/addNumber.jsp?equipEquipId="+equipId+"&currentNumber="+number;
$.ajax({
		type: "POST",
		url: loadreduceUrl,
		success: function(msg){	
	        $("#butCartList").html(msg);
	        $("#butCartList").toggle("slow");
		}
	});
}
}

function removeCart(removeEquipIndex){
$("#butCartList").fadeOut("slow");
var  loadUrl="Token_View/removeCart.jsp?removeEquipIndex="+removeEquipIndex;
$.ajax({
type:"post",
url:loadUrl,
success:function(msg){
	      if(msg.indexOf("completely empty!")!=-1){
	          alert("Shopping cart has been completely empty!");
	          $("#shopping_cart").css("display","none");
	        }else{
	           $("#butCartList").html(msg);
	           $("#butCartList").toggle("slow");
	        }
}
});
}



function hiddenItemList(){
if($("#butCartList").css("display")=="none"){
   $("#butCartList").show(1000);
   $("#expandImg").attr("src","images/index/ico_hidden.gif");
}else if($("#butCartList").css("display")=="block"){
   $("#butCartList").hide(1000);
   $("#expandImg").attr("src","images/index/ico_add.gif");
}
}


function cilckNowBuy(){
document.equipListForm.submit();
}
