    var cat1 = "" ;
    var cat2 = "" ;
    $(document).ready(function(){
        initCat1("");
        initCat2($("#drp_Catalog").find("option:selected").attr("value"),"")
        $(".step2").hide();
        $("#submit").hide();
        
        $("#btn_Next").click(function(){
            validPadUrl();
        })
  
        $("#drp_Catalog").change(function(){
            initCat2($('#drp_Catalog option:selected').attr("value"),"");
        })
        
        $("#btn_Submit").click(function(){
            submit();
        })
    })
    
    function validPadUrl()
    {
        var padUrl = $("#txt_PadUrl").val() ;
        if (padUrl == "")
        {
            alert("Please enter PadUrl address") ;
            $("#txt_PadUrl").focus();
            return ;
        }
         $.get("/include/ajax.aspx", { op: "GetPadCatalog", padUrl: padUrl },
          function(data){
            if (data == "-1")
            {
                alert("This PadUrl can not access or malformed");
                $("#txt_PadUrl").focus();
                return ;
            }
            else
            {
                $("#next").hide();
                $("#submit").show();
                $(".step2").show();
            }
            var arrCatalog = data.split("::") ;
            cat1 = "" ;
            cat2 = "" ;
            if (arrCatalog.length>1)
            {
                cat1 = arrCatalog[0] ;
                cat2 = arrCatalog[1] ;
                
                $("#drp_Catalog option[text='" + cat1 + "']").attr("selected",true) 
                initCat2($("#drp_Catalog option[text='" + cat1 + "']").attr("value"),
                    $("#drp_SubCatalog option[text='" + cat2 + "']").attr("value"));
            }

          });   

    }
    
 

    function initCat1(cat1)
    {
        $("#drp_Catalog").empty();
        $("#catalog .cat1").each(function(){
            $("<option value=\"" + $(this).attr("id") + "\">" + $(this).attr("name") + "</option>").appendTo($("#drp_Catalog")) ;
        })
    }
    function initCat2(cat1,cat2)
    {

        $("#drp_SubCatalog").empty();
        $("#catalog #" + cat1 + " li").each(function(){
            $("<option value=\"" + $(this).attr("id") + "\">" + $(this).html() + "</option>").appendTo($("#drp_SubCatalog")) ;
        })
    }
    
    function submit()
    {
        $("#submitform").hide();
        $("#loading").show();
        $.get("/include/ajax.aspx", { op: "AddProduct", padUrl: $("#txt_PadUrl").val(),catId:$('#drp_SubCatalog option:selected').attr("value") },
            function(data){
                var pid = parseInt(data) ;
                if (pid>0)
                {
                    var viewUrl = "/preview-x" + pid + ".html" ;
                    $("#loading").hide();     
                    $("#success").show() ; 
                    $("#aView").attr("href",viewUrl) ;
                    location.href = viewUrl ;
                }
                else
                {
                    var msg = "Submit Error:" ;
                    if (pid == -2)
                        msg += "PadUrl Read Error" ;
                    if (pid == 0)
                        msg += "Database operation error" ;    
                    $("#loading").hide();          
                    $("#error").show() ;
                    $("#errormsg").html(msg) ;
                }
            }
        )
    }
