function noenter(evt) {
    evt = (evt) ? evt : ((event) ? event : null);
	keycode = evt.keyCode;
	//alert(keycode);
    if (keycode == "13") {
		
		send_client_text();
		if(evt.preventDefault){
			evt.preventDefault();
			return false;
		}
		
		return false;
    }
}

function start_chat(){
	$chat_number = $chat_number  + 1;
	$chat_box = '<div id="chatBox"><div class="chat_header">Need Help?</div><div id="chatConversation">';
	$chat_box = $chat_box +  '<p id="chatLine' + $chat_number +  '" class="convLine"><span class="chat_name">' + $admin_name + ':</span> <span class="chat_text">' + $initial_text + '</span></p>'
	
	$chat_box = $chat_box + '</div><div id="chatCurrentText"><form id="form1" method="post" action=""><label><input name="client_text" type="text" id="client_text" onfocus="javascript:chat_box_focus();" onkeypress="return noenter(event);" value="Just Type Here..." /></label><label><input type="button" name="send" id="send" value="Send" onclick="javascript:send_client_text();" /></label><a href="javascript:chat_close();">close</a> <span id="transStatus">&nbsp;</span></form></div></div>';
	$("span#chat_starter").after($chat_box);
	$("div#chatBox").fadeIn(2000);
	$chat_displayed = true;
	Nifty("div#chatBox","small");
	
	if($chat_conversation == undefined){
		
		//$chat_conversation = new Object:
	} else {
		
		add_conversation();
		scroll_to_bottom($scrollt);
	}
	
}

function chat_box_focus(){
	$my_text = $("input#client_text").attr("value");
	
	if($my_text == "Just Type Here..." || $my_text == "Type to continue..."){
		$("input#client_text").attr({value: ""});
	}
}

function add_conversation(){
		$con  = eval($chat_conversation);
	 	//alert("JSON Data: " + json[0]);
		$x = 0;
		// loops through all the responses and displayes them
		while($x < $con.length){
			//alert($con[$x][0] + " " + $con[$x][1]);
			if($con[$x][0]=="0"){
				$me_text = format_chat_text_admin($con[$x][1]);
				add_text($me_text)
			}
			if($con[$x][0]=="1"){
				$me_text = format_chat_text($con[$x][1]);
				add_text($me_text);
			}
			
			
			
			$x = $x + 1;
		}
}


function chat_close(text1){
	$("div#chatBox").slideUp(2000);
	if(text1 == "" || text1 == undefined){
		text1 = "Chat Terminated By Client";
	} else {
		
	}
	
	if($chat_initiated && text1 != "closer"){
		text1 = "Chat Terminated By Client";
		$.getJSON($url + "apps/chat/",{ client_close: text1, chat_code: $chat_code },
		   function(json){
			ajax_return_data(json)
			
		   }
		 );
	} else {
		text1 = "Chat cancelled";
		$.getJSON($url + "apps/chat/",{ client_close: text1, chat_code: $chat_code },
		   function(json){
		   }
		 );
	}
	
	$my_text = "Chat Closed.";
	$("input#client_text").attr({value: "Type to continue..."});
	$me_text = format_chat_text($my_text);
	add_text($me_text);
	
	display_chat_icon();
	
}

function chat_open(){
	if($chat_initiated){
		text1 = "Chat Opened By Client";
		$.getJSON($url + "apps/chat/",{ client_open: text1, chat_code: $chat_code },
		   function(json){
		   }
		 );
	} else {
		text1 = "Chat opened";
		$.getJSON($url + "apps/chat/",{ client_open: text1, chat_code: $chat_code },
		   function(json){
		   }
		 );
	}
	
	$my_text = "Chat Opened.";
	$("input#client_text").attr({value: "Type to continue..."});
	$me_text = format_chat_text($my_text);
	add_text($me_text);	
}


function diaplay_chat(){
	
	if($chat_displayed){
		$("div#chat_icon").hide();
		chat_open();
		$("div#chatBox").fadeIn(2000);
		
	} else {
		$("div#chat_icon").hide();
		start_chat();
	}
	
}


function display_chat_icon(){
	if($icon_displayed){
		$("div#chat_icon").show();
	} else {
	
	 $chat_icon =  '<div id="chat_icon" class="chat_icon"><a href="javascript:diaplay_chat();"><img src="' + $url + 'images/framing-chat-icon-online.gif" alt="Professional Framer Online. Click here to chat" width="150" height="50" border="0" /></a></div>';
	 $("span#chat_starter").after($chat_icon);
	 
	
	$icon_displayed = true;
	
	}
	close_timer();
}

