//DECLARING VARIABLES FOR POPUP WINDOWS
var winWhatsThis;
var winEmailFriend;
var winPrintContent;
var winSeminarViewPopup;
var winOrderPrintView;
var blnOpenPrintDialog;
var winEthicsCase;

function setInnerHtmlFromHttpRequest(Id, Url)
{
	var xmlhttp = null;
	if (window.XMLHttpRequest)
	{	//code for Mozilla, etc.
		xmlhttp = new XMLHttpRequest()
	} else if(window.ActiveXObject)
	{	//code for IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	if(xmlhttp)
	{	//wire up the event to handle the response on successful load
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState==4 && xmlhttp.status==200 && xmlhttp.responseText!='') {
				var container = document.getElementById(Id);
				container.innerHTML = xmlhttp.responseText;
				container.style.display = '';
			}
		}
		
		xmlhttp.open("GET",Url,true)
		xmlhttp.send(null)
	}
}

function writeResponseFromHttpRequest(Url)
{
	var xmlhttp = null;
	if (window.XMLHttpRequest)
	{	//code for Mozilla, etc.
		xmlhttp = new XMLHttpRequest()
	} else if(window.ActiveXObject)
	{	//code for IE
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	
	if(xmlhttp)
	{	//wire up the event to handle the response on successful load
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState==4 && xmlhttp.status==200 && xmlhttp.responseText!='') {
				alert(xmlhttp.responseText);
				document.write(xmlhttp.responseText);
			}
		}
		
		xmlhttp.open("GET",Url,true)
		xmlhttp.send(null)
	}
}

//begin helpers for wiring-up event handlers
function addEventHandler_OnLoad(oFunc)
{
	var oldHandler = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = oFunc;
	} else {
		window.onload = function() {
			oldHandler();
			oFunc();
		}
	}
}

function addEventHandler_OnUnload(oFunc)
{
	var oldHandler = window.onunload;
	if (typeof window.onunload != 'function') {
		window.onunload = oFunc;
	} else {
		window.onunload = function() {
			oldHandler();
			oFunc();
		}
	}
}

function addEventHandler_OnLoadAndUnload(oFunc)
{
	addEventHandler_OnLoad(oFunc);
	addEventHandler_OnUnload(oFunc);
}

function addEventHandler_OnSubmit(oForm,oFunc)
{
	var oldHandler = oForm.onsubmit;
	if (typeof oForm.onsubmit != 'function') {
		oForm.onsubmit = oFunc;
	} else {
		oForm.onsubmit = function() {
			oldHandler();
			oFunc();
		}
	}
}

//date funtions
function ClearDayList(obj_selectDay)
{
	var i;
	var intLength;
	intLength = obj_selectDay.options.length = 1;
}
	
function IsLeapYear(int_year)
{
	var blnIsLeapYear = false;
	
	if ((((int_year % 4) == 0) && ((int_year % 100) != 0)) || ((int_year % 400) == 0))
	{
		blnIsLeapYear = true;
	}
	
	return(blnIsLeapYear);
}

function PopulateDays(obj_selectMonth, obj_selectDay, obj_selectYear, int_defaultDay)
{
	ClearDayList(obj_selectDay);
	
	var intYear = obj_selectYear.options[obj_selectYear.selectedIndex].value
	
	if (IsLeapYear(intYear) == true)
	{
		arrDaysInMonth[2] = 29;
	}
	else
	{
		arrDaysInMonth[2] = 28;
	}
	
	var intDaysInMonth	= arrDaysInMonth[obj_selectMonth.selectedIndex];
	var intDay			= '';
	
	for (i = 1; i <= intDaysInMonth; i++)
	{
		intDay = '' + i + ''
		if (intDay.length == 1)
		{
			intDay = '0' + intDay;
		}
		obj_selectDay.options[i] = new Option(intDay, intDay, false, false);
	}
	
	var intSelectedIndex = 0;
	
	if (int_defaultDay != '')
	{
		intSelectedIndex = int_defaultDay;
	}
	else
	{
		intSelectedIndex = 0;
	}
	
	obj_selectDay.selectedIndex = intSelectedIndex;
	
}

