function $(aId)
{
	return document.getElementById(aId);
}
function UserProfile_load()
{
	UserProfile_CheckValues();
	UserProfile_cleanText(false);
	UserProfile_CheckLanguage();
}
function UserProfile_unload()
{
	UserProfile_cleanText(true);
}
function UserProfile_CheckLanguage()
{
	if( $('users_LanguageID').value == 2 )
	{
		UserProfile_SelTab(1);
	}
}
function ProfileViewer_load()
{
	if( $('profile_viewer_LanguageID').value == 2 )
	{
		UserProfile_SelTab(1);
	}
}
function UserAccount_SavePhoto()
{
	var IsValid = true;
	
	if( String($('UserProfile_UploadPhoto').value).length < 1)
	{
		alert( getLanguage("USER_PROFILE_SELECT_PHOTO") );
		IsValid = false;
	}
	
	if( IsValid )
	{
		UserProfile_unload();
		DoAction('save_photo');
	}
}
function UserAccount_Validate()
{
	if( $('users_Terms').checked )
	{  
		DoAction('signup_user_account'); 
	}
	else
	{
		alert(getLanguage("USER_ACCOUNT_TERMS_REQ"));
	}
}
function UserProfile_Validate()
{
	var IsValid = true;
	/*
	if()
	{
		row-users_profiles_ForWhatOptions
	}
	*/
	if( ffw_GetCheckBoxValues('ForWhatIDOptions_CheckBox').length  == 0 )
	{
		alert( getLanguage("USER_PROFILE_SELECT_ACTIVITY") );
		addCssClass($('row-users_profiles_ForWhatOptions'),"field-error");
		IsValid = false;
	}
	
	if( IsValid )
	{
		UserProfile_unload();
		DoAction('save_profile'); 
	}
}
function User_CountryChange(DropCountry, IdDropStates)
{
	if( DropCountry.value == 1 )
	{
		UserProfile_CountryOptions(true);
		$(IdDropStates).selectedIndex = 0;
		User_loadCities(DropCountry.value,$(IdDropStates).value);
	}
	else
	{
		UserProfile_CountryOptions(false);
		$(IdDropStates).selectedIndex = $(IdDropStates).length - 1;
		User_loadCities(DropCountry.value,null);
	}
}
function UserProfile_CheckValues()
{
	if( $('users_CountryID').value == 1 )
	{
		UserProfile_CountryOptions(true);
	}
	else
	{
		UserProfile_CountryOptions(false);
	}
}
function UserProfile_CountryOptions(Enable)
{
	if(Enable)
	{
		$('up-state-outside-us-users_profiles_StateID').style.display = "none";
		$('users_profiles_StateID').style.display = "inline";
		$('up-state-outside-us-users_profiles_ZipCode').style.display = "none";
		$('users_profiles_ZipCode').style.display = "inline";
		
		$('users_profiles_StateID').name = 'users_profiles_StateID';
		$('StateIDNonUS').name = 'nonState';
		$('StateIDNonUS').value = '';
	}
	else
	{
		$('up-state-outside-us-users_profiles_StateID').style.display = "inline";
		$('users_profiles_StateID').style.display = "none";
		$('up-state-outside-us-users_profiles_ZipCode').style.display = "inline";
		$('users_profiles_ZipCode').style.display = "none";
		$('users_profiles_ZipCode').value = '';
		
		$('users_profiles_StateID').name = '';
		$('StateIDNonUS').name = 'users_profiles_StateID';
		$('StateIDNonUS').value = 100;
	}
}
function User_StateChange(DropStates, DropCountryID)
{
	if( $(DropCountryID).value == 1 )
	{
		User_loadCities($(DropCountryID).value,DropStates.value);
	}
}
function User_loadCities(aCountryID, aStateID)
{
	var url="Application.php?Page=UserProfile&View=LoadCities&ContentType=TYPEHTML";
	url+="&CountryID=" + aCountryID;
	if( aStateID != null )
	{
		url+="&StateID=" + aStateID;
	}
	xmlHttp = ffwGetXmlHttpObject(User_loadCities_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	$('up-cities-holder').innerHTML = '<div class="form-element"><label>&nbsp;</label>'+getLanguage("USER_PROFILE_READING_CITIES")+'</div>';
}

function User_loadCities_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when try to load the cities: " + Answer.Msg);
		}
		$('up-cities-holder').innerHTML = Answer.Msg;
	}
}
var UP_TabName = 'tab-opt-';
var UP_ButtonName = 'tab-but-';
function UserProfile_SelTab(aTab)
{
	UserProfile_CleanContent();
	$(UP_TabName + aTab).style.display = "block";
	$(UP_ButtonName + aTab).className = "selected";
}
function UserProfile_CleanContent()
{
	var TotalTabs = 2;
	var i;
	for(i=0; i<TotalTabs; i++)
	{
		$(UP_TabName + i).style.display = "none";
		$(UP_ButtonName + i).className = "";
	}
}
function UserProfile_Detail_Focus(Target, IsFocus)
{
	var text = Target.getAttribute('help_text'); 
	if( IsFocus )
	{
		var user_value = Target.value.replace("\r",""); 
		var default_value = text.replace("\r","");
		if( user_value == default_value )
		{
			removeCssClass(Target,'FFW_TextAreaNotFocus');
			Target.value = '';
		}
	}
	else
	{
		if( Target.value == '' )
		{
			addCssClass(Target,'FFW_TextAreaNotFocus');
			Target.value = text;
		}
	}
}
function UserProfile_cleanText(Enable)
{
	UserProfile_Detail_Focus($('users_profiles_AboutYou_en'), Enable);
	UserProfile_Detail_Focus($('users_profiles_AboutYou_es'), Enable);
	UserProfile_Detail_Focus($('users_profiles_Interests_en'), Enable);
	UserProfile_Detail_Focus($('users_profiles_Interests_es'), Enable);
	UserProfile_Detail_Focus($('users_profiles_FirstDate_en'), Enable);
	UserProfile_Detail_Focus($('users_profiles_FirstDate_es'), Enable);
}
function UserProfile_removePhoto(PhotoID)
{
	var url="Application.php?Page=UserProfile&View=RemovePhoto&ContentType=TYPEHTML";
	url+="&PhotoID=" + PhotoID;
	xmlHttp = ffwGetXmlHttpObject(UserProfile_removePhoto_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	$('user-photo_'+PhotoID).innerHTML = '<span class="remove-text">removing the photo...</span>';
}

function UserProfile_removePhoto_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when try to remove the photo: " + Answer.Msg);
		}
		var PhotoID = Answer.PhotoID;
		var elem = $('user-photo_'+PhotoID);
		elem.parentNode.removeChild(elem);
		if( !isNaN(Number(Answer.NewDefaultPhoto)) && Number(Answer.NewDefaultPhoto) > 0)
		{
			UserProfile_setDefaultPhoto(Answer.NewDefaultPhoto);
		}
		else
		{
			if( Answer.NewDefaultPhoto == "NULL" )
			{
				$('up-defualt-photo-img').src = Answer.DefaultPhotoUrl;
				$('user-photo_Default').style.display = "block";
			}
		}
	}
}
function UserProfile_setDefaultPhoto(PhotoID)
{
	var url="Application.php?Page=UserProfile&View=SetDefaultPhoto&ContentType=TYPEHTML";
	url+="&PhotoID=" + PhotoID;
	xmlHttp = ffwGetXmlHttpObject(UserProfile_setDefaultPhoto_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	$('UserProfile_PhotoSelected_' + PhotoID).disabled = true;
	$('UserProfile_PhotoSelected_' + PhotoID).checked = true;
}

function UserProfile_setDefaultPhoto_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when try to set the default the photo: " + Answer.Msg);
		}
		$('UserProfile_PhotoSelected_' + Answer.PhotoID).disabled = false;
		$('up-defualt-photo-img').src = Answer.NewImage;
	}
}
function ProfileViewer_setProfilePhoto(url)
{
	$('up-defualt-photo-img').src = url;
}
function User_ConfirmEmail()
{
	var IsValid = true;
	if( $('user_confirm_email').value.length != 7 )
	{
		alert(getLanguage("USER_ACCOUNT_CONFIRM_MAIL"));
		IsValid = false;
	}
	if( IsValid )
	{
		DoAction('ConfirmEmail');
	} 
}
function User_ReSendPassword()
{
	DoAction('SendPassword');
}

