/*-------------------------------------------
* register.js
* functions to validate the registration form
-------------------------------------------*/

/*---------------------------------------------------
* check whether the form can be submitted or not
---------------------------------------------------*/
function check(reg_form)
{

    if (!G_touched) {
        //None of the fields was changed
        return false;
    }
	
    var L_BAM_id = $("#bamid").val();
    var L_user_mame = $("#reg_userName").val();
    var L_password1 = $("#reg_password1").val();
    var L_email = $("#reg_email").val();

    if ((!G_validated_user_name && L_BAM_id == 0) || (!G_validated_user_name && L_BAM_id > 0 && L_user_mame != "")) {
        $("#user_name_img").attr('src', '/graphics/not_ok.gif');
        $("#user_name_error_text").html("<li>" + G_error_text_username);
        $('#error_user_name').slideDown(500);
        return false;
    }
    else if ((!G_validated_email_address && L_BAM_id == 0) || (!G_validated_email_address && L_BAM_id > 0 && L_password1 != "")) {
        $("#email_img").attr('src', '/graphics/not_ok.gif');
        $("#email_error_text").html("<li>" + G_error_text_email);
        $('#error_email').slideDown(500);
        return false;
    }
    else if ((!G_validated_password && L_BAM_id == 0) || (!G_validated_password && L_BAM_id > 0 && L_email != "")) {
        $("#password1_img").attr('src', '/graphics/not_ok.gif');
        $("#password2_img").attr('src', '/graphics/not_ok.gif');
        $("#password_error_text").html("<li>" + G_error_text_password);
        $('#error_password').slideDown(500);

        return false;
    }

    return true;

}

/*----------------------
* validate username
----------------------*/
function validate_user_name() {
    G_touched = true;

    var L_user_mame = $("#reg_userName").val();
    var L_BAM_id = $("#bamid").val();
    if (L_user_mame != "") {
        $("#user_name_img").attr('src', '/graphics/loading.gif');

        $.get('/bam/register_rpc.php', {
            username: L_user_mame,
            bamid: L_BAM_id
        }, function(xml) {
            read_user_rpc(xml)
        });
    } else {
        $('#error_user_name').slideUp(500);
        $("#user_name_img").attr('src', '/graphics/spacer14x14.gif');
        G_validated_user_name = false;
    }

}

function read_user_rpc(L_xml) {
    var count = L_xml.getElementsByTagName("count").item(0).firstChild.data;
    if (count > 0) {
        $("#user_name_img").attr('src', '/graphics/not_ok.gif');
        G_validated_user_name = false;
        G_error_text_username = G_error_text_username1;
    } else {
        $("#user_name_img").attr('src', '/graphics/ok.gif');
        G_validated_user_name = true;
        $('#error_user_name').slideUp(500);
    }

}

/*----------------------
* validate password
----------------------*/
function validate_password() {
    G_touched = true;

    var L_password1 = $("#reg_password1").val();
    var L_password2 = $("#reg_password2").val();
    var L_strength = $("#password_strength").val();
    var L_BAM_id = $("#bamid").val();
	
    if (L_password1 == "" && L_password2 == "") {
        G_validated_password = false;
        G_error_text_password = G_error_text_password4;
    } else if (L_password1 != "" && L_password2 == "") {
        G_validated_password = false;
    //document.getElementById("password_error_text").innerHTML = "<li>" + G_error_text_password3;
    //new Effect.BlindDown("error_password");
    } else if (L_password1 == "" && L_password2 != "") {
        G_validated_password = false;
        G_error_text_password = G_error_text_password3;
    } else if (L_password1 != L_password2) {
        G_validated_password = false;
        G_error_text_password = G_error_text_password2;
    } else if (L_password1 == L_password2) {
        /*if (L_strength < 15)
		{
			G_validated_password = false;
			G_error_text_password = G_error_text_password1;
			document.getElementById("password_error_text").innerHTML = "<li>" + G_error_text_password1;
		}
		else
		{*/
        if (L_password1.lenght < 6) {
            G_validated_password = false;
            G_error_text_password = G_error_text_password1;
        } else {
        $("#password1_img").attr('src', '/graphics/ok.gif');
        $("#password2_img").attr('src', '/graphics/ok.gif');
            G_validated_password = true;
            $('#error_password').slideUp(500);
        }
    }


    if (L_BAM_id > 0 && L_password1 == "" && L_password2 == "") {
        G_validated_password = true;
    }

}