function ConstructDate(txt_hiddenField, int_monthSelected, int_daySelected, int_yearSelected)
{
	var txt_hiddenTemp;
	txt_hiddenTemp = int_monthSelected + '/' + int_daySelected + '/' + int_yearSelected;
	
	if(txt_hiddenTemp == '//')
	{
		txt_hiddenTemp = ''
	}
	
	txt_hiddenField.value = txt_hiddenTemp
}

//window functions
function focusPopup(objPopup,theURL,winName,features,width,height)
{
	var blnIsOpen = true;
	if (typeof(objPopup) != 'object') {
		blnIsOpen = false;
	} else if (objPopup.closed) {
		blnIsOpen = false;
	}
	if (blnIsOpen== false) {
		objPopup = returnPopup(theURL,winName,features,width,height);
	}
	 objPopup.focus();
	 return objPopup;
}

function openPopup(theURL,winName,features,width,height)
{
	var winWidth	= width;
	var winHeight	= height;
	var strWinSize	= ",width=" + winWidth + ",height=" + winHeight;

	if (window.screen) {
		var winPosL = (screen.availWidth - winWidth) / 2;
		var winPosT = (screen.availHeight - winHeight) / 2;
		strWinSize += ",left=" + winPosL + ",screenX=" + winPosL + ",top=" + winPosT + ",screenY=" + winPosT;
	}	
	
	window.open(theURL,winName,features + strWinSize);
 	}

function returnPopup(theURL,winName,features,width,height)
{
	var winWidth	= width;
	var winHeight	= height;
	var strWinSize	= ",width=" + winWidth + ",height=" + winHeight;

	if (window.screen) {
		var winPosL = (screen.availWidth - winWidth) / 2;
		var winPosT = (screen.availHeight - winHeight) / 2;
		strWinSize += ",left=" + winPosL + ",screenX=" + winPosL + ",top=" + winPosT + ",screenY=" + winPosT;
	}	
	
	return window.open(theURL,winName,features + strWinSize);
 	}

function openModule(moduleURL,moduleName,width,height,formName,inputVisible,inputHidden,qsArgs)
{
	var winWidth	= width;
	var winHeight	= height;
	var strWinSize	= ",width=" + winWidth + ",height=" + winHeight;

	if (window.screen) {
		var winPosL = (screen.availWidth - winWidth) / 2;
		var winPosT = (screen.availHeight - winHeight) / 2;
		strWinSize += ",left=" + winPosL + ",screenX=" + winPosL + ",top=" + winPosT + ",screenY=" + winPosT;
	}	
	
	moduleURL += "?refresh=true&fref=" + formName + "&iv=" + inputVisible + "&ih=" + inputHidden + qsArgs
	
	window.open(moduleURL,moduleName,'toolbar=0,status,scrollbars,location=0,menubar=0,resizable' + strWinSize);
 	}

function ClosePopup()
{
	window.close(self);
}

function openerLocation(strLocation)
{
	var blnIsOpen = true;
	if (typeof(opener) != 'object') {
		blnIsOpen = false;
	} else if (opener.closed) {
		blnIsOpen = false;
	}
	if (blnIsOpen) {
		opener.location.href=strLocation;
		opener.focus();
	} else {
		window.open(strLocation);
		window.close(self);
	}
}

//form functions
function addHiddenInputToForm(form, name, value)
{
	var newInput = document.createElement('INPUT');
	if(document.all) {
		newInput.type = 'hidden';
		newInput.id  = name;
		newInput.name = name;
		newInput.value = value;
	} else {
		newInput.setAttribute('type','hidden');
		newInput.setAttribute('id',name);
		newInput.setAttribute('name',name);
		newInput.setAttribute('value',value);
	}
	form.appendChild(newInput);
}

function DataGridItem_Delete(sender, ItemID, HiddenInputName, CssClass)
{	//default missing args
	HiddenInputName = HiddenInputName==null ? 'DeleteID' : HiddenInputName;
	CssClass = CssClass==null ? 'delitem' : CssClass;
	
	var ItemRow = document.getElementById(ItemID.toString());
	var strPrevClass = ItemRow.className;
	ItemRow.className = CssClass;
	
	if(confirm('Are you sure you want to delete the selected item?\t'))
	{
		eval('sender.form.'+HiddenInputName).value = ItemID.toString();
		return true;
	}
	else
	{
		ItemRow.className = strPrevClass;
		return false;
	}
}

