// JavaScript Document

// This file is maintained by alireza goudarzi <alireza.goudarzi@gmail.com>

function test() {
	alert(document.forms["photoUpload"]);	
	
}

function inspect() {
	var obj = document.getElementById("searchFriendTextBox");
	var func = function () {
		alert("thisis myfunc");	
	}
	//var func = obj.onfocus;
	
	var func2 = func;
	func = function () {
		func2();	
		
	}
	func();
	
	
	
}
// this variable holds the ajax engine object.

var htarget = 1;

// Ajax engine factory
function createAjax() {
	var temp;
	try {
		temp = new XMLHttpRequest();
	
	
	} catch(e) {
		if ( ! (temp = new ActiveXObject("MSXML2.XMLHTTP"))) 
			temp = new ActiveXObject("Microsoft.XMLHTTP")	
	
	}
	if ( !temp) {
		alert("couldn't create ajax engine");
		}
	return temp;
		
}

// and loadPage to the object model.
// v_obj must be reference to an object wich has innerHTML property.
function ajaxEnable(v_objid) {
	// creating an psuedo target for links using anchors to make sure the state will be kept in history.

	if ( document.getElementById("htarget") == null ) {

		newel = document.createElement("DIV");
		
		newel.style.position = "absolute";
		newel.style.display = "none";
		newel.id = "htarget";
		document.body.appendChild(newel);
	
	
	}
	
	document.getElementById(v_objid).loadPage = function (v_handler,v_data,v_unstate) {
	// fixing url

	if ( !v_unstate ) {
	document.getElementById("htarget").innerHTML = "<a style='display:none;position:absolute;' name='h"+ htarget +"'></a>";
	window.location.hash = "h"+htarget;	
	htarget++;
	}
	// proceeding to ajax operation
	var ajaxEnginName = "ajaxEngin"+v_objid;
	var ajax = null;
	ajax = createAjax();
	eval("window."+ajaxEnginName+"=ajax;");
	ajax.onreadystatechange = new Function("","if ( "+ajaxEnginName+".readyState == 4 && "+ajaxEnginName+".status == 200 ) { document.getElementById('"+this.id+"').innerHTML = "+ajaxEnginName+".responseText;}");
		ajax.open("post","ajaxserver.php?req=ajax&handler="+v_handler,true);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(v_data);


	
	}
	
	return document.getElementById(v_objid);

}

//sending an ajax message to the a handler.
	// this function expect the string "OK" as response to assume things are ok otherwise it will fire an alert 
function sendMsg(v_handler,v_data) {
	
	ajax = createAjax();
	ajax.onreadystatechange = new Function("","if ( ajax.readyState == 4 && ajax.status == 200 ) {  if (ajax.responseText.toUpperCase() != 'OK' ) alert('Operation Failed Try Again'); }");
		ajax.open("post","ajaxserver.php?req=ajax&handler="+v_handler,true);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(v_data);
}

function sendMsgD(v_handler,v_data) {
	
	ajax = createAjax();
	ajax.onreadystatechange = new Function("","if ( ajax.readyState == 4 && ajax.status == 200 ) {  alert(ajax.responseText); }");
		ajax.open("post","ajaxserver.php?req=ajax&handler="+v_handler,true);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(v_data);
}


function callFunction(v_handler,v_data,v_outdata) {
	
		ajax = createAjax();
		ajax.open("post","ajaxserver.php?req=ajax&handler="+v_handler,false);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(v_data);
		if ( ajax.status == 200 ) 
			eval( v_outdata+ '= "'+ajax.responseText+'";');
		
	}

function callFunctionD(v_handler,v_data,v_outdata) {
	
		ajax = createAjax();
		ajax.open("post","ajaxserver.php?req=ajax&handler="+v_handler,false);
		ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		ajax.send(v_data);
		if ( ajax.status == 200 ) 
			window.open().document.write( v_outdata+ '= "'+ajax.responseText+'";');
		
	}

function profileEditSave() {
	 document.forms["profileEdit"].submit();
	
		
	
}

function profileEditCancel() {
	
}

function profileShowPics(v_image,v_caseId) {
	ajaxEnable("profilePicFrame");
	document.getElementById("profilePicFrame").loadPage("profilePicFrame","imgid="+v_image+"&caseId="+v_caseId);
	
}

function ACevinfo(vid,vname) {
	this.id  = vid;
	this.name = vname;
	}
var ACevlist = Array();
function ACinfo(id,fname,lname) {
	this.id  = id;
	this.fname = fname;
	this.lname = lname;
	}
var ACfrlist = Array(); // holds acinfo object for each person in friend list
var ACsendTo = Array();// holds the list of the selecte recipient


ACmybgcolor = "#f5f5f5";
ACmyfgcolor = "#000000";
ACselectedcolor = "#ffffff";
ACselectedItem = "#3B5998";// rgb( 59 , 89 , 152 ) ; 
ACselectedMatchedBg = "#5670A6";
ACunselectedMatchedBg = "#D9E0EA";

function fixCharColor(obj,color,bg) {
	var id = "namespan" + obj.id.substr(5);
	var namespan = document.getElementById(id);
	for ( var i = 0 ; i < namespan.childNodes.length ; i++ )  {
//		alert( namespan.childNodes[i].className);
		if ( namespan.childNodes[i].className == "ACmatchedletters" ){
//			namespan.childNodes[i].innerHTML = "hi";
			namespan.childNodes[i].style.color=color;
			namespan.childNodes[i].style.backgroundColor=bg;
			}
	
	}
		
}
function makeLabel(current,destId) {

	return "<span style='float:left' id='sendTo"+ACfrlist[current].id+"'>"+createRecepientLabel(ACfrlist[current].fname+" "+ACfrlist[current].lname,"document.getElementById('"+destId+"').removeChild(document.getElementById('sendTo"+ACfrlist[current].id+"'));")+"</span>"

}
function setColors(key,listId,destId) {
	// key = 40 is down
	// key = 38 is up

	var list  = document.getElementById(listId);
	var current=-1;
	var start = 1;// forfirerox
	if ( checkBrowser() == "MSIE") {	
		start = 0;
		for( var i = start ; i < list.childNodes.length ; i++ ) {
			if ( list.childNodes[i].style.backgroundColor.toUpperCase() == ACselectedItem ) {
				current = i;
				
//				alert(list.childNodes[i].style.backgroundColor);
				list.childNodes[i].style.backgroundColor =  ACmybgcolor;
				list.childNodes[i].style.color =  ACmyfgcolor;
				fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
				
				}
			
	}
} else { // for firefox
	for( var i = start ; i < list.childNodes.length ; i++ ) {
		if ( list.childNodes[i].style.backgroundColor == "rgb(59, 89, 152)" ) {
			current = i;
			list.childNodes[i].style.backgroundColor = ACmybgcolor;
			list.childNodes[i].style.color =  ACmyfgcolor;			
			fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
	
			}
	}
}
	if ( current==-1 ) {
		if ( key == 40 ) {
			for ( var i = start ; i < list.childNodes.length ; i++ )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }
			}
		
		} else if ( current >= start ) {
		
			if ( key == 38 ) {
				for ( var i = current-1 ; i > -1 ; i-- )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }

			} else if ( key == 40)  { // means key == 40
				var flag = false;
				for ( var i = current+1 ; i < list.childNodes.length; i++ ){
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
 						 list.childNodes[i].style.color =  ACselectedcolor;
						 fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 flag = true;
						 break;
						 } 
					} 
				if ( !flag) {
					 list.childNodes[current].style.backgroundColor = ACselectedItem;
			 		 list.childNodes[current].style.color =  ACselectedcolor;
					 fixCharColor(list.childNodes[current],"#ffffff",ACselectedMatchedBg);
						 }
			} else if ( key == 13 ) {
				
				document.getElementById(destId).innerHTML += makeLabel(current,destId);
				
//			alert(document.getElementById('sendTo'+ACfrlist[current].id).innerHTML);
			}
		} 
		
}
function resetAC(obj,oEv,listId,destId) {
	var txt = obj.value;
	var list = document.getElementById(listId);
	if ( txt != "" ) 
		list.style.visibility = "visible";
	else 
		list.style.visibility = "hidden";
	var txtArray = txt.split(" ");
	var reArray = Array();
	var names = Array();
	
	var flag = Array() // this flag will show the result for each RE
	var sumflag = true; // the result of and operation on all of flag elements. means if all REs have been matched.
	for ( var i = 0 ; i < txtArray.length ; i++ ) {
	 	reArray[i] = new RegExp("^"+txtArray[i],"i");
		flag[i] = false;
		}
	for ( var i  = 0 ; i < ACfrlist.length ; i++ ) {
			for ( var flagc = 0 ; flagc < flag.length ; flagc++ ) 
				flag[flagc] = false; // reseting flags for all RE for the new name in the friend list
			sumflag = true; // reset sumflag;
			names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
			var k;			
			for ( var j = 0 ; j < reArray.length ; j++ ){ // these two for match each RE with every segment of the name

				for (  k = 0 ; k < names.length ; k++ ){ 
						if ( reArray[j].test(names[k]) ){ // match RE and set flag if true
							flag[j] = true;
							break;
							} // after first occurance don't need to continue
				}
				if ( flag[j])
					names.splice(k,1); //the name element has been matched we remove it from the array
//				alert(flag+ " " + frlist[i].fname+" " + frlist[i].lname);
			}
			for ( var l = 0 ; l < flag.length ; l++ )
				sumflag = sumflag && flag[l];	// AND all the RE match results.
			
			if ( sumflag ) { // now we show the result 
					names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
					str = "";
					submatch = null;
					rest = "";
					subflag = false;
				for ( var nc = 0 ; nc < names.length ; nc++ ) {
						subflag = false; // reset subflag for each name
						for ( var rec = 0 ; rec < reArray.length ; rec++ ) {
							
							if ( submatch = reArray[rec].exec(names[nc])) {
								rest = names[nc].substr(submatch[0].length);
								str += "<span class='ACmatchedletters'>"+submatch[0] + "</span>" + rest+" ";
								subflag = true;
								break;
							}
						}
						if ( !subflag) // if name section doesn't have match just added it to the string
							str += names[nc]+ " ";
				}

				document.getElementById("namespan"+ACfrlist[i]['id']).innerHTML = str;
				document.getElementById("frdiv"+ACfrlist[i]['id']).style.display = "block";
	//			document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
				
			} else {

				document.getElementById("frdiv"+ACfrlist[i]['id']).style.display = "none";
//				document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
			}
			
		}
		
	setColors(oEv.keyCode,listId,destId);	
}
function hideAC(listId) {

		document.getElementById(listId).style.visibility="hidden";
		var list  = document.getElementById(listId);
		var start = 1;// forfirerox
		if ( checkBrowser() == "MSIE") {	
			start = 0;
			for( var i = start ; i < list.childNodes.length ; i++ ) {
					list.childNodes[i].style.backgroundColor = ACmybgcolor;
					list.childNodes[i].style.color = ACmyfgcolor;
			}
		} else { // for firefox
			for( var i = start ; i < list.childNodes.length ; i++ ) {
				list.childNodes[i].style.backgroundColor =  ACmybgcolor;
				list.childNodes[i].style.color = ACmyfgcolor;
		}
	}
}
function enableAC(inputId,listId,outputId) {
	document.getElementById(inputId).onkeyup = new Function ("","resetAC(this,event,'"+listId+"','"+outputId+"');");
	document.getElementById(inputId).onblur = new Function ("","hideAC('"+listId+"');");

}

