// JavaScript Document


function saveUserInfoAndSubmit(){
	var el=document.loginForm;	
	
	if(el.rememberMe.checked==false){ 
		DeleteCookie("username");
		DeleteCookie("password");
	}
	if(el.rememberMe.checked&&el.username.value){//save user info to cookie
		expiryDate = new Date();
		expiryDate.setTime(expiryDate.getTime() + 1000 * 60 * 60 * 24 * 365 * 5);//set expiry date for 5 years
		SetCookie("username",el.username.value,expiryDate);
		SetCookie("password",el.password.value,expiryDate);
	}
	
	el.submit();
}


function saveUserInfo() {
	var el=document.loginForm;
	if(el.rememberMe.checked==false){ 
		DeleteCookie("email");
		DeleteCookie("password");
	}
	if(el.rememberMe.checked&&el.email.value){//save user info to cookie
		expiryDate = new Date();
		expiryDate.setTime(expiryDate.getTime() + 1000 * 60 * 60 * 24 * 365 * 5);//set expiry date for 5 years
		SetCookie("email",el.email.value,expiryDate);
		SetCookie("password",el.password.value,expiryDate);
	}
}


function populateSavedInfo(){
	var el=document.loginForm;
	
	testVar = GetCookie("email");
	if(testVar != null){
		el.email.value = GetCookie("email");
		el.password.value = GetCookie("password");
		el.rememberMe.checked = true;
	}
}


function ClearOptions(OptionList) {

   // Always clear an option list from the last entry to the first
   for (x = OptionList.length; x >= 0; x--) {
      OptionList[x] = null;
   }
}

function AddToOptionList(OptionList, OptionValue, OptionText) {
   // Add option to the bottom of the list
   OptionList[OptionList.length] = new Option(OptionText, OptionValue);
}

function populateCities(preset){
	//alert(preset);
	
	ClearOptions(document.searchForm.id_cities);
	
	AddToOptionList(document.searchForm.id_cities, "", "All Cities");
	
	var cityVals = new Array();
	for(x in countries[document.searchForm.country.value]){
		//alert(countries[document.searchForm.country.value][x]);
		cityVals = countries[document.searchForm.country.value][x].split('|');
		AddToOptionList(document.searchForm.id_cities, cityVals[0], cityVals[1]);
	}
	
	document.searchForm.id_cities.disabled = false;
	
	if(preset){
		for(var idx=0;idx<document.searchForm.id_cities.options.length;idx++) {
			if(document.searchForm.id_cities.options[idx].value == preset) document.searchForm.id_cities.options[idx].selected = true;
		}
		
		$("#cityFieldContainer").show();
	}

}


function populateStates(formName){
	//alert(preset);
	
	formName = typeof(formName) != 'undefined' ? formName : "regForm";
	//alert(formName);
	ClearOptions(document.forms[formName].new_state);
	
	AddToOptionList(document.forms[formName].new_state, "", "Select -->");
	
	var stateVals = new Array();
	//alert(document.forms[formName].new_country.value);
	for(x in states[document.forms[formName].new_country.value]){
		//alert(countries[document.searchForm.country.value][x]);
		stateVals = states[document.forms[formName].new_country.value][x].split('|');
		AddToOptionList(document.forms[formName].new_state, stateVals[0], stateVals[1]);
	}
	
	document.forms[formName].new_state.disabled = false;
}



function populateCities2(){
	//alert(preset);
	
	ClearOptions(document.changeHomeCityForm.id_cities);
	
	AddToOptionList(document.changeHomeCityForm.id_cities, "", "All Cities");
	
	var cityVals = new Array();
	for(x in countries[document.changeHomeCityForm.country.value]){
		//alert(countries[document.searchForm.country.value][x]);
		cityVals = countries[document.changeHomeCityForm.country.value][x].split('|');
		AddToOptionList(document.changeHomeCityForm.id_cities, cityVals[0], cityVals[1]);
	}
	
	document.changeHomeCityForm.id_cities.disabled = false;

}



var doRollOver = function(id1,id2){ 

	var upSrc = $("#"+id1).attr("src");
	var rlSrc = $("#"+id2).attr("src");
	
	var rl = function(){ 
		($("#"+id1)).attr("src", rlSrc);
	} 

	var up = function(){ 
		$("#"+id1).attr("src", upSrc);
	} 

	$("#"+id1).mouseover( function(){ rl(); }); 

	$("#"+id1).mouseout( function(){ up(); }); 

}; 


