function  trim(str){
return  str.replace(/(^\s*)|(\s*$)/g,"");
}
var  packList=new Array();
$(document).ready(function(){
initData();
loadingLevelPrice();
loadingSkill_PL('Skill PL','skill_powerlevelings','0');
}
);


function  changeMenuStyle(i){
$("#changeArea>li").each(function(index){
           if(i==index){
              $(this).children("a").addClass("current");
         }else{
               $(this).children("a").removeClass("current");
         }
});

}





function  loadingLevelPrice(){
compute_level_price();
$("#skillType").change(function(){
 compute_level_price();
});
$("#currentLevel").change(function(){
compute_select_level_price();
});
$("#desiredLevel").change(function(){
compute_select_level_price();
});

}

function compute_level_price(){
var showList=["Attack","Defence","Strength"];
var attachList=["Magic","Crafting","Runecrafting","Smithing","Prayer","Construction-Members Only"];
packList.length=0;
var priceFlag="$";
var skillType=$("#skillType").val();
var flag=false;
var atflag=false;
var appentcontent="";
for(var y=0;y<showList.length;y++){
   if(showList[y]==skillType){
       flag=true;
   }else{
    appentcontent+="<tr><td align=\"right\">"+showList[y]+" Level:</td><td align=\"left\">"+this.appendAddItem(showList[y],"")+"</td></tr>";
   }
}
appentcontent+="";
if(flag){
 $("#appendContent").html(appentcontent);
 $("#appendContent").show();
}else{
 $("#appendContent").hide();
}
for(var f=0;f<attachList.length;f++){
   if(attachList[f]==skillType){
   atflag=true;
   }
}
if(atflag){
 $("#rdoOne").attr("checked",false);
 $("#rdoTwo").attr("checked",true);
 $("#otherContent").show();
}else{
 $("#rdoOne").attr("checked",true);
 $("#rdoTwo").attr("checked",false);
 $("#otherContent").hide();
}
var currentLevel=$("#currentLevel").val();
var desiredLevel=$("#desiredLevel").val();
var searchUrl="../powerleveling/computePriceAndTime.jsp?";
var flag=true;
var validateFlag=true;
if(parseInt(currentLevel)>=parseInt(desiredLevel)){
$("#currentLevel").val(1);
validateFlag=false;
}
if(validateFlag){
if(skillType!=""){
searchUrl+="skillType="+skillType
flag=false;
}
if(currentLevel!=""){
   if(flag){
    searchUrl+="currentLevel="+currentLevel
    flag=false;
   }else{
     searchUrl+="&currentLevel="+currentLevel
   }
}
if(desiredLevel!=""){
  if(flag){
    searchUrl+="desiredLevel="+desiredLevel
    flag=false;
   }else{
     searchUrl+="&desiredLevel="+desiredLevel
   }
}
$.ajax({
		type: "POST",
		url: searchUrl,
		success: function(msg){	
		var returnResult=trim(msg);
		packList.length=0;
	      if(returnResult.indexOf("NullError")!=-1){
	        alert("Error");
	      }else if(returnResult.indexOf("ExceptionError")!=-1){
	      alert("Error");
	      }else{
	       var levelValue="";
           var neededHour=0;
           var priceValue=0;
           var timeValue=0;
	       var result=returnResult.split("-");
	       var  computePrice=result[0];
	       var  computeTime=result[1];
	       var  levelFreeGold=result[2];
	      add_powerlevelings("init_id_1","("+currentLevel+"-"+desiredLevel+")",computeTime,computePrice);
	      var checklist=$("input[id^='checkbox_']").each(function(){
              if($(this).attr("checked")){
	          var pid=$(this).attr("id");
	          var tid=pid.slice(pid.indexOf("checkbox_")+"checkbox_".length,pid.length);
	          var pname=$("#pack_"+tid).text();
	          var packTime=0;
	          var packPrice=$("#price_"+tid).text();
	          add_powerlevelings(tid,pname,packTime,packPrice);
	          }
	       });
	      for(var t=0;t<packList.length;t++){
            levelValue+=packList[t].packName+"/";
            priceValue+=parseFloat(packList[t].packPrice);
            timeValue+=parseFloat(packList[t].packTime);
          }
          var total_price=0;
      var h_price_result=$("#h_price_result").val();
       $("select[name^='level_']").each(function(){
         var cvalue=$(this).val(); 
         if(cvalue=="1-19"){
          total_price+=parseFloat(h_price_result);
         }else if(cvalue=="20-39"){
          total_price+=parseFloat(h_price_result)/2;
         }else{
          total_price+=0;
         }
       });
           $("#currentlevelResult").val(levelValue.slice(0,levelValue.lastIndexOf("/")));
           $("#priceResult").val((priceValue+total_price).toFixed(2));
           $("#h_price_result").val(result[0]);
           $("#skillTypeResult").val(skillType);
	       $("#neededResult").val(timeValue.toFixed(2));
	       if($("#otherContent").is(":visible")){
	       var goldNumber=(compute_level_freegold_number(skillType,currentLevel,desiredLevel)/100000).toFixed(5);
	       $("#requiredGoldNumber").text(goldNumber);
	       $("#blankGoldNumber").text(goldNumber);
	       $("#provideGoldNumber").text(goldNumber);
	       $("#provideGoldPrice").text((goldNumber*2).toFixed(5));
	       $("#provideGNumber").val(goldNumber);
	       }
	      }
		}
	});
}

}

