function trim(str){
return  str.replace(/(^\s*)|(\s*$)/g,"");
}
var  priceList=new Array();
function  levelPack(levelName,levelPrice){
this.levelName=levelName;
this.levelPrice=levelPrice;
}
$(document).ready(function(){
loadPowerlevelingsPriceAndTime();
$("#startLevel").change(function(){
loadPowerlevelingsPriceAndTime();
});
$("#endLevel").change(function(){
loadPowerlevelingsPriceAndTime();
});
$("#submitButton").click(function(){
clickAddToCart();
});

});

function loadPowerlevelingsPriceAndTime(){
var gameName=trim($("#gameName").val());
var itemName=trim($("#itemName").val());
var moneyType=trim($("#moneyType").val());
var websiteName=trim($("#websiteName").val());
var startLevel=trim($("#startLevel").val());
var endLevel=trim($("#endLevel").val());
if(startLevel==""){
startLevel=1;
$("#startLevel").val(startLevel);
}else if(endLevel==""){
endLevel=80;
$("#endLevel").val(endLevel);
}else if(parseInt(startLevel)>=parseInt(endLevel)){
alert("Your current level can't more than your desired level!");
startLevel=1;
$("#startLevel").val(startLevel);
}
var loadingUrl="../wowpowerlevelings/loadPriceMul.jsp?gameName="+gameName+"&itemName="+itemName+"&moneyType="+moneyType+"&websiteName="+websiteName;
$.ajax({
type:"post",
url:loadingUrl,
success:function(msg){
           var returnResult=trim(msg);
           var result=returnResult.split("+++");
           var priceAndtimeResult=trim(countLevelPrice(parseInt(startLevel),parseInt(endLevel),result[0],result[1],result[2],result[3]));
           var appentPrice=document.getElementsByName("appendPrice");
           var computePrice=0;
           var j=1;
           var tempContent="<br/>";
           var tempPrice="0";
           var itemPriceId="";
		   for(var i=0;i<appentPrice.length;i++){
		              var tempId=appentPrice[i].id.replace("item","appendContent"); 
				      if(appentPrice[i].checked){
				           computePrice+=parseInt(appentPrice[i].value);
				           tempPrice+="-"+appentPrice[i].value;
				           tempContent+="("+j+"):"+$("#"+tempId).text()+"-MTFLAG"+appentPrice[i].value+"<br/>";
				           itemPriceId+=tempId;
				           j++;  
				       }
				}
		  var  ptResult=priceAndtimeResult.split(",");
		  var  minTime=parseInt(parseFloat(ptResult[1])/24);
		  var  maxTime=minTime+1;
		  var  resultPrice=(parseFloat(ptResult[0])+parseInt(computePrice)).toFixed();
		   computeGoldCount("freeGold",startLevel,endLevel);
		   $("#wowPrice").val(formatprice(resultPrice));
		   $("#wowTime").val(minTime+"-"+maxTime);
           $("#priceType").val(result[4]);
           $("#itemId").val(result[5]);
           $("#itemAppendContent").val(tempContent); 
           $("#itemPriceValue").val(tempPrice);
           $("#itemPriceId").val(itemPriceId);
}
});
}

