
function ChangeStep(dir){
	if(dir > 0){
		$('StepBack').disabled = false;
		$('StepNext').value = "Next >>";
		if(currStep == 1){
			if($('ChallengeId').value == ""){
				$('StepBack').disabled = true;
				alert('Click the demographic that you best fit.');
				return;
			}else
				GetChallenge(false);
		}
		else if(currStep == 2){
			AddChallenge('s',$('NumSocial').value);
			scroll(0,0);
		}else if(currStep == 3){
			AddChallenge('e',$('NumEnvironmental').value);
			scroll(0,0);
		}else if(currStep == 4){
			AddChallenge('f',$('NumFinancial').value);	
			TotalChallenge();
			$('StepNext').value = "Save Scores";
			scroll(0,0);
		}else if((currStep == 5)&&(SignedIn == 'yes')){	
			SaveChallenge(false);
			return;
		}
		else if(currStep == 6){
			if($('StepSign').value == "SignUp")
				AccountSignUp(false);
			else
				AccountSignIn(false);
			return;
		}
		Num++;	
	}
	else if(dir < 0){
		scroll(0,0);
		$('StepBack').disabled = false;
		$('StepNext').value = "Next >>";
		if((currStep + dir) == 1)
			$('StepBack').disabled = true;
		Num = Num - 1;	
	}else{
		dir = 1;
		Num++;
	}
	$('Step'+currStep).style.display = "none";
	$('StepDir'+currStep).style.display = "none";
	
	currStep = currStep + dir;
	$('Num').innerHTML = Num;	
	$('Step'+currStep).style.display = "block";
	$('StepDir'+currStep).style.display = "block";
	
}

function SetChallenge(chid){
	$('ChallengeId').value = chid;
	for(var i=1;i <= NumChoice;i++){
		if($('Choice_'+i))
			$('Choice_'+i).style.color = "";
	}
	$('Choice_'+chid).style.color = "#FF0000";	
}