function compute_select_level_price(){
packList.length=0;
var priceFlag="$";
var skillType=$("#skillType").val();
var currentLevel=$("#currentLevel").val();
var desiredLevel=$("#desiredLevel").val();
var searchUrl="../powerleveling/computePriceAndTime.jsp?";
var flag=true;
var validateFlag=true;
if(parseInt(currentLevel)>=parseInt(desiredLevel)){
$("#currentLevel").val(1);
validateFlag=false;
}
if(validateFlag){
if(skillType!=""){
searchUrl+="skillType="+skillType
flag=false;
}
if(currentLevel!=""){
   if(flag){
    searchUrl+="currentLevel="+currentLevel
    flag=false;
   }else{
     searchUrl+="&currentLevel="+currentLevel
   }
}
if(desiredLevel!=""){
  if(flag){
    searchUrl+="desiredLevel="+desiredLevel
    flag=false;
   }else{
     searchUrl+="&desiredLevel="+desiredLevel
   }
}
$.ajax({
		type: "POST",
		url: searchUrl,
		success: function(msg){	
		var returnResult=trim(msg);
		packList.length=0;
	      if(returnResult.indexOf("NullError")!=-1){
	        alert("Error");
	      }else if(returnResult.indexOf("ExceptionError")!=-1){
	      alert("Error");
	      }else{
	       var levelValue="";
           var neededHour=0;
           var priceValue=0;
           var timeValue=0;
	       var result=returnResult.split("-");
	       var  computePrice=result[0];
	       var  computeTime=result[1];
	       var  levelFreeGold=result[2];
	      add_powerlevelings("init_id_1","("+currentLevel+"-"+desiredLevel+")",computeTime,computePrice);
	      var checklist=$("input[id^='checkbox_']").each(function(){
              if($(this).attr("checked")){
	          var pid=$(this).attr("id");
	          var tid=pid.slice(pid.indexOf("checkbox_")+"checkbox_".length,pid.length);
	          var pname=$("#pack_"+tid).text();
	          var packTime=0;
	          var packPrice=$("#price_"+tid).text();
	          add_powerlevelings(tid,pname,packTime,packPrice);
	          }
	       });
	      for(var t=0;t<packList.length;t++){
            levelValue+=packList[t].packName+"/";
            priceValue+=parseFloat(packList[t].packPrice);
            timeValue+=parseFloat(packList[t].packTime);
          }
          var total_price=0;
      var h_price_result=$("#h_price_result").val();
       $("select[name^='level_']").each(function(){
         var cvalue=$(this).val(); 
         if(cvalue=="1-19"){
          total_price+=parseFloat(h_price_result);
         }else if(cvalue=="20-39"){
          total_price+=parseFloat(h_price_result)/2;
         }else{
          total_price+=0;
         }
       });
           $("#currentlevelResult").val(levelValue.slice(0,levelValue.lastIndexOf("/")));
           $("#priceResult").val((priceValue+total_price).toFixed(2));
           $("#h_price_result").val(result[0]);
           $("#skillTypeResult").val(skillType);
	       $("#neededResult").val(timeValue.toFixed(2));
	       if($("#otherContent").is(":visible")){
	       var goldNumber=(compute_level_freegold_number(skillType,currentLevel,desiredLevel)/100000).toFixed(5);
	       $("#requiredGoldNumber").text(goldNumber);
	       $("#blankGoldNumber").text(goldNumber);
	       $("#provideGoldNumber").text(goldNumber);
	       $("#provideGoldPrice").text((goldNumber*2).toFixed(5));
	       $("#provideGNumber").val(goldNumber);
	       }
	      }
		}
	});
}

}

