function popWin(winName,loc,height,width) {
	if (!width) width="525";
	if (!height) height="400";
	if (loc.match(/\?/)) {
		loc += "&pop=yes";
	} else {
		loc += "?pop=yes";
	}
	var newWin = window.open(loc, winName, "toolbar=no,location=no,titlebar=no,status=yes,resizable,scrollbars=yes,menubar=no,height=" + height + ",width=" + width);	
}
function popImg(loc,height,width) {
	if (!width) width="525";
	if (!height) height="400";
	var newWin = window.open('/img.php?img='+loc, 'graphics', "toolbar=no,location=no,titlebar=no,status=yes,scrollbars=no,menubar=no,height=" + height + ",width=" + width);	
}
//AJAX RELATED FUNCTIONS
function httpCnxn() {
	var http = null;
	if(window.XMLHttpRequest) {
		http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return http;
}

function toggleNew(type) {
	var id = "new" + type;
	var div = document.getElementById(id);
	var searchBox = document.getElementsByName(type + "Name")[0];
	var dropDown = document.getElementsByName(type + "Id")[0];
	var labels = document.getElementById(id).getElementsByTagName("LABEL");


	if (div.style.display != 'block') {
		div.style.display = 'block';
		if (searchBox) {
			searchBox.value = '';
			dropDown.value = ''; // when we have a search box type + "Id" is really a hidden field
			searchBox.previousSibling.className = '';
		} else {
			dropDown.previousSibling.className = '';
		}
		for (var i = 0; i < labels.length; i++) {
			labels[i].className = 'required';
		}
	} else {
		div.style.display = 'none';
		if (searchBox) {
			searchBox.previousSibling.className = 'required';
		} else {
			dropDown.previousSibling.className = 'required';
		}
		for (var i = 0; i < labels.length; i++) {
                        labels[i].className = '';
                }
	}
}

function searchPop(textField, type, formName, data) {
	var ttype = type;
	var popDiv = type+"popResults";
	var http = httpCnxn();
	http.onreadystatechange = function()  {
		if(http.readyState == 4) {
			document.getElementById(popDiv).innerHTML = "";
			document.getElementById(popDiv).style.display="inline";
			document.getElementById(popDiv).innerHTML = "<B>SEARCH RESULTS</B><BR>"+http.responseText;
		}
  	}
	if (type == "home" || type == "away") {
		ttype = "team";
	}
	url = "/manage/"+ttype+"/quicksearch.php";
	http.open('post', url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(ttype+"Name=" + textField.value + "&fieldType=" + type + "&formName=" + formName + data);
}

function moveData(theId, theText, type, formName) {
	var popDiv = type+"popResults";
	var idFld = type+"Id";
	var nmFld = type+"Name";
	if (type == "home" || type == "away") {
		nmFld = type+"Team";
		idFld = type+"TeamId";
	}
	var form = document.getElementsByName(formName);
	if (theText != "") {
		form[0].elements[idFld].value=theId;
		form[0].elements[nmFld].value=theText;
	}
	document.getElementById(popDiv).innerHTML = "";
	document.getElementById(popDiv).style.display = "none";
}

function delConfirm(tableName, idFld) {
	if (confirm("\nAre you sure you want to delete?\n")) {
		var http = httpCnxn();
		http.onreadystatechange = function()  {
			if(http.readyState == 4) {
				var rowId = "row"+idFld;
				var trow = document.getElementById(rowId);
				trow.parentNode.removeChild(trow);
			}
	  	}
		var newStr = "";
		newStr = "&"+tableName+"Id="+idFld;
		
		var delUrl = "/manage/index.php";
		http.open('post', delUrl, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.send("module="+tableName+"-save&delete=yes"+newStr);
	}
}

function delConfirm2(tableName) {
	if (arguments < 2) return;
	
	if (confirm("\nAre you sure you want to delete?\n")) {
		var extra = "";
		var idFld = "row";
		for (var i = 1; i < arguments.length; i++) {
			var t = arguments[i].split('~');
			extra += "&" + t[0] + "=" + t[1];
			idFld += t[1];
		}

		var http = httpCnxn();
		http.onreadystatechange = function() {
			if(http.readyState == 4) {
                                var trow = document.getElementById(idFld);
                                trow.parentNode.removeChild(trow);
				
                        }
		}

		var delUrl = "/manage/index.php";
		http.open('post', delUrl, true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.send("module="+tableName+"-save&delete=yes"+extra);
	}
	
}

function batchDelete(tableName) {
	if (confirm("\nAre you sure you want to delete all checked records?\n")) {
		if (confirm("\nAre you really sure you want to delete all checked records?\n")) {
			var theForm = document.postresults;
			var idList = "";
			var rowList = new Array();
			for (var s=0;s<theForm.elements.length;s++) {
				if (theForm.elements[s].type == "checkbox" && theForm.elements[s].name.match(/chk/) && theForm.elements[s].checked) {
					idList += theForm.elements[s].value+", ";
					rowList.push(theForm.elements[s].value)
				}
			}
			idList = idList.substring(0, idList.lastIndexOf(", "));
			var http = httpCnxn();
			http.onreadystatechange = function()  {
				if(http.readyState == 4) {
					for (var u=0;u<rowList.length;u++) {
						var rowId = "row"+rowList[u];
						var trow = document.getElementById(rowId);
						trow.parentNode.removeChild(trow);
					}
				}
		  	}
			var delUrl = "/manage/index.php";
			http.open('post', delUrl, true);
			http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			http.send("module="+tableName+"-save&delete=yes&"+tableName+"Id="+idList);
		}
	}
}

function updateRecords(tableName, state) {
	var theForm;
	if (document.postresults) {
		theForm = document.postresults;
	} else {
		theForm = document.forms["post"+tableName];
	}
	var idList = "";
	for (var s=0;s<theForm.elements.length;s++) {
		if (theForm.elements[s].name.match(/chk/) && theForm.elements[s].checked) {
			idList += theForm.elements[s].value+", ";
			document.getElementById("stat"+theForm.elements[s].value).innerHTML = "<IMG BORDER=0 SRC=\"/manage/images/icn_"+state+".png\">";
		}
	}
	idList = idList.substring(0, idList.lastIndexOf(", "));
	var http = httpCnxn();
	http.onreadystatechange = function()  {
		if(http.readyState == 4) {
		}
  	}
	var delUrl = "/manage/index.php";
	http.open('post', delUrl, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send("module="+tableName+"-save&publish=yes&"+tableName+"Id="+idList+"&state="+state);
}

function changeOrder(contentId, place) {
	var theForm;
	theForm = document.forms["postcontent"];
	var http = httpCnxn();
	http.onreadystatechange = function() {
		if(http.readyState == 4) {
		}
  	}
	http.open('post', "/manage/index.php", true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send("module=content-save&reorder=yes&contentId="+contentId+"&place="+place);
}

//END AJAX SECTION

function checkAll(formName) {
	var theForm = document.forms[formName];
	var state = theForm.master.checked;
	for (var s=0;s<theForm.elements.length;s++) {
		if (theForm.elements[s].name.match(/chk/)) {
			theForm.elements[s].checked = state;
		}
	}
}
function isValid (theField) {
	var returnVal = true;
	if (theField.type.match(/selec/)) {
		if (theField.selectedIndex <= 0) {
			returnVal = false;
		}
	} else if (theField.type.match(/checkbox|radio/)) {
		if (theField.checked == false) {
			returnVal = false;
		}
	}  else {
		if (theField.value.match(/^ /) || theField.value == "") {
			returnVal = false;
		}
	}
	return returnVal;
}
function validForm (formName) {
	var daForm = document.forms[formName];
	var allLabels = daForm.getElementsByTagName('label');
	var end = allLabels.length;
	var errmsg = "";
	//uncomment if multiple password fields are being used
	var pinc = 0;
	var passArr = new Array();
	for (var x=0;x<end;x++) {
		txt = "";
		if (allLabels[x].className == "required") {
			var theFld = daForm.elements[allLabels[x].htmlFor];
			//uncomment if multiple password fields are being used
			if (theFld.type == "password" && theFld.name != "oldpasswd") {
				passArr[pinc] = theFld.value
				pinc++;
			}
			if (!theFld.disabled && theFld.name != "control") {
				if (theFld.name == "email") {
					var mailCheck = theFld.value.match(/([A-Z0-9\.\-\_]+)\@([A-Z0-9\.\-\_]+)\.(com|net|edu|org|biz|us|info|gov)/i);
					if(mailCheck == null) {
						theFld.className="required";
						if (allLabels[x]) {
							txt = allLabels[x].firstChild.nodeValue;
							errmsg += "<LI><B>"+txt+"<BR>"+"</B><BR>is not in correct format.  Should be username@server.ext.</LI>";
						}
					}
				} else {
					var isval = isValid(theFld);
					if (!isval) {
						theFld.className="required";
						if (allLabels[x]) {
							txt = allLabels[x].firstChild.nodeValue;
							errmsg += "<LI><B>"+txt+"</B></LI>";
						}
					} else {
						theFld.className="";
					}
				}
			}
		}
	}
	//uncomment if multiple password fields are being used
	if (passArr[0] != passArr[1]) {
		errmsg += "\n\nPasswords do not match\n";
	}
	if (errmsg != "") {
		document.getElementById("errorbox").innerHTML ="<DIV CLASS=\"top\"><INPUT TYPE=\"button\" onClick=\"closeError\(\);\" VALUE=\"x\"></DIV><BR><SPAN CLASS=\"warn\">The following fields were not completed correctly!</SPAN><BR><BR>"+errmsg
		document.getElementById("errorbox").style.visibility="visible";
		return false;
	} else {
		return true;
	}
}

function enableOther (qId) {
	var daForm = document.postreg;
	var q = "question["+qId+"][]";
	var otherQ = 'otherquestion['+qId+']';
	var otherSource = daForm.elements[otherQ];
	
	var daSource = daForm.elements[q];
	if (daSource.value == "Other") {
		otherSource.className="";
		otherSource.disabled=false;
	} else {
		otherSource.className="grayout";
		otherSource.disabled=true;
	}
}
function enable(fldNm) {
	var daForm = document.postreg;
	if (!daForm.elements['question[13]'].checked) {
		daForm.elements['question[14]'].className="";
		daForm.elements['question[14]'].disabled=false;
		daForm.elements['question[15]'].className="";
		daForm.elements['question[15]'].disabled=false;
		daForm.elements['question[16]'].className="";
		daForm.elements['question[16]'].disabled=false;
	} else {
		daForm.elements['question[14]'].className="grayout";
		daForm.elements['question[14]'].disabled=true;
		daForm.elements['question[15]'].className="grayout";
		daForm.elements['question[15]'].disabled=true;
		daForm.elements['question[16]'].className="grayout";
		daForm.elements['question[16]'].disabled=true;
	}
}
function closeError(){
	document.getElementById('errorbox').innerHTML = "";
	document.getElementById('errorbox').style.visibility='hidden';
}

function isBlankForm(formName) {
	//formName = document.formName;
	var dispText = "";
	var fieldVal;
	var retVal = false;
	for (i=0;i<=formName.length-1;i++) {
		var thisEleType = formName.elements[i].type;
		var thisField = formName.elements[i];
		if (thisEleType != "reset" && thisEleType != "submit" && thisEleType != "checkbox" && thisEleType != "hidden" && thisEleType != "radio" && thisField.name != "searchCrit") {
			if (thisEleType == "select-one" || thisEleType == "select-multiple") {
				if (thisField.selectedIndex != -1  && thisField.selectedIndex != 0) {
					fieldVal = thisField.options[thisField.selectedIndex].value;
					retVal = true;
				}
			} else {
				
				fieldVal = thisField.value;
				if(thisField.name == "keyword" && fieldVal.length > 0) {
					retVal = true;
				}
			}
			if (fieldVal != "  " && fieldVal.length > 1 && fieldVal != "TODAY") {
				retVal = true;
			}
		}
	}
	return retVal;
}


function moveField (formName, fieldName, dir) {
	var pos = 0;
	var theForm = document.forms[formName];
	var thefield = theForm.elements[fieldName];
	sel = thefield.options.selectedIndex;
	if (dir == "up" && sel != 0) {
		pos = sel-1;
	}
	if (dir == "dn" && sel != thefield.length-1) {
		pos = sel+2;
	}
	if (thefield.options.selectedIndex == "-1") {
		alert("You must select an item in list before trying to change the order");
	} else {
		thefield.insertBefore(thefield[sel], thefield[pos])
	}
}
function tabIt(tab, dirName) {
	var tablist = document.getElementById("tabbar").getElementsByTagName("LI");

	for (var j=0;j<tablist.length; j++) {
		var strip = tablist[j].id.replace("tab", "");
		tablist[j].className = 'taboff';
		tablist[j].firstChild.className = 'taboff';
		document.getElementById("body"+strip).style.visibility='hidden';
		document.getElementById("body"+strip).style.display='none';
	}
	var tabName = "tab"+tab;
	var bodyName = "body"+tab;
	document.getElementById(tabName).className='tabon';
	document.getElementById(tabName).firstChild.className='tabon';
	document.getElementById(bodyName).style.visibility='visible';
	document.getElementById(bodyName).style.display='block';

	var http = httpCnxn();
	http.onreadystatechange = function()  {
		if(http.readyState == 4) {
		}
  	}
	url = "/manage/lib/settab.php";
	http.open('post', url, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send("dirName=" + dirName +"&tabName=" + tab);
}

function emoticon(theForm) {
	var linkText = theForm.menuLink.options[theForm.menuLink.selectedIndex].text;
	rExp = /\s\s\s\s/i;
	linkText = linkText.replace(rExp, "");
	var linkId = theForm.menuLink.options[theForm.menuLink.selectedIndex].value;
	var text = " <A HREF=\"/index.php?page="+linkId+"\">"+linkText+"</A> ";

	var txtarea = theForm.contentBlock;
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? caretPos.text + text + ' ' : caretPos.text + text;
		txtarea.focus();
	} else if (txtarea.selectionStart || txtarea.selectionStart == '0') { 
		var startPos = txtarea.selectionStart; 
		var endPos = txtarea.selectionEnd; 
		txtarea.value = txtarea.value.substring(0, startPos) + text + txtarea.value.substring(endPos, txtarea.value.length); 
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

function popTableName(thisForm) {
	document.forms[thisForm].action = "";
	document.forms[thisForm].submit();
}
