YUI.namespace('MCI');
YUI().use('yui2-container','yui2-calendar','node','event','dump', function(Y) {
    var YAHOO = Y.YUI2;

    ////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////
    YUI.MCI.NoticeContainer =
    new YAHOO.widget.Panel("wait",
    {
        width: "350px",
        fixedcenter: true,
        close: true,
        draggable: false,
        zindex:4,
        modal: true,
        visible: false
    }
    );

    YUI.MCI.NoticeContainer.setHeader("Loading, please wait...");
    YUI.MCI.NoticeContainer.setBody('<img src="http://admin.meyercomputer.com/UserIcons/main/ajax-loader.gif" />');
    YUI.MCI.NoticeContainer.render(document.body);

    ////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////
    var calObj;

    var popCalHandler = function(type, args, obj) {
        //Y.log(args);
        //Y.log(type);
        //Y.log(obj);
        var cal_id = obj;
        //Y.log(cal_id);
        var dates = args[0];
        var date = dates[0];
        var NewDate = date[1]+'/'+date[2]+'/'+date[0];
        //Y.log(NewDate);
        var monthStr = date[1];
        var dayStr = date[2];
        monthStr = monthStr.toString();
        dayStr = dayStr.toString();
        if (monthStr.length == 1) {
            monthStr = '0'+monthStr;
        }
        if (dayStr.length == 1) {
            dayStr = '0'+dayStr;
        }
        Y.one('#CalMonth_'+cal_id).set('value',monthStr);
        Y.one('#CalDay_'+cal_id).set('value',dayStr);
        var ie_year = date[0].toString();
        Y.one('#CalYear_'+cal_id).set('value',ie_year);
        if (cal_id == 'Arrival') {
            Y.one('#CalMonth_Departure').set('value',monthStr);
            Y.one('#CalDay_Departure').set('value',dayStr);
            Y.one('#CalYear_Departure').set('value',ie_year);
        }
        calObj.hide();
        calObj = '';

        //calObj.hide();
    }

    YUI.MCI.popCal = function (cal_id) {
        //Y.log(cal_id);
        var m = Y.one('#CalMonth_'+cal_id).get('value');
        var d = Y.one('#CalDay_'+cal_id).get('value');
        var y = Y.one('#CalYear_'+cal_id).get('value');
        //Y.log(m+'/'+d+'/'+y);
        if (Y.one('#popupCalContainer')) {
            var my = m + '/' + y;
            var mdy = m + '/' + d + '/' + y;
            if (!calObj) {
                calObj = new YAHOO.widget.Calendar ('popupCal', 'popupCalContainer', {
                    title:'Select&nbsp;Date...',
                    pageDate:my,
                    selected:mdy,
                    close:true
                });
                calObj.selectEvent.subscribe (popCalHandler, cal_id);
            } else {
                calObj.cfg.setProperty ('pageDate', my, false);
                calObj.cfg.setProperty ('selected', mdy, false);
            }
            var xy = Y.one('#cal_loc_'+cal_id).getXY();
            xy[0] += 20;
            xy[1] += 10;
            calObj.render();
            calObj.show();
            Y.one('#popupCalContainer').setXY(xy);
        }
    }
});


