//
// $Id: Address.js,v 1.1 2010/07/10 16:48:25 steve Exp $
//
var Address = function() {

    this.html     = new Html()
    this.thickbox = new ThickBoxGeneric()
    this.url      = new Url()

    this.initAddress = function() {

        var self = this

        self.thickbox.init()

        self.id = self.url.clickedUrlArg('Id')

        self.loadAddressParameters()

        $('#addressSaveButton').click( function() { self.saveAddress() })
    }

    this.initTab = function() {

        var self = this

        self.thickbox.applyThickBox($('#contactUsTabContent a.thickbox'))
    }
    
    this.loadAddressParameters = function() {

        var self = this

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=loadAddressParameters',
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#countyTd').html(data.Counties)
                        if (self.id) {
                            self.loadAddress()         
                        } else {
                            $('#addressTitle').html('New Address')
                            self.html.setCheckBoxValue($('#addressTable .initialChecked'), 1)
                            self.showTable()
                        }
                    }
                }
        })
    }

    this.loadAddress = function() {

        var self = this

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=loadAddress&Id=' + self.id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#addressTitle').html("Edit '" + data.Address.address1 + "'")
                        $('#address1').val(data.Address.address1)
                        self.html.setCheckBoxValue($('#address1Show'), data.Address.address1show)
                        $('#address2').val(data.Address.address2)
                        self.html.setCheckBoxValue($('#address2Show'), data.Address.address2show)
                        $('#address3').val(data.Address.address3)
                        self.html.setCheckBoxValue($('#address3Show'), data.Address.address3show)
                        $('#town').val(data.Address.town)
                        self.html.setCheckBoxValue($('#townShow'), data.Address.townshow)
                        $('#county').val(data.Address.countyid)
                        self.html.setCheckBoxValue($('#countyShow'), data.Address.countyshow)
                        $('#postcode').val(data.Address.address1)
                        self.html.setCheckBoxValue($('#postcodeShow'), data.Address.postcodeshow)

                        self.showTable()
                    }
                }
        })
    }

    this.showTable = function() {

        $('#addressTable').show()
    }

    this.saveAddress = function() {

        var self = this

        var address1     = $('#address1').val()
        var address1Show = self.html.getCheckBoxValue($('#address1Show'))
        var address2     = $('#address2').val()
        var address2Show = self.html.getCheckBoxValue($('#address2Show'))
        var address3     = $('#address3').val()
        var address3Show = self.html.getCheckBoxValue($('#address3Show'))
        var town         = $('#town').val()
        var townShow     = self.html.getCheckBoxValue($('#townShow'))
        var countyId     = $('#county').val()
        var countyShow   = self.html.getCheckBoxValue($('#countyShow'))
        var postcode     = $('#postcode').val()
        var postcodeShow = self.html.getCheckBoxValue($('#postcodeShow'))

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=saveAddress&Id=' + self.id +
                                              '&Address1=' + address1 + 
                                              '&Address1Show=' + address1Show + 
                                              '&Address2=' + address2 + 
                                              '&Address2Show=' + address2Show + 
                                              '&Address3=' + address3 + 
                                              '&Address3Show=' + address3Show + 
                                              '&Town=' + town + 
                                              '&TownShow=' + townShow + 
                                              '&CountyId=' + countyId + 
                                              '&CountyShow=' + countyShow + 
                                              '&Postcode=' + postcode + 
                                              '&PostcodeShow=' + postcodeShow,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#contactUsTabContent').html(data.Content)
                        self.initTab()
                        if (self.id) {
                            alert('Address updated')         
                        } else {
                            alert('Address created')         
                        }
                    }
                    tb_remove()
                }
        })
    }
}
