var nb_childs;
var nb_hobbies;
var nb_networks;
var num_photo_affiche;

var wgTimer;
var $tm = 0;
var idx = 0;
var dly = -1000;
var nb_text_char = 0;
var action;

var browser = navigator.appName; 
var NS4 = (document.layers) ? true : false;

function myCheckEnter(event, id)
{
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	
	if (code==13)
		$(id).submit();
}

function IsEnter(event)
{
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	
	if (code==13)
		return true;
	return false;
}

function EnterHobby(id, event)
{
	if (IsEnter(event) == true)
	{
		add_hobby();
		return false;
	}
	else
	{
		gestion_intelligente(id, event);
		return true;
	}
}

function EnterChild(event)
{
	if (IsEnter(event) == true)
		add_child();
}

function EnterNetwork(event)
{
	if (IsEnter(event) == true)
		add_network();
}
		
function wg_timer()
{
	dly += 1000;
	
	if(dly >= tm)
	{
		clearTimeout(wgTimer);
		dly = -1000;
		eval(action);
		action = '';
		idx = 0;
		return true;
	}
	
	wgTimer=setTimeout("wg_timer()", 1000);
}

function filllist(elt, script)
{
	word = $F(elt);
	$(elt + '_list').style.display = 'none';
	$(elt + '_list').innerHTML = '';
	
	if(word.length > 1)
	{
		au = new Ajax.Updater(elt + '_list', script + '.php',
		{
			onComplete: //$(elt + '_list').style.display = 'block', 
				function(valeur) {   
					var response = valeur.responseText || "error";
					if (response == "error")
						$(elt + '_list').style.display = 'none';
					else
						$(elt + '_list').style.display = 'block';
				},
			postBody: $H({'word': word, 'elt': elt}).toQueryString()
		});
	}	
}

function gestion_intelligente(elt, wgevent)
{
	/*
	KEY_BACKSPACE: 8,
	KEY_TAB:       9,
	KEY_RETURN:   13,
	KEY_ESC:      27,
	KEY_LEFT:     37,
	KEY_UP:       38,
	KEY_RIGHT:    39,
	KEY_DOWN:     40,
	KEY_DELETE:   46
	*/  
	
	var script = elt;
	
	if ((elt == "prefered_travel") || (elt == "dream_travel") || (elt == "expert_location"))
	{
		script = "location";
	}
	else if (elt == "hby")
	{
		script = "hobby_description";
	}
	else if (elt == "area_r")
	{
		script = "area";
	}
	else if (elt == "city_r")
	{
		script = "city";
	}
	else if (elt == "company")
	{
		script = "company_travel";
	}
	
	if ((wgevent.keyCode == 9) || (wgevent.keyCode == 27))
	{
		$(elt + '_list').style.display = 'none';
		$(elt + '_list').innerHTML = '';
		
		if (wgevent.keyCode == 9)
		{
			$(elt).focus();
		}
	}
	else
		filllist(elt,script);
}

function SI_Close()
{
	/*
	if(myId.substring(0, 6) == 'close_')
		theId = myId.substring(6, myId.length);
	*/
	
	var listeElement = new Array('country_list', 'area_list', 'city_list', 'hostel_club_list', 'hby_list', 'company_list', 'last_graduate_list', 'high_school_list', 'prefered_travel_list', 'dream_travel_list', 'expert_location_list', 'company_travel_list');
	
	for(var i=0; i < listeElement.length; i++)
	{
        	myElement = $(listeElement[i]);
		if(myElement)
			{myElement.innerHTML = ''; myElement.style.display = 'none';}
	}
	
	return true;
}

function setstylelist(elt,type)
{
	if(type == 1)
	{
		$(elt + '_list').style.backgroundColor = '#E77417';
		
		$(elt + '_list').style.width = '100px';
	}
	else if(type == 2)
	{
		$(elt + '_list').style.backgroundColor = '#FFFFFF';
		
		$(elt + '_list').style.width = '90px';
	}
	else if(type == 3)
	{
		$(elt + '_list').style.backgroundColor = '#EFE0D5';
		
		$(elt + '_list').style.width = '100px';
	}
}

function setNote(name,idx, len)
{
	for(i = 1; i <= len; i++)
	{
		if(i <= idx)
		{
			prefix = 'full';
		}
		else
		{
			prefix = 'empty';
		}

		document.images[name+ '_' + i].src = 'img/common/' + prefix + '_star.gif';
		
		if($(name) != null)
		{
			$(name).value = idx;
		}
	}
}

