function isValidEmail(email, required) {
   if (required==undefined) {   // if not specified, assume it's required
       required=true;
   }
   if (email==null) {
       if (required) {
           return false;
       }
       return true;
   }
   if (email.length==0) {
       if (required) {
           return false;
       }
       return true;
   }
   if (! allValidChars(email)) {  // check to make sure all characters are valid
       return false;
   }
   if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
       return false;
   } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
       return false;
   } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
       return false;
   } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
       return false;
   } else if (email.indexOf(".") == email.length) {  // . must not be the last character
       return false;
   }
   return true;
}

function allValidChars(email) {
 var parsed = true;
 var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
 for (var i=0; i < email.length; i++) {
   var letter = email.charAt(i).toLowerCase();
   if (validchars.indexOf(letter) != -1)
     continue;
   parsed = false;
   break;
 }
 return parsed;
}

function checkAvailability(element, isUname) {
	if (element.value == '') return;
	if( document.all ) {
		var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		var xmlHttp = new XMLHttpRequest();
	}
	
	var elementName = '';
	var postString = '';
	var errorMsg = '';
	var successMsg = '';
	if (isUname != undefined) {
		if (isUname) {
			// doing a username check
			errorName = 'user_error';
			postString = 'username=';
			errorMsg = 'Username unavailable';
			successMsg = 'Username available';
		}
		else {
			// doing an email check
			errorName = 'email_error';
			postString = 'email=';
			errorMsg = 'Email already in use';
		}
	}
	else {
		// checking the captcha
		errorName = 'captcha_error';
		postString = 'captcha=';
		errorMsg = 'Incorrect code, try again';
		successMsg = 'Code is correct';
	}
	postString += element.value;
	var er = document.getElementById(errorName);
	xmlHttp.open("POST", '/username.php', true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", postString.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4) {
			if (xmlHttp.status == 200) {
				var success = true;		
				if (xmlHttp.responseText != 'OK') {
					er.innerHTML = errorMsg;
					er.className = 'error';
					element.focus();
					success = false;
				}
				else {
					er.innerHTML = successMsg;
					er.className = 'success';
				}
			}
			else if (xmlHttp.status != 0) alert('The server is unavailable at this time, please try to register later.');
		}
	}
	xmlHttp.send(postString);
}

function validate() {
	if (document.postform.username.value == '') {
		alert('You must enter a username');
		document.postform.username.focus();
		return false;
	}
	if (document.postform.email.value == '') {
		alert('You must enter a valid email address');
		document.postform.email.focus();
		return false;
	}
	if (!isValidEmail(document.postform.email.value)) {
		alert('The email address you entered is not a valid email address');
		document.postform.email.focus();
		return false;
	}
	if (document.postform.pwd.value != document.postform.pwd2.value) {
		alert('The entered passwords do not match, please try again');
		document.postform.pwd.focus();
		return false;
	}
	if (document.postform.post_title.value == '') {
		alert('You must enter a title for your post');
		document.postform.post_title.focus();
		return false;
	}
}

function validateAnonymous() {
	if (document.postform.post_title.value == '') {
		alert('You must enter a title for your post');
		document.postform.post_title.focus();
		return false;
	}
	var d = document.getElementById('captcha_error').innerHTML;
	if (d == '' || d == 'Incorrect code, try again') {
		alert('You must correctly enter the security code');
		document.postform.code.focus();
		return false;
	}
}

function vote(value, postId, spanId, thumbsUpVal, thumbsDownVal) {
	var postStr = 'vote=' + value + '&post_id=' + postId;
	AJAX_send(postStr, '/?vote');
	var d = document.getElementById(spanId);
	if (value > 0) thumbsUpVal += 1;
	else thumbsDownVal += 1;
	var newHTML = '<span>Kick \'em out! (' + thumbsUpVal + ') - Let \'em stay! (' + thumbsDownVal + ')</span>';
	d.innerHTML = newHTML;
}

function AJAX_send( post_xml_str, url ) {
	if( document.all ) {
		var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		var xmlHttp = new XMLHttpRequest();
	}
	// test support for other browsers esp SAFARI

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", post_xml_str.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			//alert( xmlHttp.responseText );
			eval( xmlHttp.responseText );
		}
	}
	xmlHttp.send(post_xml_str);
	//xmlHttp.close();
}
