var re = /mac/i;
var isMac = re.test(navigator.platform);
	
function deletePmpObject(pmpObjectTitle,pmpObjectId,pmpObjectTypeId)
{
	/*
	alert(pmpObjectTitle);
	alert(pmpObjectId);
	alert(pmpObjectTypeId);
	*/
	var strConfirmMsg;
	switch (pmpObjectTypeId) {
		// lesson plan
		case 2:
			strConfirmMsg = "Are you sure you wish to delete the the lesson plan '" + pmpObjectTitle + "'?";
			break;
		// assignment
		case 3:
			strConfirmMsg = "Are you sure you wish to delete the the assignment '" + pmpObjectTitle + "'?";
			break;
		// quiz
		case 4:
			strConfirmMsg = "Are you sure you wish to delete the the quiz '" + pmpObjectTitle + "'?";
			break;
		default : statement;
			return false;
			break;
	}
	boolDelete = confirm(strConfirmMsg);
	if (boolDelete){
		location.href = "pmpObjectEdit.asp?mode=delete&pmpObjectTypeId=" + pmpObjectTypeId + "&pmpObjectId=" + pmpObjectId;
	}
	return false;
}

// Update order
function updateOrder() {
	var bin = document.getElementById("elementBin");
	var elements = bin.getElementsByTagName("input");
	var sequence = document.getElementById("sequence");
	var list = "";
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].name.indexOf('sequence') == -1 ){
			list += elements[i].name + ",";
		}
	}
	sequence.value = list.substring(0, list.length - 1);
}


/* START lessonPlan/assignment edit pages */

function deleteItem(theId,itemType,itemTitle){
	
	if (editing) {
		alert('Please finish editing items before deleting others.');
		return false;	
	}
	
	if (itemType == 'element' || itemType == 'media'){
		strConfMsg = 'Are you sure you wish to remove \'' + itemTitle + '\'?';
	}
	else{
		strConfMsg = 'Are you sure you wish to delete the \'' + itemTitle + '\' connection?';
	}
	
	// make sure they want to delete this
	boolDelete = confirm(strConfMsg);
	if(boolDelete == false){
		return false;
	}
		
	// get a handle on the bin
	var strItemTypeBin = itemType + 'Bin';
	
	// clear all the text nodes in the bin
	clearTextNodes(strItemTypeBin);
	
	// delete the item
	var elementId = itemType + theId;
	var delItem = document.getElementById(elementId);
	var delParent = delItem.parentNode;
	delParent.removeChild(delItem);
	
	// check that there is only one item left, if so delete the header as well
	if(delParent.childNodes.length == 1){
		var delGrandParent = delParent.parentNode;
		delGrandParent.removeChild(delParent);
		
		// if its the element bit...
		if(itemType=='element'){
			if(document.getElementById('pmpObjElementHeader')){
				delParent = document.getElementById('pmpObjElementHeader').parentNode;
				delParent.removeChild(document.getElementById('pmpObjElementHeader'));
			}
		}
		else if(itemType == "resource") {
			if(document.getElementById('resourceHeader')){
				delParent = document.getElementById('resourceHeader').parentNode;
				delParent.removeChild(document.getElementById('resourceHeader'));
			}
		}
	}
	
	editing = false;
	return false;
}

