//Ubicar una pagina
function Ubicar(Dir)
{
	window.location(Dir);
}

// Detects wether we are using netscape or ie

//

var ie4 = (document.all) ? true : false;

var ns4 = !ie4;



// Stores MonthNames

//

var MonthNames = new Array;

MonthNames[0]  = 'Enero';

MonthNames[1]  = 'Febrero';

MonthNames[2]  = 'Marzo';

MonthNames[3]  = 'Abril';

MonthNames[4]  = 'Mayo';

MonthNames[5]  = 'Junio';

MonthNames[6]  = 'Julio';

MonthNames[7]  = 'Agosto';

MonthNames[8]  = 'Setiembre';

MonthNames[9]  = 'Octubre';

MonthNames[10] = 'Noviembre';

MonthNames[11] = 'Diciembre';





// Returns a month name as a spanish string

//

function GetMonthName(dateObject)

{

	return MonthNames[GetObject(dateObject).getMonth()];

}



// Checks to see if client is IE5

//

function IsIE5()

{

	var ie4 = (document.all) ? true : false;

	if(ie4) {

		return navigator.userAgent.indexOf('MSIE 5') > 0;

	}

	else {

		return false;

	}

}



// Go back in history

//

function GoBack()

{

	history.back();

}



// Go forward in history

//

function GoForward()

{

	history.forward();

}



// Default parameter

//

function DefaultParam(param, value)

{

	if(!IsDefined(param)) {

		param = value;

	}

	return param;

}



// Rect constructor

//

function Rect(left, top, right, bottom)

{

	this.left 	= left;

	this.top  	= top;

	this.right  = right;

	this.bottom = bottom;

}



// Sets attributes for an object or objects

// Note: If a string value is needed for attribute, it's

// necesary to escape it with quotes.

//

function SetObjectAttribute(object, attribute, newValue)

{

	var objects = GetObjectsArray(object);

	for(var i = 0; i < objects.length; i++) {

		eval("objects[i]." + attribute + " = " + newValue + ";");

	}

}



// If object it's a string then

// returns the document children that

// matches, else, it returns object unchanged.

//

function GetObject(object, parent)

{

	if(typeof(parent) == "undefined") {

		parent = document;

	}

	if(typeof(object) == "string") {

		if(ns4) {

			return eval("document." + object);

		}

		else {

			return parent.all[object];

		}

	}

	else {

		return object;

	}

}



// Returns true if argument is a defined object

//

function IsDefined(object)

{

	return (typeof(GetObject(object)) != "undefined");

}



// Returns an array of objects. If the given

// object it's not an array, then it is put

// on one, so we can use an array uniformly.

// If tagName is specified, then we filter the

// array and add objects only if they have that tagName

//

function GetObjectsArray(object, tagName)

{



	var objectsArray = new Array();

	object 					 = GetObject(object); // So when we're given a name we can still work

	tagName 				 = DefaultParam(tagName, "");



	if(tagName == "") {

//		if(typeof object.length == "undefined") {
		if(typeof object == "undefined") {

			objectsArray[0] = object;

		}

		else {

			for(var i = 0; i < object.length; i++) {

				objectsArray[i] = object[i];

			}

		}

	}

	else {

		if(typeof(object.length) == "undefined") {

			if(object.tagName == tagName) {

				objectsArray[0] = object;

			}

		}

		else {

			var j = 0;

			for(var i = 0; i < object.length; i++) {

				if(object[i].tagName == tagName) {

					objectsArray[j] = object[i];

					j++;

				}

			}

		}

	}

	return objectsArray;

}



// Changes style of object according to showIt

//

function ShowObject(object, showIt)

{

	// Default for showIt is true

	//

	showIt = DefaultParam(showIt, false);



	// object could be more than one

	//

	var objects = GetObjectsArray(object);



	if(ns4) {
		for(var i = 0; i < objects.length; i++) {


			if(showIt) {

				// Change style so this item

				// will be displayed

				//

				objects[i].visibility = "show"

			}

			else {

				objects[i].visibility = "hide"

			}

		}

	}

	else { 
		for(var i = 0; i < objects.length; i++) {

			if(showIt) {

				// Change style so this item

				// will be displayed

				//

				objects[i].style.display = ""

			}

			else {

				// Change style so this item

				// will be hidden

				//

				objects[i].style.display = "none"

			}

		}

	 }

}