function add_child()
{
	birthday = document.forms['childs'].elements['birthday'].value;
	first_name = document.forms['childs'].elements['first_name'].value;
	nb_childs = parseInt($('nb_childs').innerHTML);
	
	if(birthday && nb_childs < 12)
	{
		au = new Ajax.Request('profile_family_infos.php',
		{
			postBody: $H({'c_birthday': birthday, 'c_first_name': first_name}).toQueryString(),
			onComplete: function (xhr)
						{
							json = xhr.responseText.evalJSON();
							
							if(json.birthday == null)
							{
								json.each
								(
									function(rep)
									{
										$(rep).style.display = 'block';
									}
								)
							}
							else
							{
								$('validate_birthday').style.display = 'none';
								$('validate_impday_birthday').style.display = 'none';
								$('validate_first_name').style.display = 'none';
								$('validate_unique_first_name').style.display = 'none';
								$('bday').value = 'JJ/MM/AAAA';
								$('fname').value = '';
								
								add_childdiv(json.id, json.birthday, escape(json.first_name));
								nb_childs++;
								$('nb_childs').innerHTML = nb_childs;
								
								if(nb_childs == 12)
								{
									$('can_add_child').style.display = 'none';
									$('cant_add_child').style.display = 'block';
								}
							}
						}
		});
	}
}

function rm_childdiv(id)
{
	nb_childs = parseInt($('nb_childs').innerHTML);
	
	au = new Ajax.Request('profile_family_infos.php',
	{
		postBody: $H({'idc': id, 'del': 1}).toQueryString(),
		onComplete: function(xhr)
					{
						rep = xhr.responseText;
						
						if(rep == '1')
						{		
							$('child_content').removeChild($('child_' + id));
							nb_childs--;
							$('nb_childs').innerHTML = nb_childs;

							$('can_add_child').style.display = 'block';
							$('cant_add_child').style.display = 'none';
						}
					}
	});
}

function rm_friend(id)
{
	au = new Ajax.Request('member.php',
	{
		postBody: $H({'id_user': id, 'action': 'del_friend'}).toQueryString(),
		onComplete: function (xhr)
					{
						rep = xhr.responseText;
						
						if(rep == '1')
						{
							$('friend_content').removeChild($('friend_' + id));
						}
					}
	});
}

function add_hobby()
{
	id_hobby = $('idh').options[$('idh').selectedIndex].value;
	hobby_description = document.forms['hobby'].elements['hobby_description'].value;
	
	level = $('lvl').options[$('lvl').selectedIndex].value;
	nb_hobbies = parseInt($('nb_hobbies').innerHTML);
	
	$('validate_hobby').style.display = 'none';
	$('validate_level').style.display = 'none';
	
	if(nb_hobbies < 8)
	{
		valid = true;
		
		if(!id_hobby)
		{
			$('validate_hobby').style.display = 'block';
			valid = false;
		}
		
		if(!level)
		{
			$('validate_level').style.display = 'block';
			valid = false;
		}
		
		if(valid == true)
		{
			au = new Ajax.Request('profile_hobbies.php',
			{
				postBody: $H({'id_hobby': id_hobby, 'hobby_description': hobby_description,'level': level}).toQueryString(),
				onComplete: function (xhr)
							{
								json = xhr.responseText.evalJSON();
								
								if(json.hobby_description == null)
								{
									json.each
									(
										function(rep)
										{
											$(rep).style.display = 'block';
										}
									)
								}
								else
								{
									$('validate_hobby_description').style.display = 'none';
									$('validate_unique_hobby_description').style.display = 'none';
									$('validate_hobby').style.display = 'none';
									$('validate_level').style.display = 'none';
									$('hby').value = '';
									
									add_hobbydiv(json.id, escape(json.hobby), escape(json.hobby_description), json.level);
									nb_hobbies++;
									$('nb_hobbies').innerHTML = nb_hobbies;
									$('idh').selectedIndex = 0;
									$('hby').value = '';
									$('lvl').selectedIndex = 0;
									
									if(nb_hobbies == 8)
									{
										$('can_add_hobby').style.display = 'none';
										$('cant_add_hobby').style.display = 'block';
									}
								}
							}
			});
		}
	}
}

function rm_hobbydiv(id)
{
		nb_hobbies = parseInt($('nb_hobbies').innerHTML);
		
		au = new Ajax.Request('profile_hobbies.php',
		{
			postBody: $H({'idh': id, 'del': 1}).toQueryString(),
			onComplete: function(xhr)
						{
							rep = xhr.responseText;
							
							if(rep == '1')
							{		
								$('hobby_content').removeChild($('hobby_' + id));
								nb_hobbies--;
								$('nb_hobbies').innerHTML = nb_hobbies;
								$('idh').selectedIndex = 0;
								$('hby').value = '';
								$('lvl').selectedIndex = 0;
								
								$('can_add_hobby').style.display = 'block';
								$('cant_add_hobby').style.display = 'none';
							}
						}
		});
}

