// Some standard functions to have

/* Ensure a field is not blank or null */
function validate_required(field) {
  with (field) { return validate_required_text(field.value); }
  }


function validate_required_text(text) {
  if (text==null||text=="") { return false}
  else {return true}
  }


/* See if an entry is in a "^" separated list of entries */
function validate_available(field1, field2) {
  /* Field2 = List of Values */
  var vField1Value = field1.value.toLowerCase()
  var vField2Value = field2.value.toLowerCase()
 
  /* Make an array of the "^" separated list */
  var vField2Array = vField2Value.split("^")
  var vField2ArrayLength = vField2Array.length

  /* return true = OK, false = not OK */
  for (vLoop = 0; vLoop <= vField2ArrayLength; vLoop ++)
  {
    if (vField2Array[vLoop] == vField1Value)
    { return false }
  }
  /* If we got here, all was well */
  return true
}


/* Validates the date drop-downs */
function validate_datefields( ip_day, ip_month, ip_year )
{
  // Some Variables
  var vDate = new Date()
  // Get the selected values
  var vSelDay     = ip_day.selectedIndex
  var vSelMonth   = ip_month.selectedIndex
  var vSelYear    = ip_year.selectedIndex
  var vDay        = ip_day.options[vSelDay].value.replace(/'/g,"")
  var vMonth      = ip_month.options[vSelMonth].value.replace(/'/g,"")
  var vYear       = ip_year.options[vSelYear].value.replace(/'/g,"")


  // If the month is Dec, increment the year & set month to Jan
  if (vMonth == 12)
    {
    vMonth = 0 
    vYear ++
    }

  // JS sets day zero as last day of prev month
  vDate.setFullYear( vYear, vMonth, 0)

  // Get the day from the new date (day of end of month)
  var vEOMDay = vDate.getDate()

  // If the day passed in is greater than the End Of Month day, error it!
  if (vDay > vEOMDay)
    {return false}
  else
    {return true}
}


/* Ensure a field is an integer */
function validate_integer ( ip_field )
{
  /* Get the length of the field.  Loop each char in the field & see if it's an integer */
  var vValue   = ip_field.value
  var vLength  = ip_field.value.length
  var vNumbers = '1234567890'

  if (vLength == 0)
    {return false}
  else
    {vChar = ''
     for (vLoop=0;vLoop<=vLength;vLoop++)
       {vChar = vValue.substr(vLoop,1)
        if (vNumbers.indexOf(vChar) == -1)
          {return false}
       }
    }
  /* If we got here it must be an integer */
  return true
}



// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}



/* Not used */
function f_DateStringToDate( ip_datestr )
{
  // Changes the string of a date to a date object
  // Assumes a separator of '/' for 'YYYY/MM/DD'
  // Variables
  var vPosition
  var vDateList
  var vDay
  var vMonth
  var vYear
  var vDate = new Date()
  
  for (vPosition = 0; vPosition < ip_datestr.length; vPosition ++ )
  {
    if (ip_datestr.indexOf("/") != -1) 
    {
      // Convert to a comma-separated list of date elements
      vDateList = ip_datestr.split()
      vYear     = strDateArray[0];
      vMonth    = strDateArray[1];
      vDay      = strDateArray[2];
      vDate.setFullYear( vYear, vMonth, vDay )
      return vDate
    }
  }
  return ""
}

/* Not used */
function f_InDateRange( ip_fdate, ip_tdate, ip_odate )
{
  // The dates will be received as date objects
  var vResult = false

  if ( ip_odate >= ip_fdate ) and ( ip_odate <= ip_tdate )
  { vResult = true }
  
  return vResult
}


function fEnableField ( ip_CheckField, ip_EnableField ) 
  {
  /* Input the field to check the ID of & field to enable or disable */
  vFieldValue = 0;  /* initialise */
  if (document.getElementById(ip_CheckField).type == "checkbox" ) {
    if (document.getElementById(ip_CheckField).checked == true) { vFieldValue = 1; };
    }
  else
    vFieldValue = document.getElementById(ip_CheckField).value;

  if (vFieldValue == 0 || vFieldValue == '') { document.getElementById(ip_EnableField).disabled = true; }
  else { document.getElementById(ip_EnableField).disabled = false; }

  return true;
  }


function fCopySelectOptions ( ip_select1, ip_select2 ) {
  // Copy all Select options from combo 1 to combo 2

  // First, get a reference to the select objects
  var vSelect1 = document.getElementById(ip_select1);
  var vSelect2 = document.getElementById(ip_select2);

  // Next, get the options from Select1
  var vSelOptions1 = vSelect1.options;
  var vSelOptions2 = vSelect2.options;
  var vSelected1   = vSelect1.selectedIndex;
  var vSelected2   = vSelect2.selectedIndex;
  var vSelected2OptValue = vSelOptions2[vSelected2].value;


  // Clear the old Options from Select2
  var vElement = 0;
  for (vElement = vSelect2.length - 1; vElement >= 0; vElement--) {
    try {
      vSelect2.remove(vElement);
      } catch (ex) {
      }
    }

  // Add the new Options to Select2, minus the selected one
  vSelected2 = 0;  // initialise selectedIndex
  vCount = 0;
  for (vItem in vSelOptions1) {

    if (vItem < vSelOptions1.length && vItem != vSelected1) {
      // Create the Option element
      vNewOpt = document.createElement('option');
      vNewOpt.value = vSelOptions1[vItem].value;
      vNewOpt.text = vSelOptions1[vItem].text;

      // If the values match, set the selectedIndex
      if (vNewOpt.value == vSelected2OptValue) {
        vSelected2 = vCount;
        }

      // Add the option.  Allow for IE (non-standards compliant).
      try {
        vSelect2.add(vNewOpt, null);  // standards compliant
        } // catch (std)
      catch (ex) {
        vSelect2.add(vNewOpt);  // IE only
        } // catch (IE)

      // Increment the count of those added (the new index)
      vCount += 1;
      } // if item not selected
    } // for each item


  // Finally, set the Selected option
  try {
    vSelect2.selectedIndex = vSelected2;
    } // catch (std)
  catch (ex) {
    vSelect2.selectedIndex = 0;
    } // catch (IE)

  }  // function fCopySelectOptions


// Strip all HTML tags from the field passed in & return as text string
function stripHTML(field){
  return stripHTMLtext(field.value);
  }

function stripHTMLtext(text){
  var re= /<\S[^><]*>/g;
  vStripped=text.replace(re, "");
  return vStripped;
  }

