/**
 * 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, force) {
	if(obj && force == undefined) {
		obj.style.display = (obj.style.display == 'none') ? 'block' : 'none';
	}
	else {
		if(force) {
			obj.style.display = 'block';
		}
		else {
			obj.style.display = 'none';
		}
	}
}

/**
 * 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 === undefined) {
		//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 validEmail(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 invertNavHover() {
	var imgs = document.getElementById('nav').getElementsByTagName('img');
	for(key in imgs) {
		swapImage(imgs[key]);
	}
}


	function arr2obj(a) {
		var o = {};
		for(var i=0;i<a.length;i++) {
			o[a[i]]='';
		}
		return o;
	}

	function checkField(field) {
		var exclusion = new Array('spouse_name', 'fax', 'billing_address2', '.*player_1', '.*player_2', '.*player_3', '.*player_4', 'cc_name', 'cc_number', 'ccvn');
		var excluded = false;
		for(var i in exclusion) {
			var reg = new RegExp(exclusion[i], "i");
			if(field.name.match(reg)) {
				excluded = true;
				break;
			}
		}
		
		if(field.type=='select') {
			excluded = true;
		}
		
		if(excluded) {
			field.className = '';
			return true;
		}
		
		if(field.type=='radio') {
			var els = field.parentNode.getElementsByTagName('input');
			var noSelection = true;
			for(var i=0; i<els.length; i++) {
				if(els[i].type != 'radio') {
					continue;
				}
				noSelection = !els[i].checked;
				if(noSelection == false) {
					break;
				}
			}
			if(noSelection) {
				field.className = 'bad';
				return false;
			}
			else {
				field.className = '';
				return true;
			}
		}
		else if(field.value == '') {
			field.className = 'bad';
			return false;
		}
		else {
			field.className = '';
			return true;
		}
	}

	function checkForm(form) {
		var formOkay = true;
		var els = form.getElementsByTagName('input');
		for(var i=0; i<els.length; i++) {
			formOkay = (checkField(els[i]) && formOkay);
		}
		var els = form.getElementsByTagName('textarea');
		for(var i=0; i<els.length; i++) {
			formOkay = (checkField(els[i]) && formOkay);
		}
		if(form.email) {
			var valid_email = validEmail(form.email.value);
			if(!valid_email) {
				form.email.className = 'bad';
				formOkay = false;
			}
		}
		
		if(formOkay) {
			form.submit();
		}
		else {
			swapVisible(document.getElementById('errors'), true);
			window.scrollTo(0,0);
		}
	}
