function formCheck(formobj){
	// field name
	var fieldRequired = Array("first_name", "last_name", "primary_email", "postcode", "mobile", "owner_buyer");
	// field description to appear in the dialog box
	var fieldDescription = Array("First Name", "Last Name", "Email", "Postcode", "Mobile", "About You");
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.options[obj.selectedIndex].value == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}


$(document).ready(function(){
	
	$('input#postcode').bind('keyup', function(){
		 
		// Find form action
		var zip = $('input#postcode').val();
		
		if( zip.length == 4 )
		{
			// swordfish.blockshome.com
			$.ajax({
			  url: 'http://swordfish.blockshome.com/custom/form_ids/'+zip+'?callback=?',
			  cache: false,
			  success: function(data){
				if( data.id != '' )
				{
					// zip ok
					// console.log('ID: '+data.id);
					var new_form_action = "http://swordfish.blockshome.com/forms/"+data.id;
					$('#reg1_form').attr('action',new_form_action);
				}
			  },
			dataType: 'json'
			});
		}
		
	});
	
	
	$('#reg1_form').submit(function(){
		if( formCheck(this) )
		{
			if( $('input#postcode').val().length != 4 )
			{
				alert("Please enter your postcode!");
				return false;
			}
			else
			{
				//alert("form submit");
				return true;
				// return false;
			}
		}
		else
		{
			return false;
		}

		// return false;
	});
	
});
