﻿/// <reference path="jquery-1.2.6.js" />

var previousValue = "";

//When DOM is ready...
$(function() {

    //Hide all <tr> elements that have contain the class 'shipto'
    $("tr[class*='shipto']").hide();
       
    //When any select elements whose ID starts with shipto_ are changed            
    $("select[id^='shipto_']").change(function() {                             
    
        var selectNumber;
        
        //Parse the select number out of the ID.
        selectNumber = $(this).attr("id");
        selectNumber = selectNumber.replace("shipto_","");         
                
        //If they've selected a new address
        if ($(this).val()=='_new')
        {                                                                                                 
            //Show the new ship to field
        
            $("tr[id='shiptoform_" + selectNumber + "']").fadeIn("slow");                        
            
            $("span[id='oops_" + selectNumber + "']").click(function() {                
                $("select[id='shipto_" + selectNumber + "']").val($("input[id='pv_" + selectNumber + "']").val());
                $("tr[id='shiptoform_" + selectNumber + "']").fadeOut("slow");
            });   
            
            //On focus of the ship to field           
            $("input[id='st_" + selectNumber + "']").focus(function() {                            
            
                if ($("input[id='st_" + selectNumber + "']").val()=="Enter Recipient's First Name")
                {
                    $("input[id='st_" + selectNumber + "']").val("");
                }                                                  
            });  
            
            //On blur of the ship to field
            $("input[id='st_" + selectNumber + "']").blur(function() {
                if ($("input[id='st_" + selectNumber + "']").val()=="") { $("input[id='st_" + selectNumber + "']").val("Enter Recipient's First Name"); }                                                                 
            });
        }
        else
        {        
            $("input[id='pv_" + selectNumber + "']").val($(this).val());            
        
            var lineItemID;            
            lineItemID = $("input[id='liid_" + selectNumber + "']").val();
        
            jQuery.get("updateShipToLevelID.aspx", { STLID: $(this).val(), LIID: lineItemID });            
            $("tr[id='shiptoform_" + selectNumber + "']").fadeOut("slow");
        }
        
    });
});