/**
 * Toggles an object's visibility
 *
 * @param obj	The object to toggle visibility on.
 * @param force	Boolean which, if set, will force the visibility on or off.
 *
 * @return void
 **/
function swapVisible(obj, obj2, force) {
	if(!obj) {
		return;
	}
	
	var o2 = obj2.innerHTML;
	if(obj && force == undefined) {
		obj.style.display = (obj.style.display == 'none') ? 'block' : 'none';
		if(obj.style.display == "block"){
		  obj2.innerHTML = "&ndash;";
		}else{
		  obj2.innerHTML = "+";
		}
	}
	else {
		if(force) {
			obj.style.display = 'block';
		}
		else {
			obj.style.display = 'none';
		}
	}
	return false;
}

/**
 * Swaps an object's class between 'active' and nothing.
 *
 * @param obj	The object to toggle 'active' on.
 *
 * @return bool	If changed to active, returns true, otherwise false.
 **/
function swapActive(obj) {
	if(obj.className != "active") {
		obj.className = "active";
		return true;
	}
	else {
		obj.className = "";
		return false;
	}
}

/**
 * Toggles an image's source, appending '_o' when the image is in its secondary state.
 *
 * @param obj	The image object, or a direct parent of the image object, which is to be toggled.
 * @param img	String which, if set, will force the img's src.
 *
 * @return bool	Boolean indicating whether the image was adjusted.
 **/
function swapImage(obj, img) {
	// If there isn't a src, this isn't an image.
	if(!obj.src) {
		//Grab the element we really want, the image
		obj = obj.getElementsByTagName("img");
		if(obj.length > 0) {
			obj = obj[0];
		}
		else {
			return false;
		}
	}
	
	// If the image is active, leave it alone
	if(obj.className == "active") {
		return false;
	}
	
	// Second image wasn't specified
	if(!img) {
		if(obj.src.indexOf("_o") == -1) {
			img=obj.src.replace(/(.*)(\.(gif|jpe?g|png))/gi, "$1_o$2");
		}
		else {
			img=obj.src.replace(/(.*)_o(\.(gif|jpe?g|png))/gi, "$1$2");
		}
	}
	
	// Whatever img is, set it now
	obj.src=img;
	
	return true;
}

/**
 * Email verification.
 *
 * @param str	String, being the email address
 *
 * @return bool	Boolean indicating whether the email is valid.
 **/
function emailCheck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if(str.indexOf(at)==-1) {
		return false;
	}

	if(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
		return false;
	}

	if(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
		return false;
	}

	if(str.indexOf(at,(lat+1))!=-1) {
		return false;
	}

	if(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
		return false;
	}

	if(str.indexOf(dot,(lat+2))==-1) {
		return false;
	}
	
	if(str.indexOf(" ")!=-1) {
		return false;
	}

	return true;
}

/**
 * Sets a cookie.
 *
 * @param name	Name for the cookie.
 * @param value	Cookie's value.
 * @param value	Days cookie is to remain set.
 *
 * @return void
 **/
function createCookie(name, value, days) {
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	
	document.cookie = name+"="+value+expires+"; path=/";
}

function dropDown(link) {
	var div = link.parentNode;
	
	var el;
	alert(div.innerHTML);
	if(div.getElementsByTagName('span').length > 0) {
		el = div.getElementsByTagName('span')[0];
	}
	else if(div.getElementsByTagName('div').length > 0) {
		el = div.getElementsByTagName('div')[0];
	}
	
	swapVisible(el);
}
