﻿function clsUserdataCtrl()
{   
    this.login = false;
    
    this.tempID = "";
    this.userID = "";
    this.userFName = "";
    this.userLName = "";
    this.userLV  = "";
}

var ctrlUserdata = new clsUserdataCtrl();

clsUserdataCtrl.prototype.initUserdata = function() {
    this.divLogin = document.getElementById("divLogin");

    this.btnLoginClose = document.getElementById("btnLoginClose");
    //this.btnLoginSubmit = document.getElementById("btnLoginSubmit");

    this.iptLoginID = document.getElementById("iptLoginID");
    this.iptLoginPass = document.getElementById("iptLoginPass");

    this.divAccount = document.getElementById("divAccount");

    this.divMenu = document.getElementById("divMenu");
    $("#divAccount").dialog({ bgiframe: true, autoOpen: false, width: 230, height: 450, modal: true,

        buttons: {
            Cancel: function() { $(this).dialog('close'); },
            'Submit': function() { ctrlUserdata.clicked_AccountSubmit(); }
        },
        close: function() {
            ctrlUserdata.removeAllAccountClasses();
            ctrlLayout.AfterDIalog();
        }
    });

    $("#divLogin").dialog({ bgiframe: true, autoOpen: false, width: 230, height: 300, modal: true,

        buttons: {
            Cancel: function() { $(this).dialog('close'); },
            'Login': function() { ctrlUserdata.clicked_LoginSubmit(); }
        },
        close: function() {
            ctrlLayout.AfterDIalog();
        }
    });

    $("#divConfirmYet").dialog({ bgiframe: true, autoOpen: false, width: 330, height: 150, modal: true,

        buttons: {
            Cancel: function() { $(this).dialog('close'); },
            'Send': function() { ctrlUserdata.requestConfirmMail(); $(this).dialog('close'); }
        },
        close: function() {
            ctrlLayout.AfterDIalog();
        }
    });

    ctrlUserdata.CheckingLogin();
    
}

clsUserdataCtrl.prototype.CheckingLogin = function() {
    var idsss = document.getElementById('lbUserID').innerHTML;
    if (idsss != "" && idsss != null) {
        ctrlUserdata.userID = idsss;
        ctrlUserdata.userFName = document.getElementById('lbFName').innerHTML;
        ctrlUserdata.userLName = document.getElementById('lbLName').innerHTML;
        ctrlUserdata.userLV = document.getElementById('lbUserLV').innerHTML;
        ctrlUserdata.LoginResult(ctrlUserdata.userLV);
    }
}

clsUserdataCtrl.prototype.clicked_Account = function(e) {
    /*
    ctrlLayout.BeforeDIalog();
    $("#divAccount").dialog('open');
    */
    ctrlUserdata.GotoAddAcountPage();
}

clsUserdataCtrl.prototype.HideMenuBox = function()
{
    this.divMenu.style.visibility = 'hidden';
}

clsUserdataCtrl.prototype.clicked_Login = function(e) {
    ctrlUserdata.GotoLoginPage();
    /*
    ctrlLayout.BeforeDIalog();
    
    $("#iptLoginID").val('lifeunderyourfeet@gmail.com');
    $("#iptLoginPass").val('');
    $("#divLogin").dialog('open');
    */
}

clsUserdataCtrl.prototype.GotoLoginPage = function() {
    var arrCurUrl = location.href.split('/');
    var strMain = "";
    var str = location.href;
    if (str.search("localhost") < 0) {
        arrCurUrl[0] = arrCurUrl[0].replace('http', 'https');
    }

    if (str.search("dev") > 0) {
        arrCurUrl[0] = arrCurUrl[0].replace('https', 'http');
    }
    
    for (var i = 0; i < arrCurUrl.length - 1; i++) {
        strMain += arrCurUrl[i] + '/';
    }
    strMain += 'Login/LoginMain.aspx';
    window.location = "";
    window.location = strMain;
}

clsUserdataCtrl.prototype.GotoAddAcountPage = function() {
    var arrCurUrl = location.href.split('/');
    var strMain = "";
    var str = location.href;
    if (str.search("localhost") < 0) {
        arrCurUrl[0] = arrCurUrl[0].replace('http', 'https');
    }
    for (var i = 0; i < arrCurUrl.length - 1; i++) {
        strMain += arrCurUrl[i] + '/';
    }
    strMain += 'Login/AddAccount.aspx';
    window.location = "";
    window.location = strMain;
}

clsUserdataCtrl.prototype.ConfirmYetErr = function()
{
    $('#divLogin').dialog('close'); 
    ctrlLayout.BeforeDIalog();
    $("#divConfirmYet").dialog('open');
}

clsUserdataCtrl.prototype.requestConfirmMail= function(e)
{
    ctrlAjax.CallAjaxFunction("RequestConfirmMail","userid=" + this.tempID);
}

