











/*
Patch #1 requested by Lohika related to bug #39572 fix
*/


YAHOO.util.Connect.setForm = function(formId, isUpload, secureUri)
{
    this.resetFormState();
    var oForm;
    if(typeof formId == 'string'){
        // Determine if the argument is a form id or a form name.
        // Note form name usage is deprecated by supported
        // here for legacy reasons.
        oForm = (document.getElementById(formId) || document.forms[formId]);
    }
    else if(typeof formId == 'object'){
        // Treat argument as an HTML form object.
        oForm = formId;
    }
    else{
        return;
    }

    // If the isUpload argument is true, setForm will call createFrame to initialize
    // an iframe as the form target.
    //
    // The argument secureURI is also required by IE in SSL environments
    // where the secureURI string is a fully qualified HTTP path, used to set the source
    // of the iframe, to a stub resource in the same domain.
    if(isUpload){

        // Create iframe in preparation for file upload.
        this.createFrame(secureUri?secureUri:null);

        // Set form reference and file upload properties to true.
        this._isFormSubmit = true;
        this._isFileUpload = true;
        this._formNode = oForm;

        return;
    }

    var oElement, oName, oValue, oDisabled;
    var hasSubmit = false;

    // Iterate over the form elements collection to construct the
    // label-value pairs.
    for (var i=0; i<oForm.elements.length; i++){
        oElement = oForm.elements[i];
        oDisabled = oForm.elements[i].disabled;
        oName = oForm.elements[i].name;
        oValue = oForm.elements[i].value;

        // Do not submit fields that are disabled or
        // do not have a name attribute value.
        if(!oDisabled && oName)
        {
            switch (oElement.type)
            {
/************************************ PATCH
                case'select-one':
************************************* PATCH */
                case 'select-multiple':
                    for(var j=0; j<oElement.options.length; j++){
                        if(oElement.options[j].selected){
                            if(window.ActiveXObject){
                                this._sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oElement.options[j].attributes['value'].specified?oElement.options[j].value:oElement.options[j].text) + '&';
                            }
                            else{
                                this._sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oElement.options[j].hasAttribute('value')?oElement.options[j].value:oElement.options[j].text) + '&';
                            }

                        }
                    }
                    break;
                case 'radio':
                case 'checkbox':
                    if(oElement.checked){
                        this._sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
                    }
                    break;
                case 'file':
                    // stub case as XMLHttpRequest will only send the file path as a string.
                case undefined:
                    // stub case for fieldset element which returns undefined.
                case 'reset':
                    // stub case for input type reset button.
                case 'button':
                    // stub case for input type button elements.
                    break;
                case 'submit':
                    if(hasSubmit === false){
                        if(this._hasSubmitListener){
                            this._sFormData += this._submitElementValue + '&';
                        }
                        else{
                            this._sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
                        }

                        hasSubmit = true;
                    }
                    break;
                default:
                    this._sFormData += encodeURIComponent(oName) + '=' + encodeURIComponent(oValue) + '&';
                    break;
            }
        }
    }

    this._isFormSubmit = true;
    this._sFormData = this._sFormData.substr(0, this._sFormData.length - 1);


    return this._sFormData;
}


/*
End Patch #1
*/











get = function(id) {
    return YAHOO.util.Dom.get(id);
}

// IE6 calls onContentReady too early, resulting in JS errors.
// Use this custom method instead of the standard YUI method to avoid this problem.
YAHOO.webui.onContentReady = function (content_id, event_handler) {
    if(navigator.userAgent.indexOf('MSIE ') != -1 && navigator.platform == 'Win32') {
        YAHOO.util.Event.addListener(window, "load", event_handler);
    }
    else {
        YAHOO.util.Event.onContentReady(content_id, event_handler);
    }
}


YAHOO.widget.MenuItem.prototype.IMG_ROOT = "/images/";

