function getSelectedValue(list){
   for(i=0; i<list.length; i++){
      if(list[ i].selected){
         return list[ i].value;
      }
   }
}
function initEditor(taName,table,spell,fp,cm,css,cssstuff,cssfile,filemgr) {
/*	
  // create an editor for the textbox
  var editor = new HTMLArea(taName);
  
  // register the Tables plugin
  if(table == '1') {
    editor.registerPlugin("TableOperations");
  }

  // register the SpellChecker plugin
  if(spell == '1') {
    editor.registerPlugin("SpellChecker");
  }
  if(fp == '1') {
    editor.registerPlugin("FullPage");
  }
  if(cm == '1') {
    editor.registerPlugin("ContextMenu");
  }
  //if(imgmgr == '1') {
  //  editor.registerPlugin("ExtendedFileManager");
  //}
  if(filemgr == '1') {
    editor.registerPlugin("ExtendedFileManager");
  }
  if(css == '1') {
    editor.registerPlugin(CSS,cssstuff);
    editor.config.pageStyle = "@import url("+cssfile+");";
  }

 
  setTimeout(function() {
    editor.generate();
  }, 500);
*/
 HTMLArea.onload = function() {
 var editor = new HTMLArea(taName);
 // register the Tables plugin
  if(table == '1') {
    editor.registerPlugin("TableOperations");
  }
  // register the SpellChecker plugin
  if(spell == '1') {
    editor.registerPlugin("SpellChecker");
  }
  if(fp == '1') {
    editor.registerPlugin("FullPage");
  }
  if(cm == '1') {
    editor.registerPlugin("ContextMenu");
  }
  if(filemgr == '1') {
    editor.registerPlugin("ExtendedFileManager");
  }
/*  if(css == '1') {
    editor.registerPlugin(CSS,cssstuff);
    editor.config.pageStyle = "@import url("+cssfile+");";
  } */

  editor.config.toolbar = [
  ['fontsize', 'space',
   'formatblock', 'space','justifyleft', 'justifycenter',
   'copy', 'cut', 'paste', 'space', 'undo', 'redo',
   'createlink', 'insertimage','orderedlist', 'unorderedlist',
   'bold', 'italic','inserthorizontalrule','popupeditor']   
];

  //editor.replace(taName, config);
 editor.generate();
 };
 HTMLArea.init();
 
  return false;

}
function deleteFile(daname,daform,recordID,removeID) {
  if(confirm('Are you sure you want to delete file '+daname+'?')) {
	window.open('getFile.php?form='+daform+'&record='+recordID+'&remove='+removeID,'_self');
  }
}
function setAttVisible(fieldName) {
  obj = document.getElementById(fieldName+'_win');
  obj.style.display='none';
}
function Replace(Expression, Find, Replace)
{
	var temp = Expression;
	var a = 0;

	for (var i = 0; i < Expression.length; i++)
	{
		a = temp.indexOf(Find);
		if (a == -1)
			break
		else
			temp = temp.substring(0, a) + Replace + temp.substring((a + Find.length));
	}

	return temp;
}

function setSelectedIndex(list, value){
   for(i=0; i<list.length; i++){
      if(list.options[i].text.toUpperCase() == value.toUpperCase()){
         list.selectedIndex = i;
         break;
      }
   }
 }
function inArray(arr,str)
{
  for (var i=0;i<arr.length;i++)
  {
    if (arr[i].trim() == str.trim()) {
	  return true;
	}
  }
  return false;
}
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function setRadio(radio, value){

         // Parameters:
            // "radio" is a radio button object, <formname>.<radiogroup>
            // "value" is the radio button to activate

         // Behavior:
            // Sets the active radio button
   obj = eval('document.forms[0].'+radio.name);
   for(i=0; i<obj.length; i++){
      if(obj[i].value == value){
         obj[i].checked = true;
      } else {
         obj[i].checked = false;
      }
   }

}
 
