var fieldBG = "#CCCCCC";
var fieldText = "#FFFFFF";
var normalBG = "#FFFFFF";
var normalText = "#000000";

if(!window.Node)
{
	var Node = {
			ELEMENT_NODE: 1,
			ATTRIBUTE_NODE: 2,
			TEXT_NODE: 3,
			COMMENT_NODE: 8,
			DOCUMENT_NODE: 9,
			DOCUMENT_FRAGMENT_NODE: 11
	};
}

function show(theobject)
{
//	theobject.style.display='inline';
	Effect.BlindDown(theobject,{duration:0.25});
}
function hide(theobject)
{
//	theobject.style.display='none';
	Effect.BlindUp(theobject,{duration:0.25});
}

function setField(fieldId,theValue)
{
	$(fieldId).value = theValue;
}

function validateForm(formName)
{
	sAlert = "";
	
	//check main fields
	var aCheckFields = arguments[1] || [];
	var aAlertTxt = arguments[2] || [];

	for(var i=0; i<aCheckFields.length; i++)
	{
		if ($(aCheckFields[i]).value == "")
		{
			$(aCheckFields[i]).style.background = fieldBG;
			$(aCheckFields[i]).style.color = fieldText;
			sAlert = sAlert + "\n" + aAlertTxt[i];
		}
		else
		{
			$(aCheckFields[i]).style.background = normalBG;
			$(aCheckFields[i]).style.color = normalText;
		}
	}
	
	//check for any checkboxes
	var aCheckBoxes = arguments[3] || [];
	var aCheckText = arguments[4] || [];
	for(i=0; i<aCheckBoxes.length; i++)
	{
		var oCheck = $(aCheckBoxes[i]);
		if(!oCheck.checked)
		{
			sAlert += "\n" + aCheckText;
		}
	}	
	if (sAlert != "")
	{
		sAlert =  "The following fields need to be filled in before you can submit this form:" + sAlert;
		alert(sAlert);
		return false;
	}
	else
	{
		$(formName).submit();
	}	
}


function validateFormAJAX(formName,params,completeHandler,loadingHandler)
{
	sAlert = "";
	
	//check main fields
	var aCheckFields = arguments[4] || [];
	var aAlertTxt = arguments[5] || [];

	for(var i=0; i<aCheckFields.length; i++)
	{
		if ($(aCheckFields[i]).value == "")
		{
			$(aCheckFields[i]).style.background = fieldBG;
			$(aCheckFields[i]).style.color = fieldText;
			sAlert = sAlert + "\n" + aAlertTxt[i];
		}
		else
		{
			$(aCheckFields[i]).style.background = normalBG;
			$(aCheckFields[i]).style.color = normalText;
		}
	}
	
	//check for any checkboxes
	var aCheckBoxes = arguments[6] || [];
	var aCheckText = arguments[7] || [];
	for(i=0; i<aCheckBoxes.length; i++)
	{
		var oCheck = $(aCheckBoxes[i]);
		if(!oCheck.checked)
		{
			sAlert += "\n" + aCheckText;
		}
	}	
		
	if (sAlert != "")
	{
		sAlert =  "The following fields need to be filled in before you can submit this form:" + sAlert;
		alert(sAlert);
		return false;
	}
	else
	{
		var theForm = $(formName);
		var theMethod = theForm.method;
		var theAction = theForm.action;
	
		var aPars = new Array();
		for(var i=0; i<params.length; i++)
		{
			aPars[i] = $(params[i]).name + "=" + $(params[i]).value;
		}
		var sPars = aPars.join("&");
		alert(sPars);
		var myAjax = new Ajax.Request(
			theAction, 
			{
				method: theMethod,
				parameters: sPars,
				onLoading: loadingHandler,
				onComplete: completeHandler
			});
	}	
}

