// VALIDAÇÃO DE FORM
function validaForm(object, validate_checkbox) {
    var last_name = "", atual_name = "", result_verify, atual_obj, name_field;
    
    if(typeof validate_checkbox == "undefined") {
        validate_checkbox = new Array();
    }
    
    var required = /campo_obrig/;
    
    for(x = 0; x < object.length; x++) {
        atual_obj = object[x];
        
        if((inArray(atual_obj.name, validate_checkbox) || required.test(atual_obj.className)) && (atual_obj.type == 'checkbox' || atual_obj.type == 'radio')) {
            atual_name = atual_obj.name;
            
            if(last_name != atual_name) {
                last_name = atual_name;
                result_verify = validar_checkbox(last_name);
                
                if(result_verify.length > 1) {
                    alert('O campo "' + result_verify + '" precisa ser marcado');
                    return false;
                }
            }
        }
        else if((atual_obj.value.length < 1 || atual_obj.value == "NULL") && required.test(atual_obj.className)) {
            atual_obj.focus();
            atual_obj.style.borderColor = '#FF0000';
            
            name_field = (atual_obj.title.length < 1) ? atual_obj.name : atual_obj.title;
            
            alert('O campo "' + name_field + '" precisa ser preenchido');
            return false;
        }        
    }
    
    return true;
};

function validar_checkbox(name_elm) {
    var elements = document.getElementsByName(name_elm);
    var titulo_check = "";
    
    for(y = 0; y < elements.length; y++) {        
        if(elements[y].checked == true) {
            return "";
        }
        
        if(elements[y].title.length > 1 && titulo_check.length < 1) {
            titulo_check = elements[y].title;
        }
    }
    
    return (titulo_check.length > 1) ? titulo_check : name_elm;
};

function inArray(value, Array) {
	for (var i=0; i < Array.length; i++) {
		if (Array[i] === value) {
			return true;
		}
	}
	return false;
};


//PRELOAD DE IMAGENS
var loadimages = new Array();
function preloadimg(){
    for (x=0; x < preloadimg.arguments.length; x++){
        loadimages[x] = new Image();
        loadimages[x].src = preloadimg.arguments[x];
    }
}

//MÁSCARA DE EMAIL
function sendMsg(domain, suffix, prefix) {
	location.href = "m" + "ai" + "lt" + "o:" + prefix + "@" + domain + suffix ;
}

//CARREGA FLASH
function arquivoFlash(arquivo,largura,altura,id) {
		var flash =	'<object id=\"globalnav-object\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"' +largura+ '\" height=\"' +altura+ '\" id=\"' +id+ '\" name=\"' +id+ '\">' +
					'<param name=\"movie\" value=\"' +arquivo+ '\" />' +
					'<param name=\"FlashVars\" value=\"loc=en_US&htmlApp=false&gatewayURL=gwurl\" />' +
					'<param name=\"bgcolor\" value=\"#ffffff\" />' +
					'<param name=\"menu\" value=\"false\" />' +
					'<param name=\"quality\" value=\"high\" />' +
					'<param name=\"salign\" value=\"tl\" />' +
					'<param name=\"scale\" value=\"noscale\" />' +
					'<param name=\"wmode\" value=\"transparent\" />' +
					'<embed id=\"globalnav-embed\" src=\"' +arquivo+ '\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" wmode=\"transparent\" flashvars=\"loc=en_US&htmlApp=false&gatewayURL=gwurl\" bgcolor=\"#ffffff\" menu=\"false\" quality=\"high\" salign=\"tl\" scale=\"noscale\" id=\"' +id+ '\" width=\"' +largura+ '\" height=\"' +altura+ '\"></embed>' +
					'</object>'
		document.writeln(flash);
}

//ABRE E FECHA
function closeDiv(id){
	if( document.getElementById(id).style.display == 'block' ){
		document.getElementById(id).style.display = 'none';
	} else {
		document.getElementById(id).style.display = 'block'
	}
}

//MASCARA PARA OS FORMULÁRIOS
function form_mascaras(valor, id, mascara, evento)
{
	var valorFinal = "";
	var tecla = evento.keyCode;	
	var valorOriginal = valor;
	var arrNaoPermitidos = new Array("-", ".", "/", "\\", "|", "(", ")", ":", " ");
	for(i1=0;i1<valor.length;i1++)
	{
		for(i2=0;i2<arrNaoPermitidos.length;i2++)
		{
			if(valor.charAt(i1) == arrNaoPermitidos[i2])
			{
				valor = valor.toString().replace( arrNaoPermitidos[i2], "" );
			}	
		}	
	}	
	if(tecla != 8)
	{	
	if(mascara.length >= valorOriginal.length)
		{
			jaTemMascara = false;
			for(i=0;i<valor.length;i++)
			{
				if(jaTemMascara == false)
				{
					if(mascara.charAt(i) == "#")
					{
						if(((tecla > 95) && (tecla < 106)) || ((tecla > 47) && (tecla < 58)))
						{
							valorFinal = valorFinal  + valor.charAt(i);
						}
						else
						{
							valorFinal = valorOriginal.substring(0, valorOriginal.length -1);
						}					
					}
					else if(mascara.charAt(i) == "@")
					{
						valorFinal = valorFinal  + valor.charAt(i);
					}
					else
					{
						if(mascara.charAt(i + 1) == "#")
						{
							if(((tecla > 95) && (tecla < 106)) || ((tecla > 47) && (tecla < 58)))
							{
								valorFinal = valorFinal + mascara.charAt(i + jaTemMascara)  + valor.charAt(i);			
								jaTemMascara = jaTemMascara + 1;	
							}
							else
							{
								valorFinal = valorOriginal.substring(0, valorOriginal.length -1);
							}
						}
						else
						{
							valorFinal = valorFinal + mascara.charAt(i + jaTemMascara)  + valor.charAt(i);			
							jaTemMascara = jaTemMascara + 1;
						}					
					}
				}
				else
				{
					if(mascara.charAt(i + jaTemMascara) == "#")
					{
						if(((tecla > 95) && (tecla < 106)) || ((tecla > 47) && (tecla < 58)))
						{
							valorFinal = valorFinal  + valor.charAt(i);
						}
						else
						{
							valorFinal = valorOriginal.substring(0, valorOriginal.length -1);
						}
					}
					else if(mascara.charAt(i + jaTemMascara) == "@")
					{
						valorFinal = valorFinal  + valor.charAt(i);
					}
					else
					{
						if(mascara.charAt(i + jaTemMascara +1) == "#")
						{
							if(((tecla > 95) && (tecla < 106)) || ((tecla > 47) && (tecla < 58)))
							{
								valorFinal = valorFinal + mascara.charAt(i + jaTemMascara)  + valor.charAt(i);			
								jaTemMascara = jaTemMascara + 1;	
							}
							else
							{
								valorFinal = valorOriginal.substring(0, valorOriginal.length -1);
							}
						}
						else
						{
							valorFinal = valorFinal + mascara.charAt(i + jaTemMascara)  + valor.charAt(i);			
							jaTemMascara = jaTemMascara + 1;
						}							
					}	
				}	
			}	
		}
		else
		{
			valorFinal = valorOriginal.substring(0, mascara.length);	
		}
	}
	else
	{
		valorFinal = valorOriginal.substring(0, valorOriginal.length);		
	}
	document.getElementById(id).value = valorFinal;
}