function GetChallenge(oReq){
	if(!oReq){
		var url = 'challenge.php';
		var data = "&Operation=GetChallenge&ChallengeId="+$('ChallengeId').value;
		var myRequest = new Ajax.Request(url, {method:'post',parameters:data,onComplete:GetChallenge});	
		$('StepNext').value = "Please Wait...";
		$('StepNext').disabled = true;
	}else{
		var r = oReq.responseText.evalJSON();
		if(r.Error){
			alert(r.Error);
		}else{
			var html1 = "";
			var html2 = "";
			var html3 = "";
			$('NumSocial').value = r.NumSocial;
			$('NumEnvironmental').value = r.NumEnvironmental;
			$('NumFinancial').value = r.NumFinancial;
			if(r.Social.length > 0){
				var currCategory = urldecode(r.Social[0].Category);
				html1 += "<div class=\"UserProfileStats\">";
				html1 += "	<h2>"+urldecode(r.Social[0].Category)+"</h2>";
				html1 += "	<div class=\"ProfileDirections\">";
				html1 += "		Check each box that you have risen to the challenge...";
				html1 += "	</div>";
				html1 += "</div>";
				html1 += "<div id=\""+urldecode(r.Social[0].Category)+"\">";
				for(var i=0;i < r.Social.length;i++){
					if(currCategory != urldecode(r.Social[i].Category)){
						html1 += "</div>";
						html1 += "<div class=\"UserProfileStats\">";
						html1 += "	<h2>"+urldecode(r.Social[i].Category)+"</h2>";
						html1 += "	<div class=\"ProfileDirections\">";
						html1 += "		Check each box that you have risen to the challenge...";
						html1 += "	</div>";
						html1 += "</div>";
						html1 += "<div id=\""+urldecode(r.Social[i].Category)+"\">";
						currCategory = urldecode(r.Social[i].Category);
					}
					html1 += "	<div class=\"ChallengeRow\"><input type=\"checkbox\" id=\"s_"+i+"\" value=\""+urldecode(r.Social[i].Points)+"\"/> &nbsp;&nbsp; "+urldecode(r.Social[i].Question)+" &nbsp; <span class=\"PointSpan\">"+urldecode(r.Social[i].Points)+" points</span></div>";
				}
				html1 += "</div>";
			}
			$('SocialChallenge').innerHTML = html1;
			if(r.Environmental.length > 0){
				var currCategory = urldecode(r.Environmental[0].Category);
				html2 += "<div class=\"UserProfileStats\">";
				html2 += "	<h2>"+urldecode(r.Environmental[0].Category)+"</h2>";
				html2 += "	<div class=\"ProfileDirections\">";
				html2 += "		Check each box that you have risen to the challenge...";
				html2 += "	</div>";
				html2 += "</div>";
				html2 += "<div id=\""+urldecode(r.Environmental[0].Category)+"\">";
				for(var i=0;i < r.Environmental.length;i++){
					if(currCategory != urldecode(r.Environmental[i].Category)){
						html2 += "</div>";
						html2 += "<div class=\"UserProfileStats\">";
						html2 += "	<h2>"+urldecode(r.Environmental[i].Category)+"</h2>";
						html2 += "	<div class=\"ProfileDirections\">";
						html2 += "		Check each box that you have risen to the challenge...";
						html2 += "	</div>";
						html2 += "</div>";
						html2 += "<div id=\""+urldecode(r.Environmental[i].Category)+"\">";
						currCategory = urldecode(r.Environmental[i].Category);
					}
					html2 += "	<div class=\"ChallengeRow\"><input type=\"checkbox\" id=\"e_"+i+"\" value=\""+urldecode(r.Environmental[i].Points)+"\"/> &nbsp;&nbsp; "+urldecode(r.Environmental[i].Question)+" &nbsp; <span class=\"PointSpan\">"+urldecode(r.Environmental[i].Points)+" points</span></div>";
				}
				html2 += "</div>";
			}
			$('EnvironmentalChallenge').innerHTML = html2;
			if(r.Financial.length > 0){
				var currCategory = urldecode(r.Financial[0].Category);
				html3 += "<div class=\"UserProfileStats\">";
				html3 += "	<h2>"+urldecode(r.Financial[0].Category)+"</h2>";
				html3 += "	<div class=\"ProfileDirections\">";
				html3 += "		Check each box that you have risen to the challenge...";
				html3 += "	</div>";
				html3 += "</div>";
				html3 += "<div id=\""+urldecode(r.Financial[0].Category)+"\">";
				for(var i=0;i < r.Financial.length;i++){
					if(currCategory != urldecode(r.Financial[i].Category)){
						html3 += "</div>";
						html3 += "<div class=\"UserProfileStats\">";
						html3 += "	<h2>"+urldecode(r.Financial[i].Category)+"</h2>";
						html3 += "	<div class=\"ProfileDirections\">";
						html3 += "		Check each box that you have risen to the challenge...";
						html3 += "	</div>";
						html3 += "</div>";
						html3 += "<div id=\""+urldecode(r.Financial[i].Category)+"\">";
						currCategory = urldecode(r.Financial[i].Category);
					}
					html3 += "	<div class=\"ChallengeRow\"><input type=\"checkbox\" id=\"f_"+i+"\" value=\""+urldecode(r.Financial[i].Points)+"\"/> &nbsp;&nbsp; "+urldecode(r.Financial[i].Question)+" &nbsp; <span class=\"PointSpan\">"+urldecode(r.Financial[i].Points)+" points</span></div>";
				}
				html3 += "</div>";
			}
			$('FinancialChallenge').innerHTML = html3;
		}
		$('StepNext').disabled = false;	
		$('StepNext').value = "Next >>";
	}
}

function AddChallenge(sphere,num){
	//alert(sphere+" "+num);
	var total = 0;
	for(var i=0;i < num;i++){
		if($(sphere+"_"+i).checked)
			total += parseInt($(sphere+"_"+i).value);
	}
	$(sphere+"_total").value = total;
	$(sphere+"t").innerHTML = total;
	//alert(total);
	return;	
}

function TotalChallenge(){
	var total = 0;
	total += parseInt($('s_total').value);
	total += parseInt($('e_total').value);
	total += parseInt($('f_total').value);
	$('ChallengeTotal').value = total;
	$('ct').innerHTML = total;
	return;
}

