﻿function beginRequest_Documents(sender, args) {
    //ApplicationPath is defined in js/interface.js
    var postbackcontrol = sender._postBackSettings.panelID.split('|')[1].replace(/\$/gi, '_');
    if (postbackcontrol != null) {
        var namearray = postbackcontrol.split('_');
        var elementid = namearray[namearray.length - 1];
        if (elementid === 'LkBtnExpColFolderDocs') {
            var imgFolderIcon = $get(postbackcontrol.replace('LkBtnExpColFolderDocs', 'ImgFolderIcon'));
            imgFolderIcon.src = ApplicationPath + 'gfx/Documents/folder_50_processing.gif';
        }
        else if (elementid === 'LkBtnFeedBack') {
            var imgFeedbackIcon = $get(postbackcontrol.replace('LkBtnFeedBack', 'ImgFeedback'));
            imgFeedbackIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
        }
        else if (elementid === 'LkBtnEditDocument') {
            var imgEditDocumentIcon = $get(postbackcontrol.replace('LkBtnEditDocument', 'ImgEditDocument'));
            imgEditDocumentIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
        }
        else if (elementid === 'LkBtnDelDocument') {
            var imgDeleteDocumentIcon = $get(postbackcontrol.replace('LkBtnDelDocument', 'ImgDelDocument'));
            imgDeleteDocumentIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
        }
        else if (elementid === 'LkBtnAddDocument') {
            var imgAddDocumentIcon = $get(postbackcontrol.replace('LkBtnAddDocument', 'ImgAddDocument'));
            imgAddDocumentIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
        }
        else if (elementid === 'LKBtnEditFolder') {
            var imgEditFolderIcon = $get(postbackcontrol.replace('LKBtnEditFolder', 'ImgEditFolder'));
            imgEditFolderIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
        }
        else if (elementid === 'LKBtnDeleteFolder') {
            var imgDeleteFolderIcon = $get(postbackcontrol.replace('LKBtnDeleteFolder', 'ImgDeleteFolder'));
            imgDeleteFolderIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
        }
        else if (elementid === 'LkBtnAddFolder') {
            var imgAddFolderIcon = $get(postbackcontrol.replace('LkBtnAddFolder', 'ImgAddFolderIcon'));
            imgAddFolderIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress.gif';
        }
        else if (elementid === 'LkBtnSaveFeedback') {
        var imgAddFeedbackIcon = $get(postbackcontrol.replace('LkBtnSaveFeedback', 'ImgSave'));
            if (imgAddFeedbackIcon == null) {
                var imgEditFeedbackIcon = $get(postbackcontrol.replace('LkBtnEditFeedback', 'ImgEditFeedback'));
                imgEditFeedbackIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
            }
            else {
                imgAddFeedbackIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
            }
        }
        else if (elementid === 'LkBtnDelFeedback') {
            var imgDeleteFeedbackIcon = $get(postbackcontrol.replace('LkBtnDelFeedback', 'ImgDelFeedback'));
            imgDeleteFeedbackIcon.src = ApplicationPath + 'gfx/Documents/pulsars/progress1.gif';
        }
        else {
            if (typeof (ImgDocIconClientID) != 'undefined') {
                var imgDocIcon = $get(ImgDocIconClientID);
                imgDocIcon.src = ApplicationPath + 'gfx/Documents/DocSearch_50_Processing.gif';
            }
        }
    }
}

function pageLoaded_Documents(sender, args) {
    ScrollView();
    InitialiseMonitors();
}

var InitialPageLoad = false;
function ScrollView() {
    if (typeof(DocumentScrollElementClientID) != 'undefined') {
        var el = document.getElementsByName(DocumentScrollElementClientID);
        if (el != null && !InitialPageLoad) {
            if (el[0] == undefined) {
                alert('You do not have permission to view this document!');
            }
            else {
                el[0].scrollIntoView();
            }
            InitialPageLoad = true;
        }
    }
}