// Moves fromObject to the location of toObject

// fromObject it's assumed to have an absolute

// position.

//

function MoveObject(fromObject, toObject)

{

	var fromObj = GetObject(fromObject);

	var toObj		= GetObject(toObject);

	// Get absolute positions

	//

	var toPos  	= GetObjectAbsPos(toObj);



	// Set the new position of fromObj

	//

	fromObj.style.posLeft = toPos.left;

	fromObj.style.posTop  = toPos.top;

}



// Returns a rect with the absolute

// position of object

//

function GetObjectAbsPos(object)

{

	var parentObj = GetObject(object);

	var newLeft   = 0;

	var newTop		= 0;



	// Traverse through parents, to get absolute

	// positions

	//

	while(typeof(parentObj) != "undefined" && parentObj != null) {

		switch(parentObj.tagName) {

			case "TD":

			case "TH":

				// Skip this tags cause their heights

				// are given by TRs

				//

				break;

			default:

				newTop = newTop  + parentObj.offsetTop;

				break;

		}

		newLeft 	= newLeft + parentObj.offsetLeft;

		parentObj = parentObj.parentElement;

	}

	return new Rect(newLeft, newTop, 0, 0);

}



// Preloads images in an arrray

// First argument it's root directory,

// next are image paths

//

function PreloadImages(rootPath)

{

	if(arguments.length > 1) {

		var images = new Array;

		for(i = 1; i < arguments.length; i++) {

			images[i - 1] 		= new Image;

			images[i - 1].src = rootPath + arguments[i];

		}

	}

}



// Loop thorugh a colection of checkboxes and

// set an input with a comma delimited string

// with it's values

//

function SetSelected(formObject,  checkBoxObject,

										 inputObject, putZeroFirst,

										 separator)

{

	var form 		 = GetObject(formObject);

	var input    = GetObject(inputObject);

	var checkBox = GetObjectsArray(checkBoxObject);



	// Don't put zero first by default

	//

	if(typeof(putZeroFirst) == "undefined") {

		putZeroFirst = false;

	}

	// Put a comma as defualt separator

	//

	if(typeof(separator) == "undefined") {

		separator = ",";

	}



	// We've a collection of boxes.

	//

	if(putZeroFirst) {

		// By default, add a zero.

		//

		input.value = '0';

	}



	for(var i = 0; i < checkBox.length; i++) {

		if(checkBox[i].status) {

			// Put a comma only if there

			// are values in input

			//

			if(input.value != "") {

				input.value = input.value + separator;

			}

			// Add this checkBox's value to the

			// list in input

			//

			input.value = input.value + checkBox[i].value;

		}

	}

}



// Reset checkboxes

//

function ResetCheckBoxes(formObject, checkBoxObject, newStatus)

{

	var form 		 = GetObject(formObject);

	var checkBox = GetObject(checkBoxObject);



	if(typeof(checkBox.length) == "undefined") {

		// We only have one checkbox, so we

		// set the input with its value

		//

		checkBox.status = newStatus;

	}

	else {

		// We've a collection of boxes.

		//

		for(var i = 0; i < checkBox.length; i++) {

			checkBox[i].status = newStatus;

		}

	}

}



// Sets status of all checkBoxes that are named as inputName

// and have as value inputValue to newStatus

//

function SetCheckBoxes(formObject, checkBoxObject, checkBoxValue, newStatus)

{

	var form 		 = GetObject(formObject);

	var checkBox = GetObject(checkBoxObject);



	if(typeof(checkBox.length) == "undefined") {

		// We only have one checkbox, so we

		// set the input with its value

		//

		if(checkBox.value == checkBoxValue) {

			checkBox.status = newStatus;

		}

	}

	else {

		// We've a collection of boxes.

		//

		for(var i = 0; i < checkBox.length; i++) {

			if(checkBox[i].value == checkBoxValue) {

				checkBox[i].status = newStatus;

			}

		}

	}

}



// Returns the substring that represents

// the ID of a node

//

function GetNodeID(nodeName)

