$(document).ready(function(){

	//***************************************************
	// Username check button
	//***************************************************
	$("#checkname").click(function() {
		var usr = $("#username").val();
		if(usr.length >= 4) {
			$("#status").html('<img align="absmiddle" src="/images/icon_ajax_loader.gif" /> Checking availability...');
			$.ajax({
				type: "POST",
				url: "/reg_namecheck.php",
				data: "username="+ usr,
				success: function(msg) {
					$("#status").ajaxComplete(function(event, request, settings) {
						if(msg == 'OK') {
							$("#username").removeClass('object_error'); // if necessary
							$("#username").addClass("object_ok");
							$(this).html(' <img align="absmiddle" src="/images/checkmark.gif" /> Available ');
						}
						else {
							$("#username").removeClass('object_ok'); // if necessary
							$("#username").addClass("object_error");
							$(this).html(' <img align="absmiddle" src="/images/cancel.png" /> '+msg);
						}
					});
				}  // end of function(msg)...
			});  //end of $.ajax ({
		}
		else {
			$("#status").html('The username should have at least 4 characters.');
			$("#username").removeClass('object_ok'); // if necessary
			$("#username").addClass("object_error");
		}
	}); 

	//***************************************************
	// Submit form check
	//***************************************************
	$("#formRegister").submit(function() {

        //Initialise variables
        var errorMsg = "";
        var errorMsgLong = "";

        //Check for a username if new account mode
        if ($("#username").length>0 && $("#username").val().length <= 3){
                errorMsg += "\n\tUsername \t- Your Username must be at least 4 characters";
        }

        //Check for a password
        if ($("#password").length>0 ) {
        	// password input field exist (new account)
	        if ($("#password").val().length < 6){
	                errorMsg += "\n\tPassword \t- Your Password must be at least 6 characters";
	        }
	
	        //Check both passwords are the same
	        if (($("#password").val()) != ($("#password2").val())){
	                errorMsg += "\n\tPassword \t- The passwords entered do not match";
	        }
        }
        
        if ($("#email").val() == "" || !EmailCheck($("#email").val()) ) 
     		{
                errorMsg +="\n\tEmail\t\t- Enter your valid email address";
        }

	    //Check for a username
	    if ($("#firstname").val().length < 2){
	            errorMsg += "\n\tFirst name\t- Length of first name is required at least 2 characters.";
	    }

	    if ($("#lastname").val().length < 2){
	            errorMsg += "\n\tLast name\t- Length of last name is required at least 2 characters.";
	    }

	    // phone field is not required. Therefore, empty value is allowed.
	    if ($("#phone").val().length > 0 && $("#phone").val().length < 8){
            errorMsg += "\n\tPhone\t- Length of Phone is required at least 8 characters.";
	    }

	    if (($("#company").val().length > 0 && $("#company").val().length < 3) || $("#company").val().length > 60 ){
            errorMsg += "\n\tCompany name\t- Length of company name is required at least 3 characters. ";
	    }

	    //Check for a security code
        if ($("#secure-code").val().length != 6){
                errorMsg += "\n\tSecurity Code \t- You must enter the security code";
        }

/*	    if ($("#BrokerID").val().length > 0 && $("#BrokerID").val().length < 3 ){
	            errorMsg += "\n\tLength of Broker ID is required at least 3 characters. Or,\n you can leave it empty if you do not have brokerID code. ";
	    }
*/

		var agree = $("#agreeterm").attr('checked');
		if (!agree) {
			errorMsg += '\n\tAgree Terms \t- To continue, please check "Do you agree" box.';
		}

        //If there is aproblem with the form then display an error
        if ((errorMsg != "") || (errorMsgLong != "")){
                msg = ""; //"_______________________________________________________________\n\n";
                msg += "The form has not been submitted because there are problem(s) with the form.\n";
                msg += "Please correct the problem(s) and re-submit the form.\n";
                msg += "_______________________________________________________________\n\n";
                msg += "The following field(s) need to be corrected: -\n";

                errorMsg += alert(msg + errorMsg + "\n" + errorMsgLong);
                return false;
        }

        return true;
	});
});

	
function CancelReg() {
	if ($("#username").length > 0) { // new account
		confCancel = confirm("Are you sure you want to cancel registration?");
		pageReturn = 'index.php';
	} else {
		confCancel = confirm("Are you sure you want to cancel changes?");
		pageReturn = 'myaccount.php';
	}
		
	if(confCancel){
		window.location.href=pageReturn;
		return false;
	}	
	else{
		return false;
	}
}

