var regMail=/^[^@]+@[^@]+.[a-z]{2,}$/i;
var regPasswd=/^[\w]{3,}$/i;
var regName=/^[^!--:-@]{3,}\s[^!--:-@]{3,}$/i;
var regAddress=/^[^!-+:-@]{6,}$/i;
var regNumber=/^[^!-+:-@]{1,}$/i;
var regBlock=/^[^!--:-@]{3,}$/i;
var regCity=/^[^!--:-@]{3,}$/i;
var regZip=/^\d{5}-\d{3}$/i;
var regDate=/^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$/i;

var errMail='E-mail vazio ou inválido!';
var errPasswd='Senha com caracteres inválidos ou menor que 3 caracteres!';
var errMatch='Senha e a confirmação da senha não são iguais!';
var errName='Nome inválido!';
var errAddress='Endereço vazio ou inválido!';
var errNumber='Número do endereço vazio ou inválido';
var errBlock='Bairro vazio ou inválido!';
var errCity='Cidade vazia ou inválida!';
var errZip='CEP inválido!';
var errBirthday='Data de nascimento vazia ou inválida!';

function sbsValidate(link,src)
{	msgError='';
	mail=document.sbsForm.email;
	passwd=document.sbsForm.password;
	passwd2=document.sbsForm.password2;
	fname=document.sbsForm.name;
	address=document.sbsForm.address;
	number=document.sbsForm.number;
	block=document.sbsForm.block;
	city=document.sbsForm.city;
	zip=document.sbsForm.zip;
	birthday=document.sbsForm.birthday;

	if(!regDate.test(birthday.value))   { msgError=errBirthday+'\n'+msgError; birthday.focus(); }
	if(!regZip.test(zip.value))         { msgError=errZip+'\n'+msgError;      zip.focus();      }
	if(!regCity.test(city.value))       { msgError=errCity+'\n'+msgError;     city.focus();     }
	if(!regBlock.test(block.value))     { msgError=errBlock+'\n'+msgError;    block.focus();    }
	if(!regNumber.test(number.value)) 	{ msgError=errNumber+'\n'+msgError;  number.focus();  }
	if(!regAddress.test(address.value)) { msgError=errAddress+'\n'+msgError;  address.focus();  }
	if(!regName.test(fname.value))      { msgError=errName+'\n'+msgError;     fname.focus();   }
	if(passwd.value!=passwd2.value)     { msgError=errMatch+'\n'+msgError;    passwd.focus();   }
	if(!regPasswd.test(passwd.value))   { msgError=errPasswd+'\n'+msgError;   passwd.focus();   }
	if(!regMail.test(mail.value))       { msgError=errMail+'\n'+msgError;     mail.focus();     }

  if(msgError.length>0)
  	alert(msgError);
	else
		dhtmlLoadScript('/_element/subscription/subscription_dhtml.php?link='+link+'&src='+src+'&opt=checkmail&mail='+mail.value,true);
}

function sbsSave(link,src)
{	passwd=encodeURIComponent(document.sbsForm.password.value);
	name=encodeURIComponent(document.sbsForm.name.value);
	address=encodeURIComponent(document.sbsForm.address.value);
	number=encodeURIComponent(document.sbsForm.number.value);
	complement=encodeURIComponent(document.sbsForm.complement.value);
	block=encodeURIComponent(document.sbsForm.block.value);
	city=encodeURIComponent(document.sbsForm.city.value);
	zip=encodeURIComponent(document.sbsForm.zip.value);
	state=encodeURIComponent(document.sbsForm.state.value);
	birthday=encodeURIComponent(document.sbsForm.birthday.value);
	graduation=encodeURIComponent(document.sbsForm.graduation.value);
	want_fdq=document.sbsForm.want_fdq.checked?'yes':'no';
	want_mc=document.sbsForm.want_mc.checked?'yes':'no';
	want_shop=document.sbsForm.want_shop.checked?'yes':'no';
	
	dhtmlInnerHtml('/_element/subscription/subscription_dhtml.php?opt=save&link='+link+'&src='+src+'&id='+document.sbsForm.id.value+'&mail='+encodeURIComponent(document.sbsForm.email.value)+'&passwd='+passwd+'&name='+name+'&address='+address+'&number='+number+'&complement='+complement+'&block='+block+'&city='+city+'&state='+state+'&zip='+zip+'&birthday='+birthday+'&graduation='+graduation+'&want_fdq='+want_fdq+'&want_mc='+want_mc+'&want_shop='+want_shop,document.getElementById('sbs_container'),true);
}

function sbsNew(link,src){dhtmlInnerHtml('/_element/subscription/subscription_dhtml.php?link='+link+'&src='+src+'&opt=new',document.getElementById('sbs_container'),true);}


function sbsCommon(link,src,opt)
{	msgError='';
	mail=document.sbsForm.email;
  passwd=document.sbsForm.passwd;
  if(!regPasswd.test(passwd.value)) {msgError=errPasswd;passwd.focus();}
  if(!regMail.test(mail.value)) {msgError=errMail+'\n'+msgError;mail.focus();}
  
  if(msgError.length>0)
  	alert(msgError);
	else
		dhtmlLoadScript('/_element/subscription/subscription_dhtml.php?link='+link+'&src='+src+'&opt='+opt+'&mail='+mail.value+'&passwd='+passwd.value,true);
}

function sbsCheck(link,src){sbsCommon(link,src,'check');}
function sbsUpdate(link,src){sbsCommon(link,src,'check_update');}
function sbsRemove(link,src){sbsCommon(link,src,'check_remove');}


function sbsSendpasswd(link,src)
{ msgError='';
  mail=document.sbsForm.emailp;
  if(!regMail.test(mail.value)) {alert(errMail);mail.focus();}
  else dhtmlLoadScript('/_element/subscription/subscription_dhtml.php?link='+link+'&src='+src+'&opt=sendpasswd&mail='+mail.value,true);
}
