﻿//******************************************************************************
// Author: Riccardo La Rosa
// Date: 12/27/2006
// File: RunEzAccount.js
// Description: Utility functions used during registration, login or any account related activity
//******************************************************************************


//*******************************************
// Function: CheckNicknameAvailability
// Description:
// Author: Riccardo La Rosa
//*******************************************
function CheckNicknameAvailability(nickname) {

    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/CheckNicknameAvailability.aspx";
    var params = "Nickname="+nickname;

    //send the request and update the 'nickname' div
    var ajax = new Ajax.Updater(
            'nickname',
            url,
            {method: 'get',parameters: params, evalScripts:true}
    );
    setTimeout('CheckNicknameDiv()', 2000);
}

function CheckNicknameDiv()
{
    var nicknameErr = $('nickname').innerHTML;
    if (nicknameErr != '')
        $('LoginStatus1_txtNickname').value = '';
}
//*******************************************
// Function: isNumberKey
// Description: check if a number was typed
// Author: Mihir
//*******************************************
function isNumberKey(evt)
{
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
}


//*******************************************
// Function: CheckNicknameAvailability
// Description: check's in 
// Author: Mihir
//*******************************************
function CheckMobileAvailability(ddlcountry, mobileField) 
{
    $('ccRequired').style.display="none";
    //$('MobileIncorrect').style.display="none";  
    $('mobilenumber').innerHTML = ""
	var ddlIndex  = ddlcountry.selectedIndex
	var countryCode = ddlcountry.options[ddlIndex].value
	var mobile = mobileField.value;
		
    if(ddlIndex == 0 && mobile == "")
    {
       // do nothing       
    }
    else if(mobile != "" && ddlIndex == 0)
    {
       // need to select country..erase number
       $('ccRequired').style.display="block";
       mobileField.value = "";
       
    }else if(mobile != "" && ddlIndex > 0)     
    {
         regMobile = new RegExp(/^\d{1,3}\-\d{10}$/);
         mobile = countryCode + '-' + mobile;
         if (!regMobile.test(mobile)) {
              $('MobileIncorrect').style.display="block";  
              mobileField.value="";            
         }else
         {
            //prepare the ajax request
            var url = "/RunEasy/AjaxRequests/CheckMobileNumberAvailability.aspx";
            var params = "Mobile="+mobile+ "&fieldName=" + mobileField.id;

            //send the request and update the 'mobilenumber' div
            var ajax = new Ajax.Updater(
                    'mobilenumber',
                    url,
                    {method: 'get',parameters: params, evalScripts:true}
            );
            
        }
     }
}

//*******************************************
// Function: CheckEmailAvailability
// Description:
// Author: Riccardo La Rosa
//*******************************************
function CheckEmailAvailability(email) {

    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/CheckEmailAvailability.aspx";
    var params = "Email="+email;

    //send the request and update the 'email' div
    var ajax = new Ajax.Updater(
            'email',
            url,
            {method: 'get',parameters: params, evalScripts:true}
    );
    setTimeout('CheckEmailDiv()', 3000);
}

function CheckEmailDiv()
{
    var emailErr = $('email').innerHTML;
    if (emailErr != '')
        $('LoginStatus1_txtEmailCreate').value = '';
}

//*******************************************
// Function: GetProfileInfo
// Description:
// Author: Riccardo La Rosa
//*******************************************
function GetProfileInfo(userid)
{
    $('ProfileDetails').innerHTML = "Loading...";
    var url = "ajaxrequests/GetProfileInfo.aspx";
    var params = "userid="+userid;
    
    //send the req and update the Profile div
    var ajax = new Ajax.Updater('ProfileDetails', url, {method: 'post', parameters: params, evalScripts:true});
    
} 

