function selectAll1() {
     var isSelectAll;
     for(var i = 0; i < document.forms[0].elements.length; i++) {
        var type = document.forms[0].elements[i].type;
         if(type == "checkbox" ) {
             var name = document.forms[0].elements[i].name;
             if (name == "selectAll"){
                 isSelectAll = document.forms[0].elements[i].checked;
             }
              if (isSelectAll){
                  document.forms[0].elements[i].checked = true;
              }else{
                  document.forms[0].elements[i].checked = false;
              }
        }
    }
}

function setAction(action) {
    alert('action' + action);
    var actionInput = document.getElementById('action');
    actionInput.value = action;
}

function setActionTarget(action, target) {
    document.forms[0].action = action;
    document.forms[0].target = target;
}


function validateNumeric(action) {
    alert('action' + action);
    var actionInput = document.getElementById('action');
    actionInput.value = action;
}

function readOnly(element) {
    alert('element: ' + element);
    var actionInput = document.getElementById(element);
    actionInput.readOnly = true;
}




function deSelectAll() {
     for(var i = 0; i < document.forms[0].elements.length; i++) {
        var type = document.forms[0].elements[i].type;

         if(type == "checkbox" ) {
            document.forms[0].elements[i].checked = false;
        }
    }
}



function clearFields(action) {
    for(var i = 0; i < document.forms[0].elements.length; i++) {
        var type = document.forms[0].elements[i].type;
        if(type == "text" || type == "textarea" || type == "select-one") {
            document.forms[0].elements[i].value = '';
        } else if(type == "checkbox" || type == "radio") {
            document.forms[0].elements[i].checked = false;
        }
    }
    setInputValue(document.forms[0].action, action);
    try {
        document.forms[0].target.value = '';
    } catch(error) {
        if(debug) alert("Error in clearFields()" + error.toString() + " @ " + error.lineNumber);
    }
    document.forms[0].submit();

}

function show(name) {
    try {
//        alert('showing  name id: ' + name);
        var mainContainer = YAHOO.util.Dom.get(name);
        mainContainer.style.display = 'block';
    } catch(e) {
        alert("error showing:" + name);
    }
}

function hide(name) {
    try {
//        alert('hiding name id: ' + name);
        var mainContainer = YAHOO.util.Dom.get(name);
        mainContainer.style.display = 'none';
    } catch(e) {
        alert("error hiding:" + name + " error: " + e);
    }
}


function addCalendar( calname, calContainer, dateid) {


    YAHOO.namespace("example.calendar");

    YAHOO.example.calendar.init = function() {
//        YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1", "cal1Container");
        YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar(calname, calContainer);
        YAHOO.example.calendar.cal1.render();
//        YAHOO.example.calendar.cal1.selectEvent.subscribe(handleSelect1(dateid), YAHOO.example.calendar.cal1, true);
        YAHOO.example.calendar.cal1.selectEvent.subscribe(handleSelect1, YAHOO.example.calendar.cal1, true);

        updateCal(YAHOO.example.calendar.cal1, dateid);


//        var cal2 = new YAHOO.widget.Calendar("cal2", "cal2Container");
//        cal2.render();
//        cal2.selectEvent.subscribe(handleSelect3, YAHOO.example.calendar.cal2, true);

        //        updateCal();
//        updateCal2(YAHOO.example.calendar.cal1, "effDate");
//        updateCal2(cal2, "date2");

    }

    YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);
}

function handleSelect1(dateid) {
    handleSelect(type, args, obj, dateid)
}

function handleSelect2(type, args, obj) {
    handleSelect(type, args, obj, "date1")
}

function handleSelect3(type, args, obj) {
    handleSelect(type, args, obj, "date2")
}

function handleSelect(type, args, obj, element) {
    var dates = args[0];
    var date = dates[0];
    var year = date[0], month = date[1], day = date[2];

    //        var txtDate1 = document.getElementById("date1");
    var txtDate1 = document.getElementById(element);
    txtDate1.value = month + "/" + day + "/" + year;
}

function updateCal( cal, elementid) {
//    var txtDate1 = document.getElementById("date1");
    var txtDate1 = document.getElementById(elementid);
     alert("txtDate1.value:" + txtDate1.value);



    if(txtDate1.value != "") {
        cal.select(txtDate1.value);
//        YAHOO.example.calendar.cal1.select(txtDate1.value);
//        var selectedDates = YAHOO.example.calendar.cal1.getSelectedDates();
        var selectedDates = cal.getSelectedDates();
        if(selectedDates.length > 0) {
//            alert("txtDate1.value2:" + txtDate1.value);

            var firstDate = selectedDates[0];
//            YAHOO.example.calendar.cal1.cfg.setProperty("pagedate", (firstDate.getMonth() + 1) + "/" + firstDate.getFullYear());
            cal.cfg.setProperty("pagedate", (firstDate.getMonth() + 1) + "/" + firstDate.getFullYear());
//            YAHOO.example.calendar.cal1.render();
            cal.render();
        } else {
            alert("Cannot select a date before 1/1/2006 or after 12/31/2011");
        }

    }
}