function add_network()
{
	id_social_network = $('ids').options[$('ids').selectedIndex].value;
	pseudo = document.forms['social_network'].elements['social_network_pseudo'].value;

	nb_networks = parseInt($('nb_networks').innerHTML);
		
	if(pseudo && nb_networks < 5)
	{
		au = new Ajax.Request('profile_social_networks.php',
		{
			postBody: $H({'id_social_network': id_social_network, 'social_network_pseudo': pseudo}).toQueryString(),
			onComplete: function (xhr)
						{
							json = xhr.responseText.evalJSON();
							
							if(json.pseudo == null)
							{
								json.each
								(
									function(rep)
									{
										$(rep).style.display = 'block';
									}
								)
							}
							else
							{
								$('validate_social_network_pseudo').style.display = 'none';
								$('snp').value = '';
								
								add_networkdiv(json.id, json.social_network, escape(json.pseudo));
								nb_networks++;
								$('nb_networks').innerHTML = nb_networks;
								
								if(nb_networks == 5)
								{
									$('can_add_network').style.display = 'none';
									$('cant_add_network').style.display = 'block';
								}
							}
						}
		});
	}
}

function rm_networkdiv(id)
{
	nb_networks = parseInt($('nb_networks').innerHTML);
	
	au = new Ajax.Request('profile_social_networks.php',
	{
		postBody: $H({'ids': id, 'del': 1}).toQueryString(),
		onComplete: function(xhr)
					{
						rep = xhr.responseText;
						
						if(rep == '1')
						{		
							$('network_content').removeChild($('network_' + id));
							nb_networks--;
							$('nb_networks').innerHTML = nb_networks;
							
							$('can_add_network').style.display = 'block';
							$('cant_add_network').style.display = 'none';
						}
					}
	});
}

function del_cv_file(id)
{
	au = new Ajax.Request('profile_career_infos.php',
	{
		postBody: $H({'cv_id_file': id, 'del': 1}).toQueryString(),
		onComplete: function (xhr)
					{
						rep = xhr.responseText;
						
						if(rep == '1')
						{		
							$('cv_file').innerHTML = '<input class="coninp1" type="file" id="cv" name="cv" /><br /><b>Titre</b><br /><input type="text" id="ct" class="subscribe" name="cv_title" maxlength="26" /><br />';
						}
					}
	});
}

function text_area_return(id, max_text_char, wgevent)
{
	/*
	if(id != null)
	{		
		keyevent = window.event ? wgevent.keyCode : wgevent.which;
		
		if(keyevent == 13)
		{
			nb_text_char = 0;
		}
		
		if(keyevent == 8 && nb_text_char > 0)
		{
			nb_text_char --;
		}	
		
		if (nb_text_char > max_text_char)
		{
			$(id).value += '\n';
			nb_text_char = 0;
		}
		
		if(keyevent != 8 && keyevent != 13)
		{
			nb_text_char ++;
		}
	}
	*/
}

function resetCompteur()
{
	nb_text_char = 0;
}

function check_search_submit(save)
{
	/*
	if(!$F('country') && !$F('area') && !$F('city') && !$F('hostel_club'))
	{
		$('validate_submit').style.display = 'block';
		tm = 2000;
		action = "$('validate_submit').style.display = 'none';";
		//wg_timer();
		return false;
	}
	*/
	
	if(save==1)
	{
		$('sc').value = 2;
	}
	
	$('search').submit();
}

function PhotoLoupe()
{
	var monId = "bigimage_"+num_photo_affiche;
	if (browser == "Microsoft Internet Explorer" ) 
	{
		document.getElementById(monId).click(); 
	}
	else
	{
		var cb = document.getElementById(monId); 
		window.open(cb);
	}
}



function gestion_photo_next(nb_photo, nb_video)
{
	if (num_photo_affiche == (nb_photo+nb_video))
		num_photo_affiche = 1;
	else
		num_photo_affiche = num_photo_affiche+1;
	
	if (browser == "Microsoft Internet Explorer" ) 
	{
		document.getElementById(num_photo_affiche).click(); 
	}
	else
	{
		var evt = document.createEvent("MouseEvents");
		evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
		var cb = document.getElementById(num_photo_affiche); 
		cb.dispatchEvent(evt);
	}
}

function gestion_photo_prev(nb_photo, nb_video)
{
	if (num_photo_affiche == 1)
		num_photo_affiche = (nb_photo+nb_video);
	else
		num_photo_affiche = num_photo_affiche-1;
	
	if (browser == "Microsoft Internet Explorer" ) 
	{
		document.getElementById(num_photo_affiche).click(); 
	}
	else
	{
		var evt = document.createEvent("MouseEvents");
		evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
		var cb = document.getElementById(num_photo_affiche); 
		cb.dispatchEvent(evt);
	}
}

function loadPage(dest)
{
	if (browser == "Microsoft Internet Explorer" ) 
	{
		document.location.href=dest;
		return true;
	}
	else
	{
		location.href=dest;
		return true;
	}
	//window.open
}
