function doSel(obj)
{
	for (i = 1; i < obj.length; i++)
		if (obj[i].selected == true)
			eval(obj[i].value);
}


var curpos = 1;
var xmlstringform;

function displayNext()
{
	if(curpos == 1)
	{
		document.getElementById("page_" + curpos).style.display = "none";
	}

	if(document.getElementById("page_" + (curpos+1)))
	{
		document.getElementById("page_" + curpos).style.display = "none";
		curpos++;
		document.getElementById("page_" + curpos).style.display = "block";
	}

}

function displayPrev()
{
	

	if(document.getElementById("page_" + (curpos-1)))
	{
		document.getElementById("page_" + curpos).style.display = "none";
		curpos--;
		document.getElementById("page_" + curpos).style.display = "block";
	
	}
}

//********** SORTING CATEGORIES START ********** //
function moveCatUp(item)
{
	var pos = findPos(item);
	//alert(pos + "up");

	if(document.getElementById("Cat_" + pos)){
		//alert("hi");
		var groupObj = document.getElementById("Cat_" + pos);									
		moveCatItem(groupObj,"up",pos-1);
	}
}

function moveCatDn(item)
{
	var pos = findPos(item);

	if(document.getElementById("Cat_" + pos)){
		var groupObj = document.getElementById("Cat_" + pos);	
		moveCatItem(groupObj,"dn",pos-1);
	}
}

function findPos(item)
{
	var ordList = document.getElementById("Cat_Order").innerHTML;
	ordList = ordList.split(",");
	var temp = item.split("_");
	temp = temp[1];
	for(var x=0;x<ordList.length;x++)
	{
		if(temp == ordList[x])
		{
			var pos = x+1;
			x=ordList.length;
		}
	}
	//alert(pos + "1");
	return pos;

}

function moveCatItem(obj,direction,pos)
{
	var catOrder = document.getElementById("Cat_Order");
	catOrder = catOrder.innerHTML.split(",");
	var objPos = pos + 1;
	var obj1 = document.getElementById("Cat_" + objPos);
	var arrayPos1 = pos;

	// Check the direction that we are moving
	if(direction == "up")
	{
		var obj2 = document.getElementById("Cat_" + (objPos - 1));
		var arrayPos2 = pos -1;
	}else{
		var obj2 = document.getElementById("Cat_" + (objPos +1));
		var arrayPos2 = pos + 1;
	}
	if(obj2 != null)
	{

		// Create a placeholder for the HTML and then swap them up
		var tempA = obj1.innerHTML;
		var tempB = obj2.innerHTML;
		obj1.innerHTML = tempB;
		obj2.innerHTML = tempA;

		// Reorder the Items in the Array
		var tempA = catOrder[arrayPos1];
		var tempB = catOrder[arrayPos2];
		catOrder[arrayPos1] = tempB;
		catOrder[arrayPos2] = tempA;
	
		document.getElementById("Cat_Order").innerHTML = catOrder;
	}

	
}

function moveForUp(cat,fID)
{
								
	
	var pos = findForPos(cat,fID);
	var catOrder = document.getElementById("Forums_" + cat);
	catOrder = catOrder.innerHTML.split(",");
	if(document.getElementById("For_" + cat + "_" + pos)){
									
		moveForeItem(cat,"up",pos-1,catOrder);
	}								
}



function moveForeItem(cat,direction,pos,catOrder)
{
	var obj1 = document.getElementById("For_" + cat + "_" + (pos+1));
	var arrayPos1 = pos;
	
	// Check the direction that we are moving
	if(direction == "up")
	{
		var obj2 = document.getElementById("For_" + cat + "_" + (pos));
		var arrayPos2 = pos -1;
	}else{
		var obj2 = document.getElementById("For_" + cat + "_" + (pos+2));
		var arrayPos2 = pos + 1;
	}
	
	if(obj2 != null)
	{

		// Create a placeholder for the HTML and then swap them up
		var tempA = obj1.innerHTML;
		var tempB = obj2.innerHTML;
		obj1.innerHTML = tempB;
		obj2.innerHTML = tempA;

		// Reorder the Items in the Array
		var tempA = catOrder[arrayPos1];
		var tempB = catOrder[arrayPos2];
		catOrder[arrayPos1] = tempB;
		catOrder[arrayPos2] = tempA;
	
		document.getElementById("Forums_" + cat).innerHTML = catOrder;
	}

	
}


