// JavaScript Document
var url_home = "http://www.myfreeonlinerecipe.com/";
//var url_home = "http://localhost/myfreeonlinerecipe.com/";

function rempass(){
	var un= $("#username").val();	
	var params = 'action=fetchrempass';
	params += "&un="+un;
	$.ajax({
		url : url_home+'rempass.php?'+params,
		success : function (data) {
			if(data!="") {
				$("#password").val(data);
				$("#rem").attr('checked', true);
			}
		}
	});
}
function gotoPage(url) {
	document.location.href = url_home+url;
}
function gotoURL(url) {
	document.location.href = url;
}
//FOR TABS
$(document).ready(function() {
	//When page loads...
	$("#tooltip").click(function(){this.hide()});
	
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content
	//On Click Event
	$("ul.tabs li").click(function() {
		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
/********** USER PAGE ***********/
	
	$("#opt_sel").click(function () {
		$("#writer-area").hide('fast');
		$("#select-art-area").show();
	});
	$("#cancel-edit-article").click(function () {
		$("#writer-area").hide('fast');
		$("#select-art-area").show();
	});
	
});
function addArticle(sel,catid,catname) {
	$("#art-option").hide();
	$("#select-art-area").hide();
	$("#writer-area").show();
	var artdef = $("#"+sel+" td div.artdef").text();
	$("#art_def").text(artdef);
	$("#cat_id").val(catid);
	$("#cat_name").text(catname);
}
var cnt = 0;

function showArticle(artid,type) {
	
	$(document).ready(function(){
	$.post("ArticleAjax.php",{suggest:artid, art_type:type},function(result){
	  $("#art_details").html(result);
	  $("#art_details").show();
	  $("#articles_div").hide('fast');
	});
	});	
}
function hideArticleDetail() {
	$("#art_details").hide('fast');
	$("#articles_div").show();
}
function showPostAnswer(divid,qid){
	var qns = $("#"+divid+" p.qns").text();
	$("#give-answer").show();
	$("#qns").text( $("#"+divid+" p.qns").text() );
	$("input[name=qnsid]").val(qid);
}

function showAnswer(divid){
	var currentId = $(divid).attr('id');
	var newdiv = currentId+"_div";
	$("#"+newdiv).slideToggle();
}
/********************/
function fillURL(txtvalue,txtfld){
	var nstr = "";
	for(var i=0;i<txtvalue.length;i++) {		
		var iChars = "!@#$%^&*()+=[]\\\';,/{}|\":<>";
		if (iChars.indexOf(txtvalue.charAt(i)) != -1) {
			nstr += '-';
		}
		else if(txtvalue[i]==" ") {
			nstr += '-';
		}
		else {
			nstr += txtvalue[i];
		}
	}
	document.getElementById(txtfld).value = nstr.toLowerCase();
}

function printPartOfPage(elementId) {
	var printContent = document.getElementById(elementId);
	var windowUrl = 'print_article.php';
	var uniqueName = new Date();
	var windowName = 'Print' + uniqueName.getTime();
	var printWindow = window.open(windowUrl, windowName, 'left=50000,top=50000,width=0,height=0'); 
	printWindow.document.write(printContent.innerHTML);
	printWindow.document.close();
	printWindow.focus();
	printWindow.print();
	printWindow.close();
}
function checkandgo(userid) {
	var dataString = "act=chkuser&uid="+userid;
	var msg = '<img src="'+url_home+'images/loading.gif" />';
	$("#result01").html(msg);
	$.ajax({
		type: "POST",
		url: url_home+"userAjax.php",
		data: dataString,
		success: function(result) {
			if(result=="1") {
				msg = "Your account is not activated. Please check your email and activate your account.";
			}
			if(result=="2") {
				//showLoginBox(logtype);
				gotoPage("user.php");
			}
			else if(result=="3") {
				msg="";
			}
			
			else if(result=="4") {
				;
			}
			$("#result01").html(msg);
		}
	});
}
function validateAskAnExpert(formid) {
	var flag = 0;	
	
	if(!valid_required(formid.qns.value))	{	
		$("#qns_res").html("Please enter your question !");
		formid.qns.focus();
		return false;
	}
	
	$("#list option:selected").text();
	var category0 = $("#category0 option:selected").val();
	var category1 = $("#category1 option:selected").val();
	var category2 = $("#category2 option:selected").val();
	if(!category0) {
		$("#category0_res").html("Please choose a category !");
		return false;
	}
	if(category0) {cat_id=category0;}
	if(category1) {cat_id=category1;}
	if(category2) {cat_id=category2;}
		
	var qns = $("textarea[name=qns]").val();
	var userid = $("input[name=uid]").val();
	//var username = $("input[name=username]").val();
	var dataString = 'act=askqns&cat_id='+ cat_id + '&qns=' + qns+'&uid='+userid;
	$("#askqns2011").show();
	$('#askqns2011').html('<p align="center"><img src="'+url_home+'images/loading.gif" /></p>');
	$.ajax({
		type: "POST",
		url: url_home+"qnsAjax.php",
		data: 'act=askqns&cat_id='+ cat_id + '&qns=' + qns+'&uid='+userid,
		success: function(result) {
			//alert (result);
			if (result==1 && newsignup) {
				$("#askqns2011").html(
					  '<div style="height:80px;">&nbsp;</div>'+
					  '<div style="margin-left:40px;">'+
					  '	<div class="puptext1">'+
					  '		Your question has been submitted to our expert.<br />'+
					  '		You will shortly receive a response via email.'+'<div>&nbsp;</div>'+
					  '		For login to member area, please activate your account from your email.'+
					  '	</div><div>&nbsp;</div>'+
					  '	<button class="btn01 hand" onclick="gotoURL(window.location.pathname)">'+
					  '	RETURN TO PREVIOUS PAGE</button>&nbsp; &nbsp; &nbsp; '+
					  ' <button class="btn01 hand" onclick="checkandgo('+userid+')">LOGIN TO ADMIN AREA</button>'+
					  '<div class="puptext1" id="result01"></div>'+
					  '</div>'); 
			}
			else if (result==1) {
				$("#askqns2011").html('<div style="height:80px;">&nbsp;</div>'+
									  '<div style="margin-left:40px;">'+
									  '<div class="puptext1">'+
									  '		Your question has been submitted to our expert.<br />'+
									  '		You will shortly receive a response via email.'+
									  '</div><div>&nbsp;</div>'+
									  '<button class="btn01 hand" onclick="gotoPage(\'user.php\')">'+
									  'LOGIN TO ADMIN AREA</button> -or- '+
									  '<button class="btn01 hand" onclick="gotoURL(window.location.pathname)">'+
									  'RETURN TO PREVIOUS PAGE</button></div>'); 
			}
			else ;
		}
	});
	return false;
}
function validateAskAnExpertForm2(formid) {
	var flag = 0;	
	$("#list option:selected").text();
	var category0 = $("#category0 option:selected").val();
	var category1 = $("#category1 option:selected").val();
	var category2 = $("#category2 option:selected").val();
	if(!category0) {
		alert("Please choose a category !");
		return false;
	}
	if(category0) {cat_id=category0;}
	if(category1) {cat_id=category1;}
	if(category2) {cat_id=category2;}
	if(!valid_required(formid.qns.value))	{	
		alert("Please enter your question !");		
		formid.qns.focus();
		return false;
	}	
	var qns = $("textarea[name=qns]").val();
	var userid = $("input[name=uid]").val();
	var dataString = 'act=askqns&cat_id='+ cat_id + '&qns=' + qns+'&uid='+userid;
	$("#askqns2011").show();
	$('#askqns2011').html('<p align="center"><img src="'+url_home+'images/loading.gif" /></p>');
	$.ajax({
		type: "POST",
		url: url_home+"qnsAjax.php",
		data: dataString,
		success: function(result) {
			if (result==1) {
				showMessage('Your question has been submitted to our expert.<br />'+
									  'You will shortly receive a response via email.');
				formid.reset();
			}
			else ;
		}
	});
	$("#ajax_loader").hide();
	return false;
}
function followAuthor(formid) {	
	if(validateFollowAuthor(formid)) {
		$('#poupcont_loader'+auth_uid).show();
		var email = formid.emailid.value;
		var vname = formid.vname.value;
		var auth_uid = formid.auth_uid.value;
		var auth_uname = formid.auth_uname.value;
		alert($('#poupcont'+auth_uid).text());
		$('#poupcont'+auth_uid).html("<div align='center'>Thank you for following this author."+
				"  You will be notified when this author submits another article or "+
				"answers a question.</div><div>&nbsp;</div>"+
				"<div align='center'><input type='button' value='CLOSE' onclick='return hs.close(this)'/></div>");
		
		$.post(url_home+"authorAjax.php", { act:"followauthor", emailid: email, name: vname, authuid:auth_uid, authuname:auth_uname },
		function(data) { });
	}
	return false;
}

function artPagination(spanid,pagenum,userid) {	
	$('#author_articles').html('<div align="center"><img src="'+url_home+'images/loading.gif" /></div>');
	setTimeout("",1000);
	$.post(url_home+"authorAjax.php", { act:"artpage", page: pagenum, uid:userid },
	function(data) {
		$("#art_pagination_div").children("span").removeClass("active");
		$(spanid).addClass("active");
		$('#author_articles').html(data);
	});
}
function ansPagination(spanid,pagenum,userid) {	
	$('#author_answers').html('<div align="center"><img src="'+url_home+'images/loading.gif" /></div>');
	$.post(url_home+"authorAjax.php", { act:"anspage", page: pagenum, uid:userid },
	function(data) {
		$("#ans_pagination_div").children("span").removeClass("active");
		$(spanid).addClass("active");
		$('#author_answers').html(data);
	});
}
function qnsPagination(spanid,pagenum,userid) {	
	$('#author_questions').html('<div align="center"><img src="'+url_home+'images/loading.gif" /></div>');
	$.post(url_home+"authorAjax.php", { act:"qnspage", page: pagenum, uid:userid },
	function(data) {
		$("#qns_pagination_div").children("span").removeClass("active");
		$(spanid).addClass("active");
		$('#author_questions').html(data);
	});
}
function questionVote (formid) {
	if(formid.vote[0].checked || formid.vote[1].checked)  {
		var qnsid = formid.qnsid.value;
		if(formid.vote[0].checked) var vote = formid.vote[0].value;
		else if(formid.vote[1].checked) var vote = formid.vote[1].value;
		$.post(url_home+"qnsAjax.php", { act:"addvote", qns_id: qnsid, qns_vote: vote },
		function(data) {
			//alert (data+' #qnsvoting'+qnsid);
			$('#qnsvoting'+qnsid).html("<div align='center'>Thank you for voting.</div><div>&nbsp;</div>"+
									   "<div align='center'><input type='button' value='CLOSE' onclick='return hs.close(this)'/></div>");});
	}
	else {
		alert ("Please select your option.")
	}
	return false;
}

function articleVote (formid) {
	var artid = formid.art_id.value;
	if(formid.vote[0].checked) var vote = formid.vote[0].value;
	else if(formid.vote[1].checked) var vote = formid.vote[1].value;
	else var vote = 0;
	if(vote==0) {
		alert ("Please select your option.");
	}else {		
		$.post(url_home+"artAjax.php", { act:"addvote", art_id: artid, art_vote: vote },
		function(data) {
			$('#artvoting'+artid).html("<div align='center'>Thank you for voting.</div><div>&nbsp;</div>"+
									   "<div align='center'><input type='button' value='CLOSE' onclick='return hs.close(this)'/></div>");});		
	}
	return false;
}
function getSubCategory(catid,dest_id) {
	$.post(url_home+"catAjax.php", { act:"subcat", cat_id: catid },
	function(data) {
		$("#"+dest_id).html(data);
		$("#tooltip").show("fast", function (e) {
		   var position = $("#subcategory").position();
		   var l = position.left;var t = position.top;
		   var leftofset = (l+200)+"px"; var topofset = (t)+"px";
		   $("#tooltip").css("left",leftofset);$("#tooltip").css("top",topofset);
     	   $("#totip").text("Please narrow your category if possible, otherwise click Submit now.");			
    	});
	});
}
function getSubsubCategory(catid,dest_id) {
	$.post(url_home+"catAjax.php", { act:"sub_subcat", cat_id: catid },
	function(data) {
		$("#"+dest_id).html(data);
		$("#tooltip").show("fast", function (e) {
		   var position = $("#narrowsubcategory").position();
		   var l = position.left;var t = position.top;
		   var leftofset = (l+200)+"px";var topofset = (t)+"px";
		   $("#tooltip").css("left",leftofset);$("#tooltip").css("top",topofset);
     	   $("#totip").text("Please narrow your category if possible, otherwiseclick Submit now.");		
    	});
	});
}
/*SHOW QUESTION DETAILS*/
function showQnsDetails(id) {
	$.post(url_home+"qnsAjax.php", { act:"qnsdtl", qns_id: id },
	function(data) {
		$("#data_list").hide();
		$("#qns_details").html(data);
		$("#qns_details").show();
	});
}
function UserCatSubscribe() {
	$.post(url_home+"UserCatSubscribeAjax.php",
	function(data) {
		$("#user_cat_tree").html(data);
	});
}

function submitComment(formid,type) {
	
	var commentvalue = trim(formid.comment.value);
	var commentvalue1 = formid.comment.value;
	var captchavalue = trim(formid.captcha.value);
	if( !valid_required(formid.comment.value) )	{	
		alert("Please enter your comment!");		
		formid.comment.focus();return false;
	}
	if( commentvalue=="Writeyourcommenthere" ||  commentvalue=="Write your comment here")	{	
		alert("Please enter your comment!");		
		formid.comment.focus();return false;
	}
	if( !valid_required(formid.captcha.value) )	{	
		alert("Please enter the security code!");		
		formid.captcha.focus();return false;
	}
	/*if(type=="qns") {
		var qns_id = trim(formid.qnsid.value);
		url = url_home+"qnsAjax.php?act=postcomment&qnsid="+qns_id+"&comment="+commentvalue;
	}
	else {
		var art_id = trim(formid.artid.value);
		url = url_home+"artAjax.php?act=postcomment&artid="+art_id+"&captcha="+captchavalue+"&comment="+commentvalue1;
	}
	
	$.get(url,function(data) {
					   
		if(data==1) {
			alert(data);
		$.modaldialog.success('Thank you for posting your comment.<br />  Please allow 24 hours for comments to post live on the website.');
		}
		else {
			alert(data);
			alert("Your verification code mismatched");
		}
	});
	formid.comment.value = "Write your comment here";
	return false;*/
	return true;
}

//EMAIL THIS ARTICLE VALIDATION
function validateEmailArticleForm(formid) {
	if(!EmailValid(formid.toemailid.value)) {
		formid.toemailid.focus();
		return false;
	}
	if(!EmailValid(formid.fromemailid.value)) {
		formid.fromemailid.focus();
		return false;
	}
	var art_title = formid.article_title.value;
	var cur_url = formid.cur_url.value;
	$("#emailthisarticleform").hide();
	$("#emailthisarticle").show();
	$("#emailthisarticle").html("<div align='center'><div>&nbsp;</div><p><font color='#006666'><b>Thank you for forwarding this article to a friend.</b></font></p><div>&nbsp;</div>"+
								"<div align='center' style='text-align:center;'>"+
								"<input type='button' value='CLOSE' onclick='return hs.close(this)' />"+
								"</div></div>");
	var dataString = 'act=sendemail&toemailid='+ formid.toemailid.value + '&fromemailid=' + formid.fromemailid.value+'&note='+formid.note.value+'&cur_url='+cur_url+'&article_title='+art_title;
	
	$.ajax({
		type: "POST",
		url: url_home+"artAjax.php",
		data: dataString,
		success: function(result) {
			;
		}
	});
	
	return false;
}
function showEmailArticleForm(formid) {
	formid.toemailid.value = "";
	formid.fromemailid.value = "";
	formid.note.value = "";
	$('#emailthisarticleform').show();
	$('#emailthisarticle').hide();
}
function refreshCaptcha() {
document.getElementById('captcha_iframe').contentWindow.location.reload();
}
function sendemail() {
	var dataString = 'act=sendconfemail';
	$.ajax({
		type: "POST",
		url: url_home+"artAjax.php",
		data: dataString,
		success: function(result) {
			;
		}
	});
}
