/*
	TellMeMyType 2.0

   JavaScript functions
*/

function get_browser_version() {
	var browserVer = 0;
	agent = navigator.userAgent
	if (agent.indexOf("Mozilla/3.0") != -1)
		browserVer = 3;
	else if (agent.indexOf("Mozilla/4.0") != -1)
		browserVer = 3;
	else if (agent.indexOf("MSIE") != -1)
		browserVer = 1;
	else if (agent.indexOf("Mozilla/2.0") != -1)
		browserVer = 2;
}

function preload_menu_images() {
	var images_num = 12;
	var image_width = 16;
   var image_height = 16;


	var images_names = new Array(images_num);
	images_names[0] = 'home';
	images_names[1] = 'rese';
	images_names[2] = 'ref';
	images_names[3] = 'faq';
	images_names[4] = 'pres';
	images_names[5] = 'con';
	images_names[6] = 'log';
	images_names[7] = 'test';
	images_names[8] = 'resu';
   images_names[9] = 'prv';
   images_names[10] = 'ter';
   images_names[11] = 'leg';

	images = new Array(images_num * 2);
	for(var i = 0; i < images_num ; i++) {
		images[i*2] = new Image(image_width, image_height);
		images[i*2].src = images_names[i] + '1.gif';
      images[i*2+1] = new Image(image_width, image_height);
      images[i*2].src = images_names[i] + '2.gif';
	}
}


function validvalue (checkStr, lenmin, lenmax, checkOK) {
	if (checkStr=="") return false;
	if (lenmin && (checkStr.length < lenmin)) return false;
	if (lenmax && (checkStr.length > lenmax)) return false;

	if (!checkOK) return true;

	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++) 
			if ((ch == checkOK.charAt(j)) || (ch == checkOK.charAt(j).toUpperCase())) 
				break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	return allValid;
}

function valid_email (email) {
	
	txt = email.toLowerCase();
	if ( !validvalue(txt, 0, 0, "qwertyuiopasdfghjklzxcvbnm0123456789.!\@#\$%^&*()_-=+") ) {
		alert('The email address you entered seems to be incorrect. Please check it up.');
		return false;
	}
	
	iat = txt.indexOf('\@');
	if ( iat < 0 ) {
		alert("The email address you entered seems to be incorrect. Please make sure that this address contains '@' sign.");
		return false;
	}
	if ( iat < 1 ) {
		alert("The email address you entered seems to be incorrect. Please check up the part of address before '@' sign.");
		return false;
	}
	
	txt = txt.substring(iat+1);
	if ( !validvalue(txt, 0, 0, "qwertyuiopasdfghjklzxcvbnm.0123456789-") ) {
		alert("The email address you entered seems to be incorrect. Please check up the part of address after '@' sign.");
		return false;
	}
	
	iat = txt.indexOf('.');
	if ( iat < 0 ) {
		alert("The email address you entered seems to be incorrect. Please check up the part of address after '@' sign.");
		return false;
	}
	
	while (iat >= 0) {
		txt  = txt.substring(iat+1);
		if (iat == 0) {
			alert("The email address you entered seems to be incorrect. Please check up the part of address after '@' sign.");
			return false;
		}
		iat = txt.indexOf('.');
	}

	if ((txt.length > 3) || (txt.length < 2)) {
		alert("The email address you entered seems to be incorrect. Please check up the part of address after '@' sign.");
		return false;
	}

	if ((txt.length > 2) && (txt != 'com') && (txt != 'net') && 
		(txt != 'org') && (txt != 'mil') && (txt != 'edu') && (txt != 'gov') ) {
		alert("This email address seems wrong. Please"
+ " check the domain name after '\@' sign."
+ " Unless you are residing outside the United States, most e-mail suffixes"
+ " should include a .com, .edu, .net, .org, .gov or .mil");
		return false;
	}

	if ( !validvalue(txt, 0, 0, "qwertyuiopasdfghjklzxcvbnm") ) {
		alert("This email address seems wrong. Please"
+ " check the domain name after '@' sign."
+ " Unless you are residing outside the United States, most e-mail suffixes"
+ " should include a .com, .edu, .net, .org, .gov or .mil");
		return false;
	}
	return true;
}


//--------------------------------


// source test form vars
var source_vars_radio = new Array (
	'f2n', 'f6n', 'f9n', 'f1n', 'n1n',
	'n4n', 'n8n', 'n3n', 'n9n', 'e4n',
	'n2n', 'n5n', 'e5n', 'e3n', 'e9n',
	'e1n', 'p6n', 'p7n', 'e8n', 'e2n'
);

var source_vars_select = new Array (
	'gender', 'agecc', 'haskid', 'kidm', 'kidf',
	'pets', 'dog', 'cat', 'bird', 'job', 'educ'
);