function SaveChallenge(oReq){
	if(!oReq){
		var url = 'challenge.php';
		var data = "&Operation=SaveChallenge&ChallengeId="+$('ChallengeId').value+"&Social="+$('s_total').value+"&Environmental="+$('e_total').value+"&Financial="+$('f_total').value+"&Total="+$('ChallengeTotal').value;
		var myRequest = new Ajax.Request(url, {method:'post',parameters:data,onComplete:SaveChallenge});	
		$('StepNext').value = "Please Wait...";
		$('StepNext').disabled = true;
	}else{
		var r = oReq.responseText.evalJSON();
		if(r.Error){
			alert(r.Error);
		}else
			window.location = "user.php";
	}
}

function AccountSignUp(oReq){
	if(!oReq){
		if(!$('SignUpEmail').value.match(/\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}/))
			alert('Enter a valid email address.');
		else if($('SignUpName').value.indexOf(" ") < 0)
			alert('Enter your full name.');
		else if($('SignUpPass').value != $('SignUpPass2').value)
			alert('Passwords do not match.');
		else if(($('SignUpPass').value.length < 4)||($('SignUpPass').value.length > 25))
			alert('Enter a password between 4 - 25 characters');
		else{	
			var url = 'account.php';
			var data = "&Operation=SignUp&Email="+$('SignUpEmail').value+"&Name="+$('SignUpName').value+"&Password="+$('SignUpPass').value;
			var myRequest = new Ajax.Request(url, {method:'post',parameters:data,onComplete:AccountSignUp});	
			$('StepNext').value = "Please Wait...";
			$('StepNext').disabled = true;
		}
	}else{
		var r = oReq.responseText.evalJSON();
		if(r.Error){
			alert(r.Error);
			$('StepNext').disabled = false;	
			$('StepNext').value = "Sign In";
		}else
			SaveChallenge(false);
		
	}
}

function AccountSignIn(oReq){
	if(!oReq){
		if(!$('SignInEmail').value.match(/\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,6}/))
			alert('Enter a valid email address.');
		else if(($('SignInPass').value.length < 4)||($('SignInPass').value.length > 25))
			alert('Enter a password between 4 - 25 characters');
		else{
			var url = 'account.php';
			var data = "&Operation=SignIn&Email="+$('SignInEmail').value+"&Password="+$('SignInPass').value;
			var myRequest = new Ajax.Request(url, {
				method:'post',
				parameters:data,
				onComplete:AccountSignIn
			});
			$('StepNext').value = "Please Wait...";
			$('StepNext').disabled = true;
		}
	}else{
		var r = oReq.responseText.evalJSON();
		if(r.Error){
			alert(r.Error);
			$('StepNext').disabled = false;	
			$('StepNext').value = "Next >>";
		}else
			SaveChallenge(false);
	}	
}


function AddBlog(oReq){
	if(!oReq){
		if(($('BlogPostTitle').value.length < 4)||($('BlogPostTitle').value.length > 128))
			alert('Enter a Headline between 4 - 128 characters in length.');
		else if((document.forms['BlogPost'].BlogPostDescription.value.length < 4))
			alert('Enter a Description atleast 4 characters in length.');
		else if(document.forms['BlogPost'].BlogPostBody.value.length < 4)
			alert('Enter a Body atleast 4 characters in length.');
		else{
			var publish = "yes";
			if($('PublishedNo').checked)
				publish = "no";
			var url = 'addblog.php';
			var data = "&Operation=";
			if(EditBlog == "yes")
				data += "EditBlogPost&BlogPostId="+BlogPostId;
			else
				data += "AddBlogPost";
			data += "&BlogId="+$('UserBlogId').value+"&Title="+urlencode($('BlogPostTitle').value)+"&Description="+urlencode(document.forms['BlogPost'].BlogPostDescription.value)+"&Body="+urlencode(document.forms['BlogPost'].BlogPostBody.value)+"&Published="+publish+"&FocusSphere="+$('FocusSphere').value+"&Sphere="+document.forms['AddSphere'].SphereToAdd.value;
			var myRequest = new Ajax.Request(url, {method:'post', parameters:data, onComplete:AddBlog});
			$('StepNext').value = "Please Wait...";
			$('StepNext').disabled = true;
			$('StepBack').disabled = true;
		}
	}else{
		var r = oReq.responseText.evalJSON();
		if(r.Error){
			alert(r.Error);
			$('StepNext').disabled = false;	
			$('StepBack').disabled = false
			$('StepNext').value = "Done";
		}else
			window.location = "user.php?Tab=3";
	}	
}