{

	return nodeName.substring(nodeName.indexOf('_', 0) + 1, nodeName.length);

}



// Returns true if parentName is parent of

// nodeName

//

function IsParent(parentName, nodeName)

{

	return nodeName.substring(0, parentName.length) == parentName &&

				 nodeName.substring(parentName.length, parentName.length + 1) == '_'

}



// Returns true if given parentName in

// given table, has children

//

function HasChildren(tableName, parentName)

{

  var curRow;

  var table = document.all[tableName];



	// Loop trough rows, returns true

	// if children found

	//

  for(var i = 0; i < table.rows.length; i++) {

  	curRow = table.rows[i];

    if(IsParent(parentName, curRow.id)) {

    	 return true;

    }

  }

}



// Changes image and function used

// to toggle from collapsed to expanded

// views

//

function ChangeToggler(tableName, parentName, type)

{

	var newTogglerImage;

	if(typeof(document.images["Toggler" + parentName]) != "undefined") {

		if(type == 'Collapse') {

		  newTogglerImage = "Minus.gif";

		}

		else {

		  newTogglerImage = "Plus.gif";

		}

		document.images["Toggler" + parentName].src = "/Common/Images/UI/" + newTogglerImage;

		document.all["TogglerAction" + parentName] = "javascript:" + type + "Node('" + tableName + "', '" + parentName + "');";

	}

}



// Sets togglers for all the childrens

// of a given parent

//

function SetTogglers(tableName)

{

  var curRow;

  var table = document.all[tableName];



	// Loop trough rows, expanding or collapsing

	//

  for(var i = 0; i < table.rows.length; i++) {

  	curRow = table.rows[i];



		// Set current toggler only if node

		// has childrens

		//

    if(HasChildren(tableName, GetNodeID(curRow.id))) {

			// Set 'Expand' toggler

    	//

    	ChangeToggler(tableName, GetNodeID(curRow.id), 'Expand');

    }

    else if(typeof(document.all["TogglerAction" + GetNodeID(curRow.id)]) != "undefined") {

			// Change anchor and set an empty space

			// in its place

    	//

			document.all["TogglerAction" + GetNodeID(curRow.id)].outerHTML = "<img src = '/Common/Images/UI/Empty.gif' align = 'middle' border = '0' width = '9' height = '9'>";

    }

  }

}



// Given a table name, and a parent name

// this shows all rows in that table that

// whose id match level

//

function ExpandNode(tableName, parentName)

{

  var curRow;

  var table = document.all[tableName];



  if(typeof(changeTogglers) == "undefined") {

  	changeTogglers = true;

 	}



	// Loop trough rows, expanding or collapsing

	//

  for(var i = 0; i < table.rows.length; i++) {

  	curRow = table.rows[i];



    if(IsParent(parentName, curRow.id)) {

			// Expand node.

    	//

      curRow.style.display = "block";

	    ChangeToggler(tableName, parentName, "Collapse");

    }

  }

}



// Collapses a given parent node

//

function CollapseNode(tableName, parentName)

{

  var curRow;

  var table = document.all[tableName];



	// Loop trough rows, expanding or collapsing

	//

  for(var i = 0; i < table.rows.length; i++) {

  	curRow = table.rows[i];



    if(IsParent(parentName, curRow.id)) {

			// Collapse childrens of this node

    	//

    	CollapseNode(tableName, GetNodeID(curRow.id))

    	table.rows[i].style.display = "none";

    	ChangeToggler(tableName, parentName, "Expand");

    }

  }

}



// Returns true if regular expressions

// are supported

//

// Borrowed from www.webreference.com

//

function IsRegExpSupported()

{

  if(window.RegExp) {

    var tmpStr = "a";

    var tmpReg = new RegExp(tmpStr);

    if(tmpReg.test(tmpStr)) {

    	return true;

    }

  }

  return false;

}



function CheckEmailInForm(form, input, value)

{

	return CheckEmail(value);

}



// Returns true if email is a valid

// string representing an email address

// (obiously doesn't check existence)

//

// Borrowed from www.webreference.com

//

