

// **********************************************************************************************************************
// * 											-[Clase formatManager]- 												*
// * 																													*
// * Descripción:																										*
// *																													*
// *																													*
// * Atributos:																											*
// *	- commaDelimiter 	| Ej: "," ó "." Según el país																*		
// *	- dotDelimiter		| Ej: "." ó "," Según el país																*
// *	- dateFormat		| Igual que en php Ej: "dd/mm/yyyy" , "mm-dd-yy"											*
// *	- phoneFormat		| Ej: (##) ###-####-#####																	*
// * Métodos:																											*
// *	- setFormat(String Value, String Format);																		*
// *	- unsetFormat(String Value, String Format);																		*
// *	Formatos disponibles: date | phone | float																		*
// * Ejemplo:																											*
// *																													*
// **********************************************************************************************************************



// Class
function formatManager()
	{
	// *************
	// * Atributos *
	// *************
		this.thousandsDelimiter		= ".";
		this.decimalDelimiter		= ",";
		this.dateFormat				= "dd/mm/yyyy";
		this.phoneFormat			= "(##) ## - #### ";
		this.decimalDelimiterPlaces	= 2;
	

	// ***********
	// * Métodos *
	// ***********
	
		// setFormat
		this.setFormat = function setFormat(value,format)
			{
			// TIPOS DE FORMATO:
			//	- DATE
			//	- PHONE
			//	- FLOAT
			
			if (format != '')
				{
				switch (format) 
					{ 
					// Float
					case "float": 
						var comma			= this.thousandsDelimiter;
						var dec				= this.decimalDelimiter;
						var preDecimal		= '';
						var postDecimal		= '';
						var minus			= '';
						var decimalPlaces 	= this.decimalDelimiterPlaces;
					  
					  try 
						  {
							// Valido los datos de entrada
							if ( value.indexOf(comma) != -1 )
								{
								if (comma == ".")
									{
									value = value.replace(/[.]/g,'');
									}
								
								if (comma == ",")
									{
									value = value.replace(/[,]/g,'');
									}
								
								}
							// Detecto si hay mas de un simbolo decimal y borro los excedentes dejando presente el ultimo unicamente
							if (value.indexOf(dec) != -1)
								{
								pos = value.indexOf(dec);
								if ( value.indexOf(dec,pos+1) != -1 )
									{
									while (value.indexOf(dec) != -1)
										{
										value_ant = value;
										value = value.replace(dec,"");
										}
									value = value_ant;
									}
								}
							
							decimalPlaces = parseInt(decimalPlaces);
							
							if (decimalPlaces < 1) { dec = ''; }
							if (value.lastIndexOf("-") == 0) 
								{ 
								minus='-';
								value=value.replace(/[-]/g,'');
								}
							
							// Busco si tiene coma
							if ( value.search(',') != -1 ) 
								{
								preDecimal 	= value.substring(0,value.lastIndexOf(","));
								//alert("Predecimal: "+preDecimal);
								postDecimal = value.substring(value.lastIndexOf(",")+1,value.length);
								
								// Si hay mas de 2 decimales lo recorto a 2
								if (postDecimal.length > 2) { postDecimal = postDecimal.substring(0,2); }
								
								// Si hay menos de 2 decimales completo con 0
								if (postDecimal.length < 2) { postDecimal = postDecimal + "0"; }
								postDecimal = "," + postDecimal;
								}
							else
								{
								preDecimal 	= value;
								postDecimal = ',00';
								}
							
							// Filtra ej: 0080    000000  
							if(preDecimal.length > 1 && preDecimal.indexOf("0") == 0 ) 
								{
								var x = 0;
								while (preDecimal.charAt(x) == "0" && x != preDecimal.length-1 ) {
									x++;
									}
								preDecimal = preDecimal.substring(x,preDecimal.length);
								}							
							
							var regex  = new RegExp('(-?[0-9]+)([0-9]{3})');
							
							while(regex.test(preDecimal)) { preDecimal = preDecimal.replace(regex, '$1' + comma + '$2'); }
							
						  }
				
						catch (exception) { AlertError("Formato del número",exception); }
						if(postDecimal == ",00") { postDecimal = "";}
						valueberToReturn = minus + preDecimal + postDecimal;
						
						if (valueberToReturn == ",00") { valueberToReturn = ""; }
						return valueberToReturn;
					break 
					
					// Phone
					case "phone": 
						
					break 
					
					// Date   
					case "date": 
					// Formatea una serie de numeros al formato de fecha dd/mm/aaaa
					var tmp_value = "";
					value = value.replace(/[\/]/g,"");
					
					for (i=0; i<8; i++)
						{
						sChar = value.charAt(i);
						tmp_value = tmp_value + sChar;
						if (i == 1) { tmp_value = tmp_value + "/"; }
						if (i == 3) { tmp_value = tmp_value + "/"; }
						}
					return tmp_value;
					break 
					   
					} 
				
				} // fin format vacio
			}
		
		// unsetFormat
		this.unsetFormat = function unsetFormat(value,format)
			{
			switch (format) 
					{ 
					// Float
					case "float": 

					break 
					
					// Phone
					case "phone": 
						
					break 
					
					// Date   
					case "date": 

					break 
					} 
			}
	
	// **********************
	// * Funciones privadas *
	// **********************
	
		// Transforma un numero de su formato de presentacion al standar 1.200,40 -> 1200.40
		this.toStandarFormat = function toStandarFormat(num)
			{
			if (num != "" && typeof(num) != 'undefined' && num != null) {
				num = String(num);
				num = num.replace(/[.]/g,'');
				num = num.replace(/[,]/g,'.');
				num = Math.round(num*100)/100;
			}
			return num;
			}
		
		// Transoforma un numero standar a su formato de presentacion EJ: 1200.40 -> 1200,40 
		this.toLayoutFormat = function toLayoutFormat(num)
			{
			if (num != '' && typeof(num) != 'undefined' && num != null) {
				num = Math.round(num*100)/100;
				num = String(num);
				num = num.replace(/[.]/g,',');
				num = this.setFormat(num,'float')
			}
			return num;
			}
	
	
	}





	
