var objeto;
var module;
var conten;
var page;
var variables=Array();
var reservado=Array("hsbToRgb","rgbToHsb","rgbToHex","test","each","getLast","getRandom","include","merge","extend","associate","contains","remove","copy","hexToRgb","indexOf","some","every","map","filter","forEach"); //Varores existentes por defecto en un array
function cerrarModalbox(){
	Modalbox.hide();
}

function llamar_mod(x){
	if(x == 'form'){
		Modalbox.show('./?page=home/formulario&id_formulario=26&categoria=5', {title: '', width: 600});
	}
	
	if(x == 'mapa'){
		Modalbox.show('./mapa_san_miguel.php', {title:'', width: 450});	
	}
}

function llamar_mod_2(x){
	if(x == 'form'){
		Modalbox.show('./?page=home/formulario&id_formulario=26&categoria=2', {title: '', width: 600});
	}
	
	if(x == 'mapa'){
		Modalbox.show('./mapa_macul.php', {title:'', width: 450});	
	}
}

function cierratelo(){
	window.document.getElementById('ERRORES').style.display="none";
}
//AÑADE VARIABLE AL ARRAY DE VARIABLES 
function imprimir(id){
		var ia=window.document.getElementById('interes_anual').value;
		var im=window.document.getElementById('interes_mensual').value;
		var tu=window.document.getElementById('total_uf').value;
		var tp=window.document.getElementById('total_pesos').value;
		var mu=window.document.getElementById('mensual_uf').value;
		var mp=window.document.getElementById('mensual_pesos').value;
		var tcu=window.document.getElementById('total_credito_uf').value;
	$url="http://www.wpchile.cl/inmobiliario/fw/?fpage=home/imprimir_cotizacion&print=1&transaccion_id="+id;
	$url= $url+"&interes_anual="+ia+"&interes_mensual="+im+"&total_uf="+tu+"&total_pesos="+tp;
	$url= $url+"&mensual_uf="+mu+"&mensual_pesos="+mp+"&total_credito_uf="+tcu;
		window.open($url,'window','fullscreen=yes,scrollbars=yes');
	
	}
function VerCotizacion(){
	var name=window.document.getElementById('campo_127').value;
	var app=window.document.getElementById('campo_128').value;
	var fono=window.document.getElementById('campo_129').value;
	var mail=window.document.getElementById('campo_130').value;
	if(name && app && fono && mail){
		window.document.getElementById("COTIZARXX").style.display="block";	
		window.document.getElementById("COTIZARPIE").style.display="block";	
	}else{
		alert('Los campos con * son obligatorios');
	}
}

function cambiaFormulario(){
			x=window.document.getElementById("vivienda_id").value;
			
			resetVariable();
			setVariable("page","home/monto");
			setVariable("vivienda_id",x);
			CargaComponente("monto");
			
			x=window.document.getElementById("vivienda_id").value;
			resetVariable();
			setVariable("page","home/ver");
			setVariable("vivienda_id",x);
			CargaComponente("VIVIENDA_TEST");
		
	}
function setVariable(nom,val){
			this.variables[nom]=val;
		} 
//FIN
//RESETEA ARRAY DE VARIABLES		
function resetVariable(){
			this.variables=Array();			
		} 
//FIN
//FUNCION GENERICA PARA CARGAR UN AJAX
function CargaComponente(contenedor,onComplete){	
	var ajax = new sack();
	ajax.resetData();
	for(key in this.variables ){
			if(!reservado[key]){
				//alert(variables[key]);
				ajax.setVar(key,variables[key]); 
				
			}
			
		}
	ajax.setVar("print","1"); 
	if(onComplete){
		
			funcion="ajax.onCompletion = "+onComplete+";";
			
			eval(funcion);
		
		}
	ajax.requestFile = "./";
	ajax.method = 'GET';
	ajax.element = contenedor;
	ajax.runAJAX();		
	//alert(contenedor);
}
//FIN
//Inserta datos en fomulario cotizar ya que desde el controler no funciona.. se marea xD
function insertarDatos(a,b,c,d,e,f,x,id){
		window.document.getElementById('interes_anual').value=a;
		
		window.document.getElementById('interes_mensual').value=b;	
		window.document.getElementById('total_uf').value=c;
		window.document.getElementById('total_pesos').value=d;
		window.document.getElementById('mensual_uf').value=e;
		window.document.getElementById('mensual_pesos').value=f;
		window.document.getElementById('total_credito_uf').value=x;
		
		//window.document.getElementById('IMPRIMIR_COTIZACION').innerHTML="<a onclick='imprimir("+id+");' style='cursor:pointer' > <strog>Imprimir</strong></a>";
		//alert();
		//<a href="javascript:imprimir();">Imprimir</a> IMPRIMIR_COTIZACION
		
	}
