/*
(C) AEwebworks Software Development Ltd., 2002-2003
IMPORTANT: This is a commercial software product and any kind of using it must agree
to the AEwebworks Software Development Ltd. license agreement. It can be found at
http://www.aewebworks.com/license.htm
This notice may not be removed from the source code.
*/

/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form, do_check)
{
    var elts      = document.forms[the_form].elements;
    var elts_cnt  = elts.length;
	
    for (var i = 0; i < elts_cnt; i++) {
        elts[i].checked = do_check;
    } // end for

    return true;
} // end of the 'setCheckboxes()' function

var adding = 'false';
var adding_to_id = "";
var adding_to_name = "";

var checking = 'false';
var win = "width=600,height=500,left=100,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
function get_gallery(id_prof)
{
   window.open("photos_gallery.php?ID="+id_prof,'gallery',win);
}

function launchTellFriend ()
{
    var win = "width=300,height=300,left=100,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes";
    window.open("tellfriend.php",'tellfriend',win);
    return false;
}

function launchTellFriendProfile ( sID )
{
    var win = "width=300,height=300,left=100,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes";
    window.open("tellfriend.php?ID="+sID,'tellfriendprofile',win);
    return false;
}

function ShowShowHide ( show_name, show_name2, hide_name )
{
    if (hide_name) hide_name.style.display = 'none';
    if (show_name) show_name.style.display = 'inline';
    if (show_name2) show_name2.style.display = 'inline';
}

function ShowHideHide ( show_name, hide_name, hide_name2 )
{
    if (hide_name) hide_name.style.display = 'none';
    if (hide_name2) hide_name2.style.display = 'none';
    if (show_name) show_name.style.display = 'inline';
}


/**
 * change images onHover mouse action
 */
function show(FileName,jpg1Name)
{
	document.images[FileName].src = jpg1Name;
}

/**
 * set status of the browser window to 's'
 */
function ss(s) 
{
	window.status = s;
	return true;
}

/**
 * set status of the browser window to empty
 */
function ce()
{
	window.status='';
}

function sendMessage(from) {
		
	ajaxIM('send', document.getElementById('im_message').value, from, document.getElementById('im_to').options[document.getElementById('im_to').selectedIndex].value);	
	
	document.getElementById('im_message').value = "";
	document.getElementById("im_message").focus();
	
	ajaxIM('check', '', from, '');

}

function addTo(to_id, to_name) {
	
	if (to_id == "") { return; }
	
	adding_to_id 	= to_id;
	adding_to_name 	= to_name;
		
	found = "0";
	for (y=0;y<document.getElementById('im_to').options.length;y++) {
		if (document.getElementById('im_to').options[y].value==to_id) {
			document.getElementById('im_to').options[y].selected = "TRUE";
			found = 1;
			return;
		}
	}	
	
	if (found == "0") {
		var select = document.getElementById('im_to');								
		select.options[select.options.length] = new Option(to_name, to_id);														
		for (x=0;x<select.options.length;x++) { select.options[x].selected = ""; }
		select.options[select.options.length-1].selected = "TRUE";
	}	
}

function latestMessages(from) {
	ajaxIM('latest', '', from, '');
}

function checkMessages(from) {
	ajaxIM('check', '', from, '');
}

function startcheckMessages(from) {	
	checkMessages(from);
	setInterval ( "ajaxIM('check', '', " + from + ", '')", 5000 );
}

function checkEnter(e){
	var characterCode

	if(e && e.which){ 
		e = e
		characterCode = e.which
	}
	else
	{
		e = event
		characterCode = e.keyCode
	}

	if (characterCode == 13) {
		sendMessage(document.getElementById("from_id").value)
		return false 
	}
	else {
		return true 
	}
}

function ajaxIM(mode, message, from, to)
{
	var post_values = "mode=" + mode + "&message=" + message + "&from=" + from + "&to=" + to;
	
	
	// if checking make sure we arent checking already
	if (mode == "check" && checking == "true") {
		return;
	}
	else if (mode == "check") {
		checking = "true";
	}
	
//	else if (mode == "send") {
//		alert("sending message of " + message);
//	}
	
	var xmlhttp;
	if (window.XMLHttpRequest)
  	{
  		// code for IE7+, Firefox, Chrome, Opera, Safari
  		xmlhttp=new XMLHttpRequest();
  	}
	else if (window.ActiveXObject)
  	{
  		// code for IE6, IE5
  		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	else
  	{
//  		alert("Your browser does not support XMLHTTP!");
  	}
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
  			response = xmlhttp.responseText;

//			alert(response);

			if (mode == "latest" || mode == "check") {
				
				var last_from_id = "0";
				
				// update div with messages
				arr_lines = response.split("\n");
				for (i=0;i<arr_lines.length;i++) {

					if (arr_lines[i] != "") {
						arr_message = arr_lines[i].split("~~");
						last_from_id 	= arr_message[0];
						last_message 	= arr_message[2];
						last_from_name 	= arr_message[3];						
						last_to_name 	= arr_message[4];
						last_to_id 		= arr_message[5];
					}

					if (arr_lines[i] != "" && arr_message[2] != undefined) {
						
//						document.getElementById("im_messages").innerHTML += "<font color='red'><b>" + last_from_name + " says</b>: " + last_message + "</font><br />\n";
						
						// create div and set html for it
						var newdiv = document.createElement("div");
						newdiv.innerHTML = "<b>" + last_from_name + " says</b>: " + last_message + "<br />\n";
						
						var container = document.getElementById("im_messages");
						container.appendChild(newdiv);
						
						list_user_id 	= last_from_id;
						list_user_name 	= last_from_name;

						if (list_user_id != "0" && list_user_name != "") {				
							var found = 0;
							for (y=0;y<document.getElementById('im_to').options.length;y++) {
								if (document.getElementById('im_to').options[y].value==list_user_id) {								
//									if (from != list_user_id && mode == "latest" && adding_to_id == "") {										
									if (from != list_user_id && (mode == "latest" || mode == "check") && adding_to_id == "") {																				
										resetAll();										
										document.getElementById('im_to').options[y].selected = "TRUE";			
									}
									found = 1;
								}
							}	
							if (found == "0" && from != list_user_id) {
								var select = document.getElementById('im_to');								
								select.options[select.options.length] = new Option(list_user_name, list_user_id);														
								
//								if (from != list_user_id && mode == "latest" && adding_to_id == "") {
								if (from != list_user_id && (mode == "latest" || mode == "check") && adding_to_id == "") {									
									resetAll();									
									select.options[select.options.length-1].selected = "TRUE";
								}
							}
						}						
					}
				}
			
				document.getElementById("im_messages").scrollTop=document.getElementById("im_messages").scrollHeight;
			}
  		}

		if (mode == "check") {
			checking = "false";
		}
	}
	xmlhttp.open("POST","im_ajax.php?" + post_values,true);
	xmlhttp.send(null);
}

function resetAll() {
	var select = document.getElementById('im_to');									
	for (x=0;x<select.options.length;x++) { select.options[x].selected = ""; }
}

function chat_popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=700,height=300');");
}