function homesetColors(oEv,listId,destId) {
	// key = 40 is down
	// key = 38 is up
	if ( oEv )
		key = oEv.keyCode;
	else
		key = event.keyCode;

	var list  = document.getElementById(listId);
	var current=-1;
	var start = 1;// forfirerox
	if ( checkBrowser() == "MSIE") {	
		start = 0;
		for( var i = start ; i < list.childNodes.length ; i++ ) {
			if ( list.childNodes[i].style.backgroundColor.toUpperCase() == ACselectedItem ) {
				current = i;
				
//				alert(list.childNodes[i].style.backgroundColor);
				list.childNodes[i].style.backgroundColor =  ACmybgcolor;
				list.childNodes[i].style.color =  ACmyfgcolor;
				fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
				
				}
			
	}
} else { // for firefox
	for( var i = start ; i < list.childNodes.length ; i++ ) {
		if ( list.childNodes[i].style.backgroundColor == "rgb(59, 89, 152)" ) {
			current = i;
			list.childNodes[i].style.backgroundColor = ACmybgcolor;
			list.childNodes[i].style.color =  ACmyfgcolor;			
			fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
	
			}
	}
}
	if ( current==-1 ) {
		if ( key == 40 ) {
			for ( var i = start ; i < list.childNodes.length ; i++ )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }
			}
		
		} else if ( current >= start ) {
		
			if ( key == 38 ) {
				for ( var i = current-1 ; i > -1 ; i-- )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }

			} else if ( key == 40)  { // means key == 40
				var flag = false;
				for ( var i = current+1 ; i < list.childNodes.length; i++ ){
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
 						 list.childNodes[i].style.color =  ACselectedcolor;
						 fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 flag = true;
						 break;
						 } 
					} 
				if ( !flag) {
					 list.childNodes[current].style.backgroundColor = ACselectedItem;
			 		 list.childNodes[current].style.color =  ACselectedcolor;
					 fixCharColor(list.childNodes[current],"#ffffff",ACselectedMatchedBg);
						 }
			} else if ( key == 13 ) {
				if ( start == 1 ) // if it is firefox 
					current--;
				document.getElementById(destId).value = ACfrlist[current].fname+" "+ACfrlist[current].lname;
				document.forms['searchfriendform'].onsubmit = function () {};
				document.forms['searchfriendform'].submit();
				
//			alert(document.getElementById('sendTo'+ACfrlist[current].id).innerHTML);
			}
		} 
			
			if ( current == -1 ) { // if the 
			document.forms['searchfriendform'].onsubmit = function () {	}
			}
	
}
function homeresetAC(obj,oEv,listId,destId) {
//alert(oEv);					
	var txt = obj.value;
	var list = document.getElementById(listId);
	if ( txt != "" ) 
		list.style.visibility = "visible";
	else 
		list.style.visibility = "hidden";
	var txtArray = txt.split(" ");
	var reArray = Array();
	var names = Array();
	
	var flag = Array() // this flag will show the result for each RE
	var sumflag = true; // the result of and operation on all of flag elements. means if all REs have been matched.
	for ( var i = 0 ; i < txtArray.length ; i++ ) {
	 	reArray[i] = new RegExp("^"+txtArray[i],"i");
		flag[i] = false;
		}
	for ( var i  = 0 ; i < ACfrlist.length ; i++ ) {
			for ( var flagc = 0 ; flagc < flag.length ; flagc++ ) 
				flag[flagc] = false; // reseting flags for all RE for the new name in the friend list
			sumflag = true; // reset sumflag;
			names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
			var k;			
			for ( var j = 0 ; j < reArray.length ; j++ ){ // these two for match each RE with every segment of the name

				for (  k = 0 ; k < names.length ; k++ ){ 
						if ( reArray[j].test(names[k]) ){ // match RE and set flag if true
							flag[j] = true;
							break;
							} // after first occurance don't need to continue
				}
				if ( flag[j])
					names.splice(k,1); //the name element has been matched we remove it from the array
//				alert(flag+ " " + frlist[i].fname+" " + frlist[i].lname);
			}
			for ( var l = 0 ; l < flag.length ; l++ )
				sumflag = sumflag && flag[l];	// AND all the RE match results.
			
			if ( sumflag ) { // now we show the result 
					names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
					str = "";
					submatch = null;
					rest = "";
					subflag = false;
				for ( var nc = 0 ; nc < names.length ; nc++ ) {
						subflag = false; // reset subflag for each name
						for ( var rec = 0 ; rec < reArray.length ; rec++ ) {
							
							if ( submatch = reArray[rec].exec(names[nc])) {
								rest = names[nc].substr(submatch[0].length);
								str += "<span class='ACmatchedletters'>"+submatch[0] + "</span>" + rest+" ";
								subflag = true;
								break;
							}
						}
						if ( !subflag) // if name section doesn't have match just added it to the string
							str += names[nc]+ " ";
				}
				document.getElementById("namespan"+ACfrlist[i]['id']).innerHTML = str;
				document.getElementById("frdiv"+ACfrlist[i]['id']).style.display = "block";
	//			document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
				
			} else {
				document.getElementById("frdiv"+ACfrlist[i]['id']).style.display = "none";
//				document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
			}
			
		}
		
	homesetColors(oEv,listId,destId);	
}

