//This file expects only jquery ajax functions

function getAJAXElementTypeDesc(tbl,col,typeID,limitID){
	$.get("_jqueryfetch.php", {'singledata':'1','tbl':'event_type','col':'description','typeID':typeID,'limitID':limitID},function(data){var ed = FCKeditorAPI.GetInstance('content') ;
	d = ed.GetHTML()
	if(d==""){
	ed.InsertHtml(data);}} );
}

function getAJAXUserLogin(evt){

	$.get("_jqueryfetch.php", {'userlogin':'1','email':$("#email").val(),'password':$("#password").val(),'eventID':evt},function(data){

		if(data == "success"){
			location.href='main.php?p='+evt
		}else{
			$("#loginform").html(data)
		}
		});
}

function showAJAXLogin(evt){
	$.get("_jqueryfetch.php", {'showlogin':'1','eventID':evt},function(data){
			$("#loginform").html(data)
			});
}

function showAJAXChangeForm(divid,evt,usr,appt){
	htmldiv = "#"+divid
	$.get("_jqueryfetch.php", {'showchangeappt':'1','eventID':evt,'userID':usr,'appointmentID':appt},function(data){
			$(htmldiv).html(data)
			});
}

function showAJAXUserFundsForm(uid){
	$.get("_jqueryfetch.php", {'showuserfundsform':'1','userID':uid},function(data){
			$("#divregistrar").html(data);
			$("#divregistrar").slideToggle(600);
			});
}

function showAJAXUserAddCouponForm(uid){
	$.get("_jqueryfetch.php", {'showusercouponform':'1','userID':uid},function(data){
			$("#usercoupon").html(data);
			});
}

function AJAXAddUserFunds(){
	uid = $("#userID").val();
	tid = $("#transactionID").val();
	sid = $("#semesterID").val();
	credits = $("#credits").val();
	amount = $("#amount").val();
	isFullAccess = ($("#isFullAccess").attr('checked'))?1:0;
	status  = $("#status").val();
//alert(isFullAccess);
	$.get("_jqueryfetch.php", {'adduserfunds':'1','userID':uid,'transactionID':tid,'semesterID':sid,'credits':credits,'amount':amount,'isFullAccess':isFullAccess,'status':status},function(data){
		$("#divregistrar").empty();
		$("#divregistrar").html(data);
		//$("#divregistrar").slideDown(600);
	});
}

function AJAXValidateCouponCode(){
	cid = $("#couponID").val();
	$.get("_jqueryfetch.php", {'validatecoupon':'1','couponID':cid},function(data){
			$("#usercoupon").empty();
			$("#usercoupon").html(data);
	});
}

function getAJAXuserdropdown(evt){
	$.get("_jqueryfetch.php", {'userdropdown':'1','eventID':evt},function(data){
			$("#dynuserregister").html(data)
			});
}

function getAJAXcalcodebyid(cal){
//	alert(cal)
	$.get("_jqueryfetch.php", {'calcodebyid':'1','calID':cal},function(data){
//alert ("data:"+data);
setStateOptions(data)
			});
}

function getAJAXcountryidbyname(nam,ste){
	$.get("_jqueryfetch.php", {'countryidbyname':'1','countryname':nam},function(data){
setStateOptions(data,ste)
			});
}

function showAJAXControlPanelTable(divid,tbl){
	htmldiv = "#"+divid
	$("#ajaxtable").empty();
	$("#controlpanelcontrols").empty();
	$("#controlpanel").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");
	$.get("_jqueryfetch.php", {'controlpaneltable':'1','cptable':tbl},function(data){
			$("#controlpanel").empty();
			$(htmldiv).html(data)

			});
}

function showAJAXUserInfo(divid,usrid){
	$("#ajaxtable").empty();
	$("#controlpanel").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");
	htmldiv = "#"+divid
	$.get("_jqueryfetch.php", {'showsingleuserinfo':'1','uid':usrid},function(data){
			$("#controlpanel").empty();
			$(htmldiv).append(data);

		});
	//
}

function showAJAXEventInfo(divid,eventid){
	$("#ajaxtable").empty();
	$("#controlpanel").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");

	htmldiv = "#"+divid
	$.get("_jqueryfetch.php", {'showsingleeventinfo':'1','eid':eventid},function(data){
		$("#controlpanel").empty();
		$(htmldiv).append(data);

	});
}

function showAJAXReportInfo(reportfunction,reportkey){
	reportsemester  = $("#esx").val();
	$("#ajaxtable").empty();
	$("#controlpanel").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");
	$.get("_jqueryfetch.php", {'showreport':'1','reportfunction':reportfunction,'reportkey':reportkey,'reportsemester':reportsemester},function(data){
		$("#controlpanel").empty();
		$("#controlpanel").append(data);

	});
}

