function pageload(url){

if(url.substring(url.indexOf('#')+1)=='' || url.indexOf('..')!=-1)
{
 cargar_url_id2(url,'main_contenido');
}
else{
    url=url.substring(url.indexOf('#')+1);
    cargar_url_id2(url,'main_contenido');
  }
}

function cargar_url_id2(url,id){
		 $('#'+id).load(url);
	return false;
}

function empezar_carga(target){
		$('#main_upload').remove();
		$('#main_wrapper').append('<div id="main_upload" align="center"><form action="include/cancel.php" target="'+target+'" name="form_upload" id="form_upload"><a onclick="document.form_upload.submit();window.terminar_carga();"><img src="plantilla/imagenes/ajax-loader.gif"><br><span style="font-size:15px;">Cancelar</span></a></form> </div>');
		$('#main_upload').fadeIn('normal');
}

function terminar_carga(){
	$('#main_upload').fadeOut('normal');
}

function loadContent(url,id) {
		$('#'+id).load(url,function(){showNewContent(id);})
}
function showNewContent(id) {
			$('#'+id).slideDown('normal',hideLoader);
}
function hideLoader() {
			$('#main_load').fadeOut('normal');
}



function loadurl_sub(url){
	var pagecnx = createXMLHttpRequest();
	pagecnx.open('GET', url, true);
	pagecnx.send(null);
}



function loadurlkey(e,url,id){
	tecla = (document.all) ? e.keyCode : e.which;
	if (tecla==13)
		loadurl(url,id);
}

function createXMLHttpRequest() {
	var xmlHttp=null;
	if (window.ActiveXObject)
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
			xmlHttp = new XMLHttpRequest();
	return xmlHttp;
}

function abrir_cerrar(id){
	if(document.getElementById(id).style.display=='none')
		$('#'+id).slideDown('fast');
	else
		$('#'+id).slideUp('fast');
}

function abrir_cerrar_tabla(id,largo){
 if(document.getElementById(id).style.display=='none'){
     $('#'+id).slideDown('fast');
     ordenar_tabla(id,largo);
 } else
     $('#'+id).slideUp('fast');
}

function cerrar(id){
	if(document.getElementById(id).style.display!='none')
		$('#'+id).slideUp('fast');
}

function abrir(id){
	if(document.getElementById(id).style.display =='none')
		$('#'+id).slideDown('fast');
}

function ocultar(id){
	document.getElementById(id).style.display='none';
}

function mostrar(id){
		document.getElementById(id).style.display='block';
}


function getCheckValue(id)
{
    if(document.getElementById(id).checked)
        return 1;
    else
        return 0;
}


function getIdValue(id){
    if(document.getElementById(id)!=undefined)
        return document.getElementById(id).value;
    else
    	return false;
}

function getIdHtml(id){
    if(document.getElementById(id)!=undefined)
        return document.getElementById(id).innerHTML;
    else
    	return false;
}

function getRadioButtonSelectedChecked(ctrl){
var temp=document.getElementsByName(ctrl);
    for(i=0;i<temp.length;i++)
        if(temp[i].checked) return true;
	return false;
}

function getRadioButtonSelectedValue(ctrl){
var temp=document.getElementsByName(ctrl);
    for(i=0;i<temp.length;i++)
        if(temp[i].checked) return temp[i].value;
}

function MarcarCheckBox(name){
    var temp=document.getElementsByName(name);
        for(i=0;i<temp.length;i++)
            temp[i].checked=1
}

function DesmarcarCheckBox(name){
    var temp=document.getElementsByName(name);
        for(i=0;i<temp.length;i++)
            temp[i].checked=0
}

function cargar_url_dragdrop(url){
	$('#main_dragdrop_aux').load(url,function(){$('#main_dragdrop').fadeIn('normal');$('#main_dragdrop_aux').fadeIn('normal');});
}


function cargar_tools_dragdrop(url){
  	$('#tools_dragdrop_aux').load(url.substring(url.indexOf('#')+1)+'.php',function(){$('#tools_dragdrop').fadeIn('normal');$('#tools_dragdrop_aux').fadeIn('normal');});


}

function cargar_url_dragdrop_posicion(url, top, left){
	$('#main_dragdrop_aux').load(url,function(){$('#main_dragdrop').fadeIn('normal');$('#main_dragdrop_aux').fadeIn('normal');});
	$('#main_dragdrop').css("top",top);
	$('#main_dragdrop').css("left",left);
}


