var popUp;

/* Open / Close Calendar */
function OpenCalendar(id, postBack)
{
	try
	{	
		var ctrl = document.getElementById(id);
		if(ctrl == null)
		{
			ctrl = document.getElementById("ctl00_CPHMain_"+id);
			if(ctrl != null)
				id = "ctl00_CPHMain_"+id;
			else
				throw "Could not find control \""+id+"\".";
		}
		
		var value = ctrl.value;

		popUp = window.open('../Popups/Calendar.aspx?id=' + id + '&selected=' + value + '&postBack=' + postBack, 
			'popupcal', 
			'width=310,height=300,left=400,top=150');
	}
	catch(s)
	{
		alert("Exception in OpenCalendar(id=\""+id+"\",postBack=\""+postBack+"\") (scripts.js): "+s);
	}
}

function SetDate(id, newDate, postBack)
{
/*	Note: If you are getting an error when you press OK it is because you have postBack=true
	and the Date textfield is not "autopostback=true"
	alert('SetDate(formName='+formName+', id='+id+', newDate='+newDate+', postBack='+postBack+')');*/
	
	var theCtrl = document.getElementById(id);
	theCtrl.value = newDate;
	
	if(postBack)
		__doPostBack(id,'');
}


function ResetPassword(userid, postBack)
{
	popUp = window.open('ResetPassword.aspx?id=' + userid, 
		'popup', 
		'width=260,height=220,left=200,top=200,statusbar=no');
}


function ChangePassword(userid, postBack)
{
	var s;
	if(IsDevEnv())
		s = "/StaffPages/Popups/ChangePassword.aspx";
	else
		s = "/Popups/ChangePassword.aspx";

	popUp = window.open(s, 'popup300x300', 
		'width=300,height=300,left=200,top=200,statusbar=no');
}

function OnSearch()
{
	var s;
	if(IsDevEnv())
		s = "/StaffPages/Popups/OnSearch.aspx";
	else
		s = "/Popups/OnSearch.aspx";

	popUpSearch = window.open(s, 'popup300x180', 
		'width=320,height=220,left=200,top=200,statusbar=no');
}

function EnquiryLookup()
{
	var s;
	if(IsDevEnv())
		s = "/StaffPages/Popups/EnquiryLookup.aspx";
	else
		s = "/Popups/EnquiryLookup.aspx";

	popUp = window.open(s, 'popup300x180', 
		'width=320,height=220,left=200,top=200,statusbar=no');
}

/* id = name of textfield etc... i.e. 'StartDate' */
function ChangeCourseNo(id, courseid, courseno)
{
	popUp = window.open('../FundMan/AlterCourseNumber.aspx?'+
			'formname='+document.forms[0].name+
			'&id='+id+
			'&postBack=true'+
			'&courseid='+courseid+
			'&courseno='+courseno,
		'popupcal', 
		'width=300,height=140,left=80,top=120');
}

/* Called by popup to refresh main page */
function Refresh(formName, id, newValue, postBack)
{
//	alert('Refresh(formName='+formName+', id='+id+', newValue='+newValue+', postBack='+postBack+')');
	
	eval('var theform = document.' + formName + ';');
	theform.all[id].innerHTML = newValue;
	if (postBack)
		__doPostBack(id,'');

	popUp.close();
}


function Inspect(name, obj)
{
	var s = name + " : \n";
	var n = 0;
	for(x in obj)
	{
		try
		{
			s += x + " (" + typeof(obj[x]) + ") = " + obj[x] + "\n";
		}
		catch(ex)
		{
			s += x + "=***Automation?***\n";
		}
		n++;
		if(n == 25)
		{
			n = 0;
			alert(s + "....");
			s = "....\n";
		}
	}
	alert(s);
}

function FindControl(name, obj)
{
	for(x in obj)
	{
		if(obj[x] == name)
			return obj[x];

		if(typeof(obj[x]) == "object")
		{
			s = FindControl(name, obj[x]);
			if(s != "")
				return obj[x] + "." + s;
		}
	}
	return "";
}

function GetCourseCode()
{
	popUp = window.open('CopyCourse.aspx', 
		'width=700,height=500,left=100,top=100,scroll=auto');
}

