function isblank(s){
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && ( c != '\n') && (c != '\t')) return false;
	}
	return true;
}

function configDateTimes(fieldList,frm) {
 var fldsArray,dateVal,hrs,mins;
 fldsArray = fieldList.split(',');
 for (i=0; i < fldsArray.length; i++) {
  dateVal = eval('frm.'+fldsArray[i]).value.split(' ');
  dateVal = dateVal[0];
  hrs = eval('frm.'+fldsArray[i]+'_hour_.selectedIndex');
  mins = eval('frm.'+fldsArray[i]+'_min_.selectedIndex');
  if ((dateVal != '') && (dateVal != 'NULL')) {
   eval('frm.'+fldsArray[i]).value = dateVal + ' '+hrs+':'+mins+':0';
  }
 }
}

function buildDropDownDate(fieldList,frm) {
 var fldsArray,dateVal,dayVal,monthVal,yearVal,dayCtrl,monthCtrl,yearCtrl,outputDate;
 fldsArray = fieldList.split(',');
 for (i=0; i < fldsArray.length; i++) {
  dayCtrl = eval('frm.'+fldsArray[i]+'_day_');
  monthCtrl = eval('frm.'+fldsArray[i]+'_month_');
  yearCtrl = eval('frm.'+fldsArray[i]+'_year_');
  dayVal = dayCtrl.options[dayCtrl.selectedIndex].value;
  monthVal = monthCtrl.options[monthCtrl.selectedIndex].value
  yearVal = yearCtrl.options[yearCtrl.selectedIndex].value;
  dateVal = eval('frm.'+fldsArray[i]).value.split(' ');
  if (dateVal.length>1) {
   timeVal = dateVal[1];
  } else {
   timeVal='';
  }
  outputDate = '';
  if ((dayVal !='') && (monthVal != '') && (yearVal != '')) {
   outputDate = yearVal + '/' +monthVal+ '/' + dayVal
  }
  if (isblank(outputDate)) {
   eval('frm.'+fldsArray[i]).value='NULL'
  } else {
   eval('frm.'+fldsArray[i]).value=outputDate
  }
  //hrs = eval('frm.'+fldsArray[i]+'_hour_.selectedIndex');
  //mins = eval('frm.'+fldsArray[i]+'_min_.selectedIndex');
  //if ((dateVal != '') && (dateVal != 'NULL')) {
   //eval('frm.'+fldsArray[i]).value = dateVal + ' '+hrs+':'+mins+':0';
  //}
 }
}

function defaultMsg(){
 var msg;
 msg =  "_______________________________________________\n\n"
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct the error(s) and re-submit.\n";
	msg +=  "_______________________________________________\n\n"
 return msg;
}

//This code prevents dotted lines when clicking on links
function ExplorerFix() {
  for (a in document.links) document.links[a].onfocus = document.links[a].blur;
}

if (document.all)	document.onmousedown = ExplorerFix;

var isNS4 = (document.layers) ? true : false;
var isIE4 = (document.all && !document.getElementById) ? true : false;
var isIE5 = (document.all && document.getElementById) ? true : false;
var isNS6 = (!document.all && document.getElementById) ? true : false;

var current = false;
 function showHide(layerName){
  if ((current) && (layerName != current)) setVis(current, 'hidden');
  if (layerName) setVis(layerName, 'visible');
  current = layerName;
 }
    
if (isNS4) document.captureEvents(Event.MOUSEUP);
document["onmouseup"] = function(event) {showHide(false);}

function checkSearch(frm) {
 var ok = true;
 if (isblank(frm.search.value)) {
  ok = false;
  alert('Please enter a search string');
 } else {
  frm.submit();
 }
 return false;
}

function confirmDelete(ref,tableName,mode,rootPath) {
 msg = 'WARNING!!!!!\nThis will permanently delete this item.\n';
 msg += 'Are you sure?...Click OK to continue or CANCEL to stop.';
 if (window.confirm(msg)) window.open(rootPath+'library/asp/del.asp?table=' + tableName + '&ref='+ref+'&mode='+mode, 'del', 'width=100,height=100,resizable');
 return false
}

//Takes a name and value and creates a cookie of the given name with the given value
function setCookie(name, val){
 document.cookie = name+"="+val;
 return true;
}

/*Takes a cookie name and returns its value as a string for numeric values 
 you need to do a parse on the result*/
function getCookie(name){
 var val;
 var cookieArray = document.cookie.split(";");
 for(var i = 0; i < cookieArray.length; i++){
  if(cookieArray[i].indexOf(name) != -1) {
   val = cookieArray[i].split("=")[1];
  }
 }
 return unescape(val);
}

// Adds an item to a comma separated list 
// if it does not exist or removes it if it does

function addRemoveListItem (item, list){
 var list2Array = list.split(",");
 var newList = new Array();
 var count = 0, i = 0;
 if (!((list2Array.length == 1) && (list2Array[0]=='undefined'))) {
  for (i = 0; i < list2Array.length; i++) {
   if (list2Array[i] != item) {
    newList[count] = list2Array[i];
    count++;
   }
  }
 }
 if (count == i){
  newList[count] = item;
 }
 return newList.join(',');
}

function autoFormat(formObj,fieldname) {
 var formattedText=eval('formObj.'+fieldname).value;
 if (formattedText.match(/<([^\n>])+>/) == null) {
  formattedText=formattedText.replace(/(\r)*\n/g,'<br>\n');
  formattedText=formattedText.replace(/\r/g,'<br>\r');
  formattedText=formattedText.replace(/((\w|-)+(\.(\w|-)+)*@(\w|-)+(\.(\w|-)+)+)/g,'<a href="mailto:$1">$1</a>');
  formattedText=formattedText.replace(/(http:\/\/(\w|-)+(\.(\w|-)+)*)/g,'<a href="$1" target="_blank">$1</a>');
  eval('formObj.'+fieldname).value=formattedText;
 } else {
  alert('Sorry, this text already contains HTML tagging and cannot be re-formatted.');
 }
}

function clearHTML(formObj,fieldname) {
 var formattedText=eval('formObj.'+fieldname).value;
 formattedText=formattedText.replace(/<([^\n>])+>/g,'');
 eval('formObj.'+fieldname).value=formattedText;
}

function htmlEncode(inputString) {
  var outputString = inputString;
  var matches;
  // All characters above 128 to be replaced by their ascii equivalents
  var regExpObj = /[^\x00-\x80]/gi;
  while ((matches = regExpObj.exec(outputString)) != null) {
   outputString = outputString.replace(matches[0],'&#'+matches[0].charCodeAt(0)+';');
  }
  // replace other known illegal characters with defined entities
  outputString = outputString.replace(/"/ig,'&quot;');
  outputString = outputString.replace(/</ig,'&lt;');
  outputString = outputString.replace(/>/ig,'&gt;');
  // replace all ampersands with &amp; that are not part of currently encoded entities
  var regExpObj = /(&)([\w#]{1,7};)?/gi;
  while (((matches = regExpObj.exec(outputString)) != null )) {
   if (matches[0]==matches[1]) {
    outputString = outputString.substring(0,matches.index) + '[~amp~]amp;' + outputString.substring(matches.index+1);
   } else {
    outputString = outputString.substring(0,matches.index) + '[~amp~]' + outputString.substring(matches.index+1);    
   }
  }
  outputString = outputString.replace(/\[~amp~\]/ig,'&');
  return outputString
 }
 
 function htmlEncodeTag(inputString) {
  var outputString = inputString;
  var regExpObj = /=\s*"([^"]+)"+/gi;
  while ((matches = regExpObj.exec(outputString)) != null) {
   outputString = outputString.substring(0,matches.index) + matches[0].replace(matches[1],htmlEncode(matches[1])).replace(/=/,'[~eq~]') + outputString.substring(matches.index+matches[0].length);
  }
  //outputString = outputString.replace(/=/ig,'[~eq~]');
  return outputString
 }

 function fixCharacterEncoding(inputString) {
  var outputString = inputString;
  var matches;
  // find all text not part of an html tag and HMTL Encode
  var regExpObj = /(^|>)((.|\n)*?)((<([a-zA-Z]|\/|\?|!))|$)/gi;
  while ((matches = regExpObj.exec(outputString)) != null) {
   outputString = outputString.replace(matches[2],htmlEncode(matches[2]));
  }
  // find all text within HTML Tags and encode
  var regExpObj = /<([a-zA-Z]|\/|\?|!)[^<>]+=[^<>]+>/gi; //="?((.|\n)+)"|\s(.|\n)+?
  while ((matches = regExpObj.exec(outputString)) != null) {
   outputString = outputString.substring(0,matches.index) + htmlEncodeTag(matches[0]) + outputString.substring(matches.index+matches[0].length);
  }
  outputString = outputString.replace(/\[~eq~\]/ig,'=');
  return outputString
 }