// ******** Step 1 ***********

function FindTrader(TraderIDID, TraderNameID, CategoryNameID, CategoryIDID, BuisinessNumberID, AddressID, TownID, PostcodeID, AddressResult1ID, AddressResult2ID, AddressResult3ID, TownResultID, CountyResultID, PostcodeResultID, PhoneResultID, MobileResultID, WebsiteResultID, EmailResultID, NextBtnID, ShowDetailsID)
{
    this.TraderID = document.getElementById(TraderIDID);
    this.TraderName = document.getElementById(TraderNameID);
    this.CategoryName = document.getElementById(CategoryNameID);
    this.CategoryID = document.getElementById(CategoryIDID);
    this.BuisinessNumber = document.getElementById(BuisinessNumberID);
    this.Address = document.getElementById(AddressID);
    this.Town = document.getElementById(TownID);
    this.Postcode = document.getElementById(PostcodeID);
    this.PleaseWait = document.getElementById('rec-wizard-pleasewait');
    this.SearchOptions = document.getElementById('rec-wizard-options');
    this.TraderList = document.getElementById('rec-wizard-traderresults');
    this.TraderListSelect = document.getElementById('lstTraderResults');
    this.AddressList = document.getElementById('rec-wizard-addressresults');
    this.AddressListSelect = document.getElementById('lstAddressResults');
    this.OptionalDetails = document.getElementById('rec-wizard-findmytrader');
    this.DetailsLabel = document.getElementById('details-label');
    this.HelpContent = document.getElementById('details-helptext');
    this.TraderErrorMessage = document.getElementById('rec-wizard-trader-errormessage');
    this.TraderSelectErrorMessage = document.getElementById('rec-wizard-select-trade-errormessage');
    this.AddressSelectErrorMessage = document.getElementById('rec-wizard-select-address-errormessage');
    this.CategoryErrorMessage = document.getElementById('rec-wizard-category-errormessage');
    this.OptionsErrorMessage = document.getElementById('rec-wizard-options-errormessage');
    this.AddressResult1 = document.getElementById(AddressResult1ID);
    this.AddressResult2 = document.getElementById(AddressResult2ID);
    this.AddressResult3 = document.getElementById(AddressResult3ID);
    this.TownResult = document.getElementById(TownResultID);
    this.CountyResult = document.getElementById(CountyResultID);
    this.PostcodeResult = document.getElementById(PostcodeResultID);
    this.PhoneResult = document.getElementById(PhoneResultID);
    this.MobileResult = document.getElementById(MobileResultID);
    this.WebsiteResult = document.getElementById(WebsiteResultID);
    this.EmailResult = document.getElementById(EmailResultID);
    this.NextBtn = document.getElementById(NextBtnID);
    this.ShowDetails = document.getElementById(ShowDetailsID);
    this.SetupEvents();
    if(this.ShowDetails.value == "0")
    {
        this.DetailsEdit();
        $(this.SearchOptions).show();
        $(this.OptionalDetails).hide();
        this.DisableNext();
    }
    else
    {
        $(this.SearchOptions).hide();
        if(this.TraderID.value == "-1")
        {
            this.DetailsLabel.innerHTML = "Optional trader details";
            this.HelpContent.innerHTML = "Please complete as many of the details below as you can to help us identify your trader.";
            this.DetailsEdit();
        }
        else
        {
            this.DetailsLabel.innerHTML = "Existing trader details";
            this.HelpContent.innerHTML = "Below are the trader details that we have on our system for " + this.TraderName + ". Please click the 'Next' button below to confirm that this is the trader you are reviewing.";
            this.DetailsReadOnly();
        }
        $(this.OptionalDetails).show();
        this.EnableNext();
    }
}

FindTrader.prototype.DisableNext = function()
{
    this.NextBtn.src = '/images/button_next_grey.png';
}

FindTrader.prototype.EnableNext = function()
{
    this.NextBtn.src = '/images/button_next_red.png';
}