function createItem(theId)
{
	if (editing) {
		alert('Only one item can be edited at a time. Please finish editing other item.');
		return false;
	}
	
	// clear out all the text nodes
	clearTextNodes('elementBin')
	
	// get a handle on the bin
	var theBin = document.getElementById('elementBin');
	
	// determine the next id
	var nextId = 0; //default to 0
	
	// loop thru the bin, if the element is an item, check its id, find the max
	for (var i = 0; i < theBin.childNodes.length; i++) {
		thisId = theBin.childNodes[i].id;
		if(thisId.indexOf("element") != -1){
			var idArray = thisId.split("element");
			thisId = parseInt(idArray[1]);
			if (thisId > nextId) {
				nextId = thisId;
			}
		}
	}
	nextId = nextId + 1; // make the nextId one more than the max
	
	var elementId = "element" + nextId;
	var headerId = "element" + nextId + "Header";
	var descId = "element" + nextId + "Desc";
	
	// create the next elementDiv
	var nextDiv = document.createElement('DIV');
	nextDiv.id = elementId;
	
	//create the header heading, give it the correct id, don't do this if component is freeform
	if(!isFreeForm){
		var header = document.createElement('h3');
		header.setAttribute('id',headerId);	
		var headerText = document.createTextNode('');
		header.appendChild(headerText);
		nextDiv.appendChild(header);
	}
	
	// create the description DIV, give it the correct id, and add it to elementDIV, add the contents
	var descDIV = document.createElement('DIV');
	descDIV.setAttribute('id',descId);
	descDIV.innerHTML = '';
	nextDiv.appendChild(descDIV);
	
	// get a handle on the placeholder
	var thePlaceHolder = document.getElementById('addPlaceHolder');
	
	// replace the placeholder with this nextDiv
	theBin.replaceChild(nextDiv,thePlaceHolder);
	
	// insert a new placeholder
	theBin.insertBefore(thePlaceHolder,nextDiv);
	
	// edit the new one
	editItem(nextId);
}

function editItem(theId)
{
	if (editing) {
		alert('Only one item can be edited at a time. Please finish editing other item.');
		return false;
	}
	
	var elementId = "element" + theId;
	var headerId = "element" + theId + "Header";
	var descId = "element" + theId + "Desc";
	
	//alert(elementId);
	//alert(headerId);
	//alert(descId);
	
	var theElement = document.getElementById(elementId);
	
	// get a handle on the element's parent
	var elementContents = theElement.parentNode;
	
	// set the header value, if this component is in freeForm, set it to nothing.
	var headerValue;
	if(isFreeForm){
		headerValue = '';
	}
	else{
		var headerValue = document.getElementById(headerId).childNodes[0].nodeValue;
	}
	
	var descValue = document.getElementById(descId).innerHTML;
	
	// create a fieldset
	var fieldSet = document.createElement('FIELDSET');
	fieldSet.className='elementEdit';
	
	// insert the fieldset
	elementContents.insertBefore(fieldSet,theElement);
	
	// create header elements, don't do this if component is in freeForm
	if(!isFreeForm){
		// create the header label
		var headerLabel = document.createElement('LABEL');
		var headerLabelText = document.createTextNode('Heading');
		headerLabel.setAttribute('for',headerId);
		headerLabel.appendChild(headerLabelText);
		
		// create the header input
		var headerText = document.createElement('INPUT');
		headerText.className='txt';
		headerText.setAttribute('id',headerId);
	}
	
	// create the description Label
	var descLabel = document.createElement('LABEL');
	var descLabelText = document.createTextNode('Text');
	descLabel.setAttribute('for',descId);
	descLabel.appendChild(descLabelText);
	
	// create the description text area
	var descTextArea = document.createElement('TEXTAREA');
	descTextArea.className='txt widgEditor';
	descTextArea.name=descId;
	descTextArea.id = descId;
	
	// create the save button, give it the correct class, give it the function
	var saveButton = document.createElement('BUTTON');
	var saveButtonText = document.createTextNode('Finished editing');
	saveButton.className='btn';
	saveButton.appendChild(saveButtonText);
	saveButton.onclick = function () {
		// saving...dont pass the header/desc text
		saveCancel('save',theId,'','');
		return false;
	};
	
	// space between save and cancel
	var spaceText = document.createTextNode(' ');
	
	// create the cancel button, give it the correct class, give it the function
	var cancelButton = document.createElement('BUTTON');
	var cancelButtonText = document.createTextNode('Cancel');
	cancelButton.className = 'btn';
	cancelButton.appendChild(cancelButtonText);
	cancelButton.onclick = function () {
		// canceling... pass the header/desc text, so that they can be put back in place
		saveCancel('cancel',theId,headerValue,descValue);
		return false;
	};
	
	if(!isFreeForm){
		// insert the label for the header
		fieldSet.appendChild(headerLabel);
		
		// insert the header input
		fieldSet.appendChild(headerText);
	}
	
	// insert the description label
	fieldSet.appendChild(descLabel);
	
	// insert the description text area
	fieldSet.appendChild(descTextArea);
	
	// insert the save button
	fieldSet.appendChild(saveButton);
	
	// insert the save button
	fieldSet.appendChild(spaceText);
	
	// insert the cancel button
	fieldSet.appendChild(cancelButton);
	
	if(!isMac){
		// turn all the appropriate textareas into editors
		widgInit();
	}
	
	//alert(elementContents.innerHTML);
	elementContents.removeChild(theElement);
	
	
	if(isMac){
		descTextArea.value = stripHTML(descValue);
	}
	else{
		descTextArea.value = descValue;
	}
	
	
	// set the header value and set focus if this component is not in freeform mode
	if(!isFreeForm){
		headerText.value = headerValue;
		headerText.focus();
	}
	
	fieldSet.id = elementId;
	
	editing = true;

}

