// JavaScript Document
$(document).ready(function() {
	$("#form_anuncio input[type=file]").change(function() {
		id = $(this).attr("id");
		$.post("resources/images.php", { image: $(this).val() },
			function(data){
			if(data != "")
			{
			$("div[class=" + id + "]").html(data);
			$("#enviar_anuncio").attr('disabled','disabled');
			}
			else
			{
			$("div[class=" + id + "]").html('');
			$("#enviar_anuncio").removeAttr('disabled');
			}
		});
	});
	
	var option = {
		success: function(data) {
			alert('Anúncio Realizado. Direcionando para o Painel.'); 
			window.location = 'http://www.feiradoanhembi.com.br/?com_content=painel';
        }
	}
	
	$("#form_anuncio").ajaxForm(option);
	
	$("#enviar_anuncio").click(function() {
		var error = 0;	
		$("#form_anuncio").find("input[class=obrigatorio]").each(function() {
			$(this).css( "background-color","" );
			if($(this).val() == '') {
			$(this).css( "background-color","#FFDDDD" );
			error++;
			}
		});
		$("#form_anuncio").find("select[class=obrigatorio]").each(function() {
			$(this).css( "background-color","");
			if($(this).val() == '') {
			$(this).css( "background-color","#FFDDDD" );
			error++;
			}
		});
		
		
		if(error > 0) {
			alert('Todos os Campos são Obrigatórios, verifique os campos em vermelho.');	
		}
		if(error == 0)
		{
		$("#form_anuncio").ajaxSubmit(option);
		}
	});
});
