﻿/*
------------------------------------------------------------------------------------------
© Digital Industry - 2008
Autor:		Lincon Ribeiro
Cliente: 	Portal Perfume
------------------------------------------------------------------------------------------
*/

var nmMenuAtual = -1;

function verificaForm(objForm){
	if(dynamicSimpleValidate(objForm) != false){
		objForm.stMethod.value='Insert';
		objForm.submit();
	}
}

function formataMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
		    var sep = 0;
		    var key = '';
		    var i = j = 0;
		    var len = len2 = 0;
		    var strCheck = '0123456789';
		    var aux = aux2 = '';
		    var whichCode = (window.Event) ? e.which : e.keyCode;    
		    // 13=enter, 8=backspace as demais retornam 0(zero)
		    // whichCode==0 faz com que seja possivel usar todas as teclas como delete, setas, etc    
		    if ((whichCode == 13) || (whichCode == 0) || (whichCode == 8))
		    	return true;
		    key = String.fromCharCode(whichCode); // Valor para o código da Chave
		 
		    if (strCheck.indexOf(key) == -1) 
		    	return false; // Chave inválida
		    len = objTextBox.value.length;
		    for(i = 0; i < len; i++)
		        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) 
		        	break;
		    aux = '';
		    for(; i < len; i++)
		        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) 
		        	aux += objTextBox.value.charAt(i);
		    aux += key;
		    len = aux.length;
		    if (len == 0) 
		    	objTextBox.value = '';
		    if (len == 1) 
		    	objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
		    if (len == 2) 
		    	objTextBox.value = '0'+ SeparadorDecimal + aux;
		    if (len > 2) {
		        aux2 = '';
		        for (j = 0, i = len - 3; i >= 0; i--) {
		            if (j == 3) {
		                aux2 += SeparadorMilesimo;
		                j = 0;
		            }
		            aux2 += aux.charAt(i);
		            j++;
		        }
		        objTextBox.value = '';
		        len2 = aux2.length;
		        for (i = len2 - 1; i >= 0; i--)
		        	objTextBox.value += aux2.charAt(i);
		        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
		    }
	return false;
}

function hideField(idField){
	document.getElementById(idField).style.display='none'
}

function displayField(idField){
	document.getElementById(idField).style.display=''
}

function showHideCheckBox(idField){
	if(document.getElementById(idField).style.display == ''){
		document.getElementById(idField).style.display='none';
		document.getElementById(idField).value='';
	}else{
		document.getElementById(idField).style.display='';
	}
}

function submitAnchor(objForm, stAnchor){
	objForm.action	=  objForm.action + stAnchor;
	objForm.submit();
}

function setValidate(stStatus, objField){
	if(stStatus == 'true'){
		objField.setAttribute('boValidate', 'true');
	}else if(stStatus == 'false'){
		objField.setAttribute('boValidate', '');
	}
}

function unsetReadOnly(objField1,objField2){
	if(objField1.value == ""){
		objField2.disabled=true;
	}else{
		objField2.disabled=false;
	}
}

function setReadOnlyCheckBox(objCheckBox, idField){
	objField		= 	document.getElementById(idField);
	if(objCheckBox.checked == true){
		objField.value = '';
		objField.disabled = true;
		objField.setAttribute('stFieldType', '');
	}else{
		objField.disabled = false;
		objField.setAttribute('stFieldType', 'cpf');
	}
}

function changeMenu(nmCountSub){
	var objNmCount 		= document.getElementById('id_nmCount')
	
	for(nmInc = 0; nmInc < objNmCount.value; nmInc ++){
		if(nmInc == nmCountSub){
			document.getElementById('id_submenu_'+nmCountSub).style.display = '';
			
			if(nmMenuAtual ==  nmCountSub){
				document.getElementById('id_submenu_'+nmCountSub).style.display = 'none';
				nmCountSub = -1
			} 
		}else{
			document.getElementById('id_submenu_'+nmInc).style.display = 'none';
		}
	}

	nmMenuAtual 		= nmCountSub
}

