function openWin(theURL, width, height, scrollbars, windowName, resize, toolbar, menubar, directories, titlebar, addressbar, statusbar)
{
	if(width == null) width = 680;
	if(height == null) height = 350;
	if(scrollbars == null) scrollbars = "yes";
	if(windowName == null) windowName = "mywindow";
	if(resize == null) resize = "yes";
	if(toolbar == null) toolbar = "no";
	if(menubar == null) menubar = "no";
	if(directories == null) directories = "no";
	if(titlebar == null) titlebar = "no";
	if(addressbar == null) addressbar = "no";
	if(statusbar == null) statusbar = "no";
	if(location == null) location = "no";

	var thewindow = window.open (theURL, windowName, "status=" + statusbar + ",statusbar=" + statusbar + ",location=" + location + ",directories=" + directories + ",titlebar="+ titlebar + ",menubar=" + menubar + ",toolbar=" + toolbar + ",resizable=" + resize + ",scrollbars=" + scrollbars + ",width=" + width + ",height=" + height);
	if(!thewindow)
	{
		if(theURL.indexOf("/studentportal/") > -1)
			alert("The portal window could not open, probably because you have a Popup Blocker enabled!. Please follow the following steps. \n - Please disable the blocker for this site. \n - Then click the student home link again.");
		else
			alert("The popup window could not open, probably because you have a Popup Blocker enabled! \n - Please disable the blocker for this site.");
	}	
	thewindow.focus();
	
	return thewindow;
}


