function changePresenceStatus ( value )
{
	if( value < 1 || value > 8 )
		return;
	
	_user.get_presence().set_status( parseInt( value ) );	
		
	try
	{
		document.getElementById( "changeStatusContainer" ).style.display = "none";
	}
	catch( e ){}
}



function changePersonalMessageStatus ()
{
	var textArea = document.getElementById("personalMessageTextArea");
	var value = textArea.value;
	
	_user.get_presence().set_personalMessage( value );
}



function addAContact ()
{
	try
	{
		var element = document.getElementById( "addingContact" );
		var contactToAdd = element.value;
		
		var answer = confirm( "Aggiungi ai tuoi contatti " + contactToAdd + "?" )
		if( answer )
			_user.addContact( contactToAdd );
	}
	catch( e )
	{
	//console.log( "ERROR on addAContact " + e.message );
	}
	
	addContactMode("false");
}




function addAContactDevelopment ()
{
	addContactMode("false");

	try
	{
		var element = document.getElementById( "addAContactTextArea" );
		_user.addContact( element.value );
	}
	catch( e )
	{
	//console.log( "ERROR on addAContactDevelopment " + e.message );
	}
}



function countContacts ()
{
	var contacts = _user.get_contacts();
	var enumerator = contacts.getEnumerator();
	var i = 0;
	while (enumerator.moveNext()) 
	{
		i++;
	};
	return i;
}




function removeAContact ()
{
	var contactAddress = contactsByAddress[ document.getElementById( "removeAContactTextArea" ).value ];
	if( contactAddress == null )
		return;

	removeContact( contactAddress );
}



function searchContacts ()
{
	var strToSearch = document.getElementById( "filterContact" ).value;
	
	filterContacts( strToSearch );
	
	showContactList();
}



function cancelSearch ()
{
	resetSearch();
}



function resetSearch ()
{
	lastSearch = document.getElementById( "filterContact" ).value;
	filterContacts( "" );
	document.getElementById( "filterContact" ).value = filterContactDefaultValue;	
}



function cancelSearch ()
{
	filterContacts( "" );
}



function addEmoticon ()
{
	var textValue = document.getElementById( "addEmoticonTextArea" ).value;
	appendEmoticon( textValue );
}



function cancelFormAction ()
{
	return false;
}


function addAContactByAction ()
{
	addAContact();
	return false;
}


var dimensionState;
function resizeWindow ( version )
{
	var width = version == "big" ? 1000 : 637;
	var height = 732;
	var screenWidth = screen.width;
	var newX;
	var newY;
	
	
	if( screenWidth < 1050 && version == "big" )
	{
		var difference = 1050 - screenWidth;		
		width -= difference;
		try
		{
			document.getElementById( "customer_openbox" ).style.width = 420 - difference;
		}
		catch( e ){}
	}
	
	dimensionState = version;
	
	
	/* avoiding messenger going out of the screen */
	newX = ( screen.width - width ) / 2;
	newY = ( screen.height - height ) / 2;
	if( version == "big" && !isNaN( newX ) && !isNaN( newY ) )
		window.moveTo( newX, newY );		
	
	
	window.resizeTo( width, height );
	
	stopResizeInterval();
	var bgImg;
	
	switch( version )
	{
		case "big":
			document.getElementById( "customer_openbox" ).style.display = "block";
			document.getElementById( "customer_closedbox" ).style.display = "none";			
			bgImg = "/images/webmessenger/customer/background_opened.jpg";
			
			if( applicationType == "customer" )
				document.getElementById( "customer_logo" ).style.display = "none";
		break;
		default:
			document.getElementById( "customer_openbox" ).style.display = "none";
			document.getElementById( "customer_closedbox" ).style.display = "block";
			bgImg = "/images/webmessenger/customer/background_closed.jpg";

			if( applicationType == "customer" )
				document.getElementById( "customer_logo" ).style.display = "block";
		break;	
	}
	
	document.body.style.background = "url('" + bgImg + "') no-repeat top left";
}



function customer_logoClick()
{
	resizeWindow( dimensionState == "big" ? "small" : "big" );
}


var resizeInterval;
function startResizeInterval()
{
	resizeInterval = setInterval( resizeIntervalTick, 10000 );
}



function resizeIntervalTick ()
{
	resizeWindow( "small" );
}



function stopResizeInterval ()
{
	clearInterval( resizeInterval );
}



function comeBefore ( str1, str2, excludingAtChar, considerLowerCase )
{
	if( considerLowerCase )
	{
		str1 = str1.toLowerCase();
		str2 = str2.toLowerCase();
	}
	
	if( str1 == str2 )
		return false;
	
	for( var i = 0; i < str1.length && i < str2.length; i++ )
	{
		if( excludingAtChar )
		{
			if( str2.charAt( i ) == "@" )
			{
				i = str2.length;
				break;
			}
			if( str1.charAt( i ) == "@" )
			{
				i = str1.length;
				break;
			}
		}
		
		if( str1.charAt( i ) != str2.charAt( i ) )
			return str1.charAt( i ) < str2.charAt( i );
	}

	if( i == str2.length )
		return false;	
	else
		return true;
}



// ANDREA ROLLOVER

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

////////////////



function getAFakeContact ()
{
	var contact = new Object();
	var currentAddress = getAFakeCurrentAddress();
	var presence = getAFakePresence();
	var groups = [ getAFakeGroup() ];
	
	contact.get_currentAddress = function(){ return currentAddress; }	
	contact.get_groups = function(){ return groups; }
	contact.get_displayName = function()
	{ 
		var chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
		return chars[ Math.round( Math.random()*25 ) ] + "_Fake display name"; 
	}
	contact.get_presence = function(){ return presence; }
	contact.add_propertyChanged = function( eventCall )
	{  
		if( Math.floor( Math.random() * 3 ) != 3 )
			return;
			
		var e = getFakeEvent();		
		eventCall( null, e );		
	}
	contact.get_isOnline = function(){ return true; }
	contact.get_id = function(){ var strToReturn = ( Math.random()*1000000 ).toString(); return strToReturn; }
	
	return contact;
}
function getAFakeCurrentAddress ()
{
	var currentAddress = new Object();
	var address = "fakeAddress" + ( Math.random()*1000000 ).toString() + "@creeostudio.it";
	currentAddress.get_address = function(){ return address; }
	return currentAddress;
}
function getAFakePresence ()
{
	var presence = new Object();
	var status = 1;
	presence.get_status = function(){ return 1; }
	presence.add_propertyChanged = function(){}
	presence.get_personalMessage = function(){ return ""; }
	return presence;
}
function getAFakeGroup ()
{
	var group = new Object();
	var chars = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
	group.get_name = function(){ return "!" + chars[ Math.round( Math.random()*25 ) ] + "fakeGroup"; }
	return group;
}
function getFakeEvent ()
{
	var event = new Object();
	group.get_propertyName = function()
	{ 
		switch( Math.round( Math.random() * 2 ) )
		{
			case 0: return "DisplayName";
			case 1: return "Status";
			case 2: return "PersonalMessage";
		}
	}
	return group;
}


function MM_openBrWindow(theURL,winName,features) 
{ //v2.0
  window.open(theURL,winName,features);
}

