<script language="JavaScript">
  <?php include("./lib/js/browser_detect.js"); ?>
</script>
<script language="JavaScript">
<!--


// rewrite of missing String.split() for ie3.x -  Andrzej Kosmol <ejki@hevelius.cnt.pl>
function mySplit(c) {
    var a = new Array();
    for (var i = j = k = 0; i < this.length; i++) {
        if (this.substring(i,i+1) == c) {
            a[k++] = this.substring(j, i);
            j = i + 1;
        }
    }
    a[k] = this.substring(j, i);  // final one
    return a;
}

// rewrite of missing String.join() for ie3.x
function myJoin(array, delimiter) {
  result = "";
  for (i = 0; i < array.length; i++) {
    j = array[i];
    if (j != '') {
      result = result + array[i] + delimeter;
    }
  }
}

// define missing String methods for ie3.x
if (is.ie3) {
    String.prototype.split = mySplit;
    String.prototype.join  = myJoin;
}

function attachment_check(n) {
    if (n.attachmentAction.value) {
	if (n.attachmentAction.value == 'add') {
	    if (n.file_upload.value == '') {
		alert ('<?php echo $lang->alert_specify_file ?>');
		n.file_upload.focus ();
		return false;
	    } else {
		return true;
	    }
	}
	
	if (n.attachmentAction.value == 'delete') {
	    sel = false;
	    
	    var i = 0;
	    
	    for (i = 0; i < n.elements.length; i++) {
		if (n.elements[i].name == 'delattachments[]') {
		    if (n.elements[i].checked) {
			sel = true;
		    }
		}
	    }
	    
	    if (sel == false) {
		alert ('<?php echo $lang->alert_select_attach_delete ?>');
		return false;
	    } else {
		return true;
	    }
	}
    }  
}

function submit_and_sign(n) {

  if (n.message.value == '') {
      alert('<?php echo $lang->alert_need_text ?>');
      n.message.focus();
      return false;
   }
   
   if (n.to.value == '') {
      alert('You must have a recipient.');
      n.to.focus();
      return false;
   }
   
   return true;
}

function bounce_submit_and_sign(n) {

   if (n.to.value == '') {
      alert('You must have a recipient.');
      n.to.focus();
      return false;
   }
   
   return true;
}

<?php if (isset($default->path_to_ispell)): ?>
function spellcheck () {
  document.spelling.message.value = document.compose.message.value;
  document.spelling.spell_lang.value = document.compose.spell_lang.options[document.compose.spell_lang.selectedIndex].value;
  document.spelling.submit();
}
<?php endif; ?>


var Addresses = new Array (
  <?php
  if (isset($addresses) && is_array($addresses)) {
    for ($i = 0; $i < count($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . $addresses[$i][0] . '"';
    }
  }
  echo "\n";
  ?>
)

var Nicknames = new Array (
  <?php
  if (isset($addresses) && is_array($addresses)) {
    for ($i = 0; $i < count($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . $addresses[$i][1] . '"';
    }
  }
  echo "\n";
  ?>
)

var Fullnames = new Array (
  <?php
  if (isset($addresses) && is_array($addresses)) {
    for ($i = 0; $i < count($addresses); $i++) {
      if ($i > 0) { echo ' , '; }
      echo '"' . $addresses[$i][2] . '"';
    }
  }
  echo "\n";
  ?>
)


function append(field) {
  if (field) {
    index = document.compose.to_options.selectedIndex - 1;
    if (Addresses[index]) {
      if (!document.compose.to.value) {
        punctuation = '';
      } else {
        punctuation = ', ';
      }
      document.compose.to.value = document.compose.to.value + punctuation + Fullnames[index] + ' <' + Addresses[index] + '>';
    }
  
  } else {
    index = document.compose.cc_options.selectedIndex - 1;
    if (Addresses[index]) {
      if (!document.compose.cc.value) {
        punctuation = '';
      } else {
        punctuation = ', ';
      }
      document.compose.cc.value = document.compose.cc.value + punctuation + Fullnames[index] + ' <' + Addresses[index] + '>';
    } else {
    index = document.compose.bcc_options.selectedIndex - 1;
    if (Addresses[index]) {
      if (!document.compose.bcc.value) {   
        punctuation = '';
      } else {
        punctuation = ', ';
      }
      document.compose.bcc.value = document.compose.bcc.value + punctuation + Fullnames[index] + ' <' + Addresses[index] + '>';
    }
    }
  }
}


function expand_to () {
  document.compose.to.value = expand(document.compose.to.value.split(',')).join(', ');
}


function expand_cc () {
  document.compose.cc.value = expand(document.compose.cc.value.split(',')).join(', ');
}

function expand_bcc () {
  document.compose.bcc.value = expand(document.compose.bcc.value.split(',')).join(', ');
}


function expand (addresses) {
  result = new Array();
  var match;
  
  if (Nicknames.length > 0) {
    for (i = 0; i < addresses.length; i++) {
      address = addresses[i];
      if (address != '') {
        while (address.charAt(address.length - 1) == ' ') { address = address.substring(0, address.length - 2); }
	while (address.charAt(0) == ' ') { address = address.substring(1, address.length); }
	
	for (j = 0; j < Nicknames.length; j++) {
          match = 0;
	  if (Nicknames[j].toLowerCase() == address.toLowerCase()) {
            result[result.length] = Fullnames[j] + ' <' + Addresses[j] + '>';
	    match = 1;
	    break;
	  }
	}
	if (match == 0) { result[result.length] = address; }
      }
    }
  
  } else { result = addresses; }
  
  return(result);
}


//-->
</script>
