var ContactBehavior = Class.create();
ContactBehavior.Load = function() {
    OS.RegisterBehaviour(ContactBehavior.Rules);
}

ContactBehavior.Rules = {
    '#Cnt_ContactForm .data input': function(element) {
        Event.observe(element, 'blur', function() {
            ContactBehavior.ValidForm(element);
        });
        Event.observe(element, 'focus', function() {
            if (element.value.length <= 0) return;
            ContactBehavior.ValidForm(element);
        });
        Event.observe(element, 'click', function() {
            if (element.value.length <= 0) return;
            ContactBehavior.ValidForm(element);
        });
    },
    '#Cnt_ContactForm .data select': function(element) {
        Event.observe(element, 'change', function() {
            ContactBehavior.ValidForm(element);
        });
    },
    '#Cmd_SendContact': function(element) {
        Event.observe(element, 'click', function() {
            if (ContactBehavior.ValidForm()) {
                ContactBehavior.SendForm();
            }
        });
    },
    '#fComments': function(element) {
        var fMaxSize = function() {
            var bValid = element.value.length <= 2048;
            if (!bValid) element.value = element.value.substring(0, 2048);
            ContactBehavior.ValidForm(element);
            return bValid;
        };
        element.onkeypress = function() { setTimeout(fMaxSize, 0); }
        element.onblur = function() { setTimeout(fMaxSize, 0); }
    }
}

ContactBehavior.OpenContactForm = function() {
    ContactBehavior.ResetValidation();
    $('Cnt_AutoResponder').innerHTML = '';
    $('Cnt_ContactProgress').innerHTML = '';
    Element.hide('Mod_ContactProgress');
    Element.hide('Mod_AutoResponder');
    Element.show('Cnt_ContactForm');
    Element.show('Ftr_ContactForm');
    if (Prototype.Browser.IE && Prototype.BrowserVersion() < 7) {
        Element.hide('Pnl_ContactInfo');
        Element.show('Pnl_ContactForm');
        Element.show('Pv_Contact');
        return;
    }
    var ef2 = new Effect.Appear('Pnl_ContactForm', { duration: .4 });
    var ef1 = new Effect.Fade('Pnl_ContactInfo', { duration: .1 });
    var efAppear = new Effect.Appear('Pv_Contact', { duration: .4 });
    ef1 = null;
    ef2 = null;
    efAppear = null;
}
ContactBehavior.CloseContactForm = function() {
    var eBody = $('Body');
    if (Prototype.Browser.IE && Prototype.BrowserVersion() < 7) {
        Element.hide('Pnl_ContactForm');
        Element.show('Pnl_ContactInfo');
        if (!Element.hasClassName(eBody, 'viewContact')) Element.hide('Pv_Contact');
        return;
    }
    if (!Element.hasClassName(eBody, 'viewContact')) {
        var efFade = new Effect.Fade('Pv_Contact', { duration: .4 });
        efFade = null;
    } else {
        var ef2 = new Effect.Appear('Pnl_ContactInfo', { duration: .4 });
        ef2 = null;
        var ef1 = new Effect.Fade('Pnl_ContactForm', { duration: .1 });
        ef1 = null;
    }
}

ContactBehavior.ResetValidation = function() {
    var eHud = $('Hud_ContactValidate');
    var eForm = $('Form_Contact');
    Application.ThrowMessages = new Hash();
    Application.ResetThrowStatus(eHud, eForm);
}
ContactBehavior.ValidForm = function(triggerInput) {
    var eHud = $('Hud_ContactValidate');
    var eForm = $('Form_Contact');
    if (triggerInput) {
        var eParent = triggerInput.parentNode;
        var eParentParent = eParent.parentNode;
        if (Element.hasClassName(eParentParent, 'radioList')) {
            Element.removeClassName(eParentParent, 'highlight');
        } else if (Element.hasClassName(eParent, 'checkbox')) {
            Element.removeClassName(eParent, 'highlight');
        } else {
            Element.removeClassName(triggerInput, 'highlight');
        }
    } else { ContactBehavior.ResetValidation(); }

    Application.RequireTextbox(eHud, 'fFirstName', 'First Name required', triggerInput);
    Application.RequireTextbox(eHud, 'fLastName', 'Last Name required', triggerInput);
    Application.RequireTextbox(eHud, 'fEmail', 'Email required', triggerInput);
    Application.RequireTextboxMask(eHud, 'fEmail', Application.ValidateEmail, 'Email Address is not a valid email address', triggerInput);
    Application.RequireSelectbox(eHud, 'fInterest', 'Interest required', triggerInput);
    Application.RequireSelectbox(eHud, 'fFoundUs', 'How You Found Us required', triggerInput);
    //Application.RequireTextbox(eHud, 'fComments', 'Message required', triggerInput);

    var bHasStatus = Application.ThrowStatusSummary(eHud);
    var bValidThrow = Application.IsValidThrowStatus(eHud);
    //if (triggerInput) Element[(bHasStatus) ? 'show' : 'hide'](eHud);
    _OnResize();
    return bValidThrow;
}
ContactBehavior.SendForm = function() {
    var eForm = $('Form_Contact');

    var pContact = {
        Command: 'SendMessage',
        FirstName: $('fFirstName').value,
        LastName: $('fLastName').value,
        Email: $('fEmail').value,
        Phone: $('fPhone').value,
        Address: $('fAddress').value,
        City: $('fCity').value,
        State: $('fState').value,
        Zip: $('fZip').value,
        Interest: $('fInterest').options[$('fInterest').selectedIndex].innerHTML,
        FoundUs: $('fFoundUs').options[$('fFoundUs').selectedIndex].innerHTML,
        OptIn: $('fOptIn').checked,
        Comments: $('fComments').value
    };

    Element.hide('Cnt_ContactForm');
    Element.hide('Ftr_ContactForm');
    $('Cnt_ContactProgress').innerHTML = 'Sending Information...';
    Element.show('Mod_ContactProgress');
    new Ajax.Request('webservices/Contact.ashx', {
        method: 'get',
        parameters: pContact,
        onError: function() {
            ContactBehavior.ShowAutoResponder('<p>Could not send contact information.  Please try again later.</p>');
        },
        onFailure: function() {
            ContactBehavior.ShowAutoResponder('<p>Could not send contact information.  Please try again later.</p>');
        },
        onSuccess: function(transport, json) {
            var sResponse = transport.responseText || "No response text";
            var jsResponse = sResponse.evalJSON(false);
            if (!jsResponse.IsSuccess) {
                Application.Track('View-ContactFormFailed');
                ContactBehavior.ShowAutoResponder('<p>Could not send contact information.  Please try again later.</p>');
                return;
            }
            Application.Track('View-ContactFormSent');
            var sMessage = '<p>Thank you for contacting Blalock Lakes. We will be in touch with you soon regarding your request.</p>';
            ContactBehavior.ShowAutoResponder(sMessage);
        }
    });
}

ContactBehavior.ShowAutoResponder = function(message) {
    Element.hide('Cnt_ContactForm');
    Element.hide('Ftr_ContactForm');
    $('Cnt_AutoResponder').innerHTML = message;
    $('Cnt_ContactProgress').innerHTML = '';
    Element.hide('Mod_ContactProgress');
    Element.show('Mod_AutoResponder');
    _OnResize();
}

ContactBehavior.Load();