function moveForDn(cat,fID)
{
	var pos = findForPos(cat,fID);
	var catOrder = document.getElementById("Forums_" + cat);
	catOrder = catOrder.innerHTML.split(",");
	if(document.getElementById("For_" + cat + "_" + pos)){
									
		moveForeItem(cat,"dn",pos-1,catOrder);
	}
}


function findForPos(cat,fID)
{
	var ordList = document.getElementById("Forums_" + cat);
	ordList = ordList.innerHTML.split(",");
	for(var x=0;x<ordList.length;x++)
	{
		if(fID == ordList[x])
		{
			var pos = x+1;
			x=ordList.length;
		}
	}
	return pos;
}


function saveForums()
{

	var catArray = document.getElementById("Cat_Order");
	catArray = catArray.innerHTML.split(",");
	
	var xmlstring = "<forums>";
	
	for(var x=0;x<catArray.length;x++)
	{
		xmlstring = xmlstring + "<category id=\"" + catArray[x] + "\">";
		var forums = document.getElementById("Forums_" + catArray[x]);
		
		if(forums != null)
		{
			if(forums.innerHTML != "")
			{
				forums = forums.innerHTML.split(",");
				//alert("Forums " + forums);
				for(var y=0;y<forums.length;y++)
				{
					//alert(forums);
					xmlstring = xmlstring + "<forum id=\"" + forums[y] + "\"/>";
				}
			}
		}
		xmlstring = xmlstring + "</category>";
	}
	xmlstring = xmlstring + "</forums>";
	//alert(xmlstring);
	document.getElementById("output").value=xmlstring;
	
	// ********************************************************************************************
	// ********************************************************************************************
	// ********************************************************************************************
	// ***************You can use this or just send it through a form******************************
	
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	
	if (xmlhttp)
	{
		//url = "http://www.thinkingcap.info/Forums.aspx?forums=" + xmlstring;
		var postback = document.getElementById("posturl");
		
		url = postback.value + "?forums=true";// + xmlstring;
		//alert(url);
		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(xmlstring);
	}

	
	function xmlhttpChange()
	{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4)
		{
		// if "OK"
		if (xmlhttp.status==200)
		{
		//*************  Do a response.redirect here to the menu? ************
		alert("Changes have been saved.");
		}
		else
		{
		alert("Problem sending XML data " + xmlhttp.status)
		}
		}
	}

	
	
} 

//********** SORTING CATEGORIES FINISH ********** //


function movescoUp(temp2)
{
	//alert("up");
	if(temp2 >1)
	{
		
		var current = eval("cell_" + temp2);
		//alert(current);
		var currentpos = eval("scoCollection.pos_" + temp2);
		var nextpos = eval("scoCollection.pos_" + (temp2-1));
		var temp = currentpos.value;
		currentpos.value = nextpos.value;
		nextpos.value = temp;
		var nextUp = eval("cell_" + (temp2 - 1));
		var nextUPHTML = nextUp.innerHTML;
		var currentHTML = current.innerHTML;
		
		current.innerHTML = nextUPHTML;
		nextUp.innerHTML = currentHTML;
		
		//preview();
	}
	
}

function movescoDown(temp2)
{
		var current = eval("cell_" + temp2);
		//alert(current);
		var nextUp = eval("cell_" + (temp2 + 1));
		var currentpos = eval("scoCollection.pos_" + temp2);
		var nextpos = eval("scoCollection.pos_" + (temp2+1));
		var temp = currentpos.value;
		currentpos.value = nextpos.value;
		nextpos.value = temp;
		var nextUPHTML = nextUp.innerHTML;
		var currentHTML = current.innerHTML;
		
		current.innerHTML = nextUPHTML;
		nextUp.innerHTML = currentHTML;
		
		//preview();

}


