/**
 * Central repository for all general Javascript functions for the
 * Admin website.
 *
 * @package   phpmultiplenewsletters_javascript
 * @author    Triangle Solutions Ltd
 * @copyright (C) 2001 Triangle Solutions Ltd
 * @version   SVN: $Id: $
 * @todo
 * \\||
 */


/**
 * Make the site load up in a new frame if it is forced into a sub frame of another site
 *
 */
if (self != top) {
    top.location.href = self.location.href;
}

var objFocusedField = '';


/**
 * @todo Document this function
 *
 */
function jumpMenu(targ, selObj, restore) {
    eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
    if (restore) {
        selObj.selectedIndex = 0;
    }
}


/**
 * @todo Document this function
 *
 */
function deletethis(link) {
    var name = confirm('Before we continue are you sure you want to delete this?');

    if (name == true && link != null) {
        window.location = link;
    } else if(name == true) {
        return true;
    } else {
        return false;
    }
}

/**
 * @todo Document this function
 *
 */
function popwindow(popsrc, stuff) {
    if (!stuff) {
        videoWindow = window.open(popsrc, 'Popup', 'width=800, height=600, left=50, top=50, scrollbars=yes, toolbars=yes, menubar=yes, location=yes');
    } else {
        videoWindow = window.open(popsrc, 'Popup', stuff)
    }
}

/**
 * @todo Document this function
 *
 */
function blocking(nr, type) {
    var browser = navigator.appName

    if (browser == "Microsoft Internet Explorer") {
        type = 'block';
    }
    if (document.layers) {
        current = (document.layers[nr].display == 'none') ? type : 'none';
        document.layers[nr].display = current;
    } else if (document.all) {
        current = (document.all[nr].style.display == 'none') ? type : 'none';
        document.all[nr].style.display = current;
    } else if (document.getElementById) {
        vista = (document.getElementById(nr).style.display == 'none') ? type : 'none';
        document.getElementById(nr).style.display = vista;
    }
}


/**
 * Highlights a row when mouseover and converts the onclick behaviour
 * to a link based on the first <a> tag found.
 *
 * @param string xTableId the Table's id attribute
 */
function ConvertRowsToLinks(xTableId) {
    var rows = document.getElementById(xTableId).rows;

    for (i = 0; i < rows.length; i++) {
        var event = '';

        // If <!-- nohighlight -- > is found within the row, do not convert to link
        if(rows[i].innerHTML.match("nohighlight") == null) {

            // If the row is not already selected, highlight it when mouseover
            rows[i].onmouseover = function() {
                if (this.className != 'row_click') {
                    this.className = 'row_highlight';
                }
            }

            // If the row is not already selected, remove the highlight when mouseout
            rows[i].onmouseout = function() {
                if (this.className != 'row_click') {
                    this.className='';
                }
            }

            rows[i].onclick = function() {
                inputs = this.getElementsByTagName('input');
                if (inputs.length > 0 && inputs[0].type == 'checkbox') {
                    var checkbox = inputs[0];
                    var id = checkbox.id;

                    if (this.className == 'row_click') {
                        checkbox.click();
                        this.className = '';
                    } else {
                        checkbox.click();
                        this.className = 'row_click';
                    }
                } else if(this.innerHTML.match("nolink")) {

                } else {
                    var link = this.getElementsByTagName('a');
                    if (link.length != 0) {
                        this.className = 'row_click';
                        document.location.href = link[0].href;
                    }
                }
            }
        }
    }
}


/**
 * When a row is clicked, inverts any checkbox on that row.
 *
 * @param event
 * @param row
 */
function invertCheckBox(event, row) {
    if (!event) var event = alert(window.event);

    // Determine which element was clicked. If it is a checkbox, do nothing
    var targ;
    if (event.target) {
        targ = event.target;
    } else if (event.srcElement) {
        targ = event.srcElement;
    }
    if (targ.nodeType == 3) { // defeat Safari bug
        targ = targ.parentNode
    }
    var tname;
    tname = targ.tagName;
    if (tname != "INPUT") {
        var inputs = row.getElementsByTagName('input');
        for (i = 0; i < inputs.length; i++) {
            if (inputs[i].type == 'checkbox') {
                var checkbox = inputs[i];
                checkbox.click();
            }
        }
    }
}


