﻿
function MyLcnFullToggle(Control, handle, hiddenfield, WidgetRef, StudentRef, url) {
    
    var TriggerImage = document.getElementById(handle);
    var OnOrOff = document.getElementById(hiddenfield) ;
    var SaveParemeters;
    var BoolOnOrOff;

    if (OnOrOff.value == '1') { // already Max

        $('#' + Control + ' .Maximised').removeClass("Maximised").addClass("Minimised");      
        
        TriggerImage.setAttribute("src", "/images/MyLCN/right.gif");
        OnOrOff.value = '0';
        BoolOnOrOff = true;        
    }
    else { //already Min
        
        OnOrOff.value = '1';
        
        $('#' + Control + ' .Minimised').removeClass("Minimised").addClass("Maximised");      
                
        TriggerImage.setAttribute("src", "/images/MyLCN/down.gif");
        BoolOnOrOff = false;
    }
    //alert(HubControlID);

    SaveParemeters = ['StudentRef', StudentRef, 'Minimised', BoolOnOrOff, 'WidgetRef', WidgetRef];
    PageMethod(url, 'SaveMinimised', SaveParemeters);
    

}

function MyLCNRowChange(Direction, CurrentRows, SaveUrl, SaveMethod, Studentref, WidgitId, UpDownControls, totalRecords, Callback) {

    var NoOfRows = parseInt(document.getElementById(CurrentRows).value);
    var NoOfRecords = parseInt(document.getElementById(totalRecords).value);
    var SaveParemeters;
    

    if (Direction == "+" && NoOfRows < 30) {
        //add one to textbox field
        NoOfRows = ++NoOfRows;

        //$('#' + Control + '>tbody>tr:nth-child(' + NoOfRows + ')').show('slow');
        document.getElementById(CurrentRows).value = NoOfRows;

        //setup parameters for ajax
        SaveParemeters = ['StudentRef', Studentref, 'VisibleRows', NoOfRows, 'WidgetRef', WidgitId];

        //ajax request
        PageMethodWithCallback(SaveUrl, SaveMethod, SaveParemeters, Callback);

    } else if (Direction == "-" && NoOfRows > 1) {
        //remove one from visble row value
        NoOfRows = --NoOfRows;

        document.getElementById(CurrentRows).value = NoOfRows;

        //setup parameters for ajax

        SaveParemeters = ['StudentRef', Studentref, 'VisibleRows', NoOfRows, 'WidgetRef', WidgitId];
        //ajax request
        PageMethodWithCallback(SaveUrl, SaveMethod, SaveParemeters, Callback);

    }
}




function PageMethod(Page, Function,  paramArray, InsertDiv) {
 
    var cb = function(resp) {

    if (InsertDiv != undefined) {

            //alert(response.d);
            InsertAjaxResult(resp, InsertDiv);
        }
        else {
            ajaxSuccessfull(resp);
        }
    }

    PageMethodWithCallback(Page, Function, paramArray, cb);
}


//function PageMethodPlusMinus(Page, Function, paramArray, Callback, InsertDiv) {

//    var cb = function(resp) {

//        if (InsertDiv != undefined) {
//            //alert(response.d);
//            InsertAjaxResult(resp, InsertDiv);
//        }
//        else {
//            ajaxSuccessfull(resp);
//        }

//        Callback();
//    }

//    PageMethodWithCallback(Page, Function, paramArray, cb);
//}

function PageMethodWithCallback(Page, Function, paramArray, Callback) {

        //Create list of parameters in  the form:  
     //   {"paramName1":"paramValue1","paramName2":"paramValue2"}  
              var paramList = '';
              if (paramArray.length > 0) {
                for (var i = 0; i < paramArray.length; i += 2) {
                   if (paramList.length > 0) paramList += ',';
                  paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
                }
            }
          paramList = '{' + paramList + '}';

    //Call the page method
          $.ajax({
              type: "POST",
              url: Page + "/" + Function,
              contentType: "application/json; charset=utf-8",
              data: paramList,
              dataType: "json",
              success: function(response) { Callback(response); },

              error: function(xhr, status, error) {

                  var err = eval("(" + xhr.responseText + ")");

                  $("#laErrorReport").html(err.Message);
                  $("#LoginPasswordTextBox").attr("value", "");
                  ToggleDisplay("trError");



                  // Display the specific error raised by the server


              }
              //         function(response) {
              //            if (response == 1) {
              //                ajaxSuccessfull();
              //            }
              //            else {
              //                ajaxFailed(response);
              //            }
              //        }
          });
}

