/*
** Author: Janne Pohjolainen
** Date: 15.07.2009
*/
jQuery.noConflict();
jQuery(document).ready( function() {
	jQuery("form[name=complete_registration]").submit(function() {
		errors = 0;

		/* not working.. don't know why */
		/*
		jQuery(".required > input:radio").each(function() {
		});
		*/


		/* This will check only input text fields */
		jQuery(".required > input:text").each(function() {
			if( jQuery(this).val() == '' ) {
				jQuery(this).focus();
				alert( jQuery(this).parent().find("label").text() + " ei saa olla tyhjä");
				errors++;
				return false;
			}

			//alert( jQuery(this).val().length );

			if( jQuery(this).attr('name') == 'mobile' && jQuery(this).val().length < 5 ) {
				jQuery(this).focus();
				alert( jQuery(this).parent().find("label").text() + "numero on liian lyhyt");
				errors++;
				return false;
			}
		});

		/* if no error was found on input textfields, check select boxes */
		if( !errors ) {
			jQuery(".required > select").each(function() {
				if( jQuery(this).val() == '' ) {
					jQuery(this).focus();
					alert( jQuery(this).parent().find("label").text() + " on pakko valita" );
					errors++;
					return false;
				}
			});
		}

		/* if no errors was found previously, check password values if given or if required */
		if( !errors ) {
			pass1 = jQuery("#password");
			pass2 = jQuery("#password_confirm");

			if( pass1.parent().hasClass('required') || (pass1.val() != '' || pass2.val() != '') ) {
				if( pass1.val() != pass2.val() ) {
					pass1.focus();
					alert("Salasanat eivät täsmää");
					errors++;
				} else if( pass1.val().length < 6 ) {
					pass1.focus();
					alert("Salasana on liian lyhyt.");
					errors++;
				} else if( pass1.parent().hasClass('required') && (pass1.val() == '' || pass2.val() == '') ) {
					pass1.focus();
					alert("Salasana on pakollinen");
					errors++;
				}
			}
		}

		if( errors ) {
			return false;
		}

		return true;
	});
});

