/*

	$Id: 2way.js,v 1.5 2006/07/19 17:18:43 scottd Exp $

*/

lastKey=null;
function hop(el, next, maxLength)
{
  // Strip out trailing non-numeric from current el,
  // and move focus from el to next as necessary.

  var pos = el.value.length ? "0123456789".indexOf(el.value.charAt(el.value.length - 1)) : 0;

  if (hop.arguments.length == 2)
    maxLength = el.maxLength;

  if (pos == -1)
    el.value = el.value.slice(0, el.value.length - 1);

  if ((lastKey == el.name + (el.value.length - 1)) && (el.value.length >= maxLength))
    next.focus();

  if (el.name == 'area')
  {
    if (el.value.charAt(0) == '1')
      el.value = el.value.slice(1);

    if (el.value.length > maxLength)
    {
      // cut and paste handler - check whole string for non-alpha
      pos = 0;
      do
      {
        if ("0123456789".indexOf(el.value.charAt(pos)) == -1)
          el.value = pos ? el.value.slice(0, pos) + el.value.slice(pos + 1) : el.value.slice(1);
        else
          pos++;
      } while (pos < el.value.length);

      // now divvy up the data amongs the other fields
      if (el.value.charAt(0) == '1')
        el.value = el.value.slice(1);

      if (el.value.length > 3)
      {
        el.form.elements.num1.value = el.value.slice(3,6);
        el.form.elements.num2.value = el.value.slice(6,10);
        el.value = el.value.slice(0,3);

        if (el.form.elements.num2.value.length > 0)
          el.form.elements.num2.focus();
        else
          if (el.form.elements.num1.value.length > 0)
          {
            if (el.form.elements.num1.value.length == 3)
              el.form.elements.num2.focus();
            else
              el.form.elements.num1.focus();
          }
      }
    }
  }

  lastKey = el.name + (el.value.length);
}

// function words
function words(content){
        var i=0;
        var numberofwords=1;
        while(i<=content.length) {
                if (content.substring(i,i+1) == " ") {
                        numberofwords++;
                        i++; 
                        // extra i++ makes it skip double spaces, or space/return
                }
                if (content.substring(i,i+1) == "\n") {
                        numberofwords++;
                        i++;
                        // extra i++ makes it skip double spaces, or space/return
                }
                i++;
        }
        return numberofwords;
}

// constants fro reply to types:  TODO:  move these somewhere better than this!

REPLY_NONE 	= 0;
REPLY_WEB 	= 1;
REPLY_MIN 	= 2;
REPLY_EMAIL = 3;


var replyTos = new Array();

// lets make possble replytos a collection of objects.
function replyTo ( type, value, selected ) {
	this.type		= type;
	this.value 		= value;
	this.selected	= selected ? true : false;
}

function populateReplyTos() {
	var sBox = document.getElementById ( "repliesTo" );
	var s = 0;
	
	if ( sBox ) {
		sBox.options[0] = null;
		while ( s < replyTos.length ) {
			sBox.options[sBox.length] = new Option ( replyTos[s].value, replyTos[s].type );
			s++;
		}
	}
}

function setupReplyTos() {
	// always set 'web' and 'none' as default replyTo methods.
	// also describes what order items will be inserted into the list.
	replyTos[replyTos.length] = new replyTo ( REPLY_WEB, SLWC_REPLYWEB );
	
	try  {
		if ( alias ) { 	
			if ( slwc_validateEmailAddress ( unescape ( alias ) ) ) { 

				replyTos[replyTos.length] = new replyTo ( REPLY_EMAIL, SLWC_REPLYEMAIL );
				
			}
		}
	
		if ( destaddress ) 
			if ( slwc_validateMIN ( unescape (destaddress) ) ) 
				replyTos[replyTos.length] = new replyTo ( REPLY_MIN, SLWC_REPLYMIN );
		
	}
	catch (e) {

	}
	//replyTos[replyTos.length] = new replyTo ( REPLY_MIN, SLWC_REPLYMIN );	
	replyTos[replyTos.length] = new replyTo ( REPLY_NONE, SLWC_REPLYNONE );
}

function updateRecipientsList (  ) {
	var target = false;
	var contact, form; 
	var x = 0;
	var oneSelected = false;
	try {
		form = document.getElementById ( "slwc_msgForm" );
		target = document.getElementById ( "msgRecipsList" );
		target.innerHTML = "";
		with ( document.addressBook ) {
			while ( contact = contacts[x] ) {
				if ( ( contact.selected == true ) || ( contact.selected == false ) ) {
					//target.insertAdjacentElement ( "afterBegin", div );
					target.innerHTML += contact.displayname +"<br>"
					//form["area"].value = contact.destaddress.substring ( 0, 3 );
					//form["num1"].value = contact.destaddress.substring ( 3, 6 );
					//form["num2"].value = contact.destaddress.substring ( 6, 10 );					
					oneSelected = true;
					
				}
				x++;
			}
		}
		//if ( !oneSelected )
		//	document.getElementById ( "msgRecipsTable" ).style.visibility = "hidden";
		//else
		//	document.getElementById ( "msgRecipsTable" ).style.visibility = "visible";
	}
	catch ( e ) {
		if ( document.log ) document.log ( "2way::updateRecipientsList -> " + e );	
	}
}

function _select () {
    try {
        event.cancelBubble = true;
    }
    catch (e) {
    }
    return false;
}


function init() {
	//setupMessageBox();
	//updateCharCount(document.getElementById("msgTextBox"));
	setupReplyTos();
	populateReplyTos();
	document.onselect = _select;
    document.onselectstart = _select;
	body = document.getElementsByTagName ( "body" )[0];
	body.unselectable = "on";
		

	// initialize the addressbook widget.

	new addressBook ( document.getElementById ( "addressBookCell" ), updateRecipientsList, false, false, true );
	// 	
	setTimeout ( "new chatInvitationPoller();", 3000 );
	updateRecipientsList();
}

