Subversion Repositories SmartDukaan

Rev

View as "text/plain" | Blame | Last modification | View Log | RSS feed

$().ready(function() {
        $("form#create-item-form input").each(function(){
                $(this).attr('autocomplete', 'off');
        });
});
$().ready(function() {
    // validate the comment form when it is submitted
    $('#create-item-form').validate({
                rules:{
                        vendor:{
                                required:true
                        },
                        itemBrand:{
                                required:true
                        },
                        description:{
                                required:true
                        }
                },
                messages:{
                        vendor:{
                                required:"Please choose vendor"
                        },
                        itemBrand:{
                                required:"Please enter brand"
                        },
                        description:{
                                required: "Please enter description"
                        }
                },
                submitHandler: function (form, event) {
                        event.preventDefault();
                        if(validateItemDetails()){
                                alert("Please fix errors");
                                return false;
                        }
                        var json = itemDetailsJson();
                        console.log("itemJson = " + json);
                        if(confirm("Are you sure you want to create Item!") == true){
                                doPostAjaxRequestWithJsonHandler(context+"/createItem", json, function(response){
                                        $('#main-content').html(response);
                                });
                     return false; // required to block normal submit since you used ajax
                        }
         }
        });
  
});


function validateItemDetails(){
        console.log("validating Item Details...");
        var error = false;
        var vendorId = $("#vendors").val();
        console.log("vendorId = " + vendorId);
        $("#vendor").removeClass("border-highlight");
        if(vendorId == ""){
                alert("Vendor is required");
                $("#vendorId").addClass("border-highlight");
                error = true;
                return error;
        }
        
        var itemBrand = $("form#create-item-form input[name=itemBrand]").val();
        console.log("itemBrand = " + itemBrand);
        $("#itemBrand").removeClass("border-highlight");
        if(itemBrand == ""){
                alert("Brand is required");
                $("#itemBrand").addClass("border-highlight");
                error = true;
                return error;
        }
        
        var description = $("form#create-item-form input[name=description]").val();
        console.log("description = " + description);
        $("#description").removeClass("border-highlight");
        if(description == ""){
                alert("Description is required");
                $("#description").addClass("border-highlight");
                error = true;
                return error;
        }
        
        var typeString = $("#types").val();
        console.log("typeString = " + typeString);
        $("#types").removeClass("border-highlight");
        if(typeString == ""){
                alert("Type is required");
                $("#types").addClass("border-highlight");
                error = true;
                return error;
        }
        
        var price = $("form#create-item-form input[name=price]").val();
        console.log("price = " + price);
        if (price == ""){
                $("form#create-item-form input[name=price]").val(0);
        }
        
        console.log("validation vendor error = " + error);
        return error;
}

function itemDetailsJson(){
        console.log("itemDetailsJson");
        var itemObject = {};
        itemObject['vendorId'] = $("#vendors").val();
        itemObject['brand'] = $("form#create-item-form input[name=itemBrand]").val();
        itemObject['description'] = $("form#create-item-form input[name=description]").val();
        itemObject['typeString'] = $('#types').val();
        itemObject['price'] = $("form#create-item-form input[name=price]").val();
        return JSON.stringify(itemObject);
}