/**
 * @author carlostristacci
 */
window.onload = function(){
	/**
	 * Preloader das imagens
	 */
	if (document.images) {
		var img1 	= new Image;
		var img2 	= new Image;
		var img3 	= new Image;
		var img4 	= new Image;
		var img5 	= new Image;
		var img6 	= new Image;
		var img7 	= new Image;
		var img8 	= new Image;
		var img1esq = new Image;
		var img1dir = new Image;
		var img2esq	= new Image;
		var img2dir	= new Image;
		var imginf	= new Image;
		var imglat	= new Image;
		var imgtra	= new Image;
		
		img1.src 	= "img/th_espeto_cabo_osso.jpg";
		img2.src 	= "img/th_garfo_rotativo_duplo.jpg";
		img3.src 	= "img/th_garfo_rotativo.jpg";
		img4.src 	= "img/th_espeto_cabo_aluminio.jpg";
		img5.src 	= "img/th_grelha_rotativa_movel.jpg";
		img6.src 	= "img/th_grelha_rotativa.jpg";
		img7.src 	= "img/th_espeto_cabo_madeira.jpg";
		img8.src 	= "img/th_suporte_para_espeto.jpg";
		img1esq.src	= "img/th_churrasqueira_1gal_grill_esq.jpg";
		img1dir.src	= "img/th_churrasqueira_1gal_grill_dir.jpg";
		img2esq.src	= "img/th_churrasqueira_2gal_grill_esq.jpg";
		img2dir.src	= "img/th_churrasqueira_2gal_grill_dir.jpg";
		imginf.src 	= "img/th_churrasqueira_grande_porte_inf.jpg";
		imglat.src 	= "img/th_churrasqueira_grande_porte_lat.jpg";
		imgtra.src 	= "img/th_churrasqueira_grande_porte_tra.jpg";
	}
	
	// FUNCOES
	/**
	 * Retorna o elemento do id especificado
	 * @param {Object} id
	 */
	function d(id){
		return document.getElementById(id);
	}
	/**
	 * Altera a imagem do acessorio pela imagem informada
	 * @param {Object} img
	 */
	function changeTarget(img){
		if (document.images) {
			d('target').src = img.src;
		}
	}
	/**
	 * Altera a imagem da orientacao pela imagem informada
	 * @param {Object} img
	 */
	function changeOrientation(img){
		if (document.images) {
			d('orientation').src = img.src;
		}
	}
	/**
	 * Verifica se o campo está vazio.
	 * @param {Object} id
	 * @param {Object} name
	 */
	function isEmpty(id, name){
		str = d(id).value;
		if(str == null || str.length == 0){
			alert('Preencha o campo '+name+'!');
			d(id).focus();
			return false;
		}else{
			return true;
		}
	}
	/**
	 * Verifica se o e-mail é válido
	 * @param {Object} id
	 */
	function isMail(id){
		str = d(id).value;
		p1 = str.indexOf('@');
		p2 = str.indexOf('.');
		leng = str.length;
		if(!(p1 >= 2 && p2 >= 5 && leng >= 9)){
			alert('Verifique o campo e-mail!');
			d(id).focus();
			return false;
		}else{
			return true;
		}
	}
	/**
	 * Verifica se um dos campos radio do grupo está selecionado
	 * @param {Object} radio - atributo name
	 * @param {Object} msg - mensagem de saída
	 */
	function isRadio(radio, msg){
		radio = document.getElementsByName(radio);
		for (var i = 0; i < radio.length; i++){
			if(radio[i].checked){
				return true;
			}
		}
		alert(msg);
		return false;
		
	}
	/**
	 * Verifica se o valor do campo é um número
	 * @param {Object} id
	 * @param {Object} nome
	 */
	function isNumber(id, nome){
		if(isNaN(d(id).value)){
			alert("Digite apenas números no campo "+nome);
			d(id).value = "";
			d(id).focus();
			return false;
		}else{
			return true;
		}
	}
	/**
	 * Insere os arquivos swf no html
	 * @param {Object} id
	 * @param {Object} swf
	 * @param {Object} w
	 * @param {Object} h
	 */
	function flash(id, swf, w, h){
		if(document.getElementById(id)){
			var so = new SWFObject( "img/"+swf, id, w, h, "9", "#FFF");
			so.addParam("wmode", "transparent");
			so.write(id);
		}	
	}
	
	// CHAMADAS PARA FUNCOES
//	flash('anima', 'topo.swf', 758, 257);
	
	// EVENTOS
	if (d('onImg1')) { d('onImg1').onclick = function(){ changeTarget(img1); } }
	if (d('onImg2')) { d('onImg2').onclick = function(){ changeTarget(img2); } }
	if (d('onImg3')) { d('onImg3').onclick = function(){ changeTarget(img3); } }
	if (d('onImg4')) { d('onImg4').onclick = function(){ changeTarget(img4); } }
	if (d('onImg5')) { d('onImg5').onclick = function(){ changeTarget(img5); } }
	if (d('onImg6')) { d('onImg6').onclick = function(){ changeTarget(img6); } }
	if (d('onImg7')) { d('onImg7').onclick = function(){ changeTarget(img7); } }
	if (d('onImg8')) { d('onImg8').onclick = function(){ changeTarget(img8); } }
	if (d('onImg9')) { d('onImg9').onclick = function(){ changeOrientation(img1esq); } }
	if (d('onImg10')) { d('onImg10').onclick = function(){ changeOrientation(img1dir); } }
	if (d('onImg11')) { d('onImg11').onclick = function(){ changeOrientation(img2esq); } }
	if (d('onImg12')) { d('onImg12').onclick = function(){ changeOrientation(img2dir); } }
	if (d('onImg13')) { d('onImg13').onclick = function(){ changeOrientation(imginf); } }
	if (d('onImg14')) { d('onImg14').onclick = function(){ changeOrientation(imglat); } }
	if (d('onImg15')) { d('onImg15').onclick = function(){ changeOrientation(imgtra); } }
	
	/*Validação Orçamentos*/
	if (d('orcamento_grill')) {
		d('orcamento_grill').onsubmit = function(){
			if(
				isRadio('orientacao', 'Escolha a posição do motor que melhor se adapta ao seu projeto') &&
				isEmpty('nome', 'Nome Completo') &&
				isEmpty('mail', 'E-mail') &&
				isMail('mail') &&
				isEmpty('fone', 'Tefefone') &&
				isEmpty('endereco', 'Endereço') &&
				isEmpty('comentario', 'Comentários / Dúvidas')
			){
				return true;
			}else{
				return false;
			}
		}
	}
	
	/*Validação Orçamentos*/
	if (d('orcamento_1cgrill')) {
		d('orcamento_1cgrill').onsubmit = function(){
			if(
				isEmpty('largura', 'Largura') &&
				isNumber('largura', 'Largura') &&
				isEmpty('profundidade', 'Profundidade') &&
				isNumber('profundidade', 'Profundidade') &&
				isRadio('orientacao', 'Escolha a posição do motor que melhor se adapta ao seu projeto') &&
				isRadio('complemento', 'Marque uma das opções de \"Deseja complementar sua churrasqueira com o Grill Felesa?\"') &&
				isEmpty('nome', 'Nome Completo') &&
				isEmpty('mail', 'E-mail') &&
				isMail('mail') &&
				isEmpty('fone', 'Tefefone') &&
				isEmpty('endereco', 'Endereço') &&
				isEmpty('comentario', 'Comentários / Dúvidas')
			){
				return true;
			}else{
				return false;
			}
		}
	}
	
	/*Validação Orçamentos*/
	if (d('orcamento_1sgrill')) {
		d('orcamento_1sgrill').onsubmit = function(){
			if(
				isEmpty('largura', 'Largura') &&
				isNumber('largura', 'Largura') &&
				isEmpty('profundidade', 'Profundidade') &&
				isNumber('profundidade', 'Profundidade') &&
				isEmpty('nome', 'Nome Completo') &&
				isEmpty('mail', 'E-mail') &&
				isMail('mail') &&
				isEmpty('fone', 'Tefefone') &&
				isEmpty('endereco', 'Endereço') &&
				isEmpty('comentario', 'Comentários / Dúvidas')
			){
				return true;
			}else{
				return false;
			}
		}
	}

	/*Validação Orçamentos*/
	if (d('orcamento_2cgrill')) {
		d('orcamento_2cgrill').onsubmit = function(){
			if(
				isEmpty('largura', 'Largura') &&
				isNumber('largura', 'Largura') &&
				isEmpty('profundidade', 'Profundidade') &&
				isNumber('profundidade', 'Profundidade') &&
				isRadio('orientacao', 'Escolha a posição do motor que melhor se adapta ao seu projeto') &&
				isRadio('complemento', 'Marque uma das opções de \"Deseja complementar sua churrasqueira com o Grill Felesa?\"') &&
				isEmpty('nome', 'Nome Completo') &&
				isEmpty('mail', 'E-mail') &&
				isMail('mail') &&
				isEmpty('fone', 'Tefefone') &&
				isEmpty('endereco', 'Endereço') &&
				isEmpty('comentario', 'Comentários / Dúvidas')
			){
				return true;
			}else{
				return false;
			}
		}
	}

	/*Validação Orçamentos*/
	if (d('orcamento_2sgrill')) {
		d('orcamento_2sgrill').onsubmit = function(){
			if(
				isEmpty('largura', 'Largura') &&
				isNumber('largura', 'Largura') &&
				isEmpty('profundidade', 'Profundidade') &&
				isNumber('profundidade', 'Profundidade') &&
				isEmpty('nome', 'Nome Completo') &&
				isEmpty('mail', 'E-mail') &&
				isMail('mail') &&
				isEmpty('fone', 'Tefefone') &&
				isEmpty('endereco', 'Endereço') &&
				isEmpty('comentario', 'Comentários / Dúvidas')
			){
				return true;
			}else{
				return false;
			}
		}
	}

	/*Validação Orçamentos*/
	if (d('orcamento_acessorios')) {
		d('orcamento_acessorios').onsubmit = function(){
			if(
				isEmpty('nome', 'Nome Completo') &&
				isEmpty('mail', 'E-mail') &&
				isMail('mail') &&
				isEmpty('fone', 'Tefefone') &&
				isEmpty('endereco', 'Endereço') &&
				isEmpty('comentario', 'Comentários / Dúvidas')
			){
				return true;
			}else{
				return false;
			}
		}
	}

	/*Validação Orçamentos*/
	if (d('orcamento_grande_porte')) {
		d('orcamento_grande_porte').onsubmit = function(){
			if(
				isEmpty('largura', 'Largura') &&
				isNumber('largura', 'Largura') &&
				isEmpty('profundidade', 'Profundidade') &&
				isNumber('profundidade', 'Profundidade') &&
				isRadio('orientacao', 'Escolha a posição do motor que melhor se adapta ao seu projeto') &&
				isEmpty('nome', 'Nome Completo') &&
				isEmpty('mail', 'E-mail') &&
				isMail('mail') &&
				isEmpty('fone', 'Tefefone') &&
				isEmpty('endereco', 'Endereço') &&
				isEmpty('comentario', 'Comentários / Dúvidas')
			){
				return true;
			}else{
				return false;
			}
		}
	}

	/*Validação Contato*/
	if (d('formContact')) {
		d('formContact').onsubmit = function(){
			if(isEmpty('nome', 'Nome') && isEmpty('mail', 'E-mail') && isMail('mail') && isEmpty('msg', 'Mensagem')){
				return true;
			}else{
				return false;
			}
		}
	}
}