function movesCatUp(temp2)
{
	//alert("up");
	if(temp2 >1)
	{
		var current = eval("category_" + temp2);
		//alert(current);
		var currentpos = eval("scoCollection.pos_" + temp2);
		var nextpos = eval("scoCollection.pos_" + (temp2-1));
		var temp = currentpos.value;
		currentpos.value = nextpos.value;
		nextpos.value = temp;
		var nextUp = eval("category_" + (temp2 - 1));
		var nextUPHTML = nextUp.innerHTML;
		var currentHTML = current.innerHTML;
		
		current.innerHTML = nextUPHTML;
		nextUp.innerHTML = currentHTML;
		
		//preview();
	}
}

function moveCatDown(temp2)
{
		var current = eval("category_" + temp2);
		//alert(current);

		var nextUp = eval("category_" + (temp2 + 1));
		var currentpos = eval("scoCollection.pos_" + temp2);
		var nextpos = eval("scoCollection.pos_" + (temp2+1));
		var temp = currentpos.value;
		currentpos.value = nextpos.value;
		nextpos.value = temp;
		var nextUPHTML = nextUp.innerHTML;
		var currentHTML = current.innerHTML;
		
		current.innerHTML = nextUPHTML;
		nextUp.innerHTML = currentHTML;
		//preview();
}


function moveForumUp(cat,temp2)
{
	//alert("up");
	if(temp2 >1)
	{
		
		var current = eval("forum_" + cat + "_" + temp2);
		//alert(current);
		//var currentpos = eval("scoCollection.pos_" + temp2);
		var nextpos = eval("scoCollection.pos_" + (temp2-1));
		var temp = currentpos.value;
		currentpos.value = nextpos.value;
		nextpos.value = temp;
		var nextUp = eval("forum_" + cat + "_" + (temp2 - 1));
		var nextUPHTML = nextUp.innerHTML;
		var currentHTML = current.innerHTML;
		
		current.innerHTML = nextUPHTML;
		nextUp.innerHTML = currentHTML;
		
		//preview();
	}
	
}

function moveForumDown(cat,temp2)
{
		var current = eval("forum_" + cat + "_" + temp2);
		//alert(current);

		var nextUp = eval("forum_" + cat + "_" + (temp2 + 1));
		var currentpos = eval("scoCollection.pos_" + temp2);
		var nextpos = eval("scoCollection.pos_" + (temp2+1));
		var temp = currentpos.value;
		currentpos.value = nextpos.value;
		nextpos.value = temp;
		var nextUPHTML = nextUp.innerHTML;
		var currentHTML = current.innerHTML;
		
		current.innerHTML = nextUPHTML;
		nextUp.innerHTML = currentHTML;
		
		//preview();

}


function moveQuesUp(page,temp2)
{
	var questionArray = document.getElementById("questionList_" + page);
		questionArray = questionArray.innerHTML.split(",");
		//alert(questionArray);
		var current = eval("question_" + page + "_" + temp2);
		
		for(var x=0;x<questionArray.length;x++)
		{
			if(current.id == questionArray[x])
			{
				var currentpos = x;
				var nextpos = x - 1;
			}
		}
		if(questionArray[nextpos])
		{
			var current = eval("question_" + page + "_" + (currentpos+1));
			
			var nextUp = eval("question_" + page + "_" + (nextpos+1));
			var tempvalue = questionArray[currentpos];
			questionArray[currentpos] = questionArray[nextpos];
			questionArray[nextpos] = tempvalue;
			var questionArrayList = document.getElementById("questionList_" + page);
			questionArrayList.innerHTML = questionArray;
			
			var nextUPHTML = nextUp.innerHTML;
			var currentHTML = current.innerHTML;
		
			current.innerHTML = nextUPHTML;
			nextUp.innerHTML = currentHTML;
			
			var questionPosArrayObj = document.getElementById("questionList_" + page + "_Ids");
			var questionPosArray = questionPosArrayObj.innerHTML.split(",");
			var tempvalue = questionPosArray[currentpos];
			questionPosArray[currentpos] = questionPosArray[nextpos];
			questionPosArray[nextpos] = tempvalue;
			questionPosArrayObj.innerHTML = questionPosArray;
			
	
			createQuestionXml();
		}
	
}
function moveQuesDown(page,temp2)
{
		var questionArray = document.getElementById("questionList_" + page);
		questionArray = questionArray.innerHTML.split(",");
		
		var current = eval("question_" + page + "_" + temp2);
		
		for(var x=0;x<questionArray.length;x++)
		{
			if(current.id == questionArray[x])
			{
				var currentpos = x;
				var nextpos = x + 1;
			}
		}
		if(questionArray[nextpos])
		{
			var current = eval("question_" + page + "_" + (currentpos+1));
			
			var nextUp = eval("question_" + page + "_" + (nextpos+1));
			var tempvalue = questionArray[currentpos];
			questionArray[currentpos] = questionArray[nextpos];
			questionArray[nextpos] = tempvalue;
			var questionArrayList = document.getElementById("questionList_" + page);
			questionArrayList.innerHTML = questionArray;
			
			var nextUPHTML = nextUp.innerHTML;
			var currentHTML = current.innerHTML;
		
			current.innerHTML = nextUPHTML;
			nextUp.innerHTML = currentHTML;
			
			var questionPosArrayObj = document.getElementById("questionList_" + page + "_Ids");
			var questionPosArray = questionPosArrayObj.innerHTML.split(",");
			var tempvalue = questionPosArray[currentpos];
			questionPosArray[currentpos] = questionPosArray[nextpos];
			questionPosArray[nextpos] = tempvalue;
			questionPosArrayObj.innerHTML = questionPosArray;
			
			createQuestionXml();
		}
		
		
		
		//preview();

}




