var nControleDebug = 0;
var nPosCPF, nPosCNPJ, aEmpresaPos=[];
var nContato = 1;
var aContatos = [[]];
// Criar Variavel com os campos a serem atualizados  com o número de Contatos:
// var aDisplay = new Array("nome_contador1" <, "nome_contador2", ...>);

// Criar Variavel com os campos a serem armazenados para cada contato:
// var aArmazenar = new Array(["nome_campo1", default] <, ["nome_campo2", default], ...>);

// Criar Variavel com os campos a serem restaurados como dados iniciais:
// var aOriginal = new Array("nome_campo1" <, "nome_campo2", ...>);

function ContatoAtual(){ return nContato; }
function NumeroContatos(){ return aContatos.length; }

function SetStyleByID(cElement, cValue){
	for(nI=0;nI<9;nI++){
		cNomeID = cElement + nI;
		this.css  = ( is_ie ? eval("document.all."+cNomeID+'.style') : ( is_gecko ? document.getElementById(cNomeID).style : eval("document."+cNomeID) ) );
		this.css.visibility = cValue;
	}
}

//	Funções para transferencia emtre SELECT's
function transfere(sbox, tbox) {
	for(var nI=sbox.options.length-1; nI>=0; nI--) {
		if ( sbox[nI].selected && sbox[nI].value != "" ){
//			tbox.options[tbox.options.length] = new Option(sbox[nI].text, sbox[nI].value);
			tbox.options[tbox.options.length] = new Option("", "");
			// Aqui rotina para ordenar select
			nPosS = tbox.length-1;
			while (nPosS > 0 && parseInt(sbox[nI].value) < parseInt(tbox[nPosS-1].value)) {
				tbox[nPosS].text = tbox[nPosS-1].text;
				tbox[nPosS].value = tbox[nPosS-1].value;
				nPosS--;
			}
			tbox[nPosS].text = sbox[nI].text;
			tbox[nPosS].value = sbox[nI].value;
			sbox[nI] = null;
		}
	}
}
//

function ativaIdioma(){
	with ( document.ficha ) SetFieldIf(outro_idioma, idioma);
}

function ativaResponsabilidade(){
	with ( document.ficha ) SetFieldIf(outra_responsabilidade, responsabilidade);
}

function ativaDepartamento(){
	with ( document.ficha ) SetFieldIf(outro_departamento, departamento);
}

function ativaDepartamentoR(){
	with ( document.ficha ) SetFieldIf(outro_departamento_r, departamento_r);
}

function SetFieldIf(oField_outro, oField){
	lDisabled  = (oField.selectedIndex != oField.options.length-1);
	if ( lDisabled ) oField_outro.value = "";
	oField_outro.disabled = lDisabled;
	if ( !lDisabled ) oField_outro.focus();
}

function Atualizar(){
	_AfterLoadContact();
	AtualizaNomeCongressistas();
	if ( typeof AfterLoadContact === "function" ) AfterLoadContact(nContato);
}

function _AfterLoadContact(){
	var cText, oForm=document.ficha;
	cText = nContato + "/" + aContatos.length;
	if ( oForm["PosLabel[]"] != undefined )
		for ( var nI=0; nI<oForm["PosLabel[]"].length; nI++ ) oForm["PosLabel[]"][nI].value = cText;
}

function AtualizaNomeCongressistas(){
	var oForm=document.ficha, nLen=0;
	if ( oForm.nome_responsavel != undefined ){
		nLen = oForm.nome_responsavel.length;
		with ( oForm ){
			if ( nLen == 1){
				nome_responsavel[0].text  = 'Selecione um Congressista';
				nome_responsavel[0].value = '';
			}
			if ( nLen <= nContato ) nome_responsavel.options[nLen] = new Option('', '');
			nome_responsavel[nContato].text  = nome.value;
			nome_responsavel[nContato].value = nome.value;
		}
	}
}

function setResponsavel(nPos){
	var oForm=document.ficha;
	nField = Localiza('responsavel');
	for (var nI=0; nI<aContatos.length; nI++) aContatos[nI][nField]=(nI==nPos-1);
	oForm.nome_responsavel.selectedIndex = nPos;
	oForm.responsavel.checked=(nContato==nPos);
}

function Navegar(nRecord){
	if ( ( nRecord + nContato > 0 ) && ( nRecord + nContato <= aContatos.length ) ){
		AlimentaContato();
		nContato += nRecord;
		CarregaContato();
		Atualizar();
	}
}

