$(document).ready(function(){

    $("#sharpie-ad input[type=text]").focus( function() {
        if ($(this).attr('value') == $(this).attr('placeholder')){
            $(this).val('');
        }
    });

    $("#sharpie-ad input[type=text]").blur( function() {
        if ($(this).attr('value') == ''){
            $(this).val($(this).attr('placeholder'));
        }
    });

    $("#sharpie-ad input[type=image]").click( function(e) {
        e.preventDefault();

        var gather = function (input) {
            var v = $(input).val();
            var p = $(input).attr('placeholder');
            if (!v || v == p)
            {
                return false;
            }
            return v;
        };

        var regex_text  = /^([a-zA-Z0-9\,\.\\\\\'\(\)\*\&\%\$\#\@\!\?\;\:\+\=\-\_\s\/\’\|\[\]\’\“\”ÀàÈèÌìÒòÁáÉéÍíÓóÚúÑñÜü¿¡])*$/;
        var regex_email = /^([a-zA-Z0-9\.\-\_]*)\@([a-zA-Z0-9\.\-\_]*)\.([a-zA-Z0-9\-\_]{2,6})$/;
        var regex_zip   = /^[0-9]{5}|[0-9]{5}-[0-9]{4}$/;
        var regex_state = /^[A-Za-z]{2}$/;

        var n = gather("input[name='sh_name']");
        var a = gather("input[name='sh_addr']");
        var c = gather("input[name='sh_city']");
        var s = gather("input[name='sh_state']");
        var z = gather("input[name='sh_zip']");
        var e = gather("input[name='sh_email']");

        if (!n || !a || !c || !s || !z || !e)
        {
            alert("All Fields are Required");
            return;
        }
        if (!n.match(regex_text)) {
            alert("Is that really your name?\nCan you simplify it for me?");
            return;
        }
        if (!a.match(regex_text)) {
            alert("Is that really your address?\nCan you simplify it for me?");
            return;
        }
        if (!c.match(regex_text)) {
            alert("Is that really your city?\nCan you simplify it for me?");
            return;
        }
        if (!s.match(regex_state)) {
            alert("Please use a two letter state code.");
            return;
        }
        if (!z.match(regex_zip)) {
            alert("Please use a standard US Zip Code.");
            return;
        }
        if (!e.match(regex_email)) {
            alert("Is that really your email address?\nPlease take another look.");
            return;
        }

        var data = {
            name      : n,
            addr      : a,
            city      : c,
            state     : s,
            zip       : z,
            email     : e
        }

        /*
        var test = '';
        for(i in data)
        {
            test += i + "-" + data[i] + "\n";
        }
        alert(test);
        */

        $.get('/sharpie/', data, function(d){
            if (d.status == 'error')
            {
                alert(d.error);
                return;
            }
            if (d.status == 'success' && d.result == '1')
            {
                //reset the form
                $('#sharpie-ad input[type=text]').each(function(){
                    $(this).val($(this).attr('placeholder'));
                });

                //show success message
                $('#sharpie-ad .msg.success').show();
            }
            else
            {
                alert("I'm having a problem submitting your form.\nReload the page and give it another try.\nIf this problem persists, please let us know.");
            }
        });

    });

});

