﻿
function getCookie() {
    var c_start = "FoxFireRealtY";
    var c_name = "ReturnedUser";
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_start + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function setCookie(expiredays) {
    var exdate = new Date();
    var c_name = "FoxFireRealtY";
    var value = "ReturnedUser";
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

function checkCookie() {
    userreturn = getCookie();
    if (userreturn != "") {
        alert("No show popup");
        return null;

    }
    else {
        //setCookie(90);
     
    }
}

function LTrim(str) {
    var whitespace = new String(" \t\n\r ");
    // last space character is not a space, but alt+0160, 
    // another invisible char. 
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        // We have a string with leading blank(s)...
        var j = 0, i = s.length;
        // Iterate from the far left of string until we
        // don't have any more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;
        // Get the substring from the first non-whitespace
        // character to the end of the string...
        s = s.substring(j, i);
    }
    return s;
}
// Trims all spaces to the right of a specific string
function RTrim(str) {
    // We don't want to trip JUST spaces, but also tabs,
    // line feeds, etc.  Add anything else you want to
    // "trim" here in whitespace
    var whitespace = new String(" \t\n\r ");
    // last space character is not a space, but alt+0160,
    // another invisible char. 
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
        // We have a string with trailing blank(s)...
        var i = s.length - 1;       // Get length of string
        // Iterate from the far right of string until we
        // don't have any more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;
        // Get the substring from the front of the string to
        // where the last non-whitespace character is...
        s = s.substring(0, i + 1);
    }
    return s;
}



$(document).ready(function () {
    userreturn = getCookie();
    if (userreturn != "") {
        return null;

    }
    else {
        setCookie(90);
        $(function () {
            $('html, body').animate({ scrollTop: 0 }, 'fast');

            // before showing the modal window, reset the form incase of previous use.
            $('.success, .error').hide();
            $('form#contactForm').show();

            // Reset all the default values in the form fields
            $('#name').val('Your name');
            $('#email').val('Your email address');
            $('#comment').val('Enter your comment or query...');

            //show the mask and contact divs
            $('#mask').show().fadeTo('', 0.7);
            $('div#contact').fadeIn();
        });
    }
    $('div#close, div#mask').click(function () {
        $('div#contact, div#mask').stop().fadeOut('slow');

    });

    $('#contactForm input').focus(function () {
        $(this).val(' ');
    });

    // when the Submit button is clicked...
    $('input#submited').click(function () {
        //Inputed Strings
        var username = $('#name').val(),
			email = $('#email').val(),
			comment = $('#comment').val(),
            txcontact = $('#txcontact').val(),
            phone = $('#phone').val();

        //Error Count
        var error_count = 0;

        //Regex Strings
        var username_regex = /^[a-z0-9_-]{3,16}$/,
			email_regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

        //Test Username
        if (username == '') {
            $('#contact_header').after('<p class=error>Invalid name entered!</p>');
            error_count += 1;
        }

        //Test Email
        if (!email_regex.test(RTrim(email))) {
            $('#contact_header').after('<p class=error>Invalid email entered!</p>');
            error_count += 1;
        }

        // Blank Time to contact
        if (txcontact == '') {
            $('#contact_header').after('<p class=error>No best time to Contact was entered!</p>');
            error_count += 1;
        }
        //Blank Comment?
        if (comment == '') {
            $('#contact_header').after('<p class=error>No Comment was entered!</p>');
            error_count += 1;
        }

        //No Errors?
        if (error_count == 0) {
            var dataString = "name=" + username + "&email=" + email + "&phone=" + phone + "&time=" + txcontact + "&comment=" + comment;
            $.ajax({
                type: "post",
                url: "process.aspx",
                data: dataString,
                error: function () {
                    $('.error').hide();
                    $('#sendError').slideDown('slow');
                },
                success: function () {
                    $('.error').hide();
                    $('.success').slideDown('slow');
                    $('form#contactForm').fadeOut('slow');
                }
            });
        }

        else {
            $('.error').show();
        }

        return false;
    });
});
