//Searchs for an elment father of
function isFatherOf(fatherid,childid,childtag)
{
	var childs = document.getElementById(fatherid).getElementsByTagName(childtag);
	
	for (var i=0;i<childs.length;i++)
	{
		if ( Trim(childs[i].id) == Trim(childid) )
		{
			return true;
		}
	}
	return false;
}

function ConvertHTMLToTextArea(HTMLCode)
{
	var strReturn;
	strReturn = HTMLCode;
	while ( strReturn.indexOf("<BR>",0) != -1 )
	{
		strReturn = strReturn.replace("<BR>","\n");
	}
	return strReturn;
}


function isBlank(id)
{
	if ( document.getElementById(id).value == null ||
		Trim(document.getElementById(id).value) == "" )
			return true;
	else
			return false;
}

function focus(id,color)
{
	document.getElementById(id).focus();
	document.getElementById(id).style.background = color;
}

function DropDownisNull(id)
{
	if (Ddl_getSelectedItem(id).value == "NULL" )
		return true;
	else
		return false;
}

/*Hides the loding image*/
function waitPreloadPage() { //DOM
	if (document.getElementById){
		document.getElementById('prepage').style.visibility='hidden';
	}
	else{
		if (document.layers){ //NS4
			document.prepage.visibility = 'hidden';
		}
		else { //IE4
		document.all.prepage.style.visibility = 'hidden';
		}
	}
}

//Valida los campos
function validate()
{
	for(i = 0; i < document.all.length; i++)
	{
		if (document.all(i).type == "text" || document.all(i).type == "select-one")
			{
				//Valida Campos requeridos
				if(document.all(i).vacio == "true")
				{
					//Valida requeridos.
					if (document.all(i).value.length == 0)
					{
						alert('Informacin requerida');
						document.all(i).focus();
						return false;
					}
				}

				//Valida Cammpos numericos Numeros.
				if(document.all(i).numerico == "true")
				{
					//Valida Numericos.
					if (!numero(document.all(i).value))
					{
						alert('Digite solo caracteres numericos');
						document.all(i).focus();
						return false;
					}
				}
			}
	}
}

//Numero: verifica que el
//campo contenga un numero
function numero(tira)
{
    var num=".0123456789";
    for(var i=0; i<=tira.length;i++)
		{
    	var temp=tira.charAt(i);
    	var este = num.indexOf(temp,0);
    	if(num.indexOf(temp,0) < 0)
			{
    			 return false;
	    	}
    	}
	return true;
}



function getLabelName(strname, hasfoo)
{
	var i = 0;
	var validatecontrolname = "";
	var namearray = strname.id.split('_');
	if (!hasfoo)
		for (i = 1;i<namearray.length;i++)
		{
			validatecontrolname += namearray[i];
			if (i + 1  < namearray.length)
				validatecontrolname += "_";
		}
	else
		for (i = 1;i<namearray.length -1 ;i++)
		{
			validatecontrolname += namearray[i];
			if (i + 1  < namearray.length - 1)
				validatecontrolname += "_";
		}
	return "lbl_" + validatecontrolname;
}

/// Valida un Control de texto que tenga datos
///
/// ControlId - Id del control a buscar
function validateTextControl(ControlId,msg)
{
	//Busco el Control
	var txtControl = document.getElementById(ControlId);
	//valido que exista
	if (txtControl != null)
	{
		//Pregunta si esta vacio
		if ( txtControl.value.length == 0  )
		{
			if (msg == "")
				alert("El Dato es Requerido");
			else
				alert(msg);
			txtControl.focus();
			txtControl.style.backgroundColor = "#ffffcc";
      		return false;
		}
		else
		{
			txtControl.style.backgroundColor = "";
			return true;
		}
	}
}

function validateTextAlmostOne(strArrayIds,strMessage)
{
	var count = 0;
	for(i = 0; i < strArrayIds.length; i++)
	{
		//Busco el Control
		var txtControl = document.getElementById(strArrayIds[i]);
		//valido que exista
		if (txtControl != null)
		{
			//Pregunta si esta vacio
			if ( txtControl.value.length != 0  )
				count++;
			//Pongo el color normal
			txtControl.style.backgroundColor = "";
		}
	}
	if (count == 0)
	{
		alert(strMessage);
		document.getElementById(strArrayIds[strArrayIds.length - 1]).focus();
		document.getElementById(strArrayIds[strArrayIds.length - 1]).style.backgroundColor = "#ffffcc";
		return false;
	}
}

function validateTextControlArray(strArrayIds)
{
	for(i = 0; i < strArrayIds.length; i++)
		if (! validateTextControl(strArrayIds[i],"") )
		{
			return false;
			break;
		}
}