function display_chat_icon_offline(){

	
	if($icon_displayed){
		$("div#chat_icon").show();
	} else {
	
	 $chat_icon =  '<div id="chat_icon" class="chat_icon"><a href="mailto:support@arttoframes.com"><img src="' + $url + 'images/framing-chat-icon-offline.gif" alt="Professional Framer Offile. Click here to contact us" width="150" height="50" border="0" /></a></div>';
	 $("span#chat_starter").after($chat_icon);
	 
	$icon_displayed = true;
	
	}
	close_timer();
}

function add_text($ctext){
	$chat_p = "p#chatLine" + ($chat_number - 1);
	$($chat_p).after(unescape($ctext));
	//$("div#chatConversation").after($ctext);
	$scrollt = $scrollt + 100;
	document.getElementById('chatConversation').scrollTop = $scrollt;
}
function scroll_to_bottom($scrollto){
	document.getElementById('chatConversation').scrollTop = $scrollto;
}
// this function sends over AJAX any text that the client enters.
// if the chat has not yet been initated it calls the "client initiate" to intiate the chat on the server
// if the chat has been initated it call the "client text" function on the server
function send_client_text(){
	$("span#transStatus").html("transferring...");
	$my_text = $("input#client_text").attr("value");

	if($chat_initiated){
		$.getJSON($url + "apps/chat/",{ client_text: escape($my_text), chat_code: $chat_code },
		   function(json){
			   $chat_initiated = true;
			   $("span#transStatus").html("&nbsp;");
				ajax_return_data(json)
			
		   }
		 );
		
	} else {
		$.getJSON($url + "apps/chat/",{ client_initiate: $my_text, admin_id: $admin_id, chat_code: $chat_code },
		   function(json){
			   $chat_initiated = true;
			   $("span#transStatus").html("&nbsp;");
				ajax_return_data(json)
			
		   }
		 );
		
	}
	// resets the decay of the time
	reset_timer();
}

// after the data is submitted to the server, this function takes care of the response
function ajax_return_data(json){
		// the second element in the return array is an array of all the admin responses
		$con = json[1];
	 	//alert("JSON Data: " + json[0]);
		$x = 0;
		// loops through all the responses and displayes them
		while($x < $con.length){
			$me_text = format_chat_text_admin($con[$x]);
			add_text($me_text)
			$x = $x + 1;
		}
		// clears the client text box
		$my_text = $("input#client_text").attr("value");
		if($my_text != "" || $my_text != undefined){
			$("input#client_text").attr({value: ""});
			$me_text = format_chat_text($my_text);
			add_text(unescape($me_text));
		}

}

// this function formats the response and any text entered by client
function format_chat_text($text){
	$chat_number = $chat_number + 1;
	$chat_text = '<p id="chatLine' + $chat_number +  '" class="convLine"><span class="chat_name">Me:</span> <span class="chat_text">' +  $text + '</span></p>';
	
	return $chat_text;

}

// formats text entered by admins
function format_chat_text_admin($text){
	if($text == "Chat terminated by Administrator"){
		chat_close("closer");
	}
	
	$chat_number = $chat_number + 1;
	$chat_text = '<p id="chatLine' + $chat_number +  '" class="convLine"><span class="chat_name">' + $admin_name + ':</span> <span class="chat_text">' +  $text + '</span></p>';
	
	return $chat_text;

}

// this function updates the chat conversation at intermitted intervals controlled the the timer
function json_update_conversation(json){
	//alert(decay_time);
	$con = json[1];
	if($con.length == 0 || $con.length == undefined){
		time_interval_val = time_interval_val + 1;
		decay_timer();								   
	} else {
		reset_timer();
	}
	
	$x1 = 0;
	while($x1 < $con.length){
		$me_text = format_chat_text_admin($con[$x1]);
		//alert($me_text);
		add_text($me_text)
		$x1 = $x1 + 1;
	}
	
}

// this is timer related functions
var interval = 0;
var decay_time = 0;
var time_interval_val = 0;
function set_timer(time_var){
	milli_seconds = time_var * 0;
	 interval  = window.setInterval(update_conversations, milli_seconds);	
	 //alert("interval" + interval);
}

function close_timer(){
	time_interval_val = time_interval_val + 1;
	//alert("interval "  + interval + " ran times " + time_interval_val);
	window.clearInterval(interval);
}


function update_conversations(){
	if(decay_time > 0){
		
		
	}
	
	$.getJSON("../apps/chat/",{ client_conversation:  "1", chat_code:  $chat_code},
	   function(json){

			json_update_conversation(json)
		
	   }
	 );
}

function decay_timer(){
	close_timer();
	decay_time = decay_time +  ((time_interval_val)/(time_interval_val/2));
	set_timer(decay_time);
}

function reset_timer(){
	close_timer();
	decay_time = 0;
	set_timer(decay_time);
}



