var funColor = new Array();

//funColor[0] = "#666666";
//funColor[1] = "#CCCCCC";

funColor[0] = "#FFFFFF";
funColor[1] = "#CCFFCC";

var resources = new Array();

var usual = 14; // ususal length for SELECT statement

function hideall(num){
	num = parseInt(num);
	if (document.getElementById("sel_bottom_" + num)){
		document.getElementById("sel_bottom_" + num).style.display = "none";
		hideall(num + 1);
	}
}

function drop_down(num){
	if(document.getElementById("sel_bottom_" + num).style.display == "block"){
		hideall(0);
		//document.getElementById("sel_bottom_" + num).style.display = "none";
	} else {
		hideall(0);
		if (document.getElementById("current_m_" + num)){
			document.getElementById("current_m_" + num).style.backgroundColor = funColor[1];
		}
		document.getElementById("sel_bottom_" + num).style.display = "block";
	}
}

function perform(num, text, obj, zz, medium, affect, limit, field, name){
	drop_down(num);
	obj.style.background = funColor[0];
	var temp = document.getElementById("sel_text_" + num).innerHTML;
	
	text = text.replace(/^\s+/i, "");
	var re = new RegExp(String.fromCharCode(160),"gi");
	text = text.replace(re, "");
		
	//alert("-" + text + "-");
	
	if (text.length > usual){
		text = text.substr(0, usual - 3).replace(/\s+$/gi, "") + "...";
	}

	text = text + " " + temp.substr(temp.indexOf("<"), temp.length - temp.indexOf("<"));
	if (document.getElementById("current_m_" + num)){
		document.getElementById("current_m_" + num).id = "nohru";
	}
	obj.id = "current_m_" + num;
	
	document.getElementById("sel_text_" + num).innerHTML = text;
	document.getElementsByName(field)[0].value = zz;
	
	var deng = "";
	
	// Hide all the OTHER select fields if multiple select fields connects to the current one
	if (medium >= 0){
		for (var i = affect; i <= limit; i++){
			if (document.getElementById("sel_top_" + i)){
				if (i == medium){
					document.getElementById("sel_top_" + i).style.display = "block";
				} else {
					document.getElementById("sel_top_" + i).style.display = "none";
					if (document.getElementById("current_m_" + i)){
						var temp = document.getElementById("sel_text_" + i).innerHTML;
						
						var tmpName = name;
						tmpName = tmpName.replace(/^\s+/, "");
						tmpName = tmpName.replace(re, "");
						if (tmpName.length > usual){
							//alert(tmpName.length + "\n-" + tmpName + "-");
							tmpName = tmpName.substr(0, usual - 3).replace(/\s+$/gi, "") + "...";
						}

						tmpName = tmpName + " " + temp.substr(temp.indexOf("<"), temp.length - temp.indexOf("<"));
						
						document.getElementById("current_m_" + i).style.backgroundColor = funColor[0];
						document.getElementById("current_m_" + i).id = "nohru";
						
						document.getElementById("sel_text_" + i).innerHTML = tmpName;
					}
				}
			}
		}
	}
	/*
	document.getElementById("debug").value =
		document.getElementsByName(field)[0].value + " / " + affect + "\n" + deng +
		"\n\n" + limit;
	*/	
}

function recurrent(num, obj){
	obj.style.backgroundColor = funColor[0];
}

function incurrent(num, obj){
	if (document.getElementById("current_m_" + num)){
		if (obj.id && obj.id.indexOf("current_m_" + num) < 0){
			document.getElementById("current_m_" + num).style.backgroundColor = funColor[0];
		} else {
			document.getElementById("current_m_" + num).style.backgroundColor = funColor[1];
		}
	}
	obj.style.backgroundColor = funColor[1];
}


function fixE(e){
	if (typeof e == 'undefined') e = window.event;
	if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
	if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
	if (typeof e.currentTarget == 'undefined') e.currentTarget = e.srcElement;
	if (typeof e.target == 'undefined') e.target = e.srcElement;
	return e;
}
	
function wheel(event){
	event = fixE(event);
	//document.getElementById("dbg").value = (zung++) + " " + event.target.tagName;
	if (event.target.id != "nohru" && event.target.id.indexOf("current_m_") < 0 && event.target.id.indexOf("sel_text_") < 0){
		hideall(0);
		//document.getElementById("debug").value = event.target.id;
	}
}

function save(id, thing){
	if (document.getElementById("TT" + thing).value == cats[thing]){
		document.getElementById("ZG" + thing).innerHTML = cats[thing];
		document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: blue; width: 70px;\" onclick=\"edit(" + id + ", " + thing + ")\" value=\"Edit\">";
	} else {
		document.catForm.action.value = "savetopic";
		document.catForm.topicname.value = document.getElementById("TT" + thing).value;
		document.catForm.item.value = id;
		document.catForm.submit();
	}
}