// The following function, help to disable the right click for a certain element name called imagetag
function rightclick(e) {
	if (document.all) {
		if (event.button == 2) {
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			if (targ.nodeType == 3) { // defeat Safari bug
				targ = targ.parentNode;
			}
			
			if(targ.id == "imagetag" && targ.id != "")
			{	
				alert("This file is copyright of realestateeducate.com");
				return false;
			}
		}
	}
	if (document.layers) 
	{
		if (e.which == 3)
		{
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			if (targ.nodeType == 3) { // defeat Safari bug
				targ = targ.parentNode;
			}
			if(targ.id == "imagetag")
			{	
				alert("You cannot save this file");
				return false;
			}
		}
	}
}
if (document.layers) {
	document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=rightclick;

function printWindow(){
	window.print();
	return (true); 
}

function CheckState(object)
{
	document.location = "/course/" + object.value; 
}

if (document.all) {
	var theTables = document.all.tags("TABLE");
	var theRows = document.all.tags("TR");
	var theCols = document.all.tags("TD");
}

// This toggles all table rows with an id tag named theObject
function toggleDisplay(theObject, theTD) 
{
	for (i=0; i<theRows.length; i++) 
	{
		if (theRows[i].id == theObject) 
		{
			if (theRows[i].style.display == "inline") 
			{
				theRows[i].style.display = "none";
			} 
			else 
			{
				theRows[i].style.display = "inline";
			}
		}
	}
	
	try
	{
		if(theTD.innerHTML.indexOf("+") > -1)
		{
			theTD.innerHTML = " - ";
		}
		else if(theTD.innerHTML.indexOf("-") > -1)
		{
			theTD.innerHTML = " + ";
		}
	}
	catch(err)
	{
	}
}


//convert Value into Money
function convertMoney(theValue)
{

	jsMoney = new Number(theValue);
	jsString = '' + Math.round(jsMoney*100)/100;

	for (i=0; i<jsString.length; i++) 
	{
		if (jsString.charAt(i) == '.') break;
	}
	if (i == jsString.length) return(jsString + ".00");
	else if (i == (jsString.length-1)) return(jsString + "00");
	else if (i == (jsString.length-2)) return(jsString + "0");
	else return(jsString);
}

var portalwindow;
// openWin(theURL, width, height, scrollbars, windowName, resize, toolbar, menubar, directories, titlebar, addressbar, statusbar)
function open_portal_main(url, confirmopen)
{
	var result = true;
	/*if(confirmopen == null) confirmopen = false;
	
	if(confirmopen)
	{
		result = confirm("This will open this page in the portal, click Yes to continue or Cancel to open the page in the current window");
	}*/
	
	if(result)
	{
		if (screen.height <= 600) {
			portalwindow = openWin(url, 785, 580, "yes", 'portal_window', "no", "no", "no", "no","no", "no", "no");
		} else {
			portalwindow = openWin(url, 785, 580, "no", 'portal_window', "no", "no", "no", "no","no", "no", "no");
		}
		if (!portalwindow.opener) portalwindow.opener = self;
		if (window.focus) 
		{
			portalwindow.focus()
		}
	}
	else
	{
		window.document.location = url;
	}
	//return true;
}


if (document.all) 
{
	var theTables = document.all.tags("TABLE");
	var theRows = document.all.tags("TR");
	var theImages = document.all.tags("img");
}

// check if at least one checkbox item is checked, before selecting the action
function validateAction(theForm, theName, theAction, theObject) 
{
	var elements = document.all.tags("input");
	with (theForm) 
	{
		isSelected = false;
		for (i=0; i<elements.length; i++) 
		{
			if (elements[i].name == theName) 
			{
				if (isSelected = elements[i].checked) break;
			}
		}
		if (!isSelected) 
		{
			alert("You must check at least one checkbox before clicking Go!");
			return(false);
		}
	}
	return(confirm("Are you sure you want to " + theAction + " " + theObject + "?"));
}

// Display the email popup window
function email_popup(type, subject, from, to, course, root)
{
	// three types (instructor, info, tech support)
	if(root == "" || root == null) root = "<?php echo $CFG->wwwroot?>";
	var useremail = "<?php echo $USER->email ?>";
	var width = 400;
	if(from != "" && useremail != "")
	{
		width = 376;
	}
		
	url = root + "/mod/message/email.php?type=" + type + "&to=" + to + "&from=" + from + "&subject=" + subject + "&course=" + course;
	openWin(url, 517, width, 0, "email", 1);
}

// default onload, calls setdefaults if one exists.
function on_load()
{
	try
	{
		setDefaults();
	}
	catch(err)
	{
		// do nothing
	}
}


// Toggle checkboxes in a list
checked = false;
function itemSelect(theForm, theName) {
	checked = !checked;
	with (theForm) for (i=0; i<elements.length; i++) {
		if (elements[i].name == theName) elements[i].checked = checked;
	}
}
function itemChecked(theForm, theName) {
	with (theForm) for (i=0; i<elements.length; i++) {
		if (elements[i].name == theName) {
			if (elements[i].checked) return(true);
		}
	}
	return(false);
}

function cancelsubmit(url)
{
	if(confirm("Are you sure you want to cancel?"))
	{
		document.location = url;
	}
}
function changedropdown(action, object, folder)
{
	folder = (folder == null) ? folder = "/course/" : folder;
	
	if(action == "location")
	{
		if(object.value != "")
		{
			window.top.location  = folder + object.value;
		}
	}
}

function isMoney(theField) 
{
	if (theField.value > '' && theField.value != ".") 
	{
		var jsMoney = new Number(theField.value);
		jsString = '' + Math.round(jsMoney*100)/100;
		for (i=0; i<jsString.length; i++) 
		{
			if (jsString.charAt(i) == '.') break;
		}
		if (i == jsString.length) theField.value = jsString + ".00";
		else if (i == (jsString.length-1)) theField.value = jsString + "00";
		else if (i == (jsString.length-2)) theField.value = jsString + "0";
		else theField.value = jsString;
	} 
	else if(theField.value == ".")
	{
		theField.value = "0.00";
	}
	
	if(theField.value.indexOf("NaN") > -1)
	{
		theField.value = "";
		theField.focus();
		alert("This is an invalid money value");			
	}
	return theField.value;
}

// Character Date
function getDateString(theDate, theFormat) 
{
	if (theDate.getMonth() < 9) theMonth = "0" + (theDate.getMonth() + 1);
	else theMonth = (theDate.getMonth() + 1);
	if (theDate.getDate() < 10) theDay = "0" + theDate.getDate();
	else theDay = theDate.getDate();
	theYear = theDate.getFullYear();
	theFormat = jsDateFormat;
	if (theFormat == "DD/MM/YYYY") theString = theDay + "/" + theMonth + "/" + theYear;
	else theString = theMonth + "/" + theDay + "/" + theYear;
	return theString;
}


// Character Time
function getTimeString(theTime) 
{
	theHours = theTime.getHours();
	theMinutes = theTime.getMinutes();
	theTime = theHours +":"+ theMinutes;
	return theTime;
}

// Date value based on DateFormat system default
function getDateValue(theDate, theFormat) 
{
	theResult = new String(theDate);
	if (theFormat == null) theFormat = jsDateFormat;
	if (theFormat == "DD/MM/YYYY") theDate = theResult.substring(3,5) + "/" + theResult.substring(0,2) + "/" + theResult.substring(6,10);
	else theDate = theResult;
	return(new Date(theDate));
}

// Date Selection Window
function getDate(theField) {
	remote = window.open("/help/calendarFrame.html", "CalendarWindow", "toolbar=no,scrollbars=no,resizable=yes,width=250,height=200");
	if (remote != null) {
		if (remote.opener == null) remote.opener = self;
		if (remote.dateField == null) remote.dateField = theField;
	}
}
// Date Selection Window
function getDate(theField) {
	remote = window.open("/lib/calendarFrame.html", "CalendarWindow", "toolbar=no,scrollbars=no,resizable=yes,width=250,height=200");
	if (remote != null) {
		if (remote.opener == null) remote.opener = self;
		if (remote.dateField == null) remote.dateField = theField;
	}
}

// Substitute String
function getSubstituteString(theString, theChar, theSubstitute) 
{
	theReplacement = "";
	for (i=0; i<theString.length; i++) 
	{
		if (theString.charAt(i) == theChar) theReplacement = theReplacement += theSubstitute;
		else theReplacement = theReplacement += theString.charAt(i);
	}
	return theReplacement;
}

// Validate Date field
function isDate(theField, theFormat) 
{
	var theDate;
	var theCentury = new Date("1/1/1900");
	theFormat = jsDateFormat;
	if (theField.value != "") 
	{
		if (theField.value == ".") 
		{
			theDate = new Date();
		} 
		else if (theField.value == "?") 
		{
			getDate(theField);
			theField.value = "";
			theField.focus();
			return false;
		} 
		else if ((theField.value.length == 6) && (theField.value.indexOf("/") == -1) && (theField.value.indexOf("-") == -1)) 
		{
			theString = new String(theField.value);
			if (theFormat == "DD/MM/YYYY") 
			{
				theDay = theString.substring(2, 4);
				theMonth = theString.substring(0, 2);
				theYear = new Number(theString.substring(4, 6));
			} 
			else 
			{
				theDay = theString.substring(0, 2);
				theMonth = theString.substring(2, 4);
				theYear = new Number(theString.substring(4, 6));
			}
			if (theYear <= 50) theYear += 2000;
			theDate = new Date(theDay + '/' + theMonth + '/' + theYear);
		} 
		else if ( (theField.value == "2/29/00") || (theField.value == "02/29/00") || (theField.value == "022900") ) 
		{
			theDate = new Date("02/29/2000");
		} 
		else if ( (theField.value == "29/2/00") || (theField.value == "29/02/00") || (theField.value == "290200") ) 
		{
			theDate = new Date("02/29/2000");
		} 
		else 
		{
			theString = getSubstituteString(theField.value, "-", "/")
			if (theFormat == "DD/MM/YYYY") 
			{
				theDay = theString.substring(0, theString.indexOf("/"));
				theString = theString.substring(theString.indexOf("/") + 1, theString.length);
				theMonth = theString.substring(0, theString.indexOf("/"));
			} 
			else 
			{
				theMonth = theString.substring(0, theString.indexOf("/"));
				theString = theString.substring(theString.indexOf("/") + 1, theString.length);
				theDay = theString.substring(0, theString.indexOf("/"));
			}
			theYear = new Number(theString.substring(theString.indexOf("/") + 1, theString.length));
			if (theYear <= 50) theYear += 2000;
			theDate = new Date(theMonth + "/" + theDay + "/" + theYear);
		}

		if ( isNaN(theDate.getMonth()) || (theDate.getTime() < theCentury.getTime()) ) 
		{
			alert("This is an invalid date (" + theField.value + ").");
			theField.value = "";
			theField.focus();
			return false;
		} 
		else
		{
			theField.value = getDateString(theDate, theFormat);
		}
	}
	return theField;
}