// JavaScript Document

/*-------------------------------------------------------------------------*/
// PHP - Seta para o PHP o número de produtos a serem carregados de acordo
// com a resolução do monitor do usuário
/*-------------------------------------------------------------------------*/
switch(browser_resolution(1,0)){
	case 800:
		nrCourses = "6";
		break;
	case 1024:
		nrCourses = "12";
		break;
	case 1280:
		nrCourses = "15";
		break;
	default:
		nrCourses = "12";
		break;
}

/*-------------------------------------------------------------------------*/
// Documento - Configurações
/*-------------------------------------------------------------------------*/
function doc_configs(){
	var form = html_getElement("formSearch");
	form.nrCrs.value = nrCourses;
}

/*-------------------------------------------------------------------------*/
// HTML Element - Altera os campos do formulário de registro
/*-------------------------------------------------------------------------*/
function html_changeRegister(checkVlr){
	if(checkVlr==1){
		html_getElement("reg_codeName").style.display = "";
		html_getElement("title_firstName").innerHTML = "<strong>NOME</strong>";
		html_getElement("title_lastName").innerHTML = "<strong>SOBRENOME</strong>";
		html_getElement("title_cad").innerHTML = "<strong>CPF</strong>";
		html_getElement("reg_rg").style.display = "";
		html_getElement("reg_ie").style.display = "none";
		html_getElement("reg_birthday").style.display = "";
		html_getElement("reg_gender").style.display = "";
	}else{
		html_getElement("reg_codeName").style.display = "none";
		html_getElement("title_firstName").innerHTML = "<strong>RAZÃO SOCIAL</strong>";
		html_getElement("title_lastName").innerHTML = "<strong>CONTATO</strong>";
		html_getElement("title_cad").innerHTML = "<strong>CNPJ</strong>";
		html_getElement("reg_rg").style.display = "none";
		html_getElement("reg_ie").style.display = "";
		html_getElement("reg_birthday").style.display = "none";
		html_getElement("reg_gender").style.display = "none";
	}
}

/*-------------------------------------------------------------------------*/
// HTML Element - Altera a sequência de passos do pedido em OFF e ON
/*-------------------------------------------------------------------------*/
function html_stepsOnOff(nr,stepOn){
	
	html_getElement("step01").src = "imgs/"+step1OFF;
	html_getElement("step02").src = "imgs/"+step2OFF;
	html_getElement("step03").src = "imgs/"+step3OFF;
	html_getElement("step04").src = "imgs/"+step4OFF;
	
	html_getElement("step0"+nr).src = "imgs/"+stepOn;
}

/*-------------------------------------------------------------------------*/
// HTML Element - Altera a ação do formulário da cesta antes de postar
/*-------------------------------------------------------------------------*/
function html_basketForm(act){
	html_getElement("act").value = act;
	ajaxForm('steps','formBasket');
}