function saveCancel(theAction,theId,hValue,dValue) {
	
	var elementId = "element" + theId;
	var headerId = "element" + theId + "Header";
	var descId = "element" + theId + "Desc";
	theElement = document.getElementById(elementId);
	if(!isMac){
		
		// update the editor (html gets cleaned, iframe gets updated from textarea or textarea gets updated from iframe
		// update editor is function added to widgEditor.js by wc
		updateEditor(theId);
			
		// get a handle on the editor iframe if it is showing now
		iframeId = "element" + theId + "DescWidgIframe";
		theIframe = document.getElementById(iframeId);
		
		// get a handle on editor text area if it is showing now
		textAreaId = "element" + theId + "Desc";
		theTextArea = document.getElementById(textAreaId);
		
		var theBody;
		
		// the editor is showing an editor iframe...
		if(theIframe){
			// get the html in the iframe
			theBody = theIframe.contentWindow.document.getElementById("iframeBody").innerHTML;
		}
		
		// the editor is showing the textarea
		if(theTextArea){
			theBody = theTextArea.value;
		}
		
		var theHTML = theBody;	
		
		// make the descTextArea have the html that was in the iframe
		var descTextArea = document.getElementById(descId);
		descTextArea.value = theHTML;
		
		// get all the values
		// don't get the header value if this component is in freeform mode
		var descValue = document.getElementById(descId).value;
		
		// set the headerValue, if this component is in freeForm, set it to an empty string.
		var headerValue;
		if(isFreeForm){
			headerValue = '';
		}
		else{
			headerValue = document.getElementById(headerId).value;
		}
		
	}
	else{
		
		//var theTextArea = document.getElementsByTagName("textarea")[0];
		//theElement = theTextArea.parentNode;
		
		descValue = document.getElementsByTagName("textarea")[0].value;
		
		// set the headerValue, if this component is in freeForm, set it to an empty string.
		var headerValue;
		if(isFreeForm){
			headerValue = '';
		}
		else{
			headerValue = document.getElementsByTagName("textarea")[0];
			headerValue = headerValue.previousSibling;		
			headerValue = headerValue.previousSibling.value;
		}
	}
	
	// they clicked the cancel button;
	if (theAction == 'cancel') {
	
		// the previous header and desc values were passed, or not.
		if (!isBlank(hValue)){ 
			headerValue = hValue; 
		}
		else{
			headerValue = ''; 
		}
		
		if (!isBlank(dValue)){ 
			descValue = dValue; 
		}
		else{
			descValue = ''; 
		}
		
		// no previous values were passed with the cancel button click, just delete the item.
		if (isBlank(hValue) && isBlank(descValue)) {
			editing = false;
			deleteItem(theId,'element',headerValue);
			return false;
		}
	}
	
	// check for header value, don't do this check if this component is in freeForm
	if(!isFreeForm){
		if (isBlank(headerValue)){ 
			alert('Please enter a heading for your item.');
			return false;
		}
	}
	
	// get a handle on the parent of the node they are editing
	var elementContents = theElement.parentNode;
	
	// create the main div, and insert it.
	var elementDIV = document.createElement('DIV');
	elementDIV.className='elementItem';
	elementContents.insertBefore(elementDIV,theElement);
	
	// add the edit/delete/reorder items
	// create the buttons div, float it with appropriate class
	var buttonsDIV = document.createElement('DIV');
	buttonsDIV.className='itemEditDelMove';
	
	// edit button
	var editImg = document.createElement('IMG');
	editImg.setAttribute('src','images/iconEdit.gif');
	editImg.setAttribute('alt','Edit');
	editImg.setAttribute('title','Edit');
	editImg.onclick = function () {
		editItem(theId);
		return false;
	};
	buttonsDIV.appendChild(editImg);
	
	//create the delete, move buttons, don't add them if this component is in freeForm
	if(!isFreeForm){
		
		// add a space
		buttonsDIV.appendChild(document.createTextNode(' '));
		
		// delete button
		var delImg = document.createElement('IMG');
		delImg.setAttribute('src','images/iconDelete.gif');
		delImg.setAttribute('alt','Delete');
		delImg.setAttribute('title','Delete');
		delImg.onclick = function () {
			deleteItem(theId,'element',headerValue);
			return false;
		};
		buttonsDIV.appendChild(delImg);
		
		// add a space
		buttonsDIV.appendChild(document.createTextNode(' '));
		
		// arrow up image
		var moveUpImg = document.createElement('IMG');
		moveUpImg.setAttribute('src','images/iconArrowUp.gif');
		moveUpImg.setAttribute('alt','Move up one slot');
		moveUpImg.setAttribute('alt','Move up one slot');
		moveUpImg.onclick = function () {
			moveItem(theId,'up','element');
			return false;
		};
		buttonsDIV.appendChild(moveUpImg);
		
		// add a space
		buttonsDIV.appendChild(document.createTextNode(' '));
	
		// arrow down image
		var moveDnImg = document.createElement('IMG');
		moveDnImg.setAttribute('src','images/iconArrowDn.gif');
		moveDnImg.setAttribute('alt','Move down one slot');
		moveDnImg.setAttribute('alt','Move down one slot');
		moveDnImg.onclick = function () {
			moveItem(theId,'down','element');
			return false;
		};
		buttonsDIV.appendChild(moveDnImg);
	}
	
	// append the button div
	elementDIV.appendChild(buttonsDIV);
	
	//create the header heading, give it the correct id, don't add the header if this component is in freeForm
	if(!isFreeForm){
		var header = document.createElement('h3');
		header.setAttribute('id',headerId);
		var headerText = document.createTextNode(headerValue);
		header.appendChild(headerText);
		elementDIV.appendChild(header);
	}
	
	// create the description DIV, give it the correct id, correct class, and add it to elementDIV, add the contents
	var descDIV = document.createElement('DIV');
	descDIV.id = descId;
	
	// if this component is freeform, use the elementDescFreeForm class (has border around it).
	if(isFreeForm){
		descDIV.className='elementDescFreeForm';
	}
	else{
		descDIV.className='elementDesc';	
	}
	
	if(isMac){
		descDIV.innerHTML = formatPlain(descValue);
	}
	else {
		descDIV.innerHTML = descValue;
	}
	
	elementDIV.appendChild(descDIV);
	
	if(!isFreeForm){
		// add the hidden header form element
		var headerHiddenElement = document.createElement('INPUT');
		headerHiddenElement.type = 'hidden';
		headerHiddenElement.id = 'element'  + theId + 'HeaderValue';
		headerHiddenElement.name = 'element'  + theId + 'HeaderValue';
		headerHiddenElement.value = headerValue;
		elementDIV.appendChild(headerHiddenElement);
	}
		
	// add the hidden description form element
	var descHiddenElement = document.createElement('INPUT');
	descHiddenElement.type = 'hidden';
	descHiddenElement.id = 'element'  + theId + 'DescValue';
	descHiddenElement.name = 'element'  + theId + 'DescValue';
	descHiddenElement.value = descValue;
	elementDIV.appendChild(descHiddenElement);
	
	// remove the edit elements
	elementContents.removeChild(theElement);
	elementDIV.id = elementId;
	
	updateOrder();
	
	editing = false;
}

