function newWindow(pagename) {
  newwin=window.open(pagename,'','left=0,top=0,width=850,height=600,resizable=0,status=0,scrollbars=1,toolbar=0,menubar=0,location=0,directories=0');
}

function deleteNote() {
    return confirm("Are you sure you want to delete this note? This action cannot be undone!");
}

function clearTextElements(form) {
  for (i = 0; i < form.elements.length; i++) {
    if (form.elements[i].type == 'text') {
      form.elements[i].value = "";
    }
  }
}

function disableTextElements(form) {
  for (i = 0; i < form.elements.length; i++) {
    if (form.elements[i].type == 'text') {
      form.elements[i].disabled = true;
    }
  }
}

function clearSelectElements() {
  l = document.getElementsByTagName("select");
  for (i = 0; i < l.length; i++) {
    it = l.item(i);
    it.selectedIndex=0;
  }
}

function dateHandler(control) {
  if (control.value.length > 2) {
    if (control.value.substr(0, 3).indexOf("-") < 0 ) {
      control.value = control.value.substr(0, 2) + "-" + control.value.substr(2, control.value.length);
    }
  }
  if (control.value.length > 5) {
    if (control.value.substr(3, 6).indexOf("-") < 0 ) {
      control.value = control.value.substr(0, 5) + "-" + control.value.substr(5, control.value.length);
    }
  }
}

function setElementValueAndSubmitForm(formId, elementId, elementValue) {
  document.getElementById(elementId).value = elementValue;
  document.getElementById(formId).submit();
}

function hideElementById(id){
  document.getElementById(id).className = 'invisible';
}

function showElementById(id, className){
  document.getElementById(id).className = className;
}

function setElementValue(id, value){
  document.getElementById(id).value = value;
}

function setElementDisabled(id, value){
  document.getElementById(id).disabled = value;
}

//purchase use decal form functions

function prepareBuyDecalOnlyForm(form){
  showElementById('mail_info_form', 'visible_block');
  hideElementById('search_info_form');
  hideElementById('add_ramp_use_decal');
  setElementValue('numberOfDecals','');
  setElementDisabled('numberOfDecals',false);
  clearTextElements(form);
  clearSelectElements();
}

function prepareRegisteredVesselForm(form){
  showElementById('search_info_form', 'visible_block');
  showElementById('add_ramp_use_decal','visible_block');
  setElementValue('numberOfDecals',1);
  setElementDisabled('numberOfDecals',true);
  hideElementById('mail_info_form');
  clearTextElements(form);
  clearSelectElements();
}

//admin - vessel search
function setFocus(id, form) {
  clearTextElements(form);
  disableTextElements(form);
  document.getElementById(id).disabled=false;
  document.getElementById(id).focus();
  return;
}

//admin - admin5 vessel misc
function changeHarbor(){
  if (isHarborExplenationAllowed(document.getElementById('placeKept_harbor'))) {
    document.getElementById('placeKept_harbor_desc').disabled=false;
  } else {
    document.getElementById('placeKept_harbor_desc').value='';
    document.getElementById('placeKept_harbor_desc').disabled=true;
  }
}

function isHarborExplenationAllowed(harborList){
  for (i = 0; i < harbors.length; i++){
    if (harbors[i].value == harborList.value){
      return harbors[i].explanationAllowed;
    }
  }
  return false;
}

function createHarbor(value, description, explanationAllowed, island){
  this.description = description;
  this.explanationAllowed = explanationAllowed;
  this.island = island;
  this.value = value;
}

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function createHarborOptions(harbor){
  harborsList=document.getElementById('placeKept_harbor');
  clearSelectOptions(harborsList);
  island =  document.getElementById('placeKept_island').options[document.getElementById('placeKept_island').selectedIndex].value;
  k=1;
  for (i=0; i < harbors.length; i++){
    if (harbors[i].island == island || harbors[i].island == 'Other'){
      addOption(harborsList, harbors[i].description, harbors[i].value);
      if (harbors[i].value == harbor) {
        harborsList.selectedIndex=k;
      }
      k++;
    }
  }
  setHarborVisibility();
}

function clearSelectOptions(selectList){ //'placeKept_harbor'
  list = selectList;
  l = list.length;
  for (i=l; i>=1; i--){
    list.options[i] = null;
  }
}

function setHarborVisibility(){
   if (document.getElementById('placeKept_island').value!='' && document.getElementById('placeKept_type').value == 1){
     showElementById('harbor_form_part', 'visible_block');
   }else{
     hideElementById('harbor_form_part');
   }
}

function selectValueSetterWhenNotEqual(checkedSelectId, conditionValue, changedSelectId, setValue){
   checkedSelect = document.getElementById(checkedSelectId);

   if(checkedSelect.options[checkedSelect.selectedIndex].value != conditionValue){
     changedSelect = document.getElementById(changedSelectId);
     for(var i=0; i<changedSelect.options.length; i++){
       if(changedSelect.options[i].value == setValue){
         changedSelect.selectedIndex = i;
       }
     }
   }
}