//CAMBIA FORMULARIO COTIZACION
//
//FUNCION PARA CARGAR VENTANAS
function llamaVentana(id){
		new Control.Modal($(id),{
			iframe: true,
			width: 400,
			height: 350
		});
	
}
		
//FIN
//FUNCION PARA CARGAR  FORMULARIO y MENU DE UN OBJETO
function CargaFormulario(mod,pag){
	this.module=mod;
	resetVariable();
	setVariable("bpage",mod+"/"+pag+"_m");
	CargaComponente("NOTICIAS_PORTAL");
	setVariable("bpage",mod+"/"+pag+"_f");
	CargaComponente("ContentBody");
}
//FIN
//FUNCION PARA CARGAR BUSCADOR , LISTADOR Y PROCESO
function CargaBuscador(mod,pag,obj){
		
	this.objeto=obj;	
	this.module=mod;
	this.page=pag;
	resetVariable();
	setVariable("bpage",mod+"/"+pag+"_b");
	CargaComponente("NOTICIAS_PORTAL","setupObjeto");
}
function setupObjeto(ord,obj,mod,pag){
		
		if(obj)
			objeto=obj;
		if(mod)
			module=mod;
		if(pag)
			page=pag;
		if(!ord)
			ord="";
document.getElementById('proceso').src="./?page=home/procesador&print=1&objeto="+objeto+"&mod="+module+"&pag="+page+"&order="+ord;
}
//FIN
//INCLUIR JS DEL MODULO CORRESPONDIENTE (se carga en el onLoad del Page)
function incluirJs(){ 
	var filename="./modules/"+module+"/js/script.js";
	var body = document.getElementsByTagName('body').item(0); 
	script = document.createElement('script'); 
	script.src = filename; 
	script.type = 'text/javascript'; 
	body.appendChild(script) 
	
}
//FIN
//FUNCION VOLVER 
function backList(obj){
	document.getElementById("listForm").style.display="none";
	document.getElementById("ShowroomForm").style.display="none";
	resetVariable();
	setVariable("bpage",module+"/"+obj+"_b");
	CargaComponente("NOTICIAS_PORTAL");
	resetVariable();
	setVariable("bpage",module+"/"+obj+"_L");
	CargaComponente("ContentBody");
	
}//FIN
//Funcion utilizada por  los paginadores
function changePage(page,mod){
		var ajax = new sack();
		x=document.getElementById("offset_list").value;	
		resetVariable();
		setVariable("bpage",mod+"/"+page);
		setVariable("fl",1);
		setVariable("offset_list",x);
		CargaComponente("ContentBody");
	}
//FIN
function cargarContenido(id){
	
		resetVariable();
		setVariable("fpage","home/contenido_display");
		setVariable("id",id);
		CargaComponente("ContentBody");
	}
