Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}
function FormFunctions_FieldCheck ($i) {
	fieldcheck = false;
	$i.each(function(){
		$f = jQuery(this);
		$fl = jQuery('label[for='+$f.attr('id')+']').text();
		if (fieldcheck || ( $f.val() != '' && $fl != $f.val() )) {
			fieldcheck = true;
		} else { 
			fieldcheck = false;
		};
	});
	return fieldcheck;
}
function FromFunctions_RequiredCheck($rf){
	if ( $rf.length == 0){
		jQuery("#requirednotice").slideUp();
		return true;
	} else {
		r = new Array();
		$rf.each(function(){
			r.push(jQuery(this).attr('rel').toLowerCase());
		});				
		
		r = r.unique();

		jQuery("#requirednotice ul").empty();
		jQuery(r).each(function(){
			if(this == 'required'){
				jQuery("[rel="+this+"].required").each(function(){
					jQuery("#requirednotice ul").append('<li>'+jQuery("label[for="+jQuery(this).attr('id')+"]").text().replace("*", "")+'</li>')
				});
			} else {
				list = new Array();
				jQuery("[rel="+this+"].required").each(function(){
					list.push(jQuery("label[for="+jQuery(this).attr('id')+"]").text().replace(" *", ""));
				});
				jQuery("#requirednotice ul").append('<li>One of the following: <ul><li>'+list.join("</li><li>")+'</li></ul></li>');
			}
		});
		jQuery("#requirednotice ul").slideDown();
		return false;
	}
}
jQuery.fn.extend({
	inlineLabel: function(commands) {
		$i = jQuery('.inlinelegend',this);
		var inactive = "inactive";
		var active = "active";
		var focused = "focused";	
		switch(commands){
			case 'dissable':
				$i.each(function(){
					$t = jQuery(this);
					obj = jQuery("#"+$t.attr("for"));
					if((obj.attr("type") == "text") || (obj[0].tagName.toLowerCase() == "textarea")){			
						var text = $t.text();
						if(obj.val() == text) {
							obj.val("");
						};				
					};	
				});			
			break;
			
			default:
				$i.each(function(){
					$t = jQuery(this);
					obj = jQuery("#"+$t.attr("for"));
					if((obj.attr("type") == "text") || (obj[0].tagName.toLowerCase() == "textarea")){			
						var text = $t.text();
						$t.css("display","none");
						if (obj.val() == "") {
							obj.val(text);
						}
						if (obj.val() == text){
							obj.addClass(inactive);			
						}
						obj.focus(function(){
							$tt = jQuery(this);
							$tt.addClass(focused)
								.removeClass(inactive)
								.removeClass(active);
							if($tt.val() == text) $tt.val("");
						});	
						obj.blur(function(){	
							$tt = jQuery(this);
							$tt.removeClass(focused);
							if($tt.val() == "") {
								$tt.val(text)
									.addClass(inactive);
							} else {
								$tt.addClass(active);		
							};				
						});				
					};	
				});
				
				jQuery(this).submit(function(){
					jQuery(this).inlineLabel('dissable');
					return true;
				});
			break;		
		}
		return this;
	}

	,required: function(commands) {

		$rf = jQuery(this).find('[rel^=required]');
		r = new Array();

		$rf.each(function(){
			$t = jQuery(this)
			//get the label for the question and append a * to IT
			jQuery("label[for="+$t.attr('id')+"]").append(' *');

			if ($t[0].tagName.toLowerCase() == "textarea"){ displayblock = 'block' }else{ displayblock = "static"}

			switch($t.attr('tagName').toLowerCase()){
				case 'input':
					if ($t.attr('type') == 'checkbox' || $t.attr('type') == 'radio'){
					} else {
						if ($t.attr('rel').match( /required\[(.*?)\]/i )) $t = $t.parents('form').find("[rel="+$t.attr('rel')+"]");
						if (FormFunctions_FieldCheck($t)){
							$t.removeClass("required");
						} else {
							$t.addClass("required");
						}
						$t.change(function(){
							$i = jQuery(this);
							if ($i.attr('rel').match( /required\[(.*?)\]/i )) $i = $i.parents('form').find("[rel="+$i.attr('rel')+"]");
							
							if (FormFunctions_FieldCheck($i)){
								$i.removeClass('required');
							} else {
								$i.addClass('required');
							}
						});
					}
				break;		
				case 'textarea':
					if ($t.val() == ''){ $t.addClass("required") }
					$t.change(function(){
						$i = jQuery(this);
						if ($i.attr('rel').match( /required\[(.*?)\]/i )) $i = $i.parents('form').find("[rel="+$i.attr('rel')+"]");
						if ($i.val() != ''){
							$i.removeClass('required');
						} else {
							$i.addClass('required');
						}
					});
				break;
				case 'select':
					if ($t.val() == ''){ $t.addClass("required") }
					$t.change(function(){
						$i = jQuery(this);
						if ($i.attr('rel').match( /required\[(.*?)\]/i )) $i = $i.parents('form').find("[rel="+$i.attr('rel')+"]");
						if ($i.val() != ''){
							$i.removeClass('required');
						} else {
							$i.addClass('required');
						}
					})
				break;
			}
		});

	
		//check form on change to remove the notice
		/*
		jQuery("input, select, textarea", this).change(function(){
			FromFunctions_RequiredCheck(
				jQuery(this).parents('form').find(".required")
			);
		});
		*/
		//check the form on submit
		jQuery(this).submit(function(){
			$rf = jQuery(this).find('.required');
			if ($rf.length > 0){
				if(jQuery("#requirednotice").length == 0 ){
					jQuery(this).prepend('<div id="requirednotice"><p><strong>Please fill in all the required fields.</strong></p><ul></ul></div>');
				}
				//make an array of the required vars
				
				FromFunctions_RequiredCheck($rf);
				
				jQuery("#requirednotice").slideDown(function(){
					$.scrollTo( "#requirednotice", 200);
				});
				return false;
			} else {
				return true;
			}
		});
		return this;
	}
});