function SearchEngine_Do()
{
	var WhoIam;
	var LookingFor;
	var AgeBegin;
	var AgeEnd;
	var WhoPrefers;
	var ProfilePicture;
	var country_id;
	var state_id;
	var city_id;
	var ErrorMsg = new Array();
	var IsValid;
	
	IsValid = true;
	

	WhoIam = ffw_GetRadioButtonValue('search_Gender');
	if( WhoIam == null )
	{
		ErrorMsg.push(getLanguage("SEARCH_SELECT_GENDER"));
		IsValid = false;
	}
	LookingFor = ffw_GetRadioButtonValue('search_LookingFor');
	if( LookingFor == null )
	{
		ErrorMsg.push(getLanguage("SEARCH_SELECT_LOOKING_FOR"));
		IsValid = false;
	}
	
	AgeBegin = $('search_AgeBegin').value;
	AgeEnd = $('search_AgeEnd').value;
	
	if(AgeEnd < AgeBegin)
	{
		ErrorMsg.push(getLanguage("SEARCH_NOT_VALID_AGE"));
		IsValid = false;
	}
	
	WhoPrefers = $('WhoPrefers').value;
	ProfilePicture = $('PhotoPrefers').value;
	for(i in ErrorMsg)
	{
		alert(ErrorMsg[i]);
	}
	
	var target_city;
	target_city = $('city'); 
	if( !target_city )
	{
		target_city = $('city-hidden');
	}
	
	country_id = $('country').value;
	if( Number(country_id) === 1 )
	{
		state_id = $('state').value;
		if(state_id > 0)
		{
			city_id = target_city.value;
		}
		else
		{
			city_id = 0;
		}
	}
	else
	{
		state_id = 0;
		if( Number(country_id) > 1 )
		{
			city_id = target_city.value;
		}
		else
		{
			city_id = 0;
		}
	}
		
	
	if( IsValid )
	{
		uri = 'find/apply-filter/g_'+WhoIam.toLowerCase()+'/look_'+LookingFor.toLowerCase();
		uri += '/age_'+AgeBegin+'_and_'+AgeEnd;
		uri += '/lan_'+WhoPrefers.toLowerCase()+'/photo_'+ProfilePicture.toLowerCase()+'/';
		uri += '/country_'+country_id+'/';
		uri += '/state_'+state_id+'/';
		uri += '/city_'+city_id+'/';

		getURI($('SiteBase').href+uri);
	}
}
function SearchEngine_changeGender()
{
	WhoIam = ffw_GetRadioButtonValue('search_Gender');
	if( WhoIam == "Man" )
	{
		LookingFor = ffw_SetRadioButtonValue('search_LookingFor','Woman');
	}
	if( WhoIam == "Woman" )
	{
		LookingFor = ffw_SetRadioButtonValue('search_LookingFor','Man');
	}
}
function Messages_getPage(targer, page)
{
	getURI($('SiteBase').href+'my-messages/p_'+page+'/');
}
function Messages_sendMessage()
{
	var IsValid = true;
	
	if( $('msg_Subject').value.length < 3 )
	{
		alert( getLanguage("MESSAGES_WRITE_SUBJECT") );
		IsValid = false;
	}
	if( $('msg_Message').value.length < 5 )
	{
		alert( getLanguage("MESSAGES_WRITE_MSG") );
		IsValid = false;
	}
	
	if(IsValid)
	{
		DoAction('SendMessage');
	}
}
function Messages_toggleSelection(target, ini, end)
{
	ffw_CheckCheckBoxValues(target.checked, "msg", ini, end);
}
function Messages_Remove()
{
	var IsValid = true;
	
	msg_selecteds = ffw_GetCheckBoxValues("msg");
	
	if(msg_selecteds.length == 0)
	{
		alert( getLanguage('MESSAGES_SELECT_MESSAGE') );
		IsValid = false;
	}
	if( IsValid )
	{
		DoAction('RemoveMessages');
	}
}
function SearchEngine_Page(target, page_id)
{
	SearchEngine_ChangePage(page_id);
}
function SearchEngine_ChangePage(page_id)
{
	if( String($('SearchID').value).length > 0 )
	{
		search_id = 'id_'+$('SearchID').value+'/';
	}
	else
	{
		search_id = '';
	}
	getURI($('SiteBase').href+ 'find/'+search_id+'page_'+page_id+'/');
}
function QuieroDate_changeLang(target)
{
	getURI($('SiteBase').href+'user/change-lang/' + target.value+'/');
}
function SearchEngine_CountryChange(DropCountry, IdDropStates)
{
	if( DropCountry.value == 1)
	{
		$('state').style.display = "block";
		$('state-help').style.display = "none";
		if( $(IdDropStates).value > 0 )
		{
			SearchEngine_loadCities(DropCountry.value, $(IdDropStates).value);
		}
		else
		{
			$('city-row').innerHTML = String(getLanguage("ANY").ucFirst());
		}
	}
	else
	{
		$('state').style.display = "none";
		$('state-help').style.display = "block";
		SearchEngine_loadCities(DropCountry.value,null);
	}
	$(IdDropStates).selectedIndex = 0;
}
function SearchEngine_StateChange(DropStates, DropCountryID)
{
	if( $(DropCountryID).value == 1 && DropStates.value > 0 )
	{
		SearchEngine_loadCities($(DropCountryID).value, DropStates.value);
	}
	else
	{
		$('city-row').innerHTML = String(getLanguage("ANY").ucFirst());
	}
}
function SearchEngine_loadCities(aCountryID, aStateID)
{
	var url="Application.php?Page=SearchEngine&View=LoadCities&ContentType=TYPEHTML";
	url+="&CountryID=" + aCountryID;
	if( aStateID != null )
	{
		url+="&StateID=" + aStateID;
	}
	xmlHttp = ffwGetXmlHttpObject(SearchEngine_loadCities_Listener);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	$('city-row').innerHTML = ''+getLanguage("USER_PROFILE_READING_CITIES")+'';
}