//FUNCONES EXTRAS!!!!!!
function cargaVentana(objeto_id,titulo,ancho,alto,pX,pY){
var clientId=objeto_id; ///este es el objeto
var iniTitle=titulo;//el titulo del div
var iniUrl=null; //si es que se inicia una url aqui se coloca
var iniX=pX;// inicio de posicion X
var iniY=pY;// inicio de posicion Y
var iniW=ancho;
var iniH=alto;
var miniW=150;
var fenceId='';
var conPad=1;
var conBor=1;
var cliBor=1;
var enMove=true;
var enResize=true;
var enMinimize=true;
var enMaximize=true;
var enClose=true;
var enStatus=false;
var enFixed=true;
var fnMove=null;
var fnResize=null;
var fnMinimize=null;
var fnMaximize=null;
var fnRestore=null;
var fnClose=null;
var fnFocus=null;
var fnLoad=null;
var clsCon='xfCon';
var clsCli='xfClient';
var clsTitlebar='xfTBar';
var clsTitlebarFocused='xfTBarF';
var clsStatusbar='xfSBar';
var clsStatusbarFocused='xfSBarF';
var clsResizeIcon='xfRIco';
var clsMinimizeIcon='xfNIco';
var clsMaximizeIcon='xfMIco';
var clsRestoreIcon='xfOIco';
var clsCloseIcon='xfCIco';
var txtResize='Resize';
var txtMin='Minimizar';
var txtMax='Maximizar';
var txtRestore='Restaurar';
var txtClose='Cerrar';
if(!xFenster.instances[objeto_id]){
new xFenster(clientId, iniTitle, iniUrl, iniX, iniY, iniW, iniH, miniW,fenceId, 
			conPad, conBor, cliBor,//12
             enMove, enResize, enMinimize,//15
			 enMaximize, enClose, enStatus,//18
			 enFixed,fnMove, fnResize, //21
			 fnMinimize, fnMaximize, fnRestore,//24
			 fnClose, fnFocus, fnLoad,//27
             clsCon, clsCli, clsTitlebar, //30
			 clsTitlebarFocused, clsStatusbar, clsStatusbarFocused,//33
             clsResizeIcon, clsMinimizeIcon, clsMaximizeIcon,// 36
			 clsRestoreIcon, clsCloseIcon,txtResize, 
			 txtMin, txtMax, 
			 txtRestore, txtClose);
}
if(!xFenster.instances[objeto_id].restore())
	xFenster.instances[objeto_id].show();
}
function debug(s){
	
		if(s==1){
			window.document.getElementById("proceso").style.display="block";
			window.document.getElementById("procesoForm").style.display="block";
			window.document.getElementById("DEBUG").href="javascript:debug('-1');";
		
		}else{
			window.document.getElementById("proceso").style.display="none";
			window.document.getElementById("procesoForm").style.display="none";
			window.document.getElementById("DEBUG").href="javascript:debug('1');";
			
			}
			
		document.getElementById('proceso').style.position = "absolute";
		document.getElementById('proceso').style.zindex = "15000";
		document.getElementById('proceso').style.height = "91";
		document.getElementById('proceso').style.width = "563px";
		document.getElementById('proceso').style.border="double";
		document.getElementById('proceso').style.top = "11px";
		document.getElementById('proceso').style.left = (document.body.clientWidth/2-210)+'px';
		
		document.getElementById('procesoForm').style.position = "absolute";
		document.getElementById('procesoForm').style.zindex = "15000";
		document.getElementById('procesoForm').style.border="double";
		document.getElementById('procesoForm').style.height = "91";
		document.getElementById('procesoForm').style.width = "563px";
		document.getElementById('procesoForm').style.top = "620px";
		document.getElementById('procesoForm').style.left = (document.body.clientWidth/2-203)+'px';
		
		
	
}
function csv(objeto){
		
		window.document.getElementById('proceso').src="./?page=ajax/genera_csv&objeto="+objeto;
		
		
	
	}
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];}}
}
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;
}
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;
}
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];}
}
function MM_nbGroup(event, grpName) { //v6.0
  var i,img,nbArr,args=MM_nbGroup.arguments;
  if (event == "init" && args.length > 2) {
    if ((img = MM_findObj(args[2])) != null && !img.MM_init) {
      img.MM_init = true; img.MM_up = args[3]; img.MM_dn = img.src;
      if ((nbArr = document[grpName]) == null) nbArr = document[grpName] = new Array();
      nbArr[nbArr.length] = img;
      for (i=4; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
        if (!img.MM_up) img.MM_up = img.src;
        img.src = img.MM_dn = args[i+1];
        nbArr[nbArr.length] = img;
    } }
  } else if (event == "over") {
    document.MM_nbOver = nbArr = new Array();
    for (i=1; i < args.length-1; i+=3) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = (img.MM_dn && args[i+2]) ? args[i+2] : ((args[i+1])? args[i+1] : img.MM_up);
      nbArr[nbArr.length] = img;
    }
  } else if (event == "out" ) {
    for (i=0; i < document.MM_nbOver.length; i++) {
      img = document.MM_nbOver[i]; img.src = (img.MM_dn) ? img.MM_dn : img.MM_up; }
  } else if (event == "down") {
    nbArr = document[grpName];
    if (nbArr)
      for (i=0; i < nbArr.length; i++) { img=nbArr[i]; img.src = img.MM_up; img.MM_dn = 0; }
    document[grpName] = nbArr = new Array();
    for (i=2; i < args.length-1; i+=2) if ((img = MM_findObj(args[i])) != null) {
      if (!img.MM_up) img.MM_up = img.src;
      img.src = img.MM_dn = (args[i+1])? args[i+1] : img.MM_up;
      nbArr[nbArr.length] = img;
  } }
}
//FUNCIONES LOGIN//
function opciones_usuario(){
	//alert('jano');
	ajax.resetData();
	ajax.setVar("page","home/user_option"); // recomended method of setting data to be parsed.
	ajax.setVar("print","1"); // recomended method of setting data to be parsed.
	ajax.requestFile = "./";
	ajax.method = 'POST';
	ajax.element = 'LoginUsuario';
	ajax.runAJAX();
}
function onchange_optionUser(){
	option = document.getElementById('OptionUser').value;
	//alert(option);
	var ajax = new sack();
	if(option == 10){
		window.location.href="./?unidad=0&seccion_id=MIS_DATOS_F";
	}
	if(option == 20){
		window.location.href="./?unidad=1000";
	}
	if(option == 30)
		window.location.href="./?page=admin/index";
	if(option == 40){
		window.location.href="./?unidad=0&seccion_id=INTRANET_F";
	}
	
	if(option == 50){
		window.location.href="./?func=userLogout&unidad=0";
	}
}
//FIN FUNCIONES LOGIN//
function cargaIntra(dir,id_curso){
	//alert('siii');
	//var ajax = new sack();
	resetVariable();
	setVariable("fpage","intranet/"+dir);
	setVariable("id_curso",id_curso);
	CargaComponente("RESULTADO");
	//alert(dir);
	
	
}
//FUNCION PARA LA AGENDA DE MI CURSO//
function cargaCalendario(id_curso,tipo){
	//alert(id_curso);
	m=document.getElementById("mes").value;
	//alert(m);
	resetVariable();
	if(tipo){
		setVariable("page","home/agenda_C");
	}else{
		t=document.getElementById("t_contenido").value;
		setVariable("t_cont",t);
		setVariable("fpage","intranet/agenda_C");
	}
	setVariable("mes",m);
	setVariable("id_curso",id_curso);
	
	CargaComponente("CALENDARIO");
}
function cargaContenido(anno,mes,dia,t_cont,id_curso,tipo){
	//alert(t_cont);
	resetVariable();
	if(tipo){
		setVariable("page","home/agenda_L");
	}else{
		setVariable("id_curso",id_curso);
		setVariable("fpage","intranet/agenda_L");
	}
	setVariable("anno",anno);
	setVariable("mes",mes);
	if(dia){
		setVariable("dia",dia);
	}
	setVariable("t_cont",t_cont);
	CargaComponente("listadoContenido");
}
function detalleAgenda(contenido_id,tipo){
	resetVariable();
	if(tipo){
		setVariable("page","home/agenda_D");
	}else{
		setVariable("fpage","intranet/agenda_D");
	}
	setVariable("contenido_id",contenido_id);
	CargaComponente("DivCuerpoF");
	cargaVentana("DivVentantaF","Detalle Actividad",550,230,310,250);
}
//FIN FUNCIONES AGENDA//
//FUNCION CAMBIA CURSO//
function changeCurso(){
	//alert('siii');
	cur=document.getElementById("curso_asig").value;
	//alert(cur);
	window.location.href="./?seccion_id=INTRANET_F&unidad=0&id_curso="+cur;	
	
}
//FIN FUNCION CAMBIA CURSO//
//CARGAR FORMULARIO//
function cargarFormulario(id){
	//alert(id);
	var contenedor="despliega_formulario";
	ajax.resetData();
	ajax.setVar("page","home/formulario");
	ajax.setVar("print","1");
	ajax.setVar("id_formulario",id);
	ajax.requestFile = "./";
	ajax.method = 'GET';
	ajax.element = contenedor;
	ajax.onLoading = whenLoading;
	ajax.onLoaded = whenLoaded; 
	ajax.onInteractive = whenInteractive;
	ajax.onCompletion = whenCompleted;
	ajax.runAJAX();
	
	document.getElementById(contenedor).style.position = "absolute";
	document.getElementById(contenedor).style.zindex = "5000";
	document.getElementById(contenedor).style.height = "550px";
	document.getElementById(contenedor).style.width = "458px";
	document.getElementById(contenedor).style.top = "120px";
	document.getElementById(contenedor).style.left = (document.body.clientWidth/2-230)+'px';
	document.getElementById(contenedor).style.display = "block";
}
//FIN CARGAR FORMULARIO
function FormularioContenido(anno,mes,dia,t_cont){
	alert(t_cont);
	document.getElementById('listadoContenido').style.height = "370";
	document.getElementById('listadoContenido').style.width = "460";
	document.getElementById('listadoContenido').style.top = "50";
	document.getElementById('listadoContenido').style.left = document.body.clientWidth/2-200;
	document.getElementById('listadoContenido').style.position = "absolute";
	document.getElementById('listadoContenido').style.display = "block";
	document.getElementById('listadoContenido').src = "./?page=home/agenda_L&unidad=0&print=1&t_cont="+t_cont+"&anno="+anno+"&mes="+mes+"&dia="+dia;
}
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];}}
}
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;
}
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;
}
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];}
}
//MUESTRA EL SHOWRROM DE VIVIENDA//
function load_vivienda(id){
	resetVariable();
	setVariable("vivienda_id",id);
	setVariable("fpage","home/contenido_vivienda");
	CargaComponente("FRONTMAIN");
}
function onKeyPressBlockLeters(e,obj)
{
	//onKeyPress='return onKeyPressBlockLeters(event,this);'
	var key = window.event ? e.keyCode : e.which;4
	var keychar = String.fromCharCode(key);
	if(key!=8){		
		reg =/^[0-9]+/;
		return reg.test(keychar);
	}
	//alert(/^\d+\.?\d*$/.test('0.'));
}
function onKeyPressBlockNum(e,obj)
{
	//onKeyPress='return onKeyPressBlockLeters(event,this);'	
	var key = window.event ? e.keyCode : e.which;4
	if(key!=8){
		var keychar = String.fromCharCode(key);
		reg =/[A-Za-z\sñ']/;
		return reg.test(keychar);
	}
	//alert(/^\d+\.?\d*$/.test('0.'));
}
function alfaNumerico(e,obj)
{
	//onKeyPress='return onKeyPressBlockLeters(event,this);'	
	var key = window.event ? e.keyCode : e.which;4
	if(key!=8){
		var keychar = String.fromCharCode(key);
		reg =/[A-Za-z\sñ'0-9]/;
		return reg.test(keychar);
	}
	//alert(/^\d+\.?\d*$/.test('0.'));
}
function alfaNumericoMail(e,obj)
{
	//onKeyPress='return onKeyPressBlockLeters(event,this);'	
	var key = window.event ? e.keyCode : e.which;4

	if(key!=8){
		var keychar = String.fromCharCode(key);
		reg =/[A-Za-z0-9@_.-]/;
		return reg.test(keychar);
	}
	//alert(/^\d+\.?\d*$/.test('0.'));
}

function Validate_String(string, return_invalid_chars)
         {
         valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         invalid_chars = '';
         
         if(string == null || string == '')
            return(true);
         
         //For every character on the string.   
         for(index = 0; index < string.length; index++)
            {
            char = string.substr(index, 1);                        
            
            //Is it a valid character?
            if(valid_chars.indexOf(char) == -1)
              {
              //If not, is it already on the list of invalid characters?
              if(invalid_chars.indexOf(char) == -1)
                {
                //If it's not, add it.
                if(invalid_chars == '')
                   invalid_chars += char;
                else
                   invalid_chars += ', ' + char;
                }
              }
            }                     
            
         //If the string does not contain invalid characters, the function will return true.
         //If it does, it will either return false or a list of the invalid characters used
         //in the string, depending on the value of the second parameter.
         if(return_invalid_chars == true && invalid_chars != '')
           {
           last_comma = invalid_chars.lastIndexOf(',');
           
           if(last_comma != -1)
              invalid_chars = invalid_chars.substr(0, $last_comma) + 
              ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
                      
           return(invalid_chars);
           }
         else
           return(invalid_chars == ''); 
         }


function Validate_Email_Address2(email_address)
         {
		//Assumes that valid email addresses consist of user_name@domain.tld
         at = email_address.indexOf('@');
         dot = email_address.indexOf('.');
         
         if(at == -1 || 
            dot == -1 || 
            dot <= at + 1 ||
            dot == 0 || 
            dot == email_address.length - 1)
            return(false);
            
         user_name = email_address.substr(0, at);
         domain_name = email_address.substr(at + 1, email_address.length);                  
         
         if(Validate_String(user_name) === false || 
            Validate_String(domain_name) === false){
			alert('EMAIL INVALIDO');
            return(false);                     
		 }
         return(true);
}