/*---------------------------
* validate email address
---------------------------*/
function validate_email() {
    G_touched = true;

    var L_email = $("#reg_email").val();
    var L_BAM_id = $("bamid").val();
    if (L_email != "") {
        if (echeck(L_email)) {
        $("#email_img").attr('src', '/graphics/loading.gif');

            $.get('/bam/register_rpc.php', {
                email: L_email,
                bamid: L_BAM_id
            }, function(xml) {
                read_email_rpc(xml)
            });
        } else {
            G_validated_email_address = false;
            G_error_text_email = G_error_text_email1;
        }
    } else {
        $("#email_img").attr('src', '/graphics/spacer14x14.gif');
        G_validated_email_address = false;
        $('#error_email').slideUp(500);
    }

}

function read_email_rpc(L_xml)
{
    var count = L_xml.getElementsByTagName("count").item(0).firstChild.data;
    if (count > 0) {
        $("#email_img").attr('src', '/graphics/not_ok.gif');
        G_validated_email_address = false;
        G_error_text_email = G_error_text_email2;
    } else {
        $("#email_img").attr('src', '/graphics/ok.gif');
        G_validated_email_address = true;
        $('#error_email').slideUp(500);
    }

}

/*------------
* validate URL
------------*/
function validate_url() {
    G_touched = true;

    var L_url = $("#reg_url").val();
    if (L_url == "") {
        $("#url_img").attr('src', '/graphics/spacer14x14.gif');
    } else {
        $("#url_img").attr('src', '/graphics/ok.gif');
    }
}

/*----------------
* validate zipcode
----------------*/
function validate_postal_code() {
    G_touched = true;

    var L_postal_code = $("#reg_postal_code").val();
    if (L_postal_code == "") {
        $("#postal_code_img").attr('src', '/graphics/spacer14x14.gif');
    } else {
       $("#postal_code_img").attr('src', '/graphics/ok.gif');
    }
}

/*--------------------------
* validate phone number
--------------------------*/
function validate_phone_number() {
    G_touched = true;

    var L_phone_number = $("#reg_phone_number").val().toUpperCase();
    if (L_phone_number == "") {
       $("#phone_number_img").attr('src', '/graphics/spacer14x14.gif');
    } else if (L_phone_number != "") {
       $("#phone_number_img").attr('src', '/graphics/ok.gif');
    }
}

function echeck(str) {
    var at="@";
    var dot=".";
    ;
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1) {
        //		alert("Invalid E-mail ID");
        return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
        //		alert("Invalid E-mail ID");
        return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
        //		alert("Invalid E-mail ID");
        return false;
    }
    if (str.indexOf(at,(lat+1))!=-1) {
        //		alert("Invalid E-mail ID");
        return false;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
        //		alert("Invalid E-mail ID");
        return false;
    }
    if (str.indexOf(dot,(lat+2))==-1) {
        //		alert("Invalid E-mail ID");
        return false;
    }
    if (str.indexOf(" ")!=-1) {
        //		alert("Invalid E-mail ID");
        return false;
    }
    return true;

}

/*--------------
* init form
--------------*/
function init_form() {
    //init global vars
    G_validated_user_name = false;
    G_validated_email_address = false;
    G_validated_password = false;

    var L_BAM_id = $("bamid").val();
    if (L_BAM_id > 0) {
        G_validated_password = true;
    }

    init_error_texts();
    G_error_text_username = "";
    G_error_text_email = G_error_text_email1;
    G_error_text_password = G_error_text_password4;

    G_touched = false;


}