function homeenableAC(inputId,listId,outputId) {
//	alert(document.getElementById(inputId).onkeyup );
	document.getElementById(inputId).onkeyup = new Function ("event","homeresetAC(this,event,'"+listId+"','"+outputId+"');");
	//document.getElementById(inputId).onkeyup = new Function ("","alert(this.event);");
	//alert(document.getElementById(inputId).onkeyup );

	document.getElementById(inputId).onblur = new Function ("","hideAC('"+listId+"');");

}

function showProfPicsToolTip(oEv,uid) {


	ajaxEnable('profPicsToolTip');
	var obj =document.getElementById('profPicsToolTip');

	obj.loadPage('profPicsToolTip','uid='+uid);
	t = oEv.screenY - 100;
//	alert(document.documentElement.scrollTop);
	if ( checkBrowser() == "MSIE" )
		t+= document.documentElement.scrollTop;
	else 
		t+= window.pageYOffset;
	if ( t > 500 ) 
		t -= 100;
	obj.style.top =t + "px";
	obj.style.left = oEv.screenX +30 + "px";
	obj.style.visibility = "visible";	
	
}

function hideProfPicsToolTip() {
	document.getElementById('profPicsToolTip').style.visibility="hidden";
	document.getElementById('profPicsToolTip').innerHTML = "";
		
}


var acFrReq=null;
function acceptFriendReq(uid) {
		callFunction("acceptFriendReq","uid="+uid,"acFrReq");
		acFrReq = acFrReq.split("&");
		
		if ( acFrReq[0] == 'ok' ) 
			{
				var tr = document.getElementById("trId"+uid);
				var parent = document.getElementById("trId"+uid).parentNode;
				parent.removeChild(tr);
				ajaxEnable('friendReqTitle').loadPage('getFriendReqTitleAjax','');

				
			} else {
				alert('problem');
			}
	
	
}

function rejectFriendReq(uid) {
		callFunction("rejectFriendReq","uid="+uid,"acFrReq");
		acFrReq = acFrReq.split("&");
		if ( acFrReq[0] == 'ok' ) 
			{
				var tr = document.getElementById("trId"+uid);
				var parent = document.getElementById("trId"+uid).parentNode;
				parent.removeChild(tr);
				ajaxEnable('friendReqTitle').loadPage('getFriendReqTitleAjax','');
			} else {
			alert('problem');	
				
			}
	
	
}
function friendReqNoUpdate() {
	
	var ReqNoObj = document.getElementById("frReqPanNo");

	if  ( ReqNoObj ) {
		ajaxEnable('frReqPanNo');
		setInterval('document.getElementById("frReqPanNo").loadPage("getFriendReqNoAjax","",true);',1000);
		
		
	}
		
}



function addmem_fixCharColor(obj,color,bg) {
	var id = "addmem_namespan" + obj.id.substr(12);
	var namespan = document.getElementById(id);
	for ( var i = 0 ; i < namespan.childNodes.length ; i++ )  {
//		alert( namespan.childNodes[i].className);
		if ( namespan.childNodes[i].className == "ACmatchedletters" ){
//			namespan.childNodes[i].innerHTML = "hi";
			namespan.childNodes[i].style.color=color;
			namespan.childNodes[i].style.backgroundColor=bg;
			}
	
	}
		
}

//"rgb(59, 89, 152)" check with ACselectedItem Color
function isACSelectedColor(thisColor) {
	if ( checkBrowser() == "MSIE" ) {
		if ( thisColor.toUpperCase() == ACselectedItem ) 
			return true;
	} 
	if ( thisColor == 	"rgb(59, 89, 152)" ) {
			return true;
	}
		
	return false;
}

function addmem_setColors(oEv,listId,destId) {
	// key = 40 is down
	// key = 38 is up

	if ( oEv )
		key = oEv.keyCode;
	else
		key = event.keyCode;

	var list  = document.getElementById(listId);
	var listitem; // will be each object item in the list
	var current=-1; // this will be the index in the childNodes array of he "listId" DIV
	var currentipid;
	var start = 0;

	// here we are detecting the currently selected item. current will be the index in the array;	
		for( var i = start ; i < list.childNodes.length ; i++ ) {
			listitem = list.childNodes[i];

			if ( listitem.tagName == "DIV" && isACSelectedColor(listitem.style.backgroundColor) ) {
				current = i;
				currentipid = listitem.id.substr(12);
				listitem.style.backgroundColor =  ACmybgcolor;
				listitem.style.color =  ACmyfgcolor;
				document.getElementById("addmem_namespan"+currentipid).style.color  =ACmyfgcolor;
			//alert(	document.getElementById("addmem_namespan"+currentipid).style.color);
				addmem_fixCharColor(listitem,"#000000",ACunselectedMatchedBg);
				}
		}
	
	if ( current==-1 ) {
		if ( key == 40 ) {
			for ( var i = start ; i < list.childNodes.length ; i++ )
					if (  list.childNodes[i].tagName=="DIV" && list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
							currentipid = list.childNodes[i].id.substr(12);
							
						 document.getElementById("addmem_namespan"+currentipid).style.color = ACselectedcolor;
						 addmem_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 list.childNodes[i].childNodes[0].focus();
						 break;
						 }
			} else if ( key==13 )  {
				
			//	document.getElementById("addIP").focus();	
			}
				
		
		} else if ( current == start && key==38 ) {
		
			// when on the first element of the list and hit up arrow re-focus on the textbox
				document.getElementById("addIP").focus();

			} else if ( current >= start ) {
		
			if ( key == 38 ) {
				for ( var i = current-1 ; i > -1 ; i-- )
					if (  list.childNodes[i].tagName=="DIV" &&list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						currentipid = list.childNodes[i].id.substr(12);
							
						 document.getElementById("addmem_namespan"+currentipid).style.color = ACselectedcolor;
						 addmem_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 list.childNodes[i].childNodes[0].focus();						 
						 break;
						 }

			} else if ( key == 40)  { // means key == 40
				var flag = false;
				for ( var i = current+1 ; i < list.childNodes.length; i++ ){
					if (  list.childNodes[i].tagName=="DIV" && list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
 						 list.childNodes[i].style.color =  ACselectedcolor;
						currentipid = list.childNodes[i].id.substr(12);
							
						 document.getElementById("addmem_namespan"+currentipid).style.color = ACselectedcolor;
						 addmem_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 list.childNodes[i].childNodes[0].focus();

						 flag = true;
						 break;
						 } 
					} 
				if ( !flag) {
					 list.childNodes[current].style.backgroundColor = ACselectedItem;
			 		 list.childNodes[current].style.color =  ACselectedcolor;
					currentipid = list.childNodes[i].id.substr(12);
							
						 document.getElementById("addmem_namespan"+currentipid).style.color = ACselectedcolor;
					 addmem_fixCharColor(list.childNodes[current],"#ffffff",ACselectedMatchedBg);
					 						
					 list.childNodes[current].childNodes[0].focus();
						 }
			} else if ( key == 13 ) {
					

} 
			
		
	
	}


}
function addmem_resetAC(inputId,oEv,listId,destId) {
	var obj = document.getElementById(inputId);
	var txt = obj.value;
	var list = document.getElementById(listId);
	var txtArray = txt.split(" ");
	var reArray = Array();
	var names = Array();
	var flag = Array() // this flag will show the result for each RE
	var sumflag = true; // the result of and operation on all of flag elements. means if all REs have been matched.
	for ( var i = 0 ; i < txtArray.length ; i++ ) {
	 	reArray[i] = new RegExp("^"+txtArray[i],"i");
		flag[i] = false;
		}
	for ( var i  = 0 ; i < ACfrlist.length ; i++ ) {
			for ( var flagc = 0 ; flagc < flag.length ; flagc++ ) 
				flag[flagc] = false; // reseting flags for all RE for the new name in the friend list
			sumflag = true; // reset sumflag;
			names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
			var k;			
			for ( var j = 0 ; j < reArray.length ; j++ ){ // these two for match each RE with every segment of the name

				for (  k = 0 ; k < names.length ; k++ ){ 
						if ( reArray[j].test(names[k]) ){ // match RE and set flag if true
							flag[j] = true;
							break;
							} // after first occurance don't need to continue
				}
				if ( flag[j])
					names.splice(k,1); //the name element has been matched we remove it from the array
//				alert(flag+ " " + frlist[i].fname+" " + frlist[i].lname);
			}
			for ( var l = 0 ; l < flag.length ; l++ )
				sumflag = sumflag && flag[l];	// AND all the RE match results.
			
			if ( sumflag ) { // now we show the result 
					names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
					str = "";
					submatch = null;
					rest = "";
					subflag = false;
				for ( var nc = 0 ; nc < names.length ; nc++ ) {
						subflag = false; // reset subflag for each name
						for ( var rec = 0 ; rec < reArray.length ; rec++ ) {
							
							if ( submatch = reArray[rec].exec(names[nc])) {
								rest = names[nc].substr(submatch[0].length);
								str += "<span class='ACmatchedletters'>"+submatch[0] + "</span>" + rest+" ";
								subflag = true;
								break;
							}
						}
						if ( !subflag) // if name section doesn't have match just added it to the string
							str += names[nc]+ " ";
				}
				document.getElementById("addmem_namespan"+ACfrlist[i]['id']).innerHTML = str;
				document.getElementById("addmem_frdiv"+ACfrlist[i]['id']).style.display = "block";
	//			document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
				
			} else {
				document.getElementById("addmem_frdiv"+ACfrlist[i]['id']).style.display = "none";
//				document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
			}
			
		}
		
	addmem_setColors(oEv,listId,destId);	
}