FindTrader.prototype.SetupEvents = function()
{
    var oThis = this;
    this.TraderListSelect.onkeydown = function (oEvent) 
    {
        if (!oEvent)
        {
            oEvent = window.event;
        } 
        if(oEvent.keyCode == 13 || oEvent.keyCode == 9)
        {
            oThis.TraderSelect();
            return false;
        }
        else if(oEvent.keyCode == 27)
        {
            oThis.TraderNotListed();
            return false;
        }
        else
        {
            return true;
        }
    };
    this.AddressListSelect.onkeydown = function (oEvent) 
    {
        if (!oEvent)
        {
            oEvent = window.event;
        } 
        if(oEvent.keyCode == 13 || oEvent.keyCode == 9)
        {
            oThis.AddressSelect();
            return false;
        }
        else if(oEvent.keyCode == 27)
        {
            oThis.AddressNotListed();
            return false;
        }
        else
        {
            return true;
        }
    };
}

FindTrader.prototype.Find = function ()
{
    
    if(this.ValidateInput())
    {
        $(this.SearchOptions).hide("slow");
        
        if(this.CategoryID.value > 0)
        {
            
            
            this.TraderLookup();
        }
        else if ((this.Address.value.length > 0 && this.Town.value.length > 0) || this.Postcode.value.length > 0)
        {
            
            this.AddressLookup();
        }
        else
        {
            this.OptionalDetailsClear();
        }
    }
}

FindTrader.prototype.ValidateInput = function () 
{
    if(( this.TraderName.value.length > 0 && this.CategoryName.value.length > 0 && $(this.CategoryName).attr('title') != this.CategoryName.value) && (this.BuisinessNumber.value.length > 0 || (this.Address.value.length > 0 && this.Town.value.length > 0) || this.Postcode.value.length > 0))
    {
        this.HideErrorMessages();
        return true;
    }
    else
    {
        $(this.OptionalDetails).hide();
        $(this.TraderList).hide();
        $(this.AddressList).hide();
        $(this.PleaseWait).hide();
        if(this.TraderName.value.length == 0)
        {
            this.TraderErrorMessage.innerHTML = 'Please enter the name of the trader \n';
            $(this.TraderErrorMessage).show();
        }
        else
        {
            $(this.TraderErrorMessage).hide();
        }
        if(this.CategoryName.value.length == 0 || $(this.CategoryName).attr('title') == this.CategoryName.value)
        {
            this.CategoryErrorMessage.innerHTML = 'You must select a category';
            $(this.CategoryErrorMessage).show();
        }
        else
        {
            $(this.CategoryErrorMessage).hide();
        }
        if(!(this.BuisinessNumber.value.length > 0 || (this.Address.value.length > 0 && this.Town.value.length > 0) || this.Postcode.value.length > 0))
        {
            this.OptionsErrorMessage.innerHTML = 'You must enter the details in one of the 3 boxes';
             $(this.OptionsErrorMessage).show();
        }
        else
        {
            $(this.OptionsErrorMessage).hide();
        }
       
        return false;
    }
}

FindTrader.prototype.TraderLookup = function () 
{
    
    $(this.PleaseWait).show("fast");
//    var oThis = this;
//    $.ajax({
//                type: "POST",
//                url: "/Controls/Review/Ajax/TraderAddressLookup.asmx/LookupTrader",
//                data: "{'sTraderName' : '" + escape(this.TraderName.value) + "', 'sCategoryID' : '" + escape(this.CategoryID.value) + "', 'sPhone' : '" + escape(this.BuisinessNumber.value) + "' , 'sAddress' : '" + escape(this.Address.value) + "' , 'sTown' : '" + escape(this.Town.value) + "' , 'sPostcode' : '" + escape(this.Postcode.value) + "'}",
//                contentType: "application/json; charset=utf-8",
//                dataType: "json",
//                success: function(results) 
//                {
//                    oThis.TraderLookupResult(results);    
//                }
    //            });
    var oThis = this;
    Sys.Net.WebServiceProxy.invoke(
            "/Controls/Review/Ajax/TraderAddressLookup.asmx",
            "LookupTrader",
            false,
            { 'sTraderName': escape(this.TraderName.value) , 'sCategoryID': escape(this.CategoryID.value), 'sPhone': escape(this.BuisinessNumber.value), 'sAddress': escape(this.Address.value), 'sTown': escape(this.Town.value) , 'sPostcode': escape(this.Postcode.value) },
            Function.createDelegate(this, this.TraderLookupResult),
            null);
}

