var ajaxObject = getAjaxObject() ;
function getAjaxObject(){
	try{ return  new XMLHttpRequest(); }
	catch(e){}
	try{ return new ActiveXObject("Msxml2.XMLHTTP");}
	catch(e){}
	try { return  new ActiveXObject("Microsoft.XMLHTTP");}
	catch(e){}
}

function setEndereco(oCep,oForm){
        show_hide('cep-loading');
        ajaxObject.open('GET', 'ajax/getenderecobycep.php?cep='+oCep+'&unique='+ new Date().getSeconds() ,true);
        ajaxObject.onreadystatechange = function(){
                if ( ajaxObject.readyState == 4 && ajaxObject.status == 200 ){                  
                        temp = eval((unescape(ajaxObject.responseText.replace(/\+/g," "))));
                        if ( temp ){
                                oForm.elements.endereco.value = temp[0] ;
                                oForm.elements.bairro.value = temp[1] ;
                                oForm.elements.cidade.value = temp[2] ;
                                for ( var i = 0, o = document.getElementById('estado_id'), m = o.options.length ; i < m ; i ++ ){
                                	if ( o.options[i].text == temp[3] ){
                                		o.options[i].selected = true ;
                                		break ;
                                	}
                                }
                                //oForm.elements.uf.value = temp[3] ;
                        }
                        show_hide('cep-loading');
                }
        }
        ajaxObject.send(null);
}

function indicarSite(){

	vusuario_nome 	= document.getElementById('usuario_nome').value ; 
	vusuario_email 	= document.getElementById('usuario_email').value ; 
	vamigo_email 	= document.getElementById('amigo_email').value ;
	vamigo_nome 	= document.getElementById('amigo_nome').value ;
	
	if ( vusuario_nome == "" ){
		alert('digite seu nome') ; 
	} 
	else if ( ! vusuario_email.isEmail() ){
		alert('digite seu e-mail corretamente') ; 
	}
	else if ( vamigo_nome == "" ){
		alert('digite o nome do seu amigo')	 ;
	}
	else if ( ! vamigo_email.isEmail() ){
		alert('digite o e-mail do seu amigo corretamente')	 ;
	}
	else {
		show_hide('enviar-email-loading') ;
		ajaxObject.open('GET',	'ajax/sendindicacaosite.php' +
							'?usuario_email=' + vusuario_email + 
							'&usuario_nome=' + vusuario_nome + 
							'&amigo_email=' + vamigo_email + 
							'&amigo_nome=' + vamigo_nome +  
							'&unique='+ new Date().getSeconds() ,true);
		ajaxObject.onreadystatechange = function(){
                if ( ajaxObject.readyState == 4 && ajaxObject.status == 200 ){                  
                        temp = unescape(ajaxObject.responseText.replace(/\+/g," "));
                        if ( temp ){
							alert(temp);
							window.close() ;
                        }
						else {	
							alert('falha ao enviar email');
						}
						show_hide('enviar-email-loading') ;
                }
        }
        ajaxObject.send(null);
	}
	return false ;
}

function enviarEmail(){
		
	vitem_id		= document.getElementById('item_id').value ; 
	vusuario_nome 	= document.getElementById('usuario_nome').value ; 
	vusuario_email 	= document.getElementById('usuario_email').value ; 
	vamigo_email 	= document.getElementById('amigo_email').value ;
	vamigo_nome 	= document.getElementById('amigo_nome').value ;
	
	if ( vusuario_nome == "" ){
		alert('digite seu nome') ; 
	} 
	else if ( ! vusuario_email.isEmail() ){
		alert('digite seu e-mail corretamente') ; 
	}
	else if ( vamigo_nome == "" ){
		alert('digite o nome do seu amigo')	 ;
	}
	else if ( ! vamigo_email.isEmail() ){
		alert('digite o e-mail do seu amigo corretamente')	 ;
	}
	else {
		show_hide('enviar-email-loading') ;
		ajaxObject.open('GET',	'ajax/sendindicacaoproduto.php' +
							'?item_id=' + vitem_id + 
							'&usuario_email=' + vusuario_email + 
							'&usuario_nome=' + vusuario_nome + 
							'&amigo_email=' + vamigo_email + 
							'&amigo_nome=' + vamigo_nome +  
							'&unique='+ new Date().getSeconds() ,true);
		ajaxObject.onreadystatechange = function(){
                if ( ajaxObject.readyState == 4 && ajaxObject.status == 200 ){                  
                        temp = unescape(ajaxObject.responseText.replace(/\+/g," "));
                        if ( temp ){
							alert(temp);								
                        }
						else {	
							alert('falha ao enviar email');
						}
						show_hide('enviar-email-loading') ;
                }
        }
        ajaxObject.send(null);
	}
}
function perguntarProduto(){

	vitem_id		= document.getElementById('item_id').value ; 
	vpergunta_nome 	= document.getElementById('pergunta_nome').value ; 
	vpergunta_email	= document.getElementById('pergunta_email').value ; 
	vpergunta		= document.getElementById('pergunta').value ; 
	
	if ( vpergunta_nome == "" ){
		alert('digite seu nome') ; 
	} 
	else if ( ! vpergunta_email.isEmail() ){
		alert('digite seu e-mail corretamente') ; 
	}	
	else {
		show_hide('perguntar-produto-loading') ;
		ajaxObject.open('GET',	'ajax/sendperguntaproduto.php' +
							'?item_id=' + vitem_id + 
							'&usuario_email=' + vpergunta_email + 
							'&usuario_nome=' + vpergunta_nome + 
							'&pergunta=' + vpergunta + 
							'&unique='+ new Date().getSeconds() ,true);
		ajaxObject.onreadystatechange = function(){
                if ( ajaxObject.readyState == 4 && ajaxObject.status == 200 ){                  
                        temp = unescape(ajaxObject.responseText.replace(/\+/g," "));
                        if ( temp ){
							alert(temp);								
                        }
						else {	
							alert('falha ao enviar pergunta');
						}
						show_hide('perguntar-produto-loading') ;
                }
        }
        ajaxObject.send(null);
	}
}