ccForm = {
	
	init: function(){
		var campos = document.getElementsByTagName("input");
		if(campos.length == 0){
			return false;
		}
		else{
			for(var i=0; i<campos.length; i++){
				//alert(campos[i].getAttribute("name"));
				if(campos[i].getAttribute("name") && campos[i].getAttribute("name").substring(0,3) == 'AC_'){
					
					campos[i].onkeydown = function(){
						var ac_id = ccForm.ciddIndex(this);
						if(document.getElementById('UF_'+ac_id).value == ''){
							alert('Selecione a UF da cidade');
							document.getElementById('UF_'+ac_id).focus();
						}
					}
					campos[i].onblur = function(){
						var ac_id = ccForm.ciddIndex(this);
						if(this.value != document.getElementById('CP_'+ac_id).value){
							document.getElementById(ac_id).value = '';
							document.getElementById('CP_'+ac_id).value = '';
						}
					}
					var ac_id = ccForm.ciddIndex(campos[i]);
					document.getElementById('UF_'+ac_id).onchange = function(){
						var ac_id = ccForm.ciddIndex(this);
						document.getElementById('AC_'+ac_id).value = '';
						document.getElementById(ac_id).value = '';
						document.getElementById('CP_'+ac_id).value = '';
					}
				}

				if(/required/.test(campos[i].className)){
					/*
					// É preciso verificar a situação de quando já existe um evento deste tipo no campo
					// Desta forma ele simplesmente da um replace. neste caso tirei por causa do combo de
					// cidade mas devo implementar aqui tbm e corrigir a situação
					var strClass = campos[i].className;
					campos[i].onfocus = function(){			
						this.className = "";	
					}
					campos[i].onblur = function(){			
						if(this.value == ""){
							this.className = strClass;
						};	
					}
					*/
				}

				if(/mask_number/.test(campos[i].className)){
					campos[i].onkeypress = function(e){			
						e = ccEvent.formatEvent(e);
						ccForm.maskNumber(e);			
					}
					campos[i].onblur = function(e){			
						ccForm.clearField(this, '');			
					}
				}
				else if(/mask_fone/.test(campos[i].className)){
					var ddd_fone = document.getElementById('ddd_'+campos[i].getAttribute("id"));					
					ddd_fone.onkeypress = function(e){			
						e = ccEvent.formatEvent(e);							
						ccForm.maskNumber(e);
					}	
					ddd_fone.onkeyup = function(e){			
						if(this.value.length == 2){
							// por alguma razão não funcionou a var fone
							var next_field = this.name.substr(4,this.name.length);
							document.getElementById(next_field).focus();
						}						
					}	
					ddd_fone.onblur = function(e){			
						ccForm.clearField(this, '');			
					}
					campos[i].onkeypress = function(e){			
						e = ccEvent.formatEvent(e);						
						ccForm.maskNumber(e);						
					}
					campos[i].onblur = function(e){			
						ccForm.clearField(this, '');			
					}

				}
				else if(/mask_date/.test(campos[i].className)){
					campos[i].onkeypress = function(e){			
						e = ccEvent.formatEvent(e);
						ccForm.maskDate(e);			
					}
					campos[i].onblur = function(e){			
						ccForm.clearField(this, '/');			
					}
				}
				else if(/mask_hour/.test(campos[i].className)){
					campos[i].onkeypress = function(e){			
						e = ccEvent.formatEvent(e);
						ccForm.maskHour(e);			
					}
					campos[i].onblur = function(e){			
						ccForm.clearField(this, ':');			
					}
				}
				else if(/mask_cep/.test(campos[i].className)){
					campos[i].onkeypress = function(e){			
						e = ccEvent.formatEvent(e);
						ccForm.maskCep(e);			
					}
					campos[i].onblur = function(e){			
						ccForm.clearField(this, '-');			
					}
				}
				else if(/mask_real/.test(campos[i].className)){
					campos[i].onkeypress = function(e){			
						e = ccEvent.formatEvent(e);
						ccForm.maskNumber(e, 'real');			
					}
					campos[i].onblur = function(e){			
						ccForm.clearField(this, ':.,');			
					}
				}
				else if(/mask_currency/.test(campos[i].className)){
					campos[i].onkeypress = function(e){			
						e = ccEvent.formatEvent(e);
						ccForm.maskNumber(e, this);			
					}
					campos[i].onkeydown = function(e){			
						e = ccEvent.formatEvent(e);
						ccForm.maskCurrency(e);			
					}
					campos[i].onblur = function(e){			
						ccForm.clearField(this, '.,');			
					}
				}	
				else if(/combo_search/.test(campos[i].className)){
					campos[i].onkeyup = function(e){		
						var searched_id = this.name.replace('search_', '');
						oComboSearched = document.getElementById(searched_id);
						ccForm.comboSearch(this, oComboSearched);		
					}
				}			

			}			
		}
			
	},

	ciddIndex: function(field){
		return field.getAttribute("name").substring(3,field.getAttribute("name").length);
	},
	
	comboSearch: function(field, oComboSearched){
		sSearch = field.value.toUpperCase();
		sSearch.cc_remove_acentos();
		iSearchLength = sSearch.length;
		for (j=0; j < oComboSearched.options.length; j++){
			sOptionText = oComboSearched.options[j].text;
			sOptionComp = sOptionText.substr(0, iSearchLength).toUpperCase();
			if(sSearch == sOptionComp){
				oComboSearched.selectedIndex = j;
				break;
			}
		}		
	},
	
	clearField: function(field, sValid){
		
		var sField = field.value;		
		var sValid = '1234567890'+sValid;
		var sClean = '';
		var iLength = sField.length;

		for(var i=0; i<iLength; i++) {
			var str = sField.substr(i,1);
			if(sValid.indexOf(str) > -1){
				sClean += str;
			}
		}
		field.value = sClean;

	},
	
	maskCurrency: function(oEvent){	
		
		// ta meia boca mas funciona
		
		var campo = ccEvent.getSource(oEvent);
		var tammax = campo.getAttribute("maxlength") - 1;
		var tecla = oEvent.keyCode;

		vr = campo.value;
		vr = vr.replace( "/", "" );
		vr = vr.replace( "/", "" );
		vr = vr.replace( ",", "" );
		vr = vr.replace( ".", "" );
		vr = vr.replace( ".", "" );
		vr = vr.replace( ".", "" );
		vr = vr.replace( ".", "" );
		tam = vr.length;

		if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

		if (tecla == 8 ){	tam = tam - 1 ; }		
			
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
			if ( tam <= 2 ){ 
				campo.value = vr ; }
			if ( (tam > 2) && (tam <= 5) ){
				campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 6) && (tam <= 8) ){
				campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 9) && (tam <= 11) ){
				campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 12) && (tam <= 14) ){
				campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
			if ( (tam >= 15) && (tam <= 17) ){
				campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
		}	

	},

	maskDate: function (oEvent) {
		// onkeypress		
		var campo = ccEvent.getSource(oEvent);
		var sValidChars = '1234567890/';
		var sChar = String.fromCharCode(oEvent.charCode);
		if(sValidChars.indexOf(sChar) == -1){
			// Se não for firefox não precisa verificar se são teclas de controle
			if(window.event){
				oEvent.preventDefault();
			}
			else{
				if(!ccEvent.keyControls(oEvent)){	
					oEvent.preventDefault();
				}
			}
		}
		else{
			if(!ccEvent.keyControls(oEvent)){
				if(campo.value.length == 2 || campo.value.length == 5){
					if(campo.value.length == 2 && sChar != '/'){
						campo.value = campo.value+'/';
					}
					else if(campo.value.length == 5 && sChar != '/'){
						campo.value = campo.value+'/';
					}
				}
				else if(sChar == '/'){
					oEvent.preventDefault();
				}
			}
		}		
	},

	maskHour: function (oEvent) {
		// onkeypress		
		var campo = ccEvent.getSource(oEvent);
		var sValidChars = '1234567890:';
		var sChar = String.fromCharCode(oEvent.charCode);
		if(sValidChars.indexOf(sChar) == -1){
			// Se não for firefox não precisa verificar se são teclas de controle
			if(window.event){
				oEvent.preventDefault();
			}
			else{
				if(!ccEvent.keyControls(oEvent)){	
					oEvent.preventDefault();
				}
			}
		}
		else{
			if(!ccEvent.keyControls(oEvent)){
				if(campo.value.length == 2){
					if(campo.value.length == 2 && sChar != ':'){
						campo.value = campo.value+':';
					}
				}
				else if(sChar == ':'){
					oEvent.preventDefault();
				}
			}
		}		
	},

	maskCep: function (oEvent) {
		// onkeypress		
		var campo = ccEvent.getSource(oEvent);
		var sValidChars = '1234567890-';
		var sChar = String.fromCharCode(oEvent.charCode);
		if(sValidChars.indexOf(sChar) == -1){
			// Se não for firefox não precisa verificar se são teclas de controle
			if(window.event){
				oEvent.preventDefault();
			}
			else{
				if(!ccEvent.keyControls(oEvent)){	
					oEvent.preventDefault();
				}
			}
		}
		else{
			if(!ccEvent.keyControls(oEvent)){
				if(campo.value.length == 5){
					if(campo.value.length == 5 && sChar != '-'){
						campo.value = campo.value+'-';
					}
				}
				else if(sChar == '-'){
					oEvent.preventDefault();
				}
			}
		}		
	},
	
	maskNumber: function (oEvent, sType) {
		//alert('hei');
		// onkeypress	
		var campo = ccEvent.getSource(oEvent);
		if(sType == 'real'){
			var sValidChars = '1234567890.,';
		}
		else{
			var sValidChars = '1234567890';
		}
		var sChar = String.fromCharCode(oEvent.charCode);
		if(sValidChars.indexOf(sChar) == -1){
			// Se não for firefox não precisa verificar se são teclas de controle
			if(window.event){
				oEvent.preventDefault();
			}
			else{
				if(!ccEvent.keyControls(oEvent)){	
					oEvent.preventDefault();
				}
			}
		}
	},

	jumpField: function(campo1, campo2, tamanho){
		if(campo1.value.length == tamanho){
			campo2.focus();
		}
	},

	focusOnFirst: function () {
		var aInput = document.getElementsByTagName("input"); 
		if(aInput.length > 0){
			for(var i=0; i<aInput.length; i++){
				if (aInput[i].type != "hidden" && 
                    aInput[i].type != "checkbox" && 
                    aInput[i].type != "radio" && 
                    aInput[i].type != "submit" && 
                    aInput[i].type != "button" && 
                    aInput[i].type != "file" && 
                    aInput[i].type != undefined){
					if(!(new RegExp("SearchInput").test(aInput[i].name))){
						aInput[i].focus();
						return;
					}
				}				
			}
		}
	}

}
ccEvent.addEvent(window, 'load', ccForm.focusOnFirst);
ccEvent.addEvent(window, 'load', ccForm.init);