function trimString(str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function ShowDetail(vPnumber) {
 var w = 480, h = 340;

 if (document.all) {
   /* the following is only available after onLoad */
   w = document.body.clientWidth;
   h = document.body.clientHeight;
 } else if (document.layers) {
   w = window.innerWidth;
   h = window.innerHeight;
 }

 var popW = 680, popH = 530;

 var leftPos = (w-popW)/2, topPos = (h-popH)/2;

 window.open("/pwatch-detail.rvt?id=" + vPnumber ,"Detail","width="+popW+",height="+popH+",top="+topPos+",left="+leftPos);

 /* window.open("/pwatch-detail.rvt?id=" + vPnumber ,"Detail",config="height=500,width=640,top=75,left=75"); */
}

function ShowDetailNG(vPnumber) {
 var w = 480, h = 340;

 if (document.all) {
   /* the following is only available after onLoad */
   w = document.body.clientWidth;
   h = document.body.clientHeight;
 } else if (document.layers) {
   w = window.innerWidth;
   h = window.innerHeight;
 }

 var popW = 680, popH = 530;

 var leftPos = (w-popW)/2, topPos = (h-popH)/2;

 window.open("/parole_watch/pwatch-detail.rvt?id=" + vPnumber ,"Detail","width="+popW+",height="+popH+",top="+topPos+",left="+leftPos);

 /* window.open("/pwatch-detail.rvt?id=" + vPnumber ,"Detail",config="height=500,width=640,top=75,left=75"); */
}

function ShowMod(vPnumber) {
 var w = 480, h = 340;

 if (document.all) {
   /* the following is only available after onLoad */
   w = document.body.clientWidth;
   h = document.body.clientHeight;
 } else if (document.layers) {
   w = window.innerWidth;
   h = window.innerHeight;
 }

 var popW = 800, popH = 550;

 var leftPos = (w-popW)/2, topPos = (h-popH)/2;

 window.open("/admin/pwatch-mod.rvt?id=" + vPnumber ,"Detail","width="+popW+",height="+popH+",top="+topPos+",left="+leftPos+",scrollbars=1");

 /* window.open("/pwatch-detail.rvt?id=" + vPnumber ,"Detail",config="height=500,width=640,top=75,left=75"); */
}

function AddNew() {
 var w = 480, h = 340;

 if (document.all) {
   /* the following is only available after onLoad */
   w = document.body.clientWidth;
   h = document.body.clientHeight;
 } else if (document.layers) {
   w = window.innerWidth;
   h = window.innerHeight;
 }

 var popW = 800, popH = 550;

 var leftPos = (w-popW)/2, topPos = (h-popH)/2;

 window.open("/admin/pwatch-new.rvt","Detail","width="+popW+",height="+popH+",top="+topPos+",left="+leftPos);

 /* window.open("/pwatch-detail.rvt?id=" + vPnumber ,"Detail",config="height=500,width=640,top=75,left=75"); */
}

function CheckFields(vMe) {
 vEmpties = 0;

 for (vCount = 0; vCount < vMe.elements.length; vCount ++) {
  if (vMe.elements[vCount].type == 'text' || vMe.elements[vCount].type == 'textarea') {
   if (vMe.elements[vCount].value.length == 0) {
    vEmpties ++
   }
  }
 }

 if (vEmpties != 0) {
  alert('One or more required fields are empty.\nPlease complete the form in order to proceed\n\nEnter \'none\' in the email field if you do not have an email address');
 } else {
  vMe.submit()
 }
}

function ManageOffenses(vDoWhat) {
 if (vDoWhat == "add") {
  var vSelectedId = document.make_mod.choose_here.value
  var vSelectedItem = document.getElementById("C_" + vSelectedId)
  var vGoesHere = document.getElementById("OS")
  var vEO = ""

  // ensure we don't already have this added
  if (! document.getElementById("S_" + vSelectedId)) {
   // get the existing offenses
   for (vCount = 0; vCount < vGoesHere.getElementsByTagName("option").length; vCount ++) {
    var vEV = vGoesHere.getElementsByTagName("option")[vCount].value 
    var vEN = vGoesHere.getElementsByTagName("option")[vCount].innerHTML
    var vEO = vEO + "<option id=\"S_" + vEV + "\" value=\"" + vEV + "\">" + vEN + "</option>"
   }
 
   var vPutIn = "<select name=\"offenses_show\" onChange=ManageOffenses('remove') size=\"3\" multiple>"
   var vPutIn = vPutIn + vEO
   var vPutIn = vPutIn + "<option id=\"S_" + vSelectedId + "\" value=\"" + vSelectedId + "\">" + vSelectedItem.innerHTML + "</option>"
   var vPutIn = vPutIn + "</select>"
 
   vGoesHere.innerHTML = vPutIn
 
   document.make_mod.offenses.value = trimString(document.make_mod.offenses.value + " " + vSelectedId)
  }
 } else {
  var vSelectedId = document.make_mod.offenses_show.value
  var vSelectedItem = document.getElementById("S_" + vSelectedId)
  var vGoesHere = document.getElementById("OS")

  for (vCount = 0; vCount < vGoesHere.getElementsByTagName("option").length; vCount ++) {
   var vEV = vGoesHere.getElementsByTagName("option")[vCount].value 

   if (vEV != vSelectedId) {
    var vEN = vGoesHere.getElementsByTagName("option")[vCount].innerHTML
    var vEO = vEO + "<option id=\"S_" + vEV + "\" value=\"" + vEV + "\">" + vEN + "</option>"
   }
  }
 
  var vPutIn = "<select name=\"offenses_show\" onChange=ManageOffenses('remove') size=\"3\" multiple>"
  var vPutIn = vPutIn + vEO
  var vPutIn = vPutIn + "</select>"

  vGoesHere.innerHTML = vPutIn

  var vUseThis = ""
  var lVals = trimString(document.make_mod.offenses.value)
  var lVals = lVals.split(" ")

  for (vCount = 0; vCount < lVals.length; vCount ++)  {
   if (lVals[vCount] != vSelectedId) {
    var vUseThis = vUseThis + lVals[vCount] + " "
   }
  }

  document.make_mod.offenses.value = vUseThis
 }
}

