﻿$(document).ready(function() {

    $("#notRegistered").click(function(){        
        $("#login").slideUp(function(){
            $("#register").slideDown();
        });        
    })
    
    $("#existingUser").click(function(){        
        $("#register").slideUp(function(){
            $("#login").slideDown(); 
        });               
    })
    
    $("#lnk_PasswordReminder").click(function(){
        $("#login").slideUp(function(){
            $("#passwordreminder").slideDown();        
        });        
    })
    
    $("#ReturnToLogin").click(function(){        
        $("#passwordreminder").slideUp(function(){
            $("#login").slideDown();
        });                    
    })
        
    var $loginform = $('#login');
    $loginform.keypress(function(e){         
        if (e.which == 13 && e.target.type != 'textarea' && e.target.type != "") {                               
           login();                                                                           
        }
    });
    
    var $registerform = $('#register');
    $registerform.keypress(function(e){         
        if (e.which == 13 && e.target.type != 'textarea' && e.target.type != "") {                               
           register();                                                                           
        }
    });   
    
    var $reminderform = $('#passwordreminder');
    $reminderform.keypress(function(e){         
        if (e.which == 13 && e.target.type != 'textarea' && e.target.type != "") {                               
           passwordReminder();                                                                           
        }
    });
    
    $("#btnReminder").click(function(){
        passwordReminder();
    })
    
    $("#btnLogin").click(function(){
        login();
    })
    
    $("#btnRegister").click(function(){
        register();
    })
    
})

function passwordReminder(){

    var errors = "";

    if(!isValidEmail($("#txt_password_reminder_Email").val())){
        errors = "** Please enter a valid e-mail **";            
    }
    
    if(errors != ""){
        $("#reminderErrors").html(errors);
        $("#reminderErrors").slideDown();    
    }else{    
        $.get("Handler/Handler.ashx", { "action": "SendPasswordReminder", "Email": $("#txt_password_reminder_Email").val() , "Refresh": new Date().getTime() }, function(data){
        
            if(data == "0"){
                errors = "** Sorry - We could not find any records on our system with the specified e-mail address **";
                $("#reminderErrors").html(errors);
                $("#reminderErrors").slideDown();                
            }else{
                $("#reminderComplete").html("Your reminder has been sent")                                            
            }        
        })            
    }   
        
}

function login(){

    var errors = "";
    if($("#txt_Login_Email").val() == ""){
        errors += "** Please enter your e-mail address **";
    }else{
        if(!isValidEmail($("#txt_Login_Email").val())){
            errors += "** Please enter a valid e-mail address **";        
        }                
    }   
    
    if($("#txt_Login_Password").val() == ""){
        if(errors != "") errors += "<br />"
        errors += "** Please enter your password **";
    }     
    
    if(errors != ""){
        $("#loginErrors").html(errors);
        $("#loginErrors").slideDown();    
    }else{          
        $.get("Handler/Handler.ashx", { "action": "Login", "Email": $("#txt_Login_Email").val(), "Password": $("#txt_Login_Password").val(), "Refresh": new Date().getTime() }, function(data){                                      
            if(data == 0){
                $("#loginErrors").html("** Login Failed - Your username or password are incorrect **");
                $("#loginErrors").slideDown();             
            }else{
                if(querySt("page") != undefined && querySt("page") != ""){                    
                    if(querySt("action")){
                        switch(querySt("action")){
                            case "add":
                              window.location.href = "MyGallery.aspx?action=add&paintingid=" + querySt("paintingid");   
                            break;
                        }
                    
                    }else{
                        switch(querySt("page")){
                            case "MyGallery":                                                            
                                window.location.href = "MyGallery.aspx"; 
                            break; 
                            case "checkout":
                                window.location.href = "Checkout_Billing.aspx"; 
                            break;                                                        
                        }
                    }                    
                }else{
                    window.location.href = "MyGallery.aspx"; 
                }                            
            }                                                                   
        })                 
    }    
    
}

function register(){
    
    var errors = "";
    if($("#txt_register_firstname").val() == ""){
        errors += "** Please enter your first name **";            
    }
    
//    if($("#txt_register_telephone").val() == ""){
//        if(errors != "") errors += "<br />"
//        errors += "** Please enter your telephone number **";            
//    }

    if($("#txt_register_lastname").val() == ""){
        if(errors != "") errors += "<br />"
        errors += "** Please enter your last name **";            
    }
    
    if($("#txt_register_email").val() == ""){
        if(errors != "") errors += "<br />"
        errors += "** Please enter your email address **";            
    }else{
        if(!isValidEmail($("#txt_register_email").val())){
            errors += "** Please enter a valid e-mail address **";        
        }          
    }
    
    if($("#txt_register_password").val() == ""){
        if(errors != "") errors += "<br />"
        errors += "** Please enter a password **";            
    }        
    
    if(errors != ""){
        $("#registerErrors").html(errors);
        $("#registerErrors").slideDown();    
    }else{    
    
    var mailinglist = $("#txt_register_newsletter").attr("checked");
    
    if (mailinglist){
        mailinglist = 1;
    }else{
        mailinglist = 0;
    }
    
        $.get("Handler/Handler.ashx", { "action": "Register", "FirstName": $("#txt_register_firstname").val(), "LastName": $("#txt_register_lastname").val(), "Telephone": $("#txt_register_telephone").val(), "Email": $("#txt_register_email").val(), "Password": $("#txt_register_password").val(), "MailingList": mailinglist, "Refresh": new Date().getTime() }, function(data){
            if(data == 0){
                $("#registerErrors").html("** A user already exists with this Email address **");
                $("#registerErrors").slideDown();             
            }else{
                if(querySt("page") != undefined && querySt("page") != ""){  
                    if(querySt("action") != undefined && querySt("action") != ""){

                        switch(querySt("action")){
                            case "add":
                              window.location = "MyGallery.aspx?action=add&paintingid=" + querySt("paintingid");   
                            break;
                        }
                    
                    }else{
                        switch(querySt("page")){
                            case "MyGallery":
                                window.location = "MyGallery.aspx"; 
                            break;   
                            case "checkout":
                                window.location = "Checkout_Billing.aspx"; 
                            break;                                                   
                        }
                    }                    
                }else{
                    window.location = "MyGallery.aspx"; 
                }   
            }                                                                   
        })            
    }
                                    
}

function isValidEmail(Email){

    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!(filter.test(Email))){
        return false;
    }else{
        return true;
    }       

}

function querySt(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}