function moveItem(theId,theDir,itemType) {
	
	if (editing) {
		alert('Please finish editing other item before moving other items.');
		return false;
	}
	
	// get a handle on the bin
	// could be elements or mediaItems
	var strItemTypeBin = itemType + 'Bin';
	
	// clear all the text nodes in the bin
	clearTextNodes(strItemTypeBin);
	
	// get a handle on the element that is moving
	var elementId = itemType + theId;
	var theElement = document.getElementById(elementId);
	var bin = theElement.parentNode;
	
	// get a handle on everything
	var posCur = theElement;
	var posUp = posCur.previousSibling;
	var posDown = posCur.nextSibling;
	var posNew;
	
	//alert("UP: " + posUp + "\nCUR: " + posCur + "\nDOWN: " + posDown);
	
	// Set new target position
	// don't move it up above the mediaItemsHeader h2
	if (theDir == "up" && posUp && posUp.id != 'addPlaceHolder' && posUp.id != 'mediaItemsHeader') {
		posNew = posUp;
	} else if (theDir == "down" && posDown) {
		posNew = posCur;
		posCur = posDown;
	}
	
	// Swap positions only if current position is not top or bottom
	if (posNew != undefined) {
		// Swap
		bin.insertBefore(posCur, posNew);
	}
	
	updateOrder();
	
	return false;
}