// this will search in the ACfrlist array for a prticular friend id and returns the index. if not returns false
function getFrindex(ipid) {
	for ( var i in ACfrlist ) 
		if ( ACfrlist[i].id == ipid ) 
			 return i;
	return false;
	
}
function addmem_setColors_onclick(oEv,listId,destId,itemObj) {
	// key = 40 is down
	// key = 38 is up
	// this event is fired onlick so there will be no key code. but this should have the same result as pressing enter on selected object.
	
	var list  = document.getElementById(listId);
	var listitem; // will be each object item in the list
	var current=-1; // this will be the index in the childNodes array of he "listId" DIV
	var currentipid;
	var start = 0;
	
			// here we highlight the item that's being clicked on 
			itemObj.style.backgroundColor = ACselectedItem;
			ipid = itemObj.id.substr(12);
			document.getElementById("addmem_namespan"+ipid).style.color  = ACselectedcolor;
			itemObj.style.color =  ACselectedcolor;
			addmem_fixCharColor(itemObj,"#ffffff",ACselectedMatchedBg);
			itemObj.childNodes[0].focus();
	
	// here we are detecting the currently selected item. current will be the index in the array;	
		for( var i = start ; i < list.childNodes.length ; i++ ) {
			listitem = list.childNodes[i];

			if ( listitem.tagName == "DIV" && isACSelectedColor(listitem.style.backgroundColor) ) {
				current = i;
				currentipid = listitem.id.substr(12);
				listitem.style.backgroundColor =  ACmybgcolor;
				listitem.style.color =  ACmyfgcolor;
				document.getElementById("addmem_namespan"+currentipid).style.color  = ACmyfgcolor;
				addmem_fixCharColor(listitem,"#000000",ACunselectedMatchedBg);
				}
		}
			// here we highlight the item that's being clicked on . because iterations above has reset them
			itemObj.style.backgroundColor = ACselectedItem;
			ipid = itemObj.id.substr(12);
			document.getElementById("addmem_namespan"+ipid).style.color  = ACselectedcolor;
			itemObj.style.color =  ACselectedcolor;
			addmem_fixCharColor(itemObj,"#ffffff",ACselectedMatchedBg);
			itemObj.childNodes[0].focus();
								
			//this line is just to ensure the page doesn't move because of choosing the link
				document.getElementById("htarget").innerHTML = "<a style='display:none;position:absolute;' name='"+(htarget++)+"'></a>";
			// now change the focus back to the text box
				document.getElementById("addIP").value="";
				document.getElementById("addIP").focus();
				// get the id from the friend
			
				
				// creating id for the image in selected friend list
				profId =  "profImageSpan" + currentipid;
				// coping the image source from the friends list
				imgsrc = document.getElementById(profId).childNodes[0].src;
				
				//we have to check if this friend is already added. if yes then nothing should happen.
				if ( document.getElementById("selectedipitem_"+currentipid) ) 
					return;
				
				// add the id to the hidden input element we are sending to the server
				document.getElementById('selectediplist').value+= currentipid+",";
				
				// now we find which element in ACfrlist contains info about the selected person
				var frindex  = getFrindex(currentipid);
				// now we add the info to the selected friends list
				document.getElementById(destId).innerHTML += "<div id='selectedipitem_"+currentipid+"'style='width:200px;border-bottom:1px solid #e0e0e0;'><table style='margin:3px;' cellspacing='0' cellpadding='0' width='184' border='0'><tr><td valign='middle' width='30'><img src='"+imgsrc+"' width='30' height='30'></td><td valign='middle' align='left' style='padding-left:5px;' >"+ ACfrlist[frindex].fname+" "+ACfrlist[frindex].lname+"<a style='float:left;' href='#removeip' onclick='removeSelectedIP("+currentipid+")' >remove</a></td></tr></table></div>";
}

function addmem_resetAC_onclick(inputId,oEv,listId,destId,itemObj) {
//alert(oEv);					
	
		
	addmem_setColors_onclick(oEv,listId,destId,itemObj);	
//	event.returnValue=false;
}


function addmem_enableAC(inputId,listId,outputId) {
	document.getElementById(inputId).onkeyup = new Function ("event","addmem_resetAC('"+inputId+"',event,'"+listId+"','"+outputId+"');");

}

function removeSelectedIP(ipid) {
	// fixing the hidden input box which holds all the selected friends
	var ipidarr = document.getElementById('selectediplist').value.split(",");
	for ( var i = 0 ; i < ipidarr.length ; i++ ) 
		if (ipidarr[i]==ipid)
			ipidarr.splice(i,1);
	document.getElementById('selectediplist').value = ipidarr.join(",");
	// now we have to remove the friend from the selected friend list.
	var itemobj = document.getElementById("selectedipitem_"+ipid);
	var itemparent = itemobj.parentNode;
	itemparent.removeChild(itemobj);
	
	
	
	document.getElementById("htarget").innerHTML = "<a style='display:none;position:absolute;' name='"+(htarget++)+"'></a>";
	
}
/************************* this is used in update memory *****************/
function removeSelectedDBIP(ipid) {
	// fixing the hidden input box which holds all the selected friends
//	var ipidarr = document.getElementById('selectediplist').value.split(",");
//	for ( var i = 0 ; i < ipidarr.length ; i++ ) 
//		if (ipidarr[i]==ipid)
//			ipidarr.splice(i,1);
//	document.getElementById('selectediplist').value = ipidarr.join(",");
	// now we have to remove the friend from the selected friend list.
	sendMsg("addMem_delSelectedDBIP","uid="+ipid);
	var itemobj = document.getElementById("selectedipitem_"+ipid);
	var itemparent = itemobj.parentNode;
	itemparent.removeChild(itemobj);
	
	
	
	
	document.getElementById("htarget").innerHTML = "<a style='display:none;position:absolute;' name='"+(htarget++)+"'></a>";
	
}

var addMem_newList_num=0;
var addMem_newTemp;

function addMem_refreshUploadedImageList() {
	var listobj = document.getElementById("uploadedImageList").childNodes[0];
	addMem_newTemp="";
	callFunction("addMem_uploadedImageList","","addMem_newTemp");
	if ( addMem_newTemp == "" ) 
		return;
	addMem_newTemp = addMem_newTemp.split("&&");
	var lasttr = listobj.childNodes[listobj.childNodes.length-1];	
	
	for ( var i = 0 ; i < addMem_newTemp.length ; i++ ) {

		if ( addMem_newList_num < 4 ) {
			ntd = lasttr.childNodes[addMem_newList_num];
			ntd.style.paddingBottom="5px";
			ntd.width="100";
			ntd.valign="top";
			ntd.align="center";
			ntd.innerHTML = addMem_newTemp[i];
			addMem_newList_num++;
		} else {
			ntr = document.createElement("TR");
			listobj.appendChild(ntr);
			for( j = 0 ; j < 4 ; j++ ) {
				ntd = document.createElement("TD");
				ntd.width="100";
				ntd.align="center";
				ntd.valign="top";
				ntr.appendChild(ntd); 
			}


			lasttr = listobj.childNodes[listobj.childNodes.length-1];
			ntd = lasttr.childNodes[0];
			ntd.style.paddingBottom="5px";
			ntd.innerHTML = addMem_newTemp[i];


			addMem_newList_num = 1;
		}
		
	}
}

