//JS Document

function exCol(elem) {
	var newstyle;
	var curr = document.getElementById(elem).style.display;
	var currArrow = document.getElementById(elem+'arrow');
	if(curr == 'block'){
		newstyle='none';
		currArrow.src='images/arrowClosed.gif';
		currArrow.alt='Expand Tier';
	}
	else{
		newstyle='block';
		currArrow.src='images/arrowExpanded.gif';
		currArrow.alt='Close Tier';
	}
	document.getElementById(elem).style.display = newstyle;

	return;
}

function popFP() {
	window.open("/community/forgot_passwd.php","fpWin","width=400,height=200,resizeable=1,location=0,status=0,menubar=0,toolbar=0");
	return;
}

function popPW() {
	window.open("/community/update_pw.php","fpWin","width=400,height=350,resizeable=1,location=0,status=0,menubar=0,toolbar=0");
	return;
}

function matchPwds() {
	var pworig = document.getElementById('newpw').value;
	var pwcopy = document.getElementById('cnewpw').value;
	if(pworig == pwcopy) return true;
		else alert("Those passwords do not match");
	return false;
}

function runUpdate() {
	var op_url = window.opener.document.location;

	window.opener.document.location = op_url;

	window.close();

	return;
}

function quickVal(fields) {
	for(i in fields) {
		if($(fields[i]).value == '')
			return false;
	}
	return true;
}

function ddSet(ddId, tgt) {
	if((elem = document.getElementById(ddId)) != undefined) {
		max = elem.options.length;
		for(i=0;i<max;++i)
			if(elem.options[i].value == tgt)
				elem.selectedIndex = i;
	}

	return;
}

function editWin(type, id) {
	var winwidth = 600;
	var winheight = 400;
	var url = '';


	switch(type) {
		case 'post':
			url = '/community/actions/disc_editpost.php?id=' + id;
		break;
		
		case 'thread':
			url = '/community/actions/disc_editthread.php?id=' + id;
		break;

		case 'topic':
		default:
			url = '/community/actions/disc_edittopic.php?id=' + id;
		break;

	}

	window.open(url,'name','height=' + winheight + ',width=' + winwidth + ', scrollbars=1');
}

function runForumPreview() {
	document.forms["addPost"].action = "#reply";
	document.forms["addPost"].submit();
	return;
}

function runForumPost() {
	if(document.getElementById("Post Subject").value == "") {
		alert("Please provide a subject for this thread.");
	} else {
		document.forms["addPost"].submit();
	}
	
	return;
}

function runThreadDelete(id) {
	var ans = confirm("Are you sure you wish to delete this thread and all the posts in it?");
	if(ans) {
		window.location = '/community/actions/disc_deletethread.php?id=' + id;
	}
}

function insertTag(myFieldID, tagn1, tagn2) {
	//v2.0, works on the Fox
	//IE support
	myField = document.getElementById(myFieldID);

	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = tagn1 + sel.text + tagn2;
	} else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos) + tagn1 + myField.value.substring(startPos, endPos) + tagn2 + myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += tagn1 + tagn2;
		//alert('IE Fail');
	}
}

function changeSchedule(myVal, courseid) {
	var retval = 0;
	
	if(myVal == 0) {	//Weekly, so increment by the value of the week dropdown (+1 offset)
		retval = document.getElementById('day_of_week_' + courseid).value;
	} else {
		retval = myVal;
	}
	document.getElementById('sched_' + courseid).value = retval;
	return;
}