function selectValueSetterWhenEqual(checkedSelectId, conditionValue, changedSelectId, setValue){
   checkedSelect = document.getElementById(checkedSelectId);

   if(checkedSelect.options[checkedSelect.selectedIndex].value == conditionValue){
     changedSelect = document.getElementById(changedSelectId);
     for(var i=0; i<changedSelect.options.length; i++){
       if(changedSelect.options[i].value == setValue){
         changedSelect.selectedIndex = i;
       }
     }
   }
}


function inputValueSetterWhenNotEqual(checkedSelectId, conditionValue, changedInputId, setValue){
   checkedSelect = document.getElementById(checkedSelectId);

   if(checkedSelect.options[checkedSelect.selectedIndex].value != conditionValue){
     document.getElementById(changedInputId).value = setValue;
   }
}

function disabledInputSetterWhenNotEqual(checkedSelectId, conditionValue, changedInputId){
   checkedSelect = document.getElementById(checkedSelectId);

   if(checkedSelect.options[checkedSelect.selectedIndex].value != conditionValue){
     document.getElementById(changedInputId).disabled=true;
   }else{
     document.getElementById(changedInputId).disabled=false;
   }
}

function inputValueSetterWhenEqual(checkedSelectId, conditionValue, changedInputId, setValue){
   checkedSelect = document.getElementById(checkedSelectId);

   if(checkedSelect.options[checkedSelect.selectedIndex].value == conditionValue){
     document.getElementById(changedInputId).value = setValue;
   }
}

function disabledInputSetterWhenEqual(checkedSelectId, conditionValue, changedInputId){
   checkedSelect = document.getElementById(checkedSelectId);

   if(checkedSelect.options[checkedSelect.selectedIndex].value == conditionValue){
     document.getElementById(changedInputId).disabled=true;
   }else{
     document.getElementById(changedInputId).disabled=false;
   }
}

function setVisibilityElementRelatedToCheckbox(elementId, checkboxId){
  if(document.getElementById(checkboxId).checked){
    hideElementById(elementId);
  }else{
   showElementById(elementId, 'visible_block');
  }
}

function LeftPad(n, digits, prefix) {
  var ns = "" + n;
  var pd = "";
  if (digits > ns.length) {
    for (i = 0 ; i < (digits - ns.length); i++) {
      pd = pd + prefix;
    }
  }
  return pd + ns;
}

function submitForm(control) {
  control.onclick = function() { return false; }
  control.value = "Processing ...";
  return true;
}

function findElementInArray(arr, element) {
  var index = 0;
  while (index < arr.length) {
    if (arr[index] == element) return true;
    index++
  }
  return false;
}

function goToNextInput(currentInput, nextInputId, e) {
  var funcKeys = [0,8,9,16,17,18,37,38,39,40,46];
  if (e.keyCode && !findElementInArray(funcKeys, e.keyCode))
    if (currentInput.value.length >= currentInput.getAttribute('maxlength'))
      document.getElementById(nextInputId).focus();
}

function phoneHandler(control) {
  if (control.value.length > 3) {
    if (control.value.substr(0, 4).indexOf("-") < 0 ) {
      control.value = control.value.substr(0, 3) + "-" + control.value.substr(3, control.value.length);
    }
  }
  if (control.value.length > 7) {
    if (control.value.substr(4, 8).indexOf("-") < 0 ) {
      control.value = control.value.substr(0, 7) + "-" + control.value.substr(7, control.value.length);
    }
  }
}

function haNumberHandler(control) {
  if (control.value.length > 2) {
    if (control.value.substr(0, 3).indexOf("-") < 0 ) {
      control.value = control.value.substr(0, 2) + "-" + control.value.substr(2, control.value.length);
    }
  }
  if (control.value.length > 7) {
    if (control.value.substr(3, 8).indexOf("-") < 0 ) {
      control.value = control.value.substr(0, 7) + "-" + control.value.substr(7, control.value.length);
    }
  }
}

function addHiddenIntoDiv(divId, hiddenName, hiddenValue){
  document.getElementById(divId).innerHTML="<input type='hidden' name='"+ hiddenName +"' value='"+ hiddenValue +"'/>";
}

function noteTypeChanged(selectId, value, elementId, rowsId, className) {
  select = document.getElementById(selectId);
  if(select.options[select.selectedIndex].value == value){
    showElementAndSetRowClass(elementId, rowsId, className);
  } else {
    hideElementAndSetRowClass(elementId, rowsId, '');
  }
}

function showElementAndSetRowClass(elementId, rowsId, className) {
  showElement(elementId, 'visible_row');
  for (var i=0;i<rowsId.length;i++) {
    document.getElementById(rowsId[i]).className = className;
  }
}

function hideElementAndSetRowClass(elementId, rowsId, className) {
  hideElement(elementId);
  for (var i=0;i<rowsId.length;i++) {
    document.getElementById(rowsId[i]).className = className;
  }
}

function showElement(id, className) {
  document.getElementById(id).className=className;
}

function hideElement(id) {
  document.getElementById(id).value="";
  document.getElementById(id).className='invisible';
}
