<!--//

// Checks and verify Email
function emailCheck(str){
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	// check if string contains @sign
	if (str.indexOf(at)==-1){
	   return false
	}
	// check if @sign not at beginning of string
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	// check if period not at beginning of string
	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					
}// end function emailCheck

function checkCaptchaCode(str, form_name){
	$(document).ready(function(){
		$.get('/includes/ajax.php',{
			captcha_code: str, 
			module: 	"send_contact",
			firstname: 	form_name.firstname.value,
			lastname: 	form_name.lastname.value,
			email:		form_name.email.value,
			comments:	form_name.comments.value
			
		}, 
		function(data){
			if (data == "valid"){
				document.getElementById("contact").innerHTML = 	"<p>Your comment has been sent.</p>" +
																"<p>We will get back to you as soon as possible.</p>";
			}
			else{
				// change the image if they guess wrong so they can't keep guessing with 4 letters + number possiblity
				alert('code is does not match with the image above');
				document.getElementById('captcha_image').src = '/captcha/securimage_show.php?sid=' + Math.random()
				return false;
			}// end if 
		});// end $.get function 
   });
}// end function checkCaptchaCode


/********************************************
* function submits data using ajax get.
* data is return in xml, and replace in id 
* elements. 
********************************************/
function submitFactOrFiction(){
	myForm = document.ff_form;
	if (myForm.fact_or_fiction[0].checked == true || myForm.fact_or_fiction[1].checked == true){
		$(document).ready(function(){
			$.get('/includes/ajax.php', { 
				fact_or_fiction:	myForm.fact_or_fiction.value, 
				questionNumber:		myForm.questionNumber.value,
				module:				"questions"
				
			},
			function(data){
				$(data).find('questions').each(function(){
					var $objectItem		= $(this);
					var statement		= $objectItem.find('statement').text();
					var answer			= $objectItem.find('fact_fiction').text();
					var explanation		= $objectItem.find('explanation').text();
					var questionId		= $objectItem.find('question_number').text();
					var prevAnswer		= $objectItem.find('previous_fact_fiction').text();
					var prevExplain		= $objectItem.find('previous_explaination').text();
					var dataHTML =	"<p>"+
										"<strong>"+prevAnswer+"</strong>: " +
										prevExplain+
									"</p>";
					document.getElementById("fact_or_fiction").innerHTML = dataHTML + "<strong>Fact or Fiction:</strong> " + statement;
					// put data into form field to avoid using session ro cookies
					myForm.questionNumber.value = questionId;
					// clear checkbox value
					myForm.fact_or_fiction[0].checked = false;
					myForm.fact_or_fiction[1].checked = false;
				}); // end $(data).find()
			}); // end $.get()
		}); // end $(document).ready
	}// end if
}// end function submitFactOrFiction

/****************************************************
* form validation thru title elements 
* in form fields check.  The title in the fields are
* the alert message if the field is empty.
****************************************************/
function formCheck(form_name){
	myForm = eval('document.'+ form_name);
	for (i=0; i < myForm.elements.length; i++){
		// check to see if fields are empty on by checking title elements
		if (myForm.elements[i].title != "" && myForm.elements[i].value == ""){
			alert(myForm.elements[i].title);
			myForm.elements[i].focus();
			return false;
			break;
		}// else if there is a form field name email, it will check if its the correct email format.
		else if (myForm.elements[i].name == "email"){
			if (emailCheck(myForm.elements[i].value) == false){
				alert('email is invalid');
				return false;
				break;
			}// end if emailCheck
		} // else if catpcha code captcha code does not match
		else if(myForm.elements[i].name == "captcha_code") {
			if (checkCaptchaCode(myForm.elements[i].value, myForm) == false ){// checkCaptchaCode keeps getting undefined because its too slow
				alert('code does not match');
				return false;
				break;
			}// end if checkCaptchaCode(myForm.elements[i].value == false
		}// end myForm.elements[i].name == "captcha_code"
	}// end for loop
}// end function formCheck
	

//-->