/* for scal prototype calendar controls.. */
    //Extend the scal library to add draggable calendar support.
    //This script block can be added to the scal.js file.
    Object.extend(scal.prototype, {
        toggleCalendar: function()
{
var element = this.element.identify() + "-container";
if($(element).visible())
{
$(element).hide();
} else
{
$(element).show();
}},
        _click: function(event,cellIndex) {
			this.element.select('.dayselected').invoke('removeClassName', 'dayselected');
			(event.target.hasClassName('daybox') ? event.target : event.target.up()).addClassName('dayselected');
			this._setCurrentDate(this.dateRange[cellIndex]);
			this._updateExternal();
			this.closeCalendar(); // added by ben to close calendar on click selection of date.
        },
        isOpen: function() { 
            return ( $(this.options.wrapper) || this.element).visible();
        }
    });
    //this is a global variable to have only one instance of the calendar
    var calendar = null;
    //@element   => is the <div> where the calender will be rendered by Scal.
    //@input     => is the <input> where the date will be updated.
    //@container => is the <div> for dragging.
    //@source    => is the img/button which raises up the calender, the script will locate the calendar over this control.
    function findPos(obj) {
    //find coordinates of a DIV
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
    }
	function showCalendar(element, input, container, source) {
		if (!calendar) {
			container = $(container);
            //the Draggable handle is hard coded to "rtop" to avoid other parameter.
			new Draggable(container, {handle: "calendar-header", starteffect: Prototype.emptyFunction, endeffect: Prototype.emptyFunction});
            //The singleton calendar is created.
			calendar = new scal(element, $(input), {
				oncalchange: function(d) {
					calendar._updateExternal(); // update input on press of controls.
				},
                updateformat: 'dd/mm/yyyy', 
                closebutton: '&nbsp;', 
                wrapper: container
            });
        } else {
            calendar.updateelement = $(input);
        }
        var date = new Date();
        var formattedDateStr = $(input).value;
		tempArray = formattedDateStr.split("/");
		date.setDate(tempArray[0]);
		date.setMonth(tempArray[1] - 1);
		date.setFullYear(tempArray[2]);
        calendar.setCurrentDate(isNaN(date) ? new Date() : date);
        //Locates the calendar over the calling control  (in this example the "img").
		if (source = $(source)) {
            var thePos = findPos(source);
            $(container).setStyle({
                left: thePos[0] + 10 + 'px',
                top: thePos[1] + 10 + 'px'
            });
		}
		//finally show the calendar =)
        calendar.openCalendar();
	    Event.observe(document, 'click', function (event) { // close the calendar if you click outside it
	    	var theTarget = event.target;
	    	if(theTarget.id != 'depdate' && theTarget.id != 'retdate'){
		    	theTarget = $(theTarget).up('#calendar');
		    	if(!theTarget){
		    		calendar.closeCalendar();
		    	}
		    }
		});
    };
    var imgCalendar_Click = function(e, input) {
        showCalendar("calendar", input, "calendar-container", Event.element(e));
    };
    
    Event.observe(window, "load", function(e) {
    	if($('depdate')){
        	Event.observe("depdate", "click", imgCalendar_Click.bindAsEventListener(this, "depdate"));
        	Event.observe("retdate", "click", imgCalendar_Click.bindAsEventListener(this, "retdate"));
        }
        var quoteSubmit = $('quote-submit');
		if(quoteSubmit){
		    quoteSubmit.observe('mouseover', function() {
			  this.setStyle({
			  	cursor: 'pointer'
			  });
			});        
		}
		var step3Chick = $('step3-chick');
		if(step3Chick){
			step3Chick.observe('click', function() {
				$('quoteForm').submit();
			});
			step3Chick.observe('mouseover',function(){
				this.setStyle({
					cursor: 'pointer'
				});
			});
		}
    });  
    