//*******************************************
// Function: SubmitForgotUoP
// Description:
// Author: Riccardo La Rosa
//*******************************************
function SubmitForgotUorP(email)
{
    if (!Page_ClientValidate('Forgot'))
        return;
    $('ForgotUorPResult').innerHTML = '';
    var url = "/RunEasy/AjaxRequests/ProcessForgotUorP.aspx";
    var params = "email="+email;
    
    //send the req and update the ForgotUorPResult div
    var ajax = new Ajax.Updater('ForgotUorPResult', url, {method: 'get', parameters: params, evalScripts:true});
    new Effect.Highlight('ForgotUorPResult', {duration:3} );
    
}

//*******************************************
// Functions to Change Avatar
// Description:
// Author: Riccardo La Rosa
//*******************************************
function ShowChangeAvatarForm() {

    $('avatarimage').style.display="none";
    $('hlChangeAvatar').style.display="none";
    $('hlCancelAvatar').style.display="block";
    $('avatarupload').style.display="block";
}

function HideChangeAvatarForm() {

    $('avatarimage').style.display="block";
    $('hlChangeAvatar').style.display="block";
    $('hlCancelAvatar').style.display="none";
    $('avatarupload').style.display="none";
}

function ShowAvatarAfterUpload(imageId) {
    
    var par = window.parent.document;
    var avatarimage = par.getElementById('avatarimage');
    avatarimage.innerHTML = '';
    if (imageId == -1) //something bad happened
    {
        var err_msg = par.createElement('span');
        err_msg.innerHTML = gMsgGenericAjaxError ;
        avatarimage.appendChild(err_msg);
    }
    else// all is good
    {
        var image_new = par.createElement('img');
        image_new.src = '/RunEasy/userpictures/Avatars/'+imageId+'.jpg';
        avatarimage.appendChild(image_new);
    }
}

//*******************************************
// Functions to Change Nickname
// Description:
// Author: Riccardo La Rosa
//*******************************************
function ShowChangeNicknameForm(txtboxName) {

    $('h2Nickname').style.display="none";
    $('ChangeNicknameTextbox').style.display="block";
    $(txtboxName).focus();
}

function HideChangeNicknameForm() {

    $('h2Nickname').style.display="block";
    $('ChangeNicknameTextbox').style.display="none";
    $('divErrorNickname').hide();
}

function UpdateNickname(nickname)
{
    // all is good, remove the error messages
    $('divErrorNickname').innerHTML = '';
    
    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/ChangeUsername.aspx";
    var params = "nickname="+nickname;

    //send the request and update the different divs
    var ajax = new Ajax.Request(url,
            {method: 'post',
            parameters: params,
            onSuccess: function(response) {
                    if (response.responseText.strip() == '') //all is good
                    {
                        HideChangeNicknameForm();
                        $('h2Nickname').innerHTML = nickname;
                        new Effect.Highlight($('h2Nickname'),{duration:3});
                     } else {
                        // there was an error
                        $('divErrorNickname').innerHTML = response.responseText;
                        $('divErrorNickname').show();
                     }
                    },
            onFailure: function(response) {
                    $('divErrorNickname').innerHTML = gMsgGenericAjaxError;
                    $('divErrorNickname').show();}
           });
}
//*******************************************
// Functions to Change Mobile
// Description:
// Author: Riccardo La Rosa
//*******************************************
function ShowChangeMobileForm(txtboxName) {

    $('ChangeMobileLink').style.display="none";
    $('ChangeMobileTextbox').style.display="";
    $(txtboxName).focus();
}

function HideChangeMobileForm() {

    $('ChangeMobileLink').style.display="";
    $('ChangeMobileTextbox').style.display="none";
    $('divErrorMobile').hide();
}

