//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 searchArticle(textField) {
	http = httpCnxn();
	http.onreadystatechange = function()  {
		if(http.readyState == 4) {
			document.getElementById('popResults').innerHTML = "";
			document.getElementById('popResults').style.display="inline";
			document.getElementById('popResults').innerHTML = "<B>SEARCH RESULTS</B><BR>"+http.responseText;
		}
  	}
	http.open('post', '/manage/article/quicksearch.php', true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send("headline=" + textField.value);
}
function moveData(theId, theText) {
	document.forms[0].articleId.value=theId;
	document.forms[0].searchText.value=theText;
	document.getElementById('popResults').innerHTML = "";
	document.getElementById('popResults').style.display = "none";
}

function popWin(winName,loc,height,width) {
	if (!width) width="525";
	if (!height) height="400";
	var newWin = window.open(loc, winName, "toolbar=no,location=no,titlebar=no,status=yes,resizable,scrollbars=yes,menubar=no,height=" + height + ",width=" + width);	
}

function storeCaret(textEl) {
	if (textEl.createTextRange) textEl.caretPos = document.selection.createRange().duplicate();
}
function emoticon(text) {
	var txtarea = document.postaddedit.body;
	text = text + ' ';
	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 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 (daForm) {
	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+"</B> is not in correct format. Should be 'username@server.ext'.</LI>";
							//alert(txt+"is not in correct format. Email should be username@domain.ext.");
						}
					}
				} else {
					var isval = isValid(theFld);
					if (!isval) {
						theFld.className="required";
						if (allLabels[x]) {
							txt = allLabels[x].firstChild.nodeValue;
							errmsg += "<LI><B>"+txt+"</B> is blank.</LI>";
						}
					} else {
						theFld.className="";
					}
				}
			}
		}
	}
	//uncomment if multiple password fields are being used
	if (passArr[0] != passArr[1]) {
		errmsg += "\n\n<b>Passwords do not match</b>\n";
	}
	if (errmsg != "") {
		document.getElementById("errorbox").innerHTML ="<DIV CLASS=\"top\"><INPUT TYPE=\"button\" onClick=\"closeError\(\);\" VALUE=\"x\"></DIV><SPAN CLASS=\"warn\">The following fields were not completed correctly!</SPAN><ul>"+errmsg+"</ul>";
		document.getElementById("errorbox").style.visibility="visible";
		return false;
	} else {
		return true;
	}
}

function closeError(){
	document.getElementById('errorbox').innerHTML = "";
	document.getElementById('errorbox').style.visibility='hidden';
}

function popTableName(thisForm) {
	document.forms[thisForm].action = "";
	document.forms[thisForm].submit();
}

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 addtolist (fieldName, optId, optText) {
	var theForm = document.postorder;
	var thefield = theForm.elements[fieldName];
	var truth = true;
	for (n=0;n<thefield.options.length;n++) {
		if (thefield.options[n].value == optId) {
			truth = false;
		}
	}
	if ((fieldName.match(/flash/)) && (thefield.options.length ==3)) {
		truth = false;
		alert ("Only 3 flash items are alllowed at the same time");
	}
	if (optId && optText && truth) {
		thefield[thefield.options.length] = new Option (optText, optId, true);
	}
}
function pullfromlist (fieldName) {
	var theForm = document.postorder;
	var thefield = theForm.elements[fieldName];
	var theIndex = theForm.elements[fieldName].selectedIndex;
	if (theIndex >= 0) {
		thefield.remove(theIndex);
		theLen = theForm.elements[fieldName].length;
		for(j=0;j<theLen;j++) {
			theForm.elements[fieldName].options[j].selected=true;
		}
		theForm.submit();
	} else {
		alert("You must first choose an item from the list to remove!");
	}
}

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])
	}
	
}

maxKeys = 1000;
var IE = (document.all) ? 1 : 0;
var DOM = 0; 
if (parseInt(navigator.appVersion) >=5) {DOM=1};

function txtshow( txt2show ) {

// Detect Browser

	if (DOM) {
		var viewer = document.getElementById("txtmsg");
		viewer.innerHTML=txt2show;
	} else if(IE) {
		document.all["txtmsg"].innerHTML=txt2show;
	}
}

function keyup(what) 
{
  var str = new String(what.value);
  var len = str.length;
  var showstr = len + " characters of " + maxKeys + " entered";
  if (len > maxKeys) showstr += '<br>Some information will be lost, please revise your entry';
  txtshow( showstr );
}
function popPayPal() {
	var itemNumber = document.postaddedit.item_number.value;
	var strings= itemNumber.split("_");
	document.postaddedit.amount.value= strings[1];
}

function showForm(divName, state) {
	document.getElementById(divName).style.display = state;
}
function showPay(divName) {
	showForm("check", "none")
	showForm("payPal", "none")
	if (divName!="0") {
		showForm(divName, "block")
	}
}