$(document).ready(function(){
	var s1 = $("#p1");
	var s2 = $("#p2");
	var s3 = $("#p3");
	s1.blur(validate1);
	s1.keyup(validate1);
	s2.blur(validate2);
	s2.keyup(validate2);
	s3.blur(validate3);
	s3.keyup(validate3);
	function validate1(){
		//if it's NOT valid
		if(isNaN(s1.val())){
			s1.addClass("error");
			s1.val(0);
			//recalc();
			//t1.val(parseInt(s2.val()) + parseInt(s3.val()) + parseInt(s4.val()));
			//$("input[name^=sum]").sum("keyup", "#totalSum");
			s1.val('');
			return false;
		}
		//if it's valid
		else{
			s1.removeClass("error");
			this.s1.val();
			return true;
		}
	}
	function validate2(){
		//if it's NOT valid
		if(isNaN(s2.val())){
			s2.addClass("error");
			s2.val(0);
			//recalc();
			//t1.val(parseInt(s2.val()) + parseInt(s3.val()) + parseInt(s4.val()));
			//$("input[name^=sum]").sum("keyup", "#totalSum");
			s2.val('');
			return false;
		}
		//if it's valid
		else{
			s2.removeClass("error");
			this.s2.val();
			return true;
		}
	}
	function validate3(){
		//if it's NOT valid
		if(isNaN(s3.val())){
			s3.addClass("error");
			s3.val(0);
			//recalc();
			//t1.val(parseInt(s2.val()) + parseInt(s3.val()) + parseInt(s4.val()));
			//$("input[name^=sum]").sum("keyup", "#totalSum");
			s3.val('');
			return false;
		}
		//if it's valid
		else{
			s3.removeClass("error");
			this.s3.val();
			return true;
		}
	}
	$('#regForm').submit(function(e) {

		register();
		e.preventDefault();
		
	});
	
});


function register()
{
	hideshow('loading',1);
	error(0);
	
	$.ajax({
		type: "POST",
		url: "submit.php",
		data: $('#regForm').serialize(),
		dataType: "json",
		success: function(msg){
			
			if(parseInt(msg.status)==1)
			{
				window.location=msg.txt;
			}
			else if(parseInt(msg.status)==0)
			{
			error(1,msg.txt);
			//document.getElementById('siimage').src = 'securimage_show.php?sid=' + Math.random();;
			//document.getElementById('captcha-form').focus();
			}
			
			hideshow('loading',0);
		}
	});

}

function hideshow(el,act)
{
	if(act) $('#'+el).css('visibility','visible');
	else $('#'+el).css('visibility','hidden');
}

function error(act,txt)
{
	hideshow('error',act);
	if(txt) $('#error').html(txt);
}
