/**
 * @author VHAANNJonesJ
 * Medora Poppies Utilities class  - provides basic functionality
 * for running Poppies, like inserting spinning "please wait"-type 
 * graphics while a service call is underway, or handling errors, and such.
 */
if (typeof medora == 'undefined')
{
	Type.registerNamespace('medora');
}
if (typeof medora.poppies == 'undefined')
{
	Type.registerNamespace('medora.poppies');
}
if (typeof medora.poppies.utilities == 'undefined')
{
	medora.poppies.utilities=function() 
	{ 
		medora.poppies.utilities.initializeBase(this);
		this.ErrorAndTimeOutHandler = null;
		this.FixVistaDate = null;
		this.ToggleSpinner = null;
		this.applyClassToElement = null;
	}
}

//////////////// Error Handler /////////////////////////////////////
medora.poppies.utilities.ErrorAndTimeOutHandler = function(arg,userContext)
{
	var message  =  (typeof arg._message == "undefined") ? arg.message : arg._message;
	alert("Error: " + message);

	this.ToggleSpinner(userContext.spinnerName);
}
//////////////////////////////////////////////////////////////////

medora.poppies.utilities.FixVistaDate = function(vistaDateString,format)
{
    if (vistaDateString != "")
    {
        var formattedDate = "";
		var year = ""; 
        var month = "";
        var day = "";
		try
		{
	        if (vistaDateString.indexOf(".") > -1) 
			{
				vistaDateString = vistaDateString.split(".");
				formattedDate = vistaDateString[0];
			}
			else
			{
				formattedDate = vistaDateString;
			}
	        year = formattedDate.substr(0,4);
	        month = formattedDate.substr(4,2);
	        day = formattedDate.substr(6,2);
		}
		finally
		{
			switch(format)
			{
			case "yyyyMMdd":
			  formattedDate = year + month + day;
			  break;    
			default:
			  formattedDate = month + "/" + day + "/" + year
			} 
			return formattedDate;
		}    
    }
}

medora.poppies.utilities.ToggleSpinner = function(elementId)
{
    var spinnerImg = document.getElementById(elementId)
    if (spinnerImg.className.indexOf("makeInvisible") > -1 || spinnerImg.className == "spinnerPos") 
    {
        spinnerImg.className = spinnerImg.className.replace(" makeInvisible","");
		spinnerImg.className += " makeVisible";
    }
    else
    {
		spinnerImg.className = spinnerImg.className.replace(" makeVisible","");
        spinnerImg.className += " makeInvisible";
    }
}

medora.poppies.utilities.applyClassToElement = function(elementId,classname)
{
	var element =  $get(elementId);
    element.className = element.className.replace(" " + classname,"");
	element.className += " " + classname;
}
medora.poppies.utilities.CreateModalWindow = function(windowName,header,text,width,height)
{
	var widthVal = (typeof width != "undefined" && width != "") ? width : "300";
	var heightVal = (typeof height != undefined && height != "") ? height : "400";
	// Initialize temporary Panel to display info
	myAlertPanel = 
			new YAHOO.widget.Panel(windowName,  
				{ width:widthVal + "px", 
				  height:heightVal + "px",
				  fixedcenter:true, 
				  close:false, 
				  draggable:false, 
				  zindex:4,
				  modal:true,
				  visible:false
				} 
			);

	myAlertPanel.setHeader(header);
	myAlertPanel.setBody(text);
	myAlertPanel.render(document.body);
	return myAlertPanel;
}