YUI().use('node','event','dump','anim', function(Y) {



    YUI.MCI.PC = function() {
        Y.one('#Calendar2').setStyle('display','none');
        Y.one('#Calendar1').setStyle('display','block');
        Y.one('#CalendarPrevious').setStyle('display','none');
        Y.one('#CalendarNext').setStyle('display','block');
    }
    YUI.MCI.NC = function() {
        Y.one('#Calendar2').setStyle('display','block');
        Y.one('#Calendar1').setStyle('display','none');
        Y.one('#CalendarPrevious').setStyle('display','block');
        Y.one('#CalendarNext').setStyle('display','none');
    }
    YUI.MCI.TabOn = function(Tab) {
        Y.all('#'+Tab+'Bottom b').setStyle('backgroundColor','#00FF00');
        Y.one('#'+Tab+'Top').setStyle('backgroundColor','#00FF00');
    }
    YUI.MCI.TabOff = function(Tab) {
        Y.all('#'+Tab+'Bottom b').setStyle('backgroundColor','#51A6EB');
        Y.one('#'+Tab+'Top').setStyle('backgroundColor','#51A6EB');
    }

    //////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////
    // Pop a Notice Box
    YUI.MCI.Notice = function (header,message) {
        YUI.MCI.NoticeContainer.setHeader('<div id="HeaderDiv">'+header+'</div>');
        YUI.MCI.NoticeContainer.setBody('<div id="NoticeDiv">'+message+'</div>');
        YUI.MCI.NoticeContainer.show();
    }

    YUI.MCI.AcceptBox = function() {
        var policy_node = Y.one('#Accept_Policies');
        var policy_box_node = Y.one('#PolicyBox');
        if (policy_node.get('checked') == true) {
            policy_box_node.setStyle('backgroundColor','#1E368D');
            policy_box_node.setStyle('borderWidth','0px');
            policy_box_node.setStyle('color','#FFFFFF');
        } else {
            policy_box_node.setStyle('backgroundColor','#FED4D4');
            policy_box_node.setStyle('border','#FF0000 solid 1px');
            policy_box_node.setStyle('color','#000000');
        }
    }

    YUI.MCI.CF1 = function() {
        var nodes = Y.all('.required');
        var message = '';
        var new_name = '';
        Y.each(nodes, function(a,b,c){
            //Y.log(a.get('type'));
            if (!a.get('value')) {
                new_name = a.get('name').replace(/_/g,' ');
                message = message + new_name+'<br />';
                //Y.log(Y.dump(a));
                a.setStyle('backgroundColor','#FED4D4');
                a.setStyle('border','#FF0000 solid 1px');
            } else {
                a.setStyle('backgroundColor','#51A6EB');
                a.setStyle('border','#000071 solid 1px');
            }
        })
        if (message) {
            YUI.MCI.Notice('Required Fields Missed:',message);
        } else {
            var policy_node = Y.one('#Accept_Policies');
            if (policy_node) {
                var policy_box_node = Y.one('#PolicyBox');
                if (policy_node.get('id') == 'Accept_Policies' && policy_node.get('checked') == false) {
                    message = 'You must accept the Rental Policies Agreement to continue booking.';
                    policy_box_node.setStyle('backgroundColor','#FED4D4');
                    policy_box_node.setStyle('border','#FF0000 solid 1px');
                    policy_box_node.setStyle('color','#000000');
                    YUI.MCI.Notice('Accept Rental Policies:',message);
                } else {
                    policy_box_node.setStyle('backgroundColor','#1E368D');
                    policy_box_node.setStyle('borderWidth','0px');
                    policy_box_node.setStyle('color','#FFFFFF');
                    Y.one('#BookingForm').submit();
                }
            } else {
                Y.one('#BookingForm').submit();
            }
        //Y.log('no message');
        }
    }

    YUI.MCI.CF2 = function() {
        var nodes = Y.all('.required');
        var message = '';
        var new_name = '';
        Y.each(nodes, function(a,b,c){
            //Y.log(a.get('type'));
            if (!a.get('value')) {
                new_name = a.get('name').replace(/_/g,' ');
                message = message + new_name+'<br />';
                //Y.log(Y.dump(a));
                a.setStyle('backgroundColor','#FED4D4');
                a.setStyle('border','#FF0000 solid 1px');
            } else {
                a.setStyle('backgroundColor','#51A6EB');
                a.setStyle('border','#000071 solid 1px');
            }
        })
        if (message) {
            YUI.MCI.Notice('Required Fields Missed:',message);
        } else {
            Y.one('#ContactForm').submit();
        }
    }

    YUI.MCI.SetBillingInfo = function() {
        var nodes = Y.all('.set');
        var from_name = '';
        var to_name = '';
        var new_value = '';
        if (Y.one('#SetBillingInfo').get('checked')) {
            Y.each(nodes, function(a,b,c){
                to_name = a.get('id');
                from_name = to_name.replace('billing_','');
                new_value = Y.one('#'+from_name).get('value');
                a.set('value',new_value);
            })
        } else {
            Y.each(nodes, function(a,b,c){
                a.set('value','');
            })
        }
    }

    YUI.MCI.CloseMessage = function() {
        Y.one('#Message').remove();
    }

    YUI.MCI.Failure = function() {
        alert('failure');
    }

    YUI.MCI.PrintReceipt = function() {
        var day = new Date();
        var id = day.getTime();
        var URL = 'Print.php';
        eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600,left = 600,top = 250');");
    }

    YUI.MCI.BookNow = function(id) {
        //Y.log(id);
        var url = 'http://www.rentalsmaui.com/BookNow.php?id='+id;

        if (Y.one('#QuoteAdults_'+id).get('value')) {
            url = url+'&adults='+Y.one('#QuoteAdults_'+id).get('value');
        }
        if (Y.one('#QuoteChildren_'+id).get('value')) {
            url = url+'&children='+Y.one('#QuoteChildren_'+id).get('value');
        }
        if (Y.one('#CalDay_Arrival').get('value')) {
            url = url+'&date_table_day='+Y.one('#CalDay_Arrival').get('value');
        }
        if (Y.one('#CalMonth_Arrival').get('value')) {
            url = url+'&date_table_month='+Y.one('#CalMonth_Arrival').get('value');
        }

        if (Y.one('#CalYear_Arrival').get('value')) {
            url = url+'&date_table_year='+Y.one('#CalYear_Arrival').get('value');
        }
        if (Y.one('#CalDay_Departure').get('value')) {
            url = url+'&date_table_day2='+Y.one('#CalDay_Departure').get('value');
        }
        if (Y.one('#CalMonth_Departure').get('value')) {
            url = url+'&date_table_month2='+Y.one('#CalMonth_Departure').get('value');
        }
        if (Y.one('#CalYear_Departure').get('value')) {
            url = url+'&date_table_year2='+Y.one('#CalYear_Departure').get('value');
        }
        //        if (Y.one('#QuoteNights').get('value') && Y.one('#QuoteAdults').get('value')) {
        //            url = url+'&task=Start2';
        //        }
        //Y.log(url);
        document.location.href=url;
    }

//    YUI.MCI.BookNow2 = function(id) {
//        var url = 'http://www.rentalsmaui.com/BookNow.php?id='+id;
////        if (Y.one('#Nights').get('value')) {
////            url = url+'&nights='+Y.one('#Nights').get('value');
////        }
//        if (Y.one('#CalDay_Arrival').get('value')) {
//            url = url+'&date_table_day='+Y.one('#CalDay_Arrival').get('value');
//        }
//        if (Y.one('#CalMonth_Arrival').get('value')) {
//            url = url+'&date_table_month='+Y.one('#CalMonth_Arrival').get('value');
//        }
//        if (Y.one('#CalYear_Arrival').get('value')) {
//            url = url+'&date_table_year='+Y.one('#CalYear_Arrival').get('value');
//        }
//        if (Y.one('#CalDay_Departure').get('value')) {
//            url = url+'&date_table_day2='+Y.one('#CalDay_Departure').get('value');
//        }
//        if (Y.one('#CalMonth_Departure').get('value')) {
//            url = url+'&date_table_month2='+Y.one('#CalMonth_Departure').get('value');
//        }
//        if (Y.one('#CalYear_Departure').get('value')) {
//            url = url+'&date_table_year2='+Y.one('#CalYear_Departure').get('value');
//        }
//        //        if (Y.one('#QuoteNights').get('value') && Y.one('#QuoteAdults').get('value')) {
//        //            url = url+'&task=Start2';
//        //        }
//        //Y.log(url);
//        document.location.href=url;
//    }

    YUI.MCI.Waiting = function(proc_id,args) {
        var box = args.box;
        var wait = '<div id="Message2" align="center"><img src="http://admin.meyercomputer.com/UserIcons/main/ajax-loader.gif"></div>';
        var node = Y.one('#'+box);
        node.setContent(wait);
    }

    YUI.MCI.Waiting2 = function(proc_id,args) {
        var box = args.box;
        var wait = '<div align="center"><img src="http://admin.meyercomputer.com/UserIcons/main/ajax-loader.gif"></div>';
        var node = Y.one('#'+box);
        node.setContent(wait);
    }

    YUI.MCI.init = function() {
        //Y.log('hey');
        var formNode = Y.one('#BookingForm');
        var formNode2 = Y.one('#ContactForm');
        var fieldNode = '';
        //var testString = '';
        if (formNode) {
            var handleBlur = Y.on('blur', function(e) {
                fieldNode = Y.one('#'+e.target.get('id'));
                if (fieldNode) {
                    if (fieldNode.get('value')) {
                        fieldNode.setStyle('backgroundColor','#51A6EB');
                        fieldNode.setStyle('border','#000071 solid 1px');
                    }
                }
            }, '#BookingForm');
        }
        if (formNode2) {
            handleBlur = Y.on('blur', function(e) {
                fieldNode = Y.one('#'+e.target.get('id'));
                if (fieldNode) {
                    if (fieldNode.get('value')) {
                        fieldNode.setStyle('backgroundColor','#51A6EB');
                        fieldNode.setStyle('border','#000071 solid 1px');
                    }
                }
            }, '#ContactForm');
        }
    }

    Y.on("domready", YUI.MCI.init);


});

