/**
 * REQUIRES HS.JQUERY
*/

function cwpLogout()
{
  hsq.post('/shared/v1/oe-client-portal/logout.jsp',
            null, 
            cwpOnLogout);
}

function cwpOnLogout(data, textStatus, XMLHttpRequest)
{
  // Redirect to home page after logging out.
  window.location.href = "/";
}

function cwpUpdateProfile()
{
  //check the form
  var objEmail = hsq("#currentEmail");
  var objNewEmail =  hsq("#newEmail");
  var objVerfNewEmail =  hsq("#newEmailVerify");
  var objCurrPwd =  hsq("#currentPassword");
  var objNewPwd =  hsq("#newPassword");
  var objVerfNewPwd =  hsq("#newPasswordVerify");

  if(objEmail.val().length == 0)
  {
    alert("Please input the Current Email.");
    return;
    }

  if(objCurrPwd.val().length == 0)
  {
    alert("Please input the Current Password.");
    return;
  }
  if(objNewEmail.val() != objVerfNewEmail.val())
  {
    alert("Please enter the same email address in both new email fields.");
    objNewEmail.val("");
    objVerfNewEmail.val("");
    return;
  }

  if(objNewPwd.val() != objVerfNewPwd.val())
  {
    alert("Please enter the same password in both new password fields.");
    objNewPwd.val("");
    objVerfNewPwd.val("");
    return;
  }

   // POST the profile data.
   hsq.post("/shared/v1/oe-client-portal/submit-profile.jsp", 
            hsq("#frmUpdateProfile").serialize(), 
            cwpProcessUpdateProfileResponse,
            "text"
   );
   
   return false;
}

function cwpProcessUpdateProfileResponse(result) {
  if(result != null && result.length > 0)
  {

    if (result == 'OK')
    {
      hsq("#newEmail").val("");
      hsq("#newEmailVerify").val("");
      hsq("#newPassword").val("");
      hsq("#newPasswordVerify").val("");
      
      alert('Profile successfully updated.');
    }
    else
    {
      // Problem with submitted fields.
      alert(result);
    }
  }
}

function cwpResetPwd()
{
            var prodId = document.getElementById("ProductID").value;
            var url="https://admainnew.morningstar.com/register/register.aspx?lang=ENU&page=ResetPwd&PID=" + prodId + "&downloadable=&rnd=" + Math.random();
            OpenWin(url,0,0,601,515,"AwsResetPwd");
}


function OpenWin(url, x, y, width, height,target)
      {
      //var option = "center:yes;status:no;dialogWidth:" + width + "px;dialogHeight:" + height + "px;dialogLeft=" + x + "px;dialogTop=" + y + "px";
      //window.showModalDialog(url, 0, option);
      if ( typeof(target) == "undefined" || target == null )
      target = "mstarawsoffice";

      var viewWidth2 = screen.availWidth-15;
      var viewHeight2 = screen.availHeight-30;
      var w = width - 0;
      var h = height - 0;
      if ( width <= 0 )
      w = viewWidth2;
      if ( height <= 0 )
    h = viewHeight2;
    var scroll = "resizable=yes,scrollbars";
    w = Math.min(w, viewWidth2);
    h = Math.min(h, viewHeight2);

    var prop = "width=";
    prop += w +",";
    prop += "height=";

    var strPosition = "screenX=" + x + ",screenY=" + y + ",left=" + x + ",top=" + y + ",";
    prop += h + "," + strPosition;
    prop += scroll;
    var newWin = window.open(url,target,prop);
    newWin.focus();
}

function cwpUpdateProfileBoxClick(event)
{
  if(event != null && typeof(event) == 'object' && typeof(event.keyCode) == 'number' && event.keyCode == 34) 
    event.returnValue=false;
}

