﻿// JScript File
// For KGUserProfileControl 

function KGUserProfileControl_ChangePersonalDetailsClick()
{
  // debugger;
  var args = 'action=ChangeDisplayMode|mode=Update';
  KGUserProfileControl_CallbackTrigger(args);
}

function KGUserProfileControl_UpdateClick(_btn)
{
  var hiddenFieldId = _btn.getAttribute("HiddenFieldId");
  var hiddenField = document.getElementById(hiddenFieldId);
  var inputControlIds = eval( '(' + hiddenField.value + ')' );
  
  var args = 'action=Update';
  args += '|FirstName=' + ReadTextBoxValue(inputControlIds.txtFirstName);
  args += '|LastName=' + ReadTextBoxValue(inputControlIds.txtLastName);
  args += '|Email=' + ReadTextBoxValue(inputControlIds.txtEmail);
  args += '|SecurityQuestionIdx=' + ReadDropListIdx(inputControlIds.dropListSecurityQuestions);
  args += '|SecurityAnswer=' + ReadTextBoxValue(inputControlIds.txtSecurityAnswer);
  args += '|GenderIdx=' + ReadDropListIdx(inputControlIds.dropListGender);
  args += '|DOBYearIdx=' + ReadDropListIdx(inputControlIds.dropListDOBYear);
  args += '|DOBMonthIdx=' + ReadDropListIdx(inputControlIds.dropListDOBMonth);
  args += '|DOBDayIdx=' + ReadDropListIdx(inputControlIds.dropListDOBDay);
  args += '|Address1=' + ReadTextBoxValue(inputControlIds.txtAddress1);
  args += '|City=' + ReadTextBoxValue(inputControlIds.txtCity);
  args += '|ZIPCode=' + ReadTextBoxValue(inputControlIds.txtZIPCode);
  args += '|CountryIdx=' + ReadDropListIdx(inputControlIds.dropListCountry);
  
  
  KGUserProfileControl_CallbackTrigger(args);
}

function ReadTextBoxValue(_textBoxId)
{
  // don't throw anything if the control doesn't exist, it might not have been added
  var value;
  if (_textBoxId)
  {
    var tb = document.getElementById(_textBoxId);
    if (tb)
      value = tb.value;
  }
  return value;
}

function ReadDropListIdx(_dropListId)
{
  // don't throw anything if the control doesn't exist, it might not have been added
  var idx;
  if (_dropListId)
  {
    var ddl = document.getElementById(_dropListId);
    if (ddl)
      idx = ddl.selectedIndex;
  }
  return idx;
}

function KGUserProfileControl_CancelUpdateClick()
{
  // debugger;
  var args = 'action=ChangeDisplayMode|mode=Display';
  KGUserProfileControl_CallbackTrigger(args);
}

function KGUserProfileControl_CallbackDone_Wrapped(controlDivID, args, context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  var container = document.getElementById(controlDivID);
  
  if(container != null)
  {
    container.innerHTML = args;
    if (args.indexOf("Exception") != 0 && args.indexOf("timmedout") != 0) // no exception
    {
      // refresh the profile header on the user's own profile page incase the details changed
      if (window.Pfl_KGProfileHeaderControl_CallbackTrigger)
        setTimeout("Pfl_KGProfileHeaderControl_CallbackTrigger('action=repopulate')", 10);
    }
  }
  else
    KGUserProfileControl_CallbackError('Can not find UserProfileForm element.');    
}

function KGUserProfileControl_CallbackError_Wrapped(controlDivID, args, context)
{
  // Here we are dealing with authentication timeout before callback, 
  // the message from the server is 'elogin' signify an error with 
  // authentication. The response will be trimmed to form the args 
  // 'login' here..... 
  if(args == 'login')
  {
    window.location='home.aspx';
  }
  else
  {
    var controlDiv = document.getElementById(controlDivID);
          
    if(controlDiv != null)
      controlDiv.innerHTML = args;
    else
      alert('Callback on KGUserProfileControl has failed: ' + args);
  }
}

/* My Preferences */

function KGMyPrefsControl_CheckBoxClicked(_cb, _preferenceID)
{
  KGMyPrefsControl_DoUpdate(_cb.id, _preferenceID, _cb.checked);
  
  _cb.disabled = true;
}

function KGMyPrefsControl_RadioButtonClicked(_rb, _isTrueOption, _preferenceID)
{
  var enabled = false;
  // enable the preference by clicking the true option (which will uncheck the false option)
  // disable the preference by clicking the false option (which will uncheck the true option)
  if (_isTrueOption && _isTrueOption.toLowerCase() == "true")
    enabled = true;
    
  KGMyPrefsControl_DoUpdate(_rb.id, _preferenceID, enabled)
    
  DisableRadioButtons(_rb, true)    
}

function KGMyPrefsControl_DoUpdate(_ctlID, _preferenceID, _enabled)
{
  var args = 'id=' + _ctlID + '|prefid=' + _preferenceID;
  if(_enabled)
  {
    args += '|enabled=1';
  }
  else
  {
    args += '|enabled=0';
  }
  
  KGMyPrefsControl_CallbackTrigger(args);
}

function DisableRadioButtons(_anyBtn, _disable)
{
  // pass in the id of any radio button so we can get the group name
  var grpName = _anyBtn.name;
  
  var btns = document.getElementsByName(grpName);
  var nBtns = btns.length;
  
  for (var b=0; b<nBtns; b++)
  {
    btn = btns[b];
    btn.disabled = _disable;
  }
}

function KGMyPrefsControl_ProcessCallBackError(_args, _context)
{
  var error = 'Error in KGMyPreferencesControl callback.';
  if(_args.length > 0)
    error += ' ' + _args;
  alert(error);
}

function KGMyPrefsControl_CallbackTrigger_Wrapped(_args, _callbackScript)
{
  var args;

  // Construct the callback arguments
  if(_args != null)
  { 
    args = _args;
    
    if(args.length > 0)
    {
      // trigger the callback
      eval(_callbackScript);
    }
  }
}

function KGMyPrefsControl_CallbackDone(args, context)
{  
  if(args.indexOf('Exception') == 0)
    alert(args);
  else
  {
    var results = args.split('|');
    if(results.length > 2)
    {
      var id = results[0];
      var prefid = results[1];
      var value = results[2];
      
      // search for the control container
      var holder = document.getElementById(id);
      
      if(holder != null && holder.parentNode != null)
      {
        // show check box and hide loading div
        if (holder.type == 'checkbox')
          holder.disabled = false;
        else if (holder.type == 'radio')
          DisableRadioButtons(holder, false);
        
      }//end if(holder ....
      else
      {
        KGMyPrefsControl_ProcessCallBackError('Checkbox holder not found.', '');
      }
    }
  }
}