$(document).ready(function(){
						   
	/*doRollOver("button1", "button1on");
	doRollOver("button2", "button2on");
	doRollOver("button3", "button3on");
	doRollOver("button4", "button4on");
	doRollOver("loginnow", "loginnow_on");
	doRollOver("searchnow", "searchnow_on");
	doRollOver("remindme", "remindme_on");
	doRollOver("resendverify", "resendverify_on");
	doRollOver("savereview", "savereview_on");
	doRollOver("readreviews", "readreviews_on");
	doRollOver("addtofaves", "addtofaves_on");
	doRollOver("removefavourite", "removefavourite_on");
	doRollOver("submitreview", "submitreview_on");
	doRollOver("backtoprofile", "backtoprofile_on");
	
	$("#mainMenu > li").each(function(){
									  
			var upSrc = $("#"+this.id).html();
			var rlSrc = $("#"+this.id+"on").html();
			
			
			$(this)
				.mouseover(function(){
					$(this).html(rlSrc);
				})
				.mouseout(function(){
					$(this).html(upSrc);
				});
				
		});
	*/
	
	
	
	
	
	$("#forgot_pw").click(function() {
			$.ajax({
				type: "POST",
				url: "resendPassword.php",
				data: "email="+$("#loginemail").val(),
				success: function(msg){
					//alert(msg);
					switch(msg){
						case "success":
							$("#passReminderContainer").html("<b>Reminder Sent!</b><br><br>We have emailed your login to your email address!<br>");
							$("#passReminderContainer").slideDown("slow");
							setTimeout(function(){$("#passReminderContainer").slideUp(500);}, 3000);
							break;
						case "error":
							$("#passReminderContainer").html("<b style=\"color: red\">Sorry, we couldn't locate that email address. Please try again.<br>");
							$("#passReminderContainer").slideDown("slow");
							setTimeout(function(){$("#passReminderContainer").slideUp(500);}, 3000);
							break;
					}
					
				}
			});
			return false;
		});
		
	$("#searchForm > input[name='searchBy']").click(function(){
		//alert($("#searchForm > input[name='searchBy']:checked").val());
		switch($("#searchForm > input[name='searchBy']:checked").val()){
			case "location":
				$("#nameFormContainer").hide();
				$("#countryFormContainer").show();
				break;
			case "name":
				$("#countryFormContainer").hide();
				$("#nameFormContainer").show();
				break;
		}
	});
	
	
	$("#countryField").change(function(){
		populateCities();
		$("#cityFieldContainer").slideDown(500);
	});
	
	$("#countryField2").change(function(){
		populateCities2();
	});
	
	/*$("#loginForm").submit(function(){
									
		$("#loginError").hide();
		
		$.ajax({
				type: "POST",
				url: $("#resendVerificationForm").attr("action"),
				data: "doThis=checkUser&username="+$("#loginForm > #username").val()+"&password="+$("#loginForm > #password").val(),
				success: function(msg){
					
					//alert(msg);
					
					switch(msg){
						case "bad_login":
							$("#loginError").show();
							return false;
							break;
						case "not_verified":
							$("#resendVerificationContainer").slideDown(500);
							//alert(msg);
							return false;
							break;
						case "not_active":
							$("#notActiveContainer").slideDown(500);
							//alert(msg);
							return false;
							break;
						case "suspended":
							$("#suspendedContainer").slideDown(500);
							//alert(msg);
							return false;
							break;
						case "deleted":
							$("#deletedContainer").slideDown(500);
							return false;
							break;
						case "verified":
							saveUserInfoAndSubmit();
							//alert(msg);
							return false;
					}
					//alert(msg);
				}
			});
		//alert($("#loginForm > #username").val()+":"+$("#loginForm > #password").val());
		return false;

	}); */
	
	$("#resendVerificationForm").submit(function(){
		$.ajax({
				type: "POST",
				url: $("#resendVerificationForm").attr("action"),
				data: "doThis=resendVerification&username="+$("#loginForm > #username").val()+"&password="+$("#loginForm > #password").val(),
				success: function(msg){
					switch(msg){
						case "success":
							$("#resendVerificationContainer").html("<b>Verification Sent!</b><br><br>Please check your email to complete your signup!<br><br>");
							setTimeout(function(){$("#resendVerificationContainer").slideUp(500);}, 2000);		
							break;
						case "error":
							$("#resendVerificationContainer").html("<b>Error!</b>");
							break;
						default:
							alert(msg);
					}
				}
			});
		return false;
	});
	
	
	$("#reactivateForm").submit(function(){
		$.ajax({
				type: "POST",
				url: $("#reactivateForm").attr("action"),
				data: "doThis=resendVerification&username="+$("#loginForm > #username").val()+"&password="+$("#loginForm > #password").val(),
				success: function(msg){
					switch(msg){
						case "success":
							$("#deletedContainer").html("<b>Verification Sent!</b><br><br>Please check your email to reactivate your account.<br><br>");
							setTimeout(function(){$("#deletedContainer").slideUp(500);}, 2000);		
							break;
						case "error":
							$("#deletedContainer").html("<b>Error!</b>");
							break;
						default:
							alert(msg);
					}
				}
			});
		return false;
	});
	
	
	$("#searchForm").submit(function(){
		
			/*if (document.searchForm.country.value == "") {
				alert("Please Select a Country");
				return false;
			} else { return true; } */
			return true;
				
	});
	
	$(".resultContainer")
		.mouseover(function(){
			$(this).addClass("resultContainerOver");
		})
		.mouseout(function(){
			$(this).removeClass("resultContainerOver");
		})
		.click(function(){
			window.open("profile.php?ad="+this.id, "rhwin"+this.id, "width=950, height=900, toolbar = 0, location = 0, directories = 0, status = 0, menubar = 0, scrollbars = 1");
		});
		
	
	$("#getComingSoon").click(function() {
		$("#header_comingSoon").removeClass("hidden");
		$("#obj_now_visiting").hide();
		$("#header_nowVisiting").addClass("hidden");
		$("#obj_coming_soon").show();
		
	});
	
	$("#getNowVisiting").click(function() {
		$("#obj_coming_soon").hide();
		$("#header_comingSoon").addClass("hidden");
		$("#obj_now_visiting").show();
		$("#header_nowVisiting").removeClass("hidden");
	});

	

});