function CheckEmail(str) {

  if(IsRegExpSupported()) {

  	// Validate through a regular expression

  	//

	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");

	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");

	  return (!r1.test(str) && r2.test(str));

  }

  else {

  	// Validate in a simpler way

  	//

    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);

	}

}



// Returns a RUC number

//

function GetRUCNumber(value)

{

	// Loop thorugh characters, skip

	// spaces, hyphens and points, and stop

	// if other character it's encountered

	//

	var numbers 	 	 = "0123456789";

	var allowedChars = ".- " + numbers;

	var ruc 			 	 = "";



	for(var i = 0; i < value.length; i++) {

		// Add only numbers to the ruc value

		//

		if(allowedChars.indexOf(value.charAt(i)) >= 0) {

			// We have an allowed char

			//

			if(numbers.indexOf(value.charAt(i)) >= 0) {

				// We have a number

				//

				ruc += value.charAt(i);

			}

		}

		else {

			// We've encountered an invalid char

			//

			return null;

		}

	}

	return ruc;

}





// Returns true if a number it's formatted

// as a RUC number (uruguayan commerce ID)

//

function CheckRUCNumber(value)

{

	var ruc = GetRUCNumber(value);

	if(ruc != null) {

		return ruc.length == 12;

	}

	else {

		return false;

	}

}



function CheckRUCNumberInForm(form, input, value)

{

	return CheckRUCNumber(value);

}





// Following functions were grabbed

// from ColdFusion, and formatted.

// Copyright remains theirs.

//



// Returns true if date is between two

// dates

//

function CheckDateRange(value, minDate, maxDate)

{

	var checkedDate 	 = CheckDate(value);

	var checkedMinDate = CheckDate(minDate);

	var checkedMaxDate = CheckDate(maxDate);

	// If checkedDate isn't boolean, then

	// it's of Date type

	//

	if(typeof(checkedDate) != "boolean") {

		if(checkedDate >= checkedMinDate && checkedDate <= checkedMaxDate) {

			return true;

		}

	}

	return false;

}



// Returns true if value is a eurodate format or is NULL

// otherwise returns false

//

function CheckDate(value, allowEmpty)

{

	allowEmpty = DefaultParam(allowEmpty, false);



  if(value.length == 0) {

    return allowEmpty;

  }



  // Returns true if value is a date in the dd/mm/yyyy format

  //

	isplit = value.indexOf('/');



	if(isplit == -1) {

		isplit = value.indexOf('.');

	}



	if(isplit == -1 || isplit == value.length) {

		return false;

	}



  sDay 			 = value.substring(0, isplit);

	monthSplit = isplit + 1;



	isplit 		 = value.indexOf('/', monthSplit);



	if(isplit == -1) {

		isplit = value.indexOf('.', monthSplit);

	}

	if(isplit == -1 ||  (isplit + 1 )  == value.length) {

		return false;

	}



	sMonth = value.substring((sDay.length + 1), isplit);



	sYear  = value.substring(isplit + 1);



	// Support for Y2K

	// problems

	if((sYear < 100) && (sYear >= 0)) {

		if(sYear > 50) {

			sYear = (Math.abs(sYear) + 1900).toString(); // I need to use this cause the scripting engine

		}																							 // has problems when mixing numbers and strings.

		else if(sYear <= 50) {												 // Obviously this is a patch, I don't know why it

			sYear = (Math.abs(sYear) + 2000).toString(); // works ... (as anything else..:)

		}

	}



	// Do checks in values

	//

	if(!CheckIntegerRange(sMonth, 1, 12)) {

		return false;

	}

	else if(!CheckIntegerRange(sYear, 0, null)) {

		return false;

	}

	else if(!CheckDay(sYear, sMonth, sDay)) {

		return false;

	}

	else {

		return new Date(sYear, sMonth - 1, sDay);

  }

}



// Checks if day is valid in given

// month and year

//

function CheckDay(checkYear, checkMonth, checkDay)

{

	maxDay = 31;



	if(checkMonth == 4 || checkMonth == 6 ||

		 checkMonth == 9 || checkMonth == 11) {

		maxDay = 30;

	}

	else if(checkMonth == 2) {

		if(checkYear % 4 > 0) {

			maxDay =28;

		}

		else if(checkYear % 100 == 0 && checkYear % 400 > 0) {

			maxDay = 28;

		}

		else {

			maxDay = 29;

		}

	}



	return CheckRange(checkDay, 1, maxDay); //check day

}



