function toggleOnList(formId, elemName, email, spanId) {
    form = document.getElementById(formId);
    txtElem = eval('form.' + elemName);
    spanElem = document.getElementById(spanId);
    txt = txtElem.value;
    emails = txt.split(',');
    for (i=0; i<emails.length; i++) {
        if(trimString(email) == trimString(emails[i])) {
            a1 = emails.splice(0,i);
            if(emails.length != 1) {
                a2 = emails.splice(1, email.length-1);
            } else {
                a2 = Array();
            }
            txtElem.value = trimString(a1.concat(a2).join(','));
            spanElem.className = '';
            return;
        }
    }
    if (emails.length == 1) {
       if (trimString(emails[0]) == "") {
          emails = Array();
       }
    }
    emails.push(' ' + email);
    txtElem.value = trimString(emails.join(','));
    spanElem.className = 'selected';
}

function toggleOnTextChange(txtElem, others) {
    txt = txtElem.value;
    emails = txt.split(',');
    all_emails = others.split(',');
    for (j=0; j<all_emails.length; j++) {
        s = 'email_' + trimString(emailToSafeStr(all_emails[j]))
        found = false;
        for (i=0; i<emails.length; i++) {   
            e = 'email_' + emailToSafeStr(emails[i]);
            if (e.toLowerCase() == s.toLowerCase()) {
               found = true;
            }
        }
        spanElem = document.getElementById(s);      
        if (spanElem != null) {
            if (! found) {
               spanElem.className = '';
            } else{
               spanElem.className = 'selected';
            }
        }
    }
}

function emailToSafeStr(str) {
   e = trimString(str)
   e = replaceAll(e, "@", "_at_");
   e = replaceAll(e, ".", "_dot_");
   e = replaceAll(e, " ", "_");
   return e;
}

function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );
    while ( idx > -1 ) {
        str = str.replace( from, to );
        idx = str.indexOf( from );
    }
    return str;
}

function trimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

function insertIntoText(formId, elemName, value) {
    form = document.getElementById(formId);
    txtElem = eval('form.' + elemName);
    insertAtCursor(txtElem, "%(" + value + ")");
    txtElem.focus();

}
function inArray(haystack, needle) {
    for(var i=0; i < haystack.length; i++) {
        if(haystack[i] == needle) {
            return true;
        }
    }
    return false;
}

function insertAtCursor(myField, myValue) {
   //IE support
   if (document.selection) {
      myField.focus();
      sel = document.selection.createRange();
      sel.text = myValue;
   }
   //MOZILLA/NETSCAPE support
   else if (myField.selectionStart || myField.selectionStart == '0') {
       var startPos = myField.selectionStart;
       var endPos = myField.selectionEnd;
       myField.value = myField.value.substring(0, startPos)
         + myValue
         + myField.value.substring(endPos, myField.value.length);
       myField.selectionEnd = endPos + myValue.length;
       myField.focus();
   } else {
      myField.value += myValue;
   }

}
