

$(function()
	{
	
	//make sure send_enquiry is enabled
	$('.send_enquiry').removeAttr('disabled');
	
	/*
	//to colorize input fields - maybe use later-----------------------------------
	function focused()
		{
		//reset inputs, textareas, tds color
		$("input").css({'border-color' : ''});
		$("textarea").css({'border-color' : ''});
		$("form").find("td").css({'color' : ''});
		//set this border colour and text colour
		$(this).css({'border-color' : '#555555'});
		$(this).parent().parent().find('td').css({'color' : '#333333'});
		};
	
	$("input").focus(focused);
	$("textarea").focus(focused);
	$("select").focus(focused);
	
	//end to colorize input fields - maybe use later--------------------------------
	*/
	
	});


//to highlight the textarea
$(function()
	{
	
	});//end function


//to validate the email address (called below)
function echeck(str)
	{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
		{
		return false
		}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		return false
		}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		return false
		}
	 if (str.indexOf(at,(lat+1))!=-1)
		{
		return false
		}
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		{
		return false
		}
	 if (str.indexOf(dot,(lat+2))==-1)
		{
		return false
		}
	 if (str.indexOf(" ")!=-1)
		{
		return false
		}
	 return true
	}


//to check for blank fields and submit the form
$(function()
	{
	//if they click send_enquiry
	$(".send_enquiry").click(function()
		{
		//start by hiding error msgs, so we never see two error msgs at once
	  	$('.error').hide();
	  	
		//get values
		var imie = $("input#imie").val(),
			email = $("input#email").val(),
			telefon = $("input#telefon").val(),
			checkfrom = $("input#checkfrom").val(),
			checkto = $("input#checkto").val(),
			adults = $("input#adults").val(),
			kids = $("input#kids").val(),
			tresc = $("textarea#tresc").val(),
			id = $("input#id").val(),
			property = $("input#property").val();
			code = $("input#ccode").val();
		
		//put field names in array
		var fn = new Array('imie', 'email', 'telefon', 'checkfrom', 'checkto', 'adults', 'kids', 'tresc','ccode');
		
		//put field values in array
		var fv = new Array(imie, email, telefon, checkfrom, checkto, adults, kids, tresc,code);
		
		//if name is blank
		if(imie == "")
			{
			$("label#imie_error").show();
			$("input#imie").focus();
			return false;
			}
		
		//if email and phone are blank
		if(email == "" && telefon == "")
			{
			$("label#email_error").show();
			$("input#email").focus();
			return false;
			}
		
		//if email is not blank, make sure it's valid
		if(email != "")
			{
			if(echeck(email)!=true)
				{
				$("label#email_error2").show();
				$("input#email").focus();
				return false;
				}
			}

		
		var dataString = 'imie=' + imie + '&email=' + email + '&telefon=' + telefon + '&checkfrom=' + checkfrom + '&checkto=' + checkto + '&adults=' + adults + '&kids=' + kids + '&tresc=' + tresc + '&id=' + id + '&property=' + property + '&ccode=' + code ;
		
		//alert (dataString); return false;

		MM_swapImage('send_enquiry','','/images/buttons/sending_enquiry.png',1);
		$.ajax(
			{
			type: "POST",
			url: "/search/msg.php", // don't add http here, otherwise problems appear when on http://www vs just http://
			data: dataString,
			success: function()
				{
				$('#messageHolder').html("<div id='message'></div>");
				$('#message').html("<strong>Your enquiry has been sent</strong>")
				.append("")
				.hide()
				.fadeIn(1500, function()
					{
					$('#message').append("");
					}); //end fade in
				} //end success
			}); //end .ajax
			//$(this).fadeOut("slow");
			//$(".send_enquiry").fadeOut(1500);
			$(".send_enquiry").attr("disabled","disabled"); //this is countered above in this js file with $('.send_enquiry').removeAttr('disabled');
			
			MM_swapImage('send_enquiry','','/images/buttons/sent.png',1);
			return false; //so page doesn't reload


		}); //end button.click function

	}); //end function