FindTrader.prototype.TraderLookupResult = function(results)
{
    if(results.length > 0)
    {
        $(this.TraderSelectErrorMessage).hide();
        this.TraderListSelect.options.length = 0;
        for (var i=0; i < results.length; i++) 
        { 
            try
            {
                this.TraderListSelect.add(new Option(results[i][0], results[i][1]), null);
            }
            catch(e)
            {
                this.TraderListSelect.add(new Option(results[i][0], results[i][1]));
            }
                
        }
        $(this.PleaseWait).hide("fast");
        $(this.TraderList).show("slow");
    }
    else
    {
        this.AddressLookup();
    }
}

FindTrader.prototype.TraderSelect = function ()
{
    if(this.TraderListSelect.value != "")
    {
        $(this.TraderSelectErrorMessage).hide();
        $(this.TraderList).hide("slow");
        $(this.PleaseWait).show("fast");
        var oThis = this;
//        $.ajax({
//                type: "POST",
//                url: "/Controls/Review/Ajax/TraderAddressLookup.asmx/GetTraderDetails",
//                data: "{'sCategoryID' : '" + this.TraderListSelect.value + "'}",
//                contentType: "application/json; charset=utf-8",
//                dataType: "json",
//                success: function(results) 
//                {
//                    oThis.TraderSelectResult(results);    
//                }
        //            });
        Sys.Net.WebServiceProxy.invoke(
            "/Controls/Review/Ajax/TraderAddressLookup.asmx",
            "GetTraderDetails",
            false,
            { 'sCategoryID': this.TraderListSelect.value },
            Function.createDelegate(this, this.TraderSelectResult),
            null);
    }
    else
    {
        this.TraderSelectErrorMessage.innerHTML = 'You must click on a trader first. Click "Not listed" if your trader is not in the list.';
        $(this.TraderSelectErrorMessage).show();
    }
}

FindTrader.prototype.TraderNotListed = function ()
{
    $(this.TraderList).hide("slow");
    $(this.PleaseWait).show("fast");
    this.AddressLookup();
}

FindTrader.prototype.TraderSelectResult = function(results)
{
    this.TraderID.value = results.TraderID;
    this.TraderName.value = results.Name
    this.CategoryName.value = results.Category
    this.CategoryID.value = results.CatID;
    this.AddressResult1.value = results.Address1;
    this.AddressResult2.value = results.Address2;
    this.AddressResult3.value = results.Address3;
    this.TownResult.value = results.Town;
    this.CountyResult.value = results.County;
    this.PostcodeResult.value = results.Postcode; 
    this.PhoneResult.value = results.Phone;   
    this.MobileResult.value = results.Mobile;   
    this.WebsiteResult.value = results.Website; 
    this.EmailResult.value = results.Email;
    this.DetailsReadOnly();
    this.DetailsLabel.innerHTML = "Existing trader details";
    this.HelpContent.innerHTML = "Below are the trader details that we have on our system for " + this.TraderName.value + ". Please click the 'Next' button below to confirm that this is the trader you are reviewing.";
    $(this.PleaseWait).hide("fast");
    $(this.OptionalDetails).show("slow");
    this.HideErrorMessages();
    this.EnableNext();
}

FindTrader.prototype.OptionalDetailsClear = function()
{
    $(this.PleaseWait).hide("fast");
    this.TraderID.value = "-1"
    this.AddressResult1.value = this.Address.value;
    this.AddressResult2.value = "";
    this.AddressResult3.value = "";
    this.TownResult.value = this.Town.value;
    this.CountyResult.value = "";
    this.PostcodeResult.value = this.Postcode.value; 
    this.PhoneResult.value = this.BuisinessNumber.value;   
    this.MobileResult.value = "";   
    this.WebsiteResult.value = ""; 
    this.EmailResult.value = "";
    this.DetailsEdit();
    this.DetailsLabel.innerHTML = "Optional trader details";
    this.HelpContent.innerHTML = "Please complete as many of the details below as you can to help us identify your trader.";
    $(this.OptionalDetails).show("slow");
    this.HideErrorMessages();
    this.EnableNext();
}

