var registryCode;
var registryCodeSelecionado;
var extensionFileType;

extensionFileType = 'php';
registryCode = '00000000-0000-0000-0000-000000000000';
registryCodeSelecionado = '00000000-0000-0000-0000-000000000000';

function setCode( Code ){
	registryCode = Code;
}

function setCodeSelecionado(){
	registryCode = registryCodeSelecionado;
}

function key( name, value ){
	this.name = name;
	this.value = value;
	this.set = set;
	this.get = get;
}

function set( value ){
	this.value = value;
}

function get(){
	return this.value ;
}

function clientForm( formName, key ){
	this.formName = formName;
	this.form = eval( 'document.' + formName )
	this.key = key;
	this.keyField = eval( 'document.' + formName + '.' + this.key.name )
	this.prvtTarget = '_self';
	this.prvtAction = '';
	this.prvtMethod = 'POST';
	//this.prvtEnctype = 'text/plain';
	this.prvtEnctype = 'application/x-www-form-urlencoded';

	this.select = prvtSelect;
	this.add = prvtAdd;
	this.search = prvtSearch;
	this.cancel = prvtCancel;
	this.save = prvtSave;
	this.remove = prvtRemove;
	this.submit = prvtSubmit;

	this.setTarget = prvtSetTarget;
	this.setMethod = prvtSetMethod;
	this.setAction = prvtSetAction;
	this.setEnctype = prvtSetEnctype;
	this.validate = publicValidate;
}

function prvtSubmit( action, target, method, confirmMessage ){
	var flag = true
	this.setAction( action || this.prvtAction );
	this.setTarget( target || this.prvtTarget );

	if ( ( confirmMessage || '' ) != '' ) flag = confirm( confirmMessage );

	if ( flag ){
		if ( this.validate() ) {
			window.status = "Carregando informações. Por favor, aguarde um momento."
			if ( this.keyField != null ){
				this.keyField.value = ( this.key.get() || 0 );
			}
			this.form.method = this.prvtMethod;
			this.form.target = this.prvtTarget;
			this.form.action = this.prvtAction;
			//this.form.enctype = this.prvtEnctype;
			this.form.encoding = this.prvtEnctype;
			this.form.submit();
		}
	}
}


function prvtSetEnctype( value ){
	this.prvtEnctype = value;
	this.form.encoding  = this.prvtEnctype;
}

function prvtSetTarget( value ){
	this.prvtTarget = value;
}

function prvtSetAction( value ){
	this.prvtAction = value;
}

function prvtSetMethod( value ){
	this.prvtMethod = value;
}

function prvtSelect( action, target, method, confirmMessage ){
	this.submit( ( action || 'form.' + extensionFileType ), ( target || this.prvtTarget ),  ( method || this.prvtMethod ), confirmMessage );
}

function prvtAdd( action, target, method, confirmMessage ){
	this.key.value = 0
	this.submit( ( action || 'form.' + extensionFileType ), ( target || this.prvtTarget ),  ( method || this.prvtMethod ), confirmMessage );
}

function prvtSearch( action, target, method, confirmMessage ){
	this.submit( ( action || 'search.' + extensionFileType ), ( target || this.prvtTarget ),  ( method || this.prvtMethod ), confirmMessage );
}

function prvtCancel( action ){
	this.submit( ( action || 'cancel.' + extensionFileType ), ( target || this.prvtTarget ),  ( method || this.prvtMethod ), ( confirmMessage || '' ) );
}

function prvtSave( action, target, method, confirmMessage ){
	this.submit( ( action || 'save.' + extensionFileType ), ( target || this.prvtTarget ),  ( method || this.prvtMethod ), ( confirmMessage || '' ) );
}

function prvtRemove( action, target, method, confirmMessage ){
	this.submit( ( action || 'delete.' + extensionFileType ), ( target || this.prvtTarget ),  ( method || this.prvtMethod ), ( confirmMessage || 'Confirma a remoção deste registro?' ) );
}

function publicValidate(){
	return true;
}

function Mascara (formato, evento, objeto){
	var campo = eval (objeto);
	var keypress=(window.event)?event.keyCode:evento.which;

	if (formato=='DDMMYYYY')
	{
		separador = '/';
		conjunto1 = 2;
		conjunto2 = 5;
		if (keypress >= 48 && keypress <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador;
			}
		}else{
			window.event.keyCode = 0;
		}
	}
	if (formato=='HHMM')
	{
		separador = ':';
		conjunto1 = 2;
		conjunto2 = 5;
		if (keypress >= 48 && keypress <= 57){
			if (keypress >= 51 && campo.value.substring(0,1) != '1' && campo.value.substring(0,1) != '2' && campo.value.substring(0,1) != '0')
			{
				campo.value = '0' + campo.value;
			}
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value;
			}
		}else{
			window.event.keyCode = 0;
		}
	}
	if (formato == '000-000.000.000-000')
	{
		separador1 = '-';
		separador2 = '.';
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
		conjunto4 = 15;
		if (keypress >= 48 && keypress <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador2;
			}
			if (campo.value.length == conjunto3){
				campo.value = campo.value + separador2;
			}
			if (campo.value.length == conjunto4){
				campo.value = campo.value + separador1;
			}
		}else{
			window.event.keyCode = 0;
		}
	}
}


function LimitaCampo( tamanho, objeto ){
	var campo = eval (objeto);
	if ( campo.value.length > tamanho )
	{
		campo.value = campo.value.substr(0,20);
	}
}


function inSet(myTag){
	if(myTag.toLowerCase() == "input")return true;
	else if(myTag.toLowerCase() == "select")return true;
	else if(myTag.toLowerCase() == "textarea")return true;
	else if(myTag.toLowerCase() == "button")return true;
	return false;
}

function tabEnter( e ){
	var controlKey;
	var objEvent = arguments[0] ? arguments[0] : false;
	if(window.event)
	{
		controlKey = window.event.keyCode;     //IE
	}
	else
	{
		controlKey = e.which;     //firefox
	}
	if(controlKey == 13)
	{
		var srcElement = document.all ? objEvent.srcElement : objEvent.currentTarget;
		var nextList;
		var x = 0;
		var found = 0;
		for(i = 0;i < srcElement.form.elements.length;i++)
		{
			var ok = inSet(srcElement.form.elements[i].tagName);
			if(srcElement.form.elements[i] == srcElement)found = 1;
			else if(found && !srcElement.form.elements[i].disabled && ok && (srcElement.form.elements[i].type ? srcElement.form.elements[i].type.toLowerCase() != "hidden" : true) && !nextList){
				nextList = srcElement.form.elements[i];x = i;
				var displayMode = nextList.style.display;
				if(displayMode.toLowerCase() == "none" || displayMode.toLowerCase() == "hidden" || displayMode.toLowerCase() == "hide")
				{
					nextList    = null;
					x           = 0;
				}
			}
		}

		if(objEvent.stopPropagation)objEvent.stopPropagation();
		srcElement.blur();
		if(nextList){nextList.unableEnter = 1;nextList.focus();}
	}
}