YUI().use('node','io','dump', function(Y) {
    YUI.MCI.QuoteDiv = function(id) {
        var node = Y.one('#QuoteDiv_'+id);
        var QuoteDivStyle = node.getStyle('display');
        if (QuoteDivStyle == 'block') {
            node.setStyle('display','none');
        } else {
            node.setStyle('display','block');
        }
    }

    YUI.MCI.GetQuote = function(id) {
        //Y.log(Y.one('#CalMonth').get('value'));
        var message = '';
        var nodes = Y.all('.required_'+id);
        //Y.log(nodes);
        Y.each(nodes, function(a,b,c){
            //Y.log(a.get('type'));
            if (!a.get('value')) {
                //new_name = a.get('name').replace(/_/g,' ');
                message = message + a.get('name')+'<br />';
                //Y.log(Y.dump(a));
                a.setStyle('backgroundColor','#FED4D4');
                a.setStyle('border','#FF0000 solid 1px');
            } else {
                a.setStyle('backgroundColor','#51A6EB');
                a.setStyle('border','#000071 solid 1px');
            }
        })

        if (message) {
            YUI.MCI.Notice('Required fields missed:',message);
        } else {
            //Y.log('QuoteForm_'+id);
            var cfg = {
                method: 'POST',
                form: {
                    id: 'QuoteForm_'+id,
                    useDisabled: true
                },
                arguments: {
                    box: 'QuoteResults_'+id,
                    id: id
                }
            };
        var uri = "http://www.rentalsmaui.com/AJAX/Main.php";
        var request = Y.io(uri, cfg);
        }
    }
    function Complete(proc_id, o, args) {
        var id = args.id;
        //Y.log(o.responseText);
        Y.one('#QuoteResults_'+id).setContent(o.responseText);
    }
    Y.on('io:start', YUI.MCI.Waiting)
    Y.on('io:success', Complete);
    Y.on('io:failure', YUI.MCI.Failure);
});

YUI().use('node','io','dump', function(Y) {
    YUI.MCI.GetDays = function(id) {
        var daysNode2 = Y.one('#DaysDiv2_'+id);
        if (daysNode2) {
            if (daysNode2.getStyle('display') == 'block') {
                daysNode2.setStyle('display','none');
            } else {
                daysNode2.setStyle('display','block');
            }
        } else {
            var cfg = {
                method: 'POST',
                form: {
                    id: 'SearchForm',
                    useDisabled: true
                },
                arguments: {
                    box: 'DaysDiv_'+id,
                    id: id
                }

            };
            var uri = "http://www.rentalsmaui.com/AJAX/GetDays.php?id="+id;
            var request = Y.io(uri, cfg);
        }
    }
    function Complete(proc_id, o, args) {
        Y.one('#DaysDiv_'+args.id).setContent(o.responseText);
    }
    Y.on('io:start', YUI.MCI.Waiting2)
    Y.on('io:success', Complete);
    Y.on('io:failure', YUI.MCI.Failure);
});
