// JavaScript Document

function adjustWindow() {
	var defHeight = 1280;
	var defWidth = 1024;
	
	var windowHeight = getWindowHeight();
	var windowWidth = getWindowWidth();

	if (windowHeight < defHeight) {
		windowHeight -= 10;
		windowWidth -= 60;
		//var newwidth = parseInt((windowHeight * defWidth) / defHeight);
		setDims(windowWidth,
windowHeight);
	}
	else 
	{
		if (windowWidth < defWidth)
		{
			windowWidth -= 60;
			//var newheight = parseInt((windowWidth * defHeight) / defWidth);
			setDims(windowWidth,
 windowHeight);
		}
		else
		{
			setDims(defWidth,
 defHeight);
		}
	}
}

function getWindowHeight() {
  var myHeight = 0;
  if (typeof(window.innerWidth) == "number") {
    return window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) {
    return document.documentElement.clientHeight;
  } else if (document.body && document.body.clientHeight) {
    return document.body.clientHeight;
  }
}

function getWindowWidth() {
  var myHeight = 0;
  if (typeof(window.innerWidth) == "number") {
    return window.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth) {
    return document.documentElement.clientWidth;
  } else if (document.body && document.body.clientWidth) {
    return document.body.clientWidth;
  }
}

function setDims(xx,yy) {
  var changeMe = $(window.resize_item);
	 if (changeMe)
	 {	
		changeMe.height = yy;
		changeMe.width = xx;
	 }

}




function MM_reloadPage(init) 
{  //reloads the window if Nav4 resized
  if (init==true) 
  {
	  with (navigator) 
	  {
			if ((appName=="Netscape")&&(parseInt(appVersion)==4)) 
			{
				document.MM_pgW=innerWidth; 
				document.MM_pgH=innerHeight; 
				onresize=MM_reloadPage; 
			}
		}
	}
  	else if (innerWidth!=document.MM_pgW || 
				innerHeight!=document.MM_pgH )
	{ 
		location.reload();
	}
}

function validate( formName, validations )
{
	clean = true;
	validation_list = $(validations).value.split(',');
	validation_list.each( function(id){ 
			var question = $(id);
			if (question)
			{
				if( question.value == 0  || question.value == '')
				{ 
					question.up('.entry').addClassName("error");
					clean = false;
				}
				else
				{
					question.up('.entry').removeClassName("error");
				}
			}
			else
			{
				alert(id);
			}
		});
	if (!clean) alert ("Please fill in the fields highlighted in Red");
	return clean;

}

/*
	Written by Jonathan Snook, http://www.snook.ca/jonathan
	Add-ons by Robert Nyman, http://www.robertnyman.com
*/

function getElementsByClassName(oElm, strTagName, strClassName)
{
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++)
	{
		oElement = arrElements[i];      
		if(oRegExp.test(oElement.className))
		{
			arrReturnElements.push(oElement);
		}   
	}
	return (arrReturnElements)
}

function setDays( formObj, objName) {
	var y = formObj[objName+'year'].options[formObj[objName+'year'].selectedIndex].value;
	var m = formObj[objName+'month'].selectedIndex;
	var d;
	// find number of days in current month
	if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) 
	{
		days = 30;
	}
	else if (m == 1) 
	{
		// check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
		if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
			days = 29
		else
			days = 28
	}
	else 
	{
		days = 31;
	}
	
	// if (days in new month > current days) then we must add the extra days
	if (days > formObj[objName+'day'].length) 
	{
		for (i = formObj[objName+'day'].length; i < days; i++) 
		{
			formObj[objName+'day'].length = days;
			formObj[objName+'day'].options[i].text = i + 1;
			formObj[objName+'day'].options[i].value = i + 1;
		}
	}
	
	// if (days in new month < current days) then we must delete the extra days
	if (days < formObj[objName+'day'].length) 
	{
		formObj[objName+'day'].length = days;
		if (formObj[objName+'day'].selectedIndex == -1) 
			formObj[objName+'day'].selectedIndex = days - 1;
	}
}
function getSubMenus( id )
{
	var elementID = 'sub_menu';
	new Ajax.Updater(
		elementID, 
		domain+'/includes/menu_get.php?id='+id, 
		{	
			asynchronous:true, 
			evalScripts:true
		}
	)
}

function toggleMenu( menuElement )
{
	current_menu.className="";
	menuElement.className="picked";
	current_menu = menuElement;
	return true;	
}

function remove_item( update_area , object, id )
{
	if (confirm("Are you sure you want to remove this item?"))
	{
		new Ajax.Updater(	update_area, 
							domain+'/'+object+'/remove/' +id,
							{
								asynchronous:true, 
								evalScripts:true,
								onComplete: function(request){
									if ($('show_disabled') && $('show_disabled').value ==2   )
									{
										$(update_area).addClassName('enabled0');
									}	
									else
									{
										Effect.Puff(update_area);	
									}
								},
								parameters: "ajax=true"
							}
						);
		return false;
	}
	return false;
}

function enable_item( update_area , object, id )
{
		new Ajax.Updater(	update_area, 
							domain+'/'+object+'/enable/'+id,
							{
								asynchronous:true, 
								evalScripts:true,
								onComplete: $(update_area).removeClassName('enabled0'),
								parameters: "ajax=true"
							}
						);
}

function remote_request( update_area, action, id, form)
{
	form_parameters = "";

	var	domain = "http://"+document.location.host;
	
	if ($(form) && $(form).tagName=='FORM')
	{
		form_parameters = Form.serialize(form);
	}
	new Ajax.Updater(   update_area,
						domain+action+'/'+id,
						{
							asynchronous:false, 
							evalScripts:true,
							onComplete: function(request){Effect.Highlight(update_area)},	
							parameters: form_parameters + "&target_id=" +update_area + "&ajax=true"
						}
					);
}

function edit_item( edit_area, object, id )
{
	new Ajax.Updater(	edit_area,
						domain+'/'+object+'/edit/'+id,
							{
								asynchronous:true, 
								evalScripts:true,
								onComplete: function (request){ 
									Effect.Highlight(edit_area)
								},
								parameters: "target_id=" +update_area + "&ajax=true"
							}
						);
	return true;
}

function DisplayTeaser( infodiv, obj )
{
	
	//Position.absolutize(infodiv);
	
	//$('information_block').innerHTML = $(infodiv).innerHTML;
	$('information_block').style.left = '250px';
	$('information_block').style.top = Event.pointerY( obj );
	//Effect.BlindDown('information_block');
	//$('information_block').style.display="block";
	Element.toggle(infodiv);
}

function HideTeaser(infodiv)
{
	//Effect.BlindUp('information_block');
	//$('information_block').style.display="none";
	Element.toggle(infodiv);
}

function updating()
{
	return '<img src = "/images/spinning-wait-icons/wait30trans.gif">Please Wait!';
}

function mark_check_box( mode, id )
{
	$$(id).each( 
		function(item){
			switch( mode )
			{
				case "checked":
					item.checked = true;
					break;
				case "unchecked":
					item.checked = false;
					break;
				case "swap":
					item.checked = !item.checked;
					break;
			}
		}
	);
}

function unselect( id )
{
	id = $(id);
	id.selectedIndex = 0;
}