function NewContato(){
	aContatos[nContato] = new Array();
	for ( var nI=0; nI<aArmazenar.length; nI++ ) aContatos[nContato][nI] = aArmazenar[nI][1];
	if ( nContato > 0 ) LerDadosEmpresaAnterior();
	nContato++;
}

function LerDadosEmpresaAnterior(){
	for ( var nI=0; nI<aEmpresaFields.length; nI++ ){
		nField = Localiza(aEmpresaFields[nI]);
		aContatos[nContato][nField] = aContatos[nContato-1][nField];
	}
}

function LimpaDadosContatoTela(cTipo){
	var nField, oForm=document.ficha;
	for ( var nI=0; nI<aContatoFields.length; nI++ )
		if ( oForm[aContatoFields[nI]+cTipo] != undefined ) oForm[aContatoFields[nI]+cTipo].value = '';
}

function LimpaDadosEmpresaTela(cTipo){
	var nField, oForm=document.ficha;
	for ( var nI=0; nI<aEmpresaFields.length; nI++ )
		if ( oForm[aEmpresaFields[nI]+cTipo] != undefined ) oForm[aEmpresaFields[nI]+cTipo].value = '';
}

function LerDadosEmpresaTela(nPosContato, cTipo){
	var nField, oForm=document.ficha;
	for ( var nI=0; nI<aEmpresaFields.length; nI++ ){
		nField = Localiza(aEmpresaFields[nI]);
		if ( oForm[aEmpresaFields[nI]+cTipo] != undefined )
			oForm[aEmpresaFields[nI]+cTipo].value = oForm[aEmpresaFields[nI]].value ;
	}
}

function AddContato(){
	AlimentaContato();
	nContato = aContatos.length;
	NewContato();
	CarregaContato();
	Atualizar();
}

function DelContato(){
//	removedElements = arrayName.splice(index, count, newelement1, newelement2,...,newelementn)
	var aDeleted;
	if (!isFunction(Array.prototype.splice)) 	alert("Não existe esta função");
	aContatos.splice(nContato-1, 1);
	if ( nContato >= aContatos.length ) nContato = aContatos.length;
	if ( nContato == 0 ) NewContato();
	CarregaContato();
	Atualizar();
}

function ConfirmaExclusao(){
	if (confirm("\nConfirma exclusão do registro atual ?\n\n")) DelContato();
}

function AlimentaContato(){
	var nI, nLen=aArmazenar.length, nRow=nContato-1;
	for ( nI=0; nI<nLen; nI++ ) AlimentaCampo(nRow, nI);
}

//function AlimentaContato(){
function AlimentaCampo(nPosContato, nCampo){
	var nI, nJ, nPos, oField, nLen1, nLen2, cTipo, aSelected, oForm=document.ficha;
	oField = oForm[aArmazenar[nCampo][0]];
	aSelected = new Array();
	if ( oField  != undefined ){
		cTipo = "" + oField.type;
		switch ( cTipo ) {
			case "hidden":
			case "textarea":
			case "text": aContatos[nPosContato][nCampo] = oField.value; break;
			case "select-one":
			case "select-multiple":
				nLen1 = oField.length;
				aContatos[nPosContato][nCampo] = new Array();
				if ( aArmazenar[nCampo].length > 2 ){
					for (nI=0; nI<nLen1; nI++){
						aSelected[aSelected.length] = oField[nI].value;
						oField[nI].selected = true;
					}
					oField2 = oForm[aArmazenar[nCampo][2]];
					transfere(oField, oField2);
					nLen1 = aSelected.length;
					nLen2 = oField2.length;
					for (nI=0; nI<nLen1; nI++){
						nPos = -1;
						for (nJ=0; nPos<0 && nJ<nLen2; nJ++)
							if ( oField2[nJ].value == aSelected[nI] ) nPos = nJ;
						if ( nPos >=0 ) aContatos[nPosContato][nCampo][aContatos[nPosContato][nCampo].length] = nPos;
					}
				} else if ( cTipo == "select-one" ) { aContatos[nPosContato][nCampo][0] = oField.selectedIndex;
				} else {
					for (nI=0; nI<nLen1; nI++)
						if ( oField[nI].selected )
							aContatos[nPosContato][nCampo][aContatos[nPosContato][nCampo].length] = nI;
				}
				break;
			case "radio":
			case "checkbox": aContatos[nPosContato][nCampo] = oField.checked; break;
			case "undefined":
				switch ( oField[0].type ) {
					case "radio":
					case "checkbox":
						nLen1 = oField.length;
						aContatos[nPosContato][nCampo] = new Array();
						for (nI=0; nI<nLen1; nI++)
							if ( oField[nI].checked )
								aContatos[nPosContato][nCampo][aContatos[nPosContato][nCampo].length] = nI;
						break;
				}
				break;
		}
	}
}

