
function swapOptions( selectObj, i, j ) {
	var iOption = new Option(selectObj.options[i].text, selectObj.options[i].value, false, false);
	var jOption = new Option(selectObj.options[j].text, selectObj.options[j].value, false, false);

	selectObj.options[i] = jOption;
	selectObj.options[j] = iOption;
	selectObj.options[j].selected = true;
}

function moveItem( listBox, isUp ) {

	var changeMade = 0;
	if (listBox.value != '') {

		if (listBox.selectedIndex >= 0) {

			var numItems = listBox.options.length;

			// Find out the new order of the items
			var itemOrderArr = new Array(numItems);

			// Set up the initial ordering
			for( var i=0; i < numItems; i++ ) {
				itemOrderArr[i] = i;
			}

			// Do another pass
			var inEnd = true;  // Are we still in a block at the end that is selected?
			var i, temp;

			// moving up
			if( isUp == 1 ) {

				for( i=0; i < numItems; i++ ) {

					// first we skip all the selected items at the end
					if( !(inEnd && listBox[i].selected) ) {

						inEnd = false;

						// If the current item is selected, switch with the one above
						if( listBox[i].selected ) {
							changeMade = 1;
							temp = itemOrderArr[i-1];
							itemOrderArr[i-1] = itemOrderArr[i];
							itemOrderArr[i] = temp;
						}
					}
				}

			// moving down
			} else {

				for( i=numItems-1; i >= 0; i-- ) {

					// first we skip all the selected items at the end
					if( !(inEnd && listBox[i].selected) ) {
						inEnd = false;

						// If the current item is selected, switch with the one above
						if( listBox[i].selected ) {
							changeMade = 1;
							temp = itemOrderArr[i+1];
							itemOrderArr[i+1] = itemOrderArr[i];
							itemOrderArr[i] = temp;
						}
					}
				}
			}

			if( changeMade ) {

				// build an array of the new Ids and Text locations
				var itemIdArr = new Array(numItems);
				var itemNameArr = new Array(numItems);
				var itemSelectedArr = new Array(numItems);

				for( i=0; i < numItems; i++ ) {

					itemIdArr[i] = listBox[i].value;
					itemNameArr[i] = listBox[i].text;
					itemSelectedArr[i] = listBox[i].selected;
				}

				// Reorder the items
				var newPostion;
				for( i=0; i < numItems; i++ ) {
					newPosition = itemOrderArr[i];
					listBox[i].value = itemIdArr[newPosition];
					listBox[i].text = itemNameArr[newPosition];
					listBox[i].selected = itemSelectedArr[newPosition];
				}
			}
		}
	}
	return changeMade;
}