FindTrader.prototype.AddressLookup  = function () 
{
   
    $(this.PleaseWait).show("fast");
    var oThis = this;
//    $.ajax({
//                type: "POST",
//                url: "/Controls/Review/Ajax/TraderAddressLookup.asmx/LookupAddress",
//                data: "{'sAddress' : '" + escape(this.Address.value) + "', 'sTown' : '" + escape(this.Town.value) + "' , 'sPostcode' : '" + escape(this.Postcode.value) + "'}",
//                contentType: "application/json; charset=utf-8",
//                dataType: "json",
//                success: function(results) 
//                {
//                    oThis.AddressLookupResult(results);    
//                }
    //            });
    var oThis = this;
    Sys.Net.WebServiceProxy.invoke(
            "/Controls/Review/Ajax/TraderAddressLookup.asmx",
            "LookupAddress",
            false,
            { 'sAddress':  escape(this.Address.value), 'sTown': escape(this.Town.value), 'sPostcode': escape(this.Postcode.value) },
            Function.createDelegate(this, this.AddressLookupResult),
            null);
}

FindTrader.prototype.AddressLookupResult = function(results)
{
    if(results.length > 0)
    {
       $(this.AddressSelectErrorMessage).hide();
        this.AddressListSelect.options.length = 0;
        for (var i=0; i < results.length; i++) 
        { 
            try
            {
                this.AddressListSelect.add(new Option(results[i][0], results[i][1]), null);
            }
            catch(e)
            {
                this.AddressListSelect.add(new Option(results[i][0], results[i][1]));
            }
                
        }
        $(this.PleaseWait).hide("fast");
        $(this.AddressList).show("slow");
    }
    else
    {
        this.OptionalDetailsClear();
    }
}

FindTrader.prototype.AddressSelect = function()
{
    if(this.AddressListSelect.value != "")
    {
        $(this.AddressList).hide("slow");
        $(this.PleaseWait).show("fast");
        var oThis = this;
//        $.ajax({
//                type: "POST",
//                url: "/Controls/Review/Ajax/TraderAddressLookup.asmx/GetAddress",
//                data: "{'sAddressID' : '" + this.AddressListSelect.value + "'}",
//                contentType: "application/json; charset=utf-8",
//                dataType: "json",
//                success: function(results) 
//                {
//                    oThis.AddressSelectResult(results);    
//                }
        //            });
        Sys.Net.WebServiceProxy.invoke(
            "/Controls/Review/Ajax/TraderAddressLookup.asmx",
            "GetAddress",
            false,
            { 'sAddressID': this.AddressListSelect.value },
            Function.createDelegate(this, this.AddressSelectResult),
            null);
    }
    else
    {
        this.AddressSelectErrorMessage.innerHTML = 'You must click on an address first. Click "Not listed" if your traders address is not in the list.';
        $(this.AddressSelectErrorMessage).show();

    }
}

FindTrader.prototype.AddressNotListed = function()
{
     
     $(this.AddressList).hide("slow");
    this.OptionalDetailsClear();   
}

FindTrader.prototype.AddressSelectResult = function(results)
{
    $(this.PleaseWait).hide("fast");
    this.TraderID.value = "-1"
    this.AddressResult1.value = results.Address1;
    this.AddressResult2.value = results.Address2;
    this.AddressResult3.value = results.Address3;
    this.TownResult.value = results.Town;
    this.CountyResult.value = results.County;
    this.PostcodeResult.value = results.Postcode; 
    this.PhoneResult.value = this.BuisinessNumber.value;   
    this.MobileResult.value = "";   
    this.WebsiteResult.value = ""; 
    this.EmailResult.value = "";
    this.DetailsEdit();
    this.DetailsLabel.innerHTML = "Address details found, please fill in any other details";
    $(this.OptionalDetails).show("slow");
    this.HideErrorMessages();
    this.EnableNext();
}

FindTrader.prototype.SearchAgain = function(results)
{
    this.TraderID.value = "-1"
    this.DisableNext();
    this.HideErrorMessages();
    $(this.OptionalDetails).hide("slow");
    this.DetailsEdit();
    $(this.SearchOptions).show("slow");
    
}

FindTrader.prototype.HideErrorMessages = function()
{
    $(this.TraderErrorMessage).hide();
    $(this.CategoryErrorMessage).hide();
    $(this.OptionsErrorMessage).hide();
}

