$(document).ready(function() {
   $("#postForm").dialog({
	  autoOpen: false,
	  modal: true,
	  //show: 'slide',
	  width:460,
	  height:430
    });

$("#formSendBut").click(function(){
postUrl =$("#commentSrc").attr("value");
name=$("#txtName").attr("value");
comment=$("#txtCommenet").attr("value");
email=$("#txtEmail").attr("value");
title=$("#txtTitle").attr("value");
CommentIN = $("#hfCommentItemNode").attr("value");
CommentGN = $("#hfCommentGalleryNode").attr("value");

    $.ajax({
      url: postUrl ,
      global: false,
      type: "POST",
      data: "txtName="+name+"&txtEmail="+email+"&txtTitle="+title+"&txtComment="+comment+"&CommentItemNode="+CommentIN+"&CommentGalleryNode="+CommentGN,
      dataType: "html",
      beforeSend :function preSubmit(XMLHttpRequest) {
	  if (!validateForm()){return false};
	  $("#commentLoading").css("display", "block");
	  $("#formSendBut").css("display", "none");
      },
      success: function(data, textStatus){
	  $("#output").html(data);
	  $("#output").attr("title", textStatus + "...!");
	  $("#output").dialog({
	    bgiframe: true,
	    modal: true,
	    buttons: {
	      Ok: function() {
		$("#postForm").dialog('close');
	        $(this).dialog('close');
	        }
	    }
	  });
      },
      error:function errorInAjax(XMLHttpRequest, textStatus, errorThrown) {
	alert(textStatus);
	  $("#output").html("There is some error in connection to server...please try again or report this error to webmaster@banafshetalei.com");
	  $("#output").attr("title", textStatus + "...!");
	  $("#output").dialog({
	    bgiframe: true,
	    modal: true,
	    buttons: {
	      Ok: function() {
		$("#postForm").dialog('close');
	        $(this).dialog('close');
	        }
	    }
	  });
      },
      complete:function complete(XMLHttpRequest, textStatus){
	  $("#commentLoading").css("display", "none");
	  $("#formSendBut").css("display", "block");
	  $("#txtName").attr("value","");
	  $("#txtCommenet").attr("value","");
	  $("#txtEmail").attr("value","");
	  $("#txtTitle").attr("value","");
      }
   }); 

});

});
			
function validateForm() {
  var nameValue = $("#txtName").attr("value");
  var commentValue = $("#txtCommenet").attr("value");
  var emailValue = $("#txtEmail").attr("value");

  if (!nameValue || !commentValue) {
    alert('Please enter your name/comment');
    return false;
  };

  if (emailValue){
    if (echeck(emailValue)==false){
      alert('Please enter valid email address. (yourname@yoursite.com)');
      return false;
    };
  };
  return true;
}

function echeck(str) {

var testresults
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
testresults=true
else{
testresults=false
}
return (testresults)

/*
return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

		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*/					
	}
