/*  CureTogether JavaScript framework
 *  (c) 2008-2010 CureTogether, Inc.
 *--------------------------------------------------------------------------*/
function canceladdresource(tag) {
	$("#newresource").val('');
}

// submits an "add resource" request when pressing enter
function enter_addresource(e, conditionid, tag, sort) {
	if( e && e.keyCode == 13 ) {
		addresource(conditionid, tag, sort);
	}
	else
		return true;
}

function enter_addsideeffect(e, canonical_resource_id, conditionid, tag, sort) {
	if( e && e.keyCode == 13 ) {
		addsideeffect(e, canonical_resource_id, conditionid, tag, sort);
	}
	else
		return true;
}

function addsideeffect(e, canonical_resource_id, conditionid, tag, sort) {
	if($("#newresource").val()=='')
		return;
		
	$("#status").html('');
	$("#loading").css("visibility","visible");
	$.post("/action.php", { 
			action: 'addsideeffect', 
			crid: canonical_resource_id, 
			sename: $("#newresource").val(),
		}, 
		function (data) {
			canceladdresource(tag);
			if((location.href).search(/survey/i)>0) {
				updateresources(conditionid, tag, sort);
			} else {
				updatestats(conditionid, tag, sort);
			}
			$("#loading").css("visibility","hidden");
			$("#status").html(data);
			$("#addnow").html("");
			//$("#introtext").html("Thank you for taking the initiative!");
		});
}

function addtreatment(conditionid, tag, sort) {
	var cat = $("#trt_cat_sel").val();
	if( cat == "" || cat == "PAC") {
		$("#status").html("<span class='errmsg' style='display:block;margin:6px 0;'>Please choose a category</span>");
		return;
	}
	
	$("#status").html('');
	$("#loading").css("visibility", "visible");

	$.post("/action.php", { 
			action: 'addresource',
			conditionid: conditionid, 
			tag: tag, 
			resourcename: $("#newresource").val(), 
			category:cat 
		}, 
		function (data) {
			canceladdresource(tag);
			if((location.href).search(/survey/i)>0) {
				updateresources(conditionid, tag, sort);
			} else {
				updatestats(conditionid, tag, sort);
			}
			$("#loading").css("visibility", "hidden");
			$("#status").html(data);
			$("#addnow").html("");
			//$("#introtext").html("Thank you for taking the initiative!");
		});
}

function addresource(conditionid, tag, sort) {
	var cat = $("#sym_cat_sel").val();
	if( cat == "" || cat == "PAC") {
		$("#status").html("<span class='errmsg' style='display:block;margin:6px 0;'>Please choose a category</span>");
		return;
	}
	
	if($("#newresource").val()=='')
		return;
		
	$("#status").html('');
	$("#loading").css("visibility","visible");
	$.post("/action.php", { 
			action: 'addresource', 
			conditionid: conditionid, 
			tag: tag,
			sort: sort, 
			resourcename: $("#newresource").val(),
			category: cat
		}, 
		function (data) {
			canceladdresource(tag);
			if((location.href).search(/survey/i)>0) {
				updateresources(conditionid, tag, sort);
			} else {
				updatestats(conditionid, tag, sort);
			}
			$("#loading").css("visibility","hidden");
			$("#status").html(data);
			$("#addnow").html("");
			//$("#introtext").html("Thank you for taking the initiative!");
		});
}


function updateresources(conditionid, tag, sort) {
	$.ajax({
			type: "POST",
			url: "/action.php", 
			async: false,
			data: "action=getresources&conditionid="+conditionid+"&tag="+tag+"&sort="+sort,
			success: 	function (data) { 
				$("#resourcedata").html(data);
			}
		});
}

function updatestats(conditionid, tag, sort) {
	if(!sort && tag=='_symptom') sort = 'as';
	if(!sort && tag=='_treatment') sort = 'ae';
	$.ajax({
			type: "POST",
			url: "/action.php", 
			async: false,
			data: "action=getstats&conditionid="+conditionid+"&tag="+tag+"&sort="+sort,
			success: 	function (data) { 
				$("#statsdata").html(data);
			}
		});
}

function checkresource(rid, val, type) {
	// first, toggle checking and un-checking
	if( val == 'y' ) {	// clicked on Y checkbox
		if( $("#checkresourceyes_"+rid).attr("checked") ) {	// if you checked Y
			$("#checkresourceno_"+rid).attr("checked", false);	// uncheck N
			$("#sharelink_"+rid).css("display", "inline");	// show the "share your experience" link
			$("#rateSpan_"+rid).css("display", ""); // show the rating
		}	 else {	// you unchecked Y
			$("#sharelink_"+rid).hide();	// hide the "share your experience" link
		}
	}
	if( val == 'n' ) { // clicked on N checkbox
		if( $("#checkresourceno_"+rid).attr("checked") ) { // if you checked N
			$("#checkresourceyes_"+rid).attr("checked", false);	// uncheck Y
			$("#sharelink_"+rid).hide();	// hide the "share your experience" link
			$("#rateSpan_"+rid).hide(); // hide the rating
			$("#r_"+rid).css("height", "38px"); // make sure the height of the row stays the same
		}
		else {
			$("#rateSpan_"+rid).css("display", ""); // show the rating
		}
	}

	// then record the correct value in the db
	var chkval = 0;
	if( $("#checkresourceyes_"+rid).attr("checked") )
		chkval = 1;
	if( $("#checkresourceno_"+rid).attr("checked") )
		chkval = -1;
	
	if (type == 'relatedConditions') {
		$.post('/action.php', { action: 'checkRelatedCondition', rid: rid, val: chkval } );	
	}
	if (type == 'sideEffect') {
		$.post('/action.php', { action: 'checkSideEffect', rid:rid, val: chkval } );
	}
	if (!type || type == '') {
		$.post('/action.php', { action: 'checkresource', rid: rid, val: chkval } );
	}
}

function saveresourcedata(rid) {
	$.post('/action.php', { action: 'checkresource', rid: rid, val: $("#saveresource_"+rid).val() } );
}

function editreview(commentid) {
	$("#statusmsg_"+commentid).html('');
	$("#editcommentbtn_"+commentid).css("display", "none");
	$("#comment_"+commentid).css("display", "none");
	$("#editcomment_"+commentid).css("display", "inline");
}
function canceleditcomment(commentid) {
	$("#editcomment_"+commentid).css("display", "none");
	$("#editcommentbtn_"+commentid).css("display", "inline");
	$("#comment_"+commentid).css("display", "");
}
function editresourcedesc(rid) {
	$("#statusmsg_"+rid).html('');
	$("#editcommentbtn_"+rid).css("display", "none");
	$("#response_"+rid).css("display", "none");
	$("#editbox_"+rid).css("display", "inline");
}
function canceleditresource(rid) {
    $("#editbox_"+rid).css("display", "none");
	$("#response_"+rid).css("display", "");
    $("#editcommentbtn_"+rid).css("display", "inline");
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function selectToggle(form) {
     var myForm = document.forms[form];
     for( var i=0; i < myForm.length; i++ ) { 
          if( myForm.elements[i].checked ) {
               myForm.elements[i].checked = "";
          } 
          else {
               myForm.elements[i].checked = "checked";
          }
     }
}
function addslashes(str) {
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\0/g,'\\0');
	return str;
}
function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\0/g,'\0');
	str=str.replace(/\\\\/g,'\\');
	return str;
}

