﻿/// <reference path="jquery-1.2.6-intellisense.js"/>
/// <reference path="json2.js" />

//find the row and show the div with text box and two buttons
function EditOrderName(orderref) 
{
    var mainDiv = $('#divOrderName' + orderref);
    mainDiv.find("DIV[ID$='divOrderNameEdit']").find("INPUT[ID$='txtOrderName']").val(jQuery.trim(mainDiv.find("A[ID$='lnkOrderName']").text()));

    mainDiv.find("A[ID$='lnkOrderName']").hide('slow');
    mainDiv.find("DIV[ID$='divOrderNameEdit']").show('slow');
}

//call web service and hide the
function SaveOrderName(orderref) 
{
    var newName = $('#divOrderName' + orderref).find("DIV[ID$='divOrderNameEdit']").find("INPUT[ID$='txtOrderName']").val();
    
    var params = { 'orderref': orderref, 'newName': jQuery.trim(newName) };
    
    var jsonString = "{JsonOrderref: '" + JSON.stringify(params) + "'}";
    
    var serviceURL = resolveURL("WebServices/OrderServices.asmx/UpdateOrderName");
    $.ajax({
        type: "POST",
        url: serviceURL,
        data: jsonString,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function(xhr, desc, exceptionobj) 
        {
            ShowAlert("Sorry, but there was a problem processing this action. Please try again or contact Helpdesk.\n" + xhr.responseText);
        },
        success: function(result) 
        {
            var mainDiv = $('#divOrderName' + orderref);
            mainDiv.find("DIV[ID$='divOrderNameEdit']").hide('slow');
                        
            if (result.d == 1) 
            {
                var name = $('#divOrderName' + orderref).find("DIV[ID$='divOrderNameEdit']").find("INPUT[ID$='txtOrderName']").val();
                mainDiv.find("A[ID$='lnkOrderName']").text(jQuery.trim(name)).show('slow');
            }
            else 
            {
                ShowAlert("Sorry, but there was a problem processing this action. Please try again or contact Helpdesk.");
                mainDiv.find("A[ID$='lnkOrderName']").show('slow');
            }            
        }
    });
}

function CancelOrderNameEdit(orderref) 
{
    var mainDiv = $('#divOrderName' + orderref);
    mainDiv.find("A[ID$='lnkOrderName']").show('slow');
    mainDiv.find("DIV[ID$='divOrderNameEdit']").hide('slow');
}