/*-------------------------------------------------------------------------*/
// Validation - Valida o formulário de busca / wshop
/*-------------------------------------------------------------------------*/
function val_search(formID){
	var form = html_getElement(formID);
	var srch = form.srch;
	var srchVlr = srch.value;
	if(srchVlr==""){
		alert("Campo de busca em branco!");
		srch.focus();
		return false;
	}
	if(srchVlr.length<3){
		alert("A busca deve conter mais de 2 caracteres!");
		srch.focus();
		srch.select();
		return false;
	}
	ajaxForm('content',formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Valida o formulário de login / formLogin
/*-------------------------------------------------------------------------*/
function val_login(targetID,formID){
	var form = html_getElement(formID);
	if(form.reg_email.value==""){
		alert("Informe o e-mail!");
		form.reg_email.focus();
		return false;
	}
	if(!val_email("reg_email")){
		return false;
	}
	if(form.reg_senha.value==""){
		alert("Informe a senha!");
		form.reg_senha.focus();
		return false;
	}
	if(!val_expReg("reg_senha",/^[\w]+$/)){
		alert("Caracter invalido!\nCaracter nao permitido no campo.");
		form.reg_senha.focus();
		form.reg_senha.select();
		return false;
	}
	ajaxForm(targetID,formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Valida o formulário de lembrete de senha / formLogin
/*-------------------------------------------------------------------------*/
function val_reminder(formID){
	var form = html_getElement(formID);
	if(form.rem_email.value==""){
		alert("Informe o e-mail!");
		form.rem_email.focus();
		return false;
	}
	if(!val_email("rem_email")){
		return false;
	}
	ajaxForm('content',formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Valida o formulário de indicação a amigo / formFriend
/*-------------------------------------------------------------------------*/
function val_toFriend(formID){
	var form = html_getElement(formID);
	if(form.nameFrom.value=="" || form.nameFrom.value=="Seu nome"){
		alert("Informe seu nome!");
		form.nameFrom.focus();
		return false;
	}
	if(form.mailTo.value=="" || form.mailTo.value=="E-mail do amigo"){
		alert("Informe o e-mail de seu amigo!");
		form.mailTo.focus();
		return false;
	}
	if(!val_email("mailTo")){
		return false;
	}
	ajaxForm('toFriend',formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Valida o formulário de sugestão de curso / formCourse
/*-------------------------------------------------------------------------*/
function val_newCourse(formID){
	var form = html_getElement(formID);
	if(form.mailReply.value=="" || form.mailReply.value=="Seu e-mail"){
		alert("Informe seu e-mail para retorno!");
		form.mailReply.focus();
		return false;
	}
	if(!val_email("mailReply")){
		return false;
	}
	if(form.mailCourse.value==""){
		alert("Sugira um curso!");
		form.mailCourse.focus();
		return false;
	}
	ajaxForm('newCourse',formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Valida o formulário de contato / formContact
/*-------------------------------------------------------------------------*/
function val_contact(formID){
	var form = html_getElement(formID);
	if(form.myName.value==""){
		alert("Informe seu nome!");
		form.myName.focus();
		return false;
	}
	if(form.myCity.value==""){
		alert("Informe sua cidade!");
		form.myCity.focus();
		return false;
	}
	if(form.myState.value==0){
		alert("Informe o estado!");
		form.myState.focus();
		return false;
	}
	if(form.myEmail.value==""){
		alert("Informe seu e-mail!");
		form.myEmail.focus();
		return false;
	}
	if(!val_email("myEmail")){
		return false;
	}
	if(form.myDDD.value==""){
		alert("Informe o DDD do telefone de contato!");
		form.myDDD.focus();
		return false;
	}
	if(!val_DDD("myDDD")){
		return false;
	}
	if(form.myPhone.value==""){
		alert("Informe o telefone de contato!");
		form.myPhone.focus();
		return false;
	}
	if(!val_fone("myPhone")){
		return false;
	}
	if(form.mySubject.value==""){
		alert("Informe o assunto!");
		form.mySubject.focus();
		return false;
	}
	if(form.myMenssage.value==""){
		alert("Digite uma mensagem!");
		form.myMenssage.focus();
		return false;
	}
	ajaxForm('content',formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Validação do Formulário de Cadastro (Total ou Parcial)
// formRegister, accIdentify, accContacts, accAddress, accPassword
/*-------------------------------------------------------------------------*/
function val_register(targetID,formID,mode){
	var form = html_getElement(formID);
	var type;
	
	if(mode=="register"){
		if(!form.ctm_termo.checked){
	        alert("Você deve aceitar o Termo de Adesão!");
	        return false;
	    }    
	}
	
	if(mode=="register"){
		if(form.ctm_type[0].checked){
			type = "F";
		}
		if(form.ctm_type[1].checked){
			type = "J";
		}
	}else{
		if(form.ctm_type.value==1){
			type = "F";
		}
		if(form.ctm_type.value==2){
			type = "J";
		}
	}
	
	if(mode=="register" || mode=="identify"){
		if(form.ctm_firstName.value==""){
			if(type=="F"){
				alert("Informe seu nome!");
			}else{
				alert("Informe a razao social da empresa!");
			}
			form.ctm_firstName.focus();
			return false;
		}
		if(form.ctm_lastName.value==""){
			if(type=="F"){
				alert("Informe seu sobrenome!");
			}else{
				alert("Informe o nome do contato na empresa!");
			}
			form.ctm_lastName.focus();
			return false;
		}
		if(form.ctm_cad.value==""){
			if(type=="F"){
				alert("Informe seu CPF!");
			}else{
				alert("Informe o CNPJ da empresa!");
			}
			form.ctm_cad.focus();
			return false;
		}
		if(!val_expReg("ctm_cad",/^[./\-\d]+$/)){
			if(type=="F"){
				alert("CPF invalido! Digite somente numeros.");
			}else{
				alert("CNPJ invalido! Digite somente numeros.");
			}
			form.ctm_cad.focus();
			form.ctm_cad.select();
			return false;
		}
		if(type=="F"){
			if(!val_CPF("ctm_cad")){
				return false;
			}
		}else{
			if(!val_CNPJ("ctm_cad")){
				return false;
			}
		}
		if(type=="F"){
			if(form.ctm_rg.value==""){
				alert("Informe seu RG!");
				form.ctm_rg.focus();
				return false;
			}
		}
		if(type=="J"){
			if(form.ctm_ie.value=="" && !form.ie_isento.checked){
				alert("Informe sua Inscricao Estadual!");
				form.ctm_ie.focus();
				return false;
			}
		}
	}
	
	if(mode=="register" || mode=="address"){
		if(form.add_logradouro.value==""){
			alert("Informe o logradouro do endereco!");
			form.add_logradouro.focus();
			return false;
		}
		if(form.add_nr.value==""){
			alert("Informe o numero do endereco!");
			form.add_nr.focus();
			return false;
		}
		if(!val_expReg("add_nr",/^[\d]+$/)){
			alert("Somente números!");
			form.add_nr.focus();
			form.add_nr.select();
			return false;
		}
		if(form.add_bairro.value==""){
			alert("Informe o bairro do endereco!");
			form.add_bairro.focus();
			return false;
		}
		if(form.add_cep.value==""){
			alert("Informe o CEP do endereco!");
			form.add_cep.focus();
			return false;
		}
		if(!val_CEP("add_cep")){
			return false;
		}
		if(form.add_cidade.value==""){
			alert("Informe a cidade do endereco!");
			form.add_cidade.focus();
			return false;
		}
		if(form.add_uf.value==0){
			alert("Informe o estado do endereco!");
			form.add_uf.focus();
			return false;
		}
	
	}
	
	if(mode=="register" || mode=="contacts"){
		if(form.ctm_email.value==""){
			alert("Informe o e-mail de contato!");
			form.ctm_email.focus();
			return false;
		}
		if(!val_email("ctm_email")){
			return false;
		}
		if(form.ph_ddd.value==""){
			alert("Informe o DDD do(s) telefone(s) de contato!");
			form.ph_ddd.focus();
			return false;
		}
		if(!val_DDD("ph_ddd")){
			return false;
		}
		if(form.ph_phone.value==""){
			alert("Informe o telefone de contato!");
			form.ph_phone.focus();
			return false;
		}
		if(!val_fone("ph_phone")){
			return false;
		}
		if(form.ph_fax.value!=""){
			if(!val_fone("ph_fax")){
				return false;
			}
		}
	}
	
	if(mode=="register" || mode=="password"){
		if(mode=="password"){
			if(form.senha.value==""){
				alert("Informe a senha atual!");
				form.senha.focus();
				return false;
			}
		}
		if(form.ctm_senha.value==""){
			alert("Informe uma nova senha de acesso!");
			form.ctm_senha.focus();
			return false;
		}
		if(form.ctm_senha.value.length<6 || form.ctm_senha.value.length>20){
			alert("A senha deve conter no mínimo 6 e no maximo 20 caracteres!");
			form.ctm_senha.focus();
			return false;
		}
		if(!val_expReg("ctm_senha",/^[\w]+$/)){
			alert("Caracter invalido!\nA senha nao pode conter caracteres especiais.\nDigite somente letras sem acentuacao e numeros.");
			form.ctm_senha.focus();
			form.ctm_senha.select();
			return false;
		}
		if(form.ctm_senha.value!=form.ctm_senha_.value){
			alert("As senhas nao conferem! Digite novamente.");
			form.ctm_senha.value = "";
			form.ctm_senha_.value = "";
			form.ctm_senha.focus();
			return false;
		}
	}
	
	ajaxForm(targetID,formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Validação do Formulário de Cupom
/*-------------------------------------------------------------------------*/
function val_cupom(targetID,formID){
	var form = html_getElement(formID);
	
	if(form.cupom.value==""){
		alert("Informe o numero do cupom!");
		form.cupom.focus();
		return false;
	}
	if(form.cupom.value.length!=20){
		alert("Numero do cupom invalido!");
		form.cupom.focus();
		return false;
	}
	if(form.aluno_name){
		if(form.aluno_name.value==""){
			alert("Informe seu nome!");
			form.aluno_name.focus();
			return false;
		}
	}
	if(form.aluno_email){
		if(form.aluno_email.value==""){
			alert("Informe o seu e-mail!");
			form.aluno_email.focus();
			return false;
		}
		if(!val_email("aluno_email")){
			return false;
		}
	}
	if(form.aluno_pwd){
		if(form.aluno_pwd.value==""){
			alert("Informe uma senha de acesso!");
			form.aluno_pwd.focus();
			return false;
		}
		if(form.aluno_pwd.value.length<6 || form.aluno_pwd.value.length>20){
			alert("A senha deve conter no mínimo 6 e no maximo 20 caracteres!");
			form.aluno_pwd.focus();
			return false;
		}
		if(!val_expReg("aluno_pwd",/^[\w]+$/)){
			alert("Caracter invalido!\nA senha nao pode conter caracteres especiais.\nDigite somente letras sem acentuacao e numeros.");
			form.aluno_pwd.focus();
			form.aluno_pwd.select();
			return false;
		}
		if(form.aluno_pwd.value!=form.aluno_pwd_.value){
			alert("As senhas nao conferem! Digite novamente.");
			form.aluno_pwd.value = "";
			form.aluno_pwd_.value = "";
			form.aluno_pwd.focus();
			return false;
		}
	}
	
	ajaxForm(targetID,formID);
}

/*-------------------------------------------------------------------------*/
// Validation - Valida o formulário de reenvio de mensagem de ativação
// de cadastro / formMailRegister
/*-------------------------------------------------------------------------*/
function val_mailRegister(formID){
	var form = html_getElement(formID);
	if(form.ctm_email.value==""){
		alert("Informe o e-mail!");
		form.ctm_email.focus();
		return false;
	}
	if(!val_email("ctm_email")){
		return false;
	}
	ajaxForm('content',formID);
}
/*-------------------------------------------------------------------------*/
// JANELA DE PAGAMENTO DO BANCO REAL
/*-------------------------------------------------------------------------*/
function abreJanelaMaximizada(payMethodID,idReal){
	var jan = 'includes/pagto.php?pay='+payMethodID+'&id='+idReal;
	switch(payMethodID){
		case 3: //Cartão de débito Visa Electron
			var x = window.open(jan,'loja','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,left=100,top=10,width=650,height=400');
			break;
		case 2: //Cartão de crédito Visa
			var x = window.open(jan,'loja','toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,left=100,top=10,width=650,height=400');
			break;
		case 4: //Débito em Conta
			var x = window.open(jan,'loja', 'toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,left=0,top=0,width=screen.availWidth,height=screen.availHeight');
			x.resizeTo(screen.availWidth,screen.availHeight);
			break;
	}
}