function SearchEngine_loadCities_Listener()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		var Answer = ffw_QueryStringToObject(xmlHttp.responseText);
		if(Answer.Result != "1")
		{
			alert("Error when try to load the cities: " + Answer.Msg);
		}
		$('city-row').innerHTML = Answer.Msg;
	}
}
function SearchEngine_SwitchAdvance()
{
	var cont = $('container-advanced-search'); 
	if( cont.style.display == "none" )
	{
		cont.style.display = "block";
	}
	else
	{
		cont.style.display = "none";
	}
}
function SearchEngine_FilterRegion(CountryID, StateID, CityID)
{
	var target_city;

	$('country').value = CountryID;
	if( StateID > 0 )
	{
		$('state').value = StateID;
	}
	else
	{
		$('state').value = 0;
	}
	
	target_city = $('city'); 
	
	if( !target_city )
	{
		target_city = $('city-hidden');
	}
	if( CityID > 0 )
	{
		target_city.value = CityID;
	}
	else
	{
		target_city.value = 0; 
	}
	SearchEngine_Do();
}
function ProfileViewer_FilterRegion(CountryID, StateID, CityID, WhoIam, LookingFor, AgeBegin, AgeEnd, WhoPrefers, ProfilePicture)
{
	IsValid = true;
	if( IsValid )
	{
		uri = 'find/apply-filter/g_'+WhoIam+'/look_'+LookingFor;
		uri += '/age_'+AgeBegin+'_and_'+AgeEnd;
		uri += '/lan_'+WhoPrefers+'/photo_'+ProfilePicture+'/';
		uri += '/country_'+CountryID+'/';
		uri += '/state_'+StateID+'/';
		uri += '/city_'+CityID+'/';

		getURI($('SiteBase').href+uri);
	}
}