/************************************************************************* 
Name:			Sitewide Scripts for IdeaBuyer.com

Description: 	This javascript file contains general routines used on all
             	pages on the site.

***************************************************************************/


function logoutCheck()
{
	if (confirm("Are you sure you want to logout?")) 
		return true;
	
	return false;
}


/*********************************************************************************** 
	This function is used to toggle the visible layer on a page to create an 
	accordian-like effect

	Requires = the DIV tag to be affected
	Ensures = the DIV tag's contents are made hidden or visible

**********************************************************************************/
function toggleLayer( whichLayer )
{
	var elem, vis;
	
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	vis = elem.style;
	
	// if the style.display value is blank we try to figure it out here
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}


/*********************************************************************************** 
	This function is used to count the characters remaining in an input field

	Requires = the INPUT tag to be affected, the maximum number of characters and
			   the current number of characters
	Ensures = the counter is accurately kept.

**********************************************************************************/

function limitText(limitField, limitCount, limitNum) 
{
	if (limitField.value.length > limitNum) 
	{
		limitField.value = limitField.value.substring(0, limitNum);
	} 
	else 
	{
		$(limitCount+'_charleft').innerHTML = limitNum - limitField.value.length;
	}
}

/*********************************************************************************** 
	This function is used to select all checkboxes of a given form

	Requires = the INPUT tag to be affected, the maximum number of characters and
			   the current number of characters
	Ensures = the counter is accurately kept.

**********************************************************************************/

function checkAll(theElement) 
{
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++)
	{
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall')
		{
			theForm[z].checked = theElement.checked;
		}
	}
}


function subNews()
{
	if($('subnewsname').value == "")
	{
		alert("You must enter a name.");
	}
	else if($('subnewsemail').value == "")
	{
		alert("You must enter an email address.");
	}
	else
	{
		new Ajax.Request("/subnews.php", {
			method: "post",
			parameters: { name: $('subnewsname').value, email: $('subnewsemail').value },
			onSuccess: function(t) { if(t.responseText=="SUCCESS") { $('newsform').innerHTML = 'Thank you for subscribing to our newsletter!'; }
				else { alert("There was a problem subscribing you to our newsletter: "+t.responseText); } },
			onFailure: function(t) { alert("We were unable to subscribe you to our newsletter.  Please check your internet connection and try again."); $('subNewsSubmit').value="Subscribe"; },
			onLoading: function(t) { $('subNewsSubmit').value="Please Wait..."; }
			
		});
	}
	return false;
}