function edit(id, thing){
	cats[thing] = document.getElementById("ZG" + thing).innerHTML;
	var temp = document.getElementById("ZG" + thing).innerHTML;

	document.getElementById("ZG" + thing).innerHTML = "<input id=\"TT" + thing + "\" type=\"text\"  maxlength=\"70\" style=\"width: 160px;\" value=\"" + temp + "\">";
	document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: magenta; width: 70px;\" onclick=\"save(" + id + ", " + thing + ")\" value=\"Save\">";
}

function deletetopic(id){
	document.catForm.action.value = "deletetopic";
	document.catForm.item.value = id;
	if (confirm("Are you really want to delete this Topic?")){
		document.catForm.submit();
	}
}

// SUBTOPIC
function savesubtopic(id, thing){
	if (document.getElementById("TT" + thing).value == cats[thing]){
		document.getElementById("ZG" + thing).innerHTML = cats[thing];
		document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: blue; width: 70px;\" onclick=\"editsubtopic(" + id + ", " + thing + ")\" value=\"Edit\">";
	} else {
		document.catForm.action.value = "savesubtopic";
		document.catForm.topicname.value = document.getElementById("TT" + thing).value;
		document.catForm.item.value = id;
		document.catForm.submit();
	}
}

function editsubtopic(id, thing){
	cats[thing] = document.getElementById("ZG" + thing).innerHTML;
	var temp = document.getElementById("ZG" + thing).innerHTML;

	document.getElementById("ZG" + thing).innerHTML = "<input id=\"TT" + thing + "\" type=\"text\"  maxlength=\"70\" style=\"width: 160px;\" value=\"" + temp + "\">";
	document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: magenta; width: 70px;\" onclick=\"savesubtopic(" + id + ", " + thing + ")\" value=\"Save\">";
}

function deletesubtopic(id){
	document.catForm.action.value = "deletesubtopic";
	document.catForm.item.value = id;
	if (confirm("Are you really want to delete this Subtopic?")){
		document.catForm.submit();
	}
}

function showsubtopics(id, title){
	document.getElementById("subtitle").innerHTML = "Subtopics for <b>" + title + "</b>:";
	
	document.subsForm.item.value = id;

	clearsubs(totalsubs);

	for (var i = 0; i < topics[id].length; i++){
		if (document.getElementById("check" + topics[id][i])){
			document.getElementById("check" + topics[id][i]).checked = true;
		}
	}
}

// CURRICULUM
function savecurriculum(id, thing){
	if (document.getElementById("TT" + thing).value == cats[thing]){
		if (dots[thing] == 1){
			cats[thing] = "\&nbsp;\&nbsp;&#149;\&nbsp;" + cats[thing];
		}
		document.getElementById("ZG" + thing).innerHTML = cats[thing];
		document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: blue; width: 70px;\" onclick=\"editcurriculum(" + id + ", " + thing + ")\" value=\"Edit\">";
	} else {
		document.catForm.action.value = "savecurriculum";
		document.catForm.topicname.value = document.getElementById("TT" + thing).value;
		document.catForm.item.value = id;
		document.catForm.submit();
	}
}

function editcurriculum(id, thing){
	cats[thing] = document.getElementById("ZG" + thing).innerHTML;
	if (cats[thing].indexOf("•") >= 0){
		//alert("•".charCodeAt(0));
		dots[thing] = 1;
		cats[thing] = cats[thing].replace("\&nbsp;\&nbsp;•\&nbsp;", "");
	} else {
		dots[thing] = 0;
	}

	document.getElementById("ZG" + thing).innerHTML = "<input id=\"TT" + thing + "\" type=\"text\"  maxlength=\"70\" style=\"width: 160px;\" value=\"" + cats[thing] + "\">";
	document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: magenta; width: 70px;\" onclick=\"savecurriculum(" + id + ", " + thing + ")\" value=\"Save\">";
}

function deletecurriculum(id){
	document.catForm.action.value = "deletecurriculum";
	document.catForm.item.value = id;
	if (confirm("Are you really want to delete this Curriculum?")){
		document.catForm.submit();
	}
}

// RESOURCES
function saveresources(id, thing){
	if (document.getElementById("TT" + thing).value == cats[thing]){
		document.getElementById("ZG" + thing).innerHTML = cats[thing];
		document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: blue; width: 70px;\" onclick=\"editresources(" + id + ", " + thing + ")\" value=\"Edit\">";
	} else {
		document.catForm.action.value = "saveresources";
		document.catForm.topicname.value = document.getElementById("TT" + thing).value;
		document.catForm.item.value = id;
		document.catForm.submit();
	}
}

function editresources(id, thing){
	cats[thing] = document.getElementById("ZG" + thing).innerHTML;
	var temp = document.getElementById("ZG" + thing).innerHTML;

	document.getElementById("ZG" + thing).innerHTML = "<input id=\"TT" + thing + "\" type=\"text\"  maxlength=\"70\" style=\"width: 160px;\" value=\"" + temp + "\">";
	document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: magenta; width: 70px;\" onclick=\"saveresources(" + id + ", " + thing + ")\" value=\"Save\">";
}