function CarregaContato(){
	var nI, nLen=aArmazenar.length, nRow=nContato-1;
	for ( nI=0; nI<nLen; nI++ ) CarregaCampo(nRow, nI);
}

function CarregaCampo(nPosContato, nCampo){
	var oField, oField2, cTipo, nI, nLen1, nLen2, oForm=document.ficha;
	oField = oForm[aArmazenar[nCampo][0]];
	if ( oField == undefined ) return false;
	cTipo  = "" + oField.type;
	switch ( cTipo ) {
		case "textarea":
		case "text": oField.value = aContatos[nPosContato][nCampo]; break;
		case "select-one":
		case "select-multiple":
			if ( typeof aContatos[nPosContato][nCampo] === "number" ){
				oField2 = aContatos[nPosContato][nCampo];
				aContatos[nPosContato][nCampo] = [oField2];
			}
			nLen1 = oField.length;
			nLen2 = aContatos[nPosContato][nCampo].length;
			if ( aArmazenar[nCampo].length > 2 ){
				for (nI=0; nI<nLen1; nI++) oField[nI].selected = true;
				oField2 = oForm[aArmazenar[nCampo][2]];
				transfere(oField, oField2);
				for (nI=0; nI<nLen2; nI++)
					oField2[aContatos[nPosContato][nCampo][nI]].selected = true;
				transfere(oField2, oField);
			} else if ( cTipo == "select-one" ) {
			  oField.selectedIndex = aContatos[nPosContato][nCampo][0];
			} else {
				for (nI=0; nI<nLen1; nI++) oField[nI].selected = false;
				for (nI=0; nI<nLen2; nI++) oField[aContatos[nPosContato][nCampo][nI]].selected = true;
			}
			break;
		case "radio":
		case "checkbox": oField.checked = aContatos[nPosContato][nCampo]; break;
		case "undefined":
			if ( typeof aContatos[nPosContato][nCampo] === "number" ){
				oField2 = aContatos[nPosContato][nCampo];
				aContatos[nPosContato][nCampo] = [oField2];
			}
			switch ( oField[0].type ) {
				case "radio":
				case "checkbox":
					nLen1 = oField.length;
					for (nI=0; nI<nLen1; nI++) oField[nI].checked = false;
					nLen1 = aContatos[nPosContato][nCampo].length;
					for (nI=0; nI<nLen1; nI++) oField[aContatos[nPosContato][nCampo][nI]].checked = true;
					break;
			}
			break;
	}
	return true;
}

var lDebug = false;
function checar(){
	// Erros no Cadastro do Trabalho
	if ( !ChecarErros(aErrosTrabalho) ) return false;
	// Erros no Cadastro do Contato
	if ( !ChecarErros(aErrosContato) )  return false;
	// Erros no Cadastro da Empresa
	if ( !ChecarErros(aErrosEmpresa) )  return false;
	// Erros na Seleção das Formas como soube do Evento
	if ( !ChecarErros(aErrosComoSoube) )  return false;
	// Erros na Forma de Pagamento  e Taxas de Inscrição
	if ( !ChecarErros(aErrosPagamento) )  return false;
	// Erros no Cadastro do Responsavel
	if ( !ChecarErros(aErrosResponsavel) ) return false;
	AlimentaContato();
	return true;
}

