﻿/// <reference path="jquery-1.2.3.js" />

var tbody;
var template;

function AddRows(count) {
    for(var i = 0; i < count; i++) {
        tbody.append(template.clone(true));
    }
}

function GetProduct(input) {
    $.getJSON(
        'ProductQuery.aspx?' + $(input).serialize(),
        function(product) {
            UpdateRow(input.parentNode.parentNode, product);
        }
    );
}

function UpdateRow(tr, product) {
    var name = $('.name', tr);
    var price = $('.price', tr);
    
    if(product != undefined) {
        name.html(product.Name);
        price.html(product.Price);
    }
    else {
        name.html('Not Found');
        price.html('');
    }
}

$(document).ready(function() {
    $('[name=code]')
    .blur(function(e) {
        GetProduct(e.target);
    });

    $('#addRows')
    .click(function(e) {
        AddRows(5);
    });
    
    tbody = $('tbody');
    template = $('tr', tbody).clone(true);

    AddRows(4);
});