/* ###############################
 * # Script to handle show/hide  #
 * # the radio buttons following #
 * # the API                     #
 * ############################### */

// dom traversal magic to get the apis with radio groups
(function() { // anonymous function to maintain scope
    // get all forms on the page
    var formList = document.getElementsByTagName('form');

    // iterate through all forms for known types
    for(var i = 0; i < formList.length; i++) {
        var form = formList[i], // store the form for reference
            urlRegex = new RegExp('.*' + window.location.host), // clean out the main url
            formAction = form.action.replace(urlRegex, ''), // get the form action
            apiFieldset = null, // init api fieldset
            appFieldset = null; // init app fieldset

        // check for known form types
        if(/\/apps\/select/.test(formAction)) { // when you "add" a key to an app
            form = formList[i]; // get the form
            apiFieldset = form.getElementsByTagName('fieldset')[1]; // get the api fieldset from the form

            break; // leave loop
        } else if(/\/apps\/register/.test(formAction)) { // myaccount->applications->new application
            form = formList[i]; // get the form
            apiFieldset = form.getElementsByTagName('fieldset')[2]; // get the api fieldset from the form
            appFieldset = form.getElementsByTagName('fieldset')[1]; // get the app fieldset

            break; // leave loop
        } else if(/\/member\/register/.test(formAction)) { // on portal->register
            form = formList[i]; // get the form
            apiFieldset = form.getElementsByTagName('fieldset')[3]; // get the api fieldset from the form
            appFieldset = form.getElementsByTagName('fieldset')[2]; // get the app fieldset from the form

            break; // leave loop
        } else if(/\/member\/join/.test(formAction)) { // when logging into a different area from an existing account
            form = formList[i]; // get the form
            apiFieldset = form.getElementsByTagName('fieldset')[2]; // get the api fieldset from the form
            appFieldset = form.getElementsByTagName('fieldset')[1]; // get the app fieldset from the form

            break; // leave loop
        } else if(/\/key\/register/.test(formAction)) {
            form = formList[i]; // get the form
            apiFieldset = form.getElementsByTagName('fieldset')[1]; // get the api fieldset from the form

            break; // leave loop
        }

    }

    // Application fieldset stuff
    if(appFieldset != undefined) { // check for the valid fieldset
        // get the DL of the fieldset
        var dl = appFieldset.getElementsByTagName('dl')[0], // should only be 1
            ddAppList = (dl ? dl.getElementsByTagName('dd') : []), // get the list of DD's
            dtAppList = (dl ? dl.getElementsByTagName('dt') : []); // get the list of DT's

        // traverse away!
        for(var d = 0; d < ddAppList.length; d++) { // look for the radio buttons first
            var dd = ddAppList[d], // store the dt/dd element
                inputList = dd.getElementsByTagName('input'), // get the input items in the DD
                input = inputList[0]; // get the first input element inside

            if(input) {
                // hide the ads_system input box by default if applicable
                if(/applications-ads_system/.test(input.id)) {
                    ddAppList[d].style.display = 'none'; // input box
                    dtAppList[d].style.display = 'none'; // label

                    // store the dd/dt position to show/hide
                    var adsSystemPosition = d;
                }
            }
        }

        // check for ads system position before proceeding
        if(adsSystemPosition) {
            // traverse away!
            for(var l = 0; l < ddAppList.length; l++) { // look for the radio buttons first
                var ddApp = ddAppList[l], // store the dt/dd element
                    labelList = ddApp.getElementsByTagName('label'), // get the label items in the DD
                    label = labelList[0]; // get the first label element inside

                if(label) {
                    // get the first input from this label
                    var labelInput = label.getElementsByTagName('input')[0];

                    // apply the onclick to the radio buttons for ads
                    if(/applications-ads/.test(labelInput.id)) {
                        labelInput['onclick'] = function() { // this is the 'yes' radio button
                            ddAppList[adsSystemPosition].style.display = 'block'; // input box
                            dtAppList[adsSystemPosition].style.display = 'block'; // label
                        };

                        // also apply it to the 'no' radio button
                        var noLabel = labelList[1];
                        var noInput = noLabel.getElementsByTagName('input')[0];

                        noInput['onclick'] = function() { // this is the 'no' radio button
                            ddAppList[adsSystemPosition].style.display = 'none'; // input box
                            dtAppList[adsSystemPosition].style.display = 'none'; // label
                        };
                    }
                }
            }
        }
    }

    // API section stuff
    if(apiFieldset) { // check for the valid fieldset
        // get the DL of the fieldset
        var dlApi = apiFieldset.getElementsByTagName('dl')[0], // should only be 1
            ddApiList = dlApi.getElementsByTagName('dd'), // get the list of DD's
            dtApiList = dlApi.getElementsByTagName('dt'); // get the list of DT's

        // next flag to denote that we've found a radio button and to apply funcitonality to the next checkbox
        var next = false;

        // traverse away!
        for(i = (ddApiList.length - 1); i >= 0; i--) { // reverse iteration to find the radio buttons
            var ddApi = ddApiList[i], // store the dt/dd element
                apiLabelList = ddApi.getElementsByTagName('label'), // get the labels in the DD
                apiLabel = apiLabelList[0]; // should be only 1

            if(apiLabel) { // make sure we got a label
                var apiInput = apiLabel.getElementsByTagName('input')[0]; // get the input box inside the label

                // apply the onchange for the checkbox input
                if(next === true) {
                    // mark the apiInput position
                    apiInput.formPosition = i;

                    // apply the onclick event
                    apiInput['onclick'] = function() {
                        var displayType = (this.checked === true) ? 'block' : 'none'; // set the type to display

                        // this does the set of radio stuff
                        var radioButtons = ddApiList[(this.formPosition + 1)]; // radio buttons
                        radioButtons.style.display = displayType; // hide the set
                        dtApiList[(this.formPosition + 2)].style.display = displayType; // label
                        
                        // check the next dd if it has a custom apiInput
                        var nextDD = ddApiList[(this.formPosition + 2)];
                        var customInput = nextDD.childNodes[1];
                        
                        // if the custom apiInput exists and has a manual_key id
                        if(customInput && (/manual_key/.test(customInput.id))) {
                            nextDD.style.display = displayType; // custom limit

                            // also make sure the custom apiInput is displayed if the custom radio button is selected
                            var radioInput = radioButtons.getElementsByTagName('input');

                            // check the last radio button since we have a customapiInput the last is always the corresponding
                            if(radioInput[radioInput.length - 1].checked) {
                                customInput.style.display = displayType;
                            }
                        }

                        // member/register has an apiInput box for custom key
                        if((/\/member\/register/.test(formAction)) || (/\/member\/join/.test(formAction)) || (/\/key\/register/.test(formAction))) {
                            ddApiList[(this.formPosition + 2)].style.display = displayType; // apiInput box for custom key
                        }
                    };

                    // if it's already unchecked hide the apiInputs
                    if(!apiInput.checked) {
                        apiInput.onclick();
                    }

                    next = false; // reset the flag
                }

                // check if it's a radio button
                if(/radio/.test(apiInput.type)) {
                    next = true; // set the flag

                    // if there are more labels we have more radio buttons
                    if(apiLabelList.length > 1) {
                        var customKey = apiLabelList[(apiLabelList.length - 1)], // get the last radio button
                            customInput = ddApiList[(i + 1)].getElementsByTagName('input')[0], // get the custom input
                            customRadio = customKey.getElementsByTagName('input')[0]; // get the radio box from it

                        // make sure there is a custom input before proceeding
                        if(customInput) {
                            // apply the show hide of the radio for the custom input
                            if(/manual_key/.test(customInput.id)) {
                                // hide if the the custom radio is not checked
                                if(!customRadio.checked) {
                                    customInput.style.display = 'none'; // hide the custom input box to start
                                }

                                customRadio.attachedInput = customInput; // keep the reference to the custom input for this radio button
                                apiInput.attachedInput = customInput; // keep the reference to the custom input for this radio button

                                customRadio['onclick'] = function() { // select custom to show
                                    this.attachedInput.style.display = 'block';
                                };

                                // other input types hide the custom input
                                apiInput['onclick'] = function() {
                                    this.attachedInput.style.display = 'none';
                                };
                            }
                        }
                    }
                }
            }
        }
    }
})();