FindTrader.prototype.DetailsReadOnly = function()
{
    this.TraderName.disabled   = true;
    this.CategoryName.disabled   = true;
    this.AddressResult1.disabled   = true;
    this.AddressResult2.disabled   = true;
    this.AddressResult3.disabled   = true;
    this.TownResult.disabled   = true;
    this.CountyResult.disabled   = true;
    this.PostcodeResult.disabled   = true;
    this.PhoneResult.disabled   = true;
    this.MobileResult.disabled   = true;
    this.WebsiteResult.disabled   = true;
    this.EmailResult.disabled   = true;
    
    $(this.TraderName).removeClass('rec-wizard-form1a');
    $(this.CategoryName).removeClass('rec-wizard-form1a');
    $(this.AddressResult1).removeClass('rec-wizard-form1a');
    $(this.AddressResult2).removeClass('rec-wizard-form1a');
    $(this.AddressResult3).removeClass('rec-wizard-form1a');
    $(this.TownResult).removeClass('rec-wizard-form1a');
    $(this.CountyResult).removeClass('rec-wizard-form1a');
    $(this.PostcodeResult).removeClass('rec-wizard-form1a');
    $(this.PhoneResult).removeClass('rec-wizard-form1a');
    $(this.MobileResult).removeClass('rec-wizard-form1a');
    $(this.WebsiteResult).removeClass('rec-wizard-form1a');
    $(this.EmailResult).removeClass('rec-wizard-form1a');
    
    $(this.TraderName).addClass('rec-wizard-form1a-readonly');
    $(this.CategoryName).addClass('rec-wizard-form1a-readonly');
    $(this.AddressResult1).addClass('rec-wizard-form1a-readonly');
    $(this.AddressResult2).addClass('rec-wizard-form1a-readonly');
    $(this.AddressResult3).addClass('rec-wizard-form1a-readonly');
    $(this.TownResult).addClass('rec-wizard-form1a-readonly');
    $(this.CountyResult).addClass('rec-wizard-form1a-readonly');
    $(this.PostcodeResult).addClass('rec-wizard-form1a-readonly');
    $(this.PhoneResult).addClass('rec-wizard-form1a-readonly');
    $(this.MobileResult).addClass('rec-wizard-form1a-readonly');
    $(this.WebsiteResult).addClass('rec-wizard-form1a-readonly');
    $(this.EmailResult).addClass('rec-wizard-form1a-readonly');
}

FindTrader.prototype.DetailsEdit = function()
{
    this.TraderName.disabled   = false;
    this.CategoryName.disabled   = false;
    this.AddressResult1.disabled   = false;
    this.AddressResult2.disabled   = false;
    this.AddressResult3.disabled   = false;
    this.TownResult.disabled   = false;
    this.CountyResult.disabled   = false;
    this.PostcodeResult.disabled   = false;
    this.PhoneResult.disabled   = false;
    this.MobileResult.disabled   = false;
    this.WebsiteResult.disabled  = false;
    this.EmailResult.disabled   = false;
    
    $(this.TraderName).removeClass('rec-wizard-form1a-readonly');
    $(this.CategoryName).removeClass('rec-wizard-form1a-readonly');
    $(this.AddressResult1).removeClass('rec-wizard-form1a-readonly');
    $(this.AddressResult2).removeClass('rec-wizard-form1a-readonly');
    $(this.AddressResult3).removeClass('rec-wizard-form1a-readonly');
    $(this.TownResult).removeClass('rec-wizard-form1a-readonly');
    $(this.CountyResult).removeClass('rec-wizard-form1a-readonly');
    $(this.PostcodeResult).removeClass('rec-wizard-form1a-readonly');
    $(this.PhoneResult).removeClass('rec-wizard-form1a-readonly');
    $(this.MobileResult).removeClass('rec-wizard-form1a-readonly');
    $(this.WebsiteResult).removeClass('rec-wizard-form1a-readonly');
    $(this.EmailResult).removeClass('rec-wizard-form1a-readonly');
    
    $(this.TraderName).addClass('rec-wizard-form1a');
    $(this.CategoryName).addClass('rec-wizard-form1a');
    $(this.AddressResult1).addClass('rec-wizard-form1a');
    $(this.AddressResult2).addClass('rec-wizard-form1a');
    $(this.AddressResult3).addClass('rec-wizard-form1a');
    $(this.TownResult).addClass('rec-wizard-form1a');
    $(this.CountyResult).addClass('rec-wizard-form1a');
    $(this.PostcodeResult).addClass('rec-wizard-form1a');
    $(this.PhoneResult).addClass('rec-wizard-form1a');
    $(this.MobileResult).addClass('rec-wizard-form1a');
    $(this.WebsiteResult).addClass('rec-wizard-form1a');
    $(this.EmailResult).addClass('rec-wizard-form1a');
}