String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
function gebi(id) { return document.getElementById(id); }

function get_cities()
{
	var zipbox = gebi('zip');
	if (zipbox.value.trim().length < 5)
	{
		gebi('city_sel').options.length = 0;
		add_option('-- Enter Zip --', '', gebi('city_sel'));
	}
	else
	{
		gebi('city_sel').options.length = 0;
		add_option('-- Searching... --', '', gebi('city_sel'));
		var params = {
			zip: zipbox.value.trim()
		};
		$.post('ajax/get_cities.php', params, get_cities_callback);
	}
}

function get_cities_callback(data)
{
	var response = data.trim();
	
	gebi('city_sel').options.length = 0;
	if (response != '0')
	{
		var cities = response.split(';');
		for (var i = 0; i < cities.length; i++)
		{
			var city_pair = cities[i].split(':');
			add_option(city_pair[1], city_pair[0], gebi('city_sel'));
		}
	}
	else
	{
		add_option('-- No Cities in ' + gebi('zip').value.trim() + ' --', '', gebi('city_sel'));
	}
}

function add_option(text, value, control)
{
	var newOption = new Option();
	newOption.text = text.trim();
	newOption.value = value.trim();
	control.options.add(newOption);
}

function do_submit()
{
	var zip = gebi('zipcode').value.trim();
	if (zip == '' || isNaN(zip) || zip.length < 5 || zip.length > 8)
	{
		alert("Please enter a valid Zip code.");
		gebi('zipcode').select();
		gebi('zipcode').focus();
		return;
	}
	
	gebi('theform').submit();
}

function reload_captcha()
{
	$.get('ajax/get_captcha.php?_=' + Math.floor(Math.random()*9999999), reload_captcha_callback);
}

function reload_captcha_callback(data)
{
	if (data.trim() == '0') { return; }
	var response = data.trim().split('::', 2);
	
	gebi('captcha-key').value = response[0];
	gebi('captcha').src = 'data:image/png;base64,' + response[1];
	gebi('captcha').style.display = '';
}

function do_signup2()
{
	// Error checking
	if (gebi('screenname').value.trim() == '') { alert("Please enter a Screenname."); return; }
	if (gebi('screenname').value.trim().length < 4) { alert("Your Screenname must be between 4 and 16 characters."); return; }
	if (gebi('birthday_month').selectedIndex == 0) { alert("Please your Month of birth."); return; }
	if (gebi('q-birthday-day').selectedIndex == 0) { alert("Please your Day of birth."); return; }
	if (gebi('birthday_year').selectedIndex == 0) { alert("Please you Year of birth."); return; }
	if (gebi('zipcode').value.trim() == '') { alert("Please enter your Zipcode."); return; }
	if (gebi('zipcode').value.trim().length != 5) { alert("Your Zipcode must be 5 characters."); return; }
	if (gebi('email').value.trim() == '') { alert("Please enter your E-mail address."); return; }
	if (gebi('email_confirm').value.trim() == '') { alert("Please enter the E-mail address Confirmation."); return; }
	if (gebi('email').value.trim() != gebi('email_confirm').value.trim()) { alert("E-mails do not match. Please type them again."); return; }
	if (gebi('password').value.trim() == '') { alert("Please enter your desired Password."); return; }
	if (gebi('password').value.trim().length < 4) { alert("Passwords must be at least 4 characters."); return; }
	if (gebi('password_confirm').value.trim() == '') { alert("Please enter the Password Confirmation."); return; }
	if (gebi('password').value.trim() != gebi('password_confirm').value.trim()) { alert("Passwords do not match. Please type them again."); return; }
	if (gebi('captcha-answer').value.trim() == '') { alert("Please type the letters you see into the box below."); return; }
	
	gebi('signup-form').submit();
}

function has_check(input)
{
	var elements = document.getElementsByName(input);
	if (elements != undefined)
	{
		for (var i = 0; i < elements.length; i++)
		{
			if (elements[i].checked) { return true; }
		}
	}
	return false;
}

function is_true(input)
{
	var elements = document.getElementsByName(input);
	if (elements != undefined)
	{
		for (var i = 0; i < elements.length; i++)
		{
			if (elements[i].checked && elements[i].value == '1') { return true; }
		}
	}
	return false;
}

