//---------------------------------------------
var IncorrectMessage = "";
//---------------------------------------------

//---------------------------------------------
$(document).ready(function() {
	$(".GeneralForm .FieldCheckBox .CheckBox .NotChecked").click(function() {
		$(this).css("display", "none");
		$(this).siblings(".FieldHiddenSendMeMail:hidden").val("true");
		$(this).siblings(".Checked").css("display", "block");
	});
	$(".GeneralForm .FieldCheckBox .CheckBox .Checked").click(function() {
		$(this).css("display", "none");
		$(this).siblings(".FieldHiddenSendMeMail:hidden").val("false");
		$(this).siblings(".NotChecked").css("display", "block");
	});
	
	IncorrectMessage = $(".IncorrectInputMessage").html();
	
	$("input").each(function() {
		$(this).attr("onkeypress", "return(disableEnterKey(event));");
	});
	
}); 
//---------------------------------------------

//---------------------------------------------
function SubmitContactForm(formID)
{
	var theForm = $("#" + formID);
	var passed = true;
	
	$(".FormField").each(function() { //alert(
		if(!CheckInput(this.id, theForm))
		{
			passed = false;
			$("input", theForm).css("background-color", "#6E6E6E");
			$("input[id*='" + this.id + "']", theForm).css("background-color", "#292929");
			var fieldName = $("label span", $(this).parent()).html();
			$(".IncorrectInputMessage").html(IncorrectMessage.replace("*", fieldName));
			$(".IncorrectInputMessage").toggle(true);
			return(false);
		}
	});
	
	if(passed)
	{
		$("input", theForm).css("background-color", "#6E6E6E");
		$(".IncorrectInputMessage").toggle(false);
	}
	return(passed);
}
//---------------------------------------------

//---------------------------------------------
function CheckInput(inputId, theForm)
{
	var field = $("input[id*='" + inputId + "']", theForm);
	
	if(inputId.search("FieldEmail") > -1)
	{
		if(!IsValidEmail($(field).attr("value")))
		{
			field.focus();
			field.select();
			return(false);
		}
	}
	else if(inputId.search("FieldFirstName") > -1  || inputId.search("FieldLastName") > -1)
	{
		if($(field).val().length < 2 || !IsEnglish($(field).val()))
		{
			field.focus();
			field.select();
			return(false);
		}
	}
	else if(inputId.search("FieldPhone") > -1 || inputId.search("FieldCellPhone") > -1)
	{
		if(!IsNumeric($(field).val()) && $(field).val() != "")
		{
			field.focus();
			field.select();
			return(false);
		}
	}
	else if(inputId.search("FieldRoleCompany") > -1  || inputId.search("FieldCompany") > -1)
	{
		if(!IsEnglish($(field).val()))
		{
			field.focus();
			field.select();
			return(false);			
		}
	}
	
	if( $(field).hasClass("IsMustField") && $(field).attr("value") == "" )
	{
		field.focus();
		field.select();
		return(false);
	}

	return(true);
}
//---------------------------------------------

//---------------------------------------------
function IsValidEmail(email)
{
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (!filter.test(email))
		return(false);
	return(true);
}
//---------------------------------------------

//---------------------------------------------
function IsNumeric(strString)
//  check for valid numeric strings	
{
	var strValidChars = "0123456789_-.,'+();: ";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		blnResult = false;
	}
	
	return blnResult;
}
//---------------------------------------------

//---------------------------------------------
function IsEnglish(strString)
{
	var filter = /^([A-Za-z0-9_\-\.\,\'\+\(\)\;\:\s])*$/;
	if (!filter.test(strString))
		return(false);
	return(true);
}
//---------------------------------------------

//---------------------------------------------
function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}
//---------------------------------------------
