/******************************
CRIR
******************************/
crir = {
	init: function() {
		arrLabels = document.getElementsByTagName('label');
	
		searchLabels:
		for (var i=0; i<arrLabels.length; i++) {			
			// get the input element based on the for attribute of the label tag
			if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value != '') {
				labelElementFor = arrLabels[i].getAttributeNode('for').value;				
				inputElement = document.getElementById(labelElementFor);
			}
			else {				
				continue searchLabels;
			}	
							
			inputElementClass = inputElement.className;	
		
			// if the input is specified to be hidden intiate it
			if (inputElementClass == 'crir_hidden_js') {
				inputElement.className = 'crir_hidden';
				
				inputElementType = inputElement.getAttributeNode('type').value;	
				
				// add the appropriate event listener to the input element
				if (inputElementType == "checkbox") {
					inputElement.onclick = crir.toggleCheckboxLabel;
				}
				else {
					inputElement.onclick = crir.toggleRadioLabel;
				}
				
				// set the initial label state
				if (inputElement.checked) {
					if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_checked'}
					else { arrLabels[i].className = 'radio_checked' }
				}
				else {
					if (inputElementType == 'checkbox') { arrLabels[i].className = 'checkbox_unchecked'}
					else { arrLabels[i].className = 'radio_unchecked' }
				}
			}
			else if (inputElement.nodeName != 'SELECT' && inputElement.getAttributeNode('type').value == 'radio') { // this so even if a radio is not hidden but belongs to a group of hidden radios it will still work.
				arrLabels[i].onclick = crir.toggleRadioLabel;
				inputElement.onclick = crir.toggleRadioLabel;
			}
		}			
	},	
	
	findLabel: function (inputElementID) {
		arrLabels = document.getElementsByTagName('label');
	
		searchLoop:
		for (var i=0; i<arrLabels.length; i++) {
			if (arrLabels[i].getAttributeNode('for') && arrLabels[i].getAttributeNode('for').value == inputElementID) {				
				return arrLabels[i];
				break searchLoop;				
			}
		}		
	},	
	
	toggleCheckboxLabel: function () {
		labelElement = crir.findLabel(this.getAttributeNode('id').value);
	
		if(labelElement.className == 'checkbox_checked') {
			labelElement.className = "checkbox_unchecked";
		}
		else {
			labelElement.className = "checkbox_checked";
		}
	},	
	
	toggleRadioLabel: function () {			 
		clickedLabelElement = crir.findLabel(this.getAttributeNode('id').value);
		
		clickedInputElement = this;
		clickedInputElementName = clickedInputElement.getAttributeNode('name').value;
		
		arrInputs = document.getElementsByTagName('input');
	
		// uncheck (label class) all radios in the same group
		for (var i=0; i<arrInputs.length; i++) {			
			inputElementType = arrInputs[i].getAttributeNode('type').value;
			if (inputElementType == 'radio') {
				inputElementName = arrInputs[i].getAttributeNode('name').value;
				inputElementClass = arrInputs[i].className;
				// find radio buttons with the same 'name' as the one we've changed and have a class of chkHidden
				// and then set them to unchecked
				if (inputElementName == clickedInputElementName && inputElementClass == 'crir_hidden') {				
					inputElementID = arrInputs[i].getAttributeNode('id').value;
					labelElement = crir.findLabel(inputElementID);
					labelElement.className = 'radio_unchecked';
				}
			}
		}
	
		// if the radio clicked is hidden set the label to checked
		if (clickedInputElement.className == 'crir_hidden') {
			clickedLabelElement.className = 'radio_checked';
		}
	},
	
	addEvent: function(element, eventType, doFunction, useCapture){
		if (element.addEventListener) 
		{
			element.addEventListener(eventType, doFunction, useCapture);
			return true;
		} else if (element.attachEvent) {
			var r = element.attachEvent('on' + eventType, doFunction);
			return r;
		} else {
			element['on' + eventType] = doFunction;
		}
	}
}

crir.addEvent(window, 'load', crir.init, false);

/******************************
CADASTRAR NEWSLETTER
******************************/
function cadastrar()
{
	nome = document.form_news.news_name.value;
	email = document.form_news.news_email.value;
	parent.location = "newsletter.asp@acao=incluir&news_name="+nome+"&news_email="+email+"";
} 


/******************************
LIMPAR CAMPOS DO FORMULÁRIO
******************************/

var active_color = '#4d4a42';
var inactive_color = '#807b6e';

window.onload = formDefaultValues;

function formDefaultValues() {
	var fields = getElementsByClassName(document, "input", "text_disappear");
	if (!fields) {
		return;
	}
	var default_values = new Array();
	for (var i = 0; i < fields.length; i++) {
		fields[i].style.color = inactive_color;
		if (!default_values[fields[i].id]) {
			default_values[fields[i].id] = fields[i].value;
		}
		fields[i].onfocus = function() {
			if (this.value == default_values[this.id]) {
				this.value = '';
				this.style.color = active_color;
			}
			this.onblur = function() {
				if (this.value == '') {
					this.style.color = inactive_color;
					this.value = default_values[this.id];
				}
			}
		}
	}
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for (var i = 0; i < arrElements.length; i++) {
		oElement = arrElements[i];
		if (oRegExp.test(oElement.className)) {
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements);
}

/******************************
E-MAIL THIS OFFER TO A FRIEND
******************************/
function addEvent()
{
var ni = document.getElementById('more_amigo');
var numi = document.getElementById('theValue');
var num = (document.getElementById("theValue").value-1)+2;
numi.value = num;
var inputIdName = "to"+num;
var newInput = document.createElement('input');
newInput.setAttribute("type","text");
newInput.setAttribute("id",inputIdName);
newInput.setAttribute("class","txtfield_amigo");
newInput.setAttribute("name",inputIdName);
ni.appendChild(newInput);
ni.appendChild(document.createElement(' br'));
}

function valida()
{
	if (document.form.yourmail.value=="" ) {
    	alert("Please inform your e-mail address.");
        document.form.yourmail.focus();
	return false;
		}		
}

/******************************
EXIBE/ESCONDE TABELA
******************************/
function changeRow() 
{
	var intCount = document.getElementById("selTest").options.length; //gets count of items in select menu
	var intValue = document.getElementById("selTest").selectedIndex; //gets number (0 index) of selected item
    	for (i=0;i<intCount;i++)
		{
        	var strValue = document.getElementById("selTest")[i].value; //gets value of selected item
	        if (i == intValue) {
            document.getElementById(strValue).style.display='block';
        }

		else
		{
            document.getElementById(strValue).style.display='none';
		}
	}
}

/******************************
BOOKMARK US
******************************/
function bookmarksite(title,url)
{
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");

	else if(window.opera && window.print) // opera
	{
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all) // ie
		window.external.AddFavorite(url, title);
}

/******************************
POPUP
******************************/

function MM_openBrWindow(theURL,winName,features)
{
	window.open(theURL,winName,features);
}

/******************************
PRELOAD IMAGES
******************************/

function MM_preloadImages()
{
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/******************************
VALIDADTE FORM
******************************/
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }