﻿/// <reference path="intellisense/jquery-1.2.6-vsdoc.js" />
/// <reference path="intellisense/jquery.ajaxdotnet.intellisense.js" />
$(function() {
    //watermark
    $('input[wm], textarea[wm], select[wm]').each(function() {
        $(this).data('water', $(this).attr('wm'));

        $(this).Watermark($(this).data('water'), '#808080');
    }); // wm

    //Check if IP in database
    $.ajaxDotNet('WebServices/TV.asmx/isIpExist'
            , { success: function(ip) {

                if (ip.IP) {
                    // IP exist Hide email
                    $('#trEmail, #tblContact').hide();
                    $('#tblMessage').show();
                    $('#message').html('Please Wait Until We Verify Your Last Email');
                }
                else {
                    $('span.email').mailme();
                    $('#trEmail, #tblContact').show();
                    $('#tblMessage').hide();
                    $('#message').html('');
                }
            }
            }); //ajaxdotnet

    $('.tip').SCCtip();
    $('#SendMessage').click(function(e) {
        e.preventDefault();
        if ($("#contactMe").is(":hidden")) {
            $("#contactMe").slideDown("slow");
            $('#txt_name').focus();
        }
        else {
            $("#contactMe").slideUp("slow");
        }
        return false;
    }); //SendMessage click



    // Submit the message
    $('#submit').click(function(e) {

        e.preventDefault();
        // Validate controls
        var mes = '';
        var $ctrl;

        // Name requiered
        $('.required').each(function() {
            var $this = $(this);
            if ($this.val() == $this.data('water'))
                mes += $this.attr('name') + ' is required\n';
        }); // Required
        // email validation
        $('.email').each(function() {
            var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
            var regex = new RegExp(emailReg);

            //check for valid email
            if (!regex.test($(this).val()))
                mes += 'Enter a valid email address\n';
        }); // email validation
        if (mes.length > 0) {
            alert(mes);
        }
        else {
            //debugger;
            // OK post the message
            //Add_Guest(string strName, string strEmail, string strTitle, string strRefer, string strCity, string strComment)
            $.ajaxDotNet('WebServices/TV.asmx/Add_Guest_New'
                    , {
                        data:
                                        {
                                            'strName': $('#txt_name').val()
                                        , 'strEmail': $('#txt_email').val()
                                        , 'strTitle': 'Coming from contact'
                                        , 'strCity': $('#txt_city').val()
                                        , 'strComment': $('#txt_comment').val()

                                        }
                                        , success: function() {
                                            //clear and hide
                                            $('input[type="text"], textarea').val('');
                                            $('#trEmail, #tblContact').hide();
                                            $('#tblMessage').show();
                                            $('#message').html('Your email has been sent. I will contact you as soon as I can. Thank you.');

                                        } //success
                                        , error: function(xhr, st, e) {
                                            debugger;
                                            alert('error');
                                        } // Error
                    }
                    ); // ajaxdotnet submit 

        }
        return false;

    });

    // Cancel click
    $('#cancel').click(function() { clearForm(); }); //Cancel Click

    // New map
    $('#divmap').click(function(e) {
        e.preventDefault();
        var content = '<div id="map" style="width: 500px; height: 300px"></div><div id="Div1"></div></div>';
        $.nyroModalManual({
            
            content: content
        , endShowContent: function(elts, settings) {
            var map = new GMap(document.getElementById("map"));
            map.addControl(new GLargeMapControl());
            map.addControl(new GMapTypeControl());
            map.centerAndZoom(new GPoint(-118.027369, 33.679522), 2);
            // Office
            var point = new GPoint(-118.027369, 33.679522);
            var address = "SCC Office<br />19368 Peachtree Circle <br />Huntington Beach, CA 92648";
            var marker = createInfoMarker(point, address);
            map.addOverlay(marker);
        } //end show content
        }); // ModalManual

        return false;
    }); //divmap click

});
function createInfoMarker(point, address) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click",
      function() {
          marker.openInfoWindowHtml(address);
      }
   );
    return marker;
}

function ShowMap() {
    var top = (screen.height - 300) / 2;
    var left = (screen.width - 500) / 2;
    var options = "menubar=no,scrollbars=no,statusbar=no";
    window.open("Contact2.aspx", "", "top=" + top + ",left=" + left + ",width=505,height=330," + options);
}

$.fn.mailme = function() {
    var at = / at /;
    var dot = / dot /g;
    this.each(function() {
        var addr = $(this).text().replace(at, "@").replace(dot, ".");
        var title = $(this).attr('title')
        $(this)
            .after('<a href="mailto:' + addr + '" title="' + title + '">' + addr + '</a>')
            .remove();
    });
};
function clearForm() {
    // Clear all field
    $('input[type="text"], textarea').val('');
    $.Watermark.ShowAll();
    $('#txt_name').focus();
}