function UpdateMobile(mobile)
{
    // all is good, remove the error messages
    $('divErrorMobile').innerHTML = '';
    
    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/ChangeMobile.aspx";
    var params = "mobile="+mobile;

    //send the request and update the different divs
    var ajax = new Ajax.Request(url,
            {method: 'post',
            parameters: params,
            onSuccess: function(response) {
                    if (response.responseText.strip() == '') //all is good
                    {
                        HideChangeMobileForm();
                        $('ChangeMobileLink').innerHTML = mobile;
                        new Effect.Highlight($('ChangeMobileLink'),{duration:3});
                     } else {
                        // there was an error
                        $('divErrorMobile').innerHTML = response.responseText;
                        $('divErrorMobile').show();
                     }
                    },
            onFailure: function(response) {
                    $('divErrorMobile').innerHTML = gMsgGenericAjaxError;
                    $('divErrorMobile').show();}
           });
}

//*******************************************
// Functions to Change Email
// Description:
// Author: Riccardo La Rosa
//*******************************************
function ShowChangeEmailForm(txtboxName) {

    $('ChangeEmailLink').style.display="none";
    $('ChangeEmailTextbox').style.display="";
    $(txtboxName).focus();
}

function HideChangeEmailForm() {

    $('ChangeEmailLink').style.display="";
    $('ChangeEmailTextbox').style.display="none";
    $('divErrorEmail').hide();
}

function UpdateEmail(email)
{
    // all is good, remove the error messages
    $('divErrorEmail').innerHTML = '';
    
    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/ChangeEmail.aspx";
    var params = "email="+email;

    //send the request and update the different divs
    var ajax = new Ajax.Request(url,
            {method: 'post',
            parameters: params,
            onSuccess: function(response) {
                    if (response.responseText.strip() == '') //all is good
                    {
                        HideChangeEmailForm();
                        $('ChangeEmailLink').innerHTML = email;
                        new Effect.Highlight($('ChangeEmailLink'),{duration:3});
                     } else {
                        // there was an error
                        $('divErrorEmail').innerHTML = response.responseText;
                        $('divErrorEmail').show();
                     }
                    },
            onFailure: function(response) {
                    $('divErrorEmail').innerHTML = gMsgGenericAjaxError;
                    $('divErrorEmail').show();}
           });
}

//*******************************************
// Functions to Change OpenId
// Description:
// Author: Craig Andrews
//*******************************************
function ShowChangeOpenIdForm(txtboxName) {

    $('ChangeOpenIdLink').style.display="none";
    $('ChangeOpenIdTextbox').style.display="";
    $(txtboxName).focus();
}

function HideChangeOpenIdForm() {

    $('ChangeOpenIdLink').style.display="";
    $('ChangeOpenIdTextbox').style.display="none";
    $('divErrorOpenId').hide();
}

function UpdateOpenId(OpenId)
{
    // all is good, remove the error messages
    $('divErrorOpenId').innerHTML = '';
    
    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/ChangeOpenId.aspx";
    var params = "OpenId="+OpenId;

    //send the request and update the different divs
    var ajax = new Ajax.Request(url,
            {method: 'post',
            parameters: params,
            onSuccess: function(response) {
                    if (response.responseText.strip() == '') //all is good
                    {
                        HideChangeOpenIdForm();
                        $('ChangeOpenIdLink').innerHTML = OpenId;
                        new Effect.Highlight($('ChangeOpenIdLink'),{duration:3});
                     } else {
                        // there was an error
                        $('divErrorOpenId').innerHTML = response.responseText;
                        $('divErrorOpenId').show();
                     }
                    },
            onFailure: function(response) {
                    $('divErrorOpenId').innerHTML = "sorry dude! something went bad, please try later!";
                    $('divErrorOpenId').show();}
           });
}

//*******************************************
// Functions to Change Pwd
// Description:
// Author: Riccardo La Rosa
//*******************************************
function ShowChangePwdForm(txtboxName) {

    $('ChangePwdLink').style.display="none";
    $('ChangePwdTextboxes').style.display="";
    $(txtboxName).focus();
}

function HideChangePwdForm() {

    $('ChangePwdLink').style.display="";
    $('ChangePwdTextboxes').style.display="none";
    $('divErrorPwd').hide();
}

