addLoadEvent(init);

var siteAddress = "http://www.onlinedating.org/";

var QUESTION_NAME_PREFIX = "question";
var NEXT_LINK_NAME_PREFIX = "nextQ";
var EARNEST_POINTS_CONTROL_NAME = "earned_points";
var QUESTIONS_FORM_NAME = "questions";

var currentQuestion = 1;

var questions = [
	{
		responsePoints: null  /* question 1 */
	},
 	{
		responsePoints: null  /* question 2 */
	},
	{
		responsePoints: null  /* question 3 */
	},
	{
		responsePoints: null  /* question 4 */
	},
	{
		responsePoints: null  /* question 5 */
	},
	{
		responsePoints: null  /* question 6 */
	},
	{
		responsePoints: null  /* question 7 */
	},
	{
		responsePoints: null  /* question 8 */
	},
	{
		responsePoints: null  /* question 9 */
	},
	{
		responsePoints: null  /* question 10 */
	},
	{
		responsePoints: null  /* question 11 */
	},
	{
		responsePoints: null  /* question 12 */
	}
]

var maleQuestionText= new Array();
var maleAnswerText=new Array();
maleQuestionText[0]="The building is on fire and you\'re in your room. You can only save one thing. What do you save?";
maleAnswerText[0]="The train set|The LCD widescreen TV|My hair gel|My wallet";
maleQuestionText[1]="You\'re with some friends and watching your favorite sporting event on the TV when suddenly, a female friend proclaims that they want to watch something on another channel. What do you do?";
maleAnswerText[1]="Glue the remote to my hand|Tell them to go home and watch it|Punch them|Say they can watch it, but then I\'d leave and go watch it somewhere else";

var femaleQuestionText= new Array();
var femaleAnswerText=new Array();
femaleQuestionText[0]="The building is on fire and you\'re in your room. You can only save one thing. What do you save?";
femaleAnswerText[0]="My mirror|My ridiculously small dog|My father\'s pocket watch or other important family heirloom|My favorite food from the fridge...No, the TV...No, the computer!";
femaleQuestionText[1]="You go to use the bathroom, but find the the seat has been left up. What do you do?";
femaleAnswerText[1]="Smash everything in a homicidal rage|Calmly put the seat down, but not before shedding a tear first|Use it anyway, complaining wont get me anywhere|Leave and attempt to find who did it";

var questionText= new Array();
var answerText=new Array();
questionText[0]="Your partner asks you to meet their parents. What do you do?";
answerText[0]="Immediately projectile vomit and say I\'m too ill to go|Say no, because I\'ve got more important things to do|Say yes, but I'm not going to have any fun|Break up with them";
questionText[1]="You're out for a meal with friends, when it comes time to pay, you realise you have no money. What do you do?";
answerText[1]="Complain that there\'s broken glass in the food in the hope I\'ll get the meal for free|Punch the nearest person and then sneak out in the ensuing brawl|Say I\'m really sorry and it wont happen again, honest|Pretend I\'m heading for the bathroom, but then slip out the door";
questionText[2]="You're at a movie theater and there's that one person that doesn\'t stop talking. What happens?";
answerText[2]="I yell at them to shut up|I throw my box of popcorn at them|I tell the usher they\'re disturbing the peace, hopefully they can do something about it|I sit behind them and set fire to their hair";
questionText[3]="A tramp asks you for change. What is your reaction?";
answerText[3]="Give him nothing, because he\'ll only spend it on drugs|Give him food instead, poor guy|Give him money, because he\'ll only spend it on drugs|Hurry on past, because...I\'m late for something. Yes, that\'s right...Late";

function setupGenderQuestions(mf) {
	var qId;
	var question;
	var labels;
	var label;
	var radios;
	for ( cnt=0; cnt < 6; cnt++) {
		qId=cnt + 2;
		question = document.getElementById('question'+qId).getElementsByTagName("h1");
		if ( cnt < 2 ){
			if ( mf == "m" ) {
				question[0].firstChild.nodeValue = unescape(maleQuestionText[cnt]);
			}else{
				question[0].firstChild.nodeValue = unescape(femaleQuestionText[cnt]);
			}
		} else {
			question[0].firstChild.nodeValue = unescape(question.nodeValue = questionText[cnt-2]);
		}	

		labels = document.getElementById('question'+qId).getElementsByTagName("label");

		for ( var x=0; x<=labels.length; x++) {
			if ( cnt < 2 ){
				if ( mf == "m" ) {
					var splitAnswers = maleAnswerText[cnt].split("|");
				}else{
					var splitAnswers = femaleAnswerText[cnt].split("|");
				}
			} else {
				var splitAnswers = answerText[cnt-2].split("|");
			}	
	
			var z = 0;
			for ( var y = 0; y < labels.length; y++) {
				var lbl = labels[y];
				lbl.firstChild.nodeValue=splitAnswers[z];
				z++;
			}
		}
	}
}

