var xmlhttp;


var layer = null;
var image = null;
var w3c = document.getElementById;
var ns;
var ie;
if(!w3c) {
	ns = (document.layers) ? true : false;
	ie = (document.all) ? true : false;
}

function viewlayer(imageName) {
	var showhide;
	showHide = "visible";
	var whichLayer = imageName;
	var style;
	if(w3c)
		style = document.getElementById(whichLayer).style;
	else if(ie)
		style = document.all[whichLayer].style;
	else if(ns)
		style = document.layers[whichLayer];
	var image;
	if(imageName)
		image = document.images[imageName];
	if(image) {
		style.left = 0;
		style.top =  0;
	}
	style.visibility = showHide;
}

function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=state_Change;
  xmlhttp.open("GET",url,true);
  xmlhttp.send(null);
  }
else
  {
  alert("Su navegador no soporta XMLHTTP.");
  }
}




function state_Change()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
    document.getElementById('cuerpo').innerHTML=xmlhttp.responseText;
    }
  else
    {
    alert("Problema al recuperar datos:" + xmlhttp.statusText);
    }
  }
}

//loadXMLDoc('rubro1/index.php');



//Funci�n para crear un objeto XML-http
function getxmlhttp (){
	//Crear una varialbe de ool para comprobar si se utiliza una instancia v�lida de ActiveX Microsoft.
	var xmlhttp = false;
	
	//Comprobar si se est� utilizando Internet Explorer.
	try {
		//Si la versi�n de javascript es superior a la 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//En caso contrario, utilizar el tradicional objeto ActiveX.
		try {
			//Si se est� utilizando Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//En caso contrarios, no se est� utilizando Internet Explorer.
			xmlhttp = false;
		}
	}
	
	//Si no se est� usando Internet Explorer, crear una instancia javascript del objeto.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}

//Funci�n para procesar una petici�n XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
	//Obtener el objeto XMLHttpRequest a utilizar.
	xmlhttp = getxmlhttp ();
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	} else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(str);
	}
}


function getformvalues (fobj, valfunc){
	
	var str = "";
	aok = true;
	var val;
	
	//Recorrer la lista de todos los objetos que contiene el formulario.
	for(var i = 0; i < fobj.elements.length; i++){
		//if(valfunc) {
		//	if (aok == true){
		//		val = valfunc (fobj.elements[i].value,fobj.elements[i].name); 
		//		if (val == false){
		//			aok = false;
		//		}
		//	}
		//}
		// funcion modificada la original tomaba erroneamente datos checkbox y radio
		//alert(fobj.elements[i].name + "=" + fobj.elements[i].type + "=" + fobj.elements[i].status);
		if((fobj.elements[i].type == "checkbox" || fobj.elements[i].type == "radio" ) && fobj.elements[i].checked == false){
			str += fobj.elements[i].name + "=" + "&";
		} else {
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		}
	}
	//Devolver valores
	return str;
}
	
function submitform (theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	if(valfunc){
		aok = valida();
	} else {
		aok = true;
	}
	//Si la validaci�n es correcta.
	if (aok == true){
		obj = document.getElementById(objID);
		processajax (serverPage, obj, "post", str);
	}
}





/*
	RAC 20080708 1.0
	             1.1 - correccion radio y checkbox en manejo datos form
*/

var t_id = setInterval(animate,20);
var pos=0;
var dir=2;
var len=0;

function remove_loading() {

	this.clearInterval(t_id);
	var targelem = document.getElementById('loader_container');
	targelem.style.display='none';
	targelem.style.visibility='hidden';
	

}
function animate()
{
	var elem = document.getElementById('progress');
	if(elem != null) {
		if (pos==0) len += dir;
		if (len>32 || pos>79) pos += dir;
		if (pos>79) len -= dir;
		if (pos>79 && len==0) pos=0;
		elem.style.left = pos;
		elem.style.width = len;
	}
}

function submitlogin (theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	if(valfunc){
		aok = veriflogeo();
	} else {
		aok = true;
	}
	//Si la validaci�n es correcta.
	if (aok == true){
		obj = document.getElementById(objID);
		processajax (serverPage, obj, "post", str);
	}
}

function submitdatospersonales(theform, serverPage, objID, valfunc){
	var file = serverPage;
	var str = getformvalues(theform,valfunc);
	if(valfunc){
		aok = datospersonales();
	} else {
		aok = true;
	}
	//Si la validaci�n es correcta.
	if (aok == true){
		obj = document.getElementById(objID);
		processajax (serverPage, obj, "post", str);
	}
}



// Obtener poblaciones
function getPoblaciones(id_provincia) {
    processajax ('templates/consultar-poblaciones.php',
                 document.getElementById('div_poblaciones'), 
                 'post', 'id_provincia='+id_provincia);
}


