
google.load("maps", "2.x");
var map;
$(document).ready(function(){
	$('#infoColumn > .content').append('<div class="box"><div class="top"><div/></div><div class="main"><div class="content"><div class="toggleContent"/><div class="clear"></div><div id="map" style="height:18em; width:26.8em; overflow:hidden;"></div><div class="clear"></div></div></div><div class="bottom"><div/></div></div><div class="clear"></div>');


	// Call this function when the page has been loaded
	function initialize() {
		map = new google.maps.Map2(document.getElementById("map"));
		map.setCenter(new google.maps.LatLng(53.7091, -3.9519), 5);
	}
	google.setOnLoadCallback(initialize);

	$("input").focus(function () {
			 $(this).next("span").children("a").click();
	});

	$("textarea").focus(function () {
			 $(this).next("span").children("a").click();
	});

	$( "#signUpElement1form0" ).blur( function() {
		//checkAvailability();
	});

	$("#checkAvailability").click( function() {
		checkAvailability();
	});

	$("#checkPostcode").click( function() {
		checkPostcode();
	});

	$("#signUpElement11form0").blur( function() {
		//checkPostcode();
	});


	if( $('#signUpElement5form0').val() == '1') {
		$("#signUpElement7form0").parent().parent().hide();
		$("#signUpElement12form0").parent().parent().hide();
		$("#signUpElement13form0").parent().parent().hide();
		$("#signUpElement14form0").parent().parent().hide();
	} else {
		if( $("#signUpElement12form0").val() == '3' ) {
			$("#signUpElement13form0").parent().parent().hide();
		}
		if( $("#signUpElement12form0").val() == '2' ) {
		$("#signUpElement14form0").parent().parent().hide();
		}
	}

	$("#signUpElement5form0").siblings(".jNiceSelectWrapper").children("ul").find("a").click( function() {
		$("this").remove();
		//alert($("#signUpElement5form0").val());
		if( $("#signUpElement5form0").val() == 2 ) {
			$("label[for='signUpElement6form0']").html('Person 1 Gender');
			$("#signUpElement7form0").parent().parent().show();
			$("#signUpElement12form0").parent().parent().show();
			if( $("#signUpElement12form0").val() == '1' || $("#signUpElement12form0").val() == '2' ) {
				$("#signUpElement13form0").parent().parent().show();
			}
			if( $("#signUpElement12form0").val() == '1' || $("#signUpElement12form0").val() == '3' ) {
				$("#signUpElement14form0").parent().parent().show();
			}
		} else {
			$("label[for='signUpElement6form0']").html('Gender');
			$("#signUpElement7form0").parent().parent().hide();
			$("#signUpElement12form0").parent().parent().hide();
			$("#signUpElement13form0").parent().parent().hide();
			$("#signUpElement14form0").parent().parent().hide();
		}
	});

	$("#signUpElement12form0").siblings(".jNiceSelectWrapper").children("ul").find("a").click( function() {
		if( $("#signUpElement12form0").val() == '1' || $("#signUpElement12form0").val() == '2' ) {
			$("#signUpElement13form0").parent().parent().show();
		} else {
			$("#signUpElement13form0").parent().parent().hide();
		}
		if( $("#signUpElement12form0").val() == '1' || $("#signUpElement12form0").val() == '3' ) {
			$("#signUpElement14form0").parent().parent().show();
		} else {
			$("#signUpElement14form0").parent().parent().hide();
		}

	});

	$("#signUpElement9form0").change( function () {
		var Arrr = window[$("#signUpElement9form0").val()];
		$("#signUpElement10form0").autocomplete(Arrr);
	});

	//$("#signUpElement9form0").autocomplete(Counties);

	$("input[name=step1submitB]").click( function() {
		//checkAvailability();
		$('input').removeClass( 'redInput' );
		if( $('#termsAndConditions:checked').val() == null ) {
			$.growl('Notice!', 'You need to accept terms&conditions in order to register an account at Meet4Less.');
			return false;
		}
		
	/*	if( validate_notequal(  $("#signUpElement9form0").val(), '', 'Please select your county from  the list', $("#signUpElement9form0") ) {*/
		if( validate_email( $("#signUpElement2form0").val(), 'Please provide a valid email address', $("#signUpElement2form0") ) ) {
			if( validate_alphanumeric( $("#signUpElement1form0").val(), 'Profile name can consist only of letters A – Z and digits 0 – 9. Spaces and other characters can’t be used. For example, you couldn’t use the profile name Sunny and Cher, but you could use SunnyAndCher', $("#signUpElement1form0") ) ) {
				if( validate_length( $("#signUpElement1form0").val(), 4, 20, 'Profile name needs to have from 4 to 20 characters.', $("#signUpElement1form0") ) ) {
					if( validate_passwords( $("#signUpElement3form0").val(), $("input[name='signUpElement4form0']").val(), 'Passwords do not match.', $("#signUpElement3form0") ) ) {
						if (validate_length($("#signUpElement3form0").val(), 4, 20, 'Password needs to have from 4 to 20 characters.', $("#signUpElement3form0" ) )  ) {
							//if( validate_required($("#signUpElement9form0").val(), 'Please fill in County', $("#signUpElement9form0") ) ) {
							//	if (validate_required($("#signUpElement10form0").val(), 'Please fill in Town', $("#signUpElement10form0") ) ) {
							//		if (validate_required($("#signUpElement11form0").val(), 'Please fill in Postcode', $("#signUpElement11form0") ) ) {


										var countAllInputs = $("[id^='signUpElement']:not(.checkbox) ").length ;
										var counter = 0;
										$("[id^='signUpElement']:not(.checkbox)").each(function () {
										if ( $(this).val() != '' ) {
										counter++;
										}
										});

										$("form").append('<input type="hidden" value="'+countAllInputs+'" name="countAllInputsStep1"/>');
										$("form").append('<input type="hidden" value="'+counter+'" name="counterStep1"/>');

										return true;
							//		}
							//	}
							//}
						}
					}
				}
			}
		}
/*		}*/

		return false;
	});

});


