var industry = "";
var formType;

/**/
$(document).ready(function () {
  //  initFields();
    $("#submit").click(function () {
        if (validateFields()) {
			formType = "contact";
            submitAjaxForm();
        }
        else {
            return false;
        }
    });
	
	$("#submitNewsletter").click(function () {	
        if (validateFields()) {
			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 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=" + formType + "&" +
				"name=" + $("#txtName").val() + "&" +
                "company=" + $("#txtCompany").val() + "&" +
				"country=" + $("#ddlCountries:selected").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 fieldsClear()
{
	$("input[type=text]").val("");
	$('input[type=checkbox]').each(function(){$(this).attr('checked', false);});
	$("textarea").val("");
	$("select option:first").attr('selected','selected');
}





//	$(document).ready(function(){	

//		if (!$.browser.opera) {
//    
//			// select element styling
//			$('select.wpcf7-select').each(function(){
//				var title = $(this).attr('title');
//				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
//				$(this)
//					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
//					.after('<span class="select">' + title + '</span>')
//					.change(function(){
//						val = $('option:selected',this).text();
//						$(this).next().text(val);
//						})
//			});

//		};
//		
//	});
    