function loadingSkill_PL(itemName,obj,index){
$("#loadingContentEffect").show("slow");
$("#shillPlContent").hide("slow");
$("#"+obj).hide("slow");
$("#Kill_Gold_Item").hide("slow");
$("#Kill_Dragon_Item").hide("slow");
$("#Combat_Skill_Item").hide("slow");
changeMenuStyle(index);
var searchUrl="../powerleveling/loadingSkillPL.jsp?itemName="+itemName;
$.ajax({
		type: "POST",
		url: searchUrl,
		success: function(msg){	
		  $("#shillPlContent").show("slow");
		  $("#"+obj).html(msg);
          $("#loadingContentEffect").hide("slow");
          $("#"+obj).show("slow");
		}
	});
}
function loadingKill_God_Package(itemName,obj,index){
changeMenuStyle(index);
$("#loadingContentEffect").show("slow");
$("#shillPlContent").hide("slow");
$("#skill_powerlevelings").hide("slow");
$("#"+obj).hide("slow");
$("#Kill_Dragon_Item").hide("slow");
$("#Combat_Skill_Item").hide("slow");
var searchUrl="../powerleveling/loadingKillGoldPackage.jsp?itemName="+itemName;
$.ajax({
		type: "POST",
		url: searchUrl,
		success: function(msg){	
		 $("#"+obj).html(msg);
		 $("#loadingContentEffect").hide("slow");
		  $("#"+obj).show("slow");
		}
	});
}

function  loadingKill_Dragon_Package(itemName,obj,index){
changeMenuStyle(index);
$("#loadingContentEffect").show("slow");
$("#shillPlContent").hide("slow");
$("#skill_powerlevelings").hide("slow");
$("#"+obj).hide("slow");
$("#Kill_Gold_Item").hide("slow");
$("#Combat_Skill_Item").hide("slow");
var searchUrl="../powerleveling/loadingBaseItemPack.jsp?itemName="+itemName;
$.ajax({
		type: "POST",
		url: searchUrl,
		success: function(msg){	
		  $("#"+obj).html(msg);
		  $("#loadingContentEffect").hide("slow");
		  $("#"+obj).show("slow");
		}
	});
}

