var timeout;

function getProperties(html_object){
		var msg='';
		var count=0;
		for(var prop in html_object){
			count++;
			var sep="";
			if(count==5){
				sep="\n";
				count=1;
			}else if(count>0){
				sep=";\t\t\t";
			}
			//if(html_object.getAttribute(prop)=="button")
			msg+=sep+ prop+": "+ html_object.getAttribute(prop);
		}
		alert(msg);
	}
	
	
	function validarRut(rut,dv){
	
		var parts=rut.split("-");

		if(parts.length==2){
			rut=parts[0];
			dv=parts[1];
		}
		
		rut=parseFloat(rut);
		
		if(!rut || !dv ||rut==""||dv==""){
			return false;
		}		
		
	  s = rut+ "" + dv;
	  s = s.toUpperCase();
	  	  
	  if (s.length > 0)
	  {
	     i = s.length - 1;
	     digito = s.charAt(i);
	     k = i-1;
	     rut = 2;
	     suma = 0;
	     for (k=i-1; k >=0; k--)
	     {	     
	        if (!(s.charAt(k) >=  "0" && s.charAt(k) <="9")) return false;
	        suma = suma + parseInt(s.charAt(k))*rut;
	        rut = rut + 1;
	        if (rut > 7)
	           rut = 2;
	     }
	     resto = suma%11;
	     revisa=11-resto;
	     if (revisa==11)
	        verif="0";
	     else if (revisa==10)
	        verif="K";
	     else
	        verif=revisa;
	     verif="" + verif;
	     if (digito!=verif)  return false;
	  }
	  return true;
	}
		
	
	function getServerResponse(url,action,param_array,input_function,parent_object,message){
		var solicitud={url:url,action:action,param_array:param_array,input_function:input_function,parent_object:parent_object}
		if(!window.cola_solicitudes)window.cola_solicitudes=[];
		cola_solicitudes[cola_solicitudes.length]=solicitud;
		
		if(cola_solicitudes.length==1){
			timeout=0;
			var msg="<br><br>Procesando Petición<br><br>";
			if(message!=null)msg="<br><br>"+message+"<br><br>";
			Dialog.info(msg,{className: "alphacube", width:250, height:100, top:100, showProgress: true});
	
			function enviarSiguienteSolicitud(){			
				if(cola_solicitudes.length==0){
					Dialog.closeInfo();					
					setTimeout("var overlay_modal=document.getElementById('overlay_modal');	if(overlay_modal)overlay_modal.style.display='none';",500);
					
					return;
				}
				
				var solicitud=cola_solicitudes[0];
												
				var str_param="";var first=true;
				if(solicitud.param_array){
					for(var key in solicitud.param_array){
						var value=solicitud.param_array[key];
						
						if(value && value!=""){
							if(first==false)str_param+="&";
							else first=false;
							
							str_param+=key+"="+value;
						}
					}
				}
				
				//alert("str_param: "+str_param);
				var http=getHttpRequest();		
				
				http.onreadystatechange=function(){
					if(http.readyState==4){
						
						var solicitud=cola_solicitudes[0];
						cola_solicitudes.splice(0,1);
						enviarSiguienteSolicitud();

						var response=null;
						if(http.responseText!="" && http.responseText.substr(0,1)=="{" && http.responseText.substr(http.responseText.length-1,1)=="}"){
							try{
								eval("response="+http.responseText);
								if(solicitud.input_function!=null){
									//alert("llamando método call");
									
									solicitud.input_function.call(solicitud.parent_object?solicitud.parent_object:this, response);
								}	
							
							}catch(err){
							  var txt="A ocurrido un error al procesar la peticion. ("+err.description+ ") \n"+http.responseText;							  
							  alert(txt);
							  response="error";
							//  throw err;
							}							
						}else{
							alert("A ocurrido un error al procesar la petición. \n'"+http.responseText+"'");
						}
						//alert("Procesando siguiente cola - pendientes: "+cola_solicitudes.length);						
					}
					return;
			  	}
				//http.open("POST",solicitud.url+"?action="+solicitud.action+(str_param!=""?"&"+str_param:"")+"&serialResquest="+Math.floor(Math.random()*12345),true);
				http.open("POST",solicitud.url+"?action="+solicitud.action+"&serialResquest="+Math.floor(Math.random()*12345),true);
				http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				http.setRequestHeader("Content-length", str_param.length);
				http.setRequestHeader("Connection", "close");
				
				http.send(str_param);
			}
			//alert("Primera solicitud: "+solicitud.action);
			enviarSiguienteSolicitud();
		}
	
	
	
	}
	
	
	
	
	function getHttpRequest(){
		xmlHttp=null;
		try{
		 // Firefox, Opera 8.0+, Safari
			 xmlHttp=new XMLHttpRequest();
		}catch (e1){
			try{
			 // Internet Explorer  try
			 xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch(e2){
			 	try{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");      
				}catch (e3){
					alert("Your browser does not support AJAX!");		
				}
			}
	    }
	    return xmlHttp;
	}
	
	function showAsociativeArray(asociative_array){
		var text="";

		for(var element in asociative_array){		
			text+=(text!=""?"\n":"")+element+": ["+ asociative_array[element]+"]";
		}
		alert(text);
	}	

	function addOption(name,text,value){
		var elOptNew = document.createElement('option');
		elOptNew.text = text;
		elOptNew.value = value;
		var elSel = document.getElementById(name);
		try {
			elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
			elSel.add(elOptNew); // IE only
		}
	}
	
	function removeOption(name,n){
	  var elSel = document.getElementById(name);
	  for (var i = elSel.length - 1; i>=n; i--) {
	      elSel.remove(i);
	  }
	}
	
	function validarEntradaNumerica(parent){		
		if(parent.value!=null && parent.value!="" && isNaN(parseFloat(parent.value))){
			//alert("El valor digitado debe ser numerico.");			
			setTimeout("parent.focus()",50);
		}
	}
