//***************************************************
// For showing Help Information - will be re-written using ajax
var intCurrentHelpPane = 1;

function ShowHelp(intToShow) {
	document.getElementById("help" + intCurrentHelpPane).style.display = 'none';
	document.getElementById("help" + intToShow).style.display = 'block';
	intCurrentHelpPane = intToShow;
}


//***************************************************
// Simulate a link
function MouseMe(obj, strAction) {
	switch (strAction) {
		case "over":
			obj.style.cursor = "pointer";
			obj.style.cursor = "default";
			obj.style.color = "#000000";
			break;
		default :
			obj.style.color = "#FFFFFF";			
			break;
	}
}

function mOver(obj, img) {
	obj.src= img;
	document.body.style.cursor='pointer';
}
function mOut(obj, img) {
	obj.src= img;
	document.body.style.cursor='default';
}

function Show(objID, blnShow) {
	if (blnShow) {
		document.getElementById(objID).style.display = 'block';
	}else{
		document.getElementById(objID).style.display = 'none';
	}
}




function ToggleView(obj) {
	var thisObj = document.getElementById('sub'+obj.id);
	if (thisObj.style.display=='none') {
		thisObj.style.display='block';
	}else{
		thisObj.style.display='none';
	}
}

//AlertMessages
function m_Message(mID) {
	switch (mID) {
		case "1": 
			alert("Timeslots will only remain On Hold for 15 minutes.");
			break;	
		case "no edit 1":
			alert("Past timeslots are uneditable.");
			break;
		case "session expired":
			alert("Your session has expired.");
			window.location="login.aspx";
			break;
		case "not permitted":
			alert("Logged in user is not permitted to perform this function.");
			break;
		case "status changed":
			alert("The status of this timeslot has changed since your last refresh.\nThe change could not be completed.\nRefresh the schedule to continue.");
			break;
		case "qualification failed":
			alert("Failed to add new qualification.");
			break;
		case "qualification exists":
			alert("A qualification by the same name already exists.");
			break;
		case "error":
			alert("An error has occurred.");
			break;
	} 
}
function m_Info(strMessage, messageStatus) {
	var intOffset;
		
	myObj = document.getElementById("message")
	
	switch (messageStatus) {
		case "info":
			myObj.style.background= "#E3BB64";
			myObj.style.color= "#000000";
			break;
		case "warning":
			myObj.style.background= "#E3E164";
			myObj.style.color= "#000000";
			break;
		case "error":
			myObj.style.background= "#F87925";
			myObj.style.color= "#000000";
			break;
	}
	myObj.innerHTML = strMessage;
	
	//Half the window width - half the body width + 20
	intOffset = (f_clientWidth() / 2) - (726/2) + 20; 
	
	myObj.style.left = intOffset + "px";
	myObj.style.top = "150px";
	myObj.style.display = "block";
	//Show it for 3 seconds
	setTimeout("myObj.style.display='none';",3000);
}


function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}





//-----------------------------------------------------------
// HTML AJAX WRAPPER

function HttpClient() { }

HttpClient.prototype = {
	// type GET,POST passed to open
		//requestType:"POST",
		//Not sure why, but it was necessary to change this to "GET" to make it work on my online host.??
		requestType:"GET",
	// when set to true, async calls are made
		isAsync:false,

	// where an XMLHttpRequest instance is stored
		xmlhttp:false,

	// what is called when a successful async call is made
		callback:false,

	// what is called when send is called on XMLHttpRequest
	// set your own function to onSend to have a custom loading
	// effect
		onSend:function() {
			Show('timer', true);
		},

	// what is called when readyState 4 is reached, this is
	// called before your callback
		onload:function() {
			Show('timer', false);
		},

	// what is called when an http error happens
		onError:function(error) {
			alert(error);
			Show('timer', false);
		},

	// method to initialize an xmlhttpclient
		init:function() {
			try {
				// Mozilla / Safari
				this.xmlhttp = new XMLHttpRequest();
			} catch (e) {
				// IE
				var XMLHTTP_IDS = new Array("MSXML2.XMLHTTP.5.0",
					"MSXML2.XMLHTTP.4.0",
					"MSXML2.XMLHTTP.3.0",
					"MSXML2.XMLHTTP",
					"Microsoft.XMLHTTP");
				//This next line was found and I'm trying to resolve the 411 length required problem
				//objXMLHTTP.setRequestHeader("Content-Length", "youractualbyteslength");
				
				var success = false;
				for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
					try {
						this.xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
						success = true;
					} catch (e) {}
				}
				if (!success) {
					this.onError("Unable to create XMLHttpRequest.");
				}
			}
		},

	// method to make a page request
	// @param string url The page to make the request to
	// @param string payload What you’re sending if this is a POST
	//            request
		makeRequest: function(url,payload) {
			if (!this.xmlhttp) {
				this.init();
			}
			
			//url = "testAjaxResponse.aspx";
			//payload = "status=blue";
			this.xmlhttp.open(this.requestType,url,this.isAsync);
   
			//this.xmlhttp.open(this.requestType,'testAjaxResponse.aspx?status=blue',this.isAsync);
			//this.xmlhttp.open("GET","testAjaxResponse.aspx?status=blue",true);//this.isAsync);
			//testAjaxResponse.aspx?status=blue

			// set onreadystatechange here since it will be reset after a
			//completed call in Mozilla
			var self = this;
			this.xmlhttp.onreadystatechange = function() {
				self._readyStateChangeCallback(); 
			}

			this.xmlhttp.send(payload);

			if (!this.isAsync) {
				return this.xmlhttp.responseText;
			}
		},

	// internal method used to handle ready state changes
		_readyStateChangeCallback:function() {
			switch(this.xmlhttp.readyState) {
				case 2:
					this.onSend();
					break;
				case 4:
					this.onload();
					if (this.xmlhttp.status == 200) {
						this.callback(this.xmlhttp.responseText);
					} else {
						//this.onError(’HTTP Error Making Request: ’ + ’[’ + this.xmlhttp.status+’]’ + ’+this.xmlhttp.statusText));
					}
					break;
			}
		}
}






