
var send_button, remote_chat, local_chat, hidden_question;

function initialize_chat()
{
	send_button = document.getElementById('send_button');
	remote_chat = document.getElementById('remote_chat');
	local_chat = document.getElementById('local_chat');
	hidden_question = document.getElementById('question');
	
	var contact_button = document.getElementById('contact_button');
	
	remote_chat.value = 'Please wait.....\n\n';
	hidden_question.value = '';
	
	remote_say( '<wait><wait><wait><wait><wait>Hello, we have attorneys standing by to answer all your legal questions...<wait><wait>//Go ahead and enter your question and someone will be right with you.' );
	
	send_button.onclick=function(){send_text()}
	contact_button.onclick=function()
	{
		validate( this, ['FirstName','LastName','Email','AreaCode','Exchange','Suffix','WhereLive'], ['First Name is required','Last Name is required','Email Address is required','Area Code is required','Phone Exchange is required','Phone Suffix is required','State is required'], '-Please Select-' );
		return false;
	}
}

function send_text()
{
	var local_said = trim( local_chat.value );

	local_say(local_said);
	local_chat.value = '';
	
	if ( local_said.length > 0 ) 
	{
		remote_say('<wait><wait><wait><wait><wait>Hi!  My name is Lauren, and I am on a call with another client right now...Can I call you right back?...  <wait>//Please provide your information so I can contact you.<form>')
	}
	else
	{
		remote_say("Please type your question in the box below and click the Send button.");
	}

}

function local_say( text )
{
	var print_text = 'you> ' + trim( text ) + '\n\n';
	remote_chat.value += print_text;
	hidden_question.value += text;
}

function remote_say( text )
{
	send_button.disabled = true;
	slow_type( text );
}

function slow_type( text )
{
		// Warning: You CANNOT run another statement after calling this and expect it to be executed after
		// the message has been completed!  This is becuase slow_type (and its children) take place in their own
		// thread.
		
		var special_code;
		if ( text.match( /^<[^>]*>/ ) )
		{
			var m = text.match( /^<([^>]*)>/ );

			special_code = m[1];
			text = text.replace(/^<([^>]*)>/, '');
		}
		else
		{
			special_code = '';
		}

		var wait_time, to_type, new_text;
		if ( special_code == 'wait' )
		{
			wait_time = 1000;
			to_type = '';
			new_text = text;
			
		}
		else if ( special_code == 'form' )
		{
			toggle_layer( 'local_chat_holder' );
			set_height( 'chat_floatover', '450px' );
			toggle_layer( 'contact_fields' ); 
			remote_chat.scrollTop = remote_chat.scrollHeight;
			to_type = '';
			new_text = text;
		}		
		else
		{
			wait_time = Math.floor( 25 + ( Math.random() * ( 80 )));
			to_type = text.charAt(0);
			new_text = text.substring(1);

		}
		
		type_char( to_type );	
		if ( text.length > 1 )
		{
			window.setTimeout( "slow_type('" + new_text + "')", wait_time );
		}
		else
		{
			// we're done!
			remote_append( "\n\n" );
			send_button.disabled = false;
			remote_chat.scrollTop = remote_chat.scrollHeight;
		}
}

function type_char( the_char )
{
	if ( the_char == '|' )
	{
		remote_backspace();
	}
	else if ( the_char == '`' )
	{
		remote_append( "'" );
	}
	else if ( the_char == '/' )
	{
		remote_append( "\n" );

	}
	else
	{
		remote_append( the_char );
	}
	remote_chat.scrollTop = remote_chat.scrollHeight;

}

function remote_backspace()
{
	var new_text = remote_chat.value.substring(0, remote_chat.value.length - 1);
	remote_chat.value = new_text;
}

function remote_append( str )
{
	remote_chat.value += str;
}

function trim( str )
{
   return str.replace(/^\s*|\s*$/g,"");
}

function toggle_layer(layerName){
	if(layerName == null || layerName == '')
	{
		return;
	}
	expand(layerName);
}

function set_height( id, new_height )
{
  if (document.all) 
  {
      document.all[id].style.height = new_height;
  }
  else 
  {
      document.getElementById(id).style.height = new_height;
  }	
}

function expand(x) {
    var elem;
    if (document.all) {
        elem = document.all[x];
    }
    else {
        elem = document.getElementById(x);
    }
    
    if (elem.style.display == 'block') {
      elem.style.display = 'none'
    } else if (elem.style.display == '' || elem.style.display == 'none') {
      elem.style.display = 'block';
    }
    
}

