
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js' type='text/javascript'></script>");
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/element/element-min.js' type='text/javascript'></script>");
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/container/container_core-min.js' type='text/javascript'></script>");
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/calendar/calendar-min.js' type='text/javascript'></script>");
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/connection/connection-min.js' type='text/javascript'></script>");
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/animation/animation-min.js' type='text/javascript'></script>");
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/dragdrop/dragdrop-min.js' type='text/javascript'></script>");
document.write("<script src='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/container/container-min.js' type='text/javascript'></script>");

document.write("<link rel='stylesheet' type='text/css' href='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/fonts/fonts-min.css' />");
//document.write("<link rel='stylesheet' type='text/css' href='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/container/assets/skins/sam/container.css' />");
document.write("<link rel='stylesheet' type='text/css' href='https://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/calendar/assets/skins/sam/calendar.css' />");

YAHOO.namespace("wisechoice.tools");

function showDialog(title,viewUrl,modalBoolean,closableBoolean,draggableBoolean, dialogWidth, dialogHeight){
	if(!document || !document.body){
		return;
	}	
	document.body.className='yui-skin-sam';
	
	//Build the panel
    YAHOO.wisechoice.tools.lightbox = 
            new YAHOO.widget.Panel("lightbox",  
                                            { width: dialogWidth,
            								  height: dialogHeight,
                                              fixedcenter: true, 
                                              close: closableBoolean, 
                                              draggable: draggableBoolean, 
                                              zindex:4,
                                              modal: modalBoolean,
                                              visible: false
                                            } 
                                        );    
    YAHOO.wisechoice.tools.lightbox.setHeader(title);
    YAHOO.wisechoice.tools.lightbox.setBody("Loading Message...");
    YAHOO.wisechoice.tools.lightbox.render(document.body);

	//Define the action to take when the view is returned
    var callback = {
            success : function(o) {
    			if (isSessionValid(o.responseText)){
    				YAHOO.wisechoice.tools.lightbox.setBody(o.responseText);
    			}else{
    				self.location.href="/wclogin";
    			}
    			
            },
            failure : function(o) {
                YAHOO.wisechoice.tools.lightbox.hide();
            }
        }
    
	// Show the panel
    YAHOO.wisechoice.tools.lightbox.show();

	//Load the view
    var conn = YAHOO.util.Connect.asyncRequest("POST", viewUrl, callback);
}