function servicioExterno(servicio){

    document.getElementById('iframe_externo').src=servicio;
}

function ordenar_tabla(id,h){
	if(document.getElementById(id)==undefined) return;

	//odd
	tbody=document.getElementById(id).getElementsByTagName('tbody');
	tbody = (tbody[0]) ? tbody[0] : null;
	var trs = tbody.getElementsByTagName('tr');
	for (x=0; x<trs.length; x++) {
		if(x%2==0)
		trs[x].className ="odd";
	}

	//height maximo
	if(h!=undefined && trs.length>2){
		tableEl=document.getElementById(id);
		tableEl.style.width='97%';

	    if(document.all && document.getElementById && !window.opera){ //IE
			containerEl = tableEl.parentNode.insertBefore(document.createElement('div'), this.tableEl);
			containerEl.appendChild(tableEl);
			containerEl.style.height = h + 'px';
			containerEl.style.width = '100%';
			containerEl.style.overflow= 'auto';
		}
		if (!document.all && document.getElementById && !window.opera){//FF
			scrollWidth=17;
			thead = this.tableEl.getElementsByTagName('thead');
			thead = (thead[0]) ? thead[0] : null;
			tfoot = this.tableEl.getElementsByTagName('tfoot');
			tfoot = (tfoot[0]) ? tfoot[0] : null;
			tbody = this.tableEl.getElementsByTagName('tbody');
			tbody = (tbody[0]) ? tbody[0] : null;

			headHeight = (thead) ? thead.clientHeight : 0;
			headFoot = (tfoot) ? tfoot.clientHeight : 0;
			bodyHeight = tbody.clientHeight;

			var trs = tbody.getElementsByTagName('tr');
			tbody.style.overflow = '-moz-scrollbars-vertical';
			for (x=0; x<trs.length; x++) {
				var tds = trs[x].getElementsByTagName('td');
				tds[tds.length-1].style.paddingRight += this.scrollWidth + 'px';
			}
			cellSpacing = (tableEl.offsetHeight - (tbody.clientHeight + headHeight)) / 4;
			tbody.style.height = (h - (headHeight + cellSpacing * 2) - (headFoot+ cellSpacing * 2)) + 'px';
        }
    }

	//ordenar tabla
	$('#'+id).tablesorter();

}

function ordenar_tabla_cookie(id,h){
	if(document.getElementById(id)==undefined) return;
	//height maximo
	if(h!=undefined){
		tableEl=document.getElementById(id);
		tableEl.style.width='97%';

	    if(document.all && document.getElementById && !window.opera){ //IE
			containerEl = tableEl.parentNode.insertBefore(document.createElement('div'), this.tableEl);
			containerEl.appendChild(tableEl);
			containerEl.style.height = h + 'px';
			containerEl.style.width = '100%';
			containerEl.style.overflow= 'auto';
		}
		if (!document.all && document.getElementById && !window.opera){//FF
			scrollWidth=17;
			thead = this.tableEl.getElementsByTagName('thead');
			thead = (thead[0]) ? thead[0] : null;
			tfoot = this.tableEl.getElementsByTagName('tfoot');
			tfoot = (tfoot[0]) ? tfoot[0] : null;
			tbody = this.tableEl.getElementsByTagName('tbody');
			tbody = (tbody[0]) ? tbody[0] : null;

			headHeight = (thead) ? thead.clientHeight : 0;
			headFoot = (tfoot) ? tfoot.clientHeight : 0;
			bodyHeight = tbody.clientHeight;

			var trs = tbody.getElementsByTagName('tr');
			tbody.style.overflow = '-moz-scrollbars-vertical';
			for (x=0; x<trs.length; x++) {
				var tds = trs[x].getElementsByTagName('td');
				tds[tds.length-1].style.paddingRight += this.scrollWidth + 'px';
			}
			cellSpacing = (tableEl.offsetHeight - (tbody.clientHeight + headHeight)) / 4;
			tbody.style.height = (h - (headHeight + cellSpacing * 2) - (headFoot+ cellSpacing * 2)) + 'px';
        }
    }

	//odd
	tbody=document.getElementById(id).getElementsByTagName('tbody');
	tbody = (tbody[0]) ? tbody[0] : null;
	var trs = tbody.getElementsByTagName('tr');
	for (x=0; x<trs.length; x++) {
		if(x%2==0)
		trs[x].className ="odd";
	}

    var tipo_orden=0;
    var columna=0;
     if($.cookie(id) != null)
     {

           columna=$.cookie(id).substring($.cookie(id).indexOf("-")+1);
           tipo_orden=$.cookie(id).substring(0,$.cookie(id).indexOf("-")); ;
     }
	//ordenar tabla
	$('#'+id).tablesorter({
        // sort on the first column and third column, order asc
        sortList: [[columna,tipo_orden]]
    });

}

