/*
	Todo (Mor butbul?) :
	 	* Use classes, not ID's!

 */

$(function () {

	var context = $("#contact_form");

	var white_color = '#fff';
	var black_color = '#000';
	var gray_color = '#afaeae';
	var red_color = '#f00';


	// set all input.text default value according to alt attribute
	$("input[type='text']", context).each(function(){
	  $(this).val($(this).attr("alt"));
	});

	// clear input.text on focus, if still in default
	$("input[type='text']", context).focus(function() {
		$(this).css('background-color',white_color);
		$(this).css('color',black_color);
	  if($(this).val()==$(this).attr("alt")) {
		$(this).val("").css('color',black_color);
	  }
	});

	// if field is empty afterward, add text again
	$("input[type='text']", context).blur(function() {
	  if($(this).val()=="") {
		$(this).val($(this).attr("alt")).css('color',gray_color);
	  }
	});

	$("#contact_form").live('submit',function () {
		check_updates_confirmation();

		return false;
	});

	function submit_contact_form()
	{
		var this_form = context;
		// Clear default values
		clear_defaults(this_form.attr('id'));

		// Serialize form inputs
		var inputs_vals = this_form.serialize();

		// Set submit url
		var url = baseUrl() + "/submit_contact.php";

		// Call the url
		$.post(url,inputs_vals,function(data){
			$("#form_submit_info").text("").css('display','none');
			if (data.error)
			{
				$.each(data.fields, function(i,xitem){
					var fieldname = "#" + xitem.fieldname;
					var bgcolor = white_color;
					if (!xitem.fieldvalue)
					{
						bgcolor = red_color;
					}

					$(fieldname).css('background-color',bgcolor);
				});
			}
			$("#form_submit_info").text(data.submit_info).css('display','block');
		},"json");
	}

	function clear_defaults(form_id)
	{
		$("#" + form_id).find("input[type='text']", context).each(function() {
			if ($(this).val() == $(this).attr('alt')){
				$(this).val('');
			}
		});
	}

	function check_updates_confirmation()
	{
		var confirmation_input = $("input[name=confirmation]", context);
		if (!confirmation_input.is(":checked"))
		{
			jConfirm("האם את מעויינת לקבל עדכונים, הצעות והטבות במייל או SMS?","קבלת עדכונים",function(r) {
				if (r)
				{
					confirmation_input.attr('checked','true');
				}
			});
		}

		submit_contact_form();
	}

	$("input.phone", context).keydown(function(event) {
        // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 ) {
                // let it happen, don't do anything
        }
        else {
                // Ensure that it is a number and stop the keypress
                if (event.keyCode < 48 || (event.keyCode > 57 && event.keyCode < 96) || event.keyCode > 105 ) {
                        event.preventDefault();
                }
        }
    });

	$("input.fullname", context).keydown(function(event) {
        // Allow only backspace and delete
        if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 ) {
                // let it happen, don't do anything
        }
        else {
                // Ensure that it is a number and stop the keypress
                if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) ) {
                        event.preventDefault();
                }
        }
    });

});