function avoidFlicker() {
	if ( !document.getElementById("htarget") ) 
		document.body.innerHTML += "<div id=\"htarget\"></div>";
	document.getElementById("htarget").innerHTML = "<a style='display:none;position:absolute;' name='"+(htarget++)+"'></a>";
	
}
function addmem_deletePhoto(thisObj,imageIndex) {
		// first stop the processs that will need the table. then we recreate the table and restore the interval object
		var container = document.getElementById("uploadedImageList_Container");
		clearInterval(window.addMem_refreshUploadedImageList_Timer);
		avoidFlicker();
		sendMsg("addMem_delImage","index="+imageIndex);
		container.innerHTML = "";
		container.innerHTML = '<table width="430" id="uploadedImageList" cellpadding="0" cellspacing="0" border="0"><tr><td valign="top" width="100" align="center">&nbsp;</td><td valign="top" width="100" align="center">&nbsp;</td><td valign="top" width="100" align="center">&nbsp;</td><td valign="top" width="100" align="center">&nbsp;</td></tr></table>';
		// reset the cell counter to begin from the start
		addMem_newList_num = 0;
		// immediatly recreate image list
		addMem_refreshUploadedImageList();
		// now restore the interval
		window.addMem_refreshUploadedImageList_Timer = setInterval("addMem_refreshUploadedImageList()",2000);
		}
		
/******************* used in case of update photos *************/
function addmem_deleteDBPhoto(thisObj,imageId) {
		// first stop the processs that will need the table. then we recreate the table and restore the interval object
		var container = document.getElementById("uploadedImageList_Container");
		clearInterval(window.addMem_refreshUploadedImageList_Timer);
		avoidFlicker();
		sendMsg("addMem_delDBImage","imageId="+imageId);
		container.innerHTML = "";
		container.innerHTML = '<table width="430" id="uploadedImageList" cellpadding="0" cellspacing="0" border="0"><tr><td valign="top" width="100" align="center">&nbsp;</td><td valign="top" width="100" align="center">&nbsp;</td><td valign="top" width="100" align="center">&nbsp;</td><td valign="top" width="100" align="center">&nbsp;</td></tr></table>';
		// reset the cell counter to begin from the start
		addMem_newList_num = 0;
		// immediatly recreate image list
		addMem_refreshUploadedImageList();
		// now restore the interval
		window.addMem_refreshUploadedImageList_Timer = setInterval("addMem_refreshUploadedImageList()",2000);
		}

var addMem_newTemp_V;

function addMem_refreshUploadedVideoList() {
//	alert(document.getElementById("uploadedVideoList").childNodes[0].tagName);
var listobj = document.getElementById("uploadedVideoList").childNodes[0];

	addMem_newTemp_V="";
	callFunction("addMem_uploadedVideoList","","addMem_newTemp_V");
	if ( addMem_newTemp_V == "" ) 
		return;
		
	addMem_newTemp_V = addMem_newTemp_V.split("&&");
	for ( var i = 0 ; i < addMem_newTemp_V.length ; i++ ) {
		var tr = document.createElement("TR");
		var td = document.createElement("TD");
		td.innerHTML = addMem_newTemp_V[i];
		td.style.borderBottom = "1px solid #cccccc";
		tr.appendChild(td);
		
		listobj.appendChild(tr);
	}
	
	
}

function addmem_showVideo(url) {
		var vid = document.getElementById("VIDEO");

		vid.url = url;
		vid.Controls.play();
}
/* this is for update memory */
function addmem_showDBVideo(videoId) {
		var vid = document.getElementById("VIDEO");
		vid.url = "getVideo.php?vid="+videoId;
		vid.Controls.play();
}


var myvidtemp;
function addmem_deleteVideo(thisObj , videoIndex ) {
			// first stop the processs that will need the table. then we recreate the table and restore the interval object
		var container = document.getElementById("uploadedVideoList_Container");
		clearInterval(window.addMem_refreshUploadedVideoList_Timer);
		avoidFlicker();
		callFunction("addMem_delVideo","index="+videoIndex,"myvidtemp");
		
		container.innerHTML = "";
		container.innerHTML = '<table width="300" id="uploadedVideoList" cellpadding="0" cellspacing="0" border="0"></table>';
		
		
		
		// immediatly recreate image list
		addMem_refreshUploadedVideoList();
		// now restore the interval
		window.addMem_refreshUploadedVideoList_Timer = setInterval("addMem_refreshUploadedVideoList()",2000);
	
}
/* this is for update memory */
function addmem_deleteDBVideo(thisObj , videoId ) {
			// first stop the processs that will need the table. then we recreate the table and restore the interval object
		var container = document.getElementById("uploadedVideoList_Container");
		clearInterval(window.addMem_refreshUploadedVideoList_Timer);
		avoidFlicker();
		callFunction("addMem_delDBVideo","videoId="+videoId,"myvidtemp");
		
		container.innerHTML = "";
		container.innerHTML = '<table width="300" id="uploadedVideoList" cellpadding="0" cellspacing="0" border="0"></table>';
		
		
		
		// immediatly recreate image list
		addMem_refreshUploadedVideoList();
		// now restore the interval
		window.addMem_refreshUploadedVideoList_Timer = setInterval("addMem_refreshUploadedVideoList()",2000);
	
}
/* this part is for add sound to memerory */

var addMem_newTemp_S;

function addMem_refreshUploadedSoundList() {
	var listobj = document.getElementById("uploadedSoundList").childNodes[0];

	addMem_newTemp_S="";
	callFunction("addMem_uploadedSoundList","","addMem_newTemp_S");
	if ( addMem_newTemp_S == "" ) 
		return;
	
	addMem_newTemp_S = addMem_newTemp_S.split("&&");
	for ( var i = 0 ; i < addMem_newTemp_S.length ; i++ ) {
		var tr = document.createElement("TR");
		var td = document.createElement("TD");
		td.innerHTML = addMem_newTemp_S[i];
		td.style.borderBottom = "1px solid #cccccc";
		tr.appendChild(td);
		
		listobj.appendChild(tr);
	}
	
	
}




function addmem_showSound(url) {
	
		var vid = document.getElementById("SOUND");
		vid.url = url;
		vid.Controls.play();
}
var myvidtemp;
function addmem_deleteSound(thisObj , soundIndex ) {
			// first stop the processs that will need the table. then we recreate the table and restore the interval object
		var container = document.getElementById("uploadedSoundList_Container");
		clearInterval(window.addMem_refreshUploadedSoundList_Timer);
		avoidFlicker();
		callFunction("addMem_delSound","index="+soundIndex,"myvidtemp");
		container.innerHTML = "";
		container.innerHTML = '<table width="300" id="uploadedSoundList" cellpadding="0" cellspacing="0" border="0"></table>';
		
		
		
		// immediatly recreate image list
		addMem_refreshUploadedSoundList();
		// now restore the interval
		window.addMem_refreshUploadedSoundList_Timer = setInterval("addMem_refreshUploadedSoundList()",2000);
	
}

/* this part is for update memory */

function addmem_showDBSound(soundId) {
		var vid = document.getElementById("SOUND");
		vid.url = "getSound.php?sid="+soundId;
		vid.Controls.play();
}

function addmem_deleteDBSound(thisObj , soundId ) {
			// first stop the processs that will need the table. then we recreate the table and restore the interval object
		var container = document.getElementById("uploadedSoundList_Container");
		clearInterval(window.addMem_refreshUploadedSoundList_Timer);
		avoidFlicker();
		callFunction("addMem_delDBSound","soundId="+soundId,"myvidtemp");
		
		container.innerHTML = "";
		container.innerHTML = '<table width="300" id="uploadedSoundList" cellpadding="0" cellspacing="0" border="0"></table>';
		
		
		
		// immediatly recreate image list
		addMem_refreshUploadedSoundList();
		// now restore the interval
		window.addMem_refreshUploadedSoundList_Timer = setInterval("addMem_refreshUploadedSoundList()",2000);
	
}


function searchMem_fixCharColor(obj,color,bg) {
	var id = "searchMem_namespan" + obj.id.substr(15);
	var namespan = document.getElementById(id);
	for ( var i = 0 ; i < namespan.childNodes.length ; i++ )  {
//		alert( namespan.childNodes[i].className);
		if ( namespan.childNodes[i].className == "ACmatchedletters" ){
//			namespan.childNodes[i].innerHTML = "hi";
			namespan.childNodes[i].style.color=color;
			namespan.childNodes[i].style.backgroundColor=bg;
			}
	
	}
		
}

