$(document).ready(function() {
  /*
  $("#frmcomment").dialog({
	  	width: 565,
		height: 330,
		modal: true,
		autoOpen: true,
		resizable: false,
		title: "Enter your comment"
	});
	*/
});

function openwindow(url)
{
	window.open(url,"fb", "menubar=1,resizable=1,width=550,height=450");
}

function resetForm(id) {
	$('#'+id).each(function(){
	        this.reset();
	});
}

function selectType(index)
{
	document.frmSubmit1.opt_type[0].checked = false;
	document.frmSubmit1.opt_type[1].checked = false;
	document.frmSubmit1.opt_type[2].checked = false;
	document.frmSubmit1.opt_type[index].checked = true;
	
	//hide and reset all form
	$("#frmSubmitText").css("display","none");
	$("#frmSubmitPic").css("display","none");
	$("#frmSubmitFML").css("display","none");
	
	$("#frmSubmitText_error").css("display","none");
	$("#frmSubmitPic_error").css("display","none");
	$("#frmSubmitFML_error").css("display","none");
	
	resetForm('frmSubmitText');
	resetForm('frmSubmitPic');
	resetForm('frmSubmitFML');
	
	if(index == 0)
	{
		$("#frmSubmitText").css("display","inline");
	}
	else if(index == 1)
	{
		$("#frmSubmitPic").css("display","inline");
	}
	else if(index == 2)
	{
		$("#frmSubmitFML").css("display","inline");
	}
}

function SubmitTextBegin()
{
	$("#loading_text").css("display","inline");
	return true;
}

function SubmitTextComplete(response)
{
	$("#loading_text").css("display","none");
	
	var response_arr = response.split("|");
	
	if(response_arr[0] == "1")
	{
		$("#frmSubmitText_error").css("display","none");
		$("#frmSubmitText").css("display","none");
		$("#select_type_box").css("display","none");
		$("#submitted_box").css("display","inline");
		$("#submitted_box").html('<strong>Your message has been submitted to EFLN. However, your message is still under moderation. We will display your message shortly. Thank you.</strong>');
	}
	else
	{
		$("#frmSubmitText_error").css("display","inline");
		$("#frmSubmitText_error").html(response_arr[1]);
	}
}

function SubmitPicBegin()
{
	$("#loading_pic").css("display","inline");
	return true;
}

function SubmitPicComplete(response)
{
	$("#loading_pic").css("display","none");
	
	var response_arr = response.split("|");
	
	if(response_arr[0] == "1")
	{
		$("#frmSubmitPic_error").css("display","none");
		$("#frmSubmitPic").css("display","none");
		$("#select_type_box").css("display","none");
		$("#submitted_box").css("display","inline");
		$("#submitted_box").html('<strong>Your message has been submitted to EFLN. However, your message is still under moderation. We will display your message shortly. Thank you.</strong>');
	}
	else
	{
		$("#frmSubmitPic_error").css("display","inline");
		$("#frmSubmitPic_error").html(response_arr[1]);
	}
}

function SubmitFMLBegin()
{
	$("#loading_fml").css("display","inline");
	return true;
}

function SubmitFMLComplete(response)
{
	$("#loading_fml").css("display","none");
	
	var response_arr = response.split("|");
	
	if(response_arr[0] == "1")
	{
		$("#frmSubmitFML_error").css("display","none");
		$("#frmSubmitFML").css("display","none");
		$("#select_type_box").css("display","none");
		$("#submitted_box").css("display","inline");
		$("#submitted_box").html('<strong>Your message has been submitted to EFLN. However, your message is still under moderation. We will display your message shortly. Thank you.</strong>');
	}
	else
	{
		$("#frmSubmitFML_error").css("display","inline");
		$("#frmSubmitFML_error").html(response_arr[1]);
	}
}

function appendItem(target_id, html_code, new_id)
{	
	$(target_id).append(html_code);
	$(new_id).fadeIn('slow', function() {});
}

function checkEmail(inputvalue){	
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)){         
		return true;
    }else{   
		return false;
    }
}

function resetForm(ele) 
{
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}

function SubmitComment()
{
	$('.error_message').css("display", "none");
	
	var error = "false";
	
	//do form validation here
	if($.trim($('#comment_nick').val()) == '')
	{
		$('#nick_e').css("display", "inline");
		error = "true";
	}
	
	if($.trim($('#comment_content').val()) == '')
	{
		$('#comment_e').css("display", "inline");
		error = "true";
	}
	
	if($.trim($('#comment_email').val()) != '')
	{
		if(!checkEmail($.trim($('#comment_email').val())))
		{
			$('#email_e').css("display", "inline");
			error = "true";
		}
	}
	
	if(error == "true")
	{
		return false;
	}
	
	$('#loading_post_comment').css("display", "inline");
	
	var form_data = $("#submit_comment").serialize();
	
	$.ajax({
	  url: web_path + 'comment_submit.php',
	  dataType: 'json',
	  data: form_data,
	  success: function(data) {
		  //alert(data.new_id);
		  appendItem(data.target_id, data.content, data.new_id);
		  $('#loading_post_comment').css("display", "none");
		  resetForm("#submit_comment");
	  }
	});
	
	return false;
}

function vote_post2(post_id, vote_val)
{
	var vote_type = 'up';
	var ctrl_name = '#vote_good_id_';
	var inner_text = 'I agree, your life sucks';
	
	if(vote_val == 0)
	{
		var vote_type = 'down';
		var ctrl_name = '#vote_bad_id_';
		var inner_text = 'You totally deserved it';
	}
	
	$.ajax({
	  url: web_path + 'vote_submit.php',
	  type: "POST",
	  dataType: 'json',
	  data: 'post_id=' + post_id + '&vote_type=' + vote_type,
	  success: function(data) {
		  $(ctrl_name + post_id).html('<div class="btn_img"></div><a>' + inner_text + ' (' + data.return_total_vote + ')</a>');
		  $(ctrl_name + post_id + ' a').click(function() {
			  vote_post2(post_id, vote_val);
		  });
	  }
	});
}

function vote_post(post_id, vote_val)
{
	var vote_type = 'up';
	var ctrl_name = '#vote_good_id_';
	var inner_text = 'Good Night';
	
	if(vote_val == 0)
	{
		var vote_type = 'down';
		var ctrl_name = '#vote_bad_id_';
		var inner_text = 'Bad Night';
	}
	
	$.ajax({
	  url: web_path + 'vote_submit.php',
	  type: "POST",
	  dataType: 'json',
	  data: 'post_id=' + post_id + '&vote_type=' + vote_type,
	  success: function(data) {
		  $(ctrl_name + post_id).html('<div class="btn_img"></div><a>' + inner_text + ' (' + data.return_total_vote + ')</a>');
		  $(ctrl_name + post_id + ' a').click(function() {
			  vote_post(post_id, vote_val);
		  });
	  }
	});
}