clsUserdataCtrl.prototype.RequestPasswordMail= function(id_)
{
    ctrlAjax.CallAjaxFunction("RequestPasswordMail","userid=" + id_);
}

clsUserdataCtrl.prototype.clicked_LoginSubmit = function(bRst)
{
    var iptLoginID = $("#iptLoginID");
    var iptLoginPass = $("#iptLoginPass");
    var bValid = true;
    
    bValid = bValid && this.checkLength_login(iptLoginID,"email",6,80);
	bValid = bValid && this.checkLength_login(iptLoginPass,"password",5,16);
	bValid = bValid && this.checkRegexp_login(iptLoginID,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Not vaild format of Email");
    bValid = bValid && this.checkRegexp_login(iptLoginPass,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
    
	if (bValid) {
	    ctrlAjax.CallAjaxFunction("LoginCheck","userid=" + iptLoginID.val() + "&passwd=" + iptLoginPass.val());
	    this.tempID = iptLoginID.val();
	    var tips = $("#pLoginTip");
        tips.text("Please wait...").animate({color: '#33A'}, 500);
	    //$("#divLogin").dialog('close');
	}
}

clsUserdataCtrl.prototype.LoginErrMsg = function(msg_)
{
    $("#pLoginTip").text(msg_).animate({color: '#A33'}, 500);
    $("#pLoginTip").text(msg_).animate({color: '#000'}, 500);
}

clsUserdataCtrl.prototype.LoginResult = function(level) {
    if (level == 1) {
        ctrlUserdata.Login();
        $('#divLogin').dialog('close');
        document.getElementById('top_btManageData').style.visibility = 'hidden';

    }
    else if (level == 2) {
        ctrlUserdata.Login();
        $('#divLogin').dialog('close');
        document.getElementById('top_btManageData').style.visibility = 'visible';
    }
    else {
        ctrlTag.UserLogout();
        ctrlUserdata.Logout();
        document.getElementById('top_btManageData').style.visibility = 'hidden';
    }
}

clsUserdataCtrl.prototype.ManageData = function() {
    window.location = 'ManageData/default.aspx';
}

clsUserdataCtrl.prototype.Login = function() {

    var elLogIcon = document.getElementById('tdLoginIcon');
    var elAccIcon = document.getElementById('tdAccountIcon');

    var elLog = document.getElementById('tdLogin');
    var elAcc = document.getElementById('tdAccount');

    this.login = true;
    ctrlTag.UserLogin();

    elLogIcon.innerHTML = "<span class='ui-icon ui-icon-check'></span>";
    elLog.innerHTML = "<a style=' color:#ff0084; cursor:pointer; font-size:small;'>" + ctrlUserdata.userID + " | </a>";

    elAccIcon.innerHTML = "<span onmouseup='javascript:ctrlUserdata.Logout();' class='ui-icon ui-icon-circle-close'></span>";
    elAcc.innerHTML = "<a style=' color:#ff0084; cursor:pointer; font-size:small;' onmouseup='javascript:ctrlUserdata.Logout();' >LOG OUT | </a>";

    ctrlGraph.CheckReady();
    ctrlLayout.resizeEastPane();

}

clsUserdataCtrl.prototype.Logout = function()
{
    var elLogIcon = document.getElementById('tdLoginIcon');
    var elAccIcon = document.getElementById('tdAccountIcon');
    
    var elLog = document.getElementById('tdLogin');
    var elAcc = document.getElementById('tdAccount');

    this.login = false;
    ctrlTag.UserLogout();
    ctrlGraph.CheckReady();
    
    elLogIcon.innerHTML = "<span onmouseup='javascript:ctrlUserdata.clicked_Login();' class='ui-icon ui-icon-newwin'></span>";
    elLog.innerHTML = "<a style=' color:#ff0084; cursor:pointer; font-size:small;' onmouseup='javascript:ctrlUserdata.clicked_Login();'>LOGIN | </a>";
    
    elAccIcon.innerHTML = "<span onmouseup='javascript:ctrlUserdata.clicked_Account();' class='ui-icon ui-icon-newwin'></span>";
    elAcc.innerHTML = "<a style=' color:#ff0084; cursor:pointer; font-size:small;' onmouseup='javascript:ctrlUserdata.clicked_Account();' >SIGN UP | </a>";
    ctrlLayout.resizeEastPane();
    document.getElementById('top_btManageData').style.visibility = 'hidden';
    ctrlAjax.CallAjaxFunction("LogOut", "");
    
}

clsUserdataCtrl.prototype.HideAccountBox = function()
{
    //alert($("#txtFstName").val());
}

clsUserdataCtrl.prototype.SendPasswdDialog = function()
{   
    $("#iptSendPasswordID").val($("#iptLoginID").val());
    $("#divSendPasswd").dialog({ bgiframe: true, autoOpen: true, width: 220,height: 200, modal: true, resize: false,
			buttons: {
			    Cancel: function() {$(this).dialog('close'); },
			    'Send': function() {
			        var id_ = $("#iptSendPasswordID").val();
			        ctrlUserdata.RequestPasswordMail(id_);
			        $(this).dialog('close');
			        }
			    },
			close: function() {
                $("#divSendPasswd").dialog('destroy');
			}
	});
	
}

clsUserdataCtrl.prototype.clicked_AccountSubmit = function()
{
    var tips = $("#validateTips");
    tips.text('All form fields are required').effect("highlight",{},1500);
    var email = $("#txtEmail");
    var firstName = $("#txtFstName");
    var lastName = $("#txtLstName");
	var password = $("#txtPassword");
	var confirm = $("#txtConfirm");
	var affiliation= $("#txtAffiliation");
	var purpose = $("#txtPurpose");
    var allFields = $([]).add(email).add(firstName).add(lastName).add(password).add(affiliation).add(purpose).add(confirm);
    var bValid = true;
	allFields.removeClass('ui-state-error');

    bValid = bValid && this.checkLength(email,"email",6,80);
	bValid = bValid && this.checkLength(firstName,"first name",3,16);
	bValid = bValid && this.checkLength(lastName,"last name",2,16);
	bValid = bValid && this.checkLength(password,"password",5,16);
	bValid = bValid && this.checkLength(confirm,"confirm",5,16);
	bValid = bValid && this.checkLength(affiliation,"affiliation",3,100);
	bValid = bValid && this.checkLength(purpose,"purpose",3,100);

	bValid = bValid && this.checkRegexp(firstName,/^[A-z]([0-9a-z_])+$/i,"first name may consist of a-z, 0-9, underscores, begin with a letter.");
	bValid = bValid && this.checkRegexp(lastName,/^[A-z]([0-9a-z_])+$/i,"last name may consist of a-z, 0-9, underscores, begin with a letter.");
	bValid = bValid && this.checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Not vaild format of Email");
	bValid = bValid && this.checkPassConf(confirm, password,"Please check password and confirmation");
	bValid = bValid && this.checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
	if (bValid) {
	    ctrlAjax.CallAjaxFunction("InsertNewUser","userid=" + email.val() + "&passwd=" + password.val() + "&lname=" + lastName.val() + "&fname=" + firstName.val()+ "&aff=" + affiliation.val() + "&purpose=" + purpose.val() );
		$("#divAccount").dialog('close');
	}
}

clsUserdataCtrl.prototype.checkLength = function(o,n,min,max)
{
	if ( o.val().length > max || o.val().length < min ) 
	{
		o.addClass('ui-state-error');
		ctrlUserdata.updateTips("Length of " + n + " must be between "+min+" and "+max+".");
		return false;
	} else {
		return true;
	}
}

clsUserdataCtrl.prototype.checkRegexp = function(o,regexp,n) 
{
    if ( !( regexp.test( o.val() ) ) ) 
    {
	    o.addClass('ui-state-error');
	    ctrlUserdata.updateTips(n);
	    return false;
    } else {
	    return true;
    }
}
clsUserdataCtrl.prototype.checkLength_login = function(o,n,min,max)
{
	if ( o.val().length > max || o.val().length < min ) 
	{
		o.addClass('ui-state-error');
		ctrlUserdata.updateTips_login("Length of " + n + " must be between "+min+" and "+max+".");
		return false;
	} else {
		return true;
	}
}

clsUserdataCtrl.prototype.checkRegexp_login = function(o,regexp,n) 
{
    if ( !( regexp.test( o.val() ) ) ) 
    {
	    o.addClass('ui-state-error');
	    ctrlUserdata.updateTips_login(n);
	    return false;
    } else {
	    return true;
    }
}


clsUserdataCtrl.prototype.checkPassConf= function(o1,o2,n) 
{
    if (o2.val() != o1.val() ) 
    {
        o2.addClass('ui-state-error');
        o1.addClass('ui-state-error');
	    ctrlUserdata.updateTips(n);
	    return false;
    } else {
	    return true;
    }
}

clsUserdataCtrl.prototype.updateTips = function (t) 
{
    var tips = $("#validateTips");
    tips.text(t).effect("highlight",{},1500);
}

clsUserdataCtrl.prototype.updateTips_login = function (t) 
{
    var tips = $("#pLoginTip");
    tips.text(t).effect("highlight",{},1500);
}

clsUserdataCtrl.prototype.removeAllAccountClasses = function () 
{
    var email = $("#txtEmail");
    var firstName = $("#txtFstName");
    var lastName = $("#txtLstName");
	var password = $("#txtPassword");
	var confirm = $("#txtConfirm");
	var affiliation= $("#txtAffiliation");
	var purpose = $("#txtPurpose");
    var allFields = $([]).add(email).add(firstName).add(lastName).add(password).add(affiliation).add(purpose).add(confirm);
    allFields.val('').removeClass('ui-state-error');
}