/**
 *
 *
 */
function check_max_chars(max_chars, element, list_id, event) {
    if (element.value.length > max_chars) {
        element.value = element.value.substring(0, max_chars);
        document.getElementById('max_' + list_id).style.display = 'block';
        document.getElementById('show_count_' + list_id).style.display = 'none';
    } else {
        document.getElementById('show_count_' + list_id).style.display = 'inline';
        document.getElementById('max_' + list_id).style.display = 'none';

        // Detect which key was pressed. If delete (unicode 8), use -1
        if (event.keyCode == '8') {
            document.getElementById('count_' + list_id).value = (max_chars - element.value.length + 1);
        } else {
            document.getElementById('count_' + list_id).value = (max_chars - element.value.length - 1);
        }

        if (document.getElementById('count_' + list_id).value < 0) {
            document.getElementById('count_' + list_id).value = 0;
        } else if (document.getElementById('count_' + list_id).value > max_chars) {
            document.getElementById('count_' + list_id).value = max_chars;
        }
    }
}


/**
 * In a given html <form>, ticks all the checkboxes.
 */
function checkbox(form, arg) {
    for (var c = 0; c < form.elements.length; c++) {
    	if (form.elements[c].type == 'checkbox') {
            form.elements[c].checked = arg;
        }
    }
}


/**
 * In a given html <form>, ticks all the unticked checkboxes and
 * unticks all the ticked ones: reverses the selection.
 */
function invert_selection(form) {
    for (var c = 0; c < form.elements.length; c++) {
        if (form.elements[c].type == 'checkbox') {
            if(form.elements[c].checked == true) {
                form.elements[c].checked = false;
            } else {
                form.elements[c].checked = true;
            }
        }
    }
}


/**
 * Checks whether any checkbox has been ticked before taking an action
 */
function check_ticks(form, element, select_obj) {
    for (var c = 0; c < form.elements.length; c++) {
        if (form.elements[c].type == 'checkbox') {
            if(form.elements[c].checked == true) {
                return true;
            }
        }
    }
    if (select_obj.selectedIndex == 1) {
        window.alert('You need to select at least one ' + element + ' before performing a global action on "selected" CLIs');
        return false;
    }
}


/**
 * Checks the state of a checkbox and updates a given input box accordingly
 */
function update_counter(checkbox, input, number) {
    var input = document.getElementById(input);
    var total = parseInt(input.value);
    var number = parseInt(number);
    if (checkbox.checked == true) {
        total += number;
    } else {
        total -= number;
    }
    input.value = total;
}


/**
 * This is an equivalent of PHP's in_array(), except
 * that it takes a third argument, the attribute. This
 * way you can send an array of HTML elements and search
 * for a matching attribute like 'id' or 'class'
 */
function in_array(needle, haystack, attribute)
{
    for (var i = 0; i < haystack.length; i++) {
        if (eval("haystack[i]." + attribute + ";") == needle) {
            return true;
        }
    }

    return false;
}


/**
 *
 */
function SwitchP(param)
{
    if (document.view.check_preview.checked == true) {
        document.view.action = "demo.php?newsid=" + param;
        document.view.target = '_blank';
        document.view.sendNews.value = "Html Preview";
        document.view.sendAdmin.style.visibility = "hidden";
    } else {
        document.view.action = "index.php?caseid=send&newsid=" + param;
        document.view.target = '_self';
        document.view.sendNews.value = "Send";
        document.view.sendAdmin.style.visibility = "visible";
    }
}


/**
 * This function will assign email variables to focused textarea
 */
function insertAtCursor(paramField, paramValue)
{
    if (paramField == '') {
        paramField=document.frmNewsLetter.htmlemail;
	}

    if (document.selection)	{
        paramField.focus();
        sel = document.selection.createRange();
        sel.text = paramValue;
    } else {
        paramField.value += paramValue;
    }
}


/**
 *
 */
function funcGetTextName(paramField)
{
    objFocusedField = new Object(paramField);
    return objFocusedField
}