var campo_errado = "#FCF0F0", campo_certo  = "#FFFFFF";
function ChecarErros(aErros){
	var oForm=document.ficha, oField, cType, cMess, nLen;
	if ( aErros.length > 1 ){
		with ( document.ficha ) {
			cMess = aErros[0];
			nLen = aErros.length;
			for ( var nJ=1; nJ<nLen; nJ++ ){
				if ( document.ficha[ aErros[nJ][1] ] != undefined ){
					cType = document.ficha[ aErros[nJ][1] ].type;     //"" + eval( "typeof " + aErros[nJ][1] );
					if ( ( cType != "undefined" ) && ( aErros[nJ][0] == '' || eval( aErros[nJ][0] ) ) )
						if ( cType == "boolean" ){
							if ( !eval( aErros[nJ][1] ) ){
								alert(cMess+aErros[nJ][2]+"\n");
								eval( aErros[nJ][1] + '.style.backgroundColor = campo_errado' );
								return false;
							}
						} else if( !ePreenchido(cMess, [aErros[nJ][1], aErros[nJ][2]]) ) return false;
				}
			}
		}
	}
	return true;
}

function ePreenchido(cMess, cCampo){
	var cAttributeVerifier="", cTipo, oForm=document.ficha, lVerifier=true;
	with ( document.ficha ) {
		oField = oForm[ cCampo[0] ];
		lContinue = true;
		cTipo = "" + oField.type;
		switch ( cTipo ) {
			case "textarea":
			case "text": lVerifier= ( oField.value == "" ); break;
			case "select-one": lVerifier= ( oField.selectedIndex == 0 ); break;
			case "radio":
			case "checkbox": lVerifier= ( oField.checked == false ); break;
			default: lContinue = false; break;
		}
		if ( lContinue ){
			if ( lVerifier ){
				alert(cMess+cCampo[1]+"\n");
				oField.focus();
				oField.style.backgroundColor = campo_errado;
				return false;
			}
		} else {
			var nJ, nLength=oField.length, lSelected=false;
			switch ( cTipo ) {
				case "select-multiple":
					for ( var nJ=0; nJ<nLength && !lSelected; nJ++) lSelected = oField[nJ].selected;
					break;
				case "undefined":
					if ( oField[0].type == "checkbox" || oField[0].type == "radio" )
						for (var nJ=0; nJ<nLength && !lSelected; nJ++) lSelected = oField[nJ].checked;
					break;
				default: lSelected = true; break;
			}
			if ( !lSelected ){
				if ( cTipo == "select-multiple" ){
					oField.focus();
		 			oField.style.backgroundColor = campo_errado;
				} else{
					oField[0].focus();
					oField[0].style.backgroundColor = campo_errado;
				}
				alert(cMess+cCampo[1]+"\n");
				return false;
			}
		}
		if ( cTipo != 'undefined' ) oField.style.backgroundColor = campo_certo;
	}
	return true;
}

var cTipoValidacao = '';
function ValidaCPF(oCPF, cTipo){
	var nI, nLen=aContatos.length, nField, oForm=document.ficha, nContatoCPF=-1;
	if ( typeof cTipo === "undefined" ) cTipo = "";
	oCPF.value = DeixaDigitos(oCPF.value);
	if ( !isCPF( oCPF.value ) ){
   	alert("\nCPF Inválido\n");
		oCPF.focus();
		return false;
	} else {
		if ( typeof nPosCPF === "undefined" ) nPosCPF = Localiza("cpf");
		if ( nPosCPF >= 0 ){
//			alert(nLen);
			for (nI=0; nContatoCPF<0 && nI<nLen; nI++){
//				alert(nI + "\n" + aContatos[nI][nPosCPF] + "\n" + oCPF.value);
				 if ( aContatos[nI][nPosCPF] == oCPF.value ) nContatoCPF = nI;
			}
			if ( nContatoCPF < 0 && cTipo != '' ) if ( oForm["cpf"].value == oCPF.value ) nContatoCPF = nContato;
			if ( nContatoCPF >= 0){
				if ( cTipo == '' ) {
					if ( nContatoCPF!=nContato ){ 
	   				alert("\nCPF Já Cadastrado\n");
						oCPF.focus();
						return false;
					}
				} else {
					for ( var nI=0; nI<aContatoFields.length; nI++ )
						if ( oForm[aContatoFields[nI]+cTipo] != undefined ){
							if ( nContatoCPF!=nContato ){
								nField = Localiza(aContatoFields[nI]);
								oForm[aContatoFields[nI]+cTipo].value = aContatos[nContatoCPF][nField];
							} else {
								oForm[aContatoFields[nI]+cTipo].value = oForm[aContatoFields[nI]].value;
							}
						}
					return false;
				}
			}
		}
	}

	cTipoValidacao = 'CPF';
	document.getElementById('HiddenIFrame').src = "/BaseSites/modulos/LocalizaCPF.php?cpf="+oCPF.value+"&cTipo="+cTipo;
	document.getElementById('_Message').innerHTML = '<strong>Executando...</strong><br>Aguarde enquanto o Sistema processa os dados referentes ao CPF.';
	with ( document.getElementById('loader').style ){
		top = ( cTipo == '' ? '300px' : '1950px' );
		display = 'block';
	}
}

