/*

	JavaScript Function Library for America's Most Wanted WordPress Customizations
	Developed by Mary Gillen and Eric Ladd
	22 April 2007
	
*/

/*

	function isEmail()
		- Paramaters passed: candidate email address (required)
		- Returns: true if parameter value matches email address pattern, false otherwise

*/
function isEmail(email) {

	if (email == undefined) {
		return false;
	};
	
	// Set up the regular expression
	re = /^[\w\-\.]+@([a-z0-9\-]+\.)+[a-z]{2,4}$/i;

	return re.test(email);

}

/*

functions Trim, and validateComment by David Durst


*/

function Trim(str) {
	var tstr = new String(str);
	tstr = tstr.replace(/^ +/gi,'');
	tstr = tstr.replace(/ +$/gi,'');
	return tstr;
}

function validateComment() {
	var retval;
	var errors;
	var errorcount;
	retval = false;
	errors = '';
	errorcount = 0;
	checkEmailAddr();
	with(document.commentform){
		if (authemail.value == 0){
			if (! isEmail(email.value)) {
					errorcount++;
					errors = errors + "You must supply a valid email address.\n";
				}
				if (Trim(author.value) == '') {
					errorcount++;
					errors = errors + "Please enter your name.\n";
				}
				if (Trim(comment.value) == '') {
					errorcount++;
					errors = errors + "Please enter your comment.\n";
				}
	
				if (Trim(comment.value.search("href")) != -1 || Trim(comment.value.search("<a")) != -1) {
					errorcount++;
					errors = errors + "Please remove all hyperlinks from your message.\n";
				}
			}

			if (errorcount > 0) {
				alert("There was a problem with your comment:\n" + errors);
			} else {
				retval = true;
			}
		}
	
	return retval;
}

function reportPost(pID) {

	var ajaxRequest = createXHR();
	if (typeof ajaxRequest == "undefined") {
	
		alert("Ajax not supported on this browser.");
	
	} else {

		ajaxRequest.open("GET", "flag_post.php?ID=" + pID, true);

		ajaxRequest.onreadystatechange = function () {
		
			if (ajaxRequest.readyState==4 && ajaxRequest.status == 200) {
			
				if (ajaxRequest.responseText.indexOf("Success") != -1) {
				
					alert("This post has been reported for review by the site administrator.");
					if (document.getElementById("postdiv-" + pID)) {
						document.getElementById("postdiv-" + pID).style.display = "none";
					};
					
				} else {
				
					alert("Attempt to report post failed! Response: " + ajaxRequest.responseText);
					
				};
			
			};
		
		}
		
		ajaxRequest.send(null);

	}
	
}

function emailComment(cID) {

	var ajaxRequest = createXHR();
	if (typeof ajaxRequest == "undefined") {
	
		alert("Ajax not supported on this browser.");
	
	} else {

		ajaxRequest.open("GET", "/flag_comment_email_process.cfm?ID=" + cID, true);

		ajaxRequest.onreadystatechange = function () {
		
			if (ajaxRequest.readyState==4 && ajaxRequest.status == 200) {
			
				if (ajaxRequest.responseText.indexOf("Success") != -1) {

//					alert("This comment has been reported for review by the site administrator.");
//					if (document.getElementById("commentdiv-" + cID)) {
//						document.getElementById("commentdiv-" + cID).style.display = "none";
//					};
					
				} else {
				
//					alert("Attempt to report comment failed! Response: " + ajaxRequest.responseText);
					
				};
			
			};
		
		}
		
		ajaxRequest.send(null);

	}
	
}


function reportComment(cID) {

	var ajaxRequest = createXHR();
	if (typeof ajaxRequest == "undefined") {
	
		alert("Ajax not supported on this browser.");
	
	} else {

		ajaxRequest.open("GET", "/flag_comment.php?ID=" + cID, true);

		ajaxRequest.onreadystatechange = function () {
		
			if (ajaxRequest.readyState==4 && ajaxRequest.status == 200) {
			
				if (ajaxRequest.responseText.indexOf("Success") != -1) {

					alert("This comment has been reported for review by the site administrator.");
//					if (document.getElementById("commentdiv-" + cID)) {
//						document.getElementById("commentdiv-" + cID).style.display = "none";
//					};
					
				} else {
				
					alert("Attempt to report comment failed! Response: " + ajaxRequest.responseText);
					
				};
			
			};
		
		}
		
		ajaxRequest.send(null);

	}
	
}

function validateEmailFriendForm(myForm) {

	var errors = new Array();
	
	if (!isEmail(myForm.senderEmail.value)) {
		errors.push("Invalid sender email address.");
	};
	
	if (myForm.senderName.value == "") {
		errors.push("No sender name provided.");
	};
	
	if (myForm.friendEmail1.value == "" && myForm.friendEmail2.value == "" && myForm.friendEmail3.value == ""  && myForm.friendEmail4.value == "" && myForm.friendEmail5.value == "") {
	
		errors.push("No 'Send to' email addresses provided.");
	
	} else {
	
		if (myForm.friendEmail1.value != "" && !isEmail(myForm.friendEmail1.value)) {
			errors.push("First 'Send to' email address is invalid.");
		};
		if (myForm.friendEmail2.value != "" && !isEmail(myForm.friendEmail2.value)) {
			errors.push("Second 'Send to' email address is invalid.");
		};
		if (myForm.friendEmail3.value != "" && !isEmail(myForm.friendEmail3.value)) {
			errors.push("Third 'Send to' email address is invalid.");
		};
		if (myForm.friendEmail4.value != "" && !isEmail(myForm.friendEmail4.value)) {
			errors.push("Fourth 'Send to' email address is invalid.");
		};
		if (myForm.friendEmail5.value != "" && !isEmail(myForm.friendEmail5.value)) {
			errors.push("Fifth 'Send to' email address is invalid.");
		};
		
	};
	
	if (errors.length > 0) {
	
		var msg = "Please correct the following items prior to submitting this form:\n";
		for (i=0; i<errors.length; i++) {
			thisItem = Number(i)+1;
			msg += "\n " + thisItem + ". " + errors[i];
		};
		alert(msg);
		return false;
	
	} else {
		
		return true;
		
	};

}

function createXHR()
{
	try
	{
		xmlhttp = new XMLHttpRequest();   // Mozilla and IE 7
	}
	catch(exc1)
	{
		var ieXmlHttpVersions = new Array();  // IE 6 and earlier
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
		ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";
		
		var i;
		for (i=0; i < ieXmlHttpVersions.length; i++)
		{
			try
			{
				xmlhttp = new ActiveXObject(ieXmlHttpVersions[i]);
				break;
			}
			catch (exc2)
			{
				//alert(ieXmlHttpVersions[i] + " not supported.");
			}
		}
	}
	return xmlhttp;
}

function checkEmailAddr() {

	var ajaxRequest = createXHR();
	if (typeof ajaxRequest == "undefined") {
	
		alert("Ajax not supported on this browser.");
	
	} else {

		with(document.commentform){
			ajaxRequest.open("GET", "/safety/getauthemail.cfm?e=" + email.value, true);
		}

		ajaxRequest.onreadystatechange = function () {
		
			if (ajaxRequest.readyState==4 && ajaxRequest.status == 200) {
				if (ajaxRequest.responseText.indexOf("isauthorized") != -1) {
					document.commentform.authemail.value = 1;
				} else {
					document.commentform.authemail.value = 0;
				};
			
			};
		
		}
		
		ajaxRequest.send(null);

	}

}