YAHOO.webui.TinyMenu = function (menu_id, menu_data) {
    var ns = YAHOO.namespace("webui."+menu_id);
    ns.menu_id = menu_id;
    ns.menu_data = menu_data;
    ns.init = function(p_oEvent) {
        ns.beforeRender = function (p_sType, psArgs, poMenu) {
            for(var i=0; i<menu_data.length; i++) {
                  var menu = ns.widget.getItem(i)
                  if(menu){
                      menu.cfg.setProperty("submenu",
                          { id:ns.menu_id+"_"+i, itemdata: menu_data[i] });
//                      menu.cfg.setProperty("onclick",
//                          {fn: YAHOO.util.Event.stopEvent, obj: this, scope: this});
                  }
            }
            YAHOO.util.Event.on("nav_menu", "click", topMenuClickHandler);
        }
        ns.widget = new YAHOO.widget.MenuBar(ns.menu_id,
            {'lazyload': true,
             'zindex': 1000,
             'constraintoviewport': true,
             'submenualignment': ['tr','br'],
             'autosubmenudisplay': true,
             'showdelay': 0,
             'hidedelay': 850,
             'submenuhidedelay': 850});
        ns.widget.beforeRenderEvent.subscribe(ns.beforeRender, ns.widget, true);
        ns.widget.render();
        ns.widget.show();
    }
    YAHOO.webui.onContentReady(ns.menu_id, ns.init);
    return ns;
}

    function topMenuClickHandler(e) {
       // ===================================================
       // function to block click event for top menu items...
       // see bug 33542 for details...
       // ===================================================
       var elTarget = YAHOO.util.Event.getTarget(e);
       if(elTarget.className && (elTarget.className.indexOf("hassubmenu")>-1
         || elTarget.className.indexOf("submenuindicator")>-1)) {
           YAHOO.util.Event.stopEvent(e);
           YAHOO.util.Event.preventDefault(e);
           YAHOO.log("prevented CLICK event");
       }
    }
             

YAHOO.webui.NavBar = function (namespace_id, menu_id, menu_data) {
    var ns = YAHOO.namespace(namespace_id);
    ns.menu_data = menu_data;
    ns.init = function(p_oEvent) {
        ns.widget = new YAHOO.widget.MenuBar(menu_id,
            {'lazyload': true,
             'zindex': 9001,
             'constraintoviewport': true,
             'autosubmenudisplay': true,
             'submenuhidedelay': 800,
             'hidedelay': 800,
             'showdelay': 0});
        ns.widget.beforeRenderEvent.subscribe(ns.beforeRender, ns.widget, true);
        ns.widget.render();
        ns.widget.show();
    }
    ns.beforeRender = function(p_sType, psArgs, poMenu) {
        alert("Override this method");
    }
    YAHOO.webui.onContentReady(menu_id, ns.init);
    return ns;
}
               
YAHOO.webui.buildModalDialog = function (namespace_id, dialog_id, ok, cancel) {
    return YAHOO.webui.LargeDialog(namespace_id, dialog_id, ok, cancel, true, false);
}
YAHOO.webui.buildDragDialog = function (namespace_id, dialog_id, ok, cancel) {
    return YAHOO.webui.LargeDialog(namespace_id, dialog_id, ok, cancel, false, true);
}
YAHOO.webui.LargeDialog = function (namespace_id, dialog_id,
                                    ok, cancel, modal, draggable, show_close) {
    if (show_close == undefined) {
      var show_close = true;
    }
    // Construct a namespace for this dialog (and return it later.)
    var ns = YAHOO.namespace(namespace_id);
    ns.init = function () {
        ns.element = get(dialog_id);
        ns.widget = new YAHOO.widget.SimpleDialog(dialog_id, {visible:false,
           fixedcenter:true, zIndex:200, modal:modal, constraintoviewport:false,
           draggable:draggable, close:show_close});
        var buttons = [];
        if (ok) buttons[0] = { text:ok, handler:ns.handleOK, isDefault:true };
        if (cancel) buttons[buttons.length] = {text:cancel, handler:ns.handleCancel};
        ns.okButtonText = ok;
        ns.buttons = buttons;
        ns.widget.cfg.queueProperty("buttons", buttons);
        var listeners = new YAHOO.util.KeyListener(document,
            { keys : 27 },
            {fn:ns.handleCancel, scope:ns.widget, correctScope:true});
        ns.widget.cfg.queueProperty("keylisteners", listeners);
        ns.widget.render(document.body);
    }
    YAHOO.webui.onContentReady(dialog_id, ns.init);

    // Override these methods after calling buildModalDialog.
    ns.show = function(e) {
        ns.widget.show();
    }
    ns.hide = function(e) {
        ns.widget.hide();
    }
    ns.handleCancel = function(e) {
        ns.widget.hide();
    }
    ns.handleOK = function(e) {
        alert("Override this method!");
        ns.widget.hide();
    }
        // quick fix
        // now it works fine now only with one validation field...

    ns.updateOK = function() {

        // this function should be used in the future...
        // it should check if there are active errors
        // and if there are any style OK button with error style
        // there should be an array of active errors...
    }

    ns.showOKError = function(bool){
        if(bool){
            ns.styleOK("error");
        }
        else{
            ns.styleOK("default");
        }
    }

    ns.styleOK = function(className){
        var okButton = ns.getOKButton();
        if(okButton){
            okButton.className = className;
        }
    }

    ns.getOKButton = function(){
        if(ns.okButton == undefined || ns.okButton == null){
//            ns.okButton = ns.getButtonByText(ns.okButtonText);
            ns.okButton = ns.getDefaultButton();
        }
        return ns.okButton;
    }
    ns.getButtonByText = function(str){
        var searchedStr = new String(str);
        var bts=ns.widget.cfg.getProperty("buttons");
        for(var i = 0; i < bts.length; i++){
            var b = bts[i];
            if(b){
                //if(b.text.toString().toLowerCase() == str.toString().toLowerCase() ){
                var txt = new String(b.text);
                if(txt.toLowerCase() == searchedStr.toLowerCase() ) {
                    var htmlButton = b.htmlButton;
                    if(htmlButton){
                        return htmlButton
                    }
                }
            }
        }
        return null;
    }

    ns.getDefaultButton = function(){
        var bts=ns.widget.cfg.getProperty("buttons");
        for(var i = 0; i < bts.length; i++){
            var b = bts[i];
            if(b){
                if(b.isDefault ) {
                    var htmlButton = b.htmlButton;
                    if(htmlButton){
                        return htmlButton
                    }
                }
            }
        }
        return null;
    }
    ns.enableOK = function(){
        var okButton = ns.getOKButton()
        if(okButton){
              okButton.disabled = false;
              ns.styleOK("default");
         }
    }
    ns.disableOK = function(){
        var okButton = ns.getOKButton();
        if(okButton){
             okButton.disabled = true;
             ns.styleOK("disabled");
         }
    }
    return ns;
}
          
YAHOO.webui.buildBubbleDialog = function (namespace_id, dialog_id, parentDivName) {
    return YAHOO.webui.BubbleDialog(namespace_id, dialog_id, false, 'ok', 'cancel', false, false, true, true, parentDivName);
}
YAHOO.webui.BubbleDialog = function (namespace_id, dialog_id, show_buttons,
                                    ok, cancel, fixedcenter, modal,
                                    constraintoviewport, draggable, parentDivName) {
    // Construct a namespace for this dialog (and return it later.)
    var ns = YAHOO.namespace(namespace_id);
    ns.init = function () {
        ns.element = get(dialog_id);
        ns.widget = new YAHOO.widget.Panel("win", { width:"220px", fixedcenter: false, constraintoviewport: true, close:true, visible:false, draggable:true, modal:false } );
        var listeners = new YAHOO.util.KeyListener(document,
            { keys : 27 },
            {fn:ns.handleCancel, scope:ns.widget, correctScope:true});
        ns.widget.cfg.queueProperty("keylisteners", listeners);
		var parentDiv = null;
		if(!parentDivName){
			parentDivName = "bubbleParentDiv";
		}
		if(parentDivName){
			parentDiv = document.getElementById(parentDivName);			
		}
		if(parentDiv){
	        ns.widget.render(parentDiv);			
		}
		else{
	        ns.widget.render(document.body);			
		}
    }
    YAHOO.util.Event.addListener(window, "load", ns.init);

    // Override these methods after calling buildModalDialog.
    ns.show = function(e) {
        ns.widget.show();
    }
    ns.hide = function(e) {
        ns.widget.hide();
    }
    ns.handleCancel = function(e) {
        ns.widget.hide();
    }
    return ns;
}
         
