function showBlock(blockName)
    {
    document.getElementById(blockName).style.display = 'block';
  }
function hideBlock(blockName)
    {
    document.getElementById(blockName).style.display = 'none';
    }
function focusToField()
    {
        if(document.forms[0] != null && document.forms[0].elements[0]) {
            document.forms[0].elements[0].focus();
        }
    }

function doRefresh() {
    setTimeout( "refresh()", 2*1000 );
}

function refresh()
{
    window.location.replace(window.location);

}

function launchChat() {
    document.forms[0].submit();
}

function centerDiv(id) {
    element = document.getElementById(id);

    width = screen.width;
    height = screen.height;

    left = '-80';

    if(screen.width == '1024')
        left = '30';

    if(screen.width == '1152')
        left = '120';


    if(screen.width == '1280')
        left = '180';

    element.style.left = ((width/2) - (400/2) - left);
    element.style.top = 200;
}

//fonction qui permet d'ouvrir le chat
function openPopupWindow() {
    mywin = window.open('/public_chat.php?action=begin','Chat','scrollbars=no, status=no, width=860, height=600, resizable=yes, left=' + ((screen.width - 860)/2) + ',top=' + ((screen.height - 600)/2));
}

function clearField() {
    parent.document.getElementById('text').value = '';
}

function launchChat() {
            document.forms[0].submit();

            //position le scoll au fond de la div
            var x = document.getElementById('chat_display');
            x.scrollTop = x.scrollHeight;
            
        }
        
        function traiteReponse(value, message, room) {
            text = '';

            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            document.getElementById('chat').innerHTML = text;
        }

        function addSmiley(id) {
            value = document.getElementById(id).title;

            document.getElementById('text').value += value;
            document.getElementById('text').focus();
        }

        function openProfile(id) {
            profile = window.open('see_profile.php?id=' + id, 'Profil','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=400, height=450');
        }

        function openPicture(id) {

            picture = window.open('see_picture.php?id=' + id, 'Photo','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=400, height=600');
        }

        function showAction(id, pName, pEvent) {
            if(document.getElementById('box_action_1').style.display == 'block') {
                document.getElementById('box_action_1').style.display = 'none';
            }
            else {
                document.getElementById('box_action_2').style.display = 'none';

                if(navigator.appName.substr(0, 3) == 'Net') {
                	document.getElementById('box_action_1').style.left = pEvent.pageX;
                    document.getElementById('box_action_1').style.top = pEvent.pageY;
                }
                else {
                    document.getElementById('box_action_1').style.left = window.event.clientX;
                    document.getElementById('box_action_1').style.top = window.event.clientY;
                }

                document.getElementById('action_name').innerHTML = '<p class="chat_menu_title">' + pName + '&nbsp;<a class="main_menu" href="#" onclick="document.getElementById(\'box_action_1\').style.display = \'none\';">[x]</a></p>';
                document.getElementById('action_1').href = 'javascript:openProfile(' + id + ');';
                document.getElementById('action_2').target = 'Process';
                document.getElementById('action_2').href = 'test.php?action=private&user=' + id;
                //document.getElementById('action_5').href = 'javascript:displayOwnRoom(' + id + ', \'' + pName + '\');';
                document.getElementById('action_3').href = 'javascript:openPicture(' + id + ');';
                document.getElementById('box_action_1').style.display = 'block';
                document.getElementById('hidden_id').value = id;
                document.getElementById('recepter_nickname').innerHTML = 'Envoyer un oufdetoi à ' + pName;

                document.getElementById('invite_id').value = id;
                document.getElementById('invite_nickname').innerHTML = 'Inviter ' + pName + ' dans le salon';
            }
        }
        
        function displayOwnRoom() {
            
            
            document.getElementById('show_box_10').style.display = 'block';
        }

        function showPrivate(user, room, pEvent) {
            if(document.getElementById('box_action_2').style.display == 'block') {
                document.getElementById('box_action_2').style.display = 'none';
            }
            else {
                document.getElementById('box_action_1').style.display = 'none';

                if(navigator.appName.substr(0, 3) == 'Net') {
                	document.getElementById('box_action_2').style.left = pEvent.pageX;
                    document.getElementById('box_action_2').style.top = pEvent.pageY;
                }
                else {
                    document.getElementById('box_action_2').style.left = window.event.clientX;
                    document.getElementById('box_action_2').style.top = window.event.clientY;
                }

                document.getElementById('private_1').href = 'test.php?action=accept&user=' + user + '&room=' + room;
                document.getElementById('private_2').href = 'test.php?action=refuse&user=' + user + '&room=' + room;
                document.getElementById('box_action_2').style.display = 'block';
            }
        }
        
        function showBoxRoom(room, pName, pEvent) {
            if(navigator.appName.substr(0, 3) == 'Net') {
            	document.getElementById('box_action_3').style.left = pEvent.pageX;
                document.getElementById('box_action_3').style.top = pEvent.pageY;
            }
            else {
                document.getElementById('box_action_3').style.left = window.event.clientX;
                document.getElementById('box_action_3').style.top = window.event.clientY;
            }

            document.getElementById('room_name').innerHTML = '<p class="chat_menu_title">' + pName + '&nbsp;<a class="main_menu" href="#" onclick="document.getElementById(\'box_action_3\').style.display = \'none\';">[x]</a></p>';
            document.getElementById('room_1').href = 'test.php?action=join&room=' + room;
            //document.getElementById('room_2').href = '';
            document.getElementById('box_action_3').style.display = 'block';

        }

        function saveInterest(id) {
            for(i=0; i<10; i++) {
                if(document.getElementById('image_' + i))
                    document.getElementById('image_' + i).style.backgroundColor = '#FFECCF';
            }
            document.getElementById('hidden_interest').value = id;
            document.getElementById('image_' + id).style.backgroundColor = 'red';
        }

        function hideAction(id) {
            document.getElementById(id).style.display = 'none';

        }

        function traiteReponse(value) {
            text = '';

            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            document.getElementById('chat_display').innerHTML = text;
        }

        function afficheRooms(value) {
            text = '<p class="chat_menu_title">Liste des salons</p>';

            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            text += '<p class="chat_menu_item" align="center" style="margin-top:6px;"><a class="create_room_link" href="test.php?action=public" target="Process">créer un salon</a></p>';

            document.getElementById('box_2').innerHTML = text;
        }

        function afficheUsers(value, connected) {
            text = '<p class="chat_menu_title">Connectés : ' + connected + '</p>';

            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            document.getElementById('box_1').innerHTML = text;
        }

        function afficheUserRoom(value) {
            text = '<table border="0" cellpadding="0" cellspacing="0"><tr>';

            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            text += '</tr></table>';

            document.getElementById('user_room').innerHTML = text;
        }

        function affichePrivate(value) {
            if(value.length == 0) {
                text = '<p class="chat_menu_title">Demande</p>';
                text += '<p class="chat_menu_item" align="center">Aucune</p>';

                document.getElementById('box_4').innerHTML = text;

                return;
            }

            text = '<p class="chat_menu_title">Demande</p>';

            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            document.getElementById('box_4').innerHTML = text;
        }

        function afficheInterest(nickname, image, id, pName) {
            if(nickname.length == 0)
                return;

            document.getElementById('show_box_4').style.display = 'block';

            text = '<table border="0" cellspacing="0" cellpadding="1" class="create">';

            for(i=0; i<nickname.length; i++) {
                text += '<tr><td class="chat_menu_item_2"><a class="popup" href="#" onclick="openProfile(' + id[i] + ');">' + nickname[i] + ': </a></td><td><img src="images/' + image[i] + '" /></td><td>' + pName + '</td></tr>';
            }

            text += '</table>';

            document.getElementById('interest_nickname').innerHTML = text;

            //command = "document.getElementById('show_box_4').style.display = 'none';";

            //setTimeout(command, 3000);
        }
        
        function afficheNewAsk(nickname, id, room, pName) {
            if(nickname.length == 0)
                return;

            box = document.getElementById('show_box_6');

            if(document.getElementById('show_box_4').style.display == 'block')
                box.style.top = '450px';

            box.style.display = 'block';

            text = '<table border="0" cellspacing="0" cellpadding="1" class="create">';

            for(i=0; i<nickname.length; i++) {
                text += '<tr><td class="chat_menu_item_2"><a class="popup" href="#" onclick="openProfile(' + id[i] + ');">' + nickname[i] + '</a> aimerait rejoindre votre salon ' + pName[i] + '&nbsp;<a class="demande_link" href="test.php?action=accept&user=' + id[i] + '&room=' + room[i] + '" onclick="box.style.display = \'none\';" target="Process">accepter</a>&nbsp;<a class="demande_link" href="test.php?action=refuse&user=' + id[i] + '&room=' + room[i] + '" target="Process">refuser</a></td></tr>';
            }

            text += '</table>';

            document.getElementById('ask_nickname').innerHTML = text;
        }
        
        function afficheNewInvitation(roomId, roomName, senderId, senderName) {
            if(roomId.length == 0)
                return;

            box = document.getElementById('show_box_11');
            box.style.display = 'block';

            text = '<table border="0" cellspacing="0" cellpadding="1" class="create">';

            for(i=0; i<roomId.length; i++) {
                text += '<tr><td class="chat_menu_item_2">';
                text += '<a class="popup" href="#" onclick="openProfile(' + senderId[i] + ');">' + senderName[i] + '</a> vous invite à rejoindre son salon ' + roomName[i] + '&nbsp;<a class="demande_link" href="test.php?invite=accept&user=' + senderId[i] + '&room=' + roomId[i] + '" onclick="box.style.display = \'none\';" target="Process">accepter</a>&nbsp;<a class="demande_link" href="test.php?invite=refuse&user=' + senderId[i] + '&room=' + roomId[i] + '" onclick="box.style.display = \'none\';" target="Process">refuser</a>';
                text += '</td></tr>';
            }

            text += '</table>';

            document.getElementById('ask_invite').innerHTML = text;
        }
        
        function afficheNewRefuse(nickname, pStatus, pMotive) {
            if(nickname.length == 0)
                return;

            box = document.getElementById('show_box_8');

            if(document.getElementById('show_box_4').style.display == 'block')
                box.style.top = '450px';

            box.style.display = 'block';

            text = '<table border="0" cellspacing="0" cellpadding="1" class="create">';

            if(pMotive == '')
                motive = '';
            else
                motive = '(motif: ' + pMotive + ')';

            for(i=0; i<nickname.length; i++) {
                text += '<tr><td class="chat_menu_item_2">' + nickname[i] + ' a ' + pStatus[i] + ' votre demande ' + motive + '</td></tr>';
            }

            text += '</table>';

            document.getElementById('refuse_nickname').innerHTML = text;
        }

        function afficheNewPrivate(nickname, id, room) {
            if(nickname.length == 0)
                return;

            box = document.getElementById('show_box_5');

            if(document.getElementById('show_box_4').style.display == 'block')
                box.style.top = '450px';

            box.style.display = 'block';

            text = '<table border="0" cellspacing="0" cellpadding="1" class="create">';

            for(i=0; i<nickname.length; i++) {
                text += '<tr><td class="chat_menu_item_2" style="margin-top:4px;"><a class="popup" href="#" onclick="openProfile(' + id[i] + ');">' + nickname[i] + '</a>&nbsp;<a class="demande_link" href="test.php?action=accept&user=' + id[i] + '&room=' + room[i] + '" onclick="box.style.display = \'none\';" target="Process">accepter</a>&nbsp;<a class="demande_link" href="test.php?action=refuse&user=' + id[i] + '&room=' + room[i] + '" target="Process" onclick="document.getElementById(\'show_box_5\').style.display = \'none\';">refuser</a></td></tr>';
            }

            text += '</table>';

            document.getElementById('private_nickname').innerHTML = text;

            //command = "document.getElementById('show_box_5').style.display = 'none';";

            //setTimeout(command, 3000);
        }
        
        function afficheNewRefusedInvit(nickname, motive) {
            if(nickname.length == 0)
                return;

            box = document.getElementById('show_box_13');
            box.style.display = 'block';

            text = '<table border="0" cellspacing="0" cellpadding="1" class="create">';

            for(i=0; i<nickname.length; i++) {
                text += '<tr><td class="chat_menu_item_2">' + nickname[i] + ' ne veut pas rejoindre votre salon (motif: ' + motive + ')</td></tr>';
            }

            text += '</table>';

            document.getElementById('refuse_invitation').innerHTML = text;
        }

        function afficheInterestBox(value) {
            if(value.length == 0) {
                text = '<p class="chat_menu_title" colspan="2">Oufdetoi reçus</p>';
                text += '<p class="chat_menu_item" align="center">Aucun</p>';

                document.getElementById('box_3').innerHTML = text;

                return;
            }

            text = '<table border="0" width="100%" cellspacing="0" cellpadding="2">';
            text += '<tr><td class="chat_menu_title" colspan="2">Oufdetoi reçus</td></tr>';

            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            text += '</table>';

            document.getElementById('box_3').innerHTML = text;
        }
        
        function invitationRooms(value) {
            text = '<select name="my_rooms" class="create_input_text">';
            
            for(i=0; i<value.length; i++) {
                text += value[i];
            }

            text += '</select>';
            text += '</form>';
            
            if(document.getElementById('invite_rooms'))
	            document.getElementById('invite_rooms').innerHTML = text;
        }

        function testF5(pEvent) {
            // Compatibilité IE / Firefox
        	if(!pEvent&&window.event) {
        		pEvent=window.event;
        	}

        	// IE
        	if(pEvent.keyCode == 116) {
                f5_flag = true;
        	}
        	// DOM
        	if(pEvent.which == 116) {
                f5_flag = true;
        	}
        }

        function quitChatIE() {
            //if(navigator.appName.substr(0, 3) == 'Mic') {
		if(f5_flag == false) {
	
			document.forms[0].submit();
	            window.open('quit_chat.php', 'Chat', 'width=200, height=40, left=' + ((screen.width-200)/2) + ', top=' + ((screen.height-40)/2));
                    //if(navigator.appName.substr(0, 3) == 'Mic') {
                      //  document.getElementById('quit_chat').target = '_blank';
                    //}
                    //document.getElementById('quit_chat').submit();

		    //document.forms[0].submit();

            
                }

                if(profile == 1) {
                    profile == null;
                    return false;
                }

                if(picture == 1) {
                    picture == null;
                    return false;
                }

                if(invitation == 1) {
                    invitation == null;
                    return false;
                }

                
            //}
        }

        function quitChatMozilla() {
            if(navigator.appName.substr(0, 3) != 'Mic') {
		if(f5_flag == false) {
			document.forms[0].submit();
                    //document.getElementById('qui_chat').submit();
	            window.open('quit_chat.php', 'Chat', 'width=200, height=40, left=' + ((screen.width-200)/2) + ', top=' + ((screen.height-40)/2));
                    //if(navigator.appName.substr(0, 3) == 'Mic') {
                      //  document.getElementById('quit_chat').target = '_blank';
                    //}
                    //document.getElementById('quit_chat').submit();

		    //document.forms[0].submit();

            
                }

                if(profile == 1) {
                    profile == null;
                    return false;
                }

                if(picture == 1) {
                    picture == null;
                    return false;
                }

                if(invitation == 1) {
                    invitation == null;
                    return false;
                }
            }
        }

        function showBox(id) {
            hideAction();

            var e = document.getElementById('box_' + id);

            for(i=0; i<15; i++) {
                if(document.getElementById('box_' + i)) {
                    document.getElementById('box_' + i).style.display = 'none';
                }
            }

            e.style.display = 'block';
        }

        function hideAction() {
            for(i=1; i<=15; i++) {
                if(document.getElementById('box_action_' + i)) {
                    document.getElementById('box_action_' + i).style.display = 'none';
                }
            }
        }
        
        function interestShow() {
            for(i=0; i<10; i++) {
                if(document.getElementById('image_' + i))
                    document.getElementById('image_' + i).style.backgroundColor = '#FFECCF';
            }
            
            document.getElementById('show_box_3').style.display = 'block';
        }
        
        function testSendInterest() {
            if(document.getElementById('hidden_id') != '')
                document.getElementById('interest_form').submit();
        }