function checkPostcode() {
	if( $( "#signUpElement11form0" ).val() !='' ) {

		/*$.getJSON("http://maps.google.com/maps/geo?q="+$( "#signUpElement11form0" ).val()+"+UK&output=json&oe=utf8&sensor=false&key=ABQIAAAAplHnZIR-fMq1qe7He_pCOhR99jTbk01f2xzMYcPBvLloo4kotBT75fj5l6KgiBnPvasfrd42p8JW_Q&callback=?",
        function(data){
			//alert(json_encode(data));
        	$('#signUpElement9form0').val(data.Placemark[0].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName);
			if(data.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName) {
				$('#signUpElement10form0').val(data.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.SubAdministrativeAreaName);
			} else {
				$('#signUpElement10form0').val(data.Placemark[0].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName);
			}
        });

        */

		paramObject = new Object();
		paramObject['postcode'] = $( "#signUpElement11form0" ).val();
		ah.sendImmediately( 'sign-up', 'check-postcode', paramObject, 'checkPostcodeRecieve' );
		$('#signUpElement11form0').parents('p.form0').after('<p style="padding-left:13.6em; display:none;" id="loadingBar">Loading, please wait.</p>');
		$('#loadingBar').slideDown();
	} else {
		$.growl( 'Notice!', 'You cannot leave your postcode blank.', 8000 );
	}
}

