var industry = "";
$(document).ready(function () {
    initFields();
    $("#submit").click(function () {
        if (validateFields()) {
            submitAjaxForm();
        }
        else {
            return false;
        }
    });
});

function initFields() {
    $("input:text, textarea").each(function () {
        if ($(this).hasClass("not-valid")) {
            $(this).removeClass("not-valid");
        }
        $(this).val("").focus(function () {
            $(this).val("");
            $(this).removeClass("not-valid");
        });
    });

}

function validateFields() {
    var isValid;
    $("input:checkbox:checked").each(function(){industry += $(this).attr('value') + ", "});
    $(".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');
            }
            $(this).css("border", "solid 1px #ccc");
            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: "send.aspx",
        data:   "type=" + $("#ddlType option:selected").text() + "&" +
                "fname=" + $("#txtFirstName").val() + "&" +
				"lname=" + $("#txtLastName").val() + "&" +
                "company=" + $("#txtCompany").val() + "&" +
				"phone=" + $("#txtPhone").val() + "&" +    
                "email=" + $("#txtEmail").val() + "&" +
				"city=" + $("#txtCity").val() + "&" +
				"country=" + $("#ddlCountry option:selected").text() + "&" +
                "industry=" + industry + "&" +
				"heardFrom=" + $("#txtHeardFrom").val() + "&" +
                "msg=" + $("#txtMsg").val(),
	            
        success: function (msg) 
        {
            //if (msg == "" || msg == " ") 
            //{
            //    alert("Sorry but it seems that there was an error duiring send proccess, please try again later.");
			//	fieldsClear();
            //}
            //else 
            //{
            //    alert("Thank you. Your message has been sent and we will contact you shortly.");
			//	fieldsClear();
            //}		
			
                window.location="thank_you.html";
				fieldsClear();
           				
        },
		error:function ()
		{
            alert("Sorry but it seems that there was an error duiring send proccess, please try again later.");
            fieldsClear();
        }
    });
}

function fieldsClear()
{
	$("input[type=text]").val("");
	$('input[type=checkbox]').each(function(){$(this).attr('checked', false);});
	$("textarea").val("");
	$("select option:first").attr('selected','selected');
}

