var DefaultWhereDidYouHearAboutUsValue = "Select an item from the list";

window.addEvent('domready', function() {
    SetupContactForm();
    //IsEmail(str);
});


function SetupFocusBGChange() {
    var InputArray = [$('ContactName'), $('ContactCompany'), $('ContactEmail'), $('ContactPhone'), $('ContactMessage')];

    InputArray.each(function(e, i, a) {
        e.addEvent('focus', function() {
            e.getParent().getParent().setStyle('background-color', "#E6E6E6");
        });

        e.addEvent('blur', function() {
            e.getParent().getParent().setStyle('background-color', "#F7F7F7");
        });
    });
}

function AddHookForSubmit() {
    $('ContactSubmit').addEvent('click', function() {
        return ValidateContactForm();
    });
}

function ReevaluateMessageBoxSize() {
    if ($('ContactMessage').getProperty('value').length > 240) {
        var newHeight = ($('ContactMessage').getProperty('value').length / 48).toInt() * 20;
        $('ContactMessage').morph({ 'height': newHeight, 'background-position': "0 " + newHeight - 77 + "px" });
    }
}

function DisplayDropDown(el) {
    var InputArray = [$('ContactWhereDidYouHearAboutUsSelector')];

    InputArray.each(function(e, i, a) {
        if (el == e) {
            if (e.getStyle("display") == "block") {
                e.setStyle("display", "none");
            }
            else {
                e.setStyle("display", "block");
            }
        }
        else {
            e.setStyle("display", "none");
            e.getParent().getParent().getParent().removeEvents("mouseleave");
        }
    });
}

function SetupContactForm() {
    //Setup Budget Arrays
    SetupFocusBGChange();
    AddHookForSubmit();

    //Setup Drop Down Lists

    //Default values
    $('ContactHearAboutUsValue').set("html", DefaultWhereDidYouHearAboutUsValue);

    $('ContactMessage').addEvent('keyup', function() {
        //ReevaluateMessageBoxSize();
    });


    //Heard about us
    $('ContactHearAboutUsValue').addEvent('click', function() {
        DisplayDropDown($('ContactWhereDidYouHearAboutUsSelector'));
    });

    //Heard about us
    $('ContactHearAboutUsValue').getNext().addEvent('click', function() {
        DisplayDropDown($('ContactWhereDidYouHearAboutUsSelector'));
    });


    //Hear about us selector events
    $('ContactWhereDidYouHearAboutUsSelector').getFirst().getChildren().each(function(e, i, a) {
        e.addEvent('click', function() {
            //update text in ContactProjectTypeDisplayValue div
            $('ContactHearAboutUsValue').set('html', e.get('html'));

            //Update Hidden Value with value from ContactProjectTypeDisplayValue
            $('ContactWhereDidYouHearAboutUs').setProperty('value', e.get('html'));

            //Hide Selector
            $('ContactWhereDidYouHearAboutUsSelector').setStyle('display', 'none');

        });
    });

};

var ValidateContactForm = function() {
    var result = true;

    //Message ContactMessage
    if ($('ContactMessage').get('value') == "") {
        $('ContactMessage').focus();

        result = false;
        $('ContactMessage').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });
    }

    //Phone
    if ($('ContactPhone').get('value') == ""){
        $('ContactPhone').focus();

        result = false;
        $('ContactPhone').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });
    }

//    if ($('QuickContactFormContactUsPhoneNumber').getProperty('value').length >= 3) {
//        $('ContactPhone').focus();

//        result = false;
//        $('ContactPhone').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });
//    }


    /*if (!IsNumber($('ContactPhone').get('value'))) {

        $('ContactPhone').focus();

        result = false;
        $('ContactPhone').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });
    }*/

    //Email
    if ($('ContactEmail').get('value') == "") {
        $('ContactEmail').focus();

        result = false;
        $('ContactEmail').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });

//        function IsEmail(str) {
//        var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
//        return pattern.test(str);
//        }
//        
//        IsEmail($('ContactEmail').get('value')));
//        
//        var myBoundFunction = myFunction.bind(myElement);
//        myBoundFunction();

    }

    if (!IsEmail($('ContactEmail').get('value'))) {

        $('ContactEmail').focus();

        result = false;
        $('ContactEmail').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });
    }

    //Company
    if ($('ContactCompany').get('value') == "") {
        $('ContactCompany').focus();

        result = false;
        $('ContactCompany').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });
    }

    //Name
    if ($('ContactName').get('value') == "") {
        $('ContactName').focus();

        result = false;
        $('ContactName').getParent().getParent().getFirst().morph({ 'color': ['#ff0000', "#4D4D4D"] });
    }

    return result;
}