var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder){
	if (window.event&&event.srcElement.value.length>=maxlength){
		return false
	}     

	else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength){
		var pressedkey=/[a-zA-Z0-9\.\,\/]/ //detect alphanumeric keys
		if (pressedkey.test(String.fromCharCode(e.which)))
			e.stopPropagation()
	}
}

function limitsize(theform,thelimit){
	if (document.all)
		eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform)}
	else if (ns6)
		document.body.addEventListener('keypress', function(event) { restrictinput(thelimit,event,theform) }, true); 
}

function ShowMetaData(button,panel)
{
	var ele = document.getElementById(panel);
    if (ele != null)
    {
	if(ele.style.display == "none"){
		ele.style.display = "block";
		button.value = "Hide MetaData";
	}else{
		ele.style.display = "none";
		button.value = "Show MetaData";
	}
	}
}

function createQuestionXml()
{
	var pageCount = document.getElementById("pageCount").innerHTML;
	xmlstringform = "<QuestionList>";
	for(var x=0;x<pageCount;x++)
	{
		xmlstringform = xmlstringform + "<Page number=\"" + (x+1) + "\">";
		if(document.getElementById("questionList_" + (x+1) + "_Ids"))
		{
			var questions = document.getElementById("questionList_" + (x+1) + "_Ids").innerHTML.split(",");
			if(questions.length>1)
			{
				for(var y=0;y<questions.length;y++)
				{
					xmlstringform = xmlstringform + "<Question id=\"" + questions[y] + "\"/>";
				}
			}
		}
		xmlstringform = xmlstringform + "</Page>";
	}
	xmlstringform = xmlstringform + "</QuestionList>";
}

function saveForms() {
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	
	if (xmlhttp) {
		if(xmlstringform!=null)
		{
			
		//alert(xmlstringform);

		//url = "http://www.thinkingcap.info/Forums.aspx?forums=" + xmlstring;
		//url = "http://localhost/McMasterProject/Admin/DiscussionForums/Categories/Categories.aspx?forums=" + xmlstringform;
		
		var postback = document.getElementById("posturl");
		url = postback.value + "?forms=true";// + xmlstringform;
		
		//alert(url);

		xmlhttp.onreadystatechange=xmlhttpChange;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(xmlstringform);
//document.write(xmlHttp.responseText)

		//xmlhttp.send();
		}
	}
}

	
function xmlhttpChange() {
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
		// if "OK"
		if (xmlhttp.status==200) {
		//*************  Do a response.redirect here to the menu? ************
			alert("Changes have been saved.");
		}else {
			alert("Problem sending XML data " + xmlhttp.status)
		}
	}
}


// ****** Form Subquestions ***** //
function subquestion(subID){
//	alert(subID);
}



	
	

	