// JavaScript Document

/* CONSTANTES */
	var shorten_url_youtube = 'http://youtu.be/[#yt_id#]';
	var largen_url_youtube	= 'http://www.youtube.com/watch?v=[#yt_id#]';
	var embed_url_youtube	= 'http://www.youtube.com/embed/[#yt_id#]';
	var iframe_youtube		= '<iframe width="[#yt_w#]" height="[#yt_h#]" src="'+ embed_url_youtube +'" frameborder="0" allowfullscreen></iframe>';
	var iframe_youtube_w	= 420;
	var iframe_youtube_h	= 315;
	var image_url_youtube	= 'http://i4.ytimg.com/vi/[#yt_id#]/0.jpg';
/* CONSTANTES */

/* FUNÇÕES GERAIS */
	function paginacao( _url , _pag , _start , _querystring ) {
		var _pars = 'pag=' + _pag + '&start=' + _start + '' + _querystring;
		self.location = _url + '?' + _pars;
	}
/* FUNÇÕES GERAIS */
	
/* FUNÇÕES ADMIN */
	function mostraEmbed( valor , id_campo , id_hidden , id_video , id_imagem ) {
		var url_youtube = '';
		var id_youtube	= '';
		var isEmbed		= false;
		
		if( valor.indexOf( 'iframe' ) >= 0 ) {
			isEmbed		= true;
			tipo		= 'iframe';
		}
		
		if( valor.indexOf( shorten_url_youtube.replace( '[#yt_id#]' , '' ) ) >= 0 ) {
			url_youtube = shorten_url_youtube.replace( '[#yt_id#]' , '' );
			isEmbed		= true;
			tipo		= 'url';
		}
		
		if( valor.indexOf( largen_url_youtube.replace( '[#yt_id#]' , '' ) ) >= 0 ) {
			url_youtube = largen_url_youtube.replace( '[#yt_id#]' , '' );
			isEmbed		= true;
			tipo		= 'url';
		}
		
		if ( isEmbed == true ) {
			if ( tipo == 'iframe' ) {
				var $valor = $( valor );
				id_youtube = $valor.attr( 'src' ).replace( embed_url_youtube.replace( '[#yt_id#]' , '' ) , '' );
			} else {
				id_youtube = valor.replace( url_youtube , '' );
			}

			if ( id_youtube.indexOf( '&' ) >= 0 ) {
				id_youtube = id_youtube.substring( 0 , id_youtube.indexOf( '&' ) );
			}
			
			var iframe_pronto = iframe_youtube.replace( '[#yt_id#]' , id_youtube ).replace( '[#yt_w#]' , iframe_youtube_w ).replace( '[#yt_h#]' , iframe_youtube_h );
			var imagem_pronto = image_url_youtube.replace( '[#yt_id#]' , id_youtube ).replace( '0.jpg' , 'default.jpg' ) + '" border="0" />';

			$( '#' + id_video ).html( '<strong>Vídeo</strong><br />' + iframe_pronto );
			$( '#' + id_imagem ).html( '<strong>Thumbnail</strong><br /><img src="' + imagem_pronto );
			$( '#' + id_campo ).val( iframe_pronto );
			$( '#' + id_hidden ).val( iframe_pronto );
		}
	}

	function FCKeditorIsEmpty( id ) {
		var FCKeditorInstance = FCKeditorAPI.GetInstance( id );
		if ( ( FCKeditorInstance.GetXHTML() == '' ) || ( FCKeditorInstance.GetXHTML() == '<br />' ) ) {
			return true;
		} else {
			return false;
		}
	}
	
	function sortIt( id ) {
		var $r = $( '#' + id + ' option' );
		$r.sort( function ( a, b ) {
			if ( a.text < b.text ) return -1;
			if ( a.text == b.text ) return 0;
			return 1;
		} );
		$( $r ).remove();
		$( '#' + id ).append( $( $r ) );
	}	
	
	function crossSelect( pFrom , pTo ) {
		$( '#'+pFrom ).children( 'option' ).each( function() {
			var $this = $( this );

			if ( ( $this.attr( 'selected' ) == true ) || ( $this.attr( 'selected' ) == 'selected' ) ) {

				$this.text( $this.text().trim() );
				$this.attr( 'title' , $this.attr( 'title' ).trim() );
				
				$( '#'+pTo ).append( $this );
			}
		} );
		
		$( '#'+pFrom ).html(
			$( '#'+pFrom+' option').sort( function( a , b ) {
				if  ( $( a ).text().trim() > $( b ).text().trim() ) { return 1 }
				else if  ( $( a ).text().trim() < $( b ).text().trim() ) { return -1 }
				else { return 0 }
			} )
		);
		
		$( '#'+pTo ).html(
			$( '#'+pTo+' option').sort( function( a , b ) {
				if  ( $( a ).text().trim() > $( b ).text().trim() ) { return 1 }
				else if  ( $( a ).text().trim() < $( b ).text().trim() ) { return -1 }
				else { return 0 }
			} )
		);
	}

	function transfereValor( pOrigem , pDestino ) {
		var aOrigem = new Array();
		
		$( '#'+pOrigem ).children( 'option' ).each( function() {
			var $this = $( this ).val();
			aOrigem.push( $this );
		} );

		$( '#'+pDestino ).val( aOrigem.join( ',' ) );
	}