function updateCal2(cal, elementid) {

    alert("elementid:" + elementid);
    var txtDate1 = document.getElementById(elementid);
//    alert("txtDate1.value:" + txtDate1.value);

    if(txtDate1.value != "") {
        cal.select(txtDate1.value);
        var selectedDates = cal.getSelectedDates();
        if(selectedDates.length > 0) {
            var firstDate = selectedDates[0];
            cal.cfg.setProperty("pagedate", (firstDate.getMonth() + 1) + "/" + firstDate.getFullYear());
            cal.render();
        } else {
            alert("Cannot select a date before 1/1/2006 or after 12/31/2011");
        }

    }
}



function formatDate(date){

    var month = date.getMonth();
    month++;
    return month + "/" +  date.getDate()  + "/" + date.getFullYear();

}


function addPopup(show, target, dialogContainer, calID){

    YAHOO.util.Event.onDOMReady(function() {

        var Event = YAHOO.util.Event,
                Dom = YAHOO.util.Dom,
                dialog,
                calendar;

        var showBtn = Dom.get(show);
//        var showBtn = Dom.get("show");

        Event.on(showBtn, "click", function() {

            // Lazy Dialog Creation - Wait to create the Dialog, and setup document click listeners, until the first time the button is clicked.
            if(!dialog) {

                // Hide Calendar if we click anywhere in the document other than the calendar
                Event.on(document, "click", function(e) {
                    var el = Event.getTarget(e);
                    var dialogEl = dialog.element;
                    if(el != dialogEl && !Dom.isAncestor(dialogEl, el) && el != showBtn && !Dom.isAncestor(showBtn, el)) {
                        dialog.hide();
                    }
                });

                function resetHandler() {
                    // Reset the current calendar page to the select date, or
                    // to today if nothing is selected.
                    var selDates = calendar.getSelectedDates();
                    var resetDate;

                    if(selDates.length > 0) {
                        resetDate = selDates[0];
                    } else {
                        resetDate = calendar.today;
                    }

                    calendar.cfg.setProperty("pagedate", resetDate);
                    calendar.render();
                }

                function closeHandler() {
                    dialog.hide();
                }

                dialog = new YAHOO.widget.Dialog(dialogContainer, {
                    visible:false,
//                    context:["show", "tl", "bl"],
                    context:[show, "tl", "bl"],
                    buttons:[
                        {
                            text:"Reset",
                            handler: resetHandler,
                            isDefault:true
                        },
                        {
                            text:"Close",
                            handler: closeHandler
                        }
                    ],
                    draggable:false,
                    close:true
                });
                dialog.setHeader('Pick A Date');
                dialog.setBody('<div id="' + calID + '"></div>');
                dialog.render(document.body);

                dialog.showEvent.subscribe(function() {
                    if(YAHOO.env.ua.ie) {
                        // Since we're hiding the table using yui-overlay-hidden, we
                        // want to let the dialog know that the content size has changed, when
                        // shown
                        dialog.fireEvent("changeContent");
                    }
                });
            }

            // Lazy Calendar Creation - Wait to create the Calendar until the first time the button is clicked.
            if(!calendar) {

//                calendar = new YAHOO.widget.Calendar("cal", {
                calendar = new YAHOO.widget.Calendar(calID, {
                    iframe:false,          // Turn iframe off, since container has iframe support.
                    hide_blank_weeks:true  // Enable, to demonstrate how we handle changing height, using changeContent
                });
                calendar.render();

                calendar.selectEvent.subscribe(function() {
                    if(calendar.getSelectedDates().length > 0) {

                        var selDate = calendar.getSelectedDates()[0];

                        // Pretty Date Output, using Calendar's Locale values: Friday, 8 February 2008
                        var wStr = calendar.cfg.getProperty("WEEKDAYS_LONG")[selDate.getDay()];
                        var dStr = selDate.getDate();
                        var mStr = calendar.cfg.getProperty("MONTHS_LONG")[selDate.getMonth()];
                        var yStr = selDate.getFullYear();

                        //                        var month = selDate.getMonth();
                        //                        month++;

                        //                        alert(month + "/" +  selDate.getDate()  + "/" + selDate.getFullYear())
                                                alert(formatDate(selDate));
                        //                        Dom.get("date").value = wStr + ", " + dStr + " " + mStr + " " + yStr;
//                        Dom.get("date").value = formatDate(selDate);
                        Dom.get(target).value = formatDate(selDate);
                    } else {
//                        Dom.get("date").value = "";
                        Dom.get(target).value = "";
                    }
                    dialog.hide();
                });

                calendar.renderEvent.subscribe(function() {
                    // Tell Dialog it's contents have changed, which allows
                    // container to redraw the underlay (for IE6/Safari2)
                    dialog.fireEvent("changeContent");
                });
            }

            var seldate = calendar.getSelectedDates();

            if(seldate.length > 0) {
                // Set the pagedate to show the selected date if it exists
                calendar.cfg.setProperty("pagedate", seldate[0]);
                calendar.render();
            }

            dialog.show();
        });
    });


}