function FirmRowChange(path, method, StudentRef, widgetid, placeholder) {
    
    SaveParemeters = ['StudentRef', StudentRef, 'widgetid', widgetid];

    PageMethod(path, method, SaveParemeters, placeholder);

}



function InsertAjaxResult(response, placeholder) {

    $('#' + placeholder).html(response.d);
  
    if (buAction != undefined) {
        ajaxSuccessfull("UpdatebuAction");
    }
   
}

function SendPassResponse(resp) {
    if (resp.d == "1") {

        $("#laErrorReport").html("You have been sent a password request email");
      
    }
    else
    {
        $("#laErrorReport").html("Please check your email address and try again");

    }
    $("#LoginPasswordTextBox").attr("value", "");
    ToggleDisplay("trError");

}

function LoginResponse(resp) {


    if (resp.d == "LoggedIn") {
        var url = new String(window.location);
        url = url.split("#")[0]
        ;
        if (url.match(/Login.aspx/ig)) {
            window.location = '/MyLCN/';
        }
        else
            window.location.reload();
    }
    else
    {


        var msg;
        switch (resp.d) {

            case "2":
                msg = "Your account has been suspended for the next 30mins due to the number of incorrect password attempts.";
                break;

            case "3":
                msgt = "Your account is suspended please contact please contact support@lcnThinkTank.com.";
                break;
            case "4":
                msg = "Your temporary password has now expired.  Please use the \"Forgotten your password\" link to request a new one.";
                break;
            case "-1": // Password was incorrect
                msg = "Invalid email address or password.";
                break;

        }

        $("#laErrorReport").html(msg);
        $("#LoginPasswordTextBox").attr("value", "");
        ToggleDisplay("trError");
    }

    
    

}


    function ajaxFailed(response)  
   {
       alert(response);
   }



   var buAction;
   var CompleteImage;
   var CompleteImage2;
   var MiniButton;
   
   
   function AjaxAyncAction(x, waitimage, completeimage, _waitimage2, _completeimage2, _minibutton ) {
       buAction = x;
       CompleteImage = completeimage;
       CompleteImage2 = _completeimage2;
       MiniButton = _minibutton;

       $('#' + x).html(waitimage);




       //second image code (ie add/remove from mylc.n)
       if (_waitimage2 != undefined) {
           $('#' + MiniButton).attr('src', _waitimage2);

           if ($('#RemoveButtonWrapper').attr("class") == "HiddenFeatures") {
               $('#RemoveButtonWrapper').attr("class", "");
               $('#' + x).parents("a:first").attr("onclick" , "");
           }
           else {
               $('#RemoveButtonWrapper').attr("class", "HiddenFeatures");
           }
       }
 
       
   }

   var SelectionRef;
   function ajaxSuccessfull(Result) {
       // current url
       var url = new String(window.location);
       url = url.split("#")[0];

       //alert(Result.d);

       if (Result.d != undefined) {

         

           if (Result.d == "Reload") {

               if (SelectionRef != undefined) {
                   //  alert(url.split("#")[0] + "#" + SelectionRef);
                   window.location.href = url + "#ref" + SelectionRef;
                   // window.location = window.location.href;
                   window.location.reload(false);

               }
               else
                   window.location.reload(true);
           }



           if (Result.d == "UpdatebuAction") {
               //$('#' + buAcction).attr("value", CompleteImage);
               $('#' + buAction).html(CompleteImage);
               if (CompleteImage2 != undefined) {
                   $('#' + MiniButton).attr('src', CompleteImage2);
               }

           }




           if (Result.d.split("#")[0] == "UpdatebuActionSwap") {
               //$('#' + buAcction).attr("value", CompleteImage);
               $('#' + buAction).html(CompleteImage);
               if (CompleteImage2 != undefined) {
                   $('#' + MiniButton).attr('src', CompleteImage2);
               }



               $('#' + buAction.split("#")[0]).attr("Selectionref", Result.d.split("#")[1]);

           }


       }
       else
           if (Result == "UpdatebuAction") {
           //$('#' + buAcction).attr("value", CompleteImage);
           $('#' + buAction).html(CompleteImage);
           if (CompleteImage2 != undefined) {
               $('#' + MiniButton).attr('src', CompleteImage2);
           }

       }



   }



