// JavaScript Document

$(document).ready(function() {
	
	/*
	 *  use javascript to show log in form
	 *  this way if user has javascript disabled 
	 *  they cannot log in
	 */
	$('form#login').css('display', 'block');
	
	// fade submenu to 0.8 opacity
	$('#top_menu ul li ul').fadeTo('fast', 0.8);
	
	$("#search").autocomplete('includes/search.ajax.php');
});


// alert errors & replace <break> with \r\n\r\n
function callError(msg, relocate) {
	breakMsg = msg.replace(/<break>/gi, '\r\n');
	alert(breakMsg);
	window.location = relocate;
}

// check email
function checkEmail(id) {
	var email = $('#'+id).val();
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var result = $('#emailRes');

	if (!filter.test(email)) {
		result.html('<img src="images/cross.gif" />');
	} else {
		result.html('<img src="images/tick.gif" />');	
	}
	
}

// activate users account from confirmation email
function startActivate(id) {
	$.ajax({
		type: "POST",
		url: "includes/activate.ajax.php",
		data: "id="+id, 
		
		success: function(msg){
			if(msg == "go") {
				window.location = "/iov_videoskills/?e=activate";	
			} else if(msg == "no") {
				window.location = "/iov_videoskills/?e=activateF";	
			}
		}
	});
}

// open video pop up window 
function studyWinOpen(url) {
	window.open(url, "TITLE", "width=620px, height=420px, address=0, status=0, resizable=0");
}

// user has completed test function
function finishUser(id, status) {
	// fade whole document
	$('#footer_bg').fadeTo("fast", 0.1);
	$('#loading_ajax').fadeIn("slow");
	
	$.ajax({
		type: "POST",
		url: "includes/finish_user.ajax.php",
		data: "id="+id+"&status="+status, 
		
		success: function(msg){
			if(msg == "ok") {
				window.location = "/iov_videoskills/";	
			} else {
				window.location = "/iov_videoskills/?e=completemailerror";
			}
		}
	});
}

// ask user to confirm befroe redirecting
function doubleCheck(msg, url) {
	var con = confirm(msg);
	if(con == true) {
		window.location = url;
	} else {
		return false;
	}
}

function txtSize(type) {
	
	var size = $('#internal_copy').css('fontSize');
	var intSize = parseInt(size);
	
	if(type == 1) {
		var newSize = intSize + 1;
	} else if(type == 0) {
		var newSize = intSize - 1;
	}

	$.ajax({
		type: "POST",
		url: "includes/change_txt.ajax.php",
		data: "size="+newSize
	});

	if (newSize < 20 && newSize > 5) {
		$('#internal_copy').css('fontSize', newSize);
	}
}

function fullReg() {
	alert("To take the exam you must first join the IOV then log in to videoskills with your IOV username and password");
}

function highScoreMod() {
	var modules = document.getElementById('modules');
	var choice = modules.options[modules.selectedIndex].value;
	
	window.location = 'high_scores.php?id='+choice;
}

function checkUrl() {
	var quirksMode = (top == self);
	if (!quirksMode) top.location.href = 'http://www.videoskills.net';
}