/* Returns true if running on a dev environment with a StaffPages folder */
function IsDevEnv()
{
	return (location.pathname).substr(0, 11) == "/StaffPages";
}


/* Called by the Help button on the menu */
function OnHelp()
{
	var lev1, lev2, str, pos;

	str  = window.location.pathname;
	pos  = str.indexOf(".");
	str  = str.substr(0,pos);
	pos  = str.lastIndexOf('/');
	lev2 = str.substr(pos+1,32);
	str  = str.substr(0,pos);
	pos  = str.lastIndexOf('/');
	lev1 = (pos == -1) ? "" : str.substr(pos+1,32);

	var url;
	if(IsDevEnv())
	{
		// For dev env, we must INCLUDE the /StaffPages
		// part of the URL and also REMOVE lev1 if at
		// the root level.
		if(lev1 == 'StaffPages')
			lev1 = '';
		url = '/StaffPages/Help/Help.aspx?page='+lev1+'+'+lev2;
	}
	else
		url = '/Help/Help.aspx?page='+lev1+'+'+lev2;

	popUp = window.open(url, 'popuphelp', 
		'width=500,height=600,left=600,top=120,scrollbars=yes');
}

function DoInfo(url)
{
	var popup;

	popUp = window.open(url, 'infopopup', 
	'width=700,height=250,left=100,top=100,scroll=auto');
}

/* Perform a popup window for View & Print Course */
function DoViewCourse(id, bPrint, nPage)
{
	if(nPage == null)
		nPage = 1;
	DoViewXXX('courseid', id, bPrint, nPage);
}


/* Perform a popup window for View & Print IA Course */
function DoViewIACourse(id, bPrint)
{
	var sURL = '../Courses/ViewCourse1IA.aspx?courseid=' + id;
	
	if(bPrint == true)
		sURL = sURL + "&fn=print";

	var popUp = window.open(sURL, 
		'popupViewCourse', 
		'left=100,top=0,width=660,height=700,status=no,scrollbars=yes,resizable=yes,toolbar=no,menubar=no,location=no');
}


/* Perform a popup window for View & Print Course */
function DoViewVenue(id, bPrint, nPage)
{
	if(nPage == null)
		nPage = 4;
	DoViewXXX('venueid', id, bPrint, nPage);
}


/* Perform a popup window for View & Print Course */
function DoViewXXX(sParam, id, bPrint, nPage)
{
	var sURL = '../Courses/ViewCourse'+nPage+'.aspx?' + sParam + '=' + id;
	
	if(bPrint == true)
		sURL = sURL + "&fn=print";

	var popUp = window.open(sURL, 
		'popupViewCourse', 
		'left=100,top=0,width=660,height=700,status=no,scrollbars=yes,resizable=yes,toolbar=no,menubar=no,location=no');
}



/* Perform a popup window for Generating a Report */
function DoViewReport(n)
{
	var sURL;
	if(n == 4 || n == 5)
		sURL = '../AttRecs/ViewReport4.aspx';
	else
		sURL = '../AttRecs/ViewReport0.aspx';

	var popUp = window.open(sURL, 
		'popupViewReport', 
		'left=100,top=0,width=660,height=555,status=no,scrollbars=yes,resizable=yes,toolbar=no,menubar=no,location=no');
}


/* Perform a popup window for View & Print Course */
function DoEditMembers(sURL, sType)
{
	var popUp;

	popUp = window.open(sURL+'?type='+sType, 
		'frEditMembers', 
		'left=100,top=0,width=660,height=555,status=no,scrollbars=yes,resizable=yes,toolbar=no,menubar=no,location=no');
}

// Perform a popup window for View & Print Course
function PreviewCIS(nCISID)
{
	var sURL = '../CIS/Preview.aspx?id='+nCISID;

	var popUp = window.open(sURL, 
		'popupCISPreview', 
		'left=100,top=0,width=700,height=555,status=no,scrollbars=yes,resizable=yes,toolbar=no,menubar=no,location=no');
}

/* Usage: <asp:TextBox onkeypress=EnsureNumeric() ...> */
function EnsureNumeric()
{
	var key = window.event.keyCode;
	if(key < 48 || key > 57) 
		window.event.returnValue = false;
}

/* Used in CourseLog screens to Print and Close */
function DoPrint()
{
	window.print();
}
function DoClose()
{
	window.close();
}
