var checkForClassrooomPE;
var ajaxResult;

function launchClassroom(tutorID, childID) {

	var classroom;
	if (childID == "") {
		classroom = tutorID + "_Practice_Classroom";
	} else {
		classroom = tutorID + "_" + childID;
	}
	
	var params = "?";

	params = params + "action=host";
	params = params + "&meetingRoomName="+ classroom;
	params = params + "&displayName="+ tutorID;
	
	params = params + "&confName=WeTutor_Classroom";
	params = params + "&privateChatEnabled=false";
	params = params + "&publicChatEnabled=true";
	params = params + "&presenterAV=av";
	params = params + "&assignMikeOnJoin=true";
	params = params + "&handsFreeOnLoa=true";

	window.open("http://206.191.32.106/dimdim/html/envcheck/connect.action" + params,"WeTutorChat","");
}

function joinClassroom(tutorID, childID){
	var classroom = tutorID + "_" + childID;
	var params = "?";
	params = params + "action=join";
	params = params + "&"+ "meetingRoomName="+ classroom;
	params = params + "&"+ "displayName="+ childID;
	window.open("http://206.191.32.106/dimdim/html/envcheck/connect.action" + params,"WeTutorChat","");
}

function checkForClassroom(tutorID, childID,uType){
	var classroom = tutorID + "_" + childID;
	var button;
	var classroomdiv = $('classroom');
	var bookmarked;
	
	if (uType == "S") { //Student
		button = $('joinbutton');
	} else { // Tutor
		button = $('launchbutton');
	}
	ajaxResult = new Ajax.Request("/tutor/bookmarked.htm",{
		parameters: {idTutor: tutorID, idChild: childID},
		method: 'post',
		asynchronous: false,
		onSuccess: function(transport){
			if (transport.responseText.match("true")) {
				bookmarked = true;
			} else {
				bookmarked = false;
			}
		}
	});
	checkForClassrooomPE = new PeriodicalExecuter(function(pe){
		ajaxResult = new Ajax.Request("/student/dimDimClassroomQuery.htm",{
		parameters: {classroom: classroom},
		method: 'post',
		onSuccess: function(transport){
			if (transport.responseText.match("true")) {
				if (button != null) {
					if (uType == "S") {
						button.className="orangeBtn";
						button.disabled="";
						if (bookmarked) {
							classroomdiv.innerHTML = "<strong>Step 3 - Virtual Classroom</strong> <br /> The tutor has lauched the classroom. Click Join Classroom to open the classroom in a new window.";
						} else {
							classroomdiv.innerHTML = "<strong>Step 4 - Virtual Classroom</strong> <br /> The tutor has lauched the classroom. Click Join Classroom to open the classroom in a new window.";						
						}
					} else {
						//button.className="grayBtn";
						//button.disabled="disabled";
						classroomdiv.innerHTML = "<strong>Step 3 - Virtual Classroom</strong> <br /> The classroom is open in a new browser window, and student was invited to join. <br /> Click Launch Classroom again if the classroom failed to open.";
					}	
				}
				
			} else {
				if (button != null) {
					if (uType == "T") {
						button.className="orangeBtn";
						button.disabled="";
						classroomdiv.innerHTML = "<strong>Step 3 - Virtual Classroom</strong> <br /> You can launch the classroom at any time.";
					} else {
						button.className="grayBtn";
						button.disabled="disabled";
						if (bookmarked) {
							classroomdiv.innerHTML = "<strong>Step 3 - Virtual Classroom</strong> <br /> After the tutor launches the classroom, you will be invited to join.";
						} else {
							classroomdiv.innerHTML = "<strong>Step 4 - Virtual Classroom</strong> <br /> After the tutor launches the classroom, you will be invited to join.";
						}
					}
				}	
			}
		}
		});
	},10);
}