var isWorking = false;
var client = new HttpClient();
client.isAsync = true;

/*
	function SimplestFunction() {
		client.callback = function(result) {
			document.getElementById("target").innerHTML = result;
		}
		//client.makeRequest(".",null);	//This was what was included in the example
		client.makeRequest("testAjaxResponse.aspx","status=green");
	}
*/	

// Function UR -> UpdateReservation
function UR(objID, strResInfo, blnGetComment) {	
	try {
		if (!isWorking) {
			var strComment 
			if (blnGetComment) {
				strComment = prompt("Additional Notes or Comments", "Grade / Course");
			} else {
				strComment = ""
			}
			if ( (! blnGetComment) || ((blnGetComment) && (strComment!=null)) ){	
				isWorking = true;
				Show('timer', true);
				document.body.style.cursor = 'wait';
				
				client.callback = function(result) {
					switch (result) {
						case 'no edit 1' :	//Past timeslots are uneditable.
							m_Message('no edit 1');
							break;
						case 'session expired' :
							m_Message('session expired');
							break;
						case 'not permitted' :
							m_Message('not permitted');
							break;
						case 'status changed' :
							m_Message('status changed');
							break;							
						case 'x' :
							//Do nothing
							break;
						default :
							document.getElementById(objID).innerHTML = result;					
							break;
					}				
					
					isWorking = false;
					document.body.style.cursor = 'default';
					Show('timer', false);
				}
				client.makeRequest("Svc_TeacherSchedule.aspx?resinfo=" + strResInfo + "&SchoolID=" + document.getElementById("SchoolID").value + "&comment=" + strComment, null);
			}
		}
	} catch (e) {
		isWorking = false;
		document.body.style.cursor = 'default';
		Show('timer', false);
		//Show error message
		//alert(e);
	}
}

//UpdateReservation- BookDay
function URBD(objIDBase, strResInfoBase, intTimeSlot, strComment) {	
	try {
		if (!isWorking) {
			if (strComment == "") {
				strComment = prompt("Additional Notes or Comments", "Grade / Course");
			}
			
			if (strComment != "") {	
				isWorking = true;
				Show('timer', true);
				document.body.style.cursor = 'wait';
				
				if (intTimeSlot == 0) {intTimeSlot = 1;}
				
				client.callback = function(result) {
					switch (result) {
						case 'no edit 1' :	//Past timeslots are uneditable.
							//m_Message('no edit 1');
							break;
						case 'session expired' :
							//m_Message('session expired');
							break;
						case 'not permitted' :
							//m_Message('not permitted');
							break;
						case 'status changed' :
							//m_Message('status changed');
							break;							
						case 'x' :
							//Do nothing
							break;
						default :
							var objID;
							objID = objIDBase.replace(/TSID/i, intTimeSlot + '');
							alert(objID);
							document.getElementById(objID).innerHTML = result;
							intTimeSlot = intTimeSlot + 1							
							break;
					}				
					
					isWorking = false;
					document.body.style.cursor = 'default';
					Show('timer', false);
					
					//********************************************************
					//Call recursively until all timeslots have been booked
					if (intTimeSlot <= 2) {
						//URBD('2007_1_13_TSID_33', '5|3|2007_1_13_TSID_33|0', intTimeSlot, strComment);
						URBD(objIDBase, strResInfoBase, intTimeSlot, strComment);					
					}
					//********************************************************
				}
				var strResInfo;
				strResInfo = strResInfoBase.replace(/TSID/i, intTimeSlot + '');
				client.makeRequest("Svc_TeacherSchedule.aspx?resinfo=" + strResInfo + "&SchoolID=" + document.getElementById("SchoolID").value + "&comment=" + strComment, null);
			}
		}
	} catch (e) {
		isWorking = false;
		document.body.style.cursor = 'default';
		Show('timer', false);
		//Show error message
		//alert(e);
	}
}

