/*if(top.location == location)
{
	top.location.href = 'http://assistant.moba-app.com/default.html';
}*/

function fun_Captcha(p)
{
	var t = new Date().valueOf();
	var html = '';
	for(var i=0;i<p;i++)
	{
		html += '<td><img src="/captcha?idx='+i+'&t='+t+'"/></td>';
	}
	$('#tr_Captcha').html(html);
	$('#tb_Captcha').css('width',p*20)
}
function FinishAuth(usr,uid,rb)
{
	$.cookie('usr', usr,{'expires': 7});
	$.cookie('seq', uid,{'expires': 7});
	$.cookie('remember', rb,{'expires': 7});
	document.location.href = '/?t='+new Date().valueOf();
}
function fun_Login()
{
	var error = false;
	$('input[class=textfield][type!=password]').each(function(){
											  this.value=this.value.replace(/(^ +| +$)/,'');
											  if(this.value=='')error=true;
											  });
	if(error)
	{
		alert('Please finish all fields');
		return;
	}
	if (!(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/).test($('#ipt_usr').val()))
	{
		alert('Apple id must be a email adress');
	}
	else
	{  
		$.ajax({type:"post",url:"/captcha",
		 data:{'password':$('#ipt_pwd1').val(),
				'user':$('#ipt_usr').val(),
				'remember':$('#ipt_remember').attr('checked')?'1':'',
				'captcha':$('#ipt_captcha').val()
			},dataType:"json",success:function(data)
			{
				if(data.error == false)
				{
					FinishAuth(data.value[0],data.value[1],data.value[2]);
				}
				else if(data.error != undefined)
				{
					alert(data.error);
				}
				else
				{
					alert('Bad request');
				}
			}});
	}
	
}


function fun_Register()
{
	var error = false;
	$('input[class=textfield][type!=password]').each(function(){
											  this.value=this.value.replace(/(^ +| +$)/,'');
											  if(this.value=='')error=true;
											  });
	if(error)
	{
		alert('Please finish all fields');
		return;
	}
	if (!(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/).test($('#ipt_usr').val()))
	{
		alert('Apple id must be a email adress');
	}
	else if(!(/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/).test($('#ipt_email').val()))
	{
		alert('Email is wrong format');
	}
	else if($('#ipt_pwd').val()!=$('#ipt_pwd_1').val()||$('#ipt_pwd1').val()!=$('#ipt_pwd1_1').val())
	{
		alert('Password doesn\'t match');
	}
	else if($('#ipt_pwd1').val().length < 8)
	{
		alert('Password is at least 8 chars');
	}
	else
	{  
		$.ajax({type:'post',url:"/account1",
		data:{'password':$('#ipt_pwd').val(),
				'user':$('#ipt_usr').val(),
				'type':$('#ipt_type').val(),
				'oldpassword1':$('#ipt_oldpwd1').val(),
				'password1':$('#ipt_pwd1').val(),
				'captcha':$('#ipt_captcha').val(),
				'email':$('#ipt_email').val()
			},dataType:"json",success:function(data)
			{
				if(data.error == false)
				{
					alert(data.msg);
					document.location.href = '/?t='+new Date().valueOf();
				}
				else if(data.error != undefined)
				{
					alert(data.error);
				}
				else
				{
					alert('Bad request');
				}
			}});
	}
	
}