function setCheckbox(radio, DAvalue){

         // Parameters:
            // "radio" is a checkbox button obj, <formname>.<radiogroup>
            // "value" is the radio button to activate

         // Behavior:
            // Sets the active radio button
value = DAvalue.split(';');
var allInpt = document.getElementsByTagName('input');
   for(i=0; i < allInpt.length; i++){
	if(allInpt[i].type == 'checkbox') {
	  if(allInpt[i].id == radio.id) {
	   if(isArray(value))
       {
         if(inArray(value,allInpt[i].value))
         {
           allInpt[i].checked = true;
         }
         else
         {
           allInpt[i].checked = false;
         }
       }
       else
       {
         if(allInpt[i].value == value){
           allInpt[i].checked = true;
         } else {
           allInpt[i].checked = false;
         }
       }//end isarray else
      }//end name match if
	} //end checkbox if
   }//end for
} //end function

function setTheField(fieldName, fieldVal) {
 fieldVal = Replace(fieldVal, '<br>','\n');
 var found = false;
 if(document.getElementById(fieldName)) {
   found = true;
   obj = document.getElementById(fieldName);
  }
  if(document.getElementById(fieldName+'[]')) {
  	found = true;
        obj = document.getElementById(fieldName+'[]');
  }
 if ( found) {
 var formType = getFormElementType(obj);
 if (formType == 'text')
 {
   document.getElementById(fieldName).value = fieldVal;
 }
 else if (formType == 'radio')
 {
  setRadio(obj,fieldVal);
 }
 else if (formType == 'select-one')
 {
  setSelectedIndex(document.getElementById(fieldName),fieldVal);
 }
 else if (formType == 'checkbox')
 {
  setCheckbox(obj,fieldVal);
 }
 else
 {
   document.getElementById(fieldName).value = fieldVal;
 }
}
else
{
  return false;
}
}
function getFieldVal(fieldName) {
  obj = document.getElementById(fieldName);
  var found = false;
  if(obj) {
   found = true;
  }
  else {
    obj = document.getElementById(fieldName+'[]');
    if(obj) {
      found = true;
    }
  }
  if ( found ) {
    var formType = getFormElementType(obj);
    if (formType == 'text')
    {
      return obj.value;
    }
    else if (formType == 'radio')
    {
      return getRadio(obj);
    }
    else if (formType == 'select-one')
    {
      return getSelectedValue(obj);
    }
    else if (formType == 'checkbox')
    {
      return obj[obj.selectedIndex].text;
    }
    else
    {
      return obj.value;
    }
  }
  else {
    return false;
  }
}
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getRadio(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function
function getRadio2(radioName) {
  var inputs=document.getElementsByTagName("INPUT");
  for (counter = 0; counter < inputs.length; counter++)  {
         if (inputs[counter].name == radioName) {
           if(inputs[counter].checked) {
             radioval = inputs[counter].value;
           }
         }
  }
  return radioval;
}

function wOpen(mypage,myname,w,h,features) {
  if(screen.width){
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  }else{winl = 0;wint =0;}
  if (winl < 0) winl = 0;
  if (wint < 0) wint = 0;
  var settings = 'height=' + h + ',';
  settings += 'width=' + w + ',';
  settings += 'top=' + wint + ',';
  settings += 'left=' + winl + ',';
  settings += features;
  win = window.open(mypage,myname,settings);
  win.window.focus();
}

function hideWhen(sourceObjName, comparative,compareValue, hideObjName) {
    hideObj = document.getElementById('obj-'+hideObjName);

  //first check to see if this is a form hide, or element hide
  if(sourceObjName == '%FORM%') {  //form hide
    hideOptions = compareValue.split(';');
    docStatus =  (document.location.href.indexOf('id=') !== -1) ? 'read' : 'new';
    docStatus =  (document.location.href.indexOf('&update') !== -1) ? 'edit' : docStatus;
    if(hideOptions[0] == '1') { //edit mode
      if(docStatus == 'edit') { hideObj.style.display = 'none'; }
    }
    if(hideOptions[1] == '1') { //read mode
      if(docStatus == 'read') { hideObj.style.display = 'none'; }
    }
    if(hideOptions[2] == '1') { //print - not applicable here
    }
    if(hideOptions[3] == '1') { //new page
      if(docStatus == 'new') { hideObj.style.display = 'none'; }
    }
  } else {  //element hide
  //now get the value of the sourceObj
    comparative2 = "";
    sourceObj = document.getElementById(sourceObjName);
    var formType = getFormElementType(sourceObj);
    if (formType == 'text')
    {
      sourceObjValue = sourceObj.value;
    }
    else if (formType == 'radio')
    {
      sourceObjValue = getRadio2(sourceObjName);
    }
    else if (formType == 'select-one')
    {
      sourceObjValue = getSelectedValue(sourceObj);
    }
    else if (formType == 'checkbox')
    {
      sourceObjValue = sourceObj[sourceObj.selectedIndex].text;
    }
    else
    {
      sourceObjValue = document.getElementById(sourceObjName).value;
    }
    if(comparative == 'contain') {
      comparative = ".indexOf(";
      comparative2 = ") > 0";
    }
    if(comparative == '!contain') {
      comparative = ".indexOf(";
      comparative2 = ") < 0";
    }
    evalStr = "'"+sourceObjValue+"'"+comparative+"'"+compareValue+"'"+comparative2+";";
    daMatch = eval(evalStr);
    if(daMatch) {
      hideObj.style.display = 'none';
      hideObj.style.zindex = '0';
    } else {
      hideObj.style.display = '';
      hideObj.style.zindex = '1';
    }
  }
}
function hideOnce(evaluationStr, hideObjName) {
  daMatch = eval(evaluationStr);
  obj = document.getElementById('obj-'+hideObjName);
  if (daMatch) {
    obj.style.display = 'none';
  }
  else
  {
    obj.style.display = '';
  }
}

function validateForm(valFields) {
  valFieldArr = valFields.split(";");
  var msg = '';
  var validated = false;
  for(x = 0; x < valFieldArr.length; x++) {
    fieldName = valFieldArr[x];
    obj = document.getElementById(fieldName);
    fieldVal = getFieldVal(fieldName);
    vOptions = validateArray[fieldName].split(";");
    vExtra = validateArrayOptions[fieldName].split("~");
    for(y=0; y < vOptions.length; y++) {
      if(vOptions[y] == '1') {      	
        switch(y) {
          case 0:  //empty
            if(fieldVal == '') { msg += vExtra[0]+"\n"; }
          break;
          case 1:  //length
            if( fieldVal.length < vExtra[1]) { msg += vExtra[3]+"\n"; }
            if( fieldVal.length > vExtra[2]) { msg += vExtra[3]+"\n"; }
          break;
          case 2:  //alpha
            if (/\d/.test(fieldVal)) { msg += vExtra[4]+"\n"; }
          break;
          case 3:  //number
            if( /[a-zA-Z]/.test(fieldVal) ) { msg += vExtra[5]+"\n"; }
          break;
          case 4:  //currency
            //if( /(\$|£)\d+/.test(fieldVal) == false) { msg += vExtra[7]+"\n"; }
          break;
          case 5:  //regEx
            var re = new RegExp(vExtra[8], 'g')
            if( re.test(fieldVal) == false) { msg += vExtra[9]+"\n"; }
          break;
          case 6:  //js Function
            if ( eval(vExtra[10]+'("'+fieldVal+'")') == false ) { msg += vExtra[11]+"\n"; }
          break;
          default: msg = msg;
        }
      }
    }
  }
  if(msg.length > 0) {
    msg = "There are errors in your submission:\n\n"+msg;
    alert(msg);
    return false;
  } else {
    return true;
  }
}
/*
function getFormElementType(formObject) {
   if(formObject.type) { 
   	return formObject.type; 
   } else if ( (formObject.length) && (formObject[0].type) ) {
   	 return formObject[0].type; 
   } else {
   	 return 'undefined';
   }
}
*/

function getFormElementType (elementObject) { 

        /** 
        * 
        * Possible return values are ... 
        * 
        * "button", "checkbox", "file", "hidden", "password" 
        * "radio", "reset", "select" (covers select-one and select-multiple) 
        * "submit", "text", "textarea" 
        * 
        */ 


        var returnValue = false; 


        if (typeof elementObject == undefined) return false; 
        (elementObject.type == undefined) ? returnValue = elementObject[0].type 
: returnValue = elementObject.type; 
        if (returnValue.indexOf('select') != -1) returnValue = 'select'; 
        return returnValue; 



} 

function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; }	
}
function queryString(key){
var page = new PageQuery(window.location.search); 
return unescape(page.getValue(key)); 
}