// JavaScript Document

var f = null;

/*
 * javascript will handle form checking and submission
 */
 
addLoadEvent( 
	function() {
		f = document.forms['storiesForm'];
		if ( document.getElementById && document.getElementsByTagName ) {
			f.onsubmit = checkForm;
		} 
		if ( document.getElementById ) {
			document.getElementById( 'form2' ).style.display = 'none';
			
			// hide error message fields (just the one so far)
			if ( document.getElementById( 'incomplete_error' ).innerHTML == '' ) {
				document.getElementById( 'incomplete_error' ).style.display = 'none';
			}
			if ( document.getElementById( 'photo_error' ).innerHTML == '' ) {
				document.getElementById( 'photo_error' ).style.display = 'none';
			}
			if ( document.getElementById( 'agreement_error' ).innerHTML == '' ) {
				document.getElementById( 'agreement_error' ).style.display = 'none';
			}
		}
		
	}
);

function checkForm(){ 
	// turn off any error messages that were previously set
	document.getElementById( 'incomplete_error' ).innerHTML = null;
	document.getElementById( 'incomplete_error' ).style.display = 'none';
	
	document.getElementById( 'photo_error' ).innerHTML = null;
	document.getElementById( 'photo_error' ).style.display = 'none';
	
	
	

	var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var fileFilter   = /^.+\.(jpe?g)|(gif)|(png)$/;
	
	if ( !f.name.value || !f.email.value ){
		document.getElementById( 'incomplete_error' ).innerHTML = 'You must complete all the fields marked with an asterisk.';
		document.getElementById( 'incomplete_error' ).style.display = 'block';
		location.hash = 'incomplet';
	}
	else if ( !emailFilter.test( f.email.value ) ) {
		document.getElementById( 'incomplete_error' ).innerHTML = 'Please enter a valid email address.';
		document.getElementById( 'incomplete_error' ).style.display = 'block';
		location.hash = 'incomplet';
	}
	else if ( f.photo.value != '' && !fileFilter.test( f.photo.value ) ) { 
		document.getElementById( 'photo_error' ).innerHTML = 'Only jpegs, pngs and gifs are allowed for upload. Please choose a different image and try again.';
		document.getElementById( 'photo_error' ).style.display = 'block';
		location.hash = 'phooto';
	}
	else {
		loadForm2();
	}
	return false;
}

function loadForm2 () {

	var agt=navigator.userAgent.toLowerCase();
	
	scroll( 0, 0 );
	f.onsubmit = checkAgreement;
	document.getElementById( 'copy1' ).style.display = 'none';
	
	if (agt.indexOf("safari") != -1){
		document.getElementById( 'form1' ).style.position = 'absolute';
		document.getElementById( 'form1' ).style.zindex = '2';
		document.getElementById( 'form1' ).style.left = '-3000px';
	}else{
		document.getElementById( 'form1' ).style.display = 'none';
	}
	
	
	document.getElementById( 'form2' ).style.display = 'block';
	document.getElementById( 'form2' ).style.display = 'block';
	document.getElementById( 'copy2' ).style.display = 'block';
	return false;
}

function makeRequest( url ) {
	var http_request = false;
	
	if ( window.XMLHttpRequest ) { // Mozilla, Safari, ...
		http_request = new XMLHttpRequest();
		if ( http_request.overrideMimeType ) {
			http_request.overrideMimeType( 'text/plain' );
		}
	} 
	else if ( window.ActiveXObject ) { // IE
		try { http_request = new ActiveXObject( 'Msxml2.XMLHTTP' ); } 
		catch ( e ) {
			try { http_request = new ActiveXObject( 'Microsoft.XMLHTTP' ); } 
			catch ( e ) {}
		}
	}
	
	if ( !http_request ) {
		alert( 'Giving up: Cannot create an XMLHTTP instance' );
		return false;
	}
	http_request.onreadystatechange = function() { getResponse( http_request ); }
	http_request.open( 'GET', url, true );
	http_request.send( null );
	
}

function getResponse( http_request ) {
	if ( http_request.readyState == 4 ) {
		if ( http_request.status == 200 ) { 
			if ( http_request.responseText == 'true' ) { 
				f.submit();
			}
			else {
				//alert( 'Codes don\'t match' );
				document.getElementById( 'code_error' ).innerHTML = 'The code you entered did not match. Please re-enter code and submit again.';
				document.getElementById( 'code_error' ).style.display = 'block';
				location.hash = 'agrement';
				
				// forces Mozilla, et al to reload image from server instead of cache
				document.getElementById('captchaImage').src = '/scripts/captcha.php?force=' + Math.round( Math.random() * 100 );
			}
		}
		else { alert( 'There was a problem with the request.' ); }
	}
}

function checkAgreement() {
	// turn off any error messages that were previously set
	document.getElementById( 'agreement_error' ).innerHTML = null;
	document.getElementById( 'agreement_error' ).style.display = 'none';	
	
	
	if ( !f.agree[0].checked || f.birthyear.value == 0 ) {
		document.getElementById( 'agreement_error' ).innerHTML = 'You must complete all the fields marked with an asterisk.';
		document.getElementById( 'agreement_error' ).style.display = 'block';
		location.hash = 'agrement';
		return false;
	}
	else { 
		makeRequest( '/scripts/captcha.php?security_code=' + f.security_code.value );
		return false;
	}
}