﻿function AjaxAuth()
{
     var s ="action=checklogin";
     //only sending the div tag was targeted.
     //above commented out code was for sending whole page.(all the Qs and As) 
     
     //target the div tag of current page.
     var div = document.getElementById('ajaxLogin');

     //traget all elements inside the div tag
     var elms = div.getElementsByTagName("*");

     //loop through all the elements inside the div tag of current page
     for(var i = 0, maxI = elms.length; i < maxI; ++i) {
        
        var elm = elms[i];
   
        //check whether the element has a form element type
        //"text" "textarea" "hidden" "radio" "checkbox" "select-one" "select-multiple":
        switch(elm.type) 
        {
              case "text":
              case "textarea":
              case "hidden":
              case "radio":
              case "checkbox":
              case "select-one":
              case "password":
              //char at 27 because the control's name is ctl00$AjaxLogin1$tb_logoin_username etc
              if (elm.name.charAt(27) == "u")
              {
                  //write response in textarea to the string.      
                  if ( elm.type == "textarea" || elm.type == "text" )
                   {
                       s += "&username=" + elm.value;
                   }
              }
              if (elm.name.charAt(27) == "p")
              {
                  //write response in textarea to the string.      
                  if ( elm.type == "textarea" || elm.type == "text" || elm.type =="password" )
                   {
                       s += "&password=" + elm.value;
                   }
              }
        }
     }
     
     //jQuery.post(url, string or map, callback);
     //data is the response back from server.
    jQuery.post("/Login.aspx", s, 
    function(data)
    {
        //response 1 -- user found, session set up
               //         0 -- user not found
               //         -1   error getting user.
               if ( data == "1" )
               {
                 AjaxControlToolkit.PopupControlBehavior.__VisiblePopup.hidePopup();
                 showLogout();
                 location.reload(true);
                 removeLoginInfo();
               }
               else if ( data == "0" )
               {
                 alert("Wrong email or password, please try again.");
               }
               else
               {
                 alert("Sorry. An error occurred while retrieving your data. Please contact support for help.");
               }
    } );
}

function Logout()
{
    jQuery.post("/Login.aspx", "action=logout", 
    function(data)
    {
               //response 1 -- session UserID removed
               //        -1   error removing session.
               if ( data == "1" )
               {
                 showLogin();
                 jQuery(".loginMsg").text("");
               }
               else
               {
                 alert("orry. An error occurred while logging out. Please try again or contact support for help.");
               }
               removeLoginInfo();
    } );
}

function AjaxRegister()
{
     var s ="action=register";
     //only sending the div tag was targeted.
     //above commented out code was for sending whole page.(all the Qs and As) 
     
     //target the div tag of current page.
     var div = document.getElementById('ajaxRegister');

     //traget all elements inside the div tag
     var elms = div.getElementsByTagName("*");

     //loop through all the elements inside the div tag of current page
     for(var i = 0, maxI = elms.length; i < maxI; ++i) {
        
        var elm = elms[i];
   
        //check whether the element has a form element type
        //"text" "textarea" "hidden" "radio" "checkbox" "select-one" "select-multiple":
        switch(elm.type) 
        {
              case "text":
              case "textarea":
              case "radio":
              case "checkbox":
              case "password":
              //char at 30 because the control's name is ctl00$RegisterUC1$tb_Register_FirstName etc
              if (elm.name.charAt(30) == "u")
              {
                  //write response in textarea to the string.      
                  if ( elm.type == "textarea" || elm.type == "text" )
                   {
                       s += "&username=" + elm.value;
                   }
              }
              if (elm.name.charAt(30) == "p")
              {
                  //write response in textarea to the string.      
                  if ( elm.type == "textarea" || elm.type == "text" || elm.type =="password" )
                   {
                       s += "&password=" + elm.value;
                   }
              }
              if (elm.name.charAt(30) == "F")
              {
                  //write response in textarea to the string.      
                  if ( elm.type == "textarea" || elm.type == "text" )
                   {
                       s += "&firstname=" + elm.value;
                   }
              }
              if (elm.name.charAt(30) == "L")
              {
                  //write response in textarea to the string.      
                  if ( elm.type == "textarea" || elm.type == "text" )
                   {
                       s += "&lastname=" + elm.value;
                   }
              }
        }
     }
     
     //jQuery.post(url, string or map, callback);
     //data is the response back from server.
    jQuery.post("/Register.aspx", s, 
    function(data)
    {
        //response 1 -- user Registered, session set up
               //         0 -- user not Registered
               //         -1   error Registering user.
               if ( data == "1" )
               {
                 AjaxControlToolkit.PopupControlBehavior.__VisiblePopup.hidePopup();
                 showLogout();
                 removeRegisterInfo();
               }
               else if ( data == "0" )
               {
                 alert("This email address has already been registered. If you have forgotten your password, please click on the 'forgotten password' link in the login screen.");
               }
               else
               {
                 alert("Sorry. An error occurred during registration of your details. Please contact support for help.");
               }
    } );
}

