$(document).ready(function() {
	$('#frmLeadBox .lead-submit').click(function(){
		 
		var fromObj = $(this).parent();
			
		if (!checkLeadForm(fromObj))
		{
			//alert("יש למלא את כל פרטי הטופס");
		}
		else
		{
			var serial = $(fromObj).serialize();
			
			$.get("index.php?module=Listings&pname=sendLead&pmode=empg&carID=" + $(fromObj).attr('leadID'), serial,function(msg){
				if (msg == "true")
				{
					fromObj.html('ההודעה נשלחה בהצלחה! הסוכנות תיצור איתך קשר בהקדם.');
					
					MyAlert("<h1>הפנייה נשלחה!</h1><h2>הסוכנות תיצור איתך קשר בהקדם.</h2><iframe style='border:none; width:1px; height:1px;' src='index.php?module=Listings&pname=leadThanx&template=Blank&carID=" + fromObj.attr('leadID') + "'></iframe>");
					
					
				}
				else
					alert("הפנייה לא נשלחה, אנא וודא שהפרטים שהזנת מלאים ובעברית.");
			});
		}
		return false;
		
	});
});

jQuery.fn.inputVal = function(title) {


	$(this).attr('firstText', title);
	$(this).val(title);
	
	/*
	divHTML = "<div style='width:" + $(this).width() + "px; height:" + $(this).height() + "px; font-size:" + $(this).css('fontSize') + "px'>" + title + "</div>";
	
	$(this).outerHTML($(this).outerHTML() + divHTML);
	*/
	
	$(this).click(function(){
		
		if ($(this).attr('firstText') == $(this).val())
		{
			$(this).val('');
		}
	});
	
	$(this).blur(function(){
		
		if ($(this).attr('firstText') != '' && $(this).val() == '')
		{
			$(this).val($(this).attr('firstText'));
		}
		else if ($(this).attr('firstText') != '' && $(this).val() != '')
		{
			$(this).attr('firstText', '');
		}
	});

};

$(document).ready(function() {
	

	$('.leadFooter').each(function(index){

		$(this).find('input[type=text]').each(function(i){
			$(this).inputVal($(this).val());

		});

	
	});
	

	//$('.leadFooter input').css('zIndex', '99999999999999999999');
	//$('.leadBoxBranch').sSelect({ddMaxHeight: '250px'});
});

function checkLeadForm(frmObj)
{
	var retVal = true;
	
	$(frmObj).find("input[type=text], select").each(function(i){
		
		if (retVal)
		{
			if ($(this).attr('name') == "branch")
			{
				if ($(this).val() == "")
				{
					alert("אנא בחר סניף");
					//email.focus
					retVal = false;
					return;
				}
			}
			if ($(this).attr('name') == "phone")
			{ phoneVal = $(this).val();
				if (phoneVal.length < 9 || phoneVal.length > 10)
				{
					alert("מספר הטלפון שהזנת אינו תקין");
					//email.focus
					retVal = false;
					return;
				}
			}
			else if ($(this).attr('name') == "email")
			{
				var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!filter.test($(this).val()))
				{
					alert("כתובת המייל שהזנת אינה תקינה");
					//email.focus
					retVal = false;
					return;
				}
			}
			else if ($(this).attr('name') == "name")
			{
				nameVal = $(this).val();
				
				if ($(this).val() == "" || $(this).val() == $(this).attr('firstText'))
				{
					alert("יש למלא את השדה: " + $(this).attr('firstText'));
					retVal = false;
					return;
				}
				else if (nameVal.length < 2)
				{
					alert("יש למלא שם באורך של יותר מ2 אותיות");
					retVal = false;
					return;
				}
				
				var filter = /^([א-ת\.\s])+$/;
				if (!filter.test($(this).val()))
				{
					alert("יש להכניס שם בעברית");
					//email.focus
					retVal = false;
					return;
				}
			}
			else if ($(this).attr('name') == "family")
			{
				famVal = $(this).val();

				if ($(this).val() == "" || $(this).val() == $(this).attr('firstText'))
				{
					alert("יש למלא את השדה: " + $(this).attr('firstText'));
					retVal = false;
					return;
				}
				else if (famVal.length < 2)
				{
					alert("יש למלא שם משפחה באורך של יותר מ2 אותיות");
					retVal = false;
					return;
				}
				
				var filter = /^([א-ת\.\s])+$/;
				if (!filter.test($(this).val()))
				{
					alert("יש להכניס שם משפחה בעברית");
					//email.focus
					retVal = false;
					return;
				}
			}
			else if ($(this).val() == "" || $(this).val() == $(this).attr('firstText'))
			{
				alert("יש למלא את השדה: " + $(this).attr('firstText'));
				retVal = false;
				return;
			}
		}
	});
	
	return (retVal);
}