function sendDataOpener(objField, objSender){
	var nmTotal 	= objField.options.length;
	var nmCount, nmErro;
	if (objSender.checked == true){
		for (nmCount = 0; nmCount < nmTotal; nmCount ++){
			if(objField.options[nmCount].value == objSender.value){
				nmErro	= 1;
			}
		}
		if (nmErro != 1){
			window.opener.addOption(objSender.name, objSender.value)
		}
	}else if(objSender.checked == false){
		for (nmCount = 0; nmCount < nmTotal; nmCount ++){
			if(objField.options[nmCount].value == objSender.value){
				objField.options[nmCount] = null
			}
		}
	}
}

function unsetReadOnlyCheckBox(objCheckBox, idField){
	objField		= 	document.getElementById(idField);
	if(objCheckBox.checked == true){
		objField.setAttribute('boValidate', 'true');
		objField.disabled = false;
		objField.value = '';
	}else{
		objField.setAttribute('boValidate', 'false');
		objField.disabled = true;
		objField.value = '';
	}
}

function confirmChildDelete(stParameters){
	if(confirm('Todos os registros ligados a este item serão excluídos. \nTem certeza que deseja excluí-lo?')){
		location.href=(stParameters)
	}
}

function removeRows(idTable){
	var objTable	= document.getElementById(idTable);
	var	nmLength	= new Number;
	nmLength		= objTable.rows.length;
	for(nmInc = 0; nmInc < nmLength; nmInc++){
		objTable.deleteRow();
	}
}

function changeValue(stValue, idField){
	document.getElementById(idField).value	= stValue;
}

function timerDiv(idField){
	//window.setTimeout(hideField(idField), 40000);
}

function countRows(idTable, idField){
	var objTable	= document.getElementById(idTable)
	var nmLength	= objTable.rows.length;
	document.getElementById(idField).value = nmLength;	
}

function cleanField(idField){
	document.getElementById(idField).value = '';
}


function enabledDetalhe(idAreaAtuacao){
	var objAcademia 		= document.getElementById('id_academia');
	var objUniversidade 	= document.getElementById('id_universidade');
	
	idAreaAtuacao == 1 	? objAcademia.style.display = '' : objAcademia.style.display = 'none';
	idAreaAtuacao == 29 ? objUniversidade.style.display = '' : objUniversidade.style.display = 'none';
}

function createAnswer(idField){
	var nmValue		= document.getElementById(idField).value
	var table 		= document.getElementById("id_table2");
	var nmLength	= table.rows.length
	var nmInc;
	if (nmValue > 0){
		//var linha, coluna;
		for(nmInc = 0; nmInc < nmValue; nmInc++){
			trElem 			= table.insertRow(table.rows.length);
				tdElem1 		= trElem.insertCell(trElem.cells.length);
				tdElem1.innerHTML 	= "Resposta:"
				tdElem2			= trElem.insertCell(trElem.cells.length);
				tdElem2.innerHTML	= "<input type='text' name='stResposta"+nmInc+"' size='30' maxlength='100' boValidate='true' stMsgError='Favor preencher o campo Resposta' class='campos' value='' />";
		}
	}
}


function checkFile(idTipo){
	var stArquivo;
	stArquivo		= document.frmUpload.stArquivo.value;
	arrayArquivo	= stArquivo.split(".")
	nmLength		= arrayArquivo.length - 1;
	stExtensao 		= arrayArquivo[nmLength];
	switch(idTipo){
		case 0:
			alert("Tipo de arquivo não especificado. Favor contatar administrador."); return false;break;
		case 1:
			if(stExtensao != "jpg" && stExtensao != "gif" && stExtensao != "jpeg" && stExtensao != "png"){
				alert("Extensão de arquivo não suportada. Envio nos formatos: jpg, gif, jpef ou png");
				return false;break;
			}else{return true;}
		case 2:
			if(stExtensao != "pdf"){
				alert("Extensão de arquivo não suportada. Somente Arquivos PDF.");
				return false;break;
			}else{return true;}
		case 3:
			if(stExtensao != "mp3"){
				alert("Extensão de arquivo não suportada. Somente arquivos Mp3.");
				return false;break;
			}else{return true;}
		case 4:
			if(stExtensao != "swf"){
				alert("Extensão de arquivo não suportada. Somente arquivos SWF.");
				return false;break;
			}else{return true;}
	}
}