/* FUNÇÕES ADMIN */

/* FUNÇÕES SITE */

// Contador Regressivo
var data_hora_inicial_padrao = '';

function atualizaContador( data_hora_final , id ) {
	
	if ( data_hora_inicial_padrao == '' ) {
		data_hora_inicial_padrao = new Date();
	}
	
	var hoje	= data_hora_inicial_padrao;
	var futuro	= data_hora_final;
	
	var ss = parseInt( ( futuro - hoje ) / 1000 );
	var mn = parseInt( ss / 60 );
	var hr = parseInt( mn / 60 );
	var dd = parseInt( hr / 24 );

	ss = ss - ( mn * 60 );
	mn = mn - ( hr * 60 );
	hr = hr - ( dd * 24 );
	hr += ( dd * 24 );
	
	if ( dd + hr + mn + ss > 0 ) {
	
		data_hora_inicial_padrao = new Date( data_hora_inicial_padrao.getTime() + 1000 );

		hr = hr.toString();
		mn = mn.toString();
		ss = ss.toString();
		
		if ( hr.length < 2 ) { hr = '0' + hr };
		if ( mn.length < 2 ) { mn = '0' + mn };
		if ( ss.length < 2 ) { ss = '0' + ss };		
		
		try { getId( id[0] ).innerHTML = ss + 's' } catch(e) {};
		try { getId( id[1] ).innerHTML = mn + 'm' } catch(e) {};
		try { getId( id[2] ).innerHTML = hr + 'h' } catch(e) {};
	}
}

function iniciaContador( data_inicial , hora_inicial , data_final , hora_final ) {
	
	var id_horario = [ 'segundo' , 'minuto' , 'hora' ];
	
	var _data_inicial = data_inicial.split( '/' )
	var _hora_inicial = hora_inicial.split( ':' );
	
	var YYi = _data_inicial[2];
	var MMi = _data_inicial[1];
	var DDi = _data_inicial[0];

	var HRi = _hora_inicial[0];
	var MNi = _hora_inicial[1];
	var SGi = _hora_inicial[2];
	
	var _data_final = data_final.split( '/' )
	var _hora_final = hora_final.split( ':' );
	
	var YYf = _data_final[2];
	var MMf = _data_final[1];
	var DDf = _data_final[0];
	
	var HRf = _hora_final[0];
	var MNf = _hora_final[1];
	var SGf = _hora_final[2];
	
	var data_hora_inicial	= new Date( YYi , MMi , DDi , HRi , MNi , SGi );
	var data_hora_final		= new Date( YYf , MMf , DDf , HRf , MNf , SGf );
	
	data_hora_inicial_padrao = data_hora_inicial;
	
	setInterval( function(){
		atualizaContador( data_hora_final , id_horario );
	} , 1000 );
}