function formFocus(strFormname, strElement) 
{
	var objE = eval('document.forms.' + strFormname + '.' + strElement);
	if (objE) objE.focus();
}

function newOption(strText, strValue, blnDefaultSelected, blnSelected)
{
	return new Option(strText, strValue, blnDefaultSelected, blnSelected);
}

function ul_onclick(jsObj)
{
   var i;
   var style;
  
   for (i = 0; i < jsObj.children.length; i++)
   {
       style = jsObj.children[i].style;
       if (style.display == "none")
       {
           style.display = "";
       }
       else
       {
           style.display = "none";
       }
   } 
}

function verifyMsg(jsStrURL, jsStrMsg) 
{
	if (confirm (jsStrMsg)) 
	{		
		this.window.location=jsStrURL;
		return true;
	}
}

function buildHumanSQL(objElement, strHeadline) {

	var inputLocal	= objElement;
	var strSQLHuman	= '<b>' + strHeadline + '</b>\n';
		strSQLHuman	+='<ul type=square style="margin-top:0; margin-left:15; padding:5">\n';
	
	if (inputLocal) {
		var len = inputLocal.length;
		var i=0;
		for (i=0 ; i<len ; i++) {
		
			if (inputLocal.options[i].selected) {
			
				strSQLHuman += '<li>' + inputLocal.options[i].text + '<br></li>\n';
			}
		}
	}
	
	strSQLHuman += '</ul>'
	objElement.form.txt_sqlHuman.value = strSQLHuman;
	return true;
}

function CheckALL(objCheckbox) {

	var len = objCheckbox.length;
	var i=0;
	for (i=0 ; i<len ; i++) {
		objCheckbox[i].checked=true;
	}
}

function UnCheckALL(objCheckbox) {

	var len = objCheckbox.length;
	var i=0;
	for (i=0 ; i<len ; i++) {
		objCheckbox[i].checked=false;
	}
}

function formLengthChecker(objForm) {

	var blnLengthOK = true;
	var strAlert = '';
	var len = objForm.length;
	var i=0;
	for (i=0 ; i<len ; i++) {
	
		if (objForm[i].value) {
			if (objForm[i].value.length > 102300) {
				blnLengthOK = false;
			}
		}
	}
	if (blnLengthOK) {
		strAlert = 'Your article is ok to submit.';
		} else {
		strAlert = 'Your article contains too much data and cannot be submitted.\nPlease reduce the length or consider creating it as a series.';
	}
	alert(strAlert);
}

function elementLengthChecker(field,maxlimit,name) {

	var strAlert = '';
	
	if (field.value.length > maxlimit) {
		strAlert = 'Your ' + name + ' contains too much data and cannot be submitted.\nPlease reduce the length to ' + maxlimit + ' characters or less.';
	} else {
		strAlert = 'Your ' + name + ' is ok to submit.';
	}
	alert(strAlert);
}

function textCounter(field,cntfield,maxlimit) {

	if (field.value.length > maxlimit) {
		// if too long trim it!
  			field.value = field.value.substring(0, maxlimit);
	} else {
		// otherwise, update 'characters left' counter
  			cntfield.value = maxlimit - field.value.length;
	}
}

function textTrimmer(field,maxlimit) {

	if (field.value.length > maxlimit) {
		// if too long trim it
  		field.value = field.value.substring(0, maxlimit);
	}

}

function getKeyPress(e) {

	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;

}

function stripHTML(val)
{
	if(val) return val.replace ( /<[\?\/!A-Za-z]+[^<>]*>?/gm, " " );
}

function getWordCount(val)
{
	if(val) {
		var sVal = stripHTML(val);
		var arrWords = sVal.match(/\b\w+\b/gm);
		if(arrWords) return arrWords.length;
	}
	return 0;
}

function alertWordCount(val)
{
	alert('Your text consists of approximately '+ getWordCount(val) +' words.');
}

//shift all the characters in the inval by shiftval characters from the charset
//  example:  "cat", 2, "abcdefghijklmnopqrstuvwxyz"
// would become "ecv"
// if a character is not found in charset, it is untouched.
//if a shift operation goes out of bounds, it will roll to the other side of charset
function CharShiftDecrypt(strInVal, shiftval, shiftCharSet) {
	var strInString = new String(strInVal);
	var intInString = strInString.length;
	var strCharSet = new String(shiftCharSet);
	var intCharSetLen = strCharSet.length;
	var strOutVal = new String('');
	
	var nextchar, nextindex, ascii_nextchar, i;
	
	//for each character
	for (i=0 ; i < intInString ; i++) {
		// grab the next character to encrypt
		nextchar = strInString.substr(i, 1);

		//look it up in charset
		nextindex = strCharSet.indexOf(nextchar,0)
		if (nextindex >= 0){ //found it
			nextindex = (nextindex  - shiftval) % intCharSetLen // modulo this so can stay in bounds for next operation
			
			//check bounds of nextindex
			if (nextindex < 0) {
				nextindex = nextindex + intCharSetLen //wrap around to high end of charset
			}
			else if (nextindex >= intCharSetLen){ //this wont happen btw, becuase of modulo, but anyway
				nextindex = nextindex - intCharSetLen
			}
			strOutVal += strCharSet.charAt(nextindex);
		}	
		else{ //char not found in set, so add it as is
			strOutVal += nextchar;
		}
	}
	
	return strOutVal;

}

//site specific window function for ethics tool
function openETPopup(theURL) {
	winEthicsCase=returnPopup(theURL,'ethics_case','scrollbars,resizable',700,470);
	winEthicsCase.focus();
	return false;
}

// Changes the font size of the element and stores the size in a cookie
function changeFontSize(strID, strSize)
{
	// get the element
	document.getElementById(strID).style.fontSize = strSize;
	// set the cookie
	SetCookie("fontSize",strSize,"December,31,2009");
}

// Function to find any cookied font size on the clients machine and set the font to their preference as specified in the cookie
function getFontSize(strID) {
	// check if the object whose id is given exists
	if(document.getElementById(strID)) {
				
		// create regular expression to match pattern 'fontSize=.......' till a semicolon is encountered or the string ends
		var regEx = new RegExp("fontSize=[^;]+");
				
		// if cookie exists
		if(regEx.test(document.cookie)){
			
			// if match found, then extract the part of the string matching the regular expression pattern
			var cookieEntry = document.cookie.toString().match(regEx);
			
			if(cookieEntry != '') {
			
				// from the match, extract the font size
				var fontSize = cookieEntry.toString().substring(9, cookieEntry.toString().length);
				
				// call the function to change the font size accordingly
				changeFontSize(strID, fontSize.toString());
			}
		}		
	}	
}

function SetCookie(name,value,expires)
{
	var exp = new Date(expires);
	document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); + ";path=";
}

function SetAEWriteMode(value)
{
	if(confirm('WARNING! \n\nTo switch editors, the current page must \nbe reloaded. Any changes that you have \nnot yet submitted will be lost. \n\nClick OK to continue... '))
	{
		location.href="/engine/set_cookie.asp?name=AEWriteMode&value="+escape(value)+"&expires=12/31/2009&redir="+location.href;
	}
	return false;
}

//writes article Category list and optionally links each Category to search results
function DoArticleKeywords(doSearchLinks,keywords,columnId)
{
	if(keywords!='')
	{	//keywords exist
		document.write('<p><b>Categories:</b> ');
		//do we need to parse the list and build search links?
		if(!doSearchLinks)
			document.write(keywords);
		else
		{	//fix column-id
			if(columnId==null) columnId = '';
			
			//split the list of Categories and write a link for each one
			var rx = new RegExp(",\\s*");
			var aKeywords = keywords.split(rx);
			for(var i=0; i<aKeywords.length; i++)
			{
				if(i>0) document.write(', ');
				document.write('<a href="/search/category.asp?k='+encodeURI(aKeywords[i])+'&cid='+columnId+'">'+aKeywords[i]+'</a>')
			}
		}
		document.write('</p>');
	}
}

function InlineDelete_Submit(sender,keyfield,id)
{
	var row = document.getElementById(id.toString());
	var strClassName = '';
	if(row)
	{
		strClassName = row.className;
		row.className = 'delitem';
	}
	
	if(confirm('Are you sure you want to delete the selected record?\t'))
	{
		eval('sender.form.'+keyfield).value = id;
		return true;
	}
	else
	{
		if(row) row.className = strClassName;
		return false;
	}
}

// begin DTHML edit functions 
function dhtmlDisplay(domID,dVal) {
	document.getElementById(domID).style.display = dVal;
}
