// show/hide blocks
function switchNav(which) {
	if(which=="search") {
		document.getElementById("search").style.display="block";
		document.getElementById("login").style.display="none";
		document.getElementById("search-login").style.background="#131635";
	} else {
		document.getElementById("login").style.display="block";
		document.getElementById("search").style.display="none";
		document.getElementById("search-login").style.background="#84CCED";
	}
}

// Window opening
function winOpenCenter(url,name,statbar,scroll,locate,resize,xWidth,yWidth)	{
	var adjustedleft = 8//optional
	var adjustedheight = 30//adjust height because of windows taskbar
	var screenwidthremainder = screen.availWidth%2//really not needed, but won't hurt
	var screenheightremainder = screen.availHeight%2
	var screenwidth = screen.availWidth - screenwidthremainder
	var screenheight = screen.availHeight - screenheightremainder
	var winheight = yWidth //set new window height properties
	var winwidth = xWidth //set new window width properties
	var winleft = parseInt(screenwidth/2) - (winwidth/2) - adjustedleft//optional
	var wintop = parseInt(screenheight/2) - (winheight/2) - adjustedheight
	var win = window.open(url,name,'width=' +winwidth+ ',height=' +winheight+',status=' +statbar+',resizable='+resize+',scrollbars='+scroll+',location='+locate+',top='+wintop+',left='+winleft);
	return win;
}

// Email Checker
function containsCharacter(str,testchar) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) == testchar) return true;
	}
	return false;
}

function validCharacters(teststr,validstr) {
	for (i = 0; i < teststr.length; i++) {
		if (validstr.indexOf(teststr.charAt(i)) < 0) return false;
	}
	return true;
}

function isEmailValid(str) {
	var pos;
	var errorCount = 0;
	var email = str;
	
	var validStr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@.[]-_";

	if (email == "") {
		alert("Please enter an email address.");
		return false;
	}
	
	if (email.charAt(0) == "@" || email.charAt(0) == "." || email.charAt(0) == "[" || email.charAt(0) == "]" || email.charAt(0) == "-" || email.charAt(0) == "_") errorCount++;
	
	if (!validCharacters(email,validStr)) errorCount++;
		
	for (i = 1; i < email.length; i++) {
		if ((email.charAt(i-1) == "." && email.charAt(i) == ".") || (email.charAt(i-1) == "." && email.charAt(i) == "@") || (email.charAt(i-1) == "@" && email.charAt(i) == ".")) {
			errorCount++;
			break;
		}
	}

	if (!errorCount && email.indexOf("@") < 0 && email.length > 0) {
		alert("Your email address is missing the domain name. The format should be "+str+"@somedomain.\n\nFor example, "+str+"@aol.com, "+str+"@earthlink.net, or "+str+"@yahoo.com.");
		return false;
	}
	if (email.length < 6) errorCount++;
	pos = email.indexOf("@");
	email = email.substring(pos+1,email.length);
	if (email.indexOf("@") != -1) errorCount++;
	if ((email.charAt(0) == "[" && email.charAt(email.length-1) != "]") || (email.charAt(0) != "[" && email.charAt(email.length-1) == "]") || (email.charAt(0) == "]" || email.charAt(email.length-1) == "[")) errorCount++;
	if (email.indexOf(".") < 0) {
		errorCount++;
	}
	else {
		pos = email.lastIndexOf(".");
		email = email.substring(pos+1,email.length);
		if ((!isNaN(email) && email != "") || (email.charAt(email.length-1) == "]")) {
			pos = str.indexOf("@");
			if (email.charAt(email.length-1) == "]") {
				email = str.substring(pos+1,str.length-1);
				if (email.charAt(0) == "[") {
					email = email.substring(1,str.length);
				}				
			}
			else {
				email = str.substring(pos+1,str.length);
			}

			var ipAddress = "";
			var periods = 0;

			for (i = 0; i < email.length; i++) {
				if (email.charAt(i) == ".") {
					periods++;
					if ((isNaN(ipAddress)) || (ipAddress < 0 || ipAddress > 255)) {
						errorCount++
						break;
					}
					ipAddress = "";
				}
				else
					ipAddress += email.charAt(i).toString();
			}
			if ((isNaN(ipAddress)) || (ipAddress < 0 || ipAddress > 255)) errorCount++

			if (periods != 3) errorCount++;
		}
		else {
			if (email.length < 2 || email.length > 3) errorCount++;
		}
	}

	if (errorCount > 0) {
		alert(str+" is not a valid email address. Please check your typing.");
		return false;
	}
	return true;
}