// new SPSS vars
var new_vars = new Array (
	'compas', 'mind', 'softy', 'sens', 'creativ',
	'ideas', 'abstr', 'task', 'produce', 'group',
	'exper', 'memory', 'energ', 'hardmt', 'speak',
	'organiz', 'commit', 'noise', 'think', 'gender',
	'agec', 'childc', 'numboyc', 'numgirlc','pets',
	'numdogsc', 'numcatsc', 'numbrdsc', 'empselfc', 'educself', 'privat'
);

// 'source vars -> SPSS vars' transitions
var transitions = new Array(
	'compas = f2n;',
	'mind = 1 - f6n;',
	'softy = f9n;',
	'sens = f1n;',
	'creativ = n1n;',
	'ideas= n4n;',
	'abstr = n8n;',
	'task = n3n;',
	'produce = 1 - n9n;',
	'group = e4n;',
	'exper = 1 - n2n;',
	'memory = 1 - n5n;',
	'energ = e3n;',
	'hardmt = 1 - e9n;',
	'speak = e1n;',
	'organiz = 1 - p6n;',
	'commit = 1- p7n;',
	'noise = e8n;',
	'think = 1 - e2n;',
	'gender = gender;',
	'agec = agecc;',
	'childc = haskid;',
	'numboyc = kidm;',
	'numgirlc = kidf;',
	'pets = pets;',
	'numdogsc = dog;',
	'numcatsc = cat;',
	'numbrdsc = bird;',
	'empselfc = 2 - job;',
	'educself = educ;',
	'privat = 1- e5n;'
);

// checks test form

//
/*
function runtest(theForm) {
   set_vars_line('0100000010110101101101101010171');
   window.location.href = 'results.html';
	return true;
}
*/

// this function is called when test form is submitted
function onRuntest(theForm)
{
	if(checkform(theForm) == false)
		return false;


   // reading test form vars
   for(i = 0; i < source_vars_radio.length; i++ ) {
      if(theForm.elements[source_vars_radio[i]][0].checked == true)
			eval('var ' + source_vars_radio[i] + "=0;");
		else
			eval('var ' + source_vars_radio[i] + "=1;");
	}

   for(i = 0; i < source_vars_select.length; i++ ) {
      elem = theForm.elements[source_vars_select[i]];
		eval(source_vars_select[i] + '=' + elem[elem.selectedIndex].value);
	}

	string = '';
   for(i = 0; i < new_vars.length; i++) {
   	eval(transitions[i]);
   	eval('string += ' + new_vars[i]);
	}

   set_vars_line(string);
	window.location.href = 'results.php';
	return true;
}


function set_vars_line(varsline) {
   document.cookie = "varsline=" + varsline;
}

function get_vars_line(){
	if((index = document.cookie.indexOf('varsline')) != -1)
		varsline = document.cookie.substring(index+9, index + 9 + new_vars.length);
	else {
		alert('Unknown error occured');
		document.location.href = 'index.html';
	}
	return varsline;
}

function extract_varsline(varsline) {
	for(i = 0; i < new_vars.length; i++)
   	eval(new_vars[i] + '=' + varsline.charAt(i));

   // BREAK OUT AGE CATEGORIES //
   
   youngn = Youngn = (agec == 1 ? 1 : 0);
   thirty = Thirty = (agec == 2 ? 1 : 0);
   fourty = Fourty = (agec == 3 ? 1 : 0);
   fifty = Fifty  = (agec == 4 ? 1 : 0);
   oldfart = OldFart =(agec == 5 ? 1 : 0);

   // over fourty.//

   ageccc = (Fourty == 1 || Fifty == 1 || OldFart == 1 ? 1 : 0);

   // under fourty //
   Under40 = (ageccc == 0 ? 1 : 0);

   // BREAK OUT EDUCATION CATS //
	educslfc = (educself >= 6 ? 1 : 0);

	// SOME OTHER PERSONALITY OPPOSITES FOR EQUATIONS //
	intuit = (exper == 0 ? 1 : 0);
   heart = (mind == 0 ? 1 : 0);
   uncommit = (commit == 0 ? 1 : 0);
   uneds = (educslfc == 0 ? 1 : 0);
   male = (gender == 1 ? 1: 0);
   female = (gender == 2 ? 1: 0);

	// MBTI dimensions //
	feeling = ((compas - mind + softy + sens) - 1.5196) / 1.3984;
	nnn = ((creativ + ideas + abstr + task - produce - exper) - .7493) / 1.9439;
	extra = ((group - privat  + energ - hardmt + speak + energ - think + noise ) - 1.1485) / 2.0993;
	perc = ((organiz + commit) - 1.3165) / .8140;
	// END CONVERSION OF WEB PERSTEST PREDICTOR NAMES TO SPSS VAR NAMES //
}


