function checkForm()

{

  var valid = true;

	for (i=0; i < this.length; i++)

	{

		if ($(this[i]).hasClass("compulsory"))

		{

			var type = this[i].type.toLowerCase();

			if (type == "textarea" || type == "text" || type == "password"){

				if (this[i].value == this[i].defaultValue || this[i].value == "")

				{

				  $(this[i]).addClass("errorField");	

				 	valid = false;

				}

				else

				{

				  $(this[i]).removeClass("errorField");				

				}

			}

			else 

			{

				if (type == "checkbox" && !this[i].checked)

				{

				 	valid = false;

				}

			}

		}

	}

	if(!valid)

	{

		alert('Please fill in required fields.')

	}

	else

	{

		for (i=0; i < this.length; i++)

		{

			var type = this[i].type.toLowerCase();

			if (type == "textarea" || type == "text" || type == "password")

			{

				if(this[i].value == this[i].defaultValue)

				{

					this[i].value = '';

				}

			}

		}		

	}

	return valid;

}



function checkValue ()

{

	if(this.value == this.defaultValue)

	{

		this.value = "";

	}

	else if(this.value=="")

	{

		this.value = this.defaultValue;

	}

}


jQuery.fn.placeholder = function(_options){
  var _options = jQuery.extend({
    className: "placeholded"
  },_options);
  
  return this.each(function(){
    var _hold = $(this);
    var _text = this.defaultValue;
    var _form = _hold.parents('form').eq(0);
    if(!_text)
      return;
    _hold.addClass(_options.className);
    _form.submit(function(){
      if(_hold.hasClass(_options.className))
        _hold.val("");
    });
    _hold.focus(function(){
      if(_hold.hasClass(_options.className))
      {
        _hold.val("");
        _hold.removeClass(_options.className);
      }
    });
    _hold.blur(function(){
      if(_hold.val() == "")
      {
        _hold.val(_text);
        _hold.addClass(_options.className);
      }
    });   
  });
}

$(document).ready(function(){  

  $("#contactForm").bind("submit", checkForm);

	$(".placeholder").placeholder();
	
	/*
  $("#contactForm textarea, #contactForm input[type='text']").each(function(){

    $(this).bind("focus", checkValue);

    $(this).bind("blur", checkValue);

  });

  $("#headerContactForm").bind("submit", checkForm);

  $("#headerContactForm textarea, #headerContactForm input[type='text']").each(function(){

    $(this).bind("focus", checkValue);

    $(this).bind("blur", checkValue);

  });*/
	
	$('#practice-email').attr('href', 'mailto:info@fresh-dental.co.uk');
	$('#practice-email').html('info@fresh-dental.co.uk');

});
