$(document).ready(function(){
	 DoAjx_getptytype ()

	$('input[@name=email]').blur(function () {  validatemail('no') })


	$('#submitbtn')
	.click(function() { 

		

		var ptyt=$('#propertytypeptsel').val()   // clashes with other ptytype on page
		if (ptyt=='empty') {
			alert('Please select a property type')
			return
		}

		var prov=$('select[@name=geosubcat1]').val()
		if (prov=='0') {
			alert('Please select a province')
			return
		}

		var city=$('select[@name=geosubcat2]').val()
		if ((city==undefined)||(city=='0')) {
			alert('Please select a city')
			return
		}

		var suburb=$('select[@name=geosubcat3]').val()
		if ((suburb==undefined)||(suburb=='0')) {
			alert('Please select a suburb')
			return
		}

		var username=$('input[@name=username]').val()
		if (username=='') {
			alert('Please enter your name')
			return
		}

		var email=$('input[@name=email]').val()
		if ((email=='') || ((email=='email'))){
			alert('Please enter your email address')
			return
		}


		validatemail('yes')

	 })
	.addClass('clickable')

// NUMERIC.js must be included.. or this will fail
		$('.numbersonly').numeric()

});


function showrest() {

}

function validatemail(andsubmit) {
	// checks email for validity and availability
	var email=$('input[@name=email]').val()
	if (email=='') return
	var poststr= "username=" + email
	$('input[@name=email').after('<img class="progress" src="rpspinbig.gif" alt="loading ...">')
			$.ajax({
   			type: "POST",
   			url: "ajq_requsersignup.php",
			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('.progress').remove()
   				$("#savinglabel").html(jr.respz.html)
				if (jr.respz.isemailvalid=='yes') {
					$('#submitbtn').show()	
					$('.fb').remove()
					if (andsubmit=='yes')	$('form[@name=getagentform]')[0].submit()
				}
				else {
					 $('#submitbtn').hide()
					$('.fb').remove()
					 $('#submitbtn').after('<span class="fb errormsg">Email is invalid<br>Correct and click <span class="clickable" id="redo">here</span></span>')
					$('#redo').click(function() { validatemail(andsubmit) })
				}
   			}
 		});
}
//============================================


function DoAjx_getptytype () {
		$('#mypropholder').html('<img class="progress" src="rpspinbig.gif" alt="loading ...">')
 		$.ajax({
   			type: "POST",
   			url: "ajq/ajq_getptytype.php",
//			data: poststr,
			dataType: "json",
   			success: function(jr){
				$('.progress').remove()
				$('#mypropholder').html(jr.respz.sel)
				showrest() 
			}
 		});
}