function showAJAXEventInfoByMonth(divid,sel){
	$("#ajaxtable").empty();
	$("#controlpanel").empty();
	$("#controlpanel").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");
	seldate = sel[sel.selectedIndex].value
	htmldiv = "#"+divid
	$.get("_jqueryfetch.php", {'showeventbymonthinfo':'1','dx':seldate},function(data){
		$("#controlpanel").empty();
		$(htmldiv).append(data);

	});
}

function showAJAXCourseInfoBySemester(divid,sel){
	$("#ajaxtable").empty();
	$("#controlpanel").empty();
	$("#controlpanel").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");
	seldate = sel[sel.selectedIndex].value
	htmldiv = "#"+divid
	$.get("_jqueryfetch.php", {'showcoursebysemesterinfo':'1','esx':seldate},function(data){
		$("#controlpanel").empty();
		$(htmldiv).append(data);

	});
}

function showAJAXChangeTimeZone(){
	$.get("_jqueryfetch.php", {'showtimezoneinfo':'1'},function(data){
		tzform = "<form method=post><select name='usertz'>"+data+"</select><input type=submit value='apply'></form>";
		$("#usertimezone").replaceWith(tzform);
	});
}

function AJAXSearchDropdown(id){
	$("#srch"+id).slideToggle(600);
	str = $("#arrowmask"+id).html()
	if(str.indexOf("right")!=-1){
		$("#arrowmask"+id).html("<img src='images/arrow_mask_down.png'>");
	}else{
		$("#arrowmask"+id).html("<img src='images/arrow_mask_right.png'>");
	}
}

function showAJAXUserDashOption(dash,sub){
	$("#dashdiv").slideToggle("slow");
	$("#dashdiv").empty();
	$.get("_jqueryfetch.php", {'userdashoption':'1','dash':dash,'sub':sub},function(data){
			$("#dashdiv").html(data)
			$("#dashdiv").slideToggle("slow")
			});
}

function showAJAXMessageTable(dash,sub,msg){
	$("#msglist").empty();
	$.get("_jqueryfetch.php", {'userdashoption':'1','dash':dash,'sub':sub,'mid':msg},function(data){
		$("#msglist").html(data)
//		if(msg !=undefined){
//			$("#msglist").prepend(msg)
//		}
	});
}

function AJAXSetUserRegStatus(rid,status){
	$.get("_jqueryfetch.php", {'setuserregstatus':'1','rid':rid,'status':status},function(data){
			if(data>0){
				$("#statusbut"+rid).css({"background-color":"#DFEEE2","border":"0px solid"})
			}
	});
}

function AJAXViewMessage(mid){
		$.get("_jqueryfetch.php", {'messageview':'1','mid':mid},function(data){
		$("#msgtxt"+mid).html(data);
		$("#msgtxt"+mid).slideToggle();
	});
		$.get("_jqueryfetch.php", {'messagecount':'1'},function(data){
		$("#newmsgcount").html(data);

		if (data > 0){
			$('#userdash_message').pulse({
				backgroundColors: ['white','#FF9281'],
				speed:2500
			});
			}else{
				$('#userdash_message').recover()
			}

	});
}

function AJAXDeleteMessage(mid){
//	if(confirm("Are you sure? This action cannot be undone.")){
		$.get("_jqueryfetch.php", {'messagedelete':'1','mid':mid},function(data){
		$("#msgdiv"+mid).fadeOut();
		$("#newmsgcount").html(data);

		if (data > 0){
			$('#userdash_message').pulse({
				backgroundColors: ['white','#FF9281'],
				speed:2500
			});
			}else{
				$('#userdash_message').recover()
			}
	});
//	}
}

function AJAXPostMessage(){
	message = FCKeditorAPI.GetInstance('message').GetXHTML();
	to = $("#to_hidden").val() ;
	from = $("#from").val() ;
	subject = $("#subject").val() ;
		$.get("_jqueryfetch.php", {'sendusermessage':'1','to':to,'from':from,'subject':subject,'message':message},function(resp){
				showAJAXMessageTable("message","inbox",resp);
	});

}

function AJAXuserDropCourse(uid,cid){
	if(confirm("Are you sure? This action cannot be undone.")){
		$.get("_jqueryfetch.php", {'userdropcourse':'1','uid':uid,'cid':cid},function(data){
		$("#scheddiv"+cid).fadeOut();
	});
	}
}

function AJAXDeleteMaterial(mid){
	if(confirm("Are you sure? This action cannot be undone.")){
		$.get("_jqueryfetch.php", {'materialdelete':'1','mid':mid},function(data){
		$("#materialdiv"+mid).fadeOut();
	});
	}
}

