/***********************************************************
 * general.js
 * 
 * project	Cross Exam Books Site : Randy Singer
 * author	Walter Sanders
 * date		2-16-2006
 *
 * This file holds all javascript functions for all pages.
 */

/***********************************************************
 * formCheck()
 *
 * formCheck checks the validity of values entered into the
 * registration form.  
 * It checks for proper email syntax, matching passwords,
 * and entered information in all fields.
 *
 * results	No form is submitted until all values are
 *		of proper format.
 *
 * errors	all errors are returned to the user via an
 *		alert box with information.
 */
function formCheck() {
	if(document.form.email.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1){
		alert('Please enter a valid email value.');
		return false;
	}
	
	if(document.form.password.value != document.form.password2.value) {
		alert('Password fields must match.');
		return false;
	}
	
	if(document.form.password.value.search(/\w{4,}/) == -1) {
		alert('Password must be at least 4 characters long');
		return false;
	}

	if(document.form.fname1.value.search(/\w{1,}/) == -1) {
		alert('Please enter a first name.');
		return false;
	}

	if(document.form.lname1.value.search(/\w{1,}/) == -1) {
		alert('Please enter a last name.');
		return false;
	}

	if(document.form.street1.value.search(/\w{1,}/) == -1) {
		alert('Please enter a street address.');
		return false;
	}

	if(document.form.city1.value.search(/\w{1,}/) == -1) {
		alert('Please enter a city.');
		return false;
	}

	if(document.form.zip1.value.search(/\d{5}/) == -1) {
		alert('Please enter a valid zipcode.');
		return false;
	}	

	if(document.form.checkbox.checked) {} else {
		alert('Please read and agree to the privacy statement.');
		return false;
	}
	
	if(document.form.checkboxrules.checked) {} else {
		alert('Please read and agree to the contest rules.');
		return false;
	}

	return true;	
}

function answerCheck() {
	if(document.form.c10.value == "") {
		alert('Please enter an answer.');
		return false;
	}

	if(document.form.reasoning.value == "") {
		alert('You must enter reasoning for your answer.');
		return false;
	}

	return true;
}

function contactCheck() {
	if(document.contactform.name.value == "") {
		alert('Please enter your name.');
		return false;
	}

	if(document.contactform.email.value == "") {
		alert('Please enter your email.');
		return false;
	}

	if(document.contactform.feedback.value == "") {
		alert('Please enter feedback.');
		return false;
	}

	return true;
}

function setfocusREG() {
	document.form.email.focus();
}

function setfocusCONTACT() {
	document.contactform.name.focus();
}


function setfocusSUCCESS() {
	document.form.c10.focus();
}

function clickFocus(btn, event) {   

	if (document.all) {
        	if (event.keyCode == 13) {
			event.cancel = true;
			document.getElementById(btn).click();
			return false;
        	}
    	}
    	else {
        	if (event.which == 13) {
			event.cancelBubble = true;
			document.getElementById(btn).click();
			return false;
        	}
    	}

	return true;
}


var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
  try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) {
    xmlhttp = false;
   }
  }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}  
}

function cypherAJAX(num) {
	var attempt = document.getElementById("c"+num+"text").value;
	var element = document.getElementById("answer"+num);
	
	xmlhttp.open("GET", "/contest/cypherCheck.php?attempt="+attempt+"&num="+num);      

	xmlhttp.onreadystatechange = function() {
      		if (xmlhttp.readyState == 4){
			if(xmlhttp.status == 200 || xmlhttp.status == 0) {
				element.innerHTML = xmlhttp.responseText;
        		}
		}
	}
     
	xmlhttp.send(null);
}