function HideControl(StudentRef, widgetid, Control, Header, visible ) {
    //var answer = confirm("Are you sure you wish to close " + Header +"?");
    //if (answer) {

    //alert(Control);
    $('#ControlPlaceholder' + Control).html("");
        
        if (visible == undefined) {
            SaveParemeters = ['StudentRef', StudentRef, 'widgetid', widgetid, 'Visible', 'False']
  

 
          $('#' + widgetid).removeAttr("checked");
      
            
        }
        else {
            SaveParemeters = ['StudentRef', StudentRef, 'widgetid', widgetid, 'Visible', Visible]
        }
    
        PageMethod('/MyLCN/Ajax/SaveSettings.aspx', 'UpdateVisible', SaveParemeters);
    //       
    //        
        
    //}
    //alert(Control);
    }


    function VisibleUpdate() {

        //  alert($(".chBoxes_Vis").size());
        WigdetArray = new Array($(".chBoxes_Vis").size());

        var i = 0;
        $(".chBoxes_Vis").each(function() {

        WigdetArray[i] = new Array(3);
        WigdetArray[i][0] = $(this).attr("id");
        WigdetArray[i][1] = $(this).attr("checked");
        WigdetArray[i][2] = "-";

            i++;
        });


        PageMethodWithCallback('/MyLCN/Ajax/SaveSettings.aspx', 'UpdateHub', ['Settings', WigdetArray], function() { window.location.reload(); });

    }


    function callVac(e) {

        // add function
        if ($(e).attr("mode") == "add") {

            // set mode so next click will remove
            $(e).attr("mode", "remove");

            // fire add js

            AjaxAyncAction($(e).attr("id") + " #LinkText" + $(e).attr("Vacref"), "Please wait...", "Remove from MyLC.N");
            PageMethod($(e).attr("Page"), "SaveVacancy", ["vacref", $(e).attr("Vacref"), "title", $(e).attr("Urltitle") + " at " + $(e).attr("Firm"), "location", $(e).attr("Location")]);


        }
        // remove function
        else {

            // set mode so next click will add
            $(e).attr("mode", "add");

            // fire add js
            AjaxAyncAction($(e).attr("id") + " #LinkText" + $(e).attr("Vacref"), "Please wait...", "Add to MyLC.N");
            PageMethod($(e).attr("Page"), "VacRemove", ["selectionref", $(e).attr("Selectionref")]);

        }
    }


    function callDiary(e) {

        // add function
        if ($(e).attr("mode") == "add") {

            // set mode so next click will remove
            $(e).attr("mode", "remove");

 
  
            // fire add js
            AjaxAyncAction($(e).attr("id") + " #LinkText" + $(e).attr("DiaryRef"), "Please wait...", "Remove from MyLC.N");
            PageMethod($(e).attr("Page"), "Save?st=1", ["diaryref", $(e).attr("DiaryRef"), "AddtoTimeline", $(e).attr("AddtoTimeline"), "priority", $(e).attr("Priority")], 'divDoohickey');


        }
        // remove function
        else {


            // set mode so next click will add
            $(e).attr("mode", "add");
    
            // fire remove js
            AjaxAyncAction($(e).attr("id") + " #LinkText" + $(e).attr("DiaryRef"), "Please wait...", "Add to MyLC.N");
            PageMethod($(e).attr("Page"), "FrontRemove?st=1", ["diaryref", $(e).attr("DiaryRef")], 'divDoohickey');

        }
    }
    
    
    
    
    
    