function SearchTeachers() {
	try {
		if (!isWorking) {	
			isWorking = true;
			Show('timer', true);
			document.body.style.cursor = 'wait';
			
			client.callback = function(result) {
				document.getElementById("searchResults").innerHTML = result;
				
				isWorking = false;
				document.body.style.cursor = 'default';
				Show('timer', false);
			}
			var strQS;
			strQS = "?pref=" + document.getElementById("schoolPreference").value;
			strQS += "&grade=" + document.getElementById("teacherGradePreference").value;
			strQS += "&qual=" + document.getElementById("teacherQualification").value;
			strQS += "&yf=" + document.getElementById("yearFrom").value;
			strQS += "&mf=" + document.getElementById("monthFrom").value;
			strQS += "&df=" + document.getElementById("dayFrom").value;
			strQS += "&yt=" + document.getElementById("yearTo").value;
			strQS += "&mt=" + document.getElementById("monthTo").value;
			strQS += "&dt=" + document.getElementById("dayTo").value;
			
			client.makeRequest("Svc_SearchTeachers.aspx" + strQS, null);
		}
	} catch (e) {
		isWorking = false;
		document.body.style.cursor = 'default';
		Show('timer', false);
	}
}

//GetTeacherProfileInformation
function GTPI(tID, eID) {
	//TeacherID + ElementID
	try {
		if (!isWorking) {	
			isWorking = true;
			Show('timer', true);
			document.body.style.cursor = 'wait';
			
			client.callback = function(result) {
				document.getElementById(eID).innerHTML = result;
				
				Show('T' + tID + '_1', false);Show('T' + tID + '_2', true);
			
				isWorking = false;
				document.body.style.cursor = 'default';
				Show('timer', false);
				
				//Hide the "Loading..." text
				document.getElementById('tm' + tID).innerHTML = "&nbsp;"
				
			}
			var strQS;
			strQS = "?id=" + tID;
			
			//Show the "Loading..." text
			document.getElementById('tm' + tID).innerHTML = "<span style='color:red;'>Loading...</span>"
			
			client.makeRequest("Svc_TeacherProfile.aspx" + strQS, null);
		} else {
			document.getElementById('tm' + tID).innerHTML = "<span style='color:red;'>Busy... Try again.</span>"
		}
	} catch (e) {
		isWorking = false;
		document.body.style.cursor = 'default';
		Show('timer', false);
	}
}
//-----------------------------------------------------------
/*
//	Qualifications
		function AddQualification() {	
			//alert("clicked");
			Show('timer', true);
			client.callback = function(result) {
				alert(result);			
				switch (result) {
					case 'session expired' :
						m_Message('session expired');
						break;
					case 'not permitted' :
						m_Message('not permitted');
						break;
					case 'error' :
						m_Message('error');
						break;
					case 'qualification failed' :
						m_Message('qualification failed');
						break;
					case 'qualification exists' :
						m_Message('qualification exists');
						break;
					case 'qualification saved' :
						GetQualifications();
						break;
				}	
				document.getElementById("subject").value = "";
				
			}
			
			if ((document.getElementById("grade").value == '') || (document.getElementById("subject").value == '')) {
				alert("Please provide full details for the new teacher qualification.");
			}else{
				client.makeRequest("Svc_Qualifications.aspx?action=1&grade=" + escape(document.getElementById("grade").value) + "&subject=" + escape(document.getElementById("subject").value), null);
			}
			
			Show('timer', false);
		}
		
		function GetQualifications() {
			Show('timer', true);
			client.callback = function(result) {
				document.getElementById("qualifications").innerHTML = result;
			}
			client.makeRequest("Svc_Qualifications.aspx?action=2&ID=" + escape(document.getElementById("TeacherID").value), null);
			Show('timer', false);
		}
		
		function SaveQualifications(strIDs) {
			var arrList = strIDs.split('|');
			var strChecked="";
			var count=0;
			
			//Build the string of checked qualifications
			while (count < arrList.length) {
				if (document.getElementById(arrList[count]).checked) {
					strChecked += document.getElementById(arrList[count]).value + "|";
				}
				count+=1;
			}
			//Add the AJax call to save and rebuild this onscreen.
			alert(strChecked);
			//Make the call and pass the string...
		}
		*/
//-----------------------------------------------------------