function ValidaCNPJ(oCNPJ, cTipo){
	var nI, nLen, nPos=-1, oField;
	oCNPJ.value = DeixaDigitos(oCNPJ.value);
	if ( !isCNPJ( oCNPJ.value ) ){
		alert("\nCNPJ Inválido\n");
		oCNPJ.focus();
		return false;
	}
	if ( typeof nPosCNPJ === "undefined" ) nPosCNPJ = Localiza("cnpj");
	if ( nPosCNPJ >= 0 && typeof aEmpresaFields != "undefined" ){
		nLen = aEmpresaFields.length;
		if ( aEmpresaPos.length != nLen )
			for(nI=0; nI<nLen; nI++) aEmpresaPos[nI] = Localiza(aEmpresaFields[nI]);
		nLen=aContatos.length;
		if ( nLen > 0 )
			for (nI=0; nPos<0 && nI<nLen; nI++)
				if ( aContatos[nI].length > nPosCNPJ && aContatos[nI][nPosCNPJ] == oCNPJ.value ) nPos = nI;
		nLen = aEmpresaFields.length;
		if ( nPos >= 0 ) for (nI=0; nI<nLen; nI++) CarregaCampo(nPos, aEmpresaPos[nI]);
	}

	cTipoValidacao = 'CNPJ';
	document.getElementById('HiddenIFrame').src = "/BaseSites/modulos/LocalizaCNPJ.php?cnpj="+oCNPJ.value+"&cTipo="+cTipo;
	document.getElementById('_Message').innerHTML = '<strong>Executando...</strong><br>Aguarde enquanto o Sistema processa os dados referente ao CNPJ.';
	with ( document.getElementById('loader').style ){
		top = ( cTipo == '' ? '900px' : '2000px' );
		display = 'block';
	}
}

function Localizado(){
	document.getElementById('loader').style.display='none';
}

function NaoLocalizado(cAlert){
	cMessage = cAlert || 'Nenhum Registro Localizado\nPor favor preencha os campos em branco';
	document.getElementById('loader').style.display='none';
	alert(cMessage);
}

function Localiza(cField){
	var nI, nLen=aArmazenar.length;
	for (nI=0; nI<nLen; nI++) if ( aArmazenar[nI][0] == cField ) return nI;
  return -1;
}

// Rotinas para contar linhas/palavras/caracteres como o Word
//
var LINHAS = 0;
var PALAVRAS = 1;
var CARACTERES = 2;
function CountText(cText){
	var charcnt, words, wordcnt, lines, lines2, linecnt;
	with ( cText ){
		charcnt = length;
		words = match(/\b\w+\b/g);
		lines = split("\n");
		lines2 = split("\r\n");
	}
	if ( lines.length > 1 ) charcnt = charcnt - ( lines2.length==lines.length ? 2 : 1)*(lines.length - 1);
	if ( words != null && charcnt != 0 ) { wordcnt=words.length; linecnt=lines.length; } else { wordcnt=0; linecnt=0; }
	return new Array(linecnt, wordcnt, charcnt);
}

function LimitaText(oField, nLen){
	var cText=oField.value; aInfo=CountText(cText);
	if ( aInfo[CARACTERES] > nLen ){
		alert("O número de caracteres supera o máximo permitido,\no texto será truncado.\n\nNúmero máximo de caracteres : " + nLen);
		do{
			cText = cText.substring(0, cText.length-(aInfo[CARACTERES]-nLen));
		}while ( (aInfo = CountText(cText))[CARACTERES] > nLen );
		oField.value = cText;
	}
	return aInfo[CARACTERES];
}

var aCharsTopico = new Array();
function ContaChars(){
	with (document.ficha) {
		aTopicos = lista_topicos.value.split("|");
		for (nI=1; nI<aTopicos.length; nI++){
			aInfo = CountText( eval( "T"+nI+".value" ) );
			aCharsTopico[nI] = aInfo[CARACTERES];
			eval( "nCharsT"+nI+".value = " + aCharsTopico[nI] );
		}
		nTotal = SomaTotalChars( 0 );
		for (nI=1; nI<aCharsTopico.length; nI++) eval( "nCharsTT"+nI+".value = " + nTotal );
	}
}