var  finallyGold="0";
function   computeGoldCount(freeGold,minLevel,maxLevel){
var  maxLevelGold="0";
var  minLevelGold="0";
if(minLevel==""&&maxLevel==""){
alert("Please Select Current Level and Desired Level!");
}else if(minLevel==""&&maxLevel!=""){
 alert("Please Select Current Level!");
}else if(minLevel!=""&&maxLevel==""){
alert("Please Select  Desired Level!");
}else  if(maxLevel>69){
maxLevelGold=1150+(parseInt(maxLevel)-70)*200;
if(minLevel>69){
minLevelGold=parseInt(maxLevelGold)-1150-(parseInt(minLevel)-70)*200;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>59){
minLevelGold=parseInt(maxLevelGold)-150-(parseInt(minLevel)-60)*100;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>49){
minLevelGold=parseInt(maxLevelGold)-50-(parseInt(minLevel)-50)*10;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>39){
minLevelGold=parseInt(maxLevelGold)-(parseInt(minLevel)-40)*5;
$("#"+freeGold).val(minLevelGold);
}else{
$("#"+freeGold).val(maxLevelGold);
}
}else  if(maxLevel>59&&maxLevel<=69){
maxLevelGold=150+(parseInt(maxLevel)-60)*100;
if(minLevel>69){
minLevelGold=parseInt(maxLevelGold)-1150-(parseInt(minLevel)-70)*200;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>59){
minLevelGold=parseInt(maxLevelGold)-150-(parseInt(minLevel)-60)*100;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>49){
minLevelGold=parseInt(maxLevelGold)-50-(parseInt(minLevel)-50)*10;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>39){
minLevelGold=parseInt(maxLevelGold)-(parseInt(minLevel)-40)*5;
$("#"+freeGold).val(minLevelGold);
}else{
$("#"+freeGold).val(maxLevelGold);
}
}else  if(maxLevel>49&&maxLevel<=59){
maxLevelGold=50+(parseInt(maxLevel)-50)*10;
if(minLevel>69){
minLevelGold=parseInt(maxLevelGold)-1150-(parseInt(minLevel)-70)*200;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>59){
minLevelGold=parseInt(maxLevelGold)-150-(parseInt(minLevel)-60)*100;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>49){
minLevelGold=parseInt(maxLevelGold)-50-(parseInt(minLevel)-50)*10;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>39){
minLevelGold=parseInt(maxLevelGold)-(parseInt(minLevel)-40)*5;
$("#"+freeGold).val(minLevelGold);
}else{
$("#"+freeGold).val(maxLevelGold);
}
}else  if(maxLevel>39&&maxLevel<=49){
maxLevelGold=(parseInt(maxLevel)-40)*5;
if(minLevel>69){
minLevelGold=parseInt(maxLevelGold)-1150-(parseInt(minLevel)-70)*200;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>59){
minLevelGold=parseInt(maxLevelGold)-150-(parseInt(minLevel)-60)*100;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>49){
minLevelGold=parseInt(maxLevelGold)-50-(parseInt(minLevel)-50)*10;
$("#"+freeGold).val(minLevelGold);
}else if(minLevel>39){
minLevelGold=parseInt(maxLevelGold)-(parseInt(minLevel)-40)*5;
$("#"+freeGold).val(minLevelGold);
}else{
$("#"+freeGold).val(maxLevelGold);
}
}else{
$("#"+freeGold).val(0);
}
}

function formatprice(price){
		return parseInt(price)+".99";
	}
	
function computeAppendContent(itemFlag){
var gameName=trim($("#gameName").val());
var itemName=trim($("#itemName").val());
var moneyType=trim($("#moneyType").val());
var websiteName=trim($("#websiteName").val());
var startLevel=trim($("#startLevel").val());
var endLevel=trim($("#endLevel").val());
if(startLevel==""){
startLevel=1;
$("#startLevel").val(startLevel);
}else if(endLevel==""){
endLevel=80;
$("#endLevel").val(endLevel);
}else if(parseInt(startLevel)>=parseInt(endLevel)){
alert("Your current level can't more than your desired level!");
startLevel=1;
$("#startLevel").val(startLevel);
}
var loadingUrl="../wowpowerlevelings/loadPriceMul.jsp?gameName="+gameName+"&itemName="+itemName+"&moneyType="+moneyType+"&websiteName="+websiteName;
$.ajax({
type:"post",
url:loadingUrl,
success:function(msg){
           var returnResult=trim(msg);
           var result=returnResult.split("+++");
           var priceAndtimeResult=trim(countLevelPrice(parseInt(startLevel),parseInt(endLevel),result[0],result[1],result[2],result[3]));
           var appentPrice=document.getElementsByName("appendPrice");
           var computePrice=0;
           var j=1;
           var tempContent="<br/>";
		   var tempPrice="0";
		   var itemPriceId="";
		   for(var i=0;i<appentPrice.length;i++){
		              var tempId=appentPrice[i].id.replace("item","appendContent"); 
				      if(appentPrice[i].checked){
				           computePrice+=parseInt(appentPrice[i].value);
				           tempPrice+="-"+appentPrice[i].value;
				           tempContent+="("+j+"):"+$("#"+tempId).text()+"-MTFLAG"+appentPrice[i].value+"<br/>";
				           itemPriceId+=tempId;
				           j++;  
				       }
				}
		  var  ptResult=priceAndtimeResult.split(",");
		  var  minTime=parseInt(parseFloat(ptResult[1])/24);
		  var  maxTime=minTime+1;
		  var  resultPrice=(parseFloat(ptResult[0])+parseInt(computePrice)).toFixed();
		   computeGoldCount("freeGold",startLevel,endLevel);
		   $("#wowPrice").val(formatprice(resultPrice));
		   $("#wowTime").val(minTime+"-"+maxTime);
           $("#priceType").val(result[4]);
           $("#itemId").val(result[5]);
           $("#itemAppendContent").val(tempContent); 
           $("#itemPriceValue").val(tempPrice);
           $("#itemPriceId").val(itemPriceId);
}
});
}





