$(function() {
		   $('input[title!=""]').hint();
		$.fn.clearForm = function() {
		  return this.each(function() {
		    var type = this.type, tag = this.tagName.toLowerCase();
		    if (tag == 'form')
		      return $(':input',this).clearForm();
		    if (type == 'text' || type == 'password' || tag == 'textarea')
		      this.value = '';
		    else if (type == 'checkbox' || type == 'radio')
		      this.checked = false;
		    else if (tag == 'select')
		      this.selectedIndex = -1;
			  this.selectedIndex = 0;
		  });
		
		};
		$('#enquiry').click(function () {		
		
		//Get the data from all the fields

		var ename = $('input[name=name]').val();
		var ephone = $('input[name=phone]').val();
		var eemail = $('input[name=email]').val();
		var emessage = $('input[name=message]').val();
		var at="@"
		var dot="."
		var lat=eemail.indexOf(at)
		var leemail=eemail.length
		var ldot=eemail.indexOf(dot)
		if (ename=='Enter Your Name') {
			alert('Please input your name');
			$('input[name=name]').focus()
			return false;
		}
		if (eemail=='Enter Email') {
			alert('Please input your email');
			$('input[name=email]').focus();
			return false;
		}
		if (eemail.indexOf(at)==-1){
		   alert("Invalid E-mail ID");
		   $('input[name=email]').focus();
		   return false;
		}

		if (eemail.indexOf(at)==-1 || eemail.indexOf(at)==0 || eemail.indexOf(at)==leemail){
		   alert("Invalid E-mail ID");
		   $('input[name=email]').focus();
		   return false;
		}

		if (eemail.indexOf(dot)==-1 || eemail.indexOf(dot)==0 || eemail.indexOf(dot)==leemail){
		    alert("Invalid E-mail ID");
			$('input[name=email]').focus();
		    return false;
		}

		 if (eemail.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID");
			$('input[name=email]').focus();
		    return false;
		 }

		 if (eemail.substring(lat-1,lat)==dot || eemail.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID");
			$('input[name=email]').focus();
		    return false;
		 }

		 if (eemail.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID");
			$('input[name=email]').focus();
		    return false;
		 }
		
		 if (eemail.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID");
			$('input[name=email]').focus();
		    return false;
		 }
		if (ephone=='Enter Phone No') {
			alert('Please input your phone no.');
			$('input[name=phone]').focus();
			return false;
		}
		
		if (emessage=='Message') {
			alert('Please input Message');
			$('input[name=message]').focus();
			return false;
		}	
		
		//organize the data properly
		var data = 'name=' + encodeURIComponent(ename)+'&phone=' + encodeURIComponent(ephone) +'&email=' + encodeURIComponent(eemail) + '&message=' + encodeURIComponent(emessage);
		//alert(data);
		//disabled all the text fields
		//$('.text').attr('disabled','true');
		
		//show the loading sign
		//$('.loading').show();
		
		//start the ajax
		$.ajax({
			//this is the php file that processes the data and send mail
			url: "message.php",	
			
			//GET method is used
			type: "post",

			//pass the data			
			data: data,		
			
			//Do not cache the page
			cache: false,
			
			//success
			success: function (html) {		
			//alert(html);		
						/*alert(html);*/
						if(html==1)
							alert('Your message has been sent Sucessfully');
						else alert('Sorry, unexpected error. Please try again again.');	 
			}		
		});
		//clearing all form data
		$('#enquiryform').clearForm();//$('#commercialform').reset();
		//cancel the submit button default behaviours
		$('input[title!=""]').hint();
		return false;
	});	
		
	});