// Contador Regressivo

// Header ------------------------------------------------------------------------------------------------------

	function selecionaSubcat( codigo ) {
		$( '#div_subcat' ).html( $( '#li_select_' + codigo ).html() );
		$( '#div_subcat' ).attr( 'title' , $( '#li_select_' + codigo ).html() );
		$( '#subcat' ).val( codigo );
		
		$( '.subitem' ).attr( 'class' , 'subitem' );
		
		if ( codigo != '0' ) {
			$( '#li_select_' + codigo ).attr( 'class' , 'subitem hover' );
		}
	}

	function buscaLista( palavra_chave , subcat ) {
		var pars	= '';
		
		if ( palavra_chave != '' ) { pars += '&palavra_chave=' + palavra_chave; }
		if ( subcat != '' ) { pars += '&subcat=' + subcat; }
		
		pars = pars.substring( 1 , pars.length );

		if ( pars != '' ) {
			self.location = url_busca + '?' + pars;
		}
	}

// Header ------------------------------------------------------------------------------------------------------

// Carrinho ------------------------------------------------------------------------------------------------------

	function fecharPedido( f , pagina ) {
		var url = url_loja;
		
		getId( f ).action = url + pagina;
		getId( f ).submit();
	}

	function alteraItem( codigo , tipo ) {
		
		var cor 	= '';
		var tamanho	= '';
		
		if ( tipo == 'add' ) {
			if ( ( !checou( document.getElementsByName( 'cor' ) ) ) || ( !checou( document.getElementsByName( 'tamanho' ) ) ) ) {
				alert( 'Por favor, selecione a cor e o tamanho do produto.' );
				return false;
			}

			cor 	= retornaChecados(  document.getElementsByName( 'cor' ) );
			tamanho	= retornaChecados(  document.getElementsByName( 'tamanho' ) );
			
			codigo += '_' + cor + '_' + tamanho;
		}
		
		self.location = url_loja + 'carrinho-adiciona.asp?codigo='+ codigo +'&tipo='+ tipo;
	}
	
	function entraSSL( f ) {
		f.action = url_ssl_loja + 'carrinho-passo-3-redireciona.asp';
		f.submit();
	}

	function saiSSL( f ) {
		f.action = url_loja + 'carrinho-final.asp';
		f.submit();
	}
	
	function imprimirBoleto( f ) {
		f.action = url_boleto_itau;
		f.submit();
	}
	
	function fechaPedidoFinal( f ) {
		if( !checou( f.cd_forma_pagamento ) ) {
			alert( "A forma de pagamento não foi selecionada." );
			f.cd_forma_pagamento[0].focus();
			return false;
		}
		
		f.action = 'carrinho-passo-3-envia.asp';		
		enviaDadosGeral( f );
	}
	
	function limpaDadosCartao() {
		$( '.tr_forma_pagamento' ).html( '' );
	}

// Carrinho ------------------------------------------------------------------------------------------------------

