
    var handleOkay   = true;
    var emailChecked = true;

    function ajax_checkDuplicate( regType, typeToCheck ) {

        var valueToCheck = "";
        if ( typeToCheck == "handle" ) {
            valueToCheck = $("#handle").val();
        } else if ( typeToCheck == "email" ) {
            valueToCheck = $("#email").val();
        }

        if ( valueToCheck != "" ) {

            $.get( "checkduplicate.php", {
                regtype : regType,
                type : typeToCheck,
                value : valueToCheck
            }, function( responseText ) {

                if ( typeToCheck == "handle" ) {

                    if ( responseText.found == true ) {
                        handleOkay = false;
                        $("#handleAlert").show();
                        $("#handle").css("background-color","#fff8d6");
                    } else {
                        handleOkay = true;
                        $("#handleAlert").hide();
                        $("#handle").css("background-color","#fff");
                    }

                } else if ( typeToCheck == "email" ) {

                    if ( responseText.found == true ) {
                        emailChecked = false;
                        $("#emailAlert").show();
                        $("#email").css("background-color","#fff8d6");
                    } else {
                        emailChecked = true;
                        $("#emailAlert").hide();
                        $("#email").css("background-color","#fff");
                    }
                }

            }, "json" )

        }

    }


    // checks string is valid email address
    function checkEmail( regType ) {

        var email = document.getElementById( "email" ).value;

        var at   = "@";
        var dot  = ".";
        var lat  = email.indexOf( at );
        var lstr = email.length;
        var ldot = email.indexOf( dot );

        var checkedOkay = true;
/*
        if ( email.indexOf( at ) == -1 ) {
           checkedOkay = false;
           alert('1');
        }
        if ( email.indexOf( at ) == -1 || email.indexOf( at ) == 0 || email.indexOf(at) == lstr ) {
           checkedOkay = false;
           alert('2');
        }
        if (email.indexOf( dot ) == -1 || email.indexOf( dot ) == 0 || email.indexOf( dot ) == lstr ) {
            checkedOkay = false;
           alert('3');
        }
        if ( email.indexOf( at, ( lat + 1 ) ) != -1 ) {
           checkedOkay = false;
           alert('4');
        }
        if ( email.substring( lat-1, lat ) == dot || email.substring( lat+1, lat+2 ) == dot ){
           checkedOkay = false;
           alert('5');
        }
        if ( email.indexOf( dot, ( lat+2 ) ) == -1) {
           checkedOkay = false;
           alert('6');
        }
        if ( email.indexOf( " " ) != -1 ) {
           checkedOkay = false;
           alert('7');
        }
*/
        if ( checkedOkay == true ) {
            emailChecked = true;
            document.getElementById( "emailAlert" ).innerHTML = "";
            document.getElementById( "emailAlert" ).style.display = "none";
            document.getElementById( "email" ).style.backgroundColor = "#fff";
            ajax_checkDuplicate( regType, "email" );
        } else {
            emailChecked = false;
            document.getElementById( "emailAlert" ).innerHTML = "<span style='color:#844;'>This is not a valid email address. Please check you have entered it correctly.</span>";
            document.getElementById( "emailAlert" ).style.display = "block";
            document.getElementById( "email" ).style.backgroundColor = "#fff8d6";
        }

    }


