//
// $Id: CMSContactUs.js,v 1.2 2010/07/10 16:48:23 steve Exp $
//
var CMSContactUs = function() {

    this.html     = new Html()
    this.thickbox = new ThickBoxGeneric()

    this.initCMS = function() {

        var self = this

        self.initTab()

        $('#contactUsTabs .contactUsTab').click( function() { self.changeTab($(this)) })
    }

    this.changeTab = function(elem) {

        var self = this

        $('#contactUsTabs .contactUsTab').removeClass('selected')
        $(elem).addClass('selected')

        var id = $(elem).attr('id').split('_')
        id     = id[1]

        self.getContactUsRecords(id) 
    }

    this.getContactUsRecords = function(type) {

        var self = this

        $('#contactUsTabContent').html('')
        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=getContactUsRecords&Type=' + type,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                         
                        $('#contactUsTabContent').html(data.Content)
                        self.thickbox.applyThickBox($('#contactUsTabContent a.thickbox'))

                        switch(type) {
                            case 'ContactUs':
                                self.initTab()
                                break
                            case 'Addresses':
                                address = new Address()
                                address.initTab()
                                break
                            case 'Locations':
                                loc = new Location()
                                loc.initTab()
                                break
                        }
                    }
                }
        })
    }    

    this.initTab = function() {

        var self = this

        self.thickbox.applyThickBox($('#contactUsTabContent a.thickbox'))

        $('#contactUsTabContent .contactUsLocation').change( function() { self.changeLocation($(this)) })
        $('#contactUsTabContent .contactUsAddress').change( function() { self.changeAddress($(this)) })
        $('#contactUsTabContent .contactUsPerson').change( function() { self.changePerson($(this)) })

        $('#contactUsTabContent .useAsMainContact').click( function() { self.useAsMainContact($(this)) })
        $('#contactUsTabContent .showContact').click( function() { self.showContact($(this)) })
        $('#contactUsTabContent .showMap').click( function() { self.showMap($(this)) })

        $('#contactUsTabContent .showLocation').click( function() { self.showItem($(this), 'location', 'Location') })
        $('#contactUsTabContent .showAddress').click( function() { self.showItem($(this), 'address', 'Address') })

        $('#contactUsTabContent .showPerson').click( function() { self.showItem($(this), 'person', 'Person') })

        $('#contactUsTabContent .showPersonPhone').click( function() { self.showItem($(this), 'phone number', 'PersonPhone') })
        $('#contactUsTabContent .showPersonMobile').click( function() { self.showItem($(this), 'mobile number', 'PersonMobile') })
        $('#contactUsTabContent .showPersonFax').click( function() { self.showItem($(this), 'fax number', 'PersonFax') })
        $('#contactUsTabContent .showPersonEmail').click( function() { self.showItem($(this), 'email address', 'PersonEmail') })

        $('#contactUsTabContent .usePersonPhone').click( function() { self.useItem($(this), 'phone number', 'Phone') })
        $('#contactUsTabContent .usePersonMobile').click( function() { self.useItem($(this), 'mobile number', 'Mobile') })
        $('#contactUsTabContent .usePersonFax').click( function() { self.useItem($(this), 'fax number', 'Fax') })
        $('#contactUsTabContent .usePersonEmail').click( function() { self.useItem($(this), 'email address', 'Email') })
    }

    this.changeLocation = function(elem) {

        var self = this

        var val = $(elem).val()
        var id  = $(elem).attr('id')
        var n   = id.split('_')
        n       = n[1]
        var loc = $('#' + id + ' option:selected').html()

        if (! confirm('Change location to ' + loc)) {
            $(elem).val($('#contactUsLocationHidden_' + n).val())
            return 
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=updateLocation&Id=' + n + '&LocationId=' + val,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })
    }    

    this.changeAddress = function(elem) {

        var self = this

        var val  = $(elem).val()
        var id   = $(elem).attr('id')
        var n    = id.split('_')
        n        = n[1]
        var addr = $('#' + id + ' option:selected').html()

        if (! confirm('Change address to ' + addr)) {
            $(elem).val($('#contactUsAddressHidden_' + n).val())
            return 
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=updateAddress&Id=' + n + '&AddressId=' + val,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })
    }

    this.changePerson = function(elem) {

        var self = this

        var val  = $(elem).val()
        var id   = $(elem).attr('id')
        var n    = id.split('_')
        n        = n[1]
        var pers = $('#' + id + ' option:selected').html()

        if (! confirm('Change person to ' + pers)) {
            $(elem).val($('#contactUsPersonHidden_' + n).val())
            return 
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=updatePerson&Id=' + n + '&PersonId=' + val,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        var showPerson = self.html.getCheckBoxValue($('#showperson_' + n))
                        self.setPerson(n, showPerson)
                    }
                }
        })
    }

    this.useAsMainContact = function(elem) {

        var self = this

        var id  = $(elem).attr('id').split('_')
        id      = id[1]
        var val = self.html.getCheckBoxValue($(elem))

        if (val) {
            if (! confirm('Use this as the main site contact?')) {
                self.html.setCheckBoxValue($(elem), 0)
                return
            }
            action = 'useAsMainContact'
        } else {
            if (! confirm('Don\'t use this as the main site contact?')) {
                self.html.setCheckBoxValue($(elem), 1)
                return
            }
            action = 'unUseAsMainContact'
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=' + action + '&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })
    }

    this.showContact = function(elem) {

        var self = this

        var id  = $(elem).attr('id').split('_')
        id      = id[1]
        var val = self.html.getCheckBoxValue($(elem))

        if (val) {
            if (! confirm('Show this contact on the site?')) {
                self.html.setCheckBoxValue($(elem), 0)
                return
            }
            action = 'showContact'
        } else {
            if (! confirm('Don\'t show this contact on the site?')) {
                self.html.setCheckBoxValue($(elem), 1)
                return
            }
            action = 'unShowContact'
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=' + action + '&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })
    }

    this.showMap = function(elem) {

        var self = this

        var id  = $(elem).attr('id').split('_')
        id      = id[1]
        var val = self.html.getCheckBoxValue($(elem))

        if (val) {
            if (! confirm('Show a map for this contact?')) {
                self.html.setCheckBoxValue($(elem), 0)
                return
            }
            action = 'showContactMap'
        } else {
            if (! confirm('Don\'t show a map for this contact?')) {
                self.html.setCheckBoxValue($(elem), 1)
                return
            }
            action = 'unShowContactMap'
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=' + action + '&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })
    }

    this.showItem = function(elem, lower, upper) {

        var self = this

        var id  = $(elem).attr('id').split('_')
        id      = id[1]
        var val = self.html.getCheckBoxValue($(elem))

        if (val) {
            if (! confirm('Show this ' + lower + ' on Contact Us pages?')) {
                self.html.setCheckBoxValue($(elem), 0)
                return
            }
            action = 'show' + upper
        } else {
            if (! confirm('Stop showing this ' + lower + ' on Contact Us pages?')) {
                self.html.setCheckBoxValue($(elem), 1)
                return
            }
            action = 'unShow' + upper
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=' + action + '&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        if (lower == 'person') {
                            self.setPerson(id, val)
                        }
                    }
                }
        })
    }

    this.setPerson = function(id, val) {

        var self = this

        $('#contactUsPhone_' + id).val('')
        $('#contactUsMobile_' + id).val('')
        $('#contactUsFax_' + id).val('') 
        $('#contactUsEmail_' + id).val('')

        if (val) {
            var personId = $('#contactUsPerson_' + id).val()
            $.ajax({
                      type:     'POST',
                      url:      CMSURL,
                      data:     'Action=loadPerson&Id=' + personId,
                      dataType: 'json',
                      timeout:  40000,
                error:
                    function() {
                        return false;
                    },
                success:
                    function(data) {
                        if (data.Error) {
                            alert(data.Message)
                        } else {
                            $('#contactUsPersonPhone_' + id).val(data.Person.phoneno)
                            $('#contactUsPersonMobile_' + id).val(data.Person.mobileno)
                            $('#contactUsPersonFax_' + id).val(data.Person.faxno)                            
                            $('#contactUsPersonEmail_' + id).val(data.Person.email)

                            if (self.html.getCheckBoxValue($('#usePersonPhone_' + id))) {
                                $('#contactUsPhone_' + id).val(data.Person.phoneno)
                            }
                            if (self.html.getCheckBoxValue($('#usePersonMobile_' + id))) {
                                $('#contactUsMobile_' + id).val(data.Person.mobileno)
                            }
                            if (self.html.getCheckBoxValue($('#usePersonFax_' + id))) {
                                $('#contactUsFax_' + id).val(data.Person.faxno)
                            }
                            if (self.html.getCheckBoxValue($('#usePersonEmail_' + id))) {
                                $('#contactUsEmail_' + id).val(data.Person.email)
                            }

                        }
                    }
            })
        }
    }

    this.useItem = function(elem, lower, upper) {

        var self = this

        var id  = $(elem).attr('id').split('_')
        id      = id[1]
        var val = self.html.getCheckBoxValue($(elem))

        var person = $('#contactUsPerson_' + id + ' option:selected').html()
        if (val) {
            if (! confirm("Use " + person + "'s " + lower + " on Contact Us pages?")) {
                self.html.setCheckBoxValue($(elem), 0)
                return
            }
            $('#contactUs' + upper + '_' + id).val($('#contactUsPerson' + upper + '_' + id).val())
            action = 'usePerson' + upper
        } else {
            if (! confirm("Don't use " + person + "'s " + lower + " on Contact Us pages?")) {
                self.html.setCheckBoxValue($(elem), 1)
                return
            }
            $('#contactUs' + upper +'_' + id).val('')
            action = 'unUsePerson' + upper
        } 

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=' + action + '&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })       
    }
}
