$(document).ready(function (){
	
	$(".container :button").click(function() {
		next_question($(this).attr('id'));
		return false;
	});

	$('#q1_box').css('opacity', 1);
	$('#q2_box').css('opacity', 0);
	$('#q3_box').css('opacity', 0);
	$('#q4_box').css('opacity', 0);
	$('#q5_box').css('opacity', 0);
	$('#q6_box').css('opacity', 0);
	$('#q7_box').css('opacity', 0);
	
	
	//$('#q6_box').css('display', 'inline');
	
	initiate();
});

//Adds the jquery function to elements
function initiate(){
	$('.choose_gift').click(function(){
		var gift_name= $(this).parent().parent().find('img').attr('id');
		$('#gift').val(gift_name);
	});
}


var question_answers='';
var complete = 'no';

function next_question(q_id){
	q_id = q_id.substring(3,4); //Get the question numer.
	var answer=$('#q'+q_id+'_box input:checked').val();//Gets the answer.
	
	if(q_id == 3){
		complete = 'yes';
		setTimeout("next_question('but4');", 3000);
	}
	
	//Validates that question has been answered.
	if(q_id != '3' && q_id != '4' && q_id != '5'){
		if ($('#q'+q_id+'_box input:checked').length<1)
			{alert('Please select an answer');return false;}
		question_answers = question_answers+ "_q"+q_id+"_"+answer;//Stores result.
	}
	$('#q'+q_id+'_box').animate({opacity:0}, 300); //Fades question out.
	setTimeout(function(){ 
		$('#q'+q_id+'_box').css('display', 'none'); //Remove old question from display.
		var next_id = parseInt(q_id)+1;
		$('#q'+next_id+'_box').css('display', 'block');
		$('#q'+next_id+'_box').animate({opacity:1}, 300);//Fades next question in.
	}, 300);
	
}

function validate(){
	if(complete=='no'){
		alert('Please complete the survey questions.');
		return false;
	}
	else{
		document.location = selectedGift+".html";
		return false;
	}
}


function moveFocus(item,next_name){
	if(item.value.length == item.maxLength){
		$('#'+next_name).focus();
	}
}
function numbersonly(myfield, e, dec){
		var key;
		var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || 
	    (key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	   return true;
	
	// decimal point jump
	else if (dec && (keychar == "."))
	   {
	   myfield.form.elements[dec].focus();
	   return false;
	   }
	else
	   return false;
}

function final_validate(){
	if ($('#fname').val() == '' || $('#lname').val() == '')
		{alert('Please enter your name'); return false;}
	else if (validate_email($('#email').val()) == false)
		return false;
	else if ($('#cell1').val().length != 3 || $('#cell2').val().length != 3 || $('#cell3').val().length != 4)
		{alert('Please enter a valid cell number'); return false;}
	else{
		submitting =1;
		$('#form1').submit();
	}
}

function validate_email(email){
	if (email == "") { 
		alert("Please enter your email address."); return false; 
	} 
	else {
		var reg = /^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i;
		var address = email;
		if(reg.test(address) == false) {
			alert('Invalid Email Address'); return false;
		}
	}
}

function runtest(gift){
	$('#q1_box').css('display', 'none');
	$('#q7_box').css('display', 'block');
	$('#q7_box').css('opacity', 1);
	$('#cell1').val('282');
	$('#cell2').val('813');
	$('#cell3').val('4826');
	$('#fname').val('testfname530');
	$('#lname').val('testlname283');
	$('#email').val('test35871@test98147.com');
	$('#gift').val(gift);
}
1