function AJAXDeleteArchive(aid){
	if(confirm("Are you sure? This action cannot be undone.")){
		$.get("_jqueryfetch.php", {'archivedelete':'1','aid':aid},function(data){
		$("#archivefilediv"+aid).fadeOut();
	});
	}
}

function AJAXDeleteClass(iid){
	if(confirm("Are you sure? This action cannot be undone.")){
		$.get("_jqueryfetch.php", {'classdelete':'1','iid':iid},function(data){
		$("#classdiv"+iid).fadeOut();
	});
	}
}

function AJAXshowLogin(eid,ref){
	$("#eid").val(eid);
	$("#ref").val(ref);
	$("#logindiv").show("slow");
	$("#loginregister").attr("href",$("#loginregister").attr("href")+"?eid="+eid+"&amp;ref="+ref);
	$("#infomsg").html("<span class=errtext>Please login. Email addresses and passwords are case sensitive. If you are not a WebYeshiva member, please register now to attend this course.</span>");
}

function AJAXshowAttendees(cid,type){
	$("#attendancegraph").hide("slow");
	$("#attendancechart").show("slow");
	$("#attendancechart").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");
	$.get("_jqueryfetch.php", {'showattendees':'1','cid':cid,'type':type},function(data){
			$("#attendancechart").html(data);
			$("#attendancechart").append("<br><a href='javascript:void(0);' onclick='$(\"#attendancegraph\").slideToggle();$(\"#attendancechart\").slideToggle();'>Show attendance graph</a>");
	});

}

function AJAXgetPublishDoc(bool,pid){
	$.get("_jqueryfetch.php", {'publishdoc':'1','bool':bool,'pid':pid},function(data){
				if(data){
					imgsrc=(bool)?"check":"ex";
					linkbool=(bool)?0:1;
					$("#pubicon"+pid).attr('src','images/icon_'+imgsrc+'.png');
					$("#publink"+pid).attr('href','javascript:AJAXgetPublishDoc('+linkbool+','+pid+')');
				}
	});

}

function AJAXgetOnlineUserCount(){
		$.get("_jqueryfetch.php", {'getonlineusercount':'1'},function(data){
				if(data){
					$("#olusercount").html(data);
				}
		});
}

function AJAXgetCancelDoc(bool,pid){
	$.get("_jqueryfetch.php", {'canceldoc':'1','bool':bool,'pid':pid},function(data){
				if(data){
					imgsrc=(bool)?"cancel":"calendar";
					linkbool=(bool)?0:1;
					$("#cancelicon"+pid).attr('src','images/icon_'+imgsrc+'.png');
					$("#cancellink"+pid).attr('href','javascript:AJAXgetCancelDoc('+linkbool+','+pid+')');
				}
	});

}

function AJAXLoginAsUser(uid){
	$.get("_jqueryfetch.php", {'loginasuser':'1','uid':uid},function(data){
		if(data){
			location.href=data;
		}
	});
}

function AJAXregisterUser(uid){
	cid = $("#userregcourses").val();
	$.get("_jqueryfetch.php", {'registeruser':'1','cid':cid,'uid':uid},function(data){

		if(data){

			if($("#nouserregclasses").length){
				$("#nouserregclasses").empty();
				$("#nouserregclasses").attr("id","userregclasses");
			}
				$("#userregclasses").append(data+"<br>");

		}else{
			alert("Course not added. User alredy registered.");
		}

	});
}

function AJAXLogAttendance(courseid,classid,userid,type){
	$.post("_jqueryfetch.php", {'loguserattendance':'1','courseID':courseid,'classID':classid,'userID':userid,'type':type});
}

function AJAXviewPageAccess(dt,cid){
	$("#pageaccess").empty();
	$("#pageaccess").html("<br><h3 align=center>Loading... please wait <img src=\"images/waiting.gif\"></h3>");
	$.get("_jqueryfetch.php", {'viewpageaccess':'1','dt':dt,'cid':cid},function(data){
		$("#pageaccess").empty();
		$("#pageaccess").append(data);

	});
}

function AJAXCoursePayment(uid,cid){
	$.get("_jqueryfetch.php", {'coursepayment':'1','uid':uid,'cid':cid},function(data){
		if(data=='paid'){
			showAJAXUserDashOption("account","all");
		}else{
			showAJAXUserDashOption("addfunds","all")
		}
	});
}

function showAJAXUserCourseArchive(cid){
	$("#dashdiv").slideToggle("slow");
	$("#dashdiv").empty();
	$.get("_jqueryfetch.php", {'usercoursearchive':'1','cid':cid},function(data){
			$("#dashdiv").html(data)
			$("#dashdiv").slideToggle("slow")
			});
}
