/**
* @package   Sutton.com
* @copyright (C)2006 Sutton Group Real Estate Services
* @file      util.js
* @version   $Id:  $
*
*/

///////////////////////////////////////////////////////////////////////////////
//
// General Functions
//
///////////////////////////////////////////////////////////////////////////////
/**
* shows element with id "element_id" and sets its css display to "display_type" */
function showElementById( element_id, display_type ) {
	if( typeof(display_type) == "undefined" ) display_type = "block";
	
	document.getElementById(element_id).style.display = display_type;
}

/**
* hides element with id "element_id" */ 
function hideElementById( element_id ) {
	document.getElementById(element_id).style.display = "none";
}

function submit_form( form_id ) {
		document.getElementById(form_id).submit();
}


/**
* finds all elements by attribute */
function getElementsByAttribute(attribute, attributeValue)
{
	var elementArray = new Array();
	var matchedArray = new Array();
	
	if (document.all)
	{
		elementArray = document.all;
	}
	else
	{
		elementArray = document.getElementsByTagName("*");
	}
	
	for (var i = 0; i < elementArray.length; i++)
	{
		if (attribute == "class")
		{
			var pattern = new RegExp("(^| )" +
			   attributeValue + "( |$)");
			
			if (pattern.test(elementArray[i].className))
			{
			 matchedArray[matchedArray.length] = elementArray[i];
			}
		}
		else if (attribute == "for")
		{
			if (elementArray[i].getAttribute("htmlFor") ||
				elementArray[i].getAttribute("for"))
			{
				if (elementArray[i].htmlFor == attributeValue)
				{
					matchedArray[matchedArray.length] = elementArray[i];
				}
			}
		}
		else if (elementArray[i].getAttribute(attribute) == attributeValue)
		{
			matchedArray[matchedArray.length] = elementArray[i];
		}
	}
	
	return matchedArray;
}

/**
* Used to help manipulation of CSS classes through JS
* 
* Possible Actions ("a" parameter):
* swap    replaces class c1 with class c2 in object o.
* add     adds class c1 to the object o.
* remove  removes class c1 from the object o.
* check   test if class c1 is already applied to object o and returns true or false. 
*
* @param string    a   defines the action you want the function to perform.
* @param htmlNode  o   the object in question.
* @param string    c1  the name of the first class
* @param string    c2  the name of the second class 
function jscss(a,o,c1,c2)
{
  switch( a ) {
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): <-
      o.className.replace(c1,c2);
    	break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    	break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    	break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    	break;
    default:
    	return false;
  }
}
*/
///////////////////////////////////////////////////////////////////////////////
//
// Flexisite Admin Menu Specific Functions
//
///////////////////////////////////////////////////////////////////////////////
/**
* all changes pertaining to changes in the admin menu */
function adminMenuAction( el ) 
{	
	if( typeof(el) != "undefined" ) {
		/////////////////////////////////////////////////////////////////////////////
		// Prep Work
		/////////////////////////////////////////////////////////////////////////////
		link_name = (el.innerHTML).replace(" ","_");
		
		
		//hide all subgroups
		stuff = getElementsByAttribute("class", "secondary_admin_menu");
		for( i = 0; i <= stuff.length - 1; i++ ) {
			stuff[i].style.display = "none";
		}	
						
		//show current group
		showElementById(link_name+'_sub_menu','block');		
	}
}

function toggleAnswer( el ) {
	fadeSpeed = "fast";
	
	if( typeof(el) != "undefined" ) {						
		answer_el_id = (el.id)+"_answer";
		
		if( $( "#"+answer_el_id ).css("display") == "block" ) {
			$( "#"+answer_el_id ).slideToggle( fadeSpeed );
		}
		else {
			$("#"+answer_el_id).slideToggle( fadeSpeed, function(){ $("#"+answer_el_id).Highlight(1000,"yellow", function(){ $("#"+answer_el_id).css("background","#fff") }) });
		}		
	}
	else {
		alert("toggleAnswer() WARNING: element '"+el.id+"' is undefined");
	}
}
