jQuery(document).ready(function() {


function validateRegistration()
{
	
	if (typeof(compact_reg_form) != 'undefined' && compact_reg_form)
	{
		return validateCompactRegistration();
	}
	
	var noError = true;
	
	
	if (jQuery("#page").val() == '1') {
		//Reset all error messages
		jQuery("#FirstNameError").html("");
		jQuery("#LastNameError").html("");
		jQuery("#UserNameError").html("");
		jQuery("#PasswordError").html("");
		jQuery("#BirthDateError").html("");
		
		if (jQuery("#name").val() == "") {
			jQuery("#FirstNameError").html("Please enter your first name!").show();

			noError = false;
		}
		else if (jQuery("#name").val().length < 2) {
			jQuery("#FirstNameError").html("First name must be 2 to 20 characters in length!").show();
			
			noError = false;
		}

		else {
			var arrStr = jQuery("#name").val().match(/[\d\.]+/g);
			if (arrStr!= null) {
				if (arrStr.length>0) {
					// showError('FirstNameError',"Only letters allowed in this field!",jQuery("#name"),false);   //showError(divid,msg,field,alertMsg=false)
					
					jQuery("#FirstNameError").html("Only letters allowed in this field!").show();
					
					noError = false;
					
				}
			}
		}
		
		
		
		if (jQuery("#surname").val() == "") {
			jQuery("#LastNameError").html("Please enter your surname!").show();

			noError = false;
		}
		else if (jQuery("#surname").val().length < 2) {
			jQuery("#LastNameError").html("Surname must be 1 to 20 characters in length!").show();
			
			noError = false;
		}

		else {
			var arrStr = jQuery("#surname").val().match(/[\d\.]+/g);
			if (arrStr!= null) {
				if (arrStr.length>0) {
					// showError('FirstNameError',"Only letters allowed in this field!",jQuery("#name"),false);   //showError(divid,msg,field,alertMsg=false)
					
					jQuery("#LastNameError").html("Only letters allowed in this field!").show();
					
					noError = false;
					
				}
			}
		}
		
		

		
		
		if (jQuery("#useremail").val() == "") {
			jQuery("#UserNameError").html("Please enter your email address!").show();
			
			noError = false;
		}

		else if (isEmail(jQuery("#useremail").val())== false) {
			jQuery("#UserNameError").html("Please enter a valid email address!").show();
			
			noError = false;
		}
		
		var userPassword = jQuery("#user_password").val();
		
		if (jQuery("#user_password").val() == "") {			
			jQuery("#PasswordError").html("Please enter a password!").show();
			
			noError = false;
		}
		else if (parseInt(userPassword.length)<5) {
			jQuery("#PasswordError").html("Password must be 5 to 20 characters in length!").show();
			
			noError = false;
		}
		
		
		if (checkSingleDropdown(jQuery("#user_birth_month"))) {			
			jQuery("#BirthDateError").html("Please enter your date of birth!").show();
			
			noError = false;
		}

		if (checkSingleDropdown(jQuery("#user_birth_day"))) {
			jQuery("#BirthDateError").html("Please enter your date of birth!").show();

			noError = false;
		}
		
		if (checkSingleDropdown(jQuery("#user_birth_year"))) {
			jQuery("#BirthDateError").html("Please enter your date of birth!").show();
			
			noError = false;
		}
	}
	
	if (jQuery("#page").val() == '2') {

          noError = true;
		
		// Reset error messages
		jQuery("#GenderError").html("");
		jQuery("#PostLiveError").html("");
		jQuery("#DefaultCityError").html("");
		jQuery("#MobileError").html("");
		jQuery("#PostWorkError").html("");
		jQuery("#AgreeError").html("");
		jQuery("#CountryLiveError").html("");
		//jQuery("#CityLiveError").html("");
		jQuery("#CountryWorkError").html("");
		//jQuery("#CityWorkError").html("");
		
		
		if (jQuery("#mobile").val().length > 0) {
			
			if(!checkInternationalPhone(jQuery("#mobile").val())) {
				jQuery("#MobileError").html("Please enter valid mobile number!").show();

				noError = false;
			}
		}

		if (checkSingleDropdown(jQuery("#default_city"))) {
			jQuery("#DefaultCityError").html("Please select the default CitySocialising city!").show();
			
			noError = false;
		}

		if (checkOptButton(document.frmRegistration.gender)) {			
			jQuery("#GenderError").html("Please select your gender!").show();
			
			noError = false;
			
		}
		
		if (checkSingleDropdown(jQuery("#postcode_live")) && checkCheckbox(jQuery("#notliveinuk"))) {
			jQuery("#PostLiveError").html("Please enter your Live-in postcode!").show();
			
			noError = false;
		}

		else if (!jQuery("#notliveinuk").is(":checked")) {
			if (checkSingleDropdown(jQuery("#otherLiveCountry"))) {
				jQuery("#CountryLiveError").html("Please choose the country you live in other than UK!").show();
				
				noError = false;
			}
		}
		
		if (!jQuery("#notworkinuk").is(":checked")) {
			if (checkSingleDropdown(jQuery("#otherWorkCountry"))) {
				jQuery("#CountryWorkError").html("Please choose the country you work in other than UK!").show();
				
				noError = false;
			}
		}
		
		var friendEmail = jQuery("#friend_email").val();
		if (friendEmail!="") {
			if (isEmail(friendEmail)== false) {
				jQuery("#friendEmailError").html("Please enter a valid email address!").show();
				
				noError = false;
			}
		}
		
		if(!jQuery("#Iagree").is(':checked')) {			
			jQuery("#AgreeError").html("You must agree to CitySocialising's Terms of Use and Special Code of Conduct before registering.").show();
			
			noError = false;
		}
	}

	if (!noError) {
		jQuery("#mainError").show();
    }

    return noError;
}


function validateCompactRegistration() {
    //Reset all error messages
    jQuery("#FirstNameError").html("");
    jQuery("#LastNameError").html("");
    jQuery("#UserNameError").html("");
    jQuery("#PasswordError").html("");
    jQuery("#BirthDateError").html("");
    jQuery("#GenderError").html("");
    jQuery("#AgreeError").html("");

    var noError = true;
    if (jQuery("#name").val() == "") {
		jQuery("#FirstNameError").html("Please enter your first name!").show();
        noError = false;
    }

    else if (parseInt(jQuery("#name").val().length)<2) {
		jQuery("#FirstNameError").html("First name must be 2 to 20 characters in length!").show();

        noError = false;
    }
    else {
        var vName = jQuery("#name").val()
        var arrStr = vName.match(/[\d\.]+/g);
 		if (arrStr!= null) {
			if (arrStr.length>0) {
				jQuery("#FirstNameError").html("Only letters allowed in this field!").show();

                noError = false;
			}
		}
	}

    if (jQuery("#surname").val() == "") {
		jQuery("#LastNameError").html("Please enter your surname!").show();

        noError = false;
    }

	else if (parseInt(jQuery("#surname").val().length)<2) {
		jQuery("#LastNameError").html("Surname must be 1 to 20 characters in length!").show();

        noError = false;
    } else {
        var arrStr = jQuery("#surname").val().match(/[\d\.]+/g);

		if (arrStr!= null) {
			if (arrStr.length>0) {
				jQuery("#LastNameError").html("Only letters allowed in this field!").show();
				
                noError = false;
			}
		}
	}

    if (jQuery("#useremail").val() == "") {
		jQuery("#UserNameError").html("Please enter your email address!").show();

        noError = false;
    }

    else if(isEmail(jQuery("#useremail").val())== false) {
		jQuery("#UserNameError").html("Please enter a valid email address!").show();

        noError = false;
    }

    var userPassword = jQuery("#user_password").val()

    if (jQuery("#user_password").val() == "") {
		jQuery("#PasswordError").html("Please enter the password!").show();

        noError = false;
    }

    else if (parseInt(userPassword.length)<5) {
		jQuery("#PasswordError").html("Password must be 5 to 20 characters in length!").show();

        noError = false;
    }

    if (checkSingleDropdown(jQuery("#user_birth_day")) || checkSingleDropdown(jQuery("#user_birth_month")) || checkSingleDropdown(jQuery("#user_birth_year"))) {
		jQuery("#BirthDateError").html("Please enter your date of birth!").show();

        noError = false;
    }

    if (checkOptButton(document.frmRegistration.gender)) {
		jQuery("#GenderError").html("Please select your gender!").show();

        noError = false;
    }

	if (checkCheckbox(jQuery("#Iagree"))) {
		jQuery("#AgreeError").html("You must agree to CitySocialising's Terms of Use before registering.").show();

		noError = false;
	}


    if(!noError) {
        jQuery("#mainError").show();
    }

    return noError;
}

function showHearDetails(type_id)
{
    if(type_id=='Another Search Engine' || type_id=='Magazine/Newspaper' || type_id== 'Another website' || type_id== 'Forum/messageboard' || type_id=='Other')
    {
        showBox("specify");
        jQuery("#hear_details").value = "";
    }
    else
    {
        hideBox("specify");
    }
}


function showBox(boxid){
    jQuery("#" + boxid).show();
}

function hideBox(boxid){
    jQuery("#" + boxid).hide();
}






function toggleOtherCountry(element) {


    if ((jQuery("#" + element).hide()) || (jQuery("#" + element).show())) {

        jQuery("#" + element).show();
    } else {

        jQuery("#" + element).hide();

    }

}


function submitForm()
{
    if(validateRegistration())
    {
        document.frmRegistration.actn.value="register"
        //document.frmRegistration.action="registration.html";
        //document.frmRegistration.submit();
        return true;
    }
    else
    {
        return false;
    }

}



	/*		Instruction labels		*/

	jQuery("#surname").focus(function() {
		jQuery("#nameHelp").show();
	});

	jQuery("#surname").blur(function() {
		jQuery("#nameHelp").hide();
	});


	jQuery("#useremail").focus(function() {
		jQuery("#emailHelp").show();
	});

	jQuery("#useremail").blur(function() {
		jQuery("#emailHelp").hide();
	});


	jQuery("#user_password").focus(function() {
		jQuery("#passwordHelp").show();
	});

	jQuery("#user_password").blur(function() {
		jQuery("#passwordHelp").hide();
	});
	
	
	jQuery("#user_birth_month").focus(function() {
		jQuery("#birthdateHelp").show();
	});

	jQuery("#user_birth_month").blur(function() {
		jQuery("#birthdateHelp").hide();
	});
	
	
	jQuery("#user_birth_day").focus(function() {
		jQuery("#birthdateHelp").show();
	});

	jQuery("#user_birth_day").blur(function() {
		jQuery("#birthdateHelp").hide();
	});
	
	
	jQuery("#user_birth_year").focus(function() {
		jQuery("#birthdateHelp").show();
	});

	jQuery("#user_birth_year").blur(function() {
		jQuery("#birthdateHelp").hide();
	});
	
	
	//	Page 2


	jQuery("#default_city").focus(function() {
		jQuery("#defaultCityHelp").show();
	});

	jQuery("#default_city").blur(function() {
		jQuery("#defaultCityHelp").hide();
	});


	jQuery("#postcode_live").focus(function() {
		jQuery("#postcodeHelp").show();
	});

	jQuery("#postcode_live").blur(function() {
		jQuery("#postcodeHelp").hide();
	});


	jQuery("#postcode_work").focus(function() {
		jQuery("#postcodeworkHelp").show();
	});

	jQuery("#postcode_work").blur(function() {
		jQuery("#postcodeworkHelp").hide();
	});
	
	
	jQuery("#mobile").focus(function() {
		jQuery("#mobileHelp").show();
	});

	jQuery("#mobile").blur(function() {
		jQuery("#mobileHelp").hide();
	});
	
	jQuery("#notliveinuk").click(function() {
		if (jQuery(this).is(":checked")) {
			jQuery("#otherCountry_live").show();
		} else {
			jQuery("#otherCountry_live").hide();
		}
	});
	
	jQuery("#notworkinuk").click(function() {
		if (jQuery(this).is(":checked")) {
			jQuery("#otherCountry").show();
		} else {
			jQuery("#otherCountry").hide();
		}
	});
	
	jQuery("#hear_options").focus(function() {
		jQuery(this).click(function() {
                  var curr_val = jQuery(this).val();
			if (curr_val == "Another Search Engine" || curr_val == "Magazine/Newspaper" || curr_val == "Another website" || curr_val == "Forum/messageboard" || curr_val == "Other") {
				jQuery("#specify").show();
				jQuery("#hear_details").val("");
			} else {
				jQuery("#specify").hide();
			}
		});
	});



	/*		Submit Form		*/
	
	jQuery("#frmRegistration").submit(function() {

		// Reset all error messages		
		jQuery(".fieldError").html("");
                var noError = true;

		if (jQuery("#page").val() == '1') {

			if (jQuery("#name").val() == "") {
				jQuery("#FirstNameError").html("Please enter your first name!").show();

				noError = false;
			}
			else if (jQuery("#name").val().length < 2) {
				jQuery("#FirstNameError").html("First name must be 2 to 20 characters in length!").show();

				noError = false;
			}

			else {
				var arrStr = jQuery("#name").val().match(/[\d\.]+/g);
				if (arrStr!= null) {
					if (arrStr.length>0) {
						// showError('FirstNameError',"Only letters allowed in this field!",jQuery("#name"),false);   //showError(divid,msg,field,alertMsg=false)

						jQuery("#FirstNameError").html("Only letters allowed in this field!").show();

						noError = false;

					}
				}
			}



			if (jQuery("#surname").val() == "") {
				jQuery("#LastNameError").html("Please enter your surname!").show();

				noError = false;
			}
			else if (jQuery("#surname").val().length < 2) {
				jQuery("#LastNameError").html("Surname must be 1 to 20 characters in length!").show();

				noError = false;
			}

			else {
				var arrStr = jQuery("#surname").val().match(/[\d\.]+/g);
				if (arrStr!= null) {
					if (arrStr.length>0) {
						// showError('FirstNameError',"Only letters allowed in this field!",jQuery("#name"),false);   //showError(divid,msg,field,alertMsg=false)

						jQuery("#LastNameError").html("Only letters allowed in this field!").show();

						noError = false;

					}
				}
			}





			if (jQuery("#useremail").val() == "") {
				jQuery("#UserNameError").html("Please enter your email address!").show();

				noError = false;
			}

			else if (isEmail(jQuery("#useremail").val())== false) {
				jQuery("#UserNameError").html("Please enter a valid email address!").show();

				noError = false;
			}

			var userPassword = jQuery("#user_password").val();

			if (jQuery("#user_password").val() == "") {			
				jQuery("#PasswordError").html("Please enter your password!").show();

				noError = false;
			}
			else if (parseInt(userPassword.length)<5) {
				jQuery("#PasswordError").html("Password must be 5 to 20 characters in length!").show();

				noError = false;
			}


			if (checkSingleDropdown(jQuery("#user_birth_month"))) {			
				jQuery("#BirthDateError").html("Please enter your date of birth!").show();

				noError = false;
			}

			if (checkSingleDropdown(jQuery("#user_birth_day"))) {
				jQuery("#BirthDateError").html("Please enter your date of birth!").show();

				noError = false;
			}

			if (checkSingleDropdown(jQuery("#user_birth_year"))) {
				jQuery("#BirthDateError").html("Please enter your date of birth!").show();

				noError = false;
			}
		}

		if (jQuery("#page").val() == '2') {

			noError = true;
			
			if (checkOptButton(document.frmRegistration.gender)) {			
				jQuery("#GenderError").html("Please select your gender!").show();
			
				noError = false;
			
			}
			

			if (jQuery("#mobile").val().length > 0) {

				if(!checkInternationalPhone(jQuery("#mobile").val())) {
					jQuery("#MobileError").html("Please enter valid mobile number!").show();

					noError = false;
				}
			}

			if (checkSingleDropdown(jQuery("#default_city"))) {
				jQuery("#DefaultCityError").html("Please select the default CitySocialising city!").show();

				noError = false;
			}
			

			if (checkSingleDropdown(jQuery("#postcode_live")) && checkCheckbox(jQuery("#notliveinuk"))) {
				jQuery("#PostLiveError").html("Please enter your Live-in postcode!").show();

				noError = false;
			}

			else if (!jQuery("#notliveinuk").is(":checked")) {
				if (checkSingleDropdown(jQuery("#otherLiveCountry"))) {
					jQuery("#CountryLiveError").html("Please choose the country you live in other than UK!").show();

					noError = false;
				}
			}

			if (!jQuery("#notworkinuk").is(":checked")) {
				if (checkSingleDropdown(jQuery("#otherWorkCountry"))) {
					jQuery("#CountryWorkError").html("Please choose the country you work in other than UK!").show();

					noError = false;
				}
			}

			var friendEmail = jQuery("#friend_email").val();
			if (friendEmail!="") {
				if (isEmail(friendEmail)== false) {
					jQuery("#friendEmailError").html("Please enter a valid email address!").show();

					noError = false;
				}
			}

			if(!jQuery("#Iagree").is(':checked')) {			
				jQuery("#AgreeError").html("You must agree to CitySocialising's Terms of Use and Special Code of Conduct before registering.").show();

				noError = false;
			}
		}

                // alert(noError);

		if (!noError) {
			jQuery("#mainError").show();
			return false;
		} else {
			return true;
		}
	});

});