// JavaScript Document

$(document).ready(function() {
	
	
	//polling
	$("form#frm_polling").submit(function(){
		var radio_checked	=	$("form#frm_polling").find("input[type=radio]:checked");
		var answer			=	radio_checked.attr("value");
		var question		=	$("form#frm_polling input#txt_question").val();
		if(parseInt(answer)>0)
		{
			$.ajax({
			type: "POST",
			url: "bin/visitor_polling.php",
			data: "qid="+question+"&aid="+answer,
			success: function(msg){
				alert(msg );
			}
			});
		}
		else
		{
			alert("Select Answer.");
		}

		
	return false;
	});
	
	//rashi
	$("select#sel_rashi").change(function(){
		
		var rashi_id	=	$("select#sel_rashi").val();
		if(parseInt(rashi_id)>0)
		{
			$.ajax({
			type: "POST",
			url: "bin/rashi.php",
			data: "rid="+rashi_id,
			ajaxStart:$("p#show_rashi").html("loading..."),
			success: function(msg){
				if(msg==0)
				{
					alert("Rashi content not found");
				}
				else
				{
					$("p#show_rashi").html(msg );
				}
			}
			});
		}
		
		return false;
		
		});
		
	//comment
	$("form#frmComment").submit(function(){
		var name		=	$("form#frmComment input#name").val();
		var email		=	$("form#frmComment input#email").val();
		var comment		=	$("form#frmComment textarea#comment").val();
//data: "qid="+question+"&aid="+answer,
			$.ajax({
			type: "POST",
			url: "bin/news_comment.php",
			ajaxStart:$("form#frmComment #loading").html("please wait..."),
			data: $("form#frmComment").serialize(),
			success: function(msg){
				$("form#frmComment #loading").html("")
				if(msg==1)
				{
					$("form#frmComment").find("input#name").val("");
					$("form#frmComment").find("input#email").val("");
					$("form#frmComment").find("textarea#comment").val("");
					$("form#frmComment").find("input#code").val("");
					document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();
					alert("Thanks for your comment");
					
				}
				if(msg!=1)
				{
					alert(msg );
				}
				
			}
			});
		

		
	return false;
	});
	
	
	//Editorial comment
	$("form#frmEditorialComment").submit(function(){
		var name		=	$("form#frmEditorialComment input#name").val();
		var email		=	$("form#frmEditorialComment input#email").val();
		var comment		=	$("form#frmEditorialComment testarea#comment").val();
//data: "qid="+question+"&aid="+answer,
			$.ajax({
			type: "POST",
			url: "bin/editorial_comment.php",
			ajaxStart:$("form#frmEditorialComment #loading").html("please wait..."),
			data: $("form#frmEditorialComment").serialize(),
			success: function(msg){
				$("form#frmEditorialComment #loading").html("")
				if(msg==1)
				{
					document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();
					alert("Thanks for your comment");
					
				}
				if(msg!=1)
				{
					alert(msg );
				}
				
			}
			});
		

		
	return false;
	});
	
	
	//Send us news
	$("form#frm_send_us_news").submit(function(){
		
		var error			=	"";
		var name			=	$("input#name").val();
		var email			=	$("input#email").val();
		var phone			=	$("input#phone").val();
		var comment			=	$("textarea#comment").val();
		var rege = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(name=="")
		{
			error+="Enter name.\n"
		}
		
		if(email=="")
		{
			error+="Enter Email.\n"
		}
		else
		{
			if(!rege.test(email))
			{
				error+="Enter Valid Email.\n"
			}
		}
		
		if(phone=="")
		{
			error+="Enter Phone.\n"
		}
		
		if(error!="")
		{
			alert(error);
			return false;
		}
		else
		{
			return true;
		}
	});

});

function reloadCaptcha()
	{
		document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();    
		document.getElementById('code').focus();
	}

