﻿Event.observe(window, 'load', init, false);

var earned_points = 0;
var offer = "";
var link = "";
var final_points_str = "";
var list_id = "";
var currentQuestionId;
var addpointsArray = new Array();

function init() { 
}


function goBack() {
	$('question' + currentQuestionId).style.display='none';
	currentQuestionId--;
	$('question' + currentQuestionId).style.display='block';
	
	earned_points -= addpointsArray.pop();
	percentage = earned_points / $('max_points').value;
    $('progress').style.width = Math.floor(percentage * 594) + 'px'; 
}


function increment_points(value) {	
  addpointsArray.push(value);

  earned_points += value;
  percentage = earned_points / $('max_points').value;
  if (percentage<0) {
	$('progress').style.width = '0px';
  }
  else {
    $('progress').style.width = Math.floor(percentage * 594) + 'px';
  }
}

function next_question(question_id) {
//  if (question_id == 0) {
//    $('description').style.display='none';
//  }
  $('question'+question_id).style.display='none';
  
  question_id++;  
  
  if ($('question'+question_id)) {
    currentQuestionId = question_id;
    $('question'+question_id).style.display='block';	
  } else {
    $('earned_points').value = earned_points;
    $('questions').submit();
  } 
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function createLink(product) {	
	switch(product) {
		case 'BOAB':
			link = "http://www.howbigwillmydownlinebe.com/building-on-a-budget/";
			document.list.unit.value = "howbig-boab";
			break;
		case 'BBR':
			link = "http://www.howbigwillmydownlinebe.com/black-belt-recruiting/";
			document.list.unit.value = "howbig-bbr";
			break;
		case 'MS':
			link = "http://www.howbigwillmydownlinebe.com/magnetic-sponsoring/";
			document.list.unit.value = "howbig-ms";
			break;
		case 'BN':
			link = 'http://www.howbigwillmydownlinebe.com/better-networker/';
			break;
	    case 'TF':
			link = 'http://www.howbigwillmydownlinebe.com/mlm-traffic-formula/';
			break;
	    default:
			link = "http://www.howbigwillmydownlinebe.com/magnetic-sponsoring/";
			document.list.unit.value = "howbig-ms";
			break;
	}
	return link;
}

function results() {
	final_points_str = addCommas(earned_points);
	document.getElementById('badgecode').innerHTML = final_points_str;
	document.getElementById('message').innerHTML = final_points_str;
	embeddedStr = "<p style='line-height: normal'><a href='http://www.howbigwillmydownlinebe.com/' style='display: block; background: 	url(http://www.howbigwillmydownlinebe.com/images/downlineresults250x200.jpg) no-repeat; width: 250px; height: 200px; font-family: Arial, sans-serif; font-size: 30px; color: #000; text-decoration: none; text-align: center;'><br style='line-height: 133px;'>" + final_points_str + "</a><br style='line-height: 5px; height: 5px;'><font style='font-size: 10px; font-family: sans-serif;'>Grow it bigger with <a href='http://magneticleadgeneration.com/magnetic-sponsoring/'>Magnetic Sponsoring</a></font></p>";
	$('tArea').value = embeddedStr;
	$('widgetresultwrap').style.display='block';
	$('quiz_container').style.display='none';
	location.hash="#result";
}

function onSubmitForm() {
	if(document.pressed == 'Free Instant Answers!') {
		if (!(validate_name(document.list.name,document.list.name.value,'Please enter your name!'))) {
			return false; }
		if (!(validate_email(document.list.from,'Please enter a valid email address!'))) {
			return false; }
		document.list.meta_redirect_onlist = link;
		document.list.action ="http://www.aweber.com/scripts/addlead.pl"
		return true;
	}
	else
	if(document.pressed == 'No, Thanks') {
		window.open(link,'');
		return false
	}
}

function capitalizeMe(obj) {
        val = obj.value;
        newVal = '';
        val = val.split(' ');
        for(var c=0; c < val.length; c++) {
                val[c] = val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length);
        }
        obj.value = val.join(' ');
}

function validate_name(fld, valid, msg) {
    if (valid)
        return true;
    alert(msg);
    fld.focus();
    try { fld.select() } catch(err) { };
    return false;
}

function validate_email(fld,msg) {
	with (fld) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2){
			alert(msg);
			fld.focus();
			try { fld.select() } catch(msg) { };
			return false;
		}
		else {return true;}
	}
}