$(document).ready(function(){
	$("#commentSubmit").click( function () {
		var userName = $("#userName").val().replace(/(^\s*)|(\s*$)/g, "");
		var email    = $("#email").val().replace(/(^\s*)|(\s*$)/g, "");
		var title    = $("#title").val().replace(/(^\s*)|(\s*$)/g, "");
		var comment  = $("#comment").val().replace(/(^\s*)|(\s*$)/g, "");
		var auth_code= $("#auth_code").val().replace(/(^\s*)|(\s*$)/g, "");
		var specialCode = $("#specialCode").val();
		
		if(userName == '' || userName == 'YOUR NAME'){
			$("#userNameTip").html('Please fill in your name.');
		}else{
			$.ajax({type: "POST",async:false,url: "/async_commentcheck.php",data: "clientid=userName&userName="+userName, success: 
				function(data){
				    if(data=="true"){
				    	$("#userNameTip").html("");
				    }else{
				    	$("#userNameTip").html("oops! Your name contained a word it shouldn't.  Please try again.");
				    }
				}
			});
		}
		if(email == '' || email == 'YOUR EMAIL'){
			$("#emailTip").html('Please fill in your email.');
		}else{
			var  reg = /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/;  
			if(!reg.test(email)){
			    $("#emailTip").html('Please enter a valid email address.');
			}else{
				$("#emailTip").html('');
			}
		}
		if(title == '' || title == 'TITLE(max 30 characters)'){
			$("#titleTip").html('Please fill in your title.');
		}else if(title.length>30){
			$("#titleTip").html("You've inputted too much Title! Please refine your title to meet the maximum number of characters allowed.(max: 30)");
		}else{
			$.ajax({type: "POST",async:false,url: "/async_commentcheck.php",data: "clientid=commentTitle&commentTitle="+title, success: 
				function(data){
				    if(data=="true"){
				    	$("#titleTip").html("");
				    }else{
				    	$("#titleTip").html("oops! Your title contained a word it shouldn't.  Please try again.");
				    }
				}
			});
		}
		if(comment=='' || comment == 'Enter your comment here ... (max 2,000 characters)'){
			$("#commentTip").html('Please fill in your comment.<br>');
		}else if(comment.length>2000){
			$("#commentTip").html("You've inputted too much comment! Please refine your comment to meet the maximum number of characters allowed.(max: 2000)");
		}else{
			$.ajax({type: "POST",async:false,url: "/async_commentcheck.php",data: "clientid=comment&comment="+comment, success: 
				function(data){
				    if(data=="true"){
				    	$("#commentTip").html("");
				    }else{
				    	$("#commentTip").html("oops! Your comment contained a word it shouldn't.  Please try again.");
				    }
				}
			});
		}
		if(auth_code==''){
			$("#auth_codeTip").html("Please fill in security code.");
		}else{
			$.ajax({type: "POST",async:false,url: "/async_commentcheck.php",data: "clientid=auth_code&auth_code="+auth_code+"&specialCode="+specialCode, success: 
				function(data){
				    if(data=="true"){
				    	$("#auth_codeTip").html("");
				    }else{
				    	$("#auth_codeTip").html("Please enter correct Security Code.");
				    }
				}
			});
		}
		var temp = '';
		$("form .tip").each(function() { temp +=$(this).html();});
		if(temp==''){ $("#form1").submit();}
	}); 
	
	if( str = getCookie('alert')){
		alert(str);
		delCookie('alert');
	}
});


function clear_tips(obj,str)
{
	if(obj.value == str)
	{
		obj.value='';
		have_tips=false;
	}
	$('.userName').parent().html();
}


function getCookie(name)
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
    if(arr != null){
    	var str = unescape(arr[2]);
        str = str.replace(/\+/g," ");
    	return str; 
    }
    return null;
}

function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

function quote(obj)
{
	var str = $("#R"+obj).parent().find(".commentText").html().replace(/\<div class\=(\")*quote(\")*\>/gi,"[quote]");
	str = specialChar(str);
	var usr = specialChar($("#R"+obj).parent().find("h6").html());
	var title = specialChar($("#R"+obj).parent().find("h5").eq(0).html());
	$("#comment").val("[quote]"+usr+"[hr]"+str+"[/quote]\n");
	$("#title").val("Re:"+title);
	$("#userName, #email,#title, #comment").css('color', '#000000');
}

function specialChar(str)
{
	str = str.replace(/\<\/div\>(\<br\>|\n)*/gi,"[/quote]");
	str = str.replace(/(\<br\>)[\n\r]*/gi,"[hr]");
	str = str.replace(/&lt;/gi,"<");
	str = str.replace(/&gt;/gi,">");
	str = str.replace(/&amp;/gi,"&");
	str = str.replace(/&nbsp;/gi," ");
	return str;
}