// Returns true if value is a number and is in range

//

function CheckIntegerRange(value, minValue, maxValue)

{

	if(value.length == 0) {

		return true;

	}



	// It's integer?

	//

	if(!CheckInteger(value)) {

		return false;

	}

	// It's in range?

	//

  else {

		return(CheckRange((eval(value)), minValue, maxValue));

	}

	// All tests passed, so...

	//

	return true;

}



// Returns true if value is a number defined as

// having an optional leading + or -.

// having at most 1 decimal point.

// otherwise containing only the characters 0-9.

//

function CheckInteger(value)

{

  if(value.length == 0) {

		return true;

	}



	var numberFormat  = " .0123456789";

	var startFormat   = " .+-0123456789";

	var trailingBlank = false;

	var decimal 		  = false;

	var digits 				= false;

	var checkChar;



  //The first character can be + - .  blank or a digit.

  //

	checkChar = startFormat.indexOf(value.charAt(0))

  //Was it a decimal?

  //

	if(checkChar == 1) {

		decimal = true;

	}

	else if(checkChar < 1) {

		return false;

	}



	// Remaining characters can be only . or a digit, but only one decimal.

	//

	for(var i = 1; i < value.length; i++) {

		checkChar = numberFormat.indexOf(value.charAt(i))



		if(checkChar < 0) {

			return false;

		}

		else if(checkChar == 1) {

			if(decimal) {		// Second decimal.

				return false;

			}

			else {

				decimal = true;

			}

		}

		else if(checkChar == 0) {

			if(decimal || digits)	{

				trailingBlank = true;   // ignore leading blanks

			}

		}

	  else if(trailingBlank) {

			return false;

		}

		else {

			digits = true;

		}

	}

  return true

}



// If value is in range then return true else return false

//

function CheckRange(value, minValue, maxValue)

{

  // Check minimum

  //

  if(minValue != null) {

    if(value < minValue) {

			return false;

		}

	}



  // check maximum

  if(maxValue != null)

	{

		if(value > maxValue) {

			return false;

		}

	}

  return true;

}



// Changes formName's action, and submits it

//

function SubmitForm(formName, actionURL)

{

	var form 		= document.forms[formName];

	form.action = actionURL;

	form.submit();

}



// Switchs an image from on to off, and vice versa

// For this to work, image.src must be a gif file finished in

// '...On.gif' or '...Off.gif'

//

function SwitchImage(image)

{

	var prefix;



	// Get the name of this image

	//

	var imageName = image.src.substr(image.src.lastIndexOf("/") + 1, image.src.length);

	var lastIdx   = imageName.lastIndexOf(".");

	while(lastIdx >= 0) {

		imageName = imageName.substr(0, imageName.length - lastIdx - 1);

		lastIdx   = imageName.lastIndexOf(".");

	}



	if(image.src.lastIndexOf("On.gif") >= 0) {

		// Image is in On state, Off it

		//

		prefix 		= image.src.substr(0, image.src.length - ("On.gif").length);

		image.src = prefix + "Off.gif";

	}

	else if(image.src.lastIndexOf("Off.gif") >= 0) {

		// Image is in Off state, On it

		//

		prefix 		= image.src.substr(0, image.src.length - ("Off.gif").length);

		image.src = prefix + "On.gif";

	}



}



// Returns an integer with the position of the

// last character in text, wich matches any of findStr

// characters.

// Returns -1 if no match is found

//

function LastIndexOf(text, findStr)

{

	var idx;

	for(var i = 0; i < text.length; i++) {

		// Look in text for findStr[i]

		//

		idx = text.lastIndexOf(findStr.charAt(i));

		if(idx >= 0) {

    	return idx; // We've found a match, return the index

    }

  }

  return -1; // We haven't found a match

}

 function cambiarDisplay(id) {
  if (!document.getElementById) return false;
  fila = document.getElementById(id);
  if (fila.style.display != "none") {
    fila.style.display = "none"; //ocultar fila
  } else {
    fila.style.display = ""; //mostrar fila
  }
}