YAHOO.webui.SmallDialog = function (namespace_id, dialog_id,
                                    title, message, ok, cancel, onOk, onCancel,
                                    modal, draggable) {
    // Construct a namespace for this dialog (and return it later.)
    var ns = YAHOO.namespace(namespace_id);
    ns.onOk = onOk;
    ns.onCancel = onCancel;
    ns.element = get(dialog_id);
    ns.widget = new YAHOO.widget.Dialog(dialog_id, {visible:false,
        width:'350px', zIndex:200, fixedcenter:true, modal:modal,
        constraintoviewport:true, draggable:draggable});
    ns.show = function(e) {
        ns.widget.show();
    }
    ns.hide = function(e) {
        ns.widget.hide();
    }
    ns.handleCancel = function(e) {
        if (ns.onCancel)
            eval(ns.onCancel);
        ns.widget.hide();
    }
    ns.handleOK = function(e) {
        eval(ns.onOk);
        ns.widget.hide();
    }
    if (title)
        ns.widget.setHeader(title);
    if (message)
        ns.widget.setBody(message);

    var buttons = [];
    if (ok) buttons[0] = { text:ok, handler:ns.handleOK, isDefault:true };
    if (cancel) buttons[buttons.length] = {text:cancel, handler:ns.handleCancel};
    ns.widget.cfg.queueProperty("buttons", buttons);
    var listeners = new Array(
        new YAHOO.util.KeyListener(document,
          {keys:27}, {fn:ns.handleCancel, scope:ns.widget, correctScope:true}),
        new YAHOO.util.KeyListener(document,
          {keys:13}, {fn:ns.handleOK, scope:ns.widget, correctScope:true}))
    ns.widget.cfg.queueProperty("keylisteners", listeners);
    ns.widget.render(document.body);

    return ns;
}
         

// Custom js functions for Query Info Dialog Widget
function toggleDialogBD(dlg, hide) {
    // function hides or shows bd container for the dlg dialog object
    var display_text = "";
    if (hide) display_text = "none";
    for (var i=0; i<dlg.element.childNodes.length; i++) {
        if (dlg.element.childNodes[i].className == 'bd') {
            dlg.element.childNodes[i].style.display = display_text;
        }
    }
}

function queryInfoHandleOk(dlg) {
    // Custom function perform close action
    dlg.handleOK = function() {
            toggleDialogBD(dlg, true);
            dlg.hide();
        }

    // Custom function perform close for cancel action
    dlg.handleCancel = function() {
            toggleDialogBD(dlg, true);
            dlg.hide();
        }
}

function showQueryInfoDialog(dlg, header) {

    // There is a problem with the width getting set in Opera
    // so this code will set it manually if the client is Opera.
    if (window.opera) {
        if (dlg.element.style.width == '0px') {
            dlg.element.style.width = '644px';
        }
    }

    //show bd div before displaying
    toggleDialogBD(dlg, false);
    dlg.widget.setHeader(header);
    dlg.show();

    // On the first showing of the dialog, the centering code
    // is not running properly so we run it manually here just
    // for Opera.
    if (window.opera) {
        dlg.widget.center();
    }
}


function jslog (msg, reset) {
    // do no thing
}



