//global variables that can be used by ALL the functions on this page.
var checks;
var radios;
var checkOn = 'http://www.sap-tests.com/images/checkOn.gif';
var checkOff = 'http://www.sap-tests.com/images/checkOff.gif';
var radioOn = 'http://www.sap-tests.com/images/radioOn.gif';
var radioOff = 'http://www.sap-tests.com/images/radioOff.gif';
var checkRedOn = 'http://www.sap-tests.com/images/checkRedOn.gif';
var checkRedOff = 'http://www.sap-tests.com/images/checkRedOff.gif';
var radioRedOn = 'http://www.sap-tests.com/images/radioRedOn.gif';
var radioRedOff = 'http://www.sap-tests.com/images/radioRedOff.gif';
var checkGreenOn = 'http://www.sap-tests.com/images/checkGreenOn.gif';
var checkGreenOff = 'http://www.sap-tests.com/images/checkGreenOff.gif';
var radioGreenOn = 'http://www.sap-tests.com/images/radioGreenOn.gif';
var radioGreenOff = 'http://www.sap-tests.com/images/radioGreenOff.gif';


function onConfirm()
{
	var answers = document.getElementsByName('answers[]');
	var sel = 0;
	
	if(answers && answers.length)
	{
		for(i = 0; i < answers.length; ++i)
		{
			if(answers[i].checked == true)
				++sel;
		}
	}
	
	if(!sel)
	{
		alert('You have to select at least one answer to confirm this question!');
		return false;
	}
	
	var inp = document.getElementById('confirm');
	if(inp)
		inp.value = 1;
	
	return true;
}

function onResult(qconf, qcount)
{
	if(qconf != qcount - 1)
	{
		if(confirm('You still have some unconfirmed questions. Continue anyway?') == false)
			return false;
	}
	else
	{
		var answers = document.getElementsByName('answers[]');
		var sel = 0;
		
		if(answers && answers.length)
		{
			for(i = 0; i < answers.length; ++i)
			{
				if(answers[i].checked == true)
					++sel;
			}
		}
		
		if(!sel)
		{
			if(confirm('You didn\'t choose an answer for the last question. Continue anyway?') == false)
				return false;
		}
	}
	
	document.forms[0].action = 'result.html';

	return true;
}

function onEndTest(page)
{
	var inp = document.getElementById('action');
	if(inp)
		inp.value = page;
}

//this function runs when the page is loaded, put all your other onload stuff in here too.
function initCheckRadio(disabled)
{
	//> preload images
	var img = new Image();
	img.src = checkOff;
	
	img = new Image();
	img.src = checkOn;
	
	img = new Image();
	img.src = radioOff;
	
	img = new Image();
	img.src = radioOn;
	//<

	replaceChecks(disabled);
	replaceRadios(disabled);
}

function replaceChecks(disabled)
{
	//get all the input fields on the page
	checks = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i = 0; i < checks.length; i++)
	{
		//check if the input is a checkbox
		if(checks[i].getAttribute('type') == 'checkbox')
		{
			var type = 0;
			if(checks[i].getAttribute('value') == 'red')
				type = 1;
			else if(checks[i].getAttribute('value') == 'green')
				type = 2;
		
			//create a new image
			var img = document.createElement('img');
			
			//check if the checkbox is checked
			if(checks[i].checked)
			{
        if(type == 0)
					img.src = checkOn;
				else if(type == 1)
					img.src = checkRedOn;
				else if(type == 2)
					img.src = checkGreenOn;
					
				img.alt = '[x]';
      }
      else
      {
        if(type == 0)
					img.src = checkOff;
				else if(type == 1)
					img.src = checkRedOff;
				else if(type == 2)
					img.src = checkGreenOff;
					
				img.alt = '[ ]';
      }

			//set image ID and onclick action
			img.id = 'checkImage'+i;
			img.width = 15;
			img.height = 15;
			//set image 
			img.onclick = new Function('checkChange('+i+', '+type+', '+disabled+')');
			//img.onmouseover = 'this.style.cursor='hand'';
			//place image in front of the checkbox
			checks[i].parentNode.insertBefore(img, checks[i]);
			
			//hide the checkbox
			checks[i].style.display='none';
		}
	}
}

