/*************************************************************************************************************************************
	LEFT 	- string.left(cantidad_caracteres) 	- devuelve los caracteres de la izquierda de string
	RIGHT	- string.right(cantidad_caractees) 	- devuelve los caracteres de la derecha de string
	TRIM	- string.trim()						- devuelve la cadena sin los espacios del principio o del final
												  Si se la llama con trim('left´) elimina los espacios de la izquierda
												  Si se la llama con trim('right´) elimina los espacios de la derecha
												  Si se la llama con trim(),sin argumentos, o trim('both'), elimina los espacios 
												  de la izquierda y de la derecha
	TOTITLECASE - string.toTitleCase()			- Pone la primera letra de cada palabra en mayúsculas
	REPLACE	- string.replace(find_string, replace_string)
												- Reemplaza en el string todas las apariciones de la cadena buscada por la sustituta.
	FORMATEO DE UN NÚMERO - format_number(number, decimals_qty) - Devuelve un string con el número con los puntos cada 3 dígitos y
												  la cantidad de decdimales que dice decimals_qty
												  
	OPCION de UN RADIOBUTTON ELEGIDA - 
				radio_check_index(radiobutton)	- Devuelve el número de opción de un radiobutton que esta seleccionada. Si no hay
												  ninguna opción seleccionada devuelve -1. Si esta seleccionada la primera devuelve 0
												  Si esta seleccionada la segunda devuelve 1, si eta seleccionada la tercera 
												  devuelve 2, etc etc.
												  Se llama así: onclick="alert (radio_check_index(document.form1.radio1));"
												  
	VALOR de UN RADIOBUTTON ELEGIDA - 
				radio_check_value(radiobutton)	- Devuelve el valor de la opción clickeada de un radiobutton. Es igual a la funcion
												  radio_check_value pero devuelve el valor de la opcion. Si no hay ninguna opción
												  seleccionada devuelve el string vacio (""). 
												  Se llama así: onclick="alert (radio_check_index(document.form1.radio1));"
	FECHAREVESAAAAMMDD - DA VUELTA UNA FECHA
				Recibe una fecha en formato AAAA-MM-DD y la transforma en DD-MM-AAAA
				
	writeFlash - Muestra un flash
													(theSRC,theWidth,theHeight,theVersion,theFlashvars)
													
	ValidarEmail
	
	getObjY          Posicion X de un Objeto
	getObjX          Posicion Y de un Objeto
	getWindowHeight  Alto de la pagina
	validarFecha (fecha)  La fecha viene en string en formato dd-mm-yyyy
**************************************************************************************************************************************/
function extract_left(total_chars) {
    return this.substring(0, total_chars)
}

String.prototype.left = extract_left

// *********************************************************************************************** //

function extract_right(total_chars) {
    return this.substring(this.length - total_chars)
}

String.prototype.right = extract_right

// *********************************************************************************************** //

function trim_spaces(from_where) {
    
    // Store the string in a temporary variable    
    var temp_string = this

    // If no argument, then trim from both sides
    if (arguments.length == 0) {
        from_where = "BOTH"
    }
    
    // Trim spaces from the left
    if (from_where.toUpperCase() == "LEFT" || from_where == "BOTH") {
        while (temp_string.left(1) == " ") {
            temp_string = temp_string.substring(1)
        }
    }
    
    // Trim spaces from the right
    if (from_where.toUpperCase() == "RIGHT" || from_where == "BOTH") {
        while (temp_string.right(1) == " ") {
            temp_string = temp_string.substring(0, temp_string.length - 2)
        }
    }
    return temp_string   

}

String.prototype.trim = trim_spaces

// *********************************************************************************************** //
function title_case() {
    var temp_string = this.toLowerCase()
    var left_string
    var right_string
    var new_letter
    
    // Make the first character uppercase
    var first_char = temp_string.left(1).toUpperCase()
    temp_string = first_char + temp_string.substring(1)
    
    // Get the position of the first space
    var space_location = temp_string.indexOf(" ")
    
    // Loop until there are no more spaces
    while (space_location != -1) {
        
        // Get the part up to and including the current space
        left_string = temp_string.left(space_location + 1)
        
        // Get the first letter of the next word and convert it to uppercase
        new_letter = temp_string.charAt(space_location + 1).toUpperCase()
        
        // Get the rest of the string after that letter
        right_string = temp_string.right(temp_string.length - space_location - 2)
        
        // Put it all together
        temp_string = left_string + new_letter + right_string
        
        // Get the next space
        space_location = temp_string.indexOf(" ", space_location + 1)
    }
    return temp_string   

}

String.prototype.toTitleCase = title_case

// *********************************************************************************************** //

function replace_string(find_string, replace_string) {

    var temp_string = this
    var left_string
    var right_string
    var start_location
    
    // Get the position of the first instance of find_string
    var replace_location = temp_string.indexOf(find_string)
    
    // Loop until there are no more instances
    while (replace_location != -1) {
        
        // Get the part up to find_string
        left_string = temp_string.left(replace_location)
        
        // Get the part after find_string
        right_string = temp_string.substring(replace_location + find_string.length)
        
        // Put it all together
        temp_string = left_string + replace_string + right_string
        
        // Make sure the next search begins after the replacement
        start_location = replace_location + replace_string.length
        
        // Get the next instance
        replace_location = temp_string.indexOf(find_string, start_location)
    }
    return temp_string   

}
String.prototype.replace = replace_string

// *********************************************************************************************** //