//var data = '{ "color": "blue", "closed": true, "points": [[10,10], [20,10], [20,20], [10,20]] }';
//var JSON_TextBoxClientData = '{ "clients": [["a", "b"], ["c", "d"]] }';
//var JSON_TextBoxClientData = '{ "clients": [["ctl00_ContentPlaceHolderTop_RepeaterDocFolders_ctl02_ViewFolderControl1_RepeaterFolderDocuments_ctl01_ViewDocumentControl1_DocumentFeedbackControl_RepeaterFeedback_ctl08_ViewFeedbackItemControl1_AddEditDocumentFeedBack_EDIT_txtbxComment", "ctl00_ContentPlaceHolderTop_RepeaterDocFolders_ctl02_ViewFolderControl1_RepeaterFolderDocuments_ctl01_ViewDocumentControl1_DocumentFeedbackControl_RepeaterFeedback_ctl08_ViewFeedbackItemControl1_AddEditDocumentFeedBack_EDIT_DivCharCount"], ["ctl00_ContentPlaceHolderTop_RepeaterDocFolders_ctl02_ViewFolderControl1_RepeaterFolderDocuments_ctl01_ViewDocumentControl1_DocumentFeedbackControl_RepeaterFeedback_ctl09_ViewFeedbackItemControl1_AddEditDocumentFeedBack_EDIT_txtbxComment", "ctl00_ContentPlaceHolderTop_RepeaterDocFolders_ctl02_ViewFolderControl1_RepeaterFolderDocuments_ctl01_ViewDocumentControl1_DocumentFeedbackControl_RepeaterFeedback_ctl09_ViewFeedbackItemControl1_AddEditDocumentFeedBack_EDIT_DivCharCount"]] }';

function InitialiseMonitors() {
    if ($get(HiddenFieldJSON_TextBoxClientDataClientID) != null) {
        var HiddenFieldJSON_TextBoxClientData = $get(HiddenFieldJSON_TextBoxClientDataClientID);
        var ClientData = eval("(" + HiddenFieldJSON_TextBoxClientData.value + ")");
        for (var i = 0; i < ClientData.clients.length; i++) {
            //alert('tid: ' + ClientData.clients[i][0] + '\ncid: ' + ClientData.clients[i][1]);
            InitialiseTextBoxLengthMonitor(ClientData.clients[i][0], ClientData.clients[i][1]);
        }
    }
}

function InitialiseTextBoxLengthMonitor(txtBoxClientID, CharCountClientID) {
	var txtBox = $get(txtBoxClientID);
	var CharCount = $get(CharCountClientID);
	var bound1 = 1500;
	var bound2 = 2000;
	var context = {};
	if (txtBox != null && CharCount != null) {
	    // Define a callback by passing a method and a context.
	    var callback = Function.createCallback(CountCharsHandler, context);

	    // Install a handler for the click event.
	    $addHandler(txtBox, 'keyup', callback);
	    $addHandler(txtBox, 'copy', callback);
	    $addHandler(txtBox, 'paste', callback);
	    $addHandler(txtBox, 'blur', callback);

	    // Fill the context object.
	    context.txtBox = txtBox;
	    context.CharCount = CharCount;
	    context.bound1 = bound1;
	    context.bound2 = bound2;

	    //Run once initially
	    CountChars(txtBox, CharCount, bound1, bound2);
	}
}

function CountCharsHandler(evt, context) {
	var txtBox = context.txtBox;
	var CharCount = context.CharCount;
	var bound1 = context.bound1;
	var bound2 = context.bound2;
	CountChars(txtBox, CharCount, bound1, bound2);
}

function CountChars(txtBox, CharCount, bound1, bound2) { 
	var count = txtBox.value.length; 
	// Color the background according to the bounds.
	if (count <= bound1) {
	    txtBox.style.backgroundColor = '#FFFFFF';
	    CharCount.innerHTML = 'Character Count: (' + count + '/' + bound2 + ')';
	}
	else if ((count > bound1) && (count <= bound2)) {
	    txtBox.style.backgroundColor = '#FFFF99';
	    CharCount.innerHTML = 'Character Count: (' + count + '/' + bound2 + ') - <span style="color:#FF7F00;font-weight:bold;">WARNING! Limit is ' + bound2 + ' chars!</span>';
	}
	else {
	    txtBox.style.backgroundColor = '#ffabab';
	    CharCount.innerHTML = 'Character Count: (' + count + '/' + bound2 + ') - <span style="color:#FF0000;font-weight:bold;">WARNING! TEXT IS TOO LONG!</span>';
	}
}

function TruncateExcess(txtBoxClientID, MaxLength) {
    var txtBox = $get(txtBoxClientID);
    if (txtBox != null) {
        if (txtBox.value.length > MaxLength) {
            txtBox.value = txtBox.value.substring(0, MaxLength);
        }
    }
}

function SearchTips() {
    alert('Search ICS Documents by:\n- Title\n- Author/s\n- Notes');
}

function sendToClipboard(s) {
    if (window.clipboardData && clipboardData.setData) {
        clipboardData.setData("Text", s);
    }
    else {
        alert("Your browser does not allow access to your clipboard!\nYou can copy the link yourself from the navigation tree on the left by right-clicking on the desired folder (then copy link).");
    }
}