function fireEvent(element,event){
	var evt;
    if (document.createEventObject){
        // dispatch for IE
        evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
        // dispatch for firefox + others
        evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}

function attachDatePicker(containerId,buttonId,inputId,caltitle,onChangeStatement){
	if(!document || !document.body){
		return;
	}	
	document.body.className='yui-skin-sam';
	
	function handleSelect(type,args,obj) {
		var dates = args[0]; 
		var date = dates[0];
		var year = date[0], month = date[1], day = date[2];
		
		var txtdue_date = document.getElementById(inputId);
		var newvalue = month + "/" + day + "/" + year;
		if(txtdue_date.value != newvalue)
		{
			//alert('oldvalue='+txtdue_date.value+',newvalue='+newvalue);
			txtdue_date.value = newvalue;
			fireEvent(txtdue_date,'change');
			eval(onChangeStatement);
		}
		YAHOO.wisechoice.tools.cals[inputId].hide();
	}
	
	function updateCal(e,inputIdName) {
		if(!document || !document.body){
			return;
		}		
		if(!inputIdName){
			inputIdName = inputId;			
		}
		
		var txtdue_date = document.getElementById(inputId);
		if (txtdue_date.value != "" && YAHOO.wisechoice.tools.cals[inputIdName]) {
			YAHOO.wisechoice.tools.cals[inputIdName].select(txtdue_date.value);
			var selectedDates = YAHOO.wisechoice.tools.cals[inputIdName].getSelectedDates();
			if (selectedDates.length > 0) {
				var firstDate = selectedDates[0];
				YAHOO.wisechoice.tools.cals[inputIdName].cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
				YAHOO.wisechoice.tools.cals[inputIdName].render();
			}			
		}
	}	
	
	function showCalendar(e, inputId) {
		if(!document || !document.body){
			return;
		}		
		var container = document.getElementById(containerId);
		
		if (getStyle(containerId,'display') == 'none'){
			YAHOO.wisechoice.tools.cals[inputId].show();
		}
		else{
			YAHOO.wisechoice.tools.cals[inputId].hide();
		}	
	}
	
	function getStyle(el,styleProp)
	{
		var x = document.getElementById(el);
		if (x.currentStyle)
			var y = x.currentStyle[styleProp];
		else if (window.getComputedStyle)
			var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
		return y;
	}
	
	if(!YAHOO.wisechoice.tools.cals){
		YAHOO.wisechoice.tools.cals = new Array();
	}
	YAHOO.wisechoice.tools.cals[inputId] = new YAHOO.widget.Calendar("cal1",containerId,{ title:caltitle, close:true });
	YAHOO.wisechoice.tools.cals[inputId].selectEvent.subscribe(handleSelect, YAHOO.wisechoice.tools.cals[inputId], true);
	YAHOO.wisechoice.tools.cals[inputId].render();	
	
	YAHOO.util.Event.addListener(inputId, "blur", updateCal,inputId);
	YAHOO.util.Event.addListener(buttonId, "click", showCalendar, inputId);	
	updateCal();	
}


function attachBlankDatePicker(containerId,buttonId,inputId,caltitle,defaultDate){
	if(!document || !document.body){
		return;
	}	
	document.body.className='yui-skin-sam';
	
	function handleSelect(type,args,obj) {
		var dates = args[0]; 
		var date = dates[0];
		var year = date[0], month = date[1], day = date[2];
		
		var txtdue_date = document.getElementById(inputId);
		var newvalue = month + "/" + day + "/" + year;
		if(txtdue_date.value != newvalue)
		{
			//alert('oldvalue='+txtdue_date.value+',newvalue='+newvalue);
			txtdue_date.value = newvalue;
			fireEvent(txtdue_date,'change');
		}
		YAHOO.wisechoice.tools.cals[inputId].hide();
	}
	
	function updateCal(e,inputIdName) {
		if(!document || !document.body){
			return;
		}		
		if(!inputIdName){
			inputIdName = inputId;			
		}
		
		if (defaultDate != "" && YAHOO.wisechoice.tools.cals[inputIdName]) {			
			YAHOO.wisechoice.tools.cals[inputIdName].select(defaultDate);
			var selectedDates = YAHOO.wisechoice.tools.cals[inputIdName].getSelectedDates();
			if (selectedDates.length > 0) {
				var firstDate = selectedDates[0];
				YAHOO.wisechoice.tools.cals[inputIdName].cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
				YAHOO.wisechoice.tools.cals[inputIdName].render();
			}			
		}
	}	
	
	function showCalendar(e, inputId) {
		if(!document || !document.body){
			return;
		}		
		
		//on first call, we havent set the calendar date yet
		var selectedDates = YAHOO.wisechoice.tools.cals[inputId].getSelectedDates();
		if (selectedDates.length == 0) {
			updateCal();			
		}
		
		var container = document.getElementById(containerId);
		
		if (getStyle(containerId,'display') == 'none'){
			YAHOO.wisechoice.tools.cals[inputId].show();
		}
		else{
			YAHOO.wisechoice.tools.cals[inputId].hide();
		}	
	}
	
	function getStyle(el,styleProp)
	{
		var x = document.getElementById(el);
		var y;
		if (x.currentStyle)
			y = x.currentStyle[styleProp];
		else if (window.getComputedStyle)
			y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
		return y;
	}
	
	if(!YAHOO.wisechoice.tools.cals){
		YAHOO.wisechoice.tools.cals = new Array();
	}
	YAHOO.wisechoice.tools.cals[inputId] = new YAHOO.widget.Calendar("cal1",containerId,{ title:caltitle, close:true });
	YAHOO.wisechoice.tools.cals[inputId].selectEvent.subscribe(handleSelect, YAHOO.wisechoice.tools.cals[inputId], true);
	YAHOO.wisechoice.tools.cals[inputId].render();	
	
	//YAHOO.util.Event.addListener(inputId, "blur", updateCal,inputId);
	YAHOO.util.Event.addListener(buttonId, "click", showCalendar, inputId);	
	//updateCal();	
}



