﻿$(function(){
    InitLevel($("#levelServer option:selected").val());
    Init();
    $(".choose_box table tr:gt(0) td div").hide();
    $(".choose a").click(function(){
        var info = $(this).attr("id");
        GetData(info.split('-')[0],info.split('-')[1]);
    });
    
    $("#game").change(function(){
        var gameid=$(this).children("option:selected").val();
        if(gameid != "0")
        {
            var server = _Default.GetGameServer(gameid).value;
            if(server.Rows.length>0)
            {
                $("#server option").remove();
                for(var i=0; i<server.Rows.length; i++)
                {
                    var option=$("<option value='"+server.Rows[i].serverid+"'>"+server.Rows[i].servername+"</option>");  
                    $("#server").append(option);   
                }
            }
        }
    });
     
    $("#server").change(function(){
        var gameid=$("#game option:selected").val();
        var serverid=$("#server option:selected").val();
       
        if(gameid!="0" && serverid!="0")
        {
            var pros=_Default.GetProducts(gameid,serverid).value;
            if(pros.Rows.length>0)
            {
                $("#product option").remove();
                 for(var i=0; i<pros.Rows.length; i++)
                {
                    var option=$("<option value='"+pros.Rows[i].webgameproductserverid+"'>"+pros.Rows[i].gameproductname+"("+parseFloat(pros.Rows[i].saleprice).toFixed(2)+")"+"</option>");
                    $("#product").append(option); 
                }
            }
        }
       
    });
   $("#indexbuygold").click(function(){
        var gameid=$("#game option:selected").val();
        var serverid=$("#server option:selected").val();
        var product=$("#product option:selected");
        if(product.val()!="0")
        {
            var result = _Default.addwebgamecart(product.val(), "1", ip, gameid).value;
            if(result)
            {
                window.location.href="buy-powerleveling-wow-checkout/Default.aspx?mode=0";
            }
        }
    });
    
    $("#levelServer").change(function(){
         $("#selCurrent option[value!='0']").remove();
         var gameid = $("#levelServer option:selected").val();
         InitLevel(gameid);
    });
    $("#selCurrent").change(function(){
        $("#selDesired option[value!='0']").remove();
        var crtLevel=$("#selCurrent option:selected").text(); 
        var lastLevel=$("#selCurrent option:last").text();
        var index=parseInt(crtLevel)+1;
        var j=parseInt(lastLevel);
        if(crtLevel != "0")
        {
            for(var i=index;i<=j;i++)
            {
                $("#selDesired").append("<option value='"+i+"'>"+i+"</option>");
            }   
        }
    });
             
    $("#selDesired").change(function(){
       var crtLevel=$("#selCurrent option:selected").val(); 
       var dsrLevel=$("#selDesired option:selected").val();
       var gameid=$("#levelServer option:selected").val();
        if(crtLevel != "0" && dsrLevel !="0" && gameid !="0")
        {
            var result=_Default.getLevelPriceDay(gameid, crtLevel, dsrLevel).value;
            if(result!='')
            {
                var temp=result.split(',');
                $("#price").val("USD $"+ temp[0]);
                $("#time").val(Math.floor(temp[1]/24).toString()  + '-' +  Math.ceil(temp[1]/24).toString() + 'Days');
            }
        }
        
    });
    
    $("#buyPower").click(function(){
        var character=$("#crcter");
        if(character.val()=="" ||character.val().length==0)
        {
            character.focus();
            return;
        }
        var tel=$("#tel");
        if(tel.val()=="" ||tel.val().length==0)
        {
            tel.focus();
            return;
        }
        var fst=$("#fst");
        if(fst.val()=="" ||fst.val().length==0)
        {
            fst.focus();
            return;
        }
        var lst=$("#lst");
        if(lst.val()=="" ||lst.val().length==0)
        {
            lst.focus();
            return;
        }
        
        
        var game=$("#levelServer option:selected");
        var levelname = game.text() + '-(level' + $("#selCurrent option:selected").val()+ " to level"+ $("#selDesired option:selected").val() + ')-(' +
                $("#time").val() + ')';
        var price=$("#price").val().replace("USD $","");
        var result =  _Default.addgamelevelingcart(levelname, '', price, ip, game.val());
        if(result.value)
        {
            window.location.href="buy-powerleveling-wow-checkout/Default.aspx?mode=2&ch=" + character.val() + "&tel=" + tel.val() + "&fst=" + fst.val() + "&lst=" + lst.val();
        }
    });
    
    $(".nav a:gt(0)").click(function(){
        var word="";
        switch($(this).text())
        {
            case "Buy Gold":
                word="Gold";
                break;
            case "PowerLeveling":
                word="PowerLeveling";
                break;
            case "Buy CDk":
                word="Cdkey";
                break;
            case "WOW Item":
                word="Item";
                break;
            default:
                break;
        }
        _Default.Trans(word);
    });
    
    $(".choose_box table tr:gt(0)").mouseover(function(){
        var td=$(this).children("td:eq(0)");
         td.children("div").show();
    }).mouseout(function(){       
         var td=$(this).children("td:eq(0)");
         td.children("div").hide();
    });
    
    $("#ordersearch").click(function(){
        var oid=$("#orderid");
        var em=$("#email");
        if((oid.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' )=="" || oid.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' ).length==0) && (em.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' )=="" || em.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' ).length==0))
        {
            alert("please enter one item at least!");
            em.focus();
            return;
        }
        if(em.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' )!="" || em.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' ).length!=0)
        {
           var reg= /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
           if(!reg.test(em.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' )))
           {
               alert("please enter a valid email");
               em.select();
               return;
            }
        }
        window.location.href=
        "buy-powerleveling-wow-aboutus/OrderSearch.aspx?oid="+oid.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' )+"&em="+em.val().replace(/^\s\s*/, '' ).replace(/\s\s*$/, '' );
    });
});

