var industry = "";
var formType;
$(document).ready(function () {
    initFields();
    $("#submit").click(function () {
        if (validateFields()) {
			formType = "contact";
            submitAjaxForm();
        }
        else {
            return false;
        }
    });
	
	$("#submitNewsletter").click(function () {	
        if (validate_nFields()) {
			formType = "newsletter";
            submitNewsletterAjaxForm();
        }
        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 validate_nFields() {
    var isValid;
    $("input:checkbox:checked").each(function(){industry += $(this).attr('value') + ", "});
    $(".required_n").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_n").each(function () {
        if (IsValidEmail($(this).val()))
        {
        if ($(this).hasClass('not-valid')) {
                $(this).removeClass('not-valid');
            }
            $(this).css("border", "solid 1px #ccc");
            
            isValid = true;
            }
        else {
            $(this).addClass('not-valid').css("border", "solid 1px #ff0000");
            isValid = false;
            $(this).val("");            
        }
    });
    return isValid;
}

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()))
        {
        if ($(this).hasClass('not-valid')) {
                $(this).removeClass('not-valid');
            }
            $(this).css("border", "solid 1px #ccc");
            
            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=" + formType + "&" +
				"name=" + $("#txtName").val() + "&" +
                "company=" + $("#txtCompany").val() + "&" +
				"country=" + $("#ddlCountry").val() + "&" +    
                "email=" + $("#txtEmail").val() + "&" +
                "msg=" + $("#txtMsg").val(),
	            
        success: function (msg) {            
                alert(msg);
				fieldsClear();            
        },
		error:function (){
            alert("Sorry but it seems that there was an error duiring send proccess, please try again later.");
            fieldsClear();
        }
    });
}



function submitNewsletterAjaxForm() {
    $.ajax({
        type: 	"POST",
        url: 	"send.aspx",
        data:  	"type=" + formType + "&" +
				"name=" + $("#txt_Name").val() + "&" +
                "company=" + $("#txt_Company").val() + "&" +
                "email=" + $("#txt_Email").val(),
	            
        success: function (msg) {            
                alert(msg);
				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');
}


