

function login() {
    //alert('login');
    var valid = 1;

    hideshow('login-msg', 0);
    if ($("#login-password").val().length == 0) {
        valid = 0;
        message(1, 'Please provide your password.');
    }

    if ($("#login-email").val().length == 0) {
        valid = 0;
        message(1, 'Please provide your email address.');
    }

    if ($("#login-email").val() == "Email Address") {
        valid = 0;
        message(1, 'Please provide your email address.');
    }


    if (valid == 1) logindb();

}

function logindb() {
    //alert('logindb');
    
    var url = ResolveUrl("~/LoginService.asmx");
    $.ajax({
        type: "POST",
        url: url + "/Login",
        data: "{UserEmail:'" + $('#login-email').val() + "',UserPassword:'" + $('#login-password').val() + "'}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            LoginSucceeded(msg.d);
        },
        error: AjaxFailed
    });

}



function logoff() {
    //alert('logoff');
    var url = ResolveUrl("~/LoginService.asmx");
    $.ajax({
        type: "POST",
        url: url + "/LogOff",
        data: "{}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            window.location.reload();
        },
        error: AjaxFailed
    });

}

function LoginSucceeded(result) {
    if (result) {
        window.location = "Default.aspx";
    } else {
        hideshow('login-msg', 1);
        $('#login-msg').html('Invalid Login. Please try again');
    }
}

function AjaxFailed(result) {
    //alert(result.status + ' ' + result.statusText);
}







function ResolveUrl(url) {
    if (url.indexOf("~/") == 0) {
        url = baseUrl + url.substring(2);
    }
    return url;
}

function register() {
    //alert('register');
    hideshow('loading', 1);
    error(0);

    var url = ResolveUrl("~/HomeSubmit.php");
    $.ajax({
        type: "POST",
        url: url,
        data: $('#regForm').serialize(),
        dataType: "json",
        success: function(msg) {

            if (parseInt(msg.status) == 1) {;
                registerdb(msg.txt)
            }
            else if (parseInt(msg.status) == 0) {
                error(1, msg.txt);
                hideshow('loading', 0);
            }

            
        }
    });

}


function registerdb(redirect) {
//    alert('logindb');
//    alert($('#uname').val());
//    alert("sex: " + $('#sex-select').val());
    
    var gender;
    var seeking;
    if ($('#sex-select').val() == '1') gender = 'F';
    else gender = 'M';

    if ($('#seeking-select').val() == '1') seeking = 'F';
    else seeking = 'M';
    
    var url = ResolveUrl("~/MasterService.asmx");
    $.ajax({
        type: "POST",
        url: url + "/Register",
        data: "{strUserName:'" + $('#uname').val() + "',strPassword:'" + $('#pass').val() + "',strEmail:'" + $('#email').val() + "',strFirstName:'" + $('#fname').val() + "',strLastName:'" + $('#lname').val() + "',strZipCode:'" + $('#zipcode').val() + "',strGender:'" + gender + "',strSeeking:'" + seeking + "',intMonth:'" + $('#month').val() + "',intDay:'" + $('#day').val() + "',intYear:'" + $('#year').val() + "'}",
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function(msg) {
            //alert(msg);
            if (msg.d > 1) {
                var errortxt = ErrorMessage(msg.d);
                error(1, errortxt);
            }else{
                window.location = redirect;
            }
            hideshow('loading', 0);
        },
        error: AjaxFailed
    });

}

function ErrorMessage(errorcode) {
    //alert(errorcode);
    var errormsg;
    switch (errorcode) {
        case 1:
            errormsg = "The user name was not found in the database.";
            break;
        case 2:
            errormsg = "The password is not formatted correctly";
            break;
        case 3:
            errormsg = "The password question is not formatted correctly.";
            break;
        case 4:
            errormsg = "The password answer is not formatted correctly.";
            break;
        case 5:
            errormsg = "The e-mail address is not formatted correctly.";
            break;
        case 6:
            errormsg = "The user name already exists in the database for the application.";
            break;
        case 7:
            errormsg = "The e-mail address already exists in the database.";
            break;
        case 8:
            errormsg = "The user was not created, for a reason defined by the provider";
            break;
        case 9:
            errormsg = "The provider user key is of an invalid type or format..";
            break;
        case 10:
            errormsg = "The user key already exists in the database for the application.";
            break;
        case 11:
            errormsg = "The an error that is not described. Please try again.";
            break;
        case 50001:
            errormsg = "Zip code doesnt exists in our system. Please try a near by zip code.";
            break; 
        case 50002:
            errormsg = "Your postal code doesnt exists in our system. Please remove Dashes.";
            break; 
        case 50006:
            errormsg = "The username already exists. Please try another.";
            break; 
        case 50007:
            errormsg = "Profile with this email already exists. Please try another.";
            break;
        case 50011:
            errormsg = "An error occurred inserting your record. Please try another.";
            break;

        default: alert("There was an error creating your profile. Please try again.");
        
    }
    return errormsg;

}



function display(el, type) {
    $('#' + el).css('display', type);
}


function displayshow(el, act) {
    if (act) $('#' + el).css('display', 'inline');
    else $('#' + el).css('display', 'none');
}

function hideshow(el, act) {
    if (act) $('#' + el).css('visibility', 'visible');
    else $('#' + el).css('visibility', 'hidden');
}

function error(act, txt) {
    hideshow('error', act);
    if (txt) $('#error').html(txt);
}


function message(act, txt) {
    hideshow('login-msg', act);
    if (txt) $('#login-msg').html(txt);
}



function addOptions() {
    for (i = 1; i <= 31; i++) {
        $('#day').append($("<option></option>").attr("value", i).text(i));
    }

    for (i = 1; i <= 12; i++) {
        $('#month').append($("<option></option>").attr("value", i).text(i));
    }

    for (i = 1900; i <= 2010; i++) {
        $('#year').append($("<option></option>").attr("value", i).text(i));
    }


}
