var wantTo;
var type;
$(document).ready(function () {
    initFields();
    $("#submit").click(function () {

        wantTo = "";
        type = "";
        $("input:checkbox[checked]").each(function () {
            wantTo = wantTo + $(this).val() + " | ";
        });
        type = getPageName();
        if (validateFields()) {
            submitAjaxForm();
        }
        else {
            return false;
        }
    });
});

function initFields() {
    wantTo = "";
    $("input:text, textarea").each(function () {
        if ($(this).hasClass("not-valid")) {
            $(this).removeClass("not-valid").css("border", "solid 1px #ccc").css("color", "#ccc");
        }
        $(this).val("").focus(function () {
            $(this).val("");
            $(this).removeClass("not-valid").css("border", "solid 1px #ccc").css("color", "#555");
        });
    });

}

function validateFields() {
    var isValid;
    $(".required").each(function () {

        if ($(this).val() == '' || $(this).val() == ' ' || $(this).val() == '  ') {
            $(this).addClass('not-valid').css("border", "solid 1px #ff0000");
            isValid = false;
        }
        else {
            if ($(this).hasClass('not-valid')) {
                $(this).removeClass('not-valid');
            }
            isValid = true;
        }
    });

    $(".mail").each(function () {
        if (IsValidEmail($(this).val()))
            isValid = true;
        else {
            $(this).addClass('not-valid').css("border", "solid 1px #ff0000");
            isValid = false;
            $(this).val("");
        }
    });
    return isValid;
}

function IsValidEmail(email) {
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    return emailPattern.test(email);
}

function submitAjaxForm() {

    $.ajax({
        type: "POST",
        url: "SendMail.aspx",
        data:   "type=" + type + "&" +
                "fname=" + $("#txtFirstName").val() + "&" +
                "lname=" + $("#txtLastName").val() + "&" +
                "company=" + $("#txtCompany").val() + "&" +
                "phone=" + $("#txtPhone").val() + "&" +
                "cell=" + $("#txtCell").val() + "&" +
                "address=" + $("#txtAddress").val() + "&" +
                "mail=" + $("#txtMail").val() + "&" +                
                "msg=" + $("#txtMsg").val(),
        success: function (msg) {
            if (msg == "") {
                alert("Sorry but it seems that there was an error during send proccess, please try again later.");
				fieldsClear();
            }
            else {
                alert("Thank you. Your messege has been sent and we will contact you shortly.");
				fieldsClear();
            }			
        },
		error:function (){
                    alert("Sorry but it seems that there was an error duiring send proccess, please try again later.");
                    fieldsClear();
                }
    });
}

function getPageName() {
    var loc = window.location;
    var pathName = loc.pathname.substring(loc.pathname.lastIndexOf('/') + 1, loc.pathname.length);
    pathName = pathName.substring(0,pathName.lastIndexOf('.'));
    return pathName;
}

function fieldsClear()
{
	$("input[type=text]").val("");
	$('input[type=checkbox]').each(function(){
		$(this).attr('checked', false);
	});
	$("textarea").val("");
}