function  clickAddToCart(){
$("#butCartList").fadeOut("slow");
var gameName=$("#gameName").val();
var itemName=$("#itemName").val();
var moneyType=$("#moneyType").val();
var websiteName=$("#websiteName").val();
var startLevel=$("#startLevel").val();
var endLevel=$("#endLevel").val();
var appentPrice=document.getElementsByName("appendPrice");
var computePrice=0;
var j=1;
var tempContent="<br/>";
var tempPrice="0";
var itemPriceId="";
 for(var i=0;i<appentPrice.length;i++){
		              var tempId=appentPrice[i].id.replace("item","appendContent"); 
				      if(appentPrice[i].checked){
				           computePrice+=parseInt(appentPrice[i].value);
				           tempPrice+="-"+appentPrice[i].value;
				           tempContent+="("+j+"):"+$("#"+tempId).text()+"-MTFLAG"+appentPrice[i].value+"<br/>";
				           itemPriceId+=tempId;
				           j++;  
}
}
$("#itemAppendContent").val(tempContent); 
$("#itemPriceValue").val(tempPrice);
$("#itemPriceId").val(itemPriceId);
var itemAppendContent=$("#itemAppendContent").val();
var itemPriceValue=$("#itemPriceValue").val();
var  loadUrl="../Level_Powerlevelings/addToCart.jsp";
$.ajax({
type:"post",
url:loadUrl,
data:{gameName:gameName,startLevel:startLevel,endLevel:endLevel,itemName:itemName,moneyType:moneyType,websiteName:websiteName,itemAppendContent:itemAppendContent,itemPriceValue:itemPriceValue,itemPriceId:itemPriceId},
success:function(msg){
            var returnResult=trim(msg);
	        $("#butCartList").html(returnResult);
	        $("#shopping_cart").fadeIn("slow");
	       // $("#butCartList").fadeIn("slow");
	        $("#butCartList").toggle("slow");
}
});
}


function  addingNumber(equipEquipId,currentNumber){
if(parseInt(currentNumber)>=90){
alert("This item can no longer increase the number of!");
}else{
$("#butCartList").fadeOut("slow");
var  loadUrl="../Level_Powerlevelings/addNumber.jsp?equipEquipId="+equipEquipId+"&currentNumber="+currentNumber;
$.ajax({
type:"post",
url:loadUrl,
success:function(msg){
	        $("#butCartList").html(trim(msg));
	        $("#shopping_cart").css("display","block");
	        $("#butCartList").toggle("slow");
}
});
}
}
function  reducingNumber(equipEquipId,currentNumber){
if(parseInt(currentNumber)==1||parseInt(currentNumber)<=0){
alert("The number of items can not be reduced!");
}else{
$("#butCartList").fadeOut("slow");
var  loadUrl="../Level_Powerlevelings/reduceNumber.jsp?equipEquipId="+equipEquipId+"&currentNumber="+currentNumber;
$.ajax({
type:"post",
url:loadUrl,
success:function(msg){
	        $("#butCartList").html(trim(msg));
	        $("#shopping_cart").css("display","block");
	        $("#butCartList").toggle("slow");
}
});
}
}
function hiddenItemList(){
if($("#butCartList").css("display")=="none"){
   $("#butCartList").show(1000);
   $("#expandImg").attr("src","../images/item/icon_-.gif");
}else if($("#butCartList").css("display")=="block"){
   $("#butCartList").hide(1000);
   $("#expandImg").attr("src","../images/item/icon_+.gif");
}
}
function removeCart(removeEquipIndex){
$("#butCartList").fadeOut("slow");
var  loadUrl="../Level_Powerlevelings/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 cilckNowBuy(){
document.equipListForm.submit();
}
