﻿
var temp_upload_file_ptrs;

//SETTING UP ThanksPopup
//0 means disabled; 1 means enabled;
var ThanksPopupStatus = 0;

function loadThanksPopup() {
    //loads ThanksPopup only if it is disabled
    if (ThanksPopupStatus == 0) {
        $("#backgroundThanksPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundThanksPopup").fadeIn("slow");
        $("#ThanksPopupContact").fadeIn("slow");
        ThanksPopupStatus = 1;
    }
}

function disableThanksPopup() {
    //disables ThanksPopup only if it is enabled
    if (ThanksPopupStatus == 1) {
        $("#backgroundThanksPopup").fadeOut("slow");
        $("#ThanksPopupContact").fadeOut("slow");
        ThanksPopupStatus = 0;
    }
}

//centering ThanksPopup
function centerThanksPopup() {
    //request data for centering

    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var ThanksPopupHeight = $("#ThanksPopupContact").height();
    var ThanksPopupWidth = $("#ThanksPopupContact").width();
    //centering
    $("#ThanksPopupContact").css({
        "position": "absolute",
        "top": windowHeight / 2 - ThanksPopupHeight ,
        "left": windowWidth / 2 - ThanksPopupWidth
    });
    //only need force for IE6

    $("#backgroundThanksPopup").css({
        "height": windowHeight
    });

}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function () {

    //CLOSING ThanksPopup

    $("#ThanksPopupContactClose").click(function () {
        disableThanksPopup();
    });
    //Click out event!
    $("#backgroundThanksPopup").click(function () {
        disableThanksPopup();
    });
    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27 && ThanksPopupStatus == 1) {
            disableThanksPopup();
        }
    });

});

var max_text_length = 3500;

function validateData() {

    if ($('#agreeement_check_box > input').attr('checked') == undefined) {
        $('.errorBox').text('You must agree to Terms & Conditions. Please select the agreement checkbox.');
        $('#agreeement_check_box > input').focus();
        $('.errorBox').show();
        return false;
    }

    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    if ($('.FirstName').val().replace(/\s/g, "") == "") {
        $('.errorBox').text('You must enter your first name.');
        $('.FirstName').focus();
        $('.errorBox').show();
        return false;
    }

    if ($('.last_name').val().replace(/\s/g, "") == "") {
        $('.errorBox').text('You must enter your last name.');
        $('.last_name').focus();
        $('.errorBox').show();
        return false;
    }

    if ($('.user_email').val().replace(/\s/g, "") == "") {
        $('.errorBox').text('You must enter your email address.');
        $('.user_email').focus();
        $('.errorBox').show();
        return false;
    }

    if (!emailReg.test($('.user_email').val())) {
        $('.errorBox').text('You must enter a valid email address.');
        $('.user_email').focus();
        $('.errorBox').show();
        return false;
    }

    var temp_text_info = $('.story_field').val();
    temp_text_info = $('<div/>').html(temp_text_info).text();
    $('.story_field').val(temp_text_info);
    var file_uploaded = false;

    if ($('.story_field').val().replace(/\s/g, "") == "") {

        temp_upload_file_ptrs = $('.controlItem > input');

        if ($(temp_upload_file_ptrs[0]).val().replace(/\s/g, "") != "")
            file_uploaded = true;

        if ($(temp_upload_file_ptrs[1]).val().replace(/\s/g, "") != "")
            file_uploaded = true;
        if ($(temp_upload_file_ptrs[2]).val().replace(/\s/g, "") != "")
            file_uploaded = true;
        if ($(temp_upload_file_ptrs[3]).val().replace(/\s/g, "") != "")
            file_uploaded = true;

        if (file_uploaded == false) {
            $('.errorBox').text('No text or file names of uploaded photos have been entered.');
            $('.story_field').focus();
            $('.errorBox').show();
            return false;
        }
    }

    if (temp_text_info.length > max_text_length) {
        $('.errorBox').text('The length of the story cannot exceed ' + max_text_length + ' characters.  There are ' + temp_text_info.length + ' characters in your story.');
        $('.story_field').focus();
        $('.errorBox').show();
        return false;
    }


    if ($('.phone_nunmber').val() != "xxx-xxx-xxxx") {
        var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
        if (!phoneNumberPattern.test($('.phone_nunmber').val())) {
            $('.errorBox').text('The phone number is in invalid format. format (xxx-xxx-xxxx)');
            $('.phone_nunmber').focus();
            $('.errorBox').show();
            return false;
        }
    }

    $('.errorBox').hide();
    return true;
}

$(document).ready(function () {

    if ($("#submit_value_flag").children(":input").val() != "false") {
        centerThanksPopup();
        //load ThanksPopup
        loadThanksPopup();
        $('.FirstName').val('');
        $('.last_name').val('');
        $('.user_email').val('');
        $('.story_field').val('');
        $('#agreeement_check_box > input').removeAttr("checked");

    }

    $(':input[title]').each(function () {
        var $this = $(this);
        if ($this.val() === '') {
            $this.val($this.attr('title'));
        }
        $this.focus(function () {
            if ($this.val() === $this.attr('title')) {
                $this.val('');
            }
        });
        $this.blur(function () {
            if ($this.val() === '') {
                $this.val($this.attr('title'));
            }
        });
    });

    $("#submitPhoto_tips_expanded").hide();
    $("#submitPhoto_terms").hide();
    $(".show_hide").show();

    $('.terms_and_agreements').click(function () {
        $("#submitPhoto_terms").slideToggle(1500, function () {
            if ($("#submitPhoto_terms").hasClass('expanded')) {
                $("#submitPhoto_terms").removeClass('expanded');
            }
            else {
                $("#submitPhoto_terms").addClass('expanded');
            }
        });
        return false;
    });

    $('.show_hide').click(function () {
        $("#submitPhoto_tips_expanded").slideToggle(500, function () {
            if ($("#submitPhoto_tips").hasClass('expanded')) {
                $("#submitPhoto_tips").removeClass('expanded');
            }
            else {
                $("#submitPhoto_tips").addClass('expanded');
            }
        });
    });


});

        