function  loadingCombat_Gold_Package(itemName,obj,index){
changeMenuStyle(index);
$("#loadingContentEffect").show("slow");
$("#shillPlContent").hide("slow");
$("#skill_powerlevelings").hide("slow");
$("#"+obj).hide("slow");
$("#Kill_Gold_Item").hide("slow");
$("#Kill_Dragon_Item").hide("slow");
var searchUrl="../powerleveling/loadingBaseItemPack.jsp?itemName="+itemName;
$.ajax({
		type: "POST",
		url: searchUrl,
		success: function(msg){	
		  $("#"+obj).html(msg);
		  $("#loadingContentEffect").hide("slow");
		  $("#"+obj).show("slow");
		}
	});
}
function computeGoldPrice(packId,value){
var realPackPrice=$("#real_price_"+packId).val();
var packName=$("#pack_"+packId).text();
var packTime=$("#real_time_"+packId).val();
if(packName=="Cool Gold"){
packTime=parseFloat(packTime)*parseInt(value);
$("#computenumber_"+packId).text(value);
$("#compute_"+packId).text(packTime);
}else if(packName=="Gold Farming"){
packTime=parseFloat(packTime)*parseInt(value);
$("#computenumber_"+packId).text(value);
$("#compute_"+packId).text(packTime);
}
var computePrice=parseFloat(realPackPrice)*parseInt(value);
$("#checkbox_"+packId).val(packName+"-"+computePrice.toFixed(2)+"-"+packTime);
$("#price_"+packId).html(computePrice.toFixed(2));
if($("#checkbox_"+packId).attr("checked")){
compute_level_price();
}
}



function packFormSubmit(itemId,packId,gameName,itemType){
$("#itemId").val(itemId);
$("#packId").val(packId);
$("#gameName").val(gameName);
$("#itemType").val(itemType);
document.packForm.submit();
}


function  showDesiredLevel(skillType){
var desiredLevel="";
var startLevelValue=10;
var endLevelValue=99;
if(skillType=="Ranged"){
startLevelValue=40;
}else if(skillType=="Runecrafting"){
startLevelValue=40;
}else if(skillType=="Fishing"){
startLevelValue=40;
}else if(skillType=="Thieving"){
startLevelValue=40;
}else if(skillType=="Mining"){
startLevelValue=40;
}else if(skillType=="Crafting"){
startLevelValue=40;
}else if(skillType=="Firemaking"){
startLevelValue=40;
}else if(skillType=="Prayer"){
startLevelValue=40;
}else if(skillType=="Hunter"){
startLevelValue=40;
}else if(skillType=="Smithing"){
startLevelValue=40;
}else if(skillType=="Fletching-Members Only"){
startLevelValue=40;
}else if(skillType=="Agility-Members Only"){
startLevelValue=40;
}else if(skillType=="Slayer-Members Only"){
startLevelValue=40;
}
for(var i=startLevelValue;i<=endLevelValue;i++){
if(i==startLevelValue){
desiredLevel+="<option value=\""+i+"\" selected>"+i+"</option>";
}else{
desiredLevel+="<option value=\""+i+"\">"+i+"</option>";
}
}
$("#desiredLevel").html(desiredLevel);
}


function pack(packId,packName,packTime,packPrice){
this.packId=packId;
this.packName=packName;
this.packTime=packTime;
this.packPrice=packPrice;
} 

function  add_powerlevelings(packId,packName,packTime,packPrice){
var  p=new pack(packId,packName,packTime,packPrice);
packList.push(p);
}

