/* Casa Juridica 
   Autor: Junior Gobira
   */

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}

function checkMail(mail) {
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);

    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    } else if (typeof(mail) == "object") {
        if(er.test(mail.value)) { 
            return true; 
        }
    }else {
        return false;
    }   
}

function checarCadastro(theForm) {
    
    var Aviso = "Casa Jurídica                   \n\nAviso\n\n";
    
    if (trim(theForm.nome.value) == "" ) {
            alert(Aviso+"Informe o Nome.         ");
            theForm.nome.focus();
            return (false);
    }

    if (!checkMail(trim(theForm.email.value))) {
        alert(Aviso+"Informe o Email corretamente.         ");
        theForm.email.focus();
        return (false);
    }

    if (theForm.ajax_cidades.value == 0 ) {
            alert(Aviso+"Informe a sua Cidade.         ");
            theForm.ajax_cidades.focus();
            return (false);
    }
    
    if (trim(theForm.nascimento.value) == "" ) {
            alert(Aviso+"Informe a sua Data de Nascimento.         ");
            theForm.nascimento.focus();
            return (false);
    }

    theForm.submit;
}


function valid_date(l,dd,mm,yy)
{
	z='err';
	if (l == 6 || l == 8)
	{
		xx=yy;
		if (dd >= 1 && dd <= 31)
		{
			if (mm == 2 || mm == 4 || mm == 6 || mm == 9 || mm == 11)
			{
				if (dd <= 30)
				{
					if (mm == 2)
					{
						if (dd <= 28)
						{
							z='ok';
						}
						else
						{
							if (dd == 29)
							{
								bb=xx%4;
								if (bb == 0)
								{
									cc=xx%100;
									if (cc == 0)
									{
										qq=xx%400;
										if (qq == 0)
										{
											z='ok';
										};
									}
									else
									{
										z='ok';
									};
								};
							};
						};
					}
					else
					{
						z='ok';
					};
				};
			}
			else
			{
				if (mm >= 1 && mm <= 12) { z='ok'; };
			};
		};
	};
	ii=200;	
	if (xx < 1600 || xx > 2400 || xx < 2005 - ii || xx > 2005 + ii) { z='err'; };
	return z;
};

function consist_date(dat)
{
	n='0123456789';
	d=dat.value;
	l=d.length;
	s='';
	for (i=0; i<l; i++)
	{
		c=d.charAt(i);
		if (n.indexOf(c) >= 0)
		{
			s+=c;
		};
	};
	l=s.length;
	xx='0000';
	r=s;
	if (l > 8)
	{
		r=s.substr(0,8);
		s=r;
		l=8;
	};
	if (l == 6)
	{
		dd=s.substr(0,2);
		mm=s.substr(2,2);
		yy=s.substr(4,2);
		if (yy < 50)
		{
			xx='20'+yy;
		}
		else
		{
			xx='19'+yy;
		};
		ww=dd+'/'+mm+'/'+yy;
	};
	if (l == 8)
	{
		dd=s.substr(0,2);
		mm=s.substr(2,2);
		yy=s.substr(4,4);
		xx=yy;
		ww=dd+'/'+mm+'/'+yy;
	};
	if (l == 6 || l == 8)
	{
		z=valid_date(l,dd,mm,xx);
		if (z == 'ok') { r=ww; };
	};
	dat.value=r;
	return 'ok';
};

function update_date(dat)
{
	n='0123456789';
	d=dat.value;
	l=d.length;
	s='';
	for (i=0; i<l; i++)
	{
		c=d.charAt(i);
		if (n.indexOf(c) >= 0)
		{
			s+=c;
		};
	};
	l=s.length;
	if (l == 6)
	{
		dd=s.substr(0,2);
		mm=s.substr(2,2);
		yy=s.substr(4,2);
		if (yy < 50)
		{
			yy='20'+yy;
		}
		else
		{
			yy='19'+yy;
		};
		ww=dd+'/'+mm+'/'+yy;
	};
	if (l == 8)
	{
		dd=s.substr(0,2);
		mm=s.substr(2,2);
		yy=s.substr(4,4);
		ww=dd+'/'+mm+'/'+yy;
	};
	rr='';
	if (l == 6 || l == 8)
	{
		z=valid_date(l,dd,mm,yy);
		if (z == 'ok')
		{
			rr=ww;
		};
	};
	dat.value=rr;
	return 'ok';
};