/**
* make(tagname, attributes, children):
*	create an HTML element with specified tagname, attributes, and children.
*
* The attributes argument is a Javascript object: the names and values of its properties are taken as the names
* and values of the attributes to set.
* If attributes is null, and children is an array or a string, the attributes can be omitted altogether and
* the children passed as the second argument.
* 
* The children argument is normally an array of children to be added to the created element. If there are no
* children, this argument can be omitted. If there is only a single child, it can be passed directly instead
* of being enclosed in an array. (But if the child is not a string and no attributes are specified, an array
* must be used.)
*
* Example: make("p", ["This is a", make("b", "bold"), " word."]);
*
* This code is from the book JavaScript: The Definitive Guide, 5th Edition,
* by David Flanagan. Copyright 2006 O'Reilly Media, Inc. (ISBN #0596101996)
*/
function make(tagname, attributes, children)
{
	//if we were invoked with two arguments, the attributes argument is
	// an array or string; it should really be the children arguments.
	if(arguments.length == 2 && (attributes instanceof Array || typeof attributes == "string"))
	{
		children = attributes;
		attributes = null;
	}
	
	//create the element
	var e = document.createElement(tagname);
	
	//set attributes
	if(attributes) 
	{
		for(var name in attributes) e.setAttribute(name, attributes[name]);
	}
	
	//add children if any were specified
	if(children != null)
	{
		//if it really is an array
		if(children instanceof Array) 
		{
			//loop through kids
			for(var i=0; i<children.length; i++)
			{
				var child = children[i];
				if(typeof child == "string")
				{
					//handle text nodes
					child = document.createTextNode(child);
				}
				e.appendChild(child); //assume anything else is a node
			}
		}
		else if(typeof children == "string") 
		{
			//handle single text child
			e.appendChild(document.createTextNode(children));
		}
		else
		{
			//handle any other single child
			e.appendChild(children);
		}
	}
	
	//return the element
	return e;
}


/**
 * animateCSS(element,numFramse,timesPerFrame,animation,whendone)
 * This function serves as a framework for creating CSS-based animations.
 * The arguments to this function are:
 *
 *     element: The HTML element to be animated.
 *     numFrames: The total number of frames in the animation.
 *     timePerFrame: The number of milliseconds to display each frame.
 *     animation: An object that defines the animation; described below.
 *     whendone: An optional function to call when the animation finishes.
 *               If specified, this function is passed element as its argument.
 *
 * The animateCSS() function simply defines an animation framework. It is
 * the properties of the animation object that specify the animation to be
 * done. Each property should have the same name as a CSS style property. The
 * value of each property must be a function that returns values for that
 * style property.  Each function is passed the frame number and the total
 * amount of elapsed time, and it can use these to compute the style value it
 * should return for that frame.  For example, to animate an image so that it
 * slides in from the upper left, you might invoke animateCSS as follows:
 *
 *  animateCSS(image, 25, 50,  // Animate image for 25 frames of 50ms each
 *             {  // Set top and left attributes for each frame as follows:
 *               top: function(frame,time) { return frame*8 + "px"; },
 *               left: function(frame,time) { return frame*8 + "px"; }
 *             });
 *
 * This code is from the book JavaScript: The Definitive Guide, 5th Edition,
 * by David Flanagan. Copyright 2006 O'Reilly Media, Inc. (ISBN #0596101996)          
 **/
function animateCSS(element, numFrames, timePerFrame, animation, whendone) {
    var frame = 0;  // Store current frame number
    var time = 0;   // Store total elapsed time

    // Arrange to call displayNextFrame() every timePerFrame milliseconds.
    // This will display each of the frames of the animation.
    var intervalId = setInterval(displayNextFrame, timePerFrame);

    // The call to animateCSS() returns now, but the previous line ensures that
   // the following nested function will be invoked once for each frame
    // of the animation.
    function displayNextFrame() {
        if (frame >= numFrames) {             // First, see if we're done
            clearInterval(intervalId);        // If so, stop calling ourselves
            if (whendone) whendone(element);  // Invoke whendone function
            return;                           // And we're finished
        }

        // Now loop through all properties defined in the animation object
        for(var cssprop in animation) {
            // For each property, call its animation function, passing the
            // frame number and the elapsed time. Use the return value of the
            // function as the new value of the corresponding style property
            // of the specified element. Use try/catch to ignore any
            // exceptions caused by bad return values.
            try {
                element.style[cssprop] = animation[cssprop](frame, time);
            } catch(e) {}
        }

        frame++;               // Increment the frame number
        time += timePerFrame;  // Increment the elapsed time
    }
}