function UpdatePwd(pwd, confirmpwd)
{
    // all is good, remove the error messages
    $('divErrorPwd').innerHTML = '';
    
    //check if pwds are blank
    if (pwd.length < gMinPasswordLength)
    {
        $('divErrorPwd').innerHTML = '<span style="color:red">'+ gMsgMinPasswordLength  +'</span>';
        $('divErrorPwd').show();
        return;
    }
    //check if pwds are the same
    if (pwd != confirmpwd)
    {
        $('divErrorPwd').innerHTML = '<span style="color:red">'+ gMsgPasswordsDontMatch +'</span>';
        $('divErrorPwd').show();
        return;
    }
    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/ChangePassword.aspx";
    var params = "pwd="+pwd;

    //send the request and update the different divs
    var ajax = new Ajax.Request(url,
            {method: 'post',
            parameters: params,
            onSuccess: function(response) {
                    if (response.responseText.strip() == '') //all is good
                    {
                        HideChangePwdForm();
                        $('ChangePwdLink').innerHTML = '******';
                        new Effect.Highlight($('ChangePwdLink'),{duration:3});
                     } else {
                        // there was an error
                        $('divErrorPwd').innerHTML = response.responseText;
                        $('divErrorPwd').show();
                     }
                    },
            onFailure: function(response) {
                    $('divErrorPwd').innerHTML = gMsgGenericAjaxError;
                    $('divErrorPwd').show();}
           });
}

//********************************************
// Functions to Change MyWebsite
// Description:
// Author: Riccardo La Rosa
//********************************************
function UpdateMyWebsite(myWebsite)
{
    // all is good, remove the error messages
    $('divErrorMyWebsite').innerHTML = '';
    
    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/ChangeMyWebsite.aspx";
    var params = "myWebsite="+myWebsite;

    //send the request and update the different divs
    var ajax = new Ajax.Request(url,
            {method: 'post',
            parameters: params,
            onSuccess: function(response) {
                    if (response.responseText.strip() == '') //all is good
                    {
                        $('ChangeMyWebsiteTextbox').style.display="none";
                        $('ChangeMyWebsiteLink').innerHTML = "<a href='" + myWebsite + "' class='external' target='_blank'>" + myWebsite + "</a>";
                        new Effect.Highlight($('ChangeMyWebsiteLink'),{duration:3});
                     } else {
                        // there was an error
                        $('divErrorMyWebsite').innerHTML = response.responseText;
                        $('divErrorMyWebsite').show();
                     }
                    },
            onFailure: function(response) {
                    $('divErrorMyWebsite').innerHTML = gMsgGenericAjaxError;
                    $('divErrorMyWebsite').show();}
           });
}

//********************************************
// Functions to Change RunEasyMeaning
// Description:
// Author: Riccardo La Rosa
//********************************************
function UpdateRunEasyMeaning(runEasyMeaning)
{
    // all is good, remove the error messages
    $('divErrorRunEasyMeaning').innerHTML = '';
    
    //prepare the ajax request
    var url = "/RunEasy/AjaxRequests/ChangeRunEasyMeaning.aspx";
    var params = "runEasyMeaning="+runEasyMeaning;

    //send the request and update the different divs
    var ajax = new Ajax.Request(url,
            {method: 'post',
            parameters: params,
            onSuccess: function(response) {
                    if (response.responseText.strip() == '') //all is good
                    {
                        $('ChangeRunEasyMeaningTextbox').style.display="none";
                        $('ChangeRunEasyMeaningLink').innerHTML = runEasyMeaning;
                        new Effect.Highlight($('ChangeRunEasyMeaningLink'),{duration:3});
                     } else {
                        // there was an error
                        $('divErrorRunEasyMeaning').innerHTML = response.responseText;
                        $('divErrorRunEasyMeaning').show();
                     }
                    },
            onFailure: function(response) {
                    $('divErrorRunEasyMeaning').innerHTML = gMsgGenericAjaxError;
                    $('divErrorRunEasyMeaning').show();}
           });
}