function Init()
{
    GetData("196","51");
}

function GetData(gameid,levelclassid)
{
    var data = _Default.GetPowerlevelingPac(gameid,levelclassid).value;
    if(data.Rows.length>0)
    {
        var html="";
        for(var i=0;i<data.Rows.length;i++)
        {
             var saleprice=parseFloat(data.Rows[i].saleprice).toFixed(2);
                html+="<tr>";
                html+="<td><h3>"+data.Rows[i].levelclassname+"-"+data.Rows[i].productname+"</h3>";
                html+="<div>"+data.Rows[i].productinfo+"</div></td>";
                html+="<td>$"+saleprice+"</td>";
                html+="<td><a onclick=\"buy('"+data.Rows[i].productname+"','','"+saleprice+"','"+ip+"','"+gameid+"')\"><img src=\"images/choose_gold/buy2.jpg\" alt='' /></a></td>";
                html+="</tr>";
        }
        $(".choose_box table tr:gt(0)").remove();
        $(".choose_box table").append(html);
    }

//    $.ajax({
//        type:"POST",
//        url:postUrl,
//        data:{gameid:gameid,levelclassid:levelclassid},
//        dataType:"json",
//        complete:function(){
//            $("#loading").hide();},
//        success:function(data){
//            var html="";
//            $.each(data.level,function(i,lvlproduct){
//                var saleprice=parseFloat(data.level[i].saleprice).toFixed(2);
//                html+="<tr>";
//                html+="<td><h3>"+data.level[i].levelclassname+"-"+data.level[i].productname+"</h3>";
//                html+="<p></p></td>";
//                html+="<td>$"+saleprice+"</td>";
//                html+="<td><a onclick=\"buy('"+data.level[i].productname+"','','"+saleprice+"','"+ip+"','"+gameid+"')\"><img src=\"images/choose_gold/buy2.jpg\" alt='' /></a></td>";
//                html+="</tr>";
//            });
//            $(".choose_box table tr:gt(0)").remove();
//            $(".choose_box table").append(html);
//        }
//    });
}

function buy(productname,productinfo,saleprice,ip,gameid)
{
    var result = _Default.addgamelevelingcart(productname, productinfo, saleprice,ip,gameid);
    if(result.value)
    {
        window.location.href="buy-powerleveling-wow-checkout/Default.aspx?mode=2";
    }
}

function InitLevel(gameid)
{
    var current = _Default.currentLevel(gameid).value;
    for(var i=1; i<=parseInt(current); i++)
    {
        $("#selCurrent").append("<option value='"+i+"'>"+i+"</option>");
    }
}

function OnEnter( field ) { 
    if( field.value == field.defaultValue ) {
         field.value = ""; 
    }
}
function OnExit( field ) { 
    if( field.value == "" ) {
         field.value = field.defaultValue; 
    }
}
    


