function celulaOn(object) {
	document.all[object].style.backgroundColor = '#612e99';
}

function celulaOff(object) {
	document.all[object].style.backgroundColor = 'Transparent';
}

function destOn(object) {
	document.all[object].style.backgroundColor = '#178e9a';
}

function destOff(object) {
	document.all[object].style.backgroundColor = '#4a246b';
}

// Script Tab automatico para formularios//
var isNN = (navigator.appName.indexOf("Netscape") != -1);
function autoTab(input, len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [ 0, 8, 9 ] : [ 0, 8, 9, 16, 17, 18, 37, 38, 39, 40,
			46 ];
	if (input.value.length >= len && !containsElement(filter, keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input) + 1) % input.form.length].focus();
	}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while (!found && index < arr.length)
			if (arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)
				index = i;
			else
				i++;
		return index;
	}
	return true;
}

function CheckSend(formType) {
	var chkTermo;
	var dddValue;
	var msisdnValue;

	window.status = "Teste";
	if (formType == "alert") {
		chkTermo = document.formAlert.chkCompromise.checked;
		dddValue = document.formAlert.ddd.value;
		msisdnValue = document.formAlert.msisdn.value;
	} else if (formType == "buy") {
		chkTermo = document.formBuy.chkCompromise.checked;
		dddValue = document.formBuy.ddd.value;
		msisdnValue = document.formBuy.msisdn.value;
	}

	if (chkTermo) {
		if (dddValue != "" && msisdnValue != "") {
			if (dddValue.length  != 2 || msisdnValue.length != 8) {
				alert('Digite o numero do seu celular corretamente.');
				return false;
			}
			return validaTelefone(formType, msisdnValue, dddValue);		
		} else {
			alert('Voce precisa preencher todos os campos antes de comprar.');
			return false;
		}
		
	} else {
		alert('Voce precisa concordar com o termo de compromisso antes de comprar.');
		return false;
	}
}

// Open POPUP de Preview
function openPreview(arquivo) { // v2.0
	window
			.open("preview.jsp?som=" + arquivo, 'tocando',
					'width=180,height=220');
}

function validaMsgManagerForm() {

	if (document.formMessage.startDate.value == "") {
		alert('Data inicial invalida.');
		return false;

	}
	if (document.formMessage.endDate.value == "") {
		alert('Data final invalida.');
		return false;

	}

	document.formMessage.submit();
}

function validaNewsManagerForm() {
	if (document.formMessage.startDate.value != "") {
		if (document.formMessage.endDate.value == "") {
			alert('Data final invalida.');
			return false;
		}
	}

	if (document.formMessage.endDate.value != "") {
		if (document.formMessage.startDate.value == "") {
			alert('Data inicial invalida.');
			return false;
		}
	}
	document.formMessage.state.value = 'filtrar';
	document.formMessage.submit();
}

function validaPromotionManagerForm() {
	if (document.formMessage.startDate.value != "") {
		if (document.formMessage.endDate.value == "") {
			alert('Data final invalida.');
			return false;
		}
	}

	if (document.formMessage.endDate.value != "") {
		if (document.formMessage.startDate.value == "") {
			alert('Data inicial invalida.');
			return false;
		}
	}

	document.formMessage.submit();
}

function checkKeyword(e) {
	if (navigator.appName == "Netscape")
		clickType = e.which;
	else
		clickType = e.button;

	var unicode = e.keyCode ? e.keyCode : e.charCode
	if (unicode == 13 || clickType == 1) {
		document.formu.keyword.value = Trim(document.formu.keyword.value);
		if (document.formu.keyword.value == "") {
			alert('Palavra-chave em branco.');
			return false;
		} else {
			changeCategoryFormu('-1');
			return true;
		}
	}
	return true;
}

function changeCategory(categoryId) {
	document.formCategory.categoryId.value = categoryId;
	document.formCategory.submit();
}

function changeCategoryFormu(categoryId) {
	document.formu.categoryId.value = categoryId;
	document.formu.submit();
}

function selectMedia(mediaId) {
	document.formMediaInfo.mediaId.value = mediaId;
	document.formMediaInfo.submit();
}

function CheckDevice() {
	var cnt = -1;
	for ( var i = document.formDevice.deviceId.length - 1; i > -1; i--) {
		if (document.formDevice.deviceId[i].checked) {
			cnt = i;
			i = -1;
		}
	}
	if (cnt > -1)
		return document.formDevice.deviceId[cnt].value;
	else
		alert("Voce precisa selecionar um aparelho antes de clicar em Ok");
	return false;
}

function validaTelefone(formType, msisdnValue, dddValue) {
	var conversao;
	var conversaoDDD;
	var msgValue;

	if (formType == "alert") {
		msgValue = "";
	} else if (formType == "buy") {
		msgValue = document.formBuy.msg.value;
	}
	conversao = parseInt(msisdnValue);
	conversaoDDD = parseInt(dddValue);

	if ((isNaN(dddValue)) || (conversaoDDD < 10)) {
		alert("DDD invalido.");
		return false;
	}
	return true;
}

function validaNewsForm(state) {
	if (document.newsForm.channel.value == "") {
		alert('A radio nao foi escolhida.');
		return false;
	} else {
		if (document.newsForm.validationDate.value == "") {
			alert('A data de validade nao foi preenchida.');
			return false;
		} else {
			if (document.newsForm.txtTexto.value == "") {
				alert('O texto nao foi preenchido.');
				return false;
			} else {
				setState(state);
				return true;
			}
		}
	}
}

function setState(state) {
	document.newsForm.state.value = state;
}

function validaPromotionFieldsForm() {
	if (document.promotionsForm.name.value == "") {
		alert('O campo nome esta em branco.');
		return false;
	}

	if (document.promotionsForm.value.value == "") {
		alert('O campo descricao esta em branco.');
		return false;
	}

	if (document.promotionsForm.radios[0].checked == 0
			&& document.promotionsForm.radios[1].checked == 0
			&& document.promotionsForm.radios[2].checked == 0
			&& document.promotionsForm.radios[3].checked == 0
			&& document.promotionsForm.radios[4].checked == 0
			&& document.promotionsForm.radios[5].checked == 0) {
		alert('E necessario que seja escolhida pelo menos uma radio.');
		return false;
	}
	document.promotionsForm.submit();
}

function Trim(TRIM_VALUE) {
	if (TRIM_VALUE.length < 1) {
		return "";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if (TRIM_VALUE == "") {
		return "";
	} else {
		return TRIM_VALUE;
	}
} // End Function

function RTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if (v_length < 0) {
		return "";
	}
	var iTemp = v_length - 1;

	while (iTemp > -1) {
		if (VALUE.charAt(iTemp) == w_space) {
		} else {
			strTemp = VALUE.substring(0, iTemp + 1);
			break;
		}
		iTemp = iTemp - 1;

	} // End While
	return strTemp;

} // End Function

function LTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	if (v_length < 1) {
		return "";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while (iTemp < v_length) {
		if (VALUE.charAt(iTemp) == w_space) {
		} else {
			strTemp = VALUE.substring(iTemp, v_length);
			break;
		}
		iTemp = iTemp + 1;
	} // End While
	return strTemp;
}

function submitRequest(id) {
	var d = document.formDevice;
	d.deviceId.value = id;
	d.submit();
}