// Cadastro ------------------------------------------------------------------------------------------------------

	function enviaDadosCadastro( f ) {
		if( !checou( f.ic_tipo_cliente ) ) {
			alert( 'Selecione o tipo de cadastro (Pessoa Física ou Pessoa Jurídica).' );
			f.input_perfil[0].focus();
			return false;
		}
		
		if( !validaEmail( f.ds_email_cliente.value ) ) {
			alert( 'O campo E-mail não foi preenchido corretamente. (E-mail inválido)' );
			f.ds_email_cliente.focus();
			return false;
		}
		
		if ( f.ds_senha_cliente.length < 6 ) {
			alert( 'O campo Senha deve ter no mínimo 6 caracteres.' );
			f.ds_senha_cliente.focus();
			return false;
		}
		
		if ( f.ds_confirma_senha_cliente.value != f.ds_senha_cliente.value ) {
			alert( 'A confirmação da senha deve ser igual a senha escolhida.' );
			f.ds_confirma_senha_cliente.focus();
			return false;
		}
		
		if( f.ic_tipo_cliente[0].checked == true ) {
		
			if( !checou( f.ic_sexo_cliente ) ) {
				alert( 'O campo Sexo não foi selecionado.' );
				f.ic_sexo_cliente[0].focus();
				return false;
			}
			f.cd_cnpj_cliente.alt = '';
			f.cd_ie_cliente.alt = '';
			f.nm_fantasia_cliente.alt = '';
			f.cd_telefone1_cliente2.alt = '';
			f.nm_contato_cliente.alt = '';
			f.ds_email_contato_cliente.alt = '';
			f.cd_telefone_contato_cliente.alt = '';
		} else {
			if( !checou( f.ic_sexo_contato_cliente ) ) {
				alert( 'O campo Sexo do Contato não foi selecionado.' );
				f.ic_sexo_contato_cliente[0].focus();
				return false;
			}						
			f.cd_cpf_cliente.alt			= '';
			f.cd_telefone1_cliente.alt	= '';
		}
				
		enviaDadosGeral( f );
	}
	
	function alteraPerfil( tipo ) {
		var title = '';
		
		if ( tipo == 'cnpj' ) {
			$( '#cpf' ).css( 'display' , 'none' );
			$( '#cnpj' ).css( 'display' , '' );
			title = ' - Pessoa Jurídica';
		} else {
			$( '#cpf' ).css( 'display' , '' );
			$( '#cnpj' ).css( 'display' , 'none' );
			title = ' - Pessoa Física';
		}
		
		$( '#span_perfil' ).html( title );
	}
	
	function enviaDadosSenha( f ) {
		if ( f.ds_senha_cliente.value != f.ds_confirma_senha_cliente.value ) {
			alert( 'A confirmação da senha deve ser igual a senha escolhida.' );
			f.ds_senha_cliente_confirma.focus();
			return false;
		}
		
		enviaDadosGeral( f );
	}
	
	function abreUrlTracking( url ) {
		window.open( url , 'tracking' , '' );
	}

	function mostraDetalhes( pedido ) {
		if ( $( '#pedido' + pedido ).html() == '' ) {
			
			var pagina = '';
			var _type	= 'GET';
			
			var _url 		= url_loja + 'meus-pedidos-lista-ajax.asp';
			var _pars	= 'pedido=' + pedido;
			
			var requisicaoAjax = jQuery.ajax( {
				dataType: 'html',
				type: _type,
				data: _pars,
				url: _url,
				success: function( _retornoAjax ) {
					var htmlRetorno	= _retornoAjax;
					
					$( '.detalhes' ).html( '' );
					
					$( '#pedido' + pedido ).html( htmlRetorno );
				}
			} )
		} else {
			$( '.detalhes' ).html( '' );
		}
	}

// Cadastro ------------------------------------------------------------------------------------------------------

// Geral ------------------------------------------------------------------------------------------------------

	function verificaCadastro( f , email_padrao , senha_padrao ) {
		
		if ( f.ds_email_cliente.value == email_padrao ) {
			alert( 'O campo E-mail não foi preenchido' );
			f.ds_email_cliente.focus();
			return false;
		}

		if ( f.ds_senha_cliente.value == senha_padrao ) {
			alert( 'O campo Senha não foi preenchido' );
			f.ds_senha_cliente.focus();
			return false;
		}
				
		f.action	= 'cliente-autentica.asp';
		enviaDadosGeral( f );
	}
	
	function criaCadastro( f ) {
		f.ds_email_cliente.alt	= '';
		f.ds_senha_cliente.alt	= '';
		f.action	= 'cadastro.asp';
		f.submit();
	}
	
// Geral ------------------------------------------------------------------------------------------------------	
	
/* FUNÇÕES SITE */