function copiar_contenido(id_desde,id_para){
    $('#'+id_para).html($('#'+id_desde).html());
}

function confirmar_url_dragdrop(mensaje,url){
    if(confirm(mensaje))
        cargar_url_dragdrop(url);

}

function guardar_config_tabla(nombre_cookie, campo)
{
   if($.cookie(nombre_cookie) != null && $.cookie(nombre_cookie)=='0-'+campo)
   {
        $.cookie(nombre_cookie, '1-'+campo);
   }
   else
   {
     $.cookie(nombre_cookie, '0-'+campo);
   }
}

function cargar_chart(id,url,w,h){
    swfobject.embedSWF("include/open-flash-chart/open-flash-chart.swf",id, w, h, "9.0.0","expressInstall.swf",{"data-file":url});

}

/// carga informacion en una capa dependiendo del valor de select
/// @var url es la ruta de archivo que se cargara en la capa
/// @var div es el nombre de la capa donde se cargara la informacion
/// @var elmnt es el nombre de select
/// @var elmntValue es el valor de select que condiciona si es mostrada la informacion o no
/// @return void,null
function cargar_url_id_limpiar(url,div,elmnt, elmntValue)
{
    if(getIdValue(elmnt) != elmntValue )
    {
		cargar_url_id(url,div);
    }
     else
     {
       document.getElementById(div).innerHTML = '';
     }
}

function cargar_url_id_cerrar(url,id){
	if(document.getElementById(id).style.display=='none'){
        cargar_url_id(url,id);
        $('#'+id).slideDown('fast');
	}else
		$('#'+id).slideUp('fast');
}

/// habilita o deshabilita un elemento
function enabled_disabled(id, accion)
{
	if(accion == true)
	{
		document.getElementById(id).disabled = false;
	}
	else
	{
		document.getElementById(id).disabled = true;
	}
}

// muestra mensaje antes de enviar a url
function confirmar_envio_url(msj,url,id)
{
	if(!window.confirm(msj)){
		return false;
	}
	else{
		cargar_url_id2(url,id);
	}
}

// muestra mensaje antes de ejecutar funcion. Ej. msj antes de eliminar
function confirmar_callback(msj,funcion)
{
	if(!window.confirm(msj)){
		return false;
	}
	else{
		return eval(funcion);
	}
}

// posiciona pantalla en elemento de DOM
function enfoca_id(id_elm)
{
	document.getElementById(id_elm).scrollIntoView(true);
}

// valida formulario, es util en el caso de no conocer los nombres de input (formulario parametrizable), es posible asignar input opcionales definiendo name='campo_opt' 
function validar_formulario(nom_form)
{
	var elementos = document.getElementById(nom_form).elements.length;
	for(i=0; i<elementos; i++)
	{
		var nom_campo = document.getElementById(nom_form).elements[i].name;
		if( nom_campo.indexOf("_opt") == -1 )
		{
			if(document.getElementById(nom_form).elements[i].value == "")
			{
				window.alert('complete todos los campos');
				return false;
			}
		}
	}
	document.getElementById(nom_form).submit();
}

// envia desde un link al archivo de accion. ej. eliminar producto
function enviar_id_submit(id,hidden,formulario){
     document.getElementById(hidden).value = id;
     document.getElementById(formulario).submit();
}




function mostrar_ocultar_div(nombre_div,nombre_hidden)
{

    if(document.getElementById(nombre_hidden).value == 'Si'){
       mostrar_div(nombre_div);
    //    document.getElementById(nombre_hidden).value = 'No';-->
        }
        else{
         ocultar_div(nombre_div);
     //     document.getElementById(nombre_hidden).value ='Si';
        }

}


function mostrar_div(div)
{
  div = document.getElementById(div);
  div.style.display = "block";
}

function ocultar_div(div)
{
  div = document.getElementById(div);
  div.style.display="none";
}
