﻿var phone = "()- 0123456789";
var zip = "- 0123456789";
var numb = "0123456789";
//var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ '-";
var alphanumeric = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -'";
var email = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -_@.'";
var filter1 = "~|#=+<>[]{}_%&";

//function to accept only characters that are in the range
function res(t, v) {
    var w = "";
    for (i = 0; i < t.value.length; i++) {
        x = t.value.charAt(i);
        if (v.indexOf(x, 0) != -1)
            w += x;
    }
    t.value = w;
}

//function to not accept characters that are in the range
function filter(t, v) {
    var w = "";
    for (i = 0; i < t.value.length; i++) {  // Search through string and append to unfiltered values to returnString.
        x = t.value.charAt(i);
        if (v.indexOf(x, 0) == -1)
            w += x;
    }
    t.value = w;
}


$(document).ready(function() {

    $('input[type="text"]').addClass("idleField");
    $('textarea').addClass("idleField");
    $('select').addClass("idleField");

    $('input[type="text"]').inputHints();
    $('textarea').inputHints();
    $('select').inputHints();

    $("input:text:visible:first").focus();

    //    $.getJSON("EmailService.svc/GetCountries", null, function(data) {
    //        $("#lstCountryCode").addItems(data);
    //    });
});

$.fn.addItems = function(data) {
    return this.each(function() {
        var list = this;
        $.each(data, function(index, itemData) {
            var option = new Option(itemData.CountryName, itemData.CountryCode);
            list.add(option);
        });
    });
};

$("#lstCountryCode").change(function() {
    alert('you selected ' + $(this).val());
});



jQuery.fn.inputHints = function() {
    // hides the input display text stored in the title on focus
    // and sets it on blur if the user hasn't changed it.

    // show the display text
//    $(this).each(function(i) {
//        $(this).val($(this).attr('title'));
//    });

    // hook up the blur & focus
    $(this).focus(function() {
//        if ($(this).val() == $(this).attr('title'))
//            $(this).val('');
        $(this).removeClass("idleField").addClass("focusField");
    }).blur(function() {
//        if ($(this).val() == '')
//            $(this).val($(this).attr('title'));
        $(this).removeClass("focusField").addClass("idleField");
    });
};

function btn_Connect_onclick() {
    alert("hello");

    // Initialize the object, before adding data to it.
    //  { } is declarative shorthand for new Object().
    var ContactFormDto = {};

    var controls = ["txtFirstName", "txtLastName", "txtMiddleInitial", "txtEmail", "txtAddressLine", "txtCityName", "txtStateProvince", "lstCountryCode", "txtPostalCode", "txtHomePhone", "txtComments"];

    $.each(controls, function(intIndex, objValue) {
        //loop through elements and create json string
        ContactFormDto[objValue.substring(3, objValue.length)] = $("#" + objValue).val();
    }
            );

    alert(ContactFormDto);
    // Create a data transfer object (DTO) with the
    //  proper structure.
    var DTO = { 'dto': ContactFormDto };

    alert(JSON2.stringify(DTO));


    //this works
    $.ajax({
        contentType: "application/json; charset=utf-8",
        data: JSON2.stringify(DTO),
        dataType: "json",
        type: "POST",
        url: "free-cd.aspx/ProcessContact",
        success: function(response) {
            alert(response.d);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert("Error Occured!");
        }
    });


}