function SomaChars(nTopico){
	with (document.ficha) {
		aInfo = CountText( eval( "T"+nTopico+".value" ) );
		aCharsTopico[nTopico] = aInfo[CARACTERES];
		eval( "nCharsT"+nTopico+".value = " + aCharsTopico[nTopico] );
		nTotal = SomaTotalChars( 0 );
		for (nI=1; nI<aCharsTopico.length; nI++) eval( "nCharsTT"+nI+".value = " + nTotal );
	}
}

function SomaTotalChars( nAvalia ){
	nCharsTotal = 0;
	with (document.ficha) {
		aTopicos = lista_topicos.value.split("|");
		aObrigatorios = obrigatorios.value.split("|");
		for (nI=1; nI<aCharsTopico.length; nI++){
			if ( aObrigatorios[nI-1] == "S" ){
				nCharsTotal += aCharsTopico[nI];
				if( nAvalia && aCharsTopico[nI]==0 ){
					alert("O Conteúdo do Tópico "+aTopicos[nI-1]+" não pode ser vazio");
					eval("T"+nI+".focus()");
					document.location="#aT"+nI;
					return 0;
				}
			}
		}
	}
	return nCharsTotal;
}

function ExtrairValor( nContato, nCampo ){
	var nJ, oField, oField2, nLen, cValue="", cChar="þ", cTipo;
	with ( document.ficha ) {
		if ( "" + eval( "typeof " + aArmazenar[nCampo][0] ) == "undefined" ) return ( "NULLß" );
		oField = eval( aArmazenar[nCampo][0] );
		cTipo = "" + oField.type;
		switch ( cTipo ) {
			case "hidden":
			case "textarea":
			case "text": cValue = aContatos[nContato][nCampo]; break;
			case "select-one":
			case "select-multiple":
				if ( aArmazenar[nCampo].length > 2 ){
					nLen = oField.length;
					for (nJ=0; nJ<nLen; nJ++) oField[nJ].selected = true;
					oField2 = eval( aArmazenar[nCampo][2] );
					transfere(oField, oField2);
					oField = oField2;
				} else cChar = "";
				nLen = aContatos[nContato][nCampo].length;
				for (nJ=0; nJ<nLen; nJ++)
					cValue += oField[aContatos[nContato][nCampo][nJ]].value + cChar;
				break;
			case "radio":
			case "checkbox": cValue = (aContatos[nContato][nCampo] ? oField.value : ""); break;
			case "undefined":
				switch ( oField[0].type ) {
					case "radio":
					   cChar = "";
					case "checkbox":
						nLen = aContatos[nContato][nCampo].length;
						for (nJ=0; nJ<nLen; nJ++)
							cValue += oField[aContatos[nContato][nCampo][nJ]].value + cChar;
						break;
				}
				break;
		}
	}
	return ( cValue + "ß" );
}

//
// Código para simular a função splice quando não existir para o Objeto ARRAY
//
function isFunction(a) {
    return typeof a == 'function';
}
if (!isFunction(Array.prototype.splice)) Array.prototype.splice = spliceMethod;
function spliceMethod(s, d) {
	var max = Math.max,
		min = Math.min,
		a = [], // The return value array
		e,  // element
		i = max(arguments.length - 2, 0),   // insert count
		k = 0,
		l = this.length,
		n,  // new length
		v,  // delta
		x;  // shift count

	s = s || 0;
	if (s < 0) s += l;
	s = max(min(s, l), 0);  // start point
	d = max(min(isNumber(d) ? d : l, l - s), 0);    // delete count
	v = i - d;
	n = l + v;
	while (k < d) {
		e = this[s + k];
		if (!isUndefined(e)) a[k] = e;
		k += 1;
	}
	x = l - s - d;
	if (v < 0) {
		k = s + i;
		while (x) {
			this[k] = this[k - v];
			k += 1;
			x -= 1;
		}
		this.length = n;
	} else if (v > 0) {
		k = 1;
		while (x) {
			this[n - k] = this[l - k];
			k += 1;
			x -= 1;
		}
	}
	for (k = 0; k < i; ++k) this[s + k] = arguments[k + 2];
   return a;
}