function clearControl(ctrlId)
{
	//Busco el Control
	var Control = document.getElementById(ctrlId);
	//valido que exista
	if (Control != null)
	{
		Control.value = "";
	}
}

function validateWithBoldLabel()
{
   for(var i = 0; i < document.all.length; i++)
	{
	   if (document.all(i).type == "text" || document.all(i).type == "select-one" )
	   {
		  //Verifica si tiene foo para el caso de los datepicker
		  if ( document.all(i).id.split('_')[0].toLowerCase() != "dtp" )
			var label = document.getElementById(getLabelName(document.all(i),false));
		  else
			var label = document.getElementById(getLabelName(document.all(i),true));
		  //Verifica si la etiqueta respectiva esta en negrita
	      if ( label != null  && label.style.fontWeight == "bold")
	      {
			//Valida el control
	        if ( validateTextControl(document.all(i).id,"") == false)
	        {
				return false;
				break;
			}
	      }
	   }
  }
  return true;
}

//Retorna el valor selecionado de una OptionList
function getSelectedItem(optListName)
{
	var list = document.getElementById(optListName);
	var options = list.getElementsByTagName("input");
	for( var x = 0; x < options.length; ++x )
	{
		if( options[x].type == "radio" && options[x].checked )
		{
			return options[x].value;
			break;
		}
	}
}


function Ddl_getSelectedItem(ddlGenericId)
{
	var ddlGeneric = document.getElementById(ddlGenericId);
	if (ddlGeneric != null)
	{
		var options = ddlGeneric.getElementsByTagName("option");
		for( var x = 0; x < options.length; ++x )
		{
			if( options[x].selected )
			{
				return options[x];
				break;
			}
		}
	}
}

function Ddl_setSelectedItem(ddlGenericId,Value)
{
	var ddlGeneric = document.getElementById(ddlGenericId);
	if (ddlGeneric != null)
	{
		var options = ddlGeneric.getElementsByTagName("option");
		for( var x = 0; x < options.length; ++x )
		{
			if( options[x].value == Value)
			{
				ddlGeneric.selectedIndex = x;
				break;
			}
		}
	}
}

function Ddl_RemoveItems(comboid)
{
 	var oOptions = document.getElementById(combo).options;

	// Remove all selected options
	while ( oOptions.length != 0)
	{
	        combo.remove(0);
	}
}

function clearAllTextErrors()
{
	for(var i = 0; i < document.all.length; i++)
		if (document.all(i).type == "text" || document.all(i).type == "select-one" )
		{
			document.all(i).style.backgroundColor = "";
		}
}


function staticBackGround(picURL)
{
	if (document.all && document.body != null)
		document.body.style.cssText="background:white url("+ picURL +") no-repeat fixed center center";
}

function TrimLeft(sString)
{
	while (sString.substring(0,1) == ' ')
		sString = sString.substring(1, sString.length);
	return sString;
}

function TrimRight(sString)
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
		sString = sString.substring(0,sString.length-1);
	return sString;
}

function Trim( sString ) {
	var resultStr = "";

	resultStr = TrimLeft(sString);
	resultStr = TrimRight(resultStr);

	return resultStr;
}

// Functions that swaps images.
function MM_swapImage() { //v3.0
   var i,j=0,x,a=MM_swapImage.arguments;
   document.MM_sr=new Array;
   for(i=0;i<(a.length-2);i+=3)
   	if ((x=MM_findObj(a[i]))!=null){
		document.MM_sr[j++]=x;
	if(!x.oSrc)
		x.oSrc=x.src;
	x.src=a[i+2];
    }
};
// Maximiza el Form
function MM_Maximaze(){
	top.window.moveTo(0,0);
	if (document.all) {
		top.window.resizeTo(screen.availWidth,screen.availHeight);
	}
	else
		if (document.layers||document.getElementById) {
			if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
				top.window.outerHeight = screen.availHeight;
				top.window.outerWidth = screen.availWidth;
			}
		}
};


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
};


// Functions that handle preload.
function MM_preloadImages() { //v3.0
   var d=document;
   if(d.images){
   	if(!d.MM_p)
		d.MM_p=new Array();
   	var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
	for(i=0; i<a.length; i++)
   		if (a[i].indexOf("#")!=0){
			d.MM_p[j]=new Image;
			d.MM_p[j++].src=a[i];
		}
	}
};


//Recargar pagina
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
};

// Buscar un Objeto
function MM_findObj(n, d) { //v4.01
  var p,i,x;
  if(!d)
  	d=document;
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n])&&d.all)
  	x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++)
  	x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)
  	x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById)
  	x=d.getElementById(n);
  return x;
};


// Desplegar Mensaje
function MM_message(msg){
   alert(msg);
};

function closeWindow()
{
	self.close();
	return false;
}