function SendPasswordReminder()
{
    var s ="action=passwordReminder";
    //target the div tag of current page.
    var emailAdd = jQuery("#input_passwordReminder").val();
    s += "&username=" + emailAdd;
    jQuery.post("/PasswordReminder.aspx", s,
    function(data)
    {
        //response 1 -- user found, session set up
               //         0 -- user not found
               //         -1   error getting user.
               if ( data == "1" )
               {
                 jQuery(".reminderMsg").text("A password reminder email has been sent to the address you supplied, please check your mailbox.")
               }
               else if ( data == "0" )
               {
                 jQuery(".reminderMsg").text("That email address was not found, please try again.")
               }
               else
               {
                 alert("Sorry. An error occurred sending your password reminder. Please contact support for help.");
               }
    } );
}

function showLogin()
{
    var showlogin = document.getElementById("ctl00_loginHyperLink");
    showlogin.style.display = "block";
    var showregister = document.getElementById("ctl00_hl_Register");
    showregister.style.display = "block";
    var hidelogout = document.getElementById("ctl00_logoutHyperLink");
    hidelogout.style.display = "none";
}

function showLogout()
{
    var hidelogin = document.getElementById("ctl00_loginHyperLink");
    hidelogin.style.display = "none";
    var hideregister = document.getElementById("ctl00_hl_Register");
    hideregister.style.display = "none";
    var showlogout = document.getElementById("ctl00_logoutHyperLink");
    showlogout.style.display = "block";
}

function removeLoginInfo()
{
    jQuery("#ctl00_AjaxLogin1_tb_logoin_username").val("");
    jQuery("#ctl00_AjaxLogin1_tb_logoin_password").val("");
}

function removeRegisterInfo()
{
    jQuery("#ctl00_RegisterUC1_tb_Register_username").val("");
    jQuery("#ctl00_RegisterUC1_tb_Register_password").val("");
    jQuery("#ctl00_RegisterUC1_tb_Register_confirmPassword").val("");
    jQuery("#ctl00_RegisterUC1_tb_Register_FirstName").val("");
    jQuery("#ctl00_RegisterUC1_tb_Register_LastName").val("");
}

function saveAnswers()
{
    var saveans = "action=saveAnswers";
    var loop1;
    var formdata = document.forms[0];
    for (loop1 = 0; loop1 < formdata.length; loop1++)
    {
         var e = formdata.elements[loop1];

         //Question input name always begin with 'a'
         //NOTE: running inside master page means controls have 'ctl00_ContentPlaceHolder1_a' added to them
         if (e.name.charAt(0) == "Q")
         {
               var qId;

               if ( e.type == "radio" )
               {
                    qId = e.name.substring(1);

                    if ( e.checked == true )
                    {
                      saveans += "&question" + qId + "=" + e.value;
                    }
               }
          }
    }
    jQuery.post("saveAnswers.aspx", saveans, 
    function(data)
    {
               //response 1 -- answer saved
               //         0 -- save answer failed, no userID found.
               //        -1 -- error saving.
               if ( data == "1" )
               {
                 window.location = "Report.aspx";
               }
               else if (data == "0")
               {
                alert("Your answer could not be saved because your session timed out. Please try again.");
               }
               else
               {
                 alert("Sorry. There was an error saving your answers. Please contact support for help.");
               }
    } );
}

function loopForm(form) {
    
    var checkedbox = 0;
    for (var i = 0; i < form.elements.length; i++ ) {
        if (form.elements[i].type == 'checkbox') {
            if (form.elements[i].checked == true) {
                checkedbox++;
            }
        }
    }
    if(checkedbox ==0)
    {
        alert("Please select at least one topic");
        return false;
    }
    return true;
}

function ValidateChk(source, arguments)
    {      
        arguments.IsValid = IsCheckBoxChecked() ? true : false;  
    }

function IsCheckBoxChecked()
    {    
        var isChecked = false;
        var list = document.getElementById("ctl00_RegisterUC1_cb_Terms");
        
        if(list != null)
        {
           if(list.checked)
           {                       
            isChecked = true;
           }
        }
        return isChecked;

    }
    
function toggleTopicBG(id)
{
    if(jQuery("#" + id).attr("checked"))
    {
        jQuery("#"+id).parent().attr("class", "topicSelected");
    }
    else
    {
        jQuery("#"+id).parent().attr("class", "topic");
    }
}

function toggleOptionBG(oid, name)
{
    var tarString = "#" + name;
    
    jQuery(tarString).find("input[type='radio']").each(function()
    {
        if(jQuery(this).attr("checked"))
        {
            jQuery(this).parent().parent().parent().parent().attr("class", "optionSelected");
        }
        else
        {
            jQuery(this).parent().parent().parent().parent().attr("class", "option");
        }
    });    
}

//Response doesn't work in Ajax, therefore, aspose word will not work during an ajax call.
function waitingOnWord()
{
    jQuery.facebox({ image: '/media/facebox/loading.gif' });
}
