	/*************************THE CHAT RELATED JS CODES***********************/
	//var c=0;
	var t, tc;
	var keep_on = true;
	var seek_conf = true;
	
	/******CONF RELATED VARIBALES*****/
	var start_conf;
	var conf_id;
	/*********************************/
	
	
	var msg_arr = new Array();
	
		//********************CHAT WINDOW STATUS ARRAY**********************
		var chat_window_status = new Array(3);
		for(var i=1;i<=3;i++)
		{
			chat_window_status[i] = 0;
		}
		//******************************************************************
		
		//****************CHAT WINDOW TARGET USERS ARRAY********************
		var target_users = new Array(3);
		for(var i=1;i<=3;i++)
		{
			target_users[i] = new Array(2);
			//Initialize them all to "ZERO"sssss.....
			target_users[i][0] = 0;				//This holds the user id
			target_users[i][1] = 0;				//This holds the window reference
			//alert(i + ")." + target_users[i][0] + " and " + target_users[i][1]);
		}
		//******************************************************************
	
	
	
	
	
	
	function loop_fetch()
	{
		//Keeps on getting msgs from the server
		if(keep_on)
		{
			render_my_msgs();
		}
		//c=c+1;
		t=setTimeout("loop_fetch()",3000);
	}
	
	function conf_loop_fetch()
	{
		//alert("Lots of giggles, I'm being called");
		//alert(seek_conf);
		if(seek_conf)
		{
			initConfChat()
		}
		tc=setTimeout("conf_loop_fetch()",5000);
	}
	
	function wrtTxt(txt_msg, msg_to)
	{
		/***********************************************************************
		This function simply posts my message to the server for the intended 
		recipient
		***********************************************************************/
				
		if(!isEmpty(txt_msg))
		{
			var poststr = "action_name=post_msg&target_user=" + msg_to + "&msg=" + txt_msg;  	
			var path = "../Basic/ChatEngine.php";
			//alert(path);
			post_msg_to_srv(path, poststr);
		}
	}
	
	function render_my_msgs()
	{
		/**********************************************************************
		This is basically a very complex function fetcthing my unread msgs from
		the server, sorting them and displaying them!			
		**********************************************************************/
		
		//Make a call to the function which fetches the javascript arra from the server end!
		var path = "../Basic/ChatEngine.php?action_name=get_my_msg";
		var inner_str = "";
		var recipient;
		get_msg_array(path);
		//Expecting to have an array with me, show what we got, before that if possible backup the first element
		if(msg_arr.length > 0)
		{
			//The array is nitialized
			last_msg_from = msg_arr[0][1];	//By default assuming the present user is the last user
			last_msg_from_username = msg_arr[0][2];	//The name of the user with whome I was chatting lastly
		}
		for(x in msg_arr)
		{
			//We'll keep on looping and keep on showing in windows group bu userid
			if(last_msg_from != msg_arr[x][1])
			{
				//A different user...backup into the last msg var
				last_msg_from = msg_arr[x][1];
				last_msg_from_username = msg_arr[0][2];
			}
			
			//The same user continues...Cool chk if this guy is already in conversation with me
			window_no = returnUserByWindow(last_msg_from, true);
			if(window_no != 0)
			{
				//This person is in conversation with me on window on "window_no", render all messages there
			}
			else
			{
				//Here in comes some complexities, this person is basically initiating a chat, have to assign and open a new window for him/her.
				//window_no = 1;
				show_chat(last_msg_from, last_msg_from_username, false);
			}
			//Finally render message
			inner_str = inner_str + "<div class='chat-grey'>" + msg_arr[x][2] + "</div><div class='chat-black'>" + msg_arr[x][0] + " </div>";
			//Again trap the window
			window_no = returnUserByWindow(last_msg_from, true);
			print_msg(window_no, inner_str);
			//recipient = msg_arr[x][1];
		}
		
		//Reset the array to its initial state!
		msg_arr.length = 0;
		/*
		<div class="chat-grey">Souvik</div>
		<div class="chat-black">Hiiiiiiiiiiiiiiiii!</div>
		*/
		
		
		//document.f1.my_msg.focus();
	}
	
	function print_msg(window_no, inner_str)
	{
		//Render the text			
		ele =  document.getElementById("chat-body" + window_no);
		if(!isEmpty(inner_str))
		{
			//First show the chat area
			//show_chat(recipient);
			
			if(!isEmpty(ele.innerHTML))
			{
				ele.innerHTML = ele.innerHTML + inner_str;
			}
			else
			{
				ele.innerHTML = inner_str;
			}
			//Also scroll down to the bottom
			//ele = document.getElementById('chat-body');
			ele.scrollTop = ele.scrollHeight;
		}
	}
	
	function blockEnter(evt, indx) {
		evt = (evt) ? evt : event;
		var charCode = (evt.charCode) ? evt.charCode :((evt.which) ? evt.which : evt.keyCode);
		if (charCode == 13 || charCode == 3) {
			
			//hit_post(indx);
			
			//NEW APPROACH
			button_name = "document.f1.chat_button" + indx;
			button_name = eval(button_name);
			
			button_name.onclick();
			//document.getElementById(button_name).onclick();
			
			return false;
		} else {
			return true;
		}
	}
	function close_chat(indx)
	{
		chat_window_name = "staticcontent" + indx;
		document.getElementById(chat_window_name).style.display="none";
		//Also free the toggle back the status of this window
		chat_window_status[indx] = 0;
		//Free the user to window relationship
		freeUserWindowRelation(indx);
	}
	
	function freeUserWindowRelation(window_indx)
	{
		//This function releases the target_user array elements associated with the passed window_indx
		for(var i=1;i<=3;i++)
		{
			if(target_users[i][1] == window_indx)
			{
				//this is it
				target_users[i][0] = 0;
				target_users[i][1] = 0;
				break;
			}
		}
	}
	
	function ifUserAlreadyInChat(userid)
	{	
		ret_val = false;
		//Loop through to find out
		for(var i=1;i<=3;i++)
		{
			if(target_users[i][0] == userid)
			{
				//Found, toggle the retval variable and break
				ret_val = true;
				break;
			}
		}
		return ret_val;
	}
	
	function assignUserToWindow(userid, window_no)
	{
		//alert(userid + " and " + window_no);
		found = false;
		//Find an empty user slot and assign there
		for(var i=1;i<=3;i++)
		{
			//alert(target_users[i][0]);
			if(target_users[i][0] == 0)
			{
				//This ones empty, assign it here
				target_users[i][0] = userid;
				target_users[i][1] = window_no;
				//Toggle the found variable
				found = true;
				break;
			}
		}
		if(!found)
		{
			//All the slots are busy [:(] assign it to the first one only
			target_users[1][0] = userid;
			target_users[1][1] = window_no;
		}
	}
	
	function returnUserByWindow(passed_ref, get_the_window)
	{
		//Returns the user associated with an window, or window associated with user depending on second parameter
		if (typeof get_the_window == "undefined") 
		{
			get_the_window = false;
		}
		
		var ret_val = 0;
		//loop...
		for(var i=1;i<=3;i++)
		{				
			if(!get_the_window)
			{
				//Get the user based on passed_ref
				if(target_users[i][1] == passed_ref)
				{
					ret_val = target_users[i][0];
					break;
				}
			}
			else
			{
				//Get the window based on passed_ref
				if(target_users[i][0] == passed_ref)
				{
					ret_val = target_users[i][1];
					break;
				}
			}
		}
		return ret_val;
	}
	
	function show_chat(to_whome, target_user_name, set_focus)
	{
		if (typeof target_user_name == "undefined") 
		{
			target_user_name = false;
		}
		
		
		if (typeof set_focus == "undefined") 
		{
			set_focus = true;
		}
		
		//A variable to track if an empty window is found or not
		found = false;
		show_window = false;
		free_index = 1;
		//target_user = to_whome;
		
		//Chk if this user is already chatting
		if(!ifUserAlreadyInChat(to_whome))
		{
			////Not in chat with me, can proceed...Chk which window is free
			for(var i=1;i<=3;i++)
			{
				//alert(chat_window_status[i]);
				if(chat_window_status[i] == 0)
				{
					//This one is free
					free_index = i;
					//alert(to_whome);
					
					//Assign the user to the target_user array along with the window number
					assignUserToWindow(to_whome, i);
					
					//Mark the window array for this index as not free
					chat_window_status[i] = 1;
					
					//Toggle the variable which shows the window
					show_window = true;
				
					
					//Break
					break;
					
					//assign the found variable to true
					found = true;
				}
			}
			if(!found)
			{
				//alert("LEAK LEAK");
				//All the available chat windows are busy [:(], assign it to the default first one only
				assignUserToWindow(to_whome, 1);
				//Mark the window array for this index as not free
				chat_window_status[1] = 1;
				//Toggle the variable which shows the window
				show_window = true;
			}
			
			if(show_window)
			{
				//Form the name of the chat window
				chat_window_name = "staticcontent" + free_index;
				document.getElementById(chat_window_name).style.display="block";
				
				//Show the name of the target user on the title bar
				chat_window_title = "chatting_with" + free_index;
				document.getElementById(chat_window_title).innerHTML = target_user_name;
				
				if(set_focus)
				{
					if(free_index == 1)
					{
						document.f1.my_msg1.focus();
					}
					elseif(free_index == 2)
					{
						document.f1.my_msg1.focus();
					}
					elseif(free_index == 3)
					{
						document.f1.my_msg1.focus();
					}
				}
				
				
				//Also clear the previous chat!
				clearChatArea(free_index);
			}
		}
	}
	
	
	
	function clearChatArea(indx)
	{
		//Just clears the chat area for a particular chat window
		chat_area_name = "chat-body" + indx;
		document.getElementById(chat_area_name).innerHTML = "";
	}
	
	
	function show_array()
	{
		for(var i=1;i<=3;i++)
		{
			alert(i + ")." + target_users[i][0] + " and " + target_users[i][1] + "chat_window)status -> " + chat_window_status[i]);
			//alert(i);
		}
	}
	
	function restoreMinimize(indx)
	{
		//Accepts the index of the chat body and minimizes/restores it
		//First get status
		
		var wrapper_name = "wrapper" + indx;		//The main wrapper area which is wrapping up the chat n the buttons etc
		var wrapper_ele = document.getElementById(wrapper_name);
		
		var icon_name = "toggle_icon" + indx;
		var icon_ele = document.getElementById(icon_name);
		
		//get the show/hide status
		if(wrapper_ele.style.display == "none")
		{
			//We have to restore it up
			wrapper_ele.style.display = "block";
			//icon_ele.src = "../images/minimize.gif";
			document.f1.toggle_icon1.src = "../images/minimize.gif";
		}
		else
		{
			//We have to minimize it down
			wrapper_ele.style.display = "none";
			//icon_ele.src = "../images/restore.gif";
			//document.f1.toggle_icon1.src = "../images/restore.gif";
			document.f1.toggle_icon1.src = document.f1.img_restore.src;
		}
	}
	/*************************************************************************/
	
	
	
	
	
	
	/*****************CONFERENCE INITIATION RELATED FUNCTIONS******************/
	function initConfChat()
	{
		//First the the server file path...
		srv_path = "../Basic/ChatConfEngine.php";
		post_data = "action_name=check_initiation";
		
		//Get JS codes from server...
		getJSFromServer(srv_path, post_data);
		
		//The above in this case should return two variables
		// `start_conf`(true/false) if conference to start and if it is to start
		//then the if of the conference `conf_id`
		if(start_conf)
		{
			//Temporarily switch off conference seeking
			seek_conf = false;
			//Open the CONF JOINING screen
			open_win("../Basic/ChatConfJoin.php?conf_id=" + conf_id);
		}
	}
	/**************************************************************************/