// clear out all the text nodes
function clearTextNodes(theBin) {
	
	// get a handle on the bin
	var theBin = document.getElementById(theBin);
	
	for (var i = 0; i < theBin.childNodes.length; i++) {
		if (theBin.childNodes[i].nodeType == 3) {
			theBin.removeChild(theBin.childNodes[i]);
		}
	}
}

/* END lessonPlan/assignment edit pages */


/* This function is for switching the mini tabs */
function switchTab(tab){

	// get a handle on the content bin
	var bin = document.getElementById('bin');
	
	// get an array of the content divs
	arrContentDivs = bin.getElementsByTagName('div');
	
	// get a handle on the tab that is to be activated
	var	activeTabId = document.getElementById(tab + 'Tab').id;
	var	activeContentId = document.getElementById(tab).id;
	
	// set up some vars
	var	thisTabId;
	var	thisTab;
	var	thisContentId;
	var	thisContent;
	
	// loop thru the bin
	for (var i = 0; i < arrContentDivs.length; i++) {
		
		// get a handle on this contentId
		thisContentId = document.getElementById(arrContentDivs[i].id).id;
		thisContent = document.getElementById(arrContentDivs[i].id);
		
		// get a handle on this tab id
		thisTabId = document.getElementById(arrContentDivs[i].id + 'Tab').id;
		thisTab = document.getElementById(arrContentDivs[i].id + 'Tab');
		
		// if this contentId matches the active content id, show it and change the class of the tab
		// otherwise, hide the content and clear the class on the tab
		if(activeContentId == thisContentId){
			// change style for the tab
			thisTab.className = 'active';
			// show the appropriate content div
			thisContent.style.display = 'block';
		}
		else{
			// change style for the tab
			thisTab.className = '';
			// hide the appropriate content div
			thisContent.style.display = 'none';
		}
	}
}

function stripHTML(str){
	theString = trimString(str.replace(/(<([^>]+)>)/ig,""));
	return theString;
} 

function formatPlain(str){
	theString = str.replace(/\n/g, "<br/>") 
	return theString;
}