function moveItem_OldWay( listbox, isUp ) {
	var changeMade = 0;

	if (listbox.selectedIndex >= 0) {

		var numItems = listbox.options.length;

		// Find out the new order of the items
		var itemOrderArr = new Array(numItems);

		// Set up the initial ordering
		for( var i=0; i < numItems; i++ ) {
			itemOrderArr[i] = i;
		}

		// Do another pass
		var inEnd = 1;  // Are we still in a block at the end that is selected?
		var i, temp;

		// moving up
		if( isUp ) {

			for( i=0; i < numItems; i++ ) {
				// first we skip all the selected items at the start
				if( !(inEnd && listbox[i].selected) ) {
					inEnd = 0;

					// If the current item is selected, switch with the one above
					if( listbox[i].selected ) {
						changeMade = 1;
						swapOptions( listbox, i, i-1 );
					}
				}
			}
		}
		// moving down
		else {

			for( i=numItems-1; i >= 0; i-- ) {

				// first we skip all the selected items at the end
				if( !(inEnd && listbox[i].selected) ) {
					inEnd = 0;

					// If the current item is selected, switch with the one above
					if( listbox[i].selected ) {
						changeMade = 1;
						swapOptions( listbox, i, i+1 );
					}
				}
			}
		}
	}
	return changeMade;
}
	ns4 = (document.layers)? true:false
	ns6 = (document.getElementById)? true:false
	ie4 = (document.all)? true:false

	function layerWrite(id,nestref,text) {
		if (ns4) {
		 	var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document;

			lyr.open();
			lyr.write(text);
	 		lyr.close();
		}
		else if (ie4) {
			document.all[id].innerHTML = text;
		}
		else if (ns6) {
			document.getElementById(id).innerHTML = text;
		}
		else {
			alert('Error: This application is not supported by your browser.  Please try again with a different browser.');
		}
	}

	function checkSpelling( dataSource ) {
		var winChk = window.open("/d2l/tools/spellchecker/spellcheckFrameset.asp?ou="+ g_orgUnitId +"&dataSource=" + dataSource, "SpellChecker", "width=400,height=450,toolbar=no,status=no,menubar=no,directories=no,resizable=yes");
	}

	function admin_help( collectionName, ptitle ) {
//		var x = window.open('/d2l/tools/languages/helpAdmin/helpAdmin.asp?ou='+ g_orgUnitId +'&cname=' + collectionName,'adminHelpWin','status=0,width=580,height=480,resizable=yes');

		var win = window.open( '/d2l/tools/frameset/genericFrameset.asp?ou='+ g_orgUnitId +'&cname=' + collectionName + '&fs_title=' + ptitle + '&fs_buttons=17&fs_page=/d2l/lp/Lang/admin/modify/language_instructions_edit.d2l', '', 'directories=0,height=800,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0,width=800');
		win.focus();
	}

	function selectDatePopup( in_basename ) {
		var formName = getFormName();

		var name_m = formName +"."+ in_basename + "Month";
		var name_d = formName +"."+ in_basename + "Day";
		var name_y = formName +"."+ in_basename + "Year";

		var valueMonth, valueDay, valueYear;
		valueMonth = dropdown( eval(name_m) );
		valueDay = dropdown( eval(name_d) );
		valueYear = dropdown( eval(name_y) );

		var winSelectDate;
		winSelectDate = window.open( '/d2l/tools/schedule/selectDatePopup.asp?ou='+ g_orgId +'&fn='+ formName +'&inBN='+ in_basename +'&cMonth='+ valueMonth +'&cDay='+ valueDay +'&cYear='+ valueYear, 'selectDay', 'height=255,width=200,resizable=yes' );
		winSelectDate.focus();
	}

	function verifySelectDate( base_name ) {
		var formName = getFormName();
		var m, d, y;
		m = dropdown( eval(formName + '.' + base_name + 'Month') );
		d = dropdown( eval(formName + '.' + base_name + 'Day') );
		y = dropdown( eval(formName + '.' + base_name + 'Year') );

		return verifyDate(m,d,y);
	}

	function verifySelectDateRange( base_name1, base_name2 ) {
		var formName = getFormName();

		var m1, d1, y1, m2, d2, y2;

		m1 = parseInt( dropdown( eval(formName + '.' + base_name1 + 'Month') ) );
		d1 = parseInt( dropdown( eval(formName + '.' + base_name1 + 'Day') ) );
		y1 = parseInt( dropdown( eval(formName + '.' + base_name1 + 'Year') ) );

		m2 = parseInt( dropdown( eval(formName + '.' + base_name2 + 'Month') ) );
		d2 = parseInt( dropdown( eval(formName + '.' + base_name2 + 'Day') ) );
		y2 = parseInt( dropdown( eval(formName + '.' + base_name2 + 'Year') ) );

		var status = -1;
		if( y2 > y1 ) {
			status = 1;
		}
		else if( y2 == y1 ) {
			if( m2 > m1 ) {
				status = 1;
			}
			else if( m2 == m1 ) {
				if( d2 > d1 ) {
					status = 1;
				}
				else if( d2 == d1 ) {
					status = 0;
				}
			}
		}

		return status;
	}

	function verifySelectTimeRange( base_name1, base_name2 ) {
		var formName = getFormName();

		var h1, m1, tp1, h2, m2, tp2;

		var status = -1;
		if( eval(formName + '.' + base_name1 +'Hour') ) {
			h1 = parseInt( dropdown( eval(formName + '.' + base_name1 +'Hour') ) );
			m1 = parseInt( dropdown( eval(formName + '.' + base_name1 +'Minute') ) );
			tp1 = dropdown( eval(formName + '.' + base_name1 +'TimePart') );

			h2 = parseInt( dropdown( eval(formName + '.' + base_name2 +'Hour') ) );
			m2 = parseInt( dropdown( eval(formName + '.' + base_name2 +'Minute') ) );
			tp2 = dropdown( eval(formName + '.' + base_name2 +'TimePart') );

			h1 = h1 % 12;
			h2 = h2 % 12;

			if( tp2 > tp1 ) {
				status = 1;
			}
			else if( tp2 == tp1 ) {
				if( h2 > h1 ) {
					status = 1;
				}
				else if( h2 == h1 ) {
					if( m2 > m1 ) {
						status = 1;
					}
					else if( m2 == m1 ) {
						status = 0;
					}
				}
			}
		}else {
			status = 0;
		}

		return status;
	}

	function verifySelectDateTimeRange( base_name1, base_name2 ) {
		var status = verifySelectDateRange( base_name1, base_name2 );

		if( status == 0 ) {
			status = verifySelectTimeRange( base_name1, base_name2 );
		}

		return status;
	}

	ns4 = (document.layers)? true:false
	ns6 = (document.getElementById)? true:false
	ie4 = (document.all)? true:false

	function layerWrite(id,nestref,text) {
		if( document.getElementById ) {
			document.getElementById(id).innerHTML = text;
		}
		else {
		 	var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document;

			lyr.open();
			lyr.write(text);
	 		lyr.close();
		}
	}

	var browser_supports_getElementById  = (document.getElementById)? true:false

	var selected_bg_colour = '#E8E8FF';
	var unselected_bg_colour = '#FFFFFF';

	var selected_folder_bg_colour = '#000084';
	var selected_folder_colour = '#FFFFFF';
	var unselected_folder_bg_colour = '#FFFFFF';
	var unselected_folder_colour = '#000000';

	function changeColour( id, colour ) {
		if( browser_supports_getElementById ) {
			document.getElementById(id).style.color = colour;
		}
	}

	function changeBGColour( id, colour ) {
		if( browser_supports_getElementById ) {
			document.getElementById(id).style.backgroundColor = colour;
		}
	}

	function getBGColour( id ) {
		if( browser_supports_getElementById ) {
			if( document.getElementById(id) ) {
				return document.getElementById(id).style.backgroundColor;
			}
		}
	}

	//call this function to get the default (un)highlighting
	// b = 1 to highlight, b = 0 to unhighlight
	function highlight( id, b, n ) {
		var colour = (b)? selected_bg_colour : unselected_bg_colour;
		if( isNaN(n) ) {
			changeBGColour( id, colour );
		} else {
			for( i=0; i<n; i++ ) {
				changeBGColour( id + String(i), colour );
			}
		}
	}

	function selectNavFolder( id, b ) {
		if( b ) {
			changeBGColour( id, selected_folder_bg_colour );
			changeColour( id, selected_folder_colour );
		} else {
			changeBGColour( id, unselected_folder_bg_colour );
			changeColour( id, unselected_folder_colour );
		}
	}

	function dropdown(obj) {
		if( obj.selectedIndex >= 0 ) {
			return obj.options[obj.selectedIndex].value;
		}
		return null;
	}

	function verifyDate( month, day, year ) {
		var daysInMonth;

		switch( parseInt(month) )
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				daysInMonth = 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				daysInMonth = 30;
				break;
			case 2:
				daysInMonth = 28
				if( parseInt(year) % 4 == 0) {
					if( parseInt(year) % 100 != 0 ) {
						daysInMonth = 29
					}
					else {
						if( parseInt(year) % 400 == 0 ) {
							daysInMonth = 29
						}
					}
				}
				break;
			default:
				return 0;
		}

		if( parseInt(day) > daysInMonth ) {
			return 0;
		}

		return 1;
	}

	function PopupWindow( url, winName, features ) {
		var winHandle;
		winHandle = window.open( url, winName, features )
		return winHandle;
	}

	function d2lTrim ( inputStringTrim ) {
		var fixedTrim;
		var lastCh;

		fixedTrim = "";
		lastCh = " ";

		for (x=0; x < inputStringTrim.length; x++) {
			ch = inputStringTrim.charAt(x);
			if ((ch != " ") || (lastCh != " ")) {
				 fixedTrim += ch;
			}
			lastCh = ch;
		}
		if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
			fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1);
		}
		return fixedTrim
	}

	function d2lPopup( ) {
		//this will be the D2L popup window function
	}

	function close_popup_window( win ) {
		if( win ) {
			if( !win.closed ) {
				win.close();
			}
		}
		return 0;
	}

	function previewText( ou, srcText, srcIsHTML, height, width ) {
		var preview_win = window.open("/d2l/tools/preview/preview_frame.asp?ou="+ ou +"&text="+ escape(srcText) +"&isHTML="+ escape(srcIsHTML), "preview_text_win", "height="+ height +",width="+ width +",resizable=yes");
		preview_win.focus();
	}

	function launchWysiwygEditor(ou, chkIsHTML, elementName) {
		if( eval(chkIsHTML) ) {
			eval(chkIsHTML).checked=true;
		}
		window.open('/d2l/tools/wysiwygEditor/editor.asp?ou='+ ou +'&cbGet=wysiwyg_'+elementName+'_get&cbSet=wysiwyg_'+elementName+'_set&strip=true', 'wysiwygEditor','width=650,height=465,resizable=yes');

	}

	// set status bar copyright
	var d = new Date();
	var status = "Powered by Desire2Learn Inc.";
	window.defaultStatus = status;
	document.onmouseout = function() { window.status = status; };