function deleteresources(id){
	document.catForm.action.value = "deleteresources";
	document.catForm.item.value = id;
	if (confirm("Are you really want to delete this Resource?")){
		document.catForm.submit();
	}
}

// GRADES
function savegrades(id, thing){
	if (document.getElementById("TT" + thing).value == cats[thing]){
		document.getElementById("ZG" + thing).innerHTML = cats[thing];
		document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: blue; width: 70px;\" onclick=\"editgrades(" + id + ", " + thing + ")\" value=\"Edit\">";
	} else {
		document.catForm.action.value = "savegrades";
		document.catForm.topicname.value = document.getElementById("TT" + thing).value;
		document.catForm.item.value = id;
		document.catForm.submit();
	}
}

function editgrades(id, thing){
	cats[thing] = document.getElementById("ZG" + thing).innerHTML;
	var temp = document.getElementById("ZG" + thing).innerHTML;

	document.getElementById("ZG" + thing).innerHTML = "<input id=\"TT" + thing + "\" type=\"text\"  maxlength=\"70\" style=\"width: 160px;\" value=\"" + temp + "\">";
	document.getElementById("GB" + thing).innerHTML = "<input type=\"button\" style=\"color: magenta; width: 70px;\" onclick=\"savegrades(" + id + ", " + thing + ")\" value=\"Save\">";
}

function deletegrades(id){
	document.catForm.action.value = "deletegrades";
	document.catForm.item.value = id;
	if (confirm("Are you really want to delete this Grade?")){
		document.catForm.submit();
	}
}

// ARTICLES
function addnewarticle(){
	document.catForm.action.value = "createarticle";
	document.catForm.method = "GET";
	document.catForm.submit();
}

function deletearticle(id){
	if (confirm("Are you really want to delete this Article?")){
		document.catForm.action.value = "delart";
		document.catForm.item.value = id;
		document.catForm.submit();
	}
}

function editarticle(id){
	document.catForm.method = "GET";
	document.catForm.action.value = "editarticle";
	document.catForm.item.value = id;
	document.catForm.submit();
}

function addArticle(){
	document.addForm.submit();
}
///////////////
function clearsubs(totalsubs){
	for (var i = 0; i <= totalsubs; i++){
		if (document.getElementById("check" + i)){
			document.getElementById("check" + i).checked = false;
		}
	}	
}

function openWindow(link){
	var newWidth = 480;
	var newHeight = 370;
	var selectArt = window.open(link, 'selectArt',
			'toolbar=no,directories=no,status=yes,menubar=no,scrollbars=yes,location=no,resizable=yes,width='+newWidth+'px,height='+newHeight+'px');
	selectArt.focus();
}

function yo(obj){
	if (obj.checked == true){
		obj.parentNode.style.backgroundColor = '#339933';
	} else {
		obj.parentNode.style.backgroundColor = '#FFFFFF';
	}
}

function getAll(){
	var ret = "";
	var help = 0;
	for (var i = 0; i < total+1; i++){
		if (document.getElementById("ca" + i) && document.getElementById("ca" + i).checked == true){
			if (ret != ""){
				ret += ", ";
			}
			ret += document.getElementById("ca" + i).value;
			help++;
		}
	}
	if (opener.document.addForm.related){
		opener.document.addForm.related.value = ret;
	}
	opener.document.getElementById("rel_help").innerHTML = help + " Selected";
	window.close();
}

function setAll(){
	var set = new Array();
	if (opener.document.addForm.related.value){
		set = opener.document.addForm.related.value.split(/,\s*/);
	}
	for (var i = 0; i < set.length; i++){
		if (document.getElementById("ca" + set[i])){
			document.getElementById("ca" + set[i]).checked = true;
			document.getElementById("ca" + set[i]).parentNode.style.backgroundColor = '#339933';
		}
	}
}

function comments(){
	var mes ="";
	with (document.commForm){
		if (name.value == ""){
			mes += "Your Name\n";
		}
		if (email.value == ""){
			mes += "Your E-mail Address\n";
		}
		if (subject.value == ""){
			mes += "Subject for Your Message\n";
		}
		if (message.value == ""){
			mes += "Your Message\n";
		}
	}
	
	if (mes == ""){
		document.commForm.submit();
	} else {
		alert("Sorry, You Forgot to Fill Out the Following Fields:\n\n" + mes);
	}
}

function divMouseOn(obj, url){
	obj.style.background = "url(" + url + ")";
	obj.style.border = "2px solid #3F7229";
	obj.style.color = "#FFFFFF";
}

function divMouseOut(obj, url){
	obj.style.background = "url(" + url + ")";
	obj.style.border = "2px solid #6E9F19";
	obj.style.color = "#065510";
}

function lego(item){
	if (document.getElementById("subdivbox" + item).style.display == "none"){
		document.getElementById("subdivbox" + item).style.display = "block";
		document.getElementById("toggle" + item).src = "/health/shp/html-2007/minus.gif";
	} else {
		document.getElementById("subdivbox" + item).style.display = "none";
		document.getElementById("toggle" + item).src = "/health/shp/html-2007/plus-on.gif";
	}
}