function do_signup3()
{
	// Check for unanswered questions
	var questions = new Array();
	if (!has_check('sports_play')) { questions.push('- Do you like to play sports for fun or recreation?'); }
	if (is_true('sports_play') && !has_check('sports_play_which[]')) { questions.push('- Choose which sports you like to play.'); }
	if (!has_check('exercise')) { questions.push('- Do you like to exercise?'); }
	if (is_true('exercise') && !has_check('exercise_which[]')) { questions.push('- Choose which exercises you like to do.'); }
	if (!has_check('outdoor')) { questions.push('- Do you like the outdoors?'); }
	if (is_true('outdoor') && !has_check('outdoor_which[]')) { questions.push('- Choose which outdoor activities you like to do.'); }
	if (!has_check('artistic')) { questions.push('- Are you an artistic person?'); }
	if (is_true('artistic') && !has_check('artistic_which[]')) { questions.push('- Choose which types of art you like to do.'); }
	if (!has_check('religion')) { questions.push('- Are you a religious person?'); }
	if (is_true('religion') && !has_check('religion_date')) { questions.push('- Would you consider dating someone of a different religion?'); }
	if (!has_check('sports_fan')) { questions.push('- Do you watch sports on TV or go to sporting events?'); }
	if (is_true('sports_fan') && !has_check('sports_fan_which[]')) { questions.push('- Choose which sports you like to watch.'); }
	if (!has_check('pet')) { questions.push('- Do you have a pet?'); }
	if (is_true('pet') && !has_check('pet_which[]')) { questions.push('- Choose which types of pets you have.'); }
	if (!has_check('race_date')) { questions.push('- Would you consider dating someone from a different race?'); }
	if (!has_check('country_date')) { questions.push('- Would you consider dating someone from a different country?'); }
	if (!has_check('smoke')) { questions.push('- Do you smoke?'); }
	if (!is_true('smoke') && !has_check('smoke_date')) { questions.push('- Would you consider dating someone who smokes?'); }
	if (!has_check('drink')) { questions.push('- Do you drink socially?'); }
	if (!has_check('diet')) { questions.push('- Do you have a strict diet?'); }
	if (!has_check('relationship_serious')) { questions.push('- Have you ever had a serious relationship?'); }
	if (is_true('relationship_serious') && !has_check('married')) { questions.push('- Have you even been married?'); }
	if (!has_check('children')) { questions.push('- Do you have any children?'); }
	if (is_true('children') && !has_check('children_want')) { questions.push('- Would you consider having another child?'); }
	if (!has_check('children_date')) { questions.push('- Would you date someone who has children?'); }
	if (!has_check('high_school')) { questions.push('- Did you attend high school?'); }
	if (!has_check('college')) { questions.push('- Did you attend college?'); }
	if (!has_check('personal_description_wanted')) { questions.push('- Would you like to write something about yourself and who you\'re looking for?'); }
	if (is_true('personal_description_wanted') && gebi('personal_description').value.trim() == '') { questions.push('- Tell us more about yourself and who you\'d like to date.'); }

	if (questions.length > 0)
	{
		alert('The following questions must be answered before you can continue:\n\n' + questions.join("\n"));
		return;
	}
	gebi('theform').submit();
}

function show_errors(val)
{
	gebi('core-popup').style.zIndex = (val) ? 1 : 100;
	gebi('err_div').style.display = (val) ? '' : 'none';
}

function page_1_gender_change()
{
	var sex = gebi('sex').options[gebi('sex').selectedIndex].value.trim();
	var sex_seeking = gebi('sex_seeking').options[gebi('sex_seeking').selectedIndex].value.trim();
	
	if (sex == 'M')
	{
		gebi('g').value = (sex_seeking == 'M') ? 3 : 1
	}
	else
	{
		gebi('g').value = (sex_seeking == 'M') ? 0 : 2
	}

	gebi('mapframe').src = '/map/map.php?g=' + gebi('g').value + '&z=' + gebi('zipcode').value.trim();
}

function clicked_user(uid)
{
	top.gebi('u').value = uid;
	top.do_submit();
}

function zip_change()
{
	if (gebi('zipcode').value.trim().length >= 5)
	{
		page_1_gender_change();
	}
}