﻿var calObj = null;

window.addEvent('domready', function() {
    var els = $(document).getElements('input[class~=InputHalfDateControls]');
    var cals = '';
    els.each(function(el, index) {
        cals += el.id + ": 'd/m/Y'";
        if (index + 1 < els.length) cals += ',';
    });
    
    if (cals.length > 0) {
      eval("calObj = new Calendar({" + cals + "}, { offset: 1, direction: 1});");
    }
 });

/////////////////////////////////////////////////////////
//
// SETUP KEYPRESS CONTROL VARS
//
var altKey = false;
var nextKey = false;

/////////////////////////////////////////////////////////
//
// DETERMINE WHICH KEYPRESS FUNCTIONS TO  
// USE BY BROWSER TYPE
//
if (!/MSIE (5\.5|6|7\.)/.test(navigator.userAgent)) { 
/////////////////////////////////////////////////////////
//
// MOZILLA-STYLE PROPAGATE DOWN EVENT ARCHITECTURE
//
// Much better than IE's. Actually captures key combo's
// and doesn't affect other key combo's

	function detectspecialkeys(e){
		var evtobj= e
		var keyCode = e.which;
		if (evtobj.ctrlKey){
			toggleAccordion(keyCode);
		}
	}
	document.onkeypress=detectspecialkeys;
	
}else{
//
/////////////////////////////////////////////////////////
//
// MICROSOFT-STYLE BUBBLE UP EVENT ARCHITECTURE
//
// retarded IE requires this funky capture system
// checks if altKey key pressed, then marks true
// then captures next key, then marks it true
// if altKey key true and next key true, then it does
// conditional statement

	document.onkeydown = function () {
		
		var keyCode=event.keyCode
		
		// if its akt key, mark it true 
		// and reset nextKey
		if(keyCode == 18){ 
		
			altKey = true;
			nextKey = false;
			
		// if altKey is true, then flag nextKey
		}else{
			nextKey = true;
		}
	
		// if both altKey and next key, then do our conditional statements
		if (altKey && nextKey) {
			
			if(keyCode == 49 || keyCode == 50 || keyCode == 51 || keyCode == 52 || keyCode == 53){
				//alert ('altKey key pressed + '+keyCode);
				toggleAccordion(keyCode);
			}
			
			// reset altKey vars
			altKey = false;
			nextKey = false;
			
			// this is here last, so it nullifies IE beviours for these buttons
			// otherwise we don't want it to nullify other bbuttons such as Control-P (for print), etc
			if(keyCode == 49 || keyCode == 50 || keyCode == 51 || keyCode == 52 || keyCode == 53){
				return false;
			}
				
		} else {
		
		  return true;
		  
		}
	}
}

function toggleAccordion(obj) {
    // check if the 1-5 keys are pressed in conjunction with the altKey key
	if(accordion && (obj == 49 || obj == 50 || obj == 51 || obj == 52 || obj == 53)) {
		accordion.display(newAccArray[obj]);				
	}
}

var accordion = null;
var accArray = new Array();
var keyArray = new Array();
var newAccArray = null;

$(document).addEvent('domready', function() {
    
    // register the accordion for the document finder
    accordion = new Accordion('h3.atStart', 'div.atStart', {
        opacity: false,
        start: 'all-closed',
        show: false,
        alwaysHide: true,
        onComplete: function(){
            ProcessAccordionClosing();
        },
        onActive: function(toggler, element){
            toggler.setStyle('color', 'Red');
        },
        onBackground: function(toggler, element){
            toggler.setStyle('color', '#000');
            
            // hide the all visible calendars
            for (var i=0; i < calObj.calendars.length; i++) {
                var el = calObj.calendars[i];
                if (el.visible) calObj.toggle(el);
            }
        }
    }, $('documentFinderAccordion'));
    
    $(document).getElements('h3[class~=toggler]').each(function(element, index) {
        accArray.push(index);
        keyArray.push(49+index);
        
        var lnk = $(element).getElement('a');
        lnk.addEvent('focus', function() {
            
            if ($(accordion.elements[index]).getHeight() == 0) {
                accordion.display(index);
            }
        });
    });
    
    newAccArray = accArray.associate(keyArray);
});

var vLastClosingSection = '';
var vPreviousClosingSection = '';
var vPreviousDivContainer = '';
var vLastDivContainer = '';
var ClosingSections = new Array();

function GenerateSummary(summaryDiv, divcontainer)
{
    vPreviousClosingSection = vLastClosingSection;
    vLastClosingSection = prefix + summaryDiv;

    vPreviousDivContainer = vLastDivContainer;
    vLastDivContainer = divcontainer;

    var vClosingSectionFound = false;
    var i = 0;
    for (i = 0; i < ClosingSections.length; i++) {
        if (ClosingSections[i] == vLastClosingSection) {
            vClosingSectionFound = true;
            break;
        }
    }
    if (!vClosingSectionFound) {
        ClosingSections[i] = vLastClosingSection;
    }
}

function ProcessAccordionClosing() {
    if (vPreviousClosingSection.length > 0 && $(vPreviousDivContainer).style.height == '0px') {
        var vPrevious = $(vPreviousClosingSection);
        vPrevious.innerHTML = '';
        $(vLastClosingSection).innerHTML = '';
        var vEls = $(vPreviousDivContainer).getElementsByTagName("input");
        var vlblElement = '';
        var vCheckBoxList = false;
        var mainLabelEl =  null;
        
        for (var i = 0; i < vEls.length; i++) {
            vlblElement = '';
            var vElement = vEls[i];
            if (vElement.type != "submit") {
                if (vlblElement == '') {                                    
                    vlblElement =  vElement.id.replace(prefix, '').replace('_'+ i, '');
                    if (i == 0) mainLabelEl = vlblElement;
                }

                if (vEls[i].type == "checkbox") {
                    if (vEls[i].checked) {
                        var vElsLabels = $(vPreviousDivContainer).getElementsByTagName("label");
                        for (var j = 0; j < vElsLabels.length; j++) {
                            if (vElsLabels[j].htmlFor == vEls[i].id) {
                                vPrevious.innerHTML += vElsLabels[j].innerHTML + ', ';
                                break;
                            }
                        }
                    }
                    vCheckBoxList = true;
                }
                
                if (vElement.type == "text") {
                    var vLogical = $(prefix + "Logical" + vlblElement);
                    var vLabel = $(prefix +"lbl" + vlblElement);
                    
                    if (vElement.value != '') {
                        if (vLogical != null) {
                            vPrevious.innerHTML += vLabel.innerHTML + " is " + vLogical[vLogical.selectedIndex].value + " to " + vElement.value + "<br />";
                        }
                        else {
                            vPrevious.innerHTML += vLabel.innerHTML + ": " + vElement.value + "<br />";
                        }
                        vlblElement = '';
                    }
                    vCheckBoxList = false;
                }
            }
        }

        var summlbl = $(prefix + 'lbl' + mainLabelEl);
        if (vPrevious.innerHTML.length > 0 && vCheckBoxList && summlbl) {   
            vPrevious.innerHTML = "Selected " + summlbl.innerHTML + "(s):<br />" + vPrevious.innerHTML.substring(0, vPrevious.innerHTML.length - 2);
        }
    }
}

function ValidateDocumentFinderForm() {
    HideMetaDataWindow();

    for (var i = 0; i< document.forms[0].length; i++)  {
        var vElement = document.forms[0].elements[i];
        if (vElement.type == "checkbox") {
            if (vElement.checked) {
                return true;
            }
        }
        if (vElement.type == "text") {
            if (vElement.value != '') {
                return true;
            }
        }
    }
    return false;
}