// generates random number between x1 and x2
function rand (x1, x2) {
   if(x1 > x2) {
   	tmp = x1;
   	x1 = x2;
   	x2 = tmp;
   }

   return x1 + Math.random()*(x2 - x1);
}

function my_round(num, digits) {
	mult = 10;
	for (var i = 1; i < digits; i++)
		mult *= 10;
	return Math.round(num * mult) / mult;
}

   // convert z scores to approximate percentiles //
function ZtoP(xug){

     Tz = Math.abs(xug);
     ToAddMe = 0;
     
     if (Tz >= .0  && Tz < .1) {ToAddMe =  rand(165, 175);}
     if (Tz >= .1  && Tz < .2) {ToAddMe =  rand(495, 595);}
     if (Tz >= .2  && Tz < .3) {ToAddMe =  rand(895, 987);}
     if (Tz >= .3  && Tz < .4) {ToAddMe =  rand(1287, 1368);}
     if (Tz >= .4  && Tz < .5) {ToAddMe =  rand(1668, 1763);}
     if (Tz >= .5  && Tz < .6) {ToAddMe =  rand(1963, 2088);}
     if (Tz >= .6  && Tz < .7) {ToAddMe =  rand(2188, 2257);}
     if (Tz >= .7  && Tz < .8) {ToAddMe =  rand(2657, 2734);}
     if (Tz >= .8  && Tz < .9) {ToAddMe =  rand(2934, 3023);}
     if (Tz >= .9  && Tz < 1.0) {ToAddMe = rand(3123, 3289);}
     if (Tz >= 1.0  && Tz < 1.1) {ToAddMe = rand(3489, 3531);}
     if (Tz >= 1.1  && Tz < 1.2) {ToAddMe = rand(3731, 3749);}
     if (Tz >= 1.2  && Tz < 1.3) {ToAddMe = rand(3849, 3944);}
     if (Tz >= 1.3  && Tz < 1.4) {ToAddMe = rand(4044, 4115);}
     if (Tz >= 1.4  && Tz < 1.5) {ToAddMe = rand(4115, 4265);}
     if (Tz >= 1.5  && Tz < 1.6) {ToAddMe = rand(4264, 4394);}
     if (Tz >= 1.6  && Tz < 1.7) {ToAddMe = rand(4444, 4505);}
     if (Tz >= 1.7  && Tz < 1.8) {ToAddMe = rand(4505, 4591);}
     if (Tz >= 1.8  && Tz < 1.9) {ToAddMe = rand(4591, 4678);}
     if (Tz >= 1.9  && Tz < 2.0) {ToAddMe = rand(4678, 4744);}
     if (Tz >= 2.0  && Tz < 2.1) {ToAddMe = rand(4744, 4798);}
     if (Tz >= 2.1  && Tz < 2.2) {ToAddMe = rand(4798, 4842);}
     if (Tz >= 2.2  && Tz < 2.3) {ToAddMe = rand(4842, 4878);}
     if (Tz >= 2.3  && Tz < 2.4) {ToAddMe = rand(4878, 4906);}
     if (Tz >= 2.4  && Tz < 2.5) {ToAddMe = rand(4906, 4934);}
     if (Tz >= 2.5  && Tz < 2.6) {ToAddMe = rand(4934, 4960);}
     if (Tz >= 2.6  && Tz < 2.7) {ToAddMe = rand(4960, 4985);}
     if (Tz >= 2.7  && Tz < 2.8) {ToAddMe = rand(4984, 4990);}
     if (Tz >= 2.8  && Tz < 2.9) {ToAddMe = rand(4990, 4995);}
     if (Tz >= 2.9  && Tz < 3.0) {ToAddMe = rand(4995, 4999);}
     if (Tz >= 3.0              ) {ToAddMe = 4999.5;}

     if (xug >= 0) {Tp = 50 + (ToAddMe) / 100;}
     if (xug < 0)  {Tp = 50 - (ToAddMe) / 100;}
     // PHP round(Tp, 2)
     Tp = Math.round(Tp*100) / 100;

     return Tp;
}

function str_repeat(str, times) {
	var local_out= '';
	for(var i = 0; i < times; i++)
		local_out += str;
	return local_out;
}

function set_page(page) {
	if(page == false)
		page ='';
	document.cookie = "page=" + page;
	return true;
}

function get_page() {
	if((pos = document.cookie.indexOf('page=')) == -1)
		return false;

	if((endpos = document.cookie.indexOf(';', pos)) == -1)
		endpos = document.cookie.length;

	if(pos + 5 != endpos)
		return document.cookie.substring(pos +5, endpos);
}

function set_pe_headers(val) {
	if(val)
		document.cookie = 'pe_headers=1';
	else
		document.cookie = 'pe_headers=0';
	return true;
}