function searchMem_setColors(oEv,listId,destId) {
	// key = 40 is down
	// key = 38 is up
	if ( oEv )
		key = oEv.keyCode;
	else
		key = event.keyCode;

	var list  = document.getElementById(listId);
	var current=-1;
	var start = 1;// forfirerox
	if ( checkBrowser() == "MSIE") {	
		start = 0;
		for( var i = start ; i < list.childNodes.length ; i++ ) {
			if ( list.childNodes[i].style.backgroundColor.toUpperCase() == ACselectedItem ) {
				current = i;
				
//				alert(list.childNodes[i].style.backgroundColor);
				list.childNodes[i].style.backgroundColor =  ACmybgcolor;
				list.childNodes[i].style.color =  ACmyfgcolor;
				searchMem_fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
				
				}
			
	}
} else { // for firefox
	for( var i = start ; i < list.childNodes.length ; i++ ) {
		if ( list.childNodes[i].style.backgroundColor == "rgb(59, 89, 152)" ) {
			current = i;
			list.childNodes[i].style.backgroundColor = ACmybgcolor;
			list.childNodes[i].style.color =  ACmyfgcolor;			
			searchMem_fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
	
			}
	}
}
	if ( current==-1 ) {
		if ( key == 40 ) {
			for ( var i = start ; i < list.childNodes.length ; i++ )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 searchMem_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }
			}
		
		} else if ( current >= start ) {
		
			if ( key == 38 ) {
				for ( var i = current-1 ; i > -1 ; i-- )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 searchMem_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }

			} else if ( key == 40)  { // means key == 40
				var flag = false;
				for ( var i = current+1 ; i < list.childNodes.length; i++ ){
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
 						 list.childNodes[i].style.color =  ACselectedcolor;
						 searchMem_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 flag = true;
						 break;
						 } 
					} 
				if ( !flag) {
					 list.childNodes[current].style.backgroundColor = ACselectedItem;
			 		 list.childNodes[current].style.color =  ACselectedcolor;
					 searchMem_fixCharColor(list.childNodes[current],"#ffffff",ACselectedMatchedBg);
						 }
			} else if ( key == 13 ) {
				if ( start == 1 ) // if it is firefox 
					current--;
				document.getElementById(destId).value = ACfrlist[current].fname+" "+ACfrlist[current].lname;
				hideAC("searchMem_AClist");
				if ( validateSearchMemXX(oEv) ) 
					document.forms['searchMemoryForm'].submit();
			}
		} 
			
			if ( current == -1 ) { // if the 
	//		document.forms['searchMemoryForm'].onsubmit = function () {	}
			}
	
}
function searchMem_resetAC(obj,oEv,listId,destId) {
//alert(oEv);					
	var txt = obj.value;
	var list = document.getElementById(listId);
	if ( txt != "" ) 
		list.style.visibility = "visible";
	else 
		list.style.visibility = "hidden";
	var txtArray = txt.split(" ");
	var reArray = Array();
	var names = Array();
	
	var flag = Array() // this flag will show the result for each RE
	var sumflag = true; // the result of and operation on all of flag elements. means if all REs have been matched.
	for ( var i = 0 ; i < txtArray.length ; i++ ) {
	 	reArray[i] = new RegExp("^"+txtArray[i],"i");
		flag[i] = false;
		}
	for ( var i  = 0 ; i < ACfrlist.length ; i++ ) {
			for ( var flagc = 0 ; flagc < flag.length ; flagc++ ) 
				flag[flagc] = false; // reseting flags for all RE for the new name in the friend list
			sumflag = true; // reset sumflag;
			names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
			var k;			
			for ( var j = 0 ; j < reArray.length ; j++ ){ // these two for match each RE with every segment of the name

				for (  k = 0 ; k < names.length ; k++ ){ 
						if ( reArray[j].test(names[k]) ){ // match RE and set flag if true
							flag[j] = true;
							break;
							} // after first occurance don't need to continue
				}
				if ( flag[j])
					names.splice(k,1); //the name element has been matched we remove it from the array
//				alert(flag+ " " + frlist[i].fname+" " + frlist[i].lname);
			}
			for ( var l = 0 ; l < flag.length ; l++ )
				sumflag = sumflag && flag[l];	// AND all the RE match results.
			
			if ( sumflag ) { // now we show the result 
					names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
					str = "";
					submatch = null;
					rest = "";
					subflag = false;
				for ( var nc = 0 ; nc < names.length ; nc++ ) {
						subflag = false; // reset subflag for each name
						for ( var rec = 0 ; rec < reArray.length ; rec++ ) {
							
							if ( submatch = reArray[rec].exec(names[nc])) {
								rest = names[nc].substr(submatch[0].length);
								str += "<span class='ACmatchedletters'>"+submatch[0] + "</span>" + rest+" ";
								subflag = true;
								break;
							}
						}
						if ( !subflag) // if name section doesn't have match just added it to the string
							str += names[nc]+ " ";
				}
				document.getElementById("searchMem_namespan"+ACfrlist[i]['id']).innerHTML = str;
				document.getElementById("searchMem_frdiv"+ACfrlist[i]['id']).style.display = "block";
	//			document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
				
			} else {
				document.getElementById("searchMem_frdiv"+ACfrlist[i]['id']).style.display = "none";
//				document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
			}
			
		}
		
	searchMem_setColors(oEv,listId,destId);	
	
}

function searchMem_enableAC(inputId,listId,outputId) {
//	alert(document.getElementById(inputId).onkeyup );
	document.getElementById(inputId).onkeyup = new Function ("event","searchMem_resetAC(this,event,'"+listId+"','"+outputId+"');");
	//document.getElementById(inputId).onkeyup = new Function ("","alert(this.event);");
	//alert(document.getElementById(inputId).onkeyup );

	document.getElementById(inputId).onblur = new Function ("","hideAC('"+listId+"');");

}
/* this section is auto complete for search memory event */
function searchMemEV_enableAC(inputId,listId,outputId) {
//	alert(document.getElementById(inputId).onkeyup );

	document.getElementById(inputId).onkeyup = new Function ("event","searchMemEV_resetAC(this,event,'"+listId+"','"+outputId+"');");
	//document.getElementById(inputId).onkeyup = new Function ("","alert(this.event);");
	//alert(document.getElementById(inputId).onkeyup );

	document.getElementById(inputId).onblur = new Function ("","hideAC('"+listId+"');");
//	alert("enableAC:"+document.getElementById(inputId).onblur);

}

function searchMemEV_fixCharColor(obj,color,bg) {
	var id = "searchMem_evnamespan" + obj.id.substr(15);
	var namespan = document.getElementById(id);
	for ( var i = 0 ; i < namespan.childNodes.length ; i++ )  {
//		alert( namespan.childNodes[i].className);
		if ( namespan.childNodes[i].className == "ACmatchedletters" ){
//			namespan.childNodes[i].innerHTML = "hi";
			namespan.childNodes[i].style.color=color;
			namespan.childNodes[i].style.backgroundColor=bg;
			}
	
	}
		
}