function replaceRadios(disabled)
{
	//get all the input fields on the page
	radios = document.getElementsByTagName('input');

	//cycle trough the input fields
	for(var i = 0; i < radios.length; i++)
	{
		//radio if the input is a radiobox
		if(radios[i].getAttribute('type') == 'radio')
		{
			var type = 0;
			if(radios[i].getAttribute('value') == 'red')
				type = 1;
			else if(radios[i].getAttribute('value') == 'green')
				type = 2;
		
			//create a new image
			var img = document.createElement('img');
			
			//radio if the radiobox is radioed
			if(radios[i].checked)
			{
        if(type == 0)
					img.src = radioOn;
				else if(type == 1)
					img.src = radioRedOn;
				else if(type == 2)
					img.src = radioGreenOn;
					
				img.alt = '(o)';
      }
      else
      {
        if(type == 0)
					img.src = radioOff;
				else if(type == 1)
					img.src = radioRedOff;
				else if(type == 2)
					img.src = radioGreenOff;
					
				img.alt = '( )';
      }

			//set image ID and onclick action
			img.id = 'radioImage'+i;
			img.width = 15;
			img.height = 15;
			//set image 
			img.onclick = new Function('radioChange('+i+', '+type+', '+disabled+')');
			//place image in front of the radiobox
			radios[i].parentNode.insertBefore(img, radios[i]);
			
			//hide the radiobox
			radios[i].style.display='none';
		}
	}
}

//change the checkbox status and the replacement image
function checkChange(n, type, disabled)
{
	if(disabled == true)
		return;
		
	var img = document.getElementById('checkImage'+n);

	if(checks[n].checked == true)
	{
		checks[n].checked = false;
		if(type == 0)
			img.src = checkOff;
		else if(type == 1)
			img.src = checkRedOff;
		else if(type == 2)
			img.src = checkGreenOff;
			
		img.alt = '[ ]';
	}
	else
	{
		checks[n].checked = true;
		if(type == 0)
			img.src = checkOn;
		else if(type == 1)
			img.src = checkRedOn;
		else if(type == 2)
			img.src = checkGreenOn;
			
		img.alt = '[x]';
	}
}

//change the radio status and the replacement image
function radioChange(n, type, disabled)
{
	if(disabled == true)
		return;

	if(radios[n].checked == false)
	{
		var img = document.getElementById('radioImage'+n);
	
		radios[n].checked = true;
		if(type == 0)
			img.src = radioOn;
		else if(type == 1)
			img.src = radioRedOn;
		else if(type == 2)
			img.src = radioGreenOn;
			
		img.alt = '(o)';
	}
	
	for(var i = 0; i < radios.length; ++i)
	{
		if(radios[i].getAttribute('type') == 'radio' && i != n)
		{
			var img = document.getElementById('radioImage'+i);
		
			if(type == 0)
				img.src = radioOff;
			else if(type == 1)
				img.src = radioRedOff;
			else if(type == 2)
				img.src = radioGreenOff;
				
			img.alt = '( )';
		}
	}
}

function getVerify()
{
	var a = Math.floor(Math.random( ) * 33);
	var b = Math.floor(Math.random( ) * 33);
	var c = a+b;
	
	document.write('<tr><td align=left>' + a + ' + ' + b + ' = </td>');
	document.write('<td><input type="text" id="verify" name="verify" value="" size=2 maxlength=255><input type=hidden id="sum" name="sum" value="' + c + '"> (Spam protection. Please, calculate or <a href="login.html">login</a>. Thank you.)</td></tr>');
}

function onComments()
{
	var inp = document.getElementById('comments');
	if(inp && inp.value && inp.value.length > 128)
	{
		inp.value = inp.value.slice(0, 128);
	}
}

function onMBPay() //for MoneyBookers
{
	var inpq = document.getElementById('mbquantity');
	var inpa = document.getElementById('mbamount');
	
	if(inpq && inpa)
		inpa.value = inpq.value*5.99;
}