function format_number(number, decimals_qty) {

	var number_string = Math.abs(number).toString();
	var point_position = number_string.indexOf(".");
	if (point_position != -1) {
		var integer = number_string.left(point_position);
		var decimals = number_string.right(number_string.length - point_position - 1);
		} else {
		var integer = number_string;
		var decimals = "0";
	}
//	alert("integer="+integer);
//	alert("decimales="+decimals);
	// Primero la parte entera

    if (integer.length >= 3) {
        
        var insert_position;

        // Calculate the position of the first comma
        switch (integer.length % 3) {
            case 1 :
                insert_position = 1;
                break;
            case 2 :
                insert_position = 2;
                break;
            case 0 :
                insert_position = 3;
                break;
        }
        while (insert_position < integer.length) {
            integer = integer.left(insert_position) + "." + 
                            integer.substring(insert_position)
            insert_position += 4
        }
		
        // If the original number was negative, tack on the minus sign
        if (number < 0) {
            integer = "-" + integer
        }
    }
	
	// Ahora los decimales
	//document.write ("numero= " + integer + "  decimales: " + decimals + "  decimals_qty= " + decimals_qty);
	if (decimals_qty >= decimals.length) {
		decimals = decimals + "000000000000000000000000000".left(decimals_qty - decimals.length);
		} else {
		decimals = decimals.left(decimals_qty);
	}

	if (decimals == "") {
		return integer;
		} else {
		return integer + "." + decimals;
	}

}

// *********************************************************************************************** //

function radio_check_index(radio_c) {

    for (var i=0;i<radio_c.length;i++){ 
       	if (radio_c[i].checked) {
          	break; 
    	} 
	}
	if (i == radio_c.length) {
		return -1;
		} else {
		return i;
	}
}

// *********************************************************************************************** //

function radio_check_value(radio_c) {

    for (var i=0;i<radio_c.length;i++){ 
       	if (radio_c[i].checked) {
          	break; 
    	} 
	}
	if (i == radio_c.length) {
		return "";
		} else {
		return radio_c[i].value;
	}
}
// *********************************************************************************************** //
function fecharevesaaaammdd (aaaammdd) {
	var separador = aaaammdd.substring(4, 1);
	return aaaammdd.right(2)+separador+aaaammdd.substring(5, 2)+separador+aaaammdd.left(4);
}
// *********************************************************************************************** //
function writeFlash(theSRC,theWidth,theHeight,theVersion,theFlashvars)
{
	document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+theVersion+',0,0,0" WIDTH="'+theWidth+'" HEIGHT="'+theHeight+'">'+
		 '<PARAM NAME=movie VALUE="'+theSRC+'">'+
		'<PARAM NAME=flashvars VALUE="'+theFlashvars+'">'+
		'<PARAM NAME=wmode VALUE="transparent">'+
		 '<PARAM NAME=quality VALUE=high>'+
		 '<EMBED src="'+theSRC+'" quality=high WIDTH="'+theWidth+'" HEIGHT="'+theHeight+'" flashvars="'+theFlashvars+'" wmode="transparent" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">'+
		 '</EMBED>'+
	'</OBJECT>');
}
// *********************************************************************************************** //
function validarEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)) {
		return (true)
	} else {
		return (false);
	}
}

//************************************************************************************************//
	function getObjY (obj) { // Posicion X de un objeto
		if(document.layers) {
			return obj.y;
		} else {
			var tmpY=0;
			while(obj) {
				 tmpY += obj.offsetTop;
				 obj=obj.offsetParent;
			}
			return tmpY;
		}
	}

//************************************************************************************************//

function getObjX (obj) { // posicion Y del objeto
		if(document.layers) {
			return obj.x;
		} else {
			var tmpX=0;
			while(obj) {
				tmpX += obj.offsetLeft;
				obj=obj.offsetParent;
			}
			return tmpX;
		}
	}

//************************************************************************************************//

	function getWindowHeight() { // Alto de la pagina
		return document.body.scrollHeight;
	}
//*************************************************************************************************//
	function validarFecha(cadena) {
		
		var ultimo_dia = 0;
		if (cadena == "") {
			return false;	
		}
		if (cadena.length != 10) {
			return false;
		}
		var dato=cadena.split("-");
		if (!dato[0] || !dato[1] || !dato[2]) {
			return false;
		}
		if (dato[0] == "" || dato[1] == "" || dato[2] == "") {
			return false;
		}
		if (isNaN(dato[0]) || isNaN(dato[1]) || isNaN(dato[2]))  {
			return false;
		}
		if (dato[0].length !=2 || dato[1].length !=2 || dato[2].length !=4) {
			return false;	
		}
		var dia = parseInt(dato[0], 10);
		var mes = parseInt(dato[1], 10);
		var anio = parseInt(dato[2], 10);
		if (dia < 1 || mes < 1 || mes > 12 || anio < 1900 || anio > 2020) {
			return false;	
		}
		var cant_dias = new Array();
		cant_dias[1] = 31;
		cant_dias[2] = 28;
		cant_dias[3] = 31;
		cant_dias[4] = 30;
		cant_dias[5] = 31;
		cant_dias[6] = 30;
		cant_dias[7] = 31;
		cant_dias[8] = 31;
		cant_dias[9] = 30;
		cant_dias[10] = 31;
		cant_dias[11] = 30;
		cant_dias[12] = 31;
		if (mes = 2 && anio % 4 == 0) {
			ultimo_dia = 29;	
		} else {
			ultimo_dia = cant_dias[mes];
		}
		if (dia > ultimo_dia) {
			return false;	
		}
		return true;
	}