function searchMemEV_setColors(oEv,listId,destId) {
	// key = 40 is down
	// key = 38 is up
	if ( oEv )
		key = oEv.keyCode;
	else
		key = event.keyCode;

	var list  = document.getElementById(listId);
	var current=-1;
	var start = 1;// forfirerox
	if ( checkBrowser() == "MSIE") {	
		start = 0;
		for( var i = start ; i < list.childNodes.length ; i++ ) {
			if ( list.childNodes[i].style.backgroundColor.toUpperCase() == ACselectedItem ) {
				current = i;
				
//				alert(list.childNodes[i].style.backgroundColor);
				list.childNodes[i].style.backgroundColor =  ACmybgcolor;
				list.childNodes[i].style.color =  ACmyfgcolor;
				searchMemEV_fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
				
				}
			
	}
} else { // for firefox
	for( var i = start ; i < list.childNodes.length ; i++ ) {
		if ( list.childNodes[i].style.backgroundColor == "rgb(59, 89, 152)" ) {
			current = i;
			list.childNodes[i].style.backgroundColor = ACmybgcolor;
			list.childNodes[i].style.color =  ACmyfgcolor;			
			searchMemEV_fixCharColor(list.childNodes[i],"#000000",ACunselectedMatchedBg);
	
			}
	}
}
	if ( current==-1 ) {
		if ( key == 40 ) {
			for ( var i = start ; i < list.childNodes.length ; i++ )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 searchMemEV_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }
			}
		
		} else if ( current >= start ) {
		
			if ( key == 38 ) {
				for ( var i = current-1 ; i > -1 ; i-- )
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						 searchMemEV_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 break;
						 }

			} else if ( key == 40)  { // means key == 40
				var flag = false;
				for ( var i = current+1 ; i < list.childNodes.length; i++ ){
					if ( list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
 						 list.childNodes[i].style.color =  ACselectedcolor;
						 searchMemEV_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
						 flag = true;
						 break;
						 } 
					} 
				if ( !flag) {
					 list.childNodes[current].style.backgroundColor = ACselectedItem;
			 		 list.childNodes[current].style.color =  ACselectedcolor;
					 searchMemEV_fixCharColor(list.childNodes[current],"#ffffff",ACselectedMatchedBg);
						 }
			} else if ( key == 13 ) {
				//if ( start == 1 ) // if it is firefox 
					//current--;
				//document.forms['searchMemoryForm'].onsubmit = function () {
					
			//		event.returnValue =false;	
				//}
				document.getElementById(destId).value = ACevlist[current].name;
				document.getElementById("searchMem_eventId").value = ACevlist[current].id;
				hideAC("searchMem_EVList");
				document.getElementById("searchMem_friendName").focus();
				if ( validateSearchMemXX(oEv) ) 
					document.forms['searchMemoryForm'].submit();

			}
		} 
			
			if ( current == -1 ) { // if the 

			}
	
}
function searchMemEV_resetAC(obj,oEv,listId,destId) {
//alert(oEv);			

	var txt = obj.value;
	var list = document.getElementById(listId);
	if ( txt != "" ) 
		list.style.visibility = "visible";
	else 
		list.style.visibility = "hidden";

	var txtArray = txt.split(" ");
	var reArray = Array();
	var names = Array();
	
	var flag = Array() // this flag will show the result for each RE
	var sumflag = true; // the result of and operation on all of flag elements. means if all REs have been matched.
	for ( var i = 0 ; i < txtArray.length ; i++ ) {
	 	reArray[i] = new RegExp("^"+txtArray[i],"i");
		flag[i] = false;
		}
	for ( var i  = 0 ; i < ACevlist.length ; i++ ) {
			for ( var flagc = 0 ; flagc < flag.length ; flagc++ ) 
				flag[flagc] = false; // reseting flags for all RE for the new name in the friend list
			sumflag = true; // reset sumflag;
			names = String(ACevlist[i].name).split(" ");// firstname and lastname can be many words.
			var k;			
			for ( var j = 0 ; j < reArray.length ; j++ ){ // these two for match each RE with every segment of the name

				for (  k = 0 ; k < names.length ; k++ ){ 
						if ( reArray[j].test(names[k]) ){ // match RE and set flag if true
							flag[j] = true;
							break;
							} // after first occurance don't need to continue
				}
				if ( flag[j])
					names.splice(k,1); //the name element has been matched we remove it from the array
//				alert(flag+ " " + frlist[i].fname+" " + frlist[i].lname);
			}
			for ( var l = 0 ; l < flag.length ; l++ )
				sumflag = sumflag && flag[l];	// AND all the RE match results.
			
			if ( sumflag ) { // now we show the result 
					names = String(ACevlist[i].name).split(" ");// firstname and lastname can be many words.
					str = "";
					submatch = null;
					rest = "";
					subflag = false;
				for ( var nc = 0 ; nc < names.length ; nc++ ) {
						subflag = false; // reset subflag for each name
						for ( var rec = 0 ; rec < reArray.length ; rec++ ) {
							
							if ( submatch = reArray[rec].exec(names[nc])) {
								rest = names[nc].substr(submatch[0].length);
								str += "<span class='ACmatchedletters'>"+submatch[0] + "</span>" + rest+" ";
								subflag = true;
								break;
							}
						}
						if ( !subflag) // if name section doesn't have match just added it to the string
							str += names[nc]+ " ";
				}
				document.getElementById("searchMem_evnamespan"+ACevlist[i]['id']).innerHTML = str;
				document.getElementById("searchMem_evdiv"+ACevlist[i]['id']).style.display = "block";
	//			document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
				
			} else {
				document.getElementById("searchMem_evdiv"+ACevlist[i]['id']).style.display = "none";
//				document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
			}
			
		}
		
	searchMemEV_setColors(oEv,listId,destId);	
	
}




function MEMUresetEvent() {
	document.forms['eventIdForm'].eventId.value = "0";
	document.getElementById("lastSelectedEventTr").style.display = "none";
	document.getElementById("10").style.display="block"
	
}



function showAddFriend_PProfile (fId) {

	var str = "friendId="+fId;
	callFunction("showAddFriend_pprofile",str,"AFRes");
	var header = "You have to be a friend to continue. Would you like to ask for friendship?";
	AFRes = AFRes.split('&&');
	if(AFRes[0] == "ok")
		{
		vheader = AFRes[1];
		vbody = AFRes[2];
		vfooter = AFRes[3];
		showBulletin(header,vbody,vfooter);
		}
	
	else
		{
		alert("problem");	
		}
		
}

function sendFriendRequest_pprofile(fId)
	{
	var message = document.getElementById('messageBody').value;
	var str = "friendId="+fId+"&message="+message;
	callFunction("sendFriendRequest",str,"SFRes");
	if(SFRes == "ok")
		{
		destroyBulletin();

		}
	else
		{
		alert("problem");	
		}
	}

function validateSearchMemXX(oEv) {

	var evId = document.getElementById('searchMem_eventId').value;
	var frName = document.getElementById('searchMem_friendName').value;
	var header;
//	alert( evId == "0" || frName == "");
	if  ( evId == "0" || frName == "" ) {
		header = "Required information is missing";
		vbody = "You must choose event and friend name before starting your search.";
		vfooter = "<input style=\"border: 0px solid #E0E0E0; height:20px; width:70px; background:#3B5999; color:#FFFFFF; font-weight:bold; border-bottom-style:outset; font-size:11px;\" type=\"button\" value=\"OK\" onclick=\"destroyBulletin();\" />";
		

		showBulletin(header,vbody,vfooter);
		if ( checkBrowser() == "MSIE" ) 
			event.returnValue = false;
		else
			oEv.preventDefault();
		return false;
	}
	return true;

}




/* this section is auto complete for compose */
function compose_enableAC(inputId,listId,outputId) {
//	alert(document.getElementById(inputId).onkeyup );
	document.getElementById(inputId).onkeyup = new Function ("event","compose_resetAC('"+inputId+"',event,'"+listId+"','"+outputId+"');");
	//document.getElementById(inputId).onkeyup = new Function ("","alert(this.event);");
	//alert(document.getElementById(inputId).onkeyup );

//	document.getElementById(inputId).onblur = new Function ("","hideAC('"+listId+"');");

}


function compose_fixCharColor(obj,color,bg) {
	var id = "compose_namespan" + obj.id.substr(13);
	var namespan = document.getElementById(id);
	for ( var i = 0 ; i < namespan.childNodes.length ; i++ )  {
//		alert( namespan.childNodes[i].className);
		if ( namespan.childNodes[i].className == "ACmatchedletters" ){
//			namespan.childNodes[i].innerHTML = "hi";
			namespan.childNodes[i].style.color=color;
			namespan.childNodes[i].style.backgroundColor=bg;
			}
	}
}

