// Functions in Ajax

var ajax;
var $path='http://www.costuralivre.com.br/';

function IniciaAjax(){
    var HTTP_REQUEST;

    try
    {
        HTTP_REQUEST = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            HTTP_REQUEST = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(ex)
        {
            try
            {
                HTTP_REQUEST = new XMLHttpRequest();
                HTTP_REQUEST.overrideMimeType('text/html');
            }
            catch(exc)
            {
                alert("Esse browser não tem recursos para uso do Ajax");
                HTTP_REQUEST = null;
            }
        }
    }
    return HTTP_REQUEST;
}

function sendMail(){
	ajax = IniciaAjax();
	
	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
				document.getElementById("contact_box").innerHTML="<div style='font-size:18px; font-weight:bold; text-align:center; width:380px; height:274px; line-height:274px;'><img src='http://www.zoje.com.br/img/spinner.gif' alt='Carregando...'>Enviando...</div>";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					//alert(ajax.responseText);
					if(ajax.responseText!="TRUE"){
						document.getElementById("contact_box").innerHTML = "<div style='font-size:18px; color:#CC0000; margin-top:100px; font-weight:bold; text-align:center; width:380px; height:274px;'>Falha ao enviar<br><br><br><a class='close_modal_btn' onclick='unloadModal();'>fechar</a></div>";
					}else{
						document.getElementById("contact_box").innerHTML = "<div style='font-size:18px; color:#339900; margin-top:100px; font-weight:bold; text-align:center; width:380px; height:274px;'>Enviado com sucesso<br><br><br><a class='close_modal_btn' onclick='unloadModal();'>fechar</a></div>";
					}
				} else {
					//alert(ajax.statusText);
				}
			}
		}

	//alert(document.frmContact.ipt_nome.value);

	//monta a query
	dados =	'nome='		+	document.frmContact.ipt_nome.value 	+ 
			'&ddd='		+	document.frmContact.ipt_ddd.value 	+
			'&telefone='+	document.frmContact.ipt_tel.value 	+
			'&email='	+	document.frmContact.ipt_email.value	+
			'&prodid=' 	+	document.frmContact.id_product.value;

	//alert(dados);


	//faz a requisicao e envio dos dados post
	ajax.open('POST', 'http://www.zoje.com.br/js/php/agendar.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(dados);				
	}

}



function financingSofisa($price){
ajax = IniciaAjax();

//alert($price);

	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
				document.getElementById("sofisa_text").innerHTML="<div class='loading_gallery' style='padding-top:130px; text-align:center;'><img src='http://www.zoje.com.br/img/spinner.gif' alt='Carregando...'></div>";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					//alert(ajax.statusText);
					if (ajax.responseText!="FALSE"){
						document.getElementById('sofisa_text').innerHTML=ajax.responseText;
					} else {
						alert('Falha ao consultar financiamento, tente novamente mais tarde.');
					};
				} else {
					alert(ajax.statusText);
				}
			}
		}
	
	//monta a query

	$PJ_OR_PF=0;
	$QUANTITY=1;
	
	dados="consumer_price="+$price+"&people="+$PJ_OR_PF+"&quantity="+$QUANTITY;
	//alert(dados);
	
	//faz a requisicao e envio dos dados post
	ajax.open('POST', 'http://www.zoje.com.br/js/php/sofisa.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(dados);
	}

}

function financingBNDES($price){
ajax = IniciaAjax();

//alert($price);

	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
				document.getElementById("bndes_text").innerHTML="<div class='loading_gallery' style='padding-top:130px; text-align:center'><img src='http://www.zoje.com.br/img/spinner.gif' alt='Carregando...'></div>";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					//alert(ajax.statusText);
					if (ajax.responseText!="FALSE"){
						document.getElementById('bndes_text').innerHTML=ajax.responseText;
					} else {
						alert('Falha ao consultar financiamento, tente novamente mais tarde.');
					};
				} else {
					alert(ajax.statusText);
				}
			}
		}
	
	//monta a query

	
	dados="consumer_price="+$price;
	//alert(dados);
	
	//faz a requisicao e envio dos dados post
	ajax.open('POST', 'http://www.zoje.com.br/js/php/bndes.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(dados);
	}

}

