/**
 * @author Publicis Modem & Dialog
 */

/*
 * form handling
 */

$(document).ready(setForms);
var commentWordCount = 1000;
var feedbackWordCount = 200;
function setForms(){
	$(".feedback .count").text(feedbackWordCount);
	$(".feedback>textarea").one("focus", clearField);
	$(".feedback>textarea").one("focus", countdown);
	$("#comments-form .count").text(commentWordCount);
	$("#comments-form textarea").one("focus", clearField);
	$("#comments-form textarea").one("focus", countdown);
	updatecount($(".feedback>textarea"), feedbackWordCount);
	updatecount($("#comments-form textarea"),commentWordCount);
	$(".why-link").bind('mouseover', showWindow);
	$("#feedback-form").bind('submit', validateForm);
	$("#comments-form").bind('submit', validateForm);
	$("#comments-form textarea").data('initval', $("#comments-form textarea").val() != "" ? $("#comments-form textarea").val() : "Please enter your comments" );
	$(".feedback>textarea").data('initval', $(".feedback>textarea").val()!= "" ? $(".feedback>textarea").val() : "Please enter your comments");
}
function clearField(){
	if($(this).text().indexOf("Enter your question") != -1){
		$(this).text("");
	}
	$(this).bind("keyup", countdown);
	$(this).bind("focus", countdown);
	$(this).bind("blur", function(){
		$(this).unbind("keyup", countdown)
		$(this).bind("focus", function(){
			$(this).bind("keyup", countdown);
		})
	});
}
function showWindow(){
	$(this).children(".info-box").show();
	$(this).children(".info-box").css("position", "absolute");
	$(this).bind('mouseout', hideWindow);
}
function hideWindow(){
	$(this).children(".info-box").hide();
	$(this).unbind('mouseout', hideWindow);
}
function validateForm(){
	var output = true;
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var emailVal = $(this).find('.email').val()
	if ( (!emailPattern.test(emailVal) || emailVal.length < 1) && $(this).find('.email') ){
		$(this).find(".email-label").text("Email Address") 
		$(this).find(".email-label").append(" <em>Email is required</em>");
		output = false;
	}else{
		$(this).find(".email-label").text("Email Address");
	}
	if ($(this).find('.author').val().length < 1 && $(this).find('.author') ){
		$(this).find(".author-label").text("Name or Nickname")  
		$(this).find(".author-label").append(" <em>Name is required</em>");
		output = false;
	}else{
		$(this).find(".author-label").text("Name or Nickname");
	}
	/*if (($(this).find('textarea').val().length < 1 || $(this).find('.text').val() == $(this).find('textarea').data('initval')) && $(this).find("textarea")){
		$(this).find("textarea").val($(this).find("textarea").data('initval'));
		$(this).find("textarea").css("color","#FF0000");
		$(this).find("textarea").bind('focus',resetColor);
		$(this).find("textarea").bind('focus',clearField);
		output = false;
	}*/
	
	if(!output){
		$(this).find("#comment-submit").removeAttr("disabled");
      	$(this).find("#comment-preview").removeAttr("disabled");
		return false;
	} else{
		mtRequestSubmitted = false;
		return mtCommentOnSubmit($(this).get(0));
	}
}
function countdown(){
	var initCount = $(".feedback>textarea").parent().attr("class") == $(this).parent().attr("class") ? feedbackWordCount : commentWordCount;
	updatecount($(this), initCount)
}
function updatecount($field, $limit){
	try {
		if($limit <= $field.val().length){
			$field.val($field.val().substring(0, $limit));
		}
		$field.parent().find(".count").text(($limit - $field.val().length) + " ");
	
	}catch($err){}
}
function resetColor(){
	$(this).css('color', 'inherit');
	$(this).unbind('focus',resetColor);
}

