addLoadEvent(init);

var siteAddress = "http://cloudfront.cupidmarket.com/";

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]="You're stranded on a desert island, but can only bring one thing. What do you bring?";
maleAnswerText[0]="My laptop|Pictures of old girlfriends|My Lamborghini Balboni|Flowers in case a girl is stranded there too";
maleQuestionText[1]="When you meet a new girl for the first time, you:";
maleAnswerText[1]="flirt your ass off|introduce yourself in a friendly way|feel awkward and shy|punch them";

var femaleQuestionText= new Array();
var femaleAnswerText=new Array();
femaleQuestionText[0]="You're stranded on a desert island, but can only bring one thing. What do you bring?";
femaleAnswerText[0]="My little dog|Photos of Ryan Reynolds|Box of chocolates|My mirror";
femaleQuestionText[1]="You exchanged phone numbers with a guy the other day. He seems nice, but you're not that physically attracted to him. Do you:";
femaleAnswerText[1]="let him be friends|ignore his calls|agree to date him as long as he brings you to a fancy restaurant|tell him to leave you the hell alone";

var questionText= new Array();
var answerText=new Array();
questionText[0]="If you could have one wish granted, you'd wish for:";
answerText[0]="fortune and fame|someone who understands and loves you|an end to hunger and war, and a new car|an understanding of the universe";
questionText[1]="If you're visiting a place you've never been before, you:";
answerText[1]="feel rather out of place and prefer to stick to an area where you are comfortable|want to go off the beaten path and get a real taste of the area|want to explore, but not stray too far|stay in the apartment";
questionText[2]="You're kickin' it at a house party. Who are you hanging out with?";
answerText[2]="doh my friends!|I mingle cuz I'm single!|Sidle on up to the hottest person there and make a move|Um house party? What's that?";
questionText[3]="How would you describe your physical apperance?";
answerText[3]="I need a paper bag|Attractive|I.. have a good personality?|Breathtaking";

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(http://cloudfront.cupidmarket.com/quiz1/quiz1-images/completion-badge/"+imgRef+".jpg)";
	    
		var embeddedStr = "<p style='line-height: normal'><a style='background: transparent url("+siteAddress+"quiz1/quiz1-images/completion-badge/"+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.cupidmarket.com/'><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 CupidMarket.com</span></a><br style='line-height: 4px;'/><font style='font-family: Arial; font-size: 9px;'>Quiz brought to you by <a href='http://www.cupidmarket.com'>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;