function checkRegister()
{
	var inp = document.getElementById('login');
	if(!inp || inp.value.length < 3)
	{
		alert('Error!\nYou should enter a vaild User Name, more than 3 symbols and contains only letters and numbers.');
		return false;
	}
	
	var inp = document.getElementById('email');
	if(!inp || inp.value == '')
	{
		alert('Error!\nEnter a valid email!');
		return false;
	}
	
	var inp = document.getElementById('email2');
	if(!inp || inp.value == '')
	{
		alert('Error!\nEnter the same email second time!');
		return false;
	}
	
	var inp = document.getElementById('password');
	if(!inp || inp.value.length < 6)
	{
		alert('Error!\nEnter password, 6 symbols minimum!');
		return false;
	}
	
	var inp = document.getElementById('password2');
	if(!inp || inp.value.length < 6)
	{
		alert('Error!\nRepeat the same password second time!');
		return false;
	}
	
	return true;
}

function checkProfile()
{
	var inp = document.getElementById('curpassword');
	if(!inp || inp.value == '')
	{
		alert('Error!\nPlease enter your current password!');
		return false;
	}
	
	return true;
}

function checkFields(who, loggedIn)
{
	if(who == 0)
	{
		var inp = document.getElementById('friendname');
		if(!inp || inp.value == '')
		{
			alert('Error! You should enter friend\'s name.');
			return false;
		}
		
		inp = document.getElementById('friendemail');
		if(!inp || inp.value == '')
		{
			alert('Error! You should enter friend\'s email.');
			return false;
		}
		
		inp = document.getElementById('yourname');
		if(!inp || inp.value == '')
		{
			alert('Error! You should enter your name.');
			return false;
		}
		
		inp = document.getElementById('youremail');
		if(!inp || inp.value == '')
		{
			alert('Error! You should enter your email.');
			return false;
		}
	}
	
	if(who == 1)
	{
		inp = document.getElementById('name');
		if(!inp || inp.value == '')
		{
			alert('Error! You should enter name.');
			return false;
		}
		
		inp = document.getElementById('email');
		if(!inp || inp.value == '')
		{
			alert('Error! You should enter email.');
			return false;
		}
	
		inp = document.getElementById('subject');
		if(!inp || inp.value == '')
		{
			alert('Error! You should enter subject.');
			return false;
		}
	}
	
	if(!loggedIn)
	{
		inp = document.getElementById('verify');
		inps = document.getElementById('sum');
		if(!inp || !inps || inp.value != inps.value)
		{
			alert('Error! Calculation is wrong.');
			return false;
		}
	}
	
	return true;
}

function onTestsNum(num)
{
	num = parseInt(num);

	var td = document.getElementById('tests_combos');
	if(td)
	{
		var html = '<table class=table_inner>';

		//> adding combos with Tests
		for(var i = 1; i <= num; i++)
		{	
			var combo = comboTests.replace(/%t/g, i);
			combo = combo.replace(/%f/g, '');
			html += combo;
		}
		//<

		//> adding extra combos
		var num2 = Math.floor(num/3);
		for(var i = 1; i <= num2; i++)
		{
			var combo = comboTests.replace(/%t/g, i+num);
			combo = combo.replace(/%f/g, ' <b><font color=red>FREE</font></b>');
			html += combo;
		}
		//<

		html += '</table>';

		td.innerHTML = html;
	}
}

function onTestsPay()
{
	var res = document.getElementById('os1');
	var tests = document.getElementsByName('test');
	
	if(res && tests)
	{
		var txt = '';
		for(var i = 0; i < tests.length; i++)
		{
			if(tests[i].value == 0)
			{
				alert('Please select all the Tests!');
				return false;
			}
		
			if(i > 0)
				txt += ',';
			txt += tests[i].value;
		}
		
		res.value = txt;
		
		return true;
	}
	
	return false;
}

function logoPayPal(domain)
{
	document.write('<a href="https://www.paypal.com/us/verified/pal=premium%40sap-tests.com" target="_blank" rel="nofollow"><img src="'+domain+'images/paypal_verified.gif" border="0"></a>');
}