jQuery(function() {
  jQuery('.error').hide();
  jQuery(".bton").click(function() {
		// validate and process form
		// first hide any error messages
    jQuery('.error').hide();
	
    var name = jQuery("input#name").val();
		if (name == "") {
      jQuery("span#name_error").show();
      jQuery("input#name").focus();
      return false;
    }
	var lname = jQuery("input#lname").val();
	var hphone = jQuery("input#hphone").val();
	
	var numericExpression = /^[0-9]+$/;
	    /* if(!numericExpression.test(hphone)){
	  jQuery("span#hphone_error").show();
      jQuery("input#hphone").focus();
      return false;  
	}
	*/
	var to = jQuery("input#to").val();
	
	var from = jQuery("input#from").val();
	     if (from == "") {
      jQuery("span#from_error").show();
      jQuery("input#from").focus();
      return false;
    }
	
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	     if(!emailReg.test(from)) {
	   jQuery("span#from_error2").show();
       jQuery("input#from").focus();
       return false;
	}
	
	var msg = jQuery("textarea#msg").val();
	  if (msg == "") {
	  jQuery("span#msg_error").show();
	  jQuery("textarea#msg").focus();
	  return false;
    }
	
	//var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
	function htmltoentity(msg){
	   var formatted = msg.replace(/(<)|(>)/g,
         function (thematch){
		   if(thematch == "<") 
		     return "&#60;";
           else
             return "&#62;" })	   
	   }
/*	
//check for alphanumeric and spaces only	
    var alphaExp = /^(\w+\s+)&/;
	     if(!alphaExp.test(msg)) {
	   jQuery("span#msg_error2").show();
	   jQuery("textarea#msg").focus();
	   return false;
	   }
//Check and replace html special chars
  
	var alphaExp = /(\w+\s+)/;
	     if(!alphaExp.test(msg)) {
	   jQuery("span#msg_error2").show();
	   msg == "";
	   jQuery("textarea#msg").focus();
	   return false;
	   }*/

	var dataString = $("#frmcontactus form").serialize();
		//var dataString = 'forename='+ forename +'&surname=' + surname + '&address=' + address + '&address2=' + address2 +'&region=' + region +'&city=' + city +'&postcode=' + postcode +'&hphone=' + hphone +'&to='+ to +'&fromemail='+ fromemail + '&subj=' + subj + '&msg=' + msg;
		//alert (dataString);return false;
		
	  jQuery.ajax({
      type: "POST",
      url: "sendmail.php",
      data: dataString,
      success: function() {
        jQuery('#contact').html("<div id='message'></div>");
        jQuery('#message').html("<strong>&#381;inut&#279; pasi&#371;sta.</strong>")
        .append("<p>&#260;&#269;i&#363; u&#382; J&#363;s&#371; d&#279;mes&#303; &#33;</p>")
        .hide()
        .fadeIn(1500, function() {
          jQuery('#message');
        });
      }
     });
    return false;
	});
});
