/**
* Fills up <select> with new options. (ajax)
* On change in country or state fills up schools or states
*
* @param name string name of select (onchange)
*
* @return bool
*/
function changeSel(name) {

	var country = document.getElementById("country_id").value;
	var state = document.getElementById("state_id").value;

	var append_other = function() {
		$('#school_id').append('<option value="0">Other...</option>');
		if (window.post_data) {
			if (name == 'country_id') {
				if (post_data.state_id) setTimeout("$('#state_id').val(post_data.state_id).change()", 200);
			} else {
				if (post_data.school_id) $('#school_id').val(post_data.school_id).change();
				if (post_data.school_other) $('#school_other').val(post_data.school_other);
			}
		}
	}

	if (name == "country_id") {
		$("#state_id").load("input_select.php", { 'country': country, 'state': 0 , 'name': 'state_id' });
		$("#school_id").load("input_select.php", { 'country': country, 'state': state, 'name': 'school_id' }, append_other);
	}
	if (name == "state_id") {
		$("#school_id").load("input_select.php", { 'country': country, 'state': state, 'name': 'school_id' }, append_other);
	}
}

function markInvalid(id, msg) {

	if ($(id).get(0).tagName.match(/select/i)) {
		$(id)
			.parent()
			.css({ border: '1px solid red' })
			.find('select')
			.focus(function() {
				$(this)
					.parent()
					.css({ border: '1px solid ' + window.selectBorder })
			});
	} else {
		$(id)
			.css({ border: '1px solid red' })
			.focus(function() {
				$(this)
					.css({ border: '1px solid #ccc' });
			});
	}
}

function validate() {

	var valid = true;

	if (!$('#first_name').val()) {
		markInvalid('#first_name', '< first name is required');
		valid = false;
	}

	if (!$('#last_name').val()) {
		markInvalid('#last_name', '< Last name is required');
		valid = false;
	}

	if (!$('#country_id').val()) {
		markInvalid('#country_id', '< country is required');
		valid = false;
	}

	if (!$('#state_id').val()) {
		markInvalid('#state_id', '< state is required');
		valid = false;
	}

	if (!$('#city').val()) {
		markInvalid('#city', '< city is required');
		valid = false;
	}

	if (!parseInt($('#school_id').val()) && !$('#school_other').val()) {
		if ($('#school_id').val() == '') {
			markInvalid('#school_id', '< school is required');
		} else {
			markInvalid('#school_other', '< school is required');
		}
		valid = false;
	}

	if ($('#school_id').val() === '') {
		markInvalid('#school_id', '< school is required');
		valid = false;
	}

	if (!$('#email').val().match(/^[\w\-.~]+@[\w\-.~]+\.[\w\-.~]{2,6}$/)) {
		markInvalid('#email', '< email is invalid');
		valid = false;
	}
	
	if (!$('#password').val()) {
		markInvalid('#password', '< password is required');
		valid = false;
	}

	if (!$('#repeat_passw').val() || $('#repeat_passw').val() != $('#password').val()) {
		markInvalid('#repeat_passw', '< repeat password');
		valid = false;
	}

	if (!$('#recaptcha_response_field').val()) {
		markInvalid('#recaptcha_response_field', '< fill captcha');
		valid = false;
	}

	if (!valid) {
		var message = 'You missed one or more fields. They have been highlighted below';
		$('div.div_mess')
			.css({ color: 'red' })
			.html(message)
			.show();
	}

	return valid;
}

$(document).ready(function() {

	$('form input[type!=submit]')
		.css({
			width: '266px',
			border: '1px solid #ccc'
		});
	
	$('form select')
		.css({
			width: '266px',
			border: '0'
		})
		.wrap('<div class="outer"></div>');

	window.selectBorder = $.browser.msie
		? 'white'
		: '#ccc';

	$('div.outer').css({
		width: '266px',
		border: '1px solid ' + window.selectBorder
	});

	$('div.outer1').each(function() {
		var select = $(this).html();
		$(this).parent().append(select);
	})
	.remove();

	$('#form_register').submit(validate);

	$('#school_other').parent().parent().css({ background: '#ffc' }).hide();
	$('#school_id').change(function() {
			if (parseInt($(this).val()) === 0) {
				$('#school_other').val('').parent().parent().css({ opacity: 0 }).show()
					.animate({ opacity: 1 }, 500);
			} else {
				$('#school_other').parent().parent().hide();
			}
		});


	if (window.post_data) {
		$('#first_name').val(post_data.first_name);
		$('#last_name').val(post_data.last_name);
		$('#country_id').val(post_data.country_id).change();
		$('#state_id').val(post_data.state_id).change();
		$('#city').val(post_data.city);
		$('#email').val(post_data.email);
		$('#school_other').val(post_data.school_other);
	}

});