function compose_setColors(oEv,listId,destId) {
	// key = 40 is down
	// key = 38 is up

	if ( oEv )
		key = oEv.keyCode;
	else
		key = event.keyCode;

	var list  = document.getElementById(listId);
	var listitem; // will be each object item in the list
	var current=-1; // this will be the index in the childNodes array of he "listId" DIV
	var currentipid;
	var start = 0;

	// here we are detecting the currently selected item. current will be the index in the array;	
		for( var i = start ; i < list.childNodes.length ; i++ ) {
			listitem = list.childNodes[i];

			if ( listitem.tagName == "DIV" && isACSelectedColor(listitem.style.backgroundColor) ) {
				current = i;
				currentipid = listitem.id.substr(13);
				listitem.style.backgroundColor =  ACmybgcolor;
				listitem.style.color =  ACmyfgcolor;
				document.getElementById("compose_namespan"+currentipid).style.color  =ACmyfgcolor;
			//alert(	document.getElementById("compose_namespan"+currentipid).style.color);
				compose_fixCharColor(listitem,"#000000",ACunselectedMatchedBg);
				}
		}
	
	if ( current==-1 ) {
		if ( key == 40 ) {
			for ( var i = start ; i < list.childNodes.length ; i++ )
					if (  list.childNodes[i].tagName=="DIV" && list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
							currentipid = list.childNodes[i].id.substr(13);
							
						 document.getElementById("compose_namespan"+currentipid).style.color = ACselectedcolor;
						 compose_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
	//					 list.childNodes[i].childNodes[0].focus();
						 break;
						 }
			} else if ( key==13 )  {
				
			//	document.getElementById("addIP").focus();	
			}
				
		
		} else if ( current == start && key==38 ) {
		
			// when on the first element of the list and hit up arrow re-focus on the textbox
//				document.getElementById("addIP").focus();

			} else if ( current >= start ) {
		
			if ( key == 38 ) {
				for ( var i = current-1 ; i > -1 ; i-- )
					if (  list.childNodes[i].tagName=="DIV" &&list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
			   			 list.childNodes[i].style.color =  ACselectedcolor;
						currentipid = list.childNodes[i].id.substr(13);
							
						 document.getElementById("compose_namespan"+currentipid).style.color = ACselectedcolor;
						 compose_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
					//	list.childNodes[i].childNodes[0].focus();						 
						 break;
						 }

			} else if ( key == 40)  { // means key == 40
				var flag = false;
				for ( var i = current+1 ; i < list.childNodes.length; i++ ){
					if (  list.childNodes[i].tagName=="DIV" && list.childNodes[i].style.display == "block" ){
						 list.childNodes[i].style.backgroundColor = ACselectedItem;
 						 list.childNodes[i].style.color =  ACselectedcolor;
						currentipid = list.childNodes[i].id.substr(13);
							
						 document.getElementById("compose_namespan"+currentipid).style.color = ACselectedcolor;
						 compose_fixCharColor(list.childNodes[i],"#ffffff",ACselectedMatchedBg);
//						 list.childNodes[i].childNodes[0].focus();

						 flag = true;
						 break;
						 } 
					} 
				if ( !flag) {
					 list.childNodes[current].style.backgroundColor = ACselectedItem;
			 		 list.childNodes[current].style.color =  ACselectedcolor;
					currentipid = list.childNodes[i].id.substr(13);
							
						 document.getElementById("compose_namespan"+currentipid).style.color = ACselectedcolor;
					 compose_fixCharColor(list.childNodes[current],"#ffffff",ACselectedMatchedBg);
					 						
					// list.childNodes[current].childNodes[0].focus();
						 }
			} else if ( key == 13 ) {
					currentipid = list.childNodes[current].id.substr(13);
					document.getElementById("composeReceiver").value  = currentipid;

					document.getElementById("receiverInput").value  = ACfrlist[current].fname+" "+ACfrlist[current].lname;
					document.getElementById("receiverInput").disabled = "disabled";
					hideAC("compose_AC");

			} 
			
		
	
	}


}
function compose_resetAC(inputId,oEv,listId,destId) {
	//	alert(inputId);
	var obj = document.getElementById(inputId);
 
	var txt = obj.value;

	var list = document.getElementById(listId);
if ( txt != "" ) 
		list.style.visibility = "visible";
	else 
		list.style.visibility = "hidden";
	var txtArray = txt.split(" ");

	var reArray = Array();
	var names = Array();
	var flag = Array() // this flag will show the result for each RE
	var sumflag = true; // the result of and operation on all of flag elements. means if all REs have been matched.
	for ( var i = 0 ; i < txtArray.length ; i++ ) {
	 	reArray[i] = new RegExp("^"+txtArray[i],"i");
		flag[i] = false;
		}
	for ( var i  = 0 ; i < ACfrlist.length ; i++ ) {
			for ( var flagc = 0 ; flagc < flag.length ; flagc++ ) 
				flag[flagc] = false; // reseting flags for all RE for the new name in the friend list
			sumflag = true; // reset sumflag;
			names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
			var k;			
			for ( var j = 0 ; j < reArray.length ; j++ ){ // these two for match each RE with every segment of the name

				for (  k = 0 ; k < names.length ; k++ ){ 
						if ( reArray[j].test(names[k]) ){ // match RE and set flag if true
							flag[j] = true;
							break;
							} // after first occurance don't need to continue
				}
				if ( flag[j])
					names.splice(k,1); //the name element has been matched we remove it from the array
//				alert(flag+ " " + frlist[i].fname+" " + frlist[i].lname);
			}
			for ( var l = 0 ; l < flag.length ; l++ )
				sumflag = sumflag && flag[l];	// AND all the RE match results.
			
			if ( sumflag ) { // now we show the result 
					names = String(ACfrlist[i].fname+" "+ACfrlist[i].lname).split(" ");// firstname and lastname can be many words.
					str = "";
					submatch = null;
					rest = "";
					subflag = false;
				for ( var nc = 0 ; nc < names.length ; nc++ ) {
						subflag = false; // reset subflag for each name
						for ( var rec = 0 ; rec < reArray.length ; rec++ ) {
							
							if ( submatch = reArray[rec].exec(names[nc])) {
								rest = names[nc].substr(submatch[0].length);
								str += "<span class='ACmatchedletters'>"+submatch[0] + "</span>" + rest+" ";
								subflag = true;
								break;
							}
						}
						if ( !subflag) // if name section doesn't have match just added it to the string
							str += names[nc]+ " ";
				}
				document.getElementById("compose_namespan"+ACfrlist[i]['id']).innerHTML = str;
				document.getElementById("compose_frdiv"+ACfrlist[i]['id']).style.display = "block";
	//			document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
				
			} else {
				document.getElementById("compose_frdiv"+ACfrlist[i]['id']).style.display = "none";
//				document.getElementById("frdiv"+frlist[i]['id']).style.backgroundColor = "#ffffff";
		
			}
			
		}
		
	compose_setColors(oEv,listId,destId);	

}


function unlockcomposeto() {
	document.getElementById("receiverInput").disabled = "";
	document.getElementById("receiverInput").value = "";
	document.getElementById("composeReceiver").value = "";
		
}


/* this function is called from compose message */
function sendMessageAC(formObj,oEv) {

var send = true;
	if ( formObj.messageBody.value == "" ) 	
		send = false;
	if ( formObj.receiver.value == "0" ) 
		send = false;
	

	if ( send ) {

		sendMessage(oEv); // this is written by pooyan in jsFunctions.js
		return ;
	}
	
	var myheader = "Message Incomplete!";
	var mybody = "You have to choose a recepient and type in a message before sending.";	
	var myfooter="<input style=\"border: 0px solid #E0E0E0; height:20px; width:70px; background:#3B5999;"+
			"color:#FFFFFF; font-weight:bold; border-bottom-style:outset; font-size:11px;\" type=\"button\" "+
			"value=\"OK\" onclick=\"destroyBulletin();\" />";
	showBulletin(myheader,mybody,myfooter);
	if ( checkBrowser() == "MSIE" ) 
		event.returnValue = false;
	else
		oEv.preventDefault();
		
		
		
}



/* this is for add mem form validation */
function validateAddMem(formObj,oEv) {
	var flag  = true;	
	var str = "<UL>";
	if ( formObj.eventId.value == "0" ){
		flag = false;
		str += "<LI>You have to choose an event for your memory by clicking on one of the images on top of the page.</li>";
	}
	if ( formObj.Location.value == ""  || formObj.Location.value == " " ){
		flag = false;
		str += "<li>You have to indicate  where you memory has happened.</li>";	
		
	}
	if ( formObj.mem_date.value == "" || formObj.mem_date.value == " " ) {
		flag = false;
		str += "<li>You have to specify the date your memory has happened.</li>";
	}
	str += "</UL>";
	if ( !flag ) {
		var myfooter = "<input style=\"border: 0px solid #E0E0E0; height:20px; width:70px; background:#3B5999;"+
			"color:#FFFFFF; font-weight:bold; border-bottom-style:outset; font-size:11px;\" type=\"button\" "+
			"value=\"OK\" onclick=\"destroyBulletin();\" />";
		showBulletin("Incomplete Form","<div style='text-align:left;'>"+str+"</div>",myfooter); 
	} else {
		var myfooter = "<input style=\"border: 0px solid #E0E0E0; height:20px; width:70px; background:#3B5999;"+
			"color:#FFFFFF; font-weight:bold; border-bottom-style:outset; font-size:11px;\" type=\"button\" "+
			"value=\"OK\" onclick=\"submitForm(event,'eventIdForm');\" />";
		showBulletin("Saving Memory","By clicking OK button you're memory will be uploaded.",myfooter); 
		
	}
	

	if ( checkBrowser() == "MSIE" )
		event.returnValue = false;
	else
		oEv.preventDefault();
}
