$(document).ready(function() {

	resetPage();
	
	var fullpath =  location.pathname;
	var host = location.host;
	var path = fullpath.replace('public_register.shtml','') + 'index.php?page=process_register';

	var options = {  
        url: path,
        type: 'post',        // 'get' or 'post', override for form's 'method' attribute 
        dataType: 'json',        // 'xml', 'script', or 'json' (expected server response type) 
        beforeSubmit: validateForm,  // pre-submit callback 
        success: showResponse  // post-submit callback 
		
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
	
	$('#register').ajaxForm(options);
});

function validateForm(formData, jqForm, options) { 

	resetPage();
	
	// here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
	var form = jqForm[0];
	var password = form.password.value;
	var password1 = form.password1.value;
	
	if(password == password1)
	{
		// alert ('Password is good');
		$('#register').hide();
		$('#wait').show('slow');
		return true;
	}else{
		$('#message').html('Password does not match');
		$('#message').show();
		return false
	}
	 
} 

function showResponse(response, status)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxForm method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
	if(status == "success")
	{
		// alert('Status is ' + response.status);
		// alert('Order is ' + response.message);
		switch(response.status)
		{
			case 'notyet':
				$('#wait').hide();
				$('#message').show();
				$('#register').show();
			break;
			
			case 'ok':
				$('#wait').hide();
				$('#message').show();
			break;
			
			case 'fail':
				$('#wait').hide();
				$('#message').show();
				$('#register').show();			
			break;
		}
		$('#message').html(response.message);
	}else{
		var message = "There is a problem. Please contact us";
		$('#message').html(message);
	}
}

function resetPage() {
	$('#message').hide();
	$('#wait').hide();
}