function uniqueCnpj($cnpj){
	ajax = IniciaAjax();
	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
				document.getElementById("cnpj_response").innerHTML="Validando Cnpj...";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					if(ajax.responseText=="true"){
						document.getElementById("cnpj_response").innerHTML="";
						document.getElementById("validCnpj").value="1";
					} else if(ajax.responseText=="exists"){
						document.getElementById("cnpj_response").innerHTML="Este CNPJ já foi cadastrado em nosso sistema";
						document.getElementById("validCnpj").value="0";
					} else {
						document.getElementById("cnpj_response").innerHTML="CNPJ inválido";
						document.getElementById("validCnpj").value="0";
					}
				} else {
					alert(ajax.statusText);
				}
			}
		}	
		
	//monta a query
	data='cnpj='+$cnpj;
		
	//faz a requisicao e envio dos dados post
	ajax.open('POST', $path+'media/js/php/uniqueCnpj.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(data);				
	}

}

function uniqueCpf($cpf){
	ajax = IniciaAjax();
	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
				document.getElementById("cpf_response").innerHTML="Validando CPF...";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					if(ajax.responseText=="true"){
						document.getElementById("cpf_response").innerHTML="";
						document.getElementById("validCpf").value="1";
					} else if(ajax.responseText=="exists"){
						document.getElementById("cpf_response").innerHTML="Este CPF já foi cadastrado em nosso sistema";
						document.getElementById("validCpf").value="0";
					} else {
						document.getElementById("cpf_response").innerHTML="CPF inválido";
						document.getElementById("validCpf").value="0";
					}
				} else {
					alert(ajax.statusText);
				}
			}
		}	
		
	//monta a query
	data='cpf='+$cpf;
		
	//faz a requisicao e envio dos dados post
	ajax.open('POST', $path+'media/js/php/uniqueCpf.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(data);				
	}

}

	function sendPaginator(){
		//document.getElementById('page').value=1;
		document.getElementById('frmPaginator').submit();
	}
	
function getCep($cep){
	
	ajax = IniciaAjax();
	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
				document.getElementById("cep_response").innerHTML="<img src='"+$path+"media/img/loading.gif' width='24' height='24' alt='' border=0;>&nbsp;&nbsp;Procurando CEP...";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					result=ajax.responseText;
					$array=(ajax.responseText).split('-');
					if($array[0]=="true"){
						document.getElementById("endereco").value = $array[1];						
						document.getElementById("bairro").value = $array[2];						
						document.getElementById("cidade").value = $array[3];						
						document.getElementById("estado").value = $array[4];
						document.getElementById("cep_response").innerHTML="";
					}else{
						document.getElementById("cep_response").innerHTML=$array[1];
					}

				} else {
					alert(ajax.statusText);
				}
			}
		}	
		
	//monta a query
	data='cep='+$cep;
		
	//faz a requisicao e envio dos dados post
	ajax.open('POST', $path+'carrinho/entrega/busca-cep', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(data);				
	}
}

function getFreight($cep, $div_id){
	ajax = IniciaAjax();
	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
				document.getElementById("freight_response_"+$div_id).innerHTML="Calculando Frete...";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					document.getElementById("freight_response_"+$div_id).innerHTML = '<b>Frete:</b> R$ '+ajax.responseText;						
					document.getElementById("freight_value_"+$div_id).value = ajax.responseText;						
					document.getElementById("btn_confirmar_"+$div_id).style.display='block';
				} else {
					document.getElementById("freight_response_"+$div_id).innerHTML = 'falha ao calcular o cep.';						
					alert(ajax.statusText);
				}
			}
		}	
		
	//monta a query
	data='cep='+$cep;
		
	//faz a requisicao e envio dos dados post
	ajax.open('POST', $path+'carrinho/entrega/calcula-frete', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(data);				
	}
}

function votar($id, $others, $modo, $id_answer){
	ajax = IniciaAjax();
	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
			document.getElementById("result").innerHTML="Carregando";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					document.getElementById("result").innerHTML = ajax.responseText;
				} else {
					alert(ajax.statusText);
				}
			}
		}


	//monta a query
	data='id='+$id+'&others='+$others+'&modo='+$modo+'&id_answer='+$id_answer;

	
	
	//faz a requisicao e envio dos dados post
	ajax.open('POST', $path+'media/js/php/poll/apgresu.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(data);
	}

}

function addSuggestion($suggestion, $pedido){

	ajax = IniciaAjax();
	if (ajax){
		ajax.onreadystatechange= function(){
			if(ajax.readyState==1){
			document.getElementById("result").innerHTML="";
			}
			if(ajax.readyState==4){
				if(ajax.status==200){
					document.getElementById("result").innerHTML = ajax.responseText;
				} else {
					alert(ajax.statusText);
				}
			}
		}


	//monta a query
	data='suggestion='+$suggestion+'&pedido='+$pedido;

	
	
	//faz a requisicao e envio dos dados post
	ajax.open('POST', $path+'media/js/php/addSuggestion.php', true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(data);
	}

}