/* This notice must be untouched at all times.

asf.js    v.1.0

Copyright (c) 2005 Mikrobeta Software Corp. All rights reserved.
Created by Milas Development Team (Web: http://www.milasweb.com)
Last modified: 10.6.2005

*/

/* advanced search field functions */

var asfGroupIdx = 0; // counts asf groups
var asfGroupList = new Array(); // holds asf group IDs, every group consist of asfElements
var asfGroupConnector = new Array(); // holds groups' connector name; asfGroupConnector[connectorName] = groupID

// Advanced Search Field

function createASFGroup(container_name, f_name, f_content, c_name, c_types, show_c_label, c_defaults, f_defaults)
{
    var i, formElement;

    asfGroupID = "asfg_" + asfGroupIdx;
    asfGroupList[asfGroupID] = new asfGroup();

    var elementGroup = asfGroupList[asfGroupID];

    elementGroup.setContainerName(container_name);
    elementGroup.setFormName(f_name);
    elementGroup.setConnectorName(c_name);
    elementGroup.showConnectorTitle(show_c_label);

    var formElements = f_content.match(/(N|n)(A|a)(M|m)(E|e)\s*=\s*["']?[a-zA-Z0-9_-]+\[\]["']?/g);
    if (formElements) { 
        for (i=0; i < formElements.length; i++) {
            if (elementName = formElements[i].replace(/name\s*=\s*["']?(.+)\[\]["']?/i, "$1")) {
                elementGroup.formElement[elementName] = new Array();
            }
        }
    } else {
        alert("No repeatable form element.");
        return;
    }
    
    elementGroup.setElementContent(f_content.replace(/([^\\])(['])/g, "$1\\$2"));
    for (i = 0; i < c_types.length; i++) {
        if (!isBlank(c_types[i][0]) && !isBlank(c_types[i][1])) {
            elementGroup.addConnector(c_types[i][0], c_types[i][1]);
        }
    }

    //    elementGroup.showConnectorLabel(false);
    elementGroup.addElement();

    if ((typeof f_defaults == "object") && (f_defaults != null)) {
        if ((typeof c_defaults == "object") && (c_defaults != null)) {
            for (i = 0; i < c_defaults.length; i++) {
                elementGroup.addElement(c_defaults[i]);
            }
        }
        var form = document.forms[elementGroup.getFormName()]; 
        
        for (i = 0; i < f_defaults.length; i++) {
            if (!isBlank(f_defaults[i][0]) && !isBlank(f_defaults[i][1])) {
                
                if (form.elements[f_defaults[i][0]]) {
                    elementGroup.setFormElement(form.elements[f_defaults[i][0]], f_defaults[i][1]);
                }
            }
        }
    }
}

function isBlank(s)
{
    if (s) {
        for (var i = 0; i < s.length; i++) {
            var c = s.charAt(i);
            if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
        }
    }
    return true;
}


function asfGroup()
{
    this.id = 'asfg_' + asfGroupIdx;
    this.containerName = '';
    this.formName = '';
    this.formElement = new Array();
    this.asfElementIdx = 0; // counts asf object in this group
    this.asfDeletedElementNum = 0;
    this.elementList = new Array(); // holds asfElements
    this.elementContent = '';
    this.connectorName = '';  
    this.connectorType = new Array(); // by label 
    this.connectorLabel = new Array(); // by type
    this.displayConnectorTitle = true;

    this.getID = asfg_get_id;
    this.setContainerName = asfg_set_container_name;
    this.getContainerName = asfg_get_container_name;
    this.setElementContent = asfg_set_element_content;
    this.getElementContent = asfg_get_element_content;
    this.addElement = asfg_add_element;
    this.display = asfg_display;
    this.setFormName = asfg_set_form_name;
    this.getFormName = asfg_get_form_name;
    this.setConnectorName = asfg_set_connector_name;
    this.getConnectorName = asfg_get_connector_name;
    this.saveFormValues = asfg_save_form_values;
    this.loadFormValues = asfg_load_form_values;
    this.addConnector = asfg_add_connector;
    this.setConnectorName = asfg_set_connector_name;
    this.getConnectorName = asfg_get_connector_name;
    this.getConnectorLabel = asfg_get_connector_label;
    this.showConnectorTitle = asfg_show_connector_title;
    this.setFormElement = asfg_set_form_element;

    asfGroupIdx++;
}

function asfg_set_form_element(formElement, value)
{
    var i;
    
    if (typeof formElement != "object") return false;
    
    switch (formElement.type)
    {
        case 'select-one':
        case 'select-multiple':
            for (i=0; i<formElement.options.length; i++) {
                if (formElement.options[i].value == value) {
                    formElement.options[i].selected = true;
                }
            }
            break;

        case 'radio':
            var radioCount = formElement.length;
            if (radioCount == undefined) {
                formElement.checked = true;
            } else {
                for (i=0; i< formElement.length; i++) {
                    if (formElement[i].value == value) {
                        formElement[i].checked = true;
                    } else {
                        formElement[i].checked = false;
                    }
                }
            }
            break;

        case 'text':
        case 'textarea':
        case 'file':
            formElement.value = value;
            break;

        case 'checkbox':
            var checkBoxCount = formElement.length;
            if (checkBoxCount == undefined) {
                formElement.checked = true;
            } else {
                for (i=0; i < formElement.length; i++) {
                    if (formElement[i].value == value) {
                        formElement[i].checked = true;
                    } else {
                        formElement[i].checked = false;
                    }
                }
            }
            break;
    }
}

function asfg_show_connector_title(flag)
{
    this.displayConnectorTitle = (flag? true: false);
}

function asfg_set_connector_name(name)
{
    if (name.match(/(.+)(\[\])?/)) {
        var connName = name.replace(/(.+)(\[\])?/, "$1");
        this.connectorName = connName;
        asfGroupConnector[connName] = this.getID();
    } else {
        alert("Invalid connector name: " + name);
        return;
    }
}

function asfg_get_connector_name()
{
    return this.connectorName;
}

function asfg_get_connector_label(type)
{
    return this.connectorLabel[type];
}

function asfg_add_connector(name, type)
{
    this.connectorType[name] = type;
    this.connectorLabel[type] = name;
}

function asfg_set_form_name(name)
{
    this.formName = name;
}

function asfg_get_form_name()
{
    return this.formName;
}

function asfg_get_id()
{
    return this.id;
}

function asfg_set_container_name(name)
{
    this.containerName = name;    
}

function asfg_get_container_name()
{
    return this.containerName;    
}

function asfg_set_element_content(content)
{
    this.elementContent = content;    
}

function asfg_get_element_content()
{
    return this.elementContent;    
}

function asfg_add_element(connectorType, parentID)
{
    if (connectorType && !this.getConnectorLabel(connectorType)) {
        alert("addElement: Undefined connector type '" + connectorType + "'");
        return;
    }

    asfElementID = this.getID() + "_" + this.asfElementIdx;
    if (parentID == null) {
        if (this.asfElementIdx > 0) {
            parentID = this.getID() + "_" + (this.asfElementIdx - 1);
        } else {
            parentID = null;
        }
    }

    this.elementList[asfElementID] = new asfElement(asfElementID);

    if (parentID) {
        this.elementList[asfElementID].setParentID(parentID);
        this.elementList[parentID].setChildID(asfElementID);
        this.elementList[parentID].setConnectorType(connectorType);
    }

    this.elementList[asfElementID].display();
    this.elementList[asfElementID].refreshAllRadios();
    this.asfElementIdx++;

    return asfElementID;
}

function asfg_display()
{
    asfElementID = this.getID() + "_0"; 
    this.elementList[asfElementID].display();
}

function asfg_save_form_values()
{
    var form = document.forms[this.getFormName()];
    var i, value;
    var formElementName, formElementRootName;
    


    for(var elementIdx = 0; elementIdx < form.elements.length; elementIdx++) {
        formElementName  = form.elements[elementIdx].name;
        if (!(formElementRootName = formElementName.replace(/([a-zA-Z0-9_-]+)\[[0-9]+\]/i, "$1"))) continue;
        if (typeof this.formElement[formElementRootName] != "object") continue;
        
        var formElement = form.elements[formElementName];

        if (typeof formElement != "object") continue;

        this.formElement[formElementRootName][formElementName] = new Array();

        switch (formElement.type)
        {
            case 'select-one':
            case 'select-multiple':
                for(var i = 0; i < formElement.options.length; i++) {
                    if (formElement.options[i].selected) {
                        this.formElement[formElementRootName][formElementName][i]=formElement.options[i].value;
                    }
                }
                break;

            case 'radio':
                var radioCount = formElement.length;
                if (radioCount == undefined) {
                    if (ddformElement.checked) {
                        this.formElement[formElementRootName][formElementName][0] =  formElement.checked;
                    }
                } else {
                    for (var i = 0; i < radioCount; i++) {
                        if (formElement[i].checked) {
                            this.formElement[formElementRootName][formElementName][i] = formElement[i].checked;
                        }
                    }
                }
                break;

            case 'text':
            case 'textarea':
            case 'file':
                this.formElement[formElementRootName][formElementName][0] = formElement.value;
                break;

            case 'checkbox':
                var checkBoxCount = formElement.length;
                if (!checkBoxCount) {
                    this.formElement[formElementRootName][formElementName][0] = formElement.checked;
                } else {
                    for (var i = 0; i < checkBoxCount; i++) {
                        if (formElement[i].checked) {
                            this.formElement[formElementRootName][formElementName][i] = formElement[i].checked;
                        }
                    }
                }
                break;
        }
    }
}

function asfg_load_form_values()
{
    var form = document.forms[this.getFormName()];
    var i, value;

    for (var formElementRootName in this.formElement) {
        for (var formElementName in this.formElement[formElementRootName]) {

            var formElement = form.elements[formElementName];
            if (typeof formElement != "object") continue;

            switch (formElement.type)
            {
                case 'select-one':
                case 'select-multiple':
                    if (value = this.formElement[formElementRootName][formElementName]) {
                        for(var i in value) {
                            if (formElement.options[i].value == value[i]) {
                                formElement.options[i].selected = true;
                            } else {
                                alert("Select index error: " + value[i]);
                            }
                        }
                    }
                    break;

                case 'radio':
                    var radioCount = formElement.length;
                    if (value = this.formElement[formElementRootName][formElementName]) {
                        if (radioCount == undefined) {
                            formElement.checked = value[0];
                        } else {
                            for(var i in value) {
                                if (formElement[i].value == value[i]) {
                                    formElement[i].checked = true;
                                } else {
                                    alert("Radio index error: " + value[i]);
                                }
                            }
                        }
                    }
                    break;

                case 'text':
                case 'textarea':
                case 'file':
                    if (value = this.formElement[formElementRootName][formElementName]) {
                        formElement.value = value[0];
                    }
                    break;

                case 'checkbox':
                    var checkBoxCount = formElement.length;
                    if (value = this.formElement[formElementRootName][formElementName]) {
                        if (checkBoxCount == undefined) {
                            formElement.checked = value[0];
                        } else {
                            for(var i in value) {
                                if (formElement[i].value == value[i]) {
                                    formElement[i].checked = true;
                                } else {
                                    alert("Checkbox index error: " + value[i]);
                                }
                            }
                        }
                    }
                    break;
            }
        }
    }
}



// return group id of the radio button



// group functions ends here


function asfElement(asfElementID)
{
    this.elementID = asfElementID;
    this.groupID = elementGetGroupID(asfElementID);
    this.parentID = null;
    this.childID = null;
    this.connectorType = null;

    this.setID = asf_set_id;
    this.getID = asf_get_id;
    this.setGroupID = asf_set_group_id;
    this.getGroupID = asf_get_group_id;
    this.setParentID = asf_set_parent_id;
    this.getParentID = asf_get_parent_id;
    this.setChildID = asf_set_child_id;
    this.getChildID = asf_get_child_id;
    this.resetConnectorType = asf_reset_connector_type;
    this.setConnectorType = asf_set_connector_type;
    this.getConnectorType = asf_get_connector_type;
    this.refreshAllRadios = asf_refresh_all_radios;
    this.getCheckedRadioType = asf_get_checked_radio_type; 
    this.display = asf_display;
    this.destroy = asf_destroy;
}

function asf_get_checked_radio_type() {
    var formName = asfGroupList[this.getGroupID()].getFormName();
    var connectorName = asfGroupList[this.getGroupID()].getConnectorName();
    var idx = this.getID().replace(/asfg_([0-9]+)_([0-9]+)/, "$2");
     
    var radioObj = document.forms[formName].elements[connectorName + "[" + idx + "]"];

    if (!radioObj) return "";

    var radioLength = radioObj.length;
    
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}


function asf_set_group_id(id)
{
    this.groupID = id;
}

function asf_get_group_id()
{
    return this.groupID;
}

function asf_set_id(id)
{
    this.elementID = id;
}

function asf_get_id()
{
    return this.elementID;
}

function asf_set_parent_id(id)
{
    this.parentID = id;
}

function asf_get_parent_id()
{
    return this.parentID;
}

function asf_set_child_id(id)
{
    this.childID = id;
}

function asf_get_child_id()
{
    return this.childID;
}

function asf_refresh_all_radios()
{
    var childID, radioObj, radioLength, connectorType;
    var elementGroup = asfGroupList[this.getGroupID()];
    var formName = elementGroup.getFormName();

    for (var elementID in elementGroup.elementList) {
        elementObj = elementGroup.elementList[elementID];
        var connectorName = asfGroupList[this.getGroupID()].getConnectorName();
        var idx = elementID.replace(/asfg_([0-9]+)_([0-9]+)/, "$2");
        radioObj = document.forms[formName].elements[connectorName + "[" + idx + "]"];

        if (!radioObj) continue;

        radioLength = radioObj.length;

        if(radioLength == undefined) {
            radioObj.checked = (radioObj.value == elementObj.getConnectorType());
        } else {
            for(var i = 0; i < radioLength; i++) {
                radioObj[i].checked = false;
                if(radioObj[i].value == elementObj.getConnectorType()) {
                    radioObj[i].checked = true;
                }
            }
        }
    } 
}

function asf_set_connector_type(type)
{
    this.connectorType = type;
}

function asf_get_connector_type()
{
    return this.connectorType;
}

function asf_reset_connector_type(type)
{
    var childID;
    var elementGroup = asfGroupList[this.getGroupID()];

    elementGroup.saveFormValues();

    if (childID = this.getChildID()) {
        this.setConnectorType(type);
        if (elementGroup.displayConnectorTitle) {
            document.getElementById(childID + "_type").innerHTML = elementGroup.getConnectorLabel(type);
        }
    } else {
        elementGroup.addElement(type, this.getID());
    }

    elementGroup.loadFormValues();
}

function asf_destroy()
{
    var elem, childID, childType;
    var elementGroup = asfGroupList[this.getGroupID()];

    if (parentID = elementGroup.elementList[this.getID()].getParentID()) {

        if (childID = elementGroup.elementList[this.getID()].getChildID()) {

            elementGroup.elementList[parentID].setChildID(childID);
            elementGroup.elementList[childID].setParentID(parentID);

            elementGroup.elementList[parentID].resetConnectorType(this.getConnectorType());
        } else {
            elementGroup.elementList[parentID].setChildID(null);
            elementGroup.elementList[parentID].setConnectorType(null);
        }

        elem = document.getElementById(this.getID());
        elem.parentNode.removeChild(elem);

        delete elementGroup.elementList[this.getID()];
        elementGroup.asfDeletedElementNum++;

        if (elementGroup.asfElementIdx == elementGroup.asfDeletedElementNum) {
            this.setConnectorType(null);
        }
    }
    this.refreshAllRadios();
}

function asf_display()
{
    var formContent, idx;
    var asfContent='';
    var connectorType;
    var elementGroup = asfGroupList[this.getGroupID()];

    if ((this.getID() == this.getGroupID() + '_0') || (!elementGroup.displayConnectorTitle)) {
        asfContent +=   "<div id=\"" + this.getID() + "\" class=\"first_as_field\">";
    } else {
        connectorType = elementGroup.elementList[this.getParentID()].getConnectorType();
        
        if (elementGroup.displayConnectorTitle) {
            asfContent +=   "<div id=\"" + this.getID() + "\" class=\"as_field\">";
            asfContent +=   "   <div id=\"" + this.getID() + "_type\" class=\"as_type\">";
            asfContent +=   elementGroup.getConnectorLabel(connectorType);
            asfContent +=   "   </div>";
        }
    }

    asfContent +=   "       <div class=\"search_field\">";

    idx = this.getID().replace(/asfg_([0-9]+)_([0-9]+)/, "$2");
    formContent = elementGroup.getElementContent();
    asfContent += formContent.replace(/(N|n)(A|a)(M|m)(E|e)="([a-zA-Z0-9_-]+)\[\]"/g, "name=\"$5[" + idx + "]\"");

    asfContent +=   "       </div>";

    if (this.getID() != this.getGroupID() + '_0') {
        asfContent += "		<div id=\"" + this.getID() + "_close\" class=\"as_field_close\" ";
        asfContent += 		"onclick=\"asf_close_clicked(this.id)\"/></div>";
    }
    if (this.getID() == this.getGroupID() + '_0') {
        asfContent +=   "   <div class=\"first_radio_box\">";
    } else {
        asfContent +=   "   <div class=\"radio_box\">";
    }

    for (var connectorLabel in elementGroup.connectorType) {
        // connector radio
        asfContent += "<input type=\"radio\" name=\"" + elementGroup.getConnectorName() + "[" + idx + "]\" value=\"";
        asfContent += elementGroup.connectorType[connectorLabel] + "\" ";
        asfContent += 		"onclick=\"asf_radio_clicked('" + elementGroup.getConnectorName() + "[" + idx + "]')\"/> ";
        asfContent += "<a class=\"radio_label\">" + connectorLabel + "</a>";
    }    
    asfContent +=   "       </div>";

    asfContent +=   "   </div>";
    document.getElementById(elementGroup.getContainerName()).innerHTML += asfContent;
}

/* asf external functions */

function btnRadioGetGroupID(name)
{
    if (name.match(/(.+)\[([0-9]+)\]/)) {
        return asfGroupConnector[name.replace(/(.+)\[([0-9]+)\]/, "$1")];        
    } 
    return null;
}

// return element id of the radio button
function btnRadioGetElementID(name)
{
    if (name.match(/(.+)\[([0-9]+)\]/)) {
        var idx = name.replace(/(.+)\[([0-9]+)\]/, "$2")
        var groupID = asfGroupConnector[name.replace(/(.+)\[([0-9]+)\]/, "$1")];        
        return groupID + '_' + idx;
    } 
    return null;
}
// return group id of the close button
function btnCloseGetGroupID(id)
{
    if (id.match(/asfg_([0-9]+)_([0-9]+)_close/)) {
        return id.replace(/asfg_([0-9]+)_([0-9]+)_close/, "asfg_$1");
    }
    return null;
}

// return element id of the close button
function btnCloseGetElementID(id)
{
    if (id.match(/asfg_([0-9]+)_([0-9]+)_close/)) {
        return id.replace(/asfg_([0-9]+)_([0-9]+)_close/, "asfg_$1_$2");
    }
    return null;
}

function elementGetGroupID(elementID)
{
    if (elementID.match(/asfg_([0-9]+)_([0-9]+)/)) {
        return elementID.replace(/asfg_([0-9]+)_([0-9]+)/, "asfg_$1");
    }
    return null;
}

/* asf functions ends here */
function groupGetElementByID(elementID)
{
    return asfGroupList[elementGetGroupID(elementID)].elementList[elementID];
}

function asf_radio_clicked(name)
{
    var elementID = btnRadioGetElementID(name);
    var connectorType = groupGetElementByID(elementID).getCheckedRadioType();
    groupGetElementByID(elementID).resetConnectorType(connectorType);
}

function asf_close_clicked(id)
{
    groupGetElementByID(btnCloseGetElementID(id)).destroy();
}