function compute_gold_price(packId){
var packName=$("#pack_"+packId).text();
var packPrice=$("#price_"+packId).text();
var packTime=0;
var skillTypeResult=$("#skillTypeResult").val();
var currentlevelResult=$("#currentlevelResult").val();
var neededResult=$("#neededResult").val();
var priceResult=$("#priceResult").val();

var levelValue="";
var neededHour=0;
var priceValue=0;
var timeValue=0;
var checkFlag=$("#checkbox_"+packId).attr("checked");
var total_price=0;
var h_price_result=$("#h_price_result").val();
$("select[name^='level_']").each(function(){
        var cvalue=$(this).val(); 
        if(cvalue=="1-19"){
          total_price+=parseFloat(h_price_result);
        }else if(cvalue=="20-39"){
          total_price+=parseFloat(h_price_result)/2;
        }else{
          total_price+=0;
        }
});

if(currentlevelResult!=""&&skillTypeResult!=""&&neededResult!=""&&priceResult!=""){
if(checkFlag){
          add_powerlevelings(packId,packName,packTime,packPrice);
}else{
        for(var t=0;t<packList.length;t++){
           if(packList[t].packId==packId){
               packList.splice(t,1);
           }
         }
}
for(var t=0;t<packList.length;t++){
    levelValue+=packList[t].packName+"/";
    priceValue+=parseFloat(packList[t].packPrice);
    timeValue+=parseFloat(packList[t].packTime);
}

$("#currentlevelResult").val(levelValue.slice(0,levelValue.lastIndexOf("/")));
$("#priceResult").val((priceValue+total_price).toFixed(2));
$("#neededResult").val(timeValue.toFixed(2));
}else{
 compute_level_price();
}
}
function levelFormSubmit(){
var skillTypeResult=$("#skillTypeResult").val();
var currentlevelResult=$("#currentlevelResult").val();
var neededResult=$("#neededResult").val();
var priceResult=$("#priceResult").val();
var skillType=$("#skillType").val();
var showList=["Attack","Defence","Strength"];
var flag=true;
var skillTypeList=" ";
var totalSkillType="";
var skillTypeValue="";
for(var t=0;t<showList.length;t++){
    if(showList[t]!=skillType){
       totalSkillType+=showList[t];
       skillTypeList+=showList[t]+" Level Or ";
       var typeValue=$("#level_"+showList[t]).val();
       skillTypeValue+=typeValue;
    }
}
if(totalSkillType!=skillTypeValue){
flag=true;
}else{
flag=false;
}

if(flag){
 if(currentlevelResult!=""&&skillTypeResult!=""&&neededResult!=""&&priceResult!=""){
  document.skillPriceForm.submit();
  }
}else{
skillTypeList=skillTypeList.slice(0,skillTypeList.lastIndexOf("Or "));
alert("Please Select "+skillTypeList);
}

}

function initData(){
$("#skillTypeResult").val("");
$("#currentlevelResult").val("");
$("#neededResult").val("");
$("#priceResult").val("");
$("#h_price_result").val("");
}

function  appendAddItem(itemName,value){
var itemList="Please Select|"+itemName+"+++"+itemName+"|1-19+++"+itemName+"|20-39+++"+itemName+"|40-59+++"+itemName+"|60+";
var itemcontent=itemList.split("+++");
var itcontent="<select name=\"level_"+itemName+"\" id=\"level_"+itemName+"\" onchange=\"change_price(this.value)\">";
for(var t=0;t<itemcontent.length;t++){
var content=itemcontent[t].split("|");
if(value==content[0]){
  itcontent+="<option value=\""+content[1]+"\" selected>"+content[0]+" "+content[1]+"</option>";
}else{
  itcontent+="<option value=\""+content[1]+"\">"+content[0]+" "+content[1]+"</option>";
}
}
itcontent+="</select>";
return itcontent;
}
function change_price(value){
var total_price=0;
var h_price_result=$("#h_price_result").val();
$("select[name^='level_']").each(function(){
        var cvalue=$(this).val(); 
        if(cvalue=="1-19"){
          total_price+=parseFloat(h_price_result);
        }else if(cvalue=="20-39"){
          total_price+=parseFloat(h_price_result)/2;
        }else{
          total_price+=0;
        }
});
 var checklist=$("input[id^='checkbox_']").each(function(){
            if($(this).attr("checked")){
	          var pid=$(this).attr("id");
	          var tid=pid.slice(pid.indexOf("checkbox_")+"checkbox_".length,pid.length);
	          var packPrice=$("#price_"+tid).text();
	          total_price+=parseFloat(packPrice);
	         }
});
h_price_result=(parseFloat(h_price_result)+total_price).toFixed(2);
$("#priceResult").val(h_price_result);
}