function calculateTotalPoints() {
	var total = 0;

	for (var i = 0;  i < questions.length; ++i) {
		var question;

		question = questions[i];
		total += question.responsePoints;
	}

	return total;
}


function goNext(question_id) {
	document.getElementById(QUESTION_NAME_PREFIX + question_id).style.display = 'none';
	if ( question_id == 1 ){
		var question = questions[0];
		var mf = '';
		if ( question.responsePoints ==  1) {
			document.getElementById('col1').className = 'guy';
			mf='m';
		}else{
			document.getElementById('col1').className = 'girl';
			mf='f';
		}
		question.responsePoints = 0;
		setupGenderQuestions(mf);
	}
	if ( document.getElementById(QUESTION_NAME_PREFIX + (question_id + 1) ) ) {
		var question;

	    question_id++;
		question = questions[question_id - 1];

	    document.getElementById(QUESTION_NAME_PREFIX + question_id).style.display = 'block';

	} else {

	    var points = calculateTotalPoints();
		
		document.getElementById(EARNEST_POINTS_CONTROL_NAME).value = points;
		
		document.getElementById('resultscontent').style.display='block';
		document.getElementById('resultscontent').className='';
		document.getElementById('quizcontent').className='hiddenDiv';
		var mf = document.getElementById('col1').className;
    	document.getElementById('col1').className = 'end';

    	if ( mf == 'guy' ){
    		mf = "man";
    	}else {
    		mf = "woman";
    	}
	    var imgRef = "";

	    if (points <= 11) {
	    	imgRef = mf+"A";
	    } else if (points >= 12 && points <= 15) {
	    	imgRef = mf+"B";
	    } else if (points >= 16 && points <= 20 ) {
	    	imgRef = mf+"C";
	    } else if (points >= 21 && points <= 24) {
	    	imgRef = mf+"D";
	    }
	    
	    document.getElementById('widgetImg').style.backgroundImage="url(images/badges/"+imgRef+".jpg)";
	    
		var embeddedStr = "<p style='line-height: normal'><a style='background: transparent url("+siteAddress+"images/badges/"+imgRef+".jpg) no-repeat scroll 0%; display: block; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; width: 250px; height: 200px; font-family: Georgia,serif; font-size: 12px; color: #ffffff; text-decoration: none; text-align: center;' href='http://www.onlinedating.org/'><br style='line-height: 170px;'><span style='padding-left: 70px; color:#FFFFFF; font-family:Georgia,serif; font-size:12px; text-align:center; text-decoration:none;'>Quiz by Onlinedating.org</span></a><br style='line-height: 4px;'/><font style='font-family: Arial; font-size: 9px;'>Quiz brought to you by <a href='http://www.onlinedating.org'>Online Dating</a></font></p>";
		
		document.getElementById('tArea').value = embeddedStr;
	}

	return question_id;
}


function goBack() {
	document.getElementById(QUESTION_NAME_PREFIX + currentQuestion).style.display='none';
	//$(QUESTION_NAME_PREFIX + currentQuestion).style.display='none';
	currentQuestion--;
	document.getElementById(QUESTION_NAME_PREFIX + currentQuestion).style.display='block';
}


function processReponseAndContinue(responsePoints) {
	var question = questions[currentQuestion - 1];

	question.responsePoints = responsePoints;

	currentQuestion = goNext(currentQuestion);
}


function init() {
	currentQuestion = 1;
}


function handleFormSubmit() {
	processReponseAndContinue();
}


// Adds commas into a number string
function addCommas( numberString )
{
	numberString += '';
	x = numberString.replace(/[^0-9.\-]*/g, "").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 addLoadEvent(func) {
	addEvent(window, "onload", func);
}

/*generic version*/
function addEvent(obj, evt, func) {
	var oldEvt = obj[evt];
	if (typeof oldEvt != 'function') {
		obj[evt] = func;
	} else {
		obj[evt] = function() { oldEvt(); func(); };
	};
}
var isIE = navigator.appName.indexOf('Microsoft') != -1;