function checkPostcodeRecieve( paramArray ) {
	$('#loadingBar').slideUp( function() { $(this).remove(); } );
	if( paramArray.status != 'ok' ) {
		$.growl( 'Error', paramArray.status, 8000 );
		return false;
	}

	obj = json_decode(paramArray.data);

	if( obj.length == 0 ) {
		html = '<label for="signUpElement10form0">Town</label><input id="signUpElement10form0" type="text" name="signUpElement10form0" tabindex="12" />';
		var parent = $('#signUpElement10form0').parents('p');
		parent.find('label').remove();
		parent.find('div').remove();
		parent.find('input[type="text"]').remove();
		parent.prepend(html);
		$('#signUpElement10form0').parents('p').jNice();
		$('#signUpElement10form0').focus();
		$.growl('Selected postcode has not been found', 'Check if you have your postcode right, and if it is correct, select your town and county.', 10000);
		return true;
	} else {
		html = '<label for="signUpElement10form0">Town</label><select id="signUpElement10form0" name="signUpElement10form0" tabindex="12" ></select>';
		var parent = $('#signUpElement10form0').parents('p');
		parent.find('label').remove();
		parent.find('div').remove();
		parent.find('input[type="text"]').remove();
		parent.prepend(html);
	}
	map.setCenter(new google.maps.LatLng(obj[0].lat, obj[0].lng), 8);
	for( x=0; x<obj.length; x++ ) {
		$('#signUpElement10form0').append('<option value="'+obj[x].town+'">'+obj[x].town+'</option>');
	}
	$('#signUpElement11form0').val(paramArray.postcode);
	$('#signUpElement10form0').append('<option onClick="addNewTown();" value="">« Not On The List »</option>');
	$('#signUpElement10form0').parents('p').jNice();

	for( x=0; x<obj.length; x++ ) {
		$('#signUpElement10form0').siblings(".jNiceSelectWrapper").find("a[index='"+x+"']").attr('onClick', '$(\'#signUpElement9form0\').siblings(\'.jNiceSelectWrapper\').find(\'a[index='+(obj[x].countyId)+']\').click(); map.setCenter(new google.maps.LatLng('+obj[x].lat+', '+obj[x].lng+'), 8);');
	}
	$('#signUpElement10form0').siblings(".jNiceSelectWrapper").find("a[index='"+x+"']").click( function() { addNewTown(); });

	$('#signUpElement9form0').siblings('.jNiceSelectWrapper').find('a[index='+(obj[0].countyId)+']').click();
}

function addNewTown() {
	html = '<label for="signUpElement10form0">Town</label><input id="signUpElement10form0" type="text" name="signUpElement10form0" tabindex="12" />';
	Tparent = $('#signUpElement10form0').parents('p');
	Tparent.find('label').remove();
	Tparent.find('div').remove();
	Tparent.prepend(html);
	$('#signUpElement10form0').parents('p').jNice();
	$('#signUpElement10form0').focus();
}


function checkAvailability() {
	if (validate_alphanumeric($("#signUpElement1form0").val(), 'Profile name can consist only of letters A – Z and digits 0 – 9. Spaces and other characters can’t be used. For example, you couldn’t use the profile name Sunny and Cher, but you could use SunnyAndCher', $("#signUpElement1form0"))) {
		if (validate_length($("#signUpElement1form0").val(), 4, 20, 'Profile name needs to have from 4 to 20 characters.')) {
			paramObject = new Object();
			paramObject['profileName'] = $( "#signUpElement1form0" ).val();
			ah.addMessage( 'sign-up', 'check-profile-name', paramObject, 'signUpProfileNameCheckRecieve' );
			$( "#signUpElement1form0" ).siblings( '.desc' ).html('Checking. Please wait...');
		}
	}
}

function signUpProfileNameCheckRecieve( recievedArrayWithData ) {
	if( recievedArrayWithData['available'] == 'true' ) {
		$( "#signUpElement1form0" ).siblings( '.desc' ).html('<span style="color:green;">This profile name is available</span>');
	} else {
		$( "#signUpElement1form0" ).siblings( '.desc' ).html('<span style="color:red;">Profile name is already taken. Choose a different one.</span>');
	}
}

