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();
}
MM_reloadPage(true);

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_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' debe contener una direccion de e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' debe contener numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' debe contener numeros entre '+min+' y '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' es requerido.\n'; }
  } if (errors) alert('Los siguientes error(es) han ocurrido:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function validarNumero(){
	if(event.keyCode == 44 || event.keyCode == 46)
		event.keyCode = 46;
  	else if((event.keyCode <= 47 || event.keyCode >= 58) && event.keyCode > 31)
		event.keyCode = 0;
}

function selectCambio(control, controlToPopulate, ItemArray){
	var myEle ;
	var x ;

	controlToPopulate.length=0;
	controlToPopulate.options[0]=new Option("[SELECCIONA]","","defauldSelected");

//	for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;
/*	myEle = document.createElement("option") ;
	myEle.value = "" ;
	myEle.text = "[Selecciona]" ;
	controlToPopulate.add(myEle) ;
*/
	i=0;
	for ( x = 0 ; x < ItemArray.length ; x++ ){
		if ( ItemArray[x][0] == control.value ){
			document.getElementById(controlToPopulate.id)[i+1]=new Option(ItemArray[x][1],ItemArray[x][1],false,false);
			i++;
/*
			myEle = document.createElement("option") ;
			myEle.value = ItemArray[x][1] ;
			myEle.text = ItemArray[x][1] ;
			controlToPopulate.add(myEle) ;
*/
		}
	}
}

function guardarInmueble(){
	var faltantes="";
	MM_validateForm('control','','R','edad','','NisNum','fecha_baja','','R','colonia','','R','direccion','','R','ext','','R','cp','','RisNum','recamaras','','NisNum','bano','','NisNum','cochera','','NisNum','frente','','NisNum','fondo','','NisNum','terreno','','NisNum','construccion','','NisNum','locales','','NisNum','oficinas','','NisNum','pisos','','NisNum','departamentos','','NisNum','valor','','NisNum','precio','','RisNum');
	if(document.MM_returnValue){
		if(DiferenciaFechas(document.agregar_inmueble.fecha_baja.value,document.agregar_inmueble.fecha_alta.value)>120)
			faltantes+="- Fecha de Vencimiento Mayor a 120 dias\n";
		if(document.agregar_inmueble.inmueble.options[document.agregar_inmueble.inmueble.selectedIndex].value=="")
			faltantes+="- Falta Seleccionar el Tipo de Inmueble\n";
		if(document.agregar_inmueble.accion.options[document.agregar_inmueble.accion.selectedIndex].value=="")
			faltantes+="- Necesitas Seleccionar el Tipo de operación a Realizar\n";
		if(document.agregar_inmueble.suelo.options[document.agregar_inmueble.suelo.selectedIndex].value=="")
			faltantes+="- Necesitas Seleccionar el Tipo de Suelo\n";
		if(document.agregar_inmueble.asesor.options[document.agregar_inmueble.asesor.selectedIndex].value=="")
			faltantes+="- Necesitas Seleccionar el Promotor Responsable\n";
		if(document.agregar_inmueble.estado.options[document.agregar_inmueble.estado.selectedIndex].value=="")
			faltantes+="- Necesitas Seleccionar el Estado del Inmueble\n";
		if(document.agregar_inmueble.municipio.options[document.agregar_inmueble.municipio.selectedIndex].value=="")
			faltantes+="- Necesitas Seleccionar el Municipio del Inmueble\n";
		if((document.agregar_inmueble.precio==null || document.agregar_inmueble.precio.value==""))
			faltantes+="- Necesitas Capturar el Precio del Inmueble\n";
		if((document.agregar_inmueble.imagen1==null || document.agregar_inmueble.imagen1.value=="") && (document.agregar_inmueble.imagen==null || document.agregar_inmueble.imagen.value=="") )
			faltantes+="- Necesitas Agregar al menos una imagen\n";
			
		
		if(faltantes==""){
			if(document.agregar_inmueble.identificador.value=="")
				document.agregar_inmueble.boton.value="Guardar";
			else
				document.agregar_inmueble.boton.value="Modificar";
			document.agregar_inmueble.submit();
		}else
			alert(faltantes);
	}else{
		document.MM_returnValue
	}
}

function modificarAsesor(valor){
	document.modificar.action="agregar_asesor.php";
	document.modificar.id.value=valor;
	document.modificar.submit();
}

function DiferenciaFechas (CadenaFecha1,CadenaFecha2) {
	var year;
   //Obtiene los datos del formulario
//   CadenaFecha1 = formulario.fecha1.value
//   CadenaFecha2 = formulario.fecha2.value
   
   //Obtiene dia, mes y año
   var fecha1 = new fecha( CadenaFecha1 )   
   var fecha2 = new fecha( CadenaFecha2 )

/*   var today = new Date()
   var year = today.getYear()
   if(year<1000) year+=1900
   year,today.getDate(),(today.getMonth()+1) 
 */    
   //Obtiene objetos Date
   var miFecha1 = new Date( fecha1.anio, fecha1.mes, fecha1.dia )
   var miFecha2 = new Date( fecha2.anio, fecha2.mes, fecha2.dia )
//   var miFecha2 = new Date( fecha2.anio, fecha2.mes, fecha2.dia )

   //Resta fechas y redondea
   var diferencia = miFecha1.getTime() - miFecha2.getTime()
   var dias = Math.floor(diferencia / (1000 * 60 * 60 * 24))
//   var segundos = Math.floor(diferencia / 1000)
//   alert ('La diferencia es de ' + dias + ' dias,\no ' + segundos + ' segundos.')
   
   return dias
}

function fecha( cadena ) {
   //Separador para la introduccion de las fechas
   var separador = "-"
   //Separa por dia, mes y año
   if ( cadena.indexOf( separador ) != -1 ) {
        var posi1 = 0
        var posi2 = cadena.indexOf( separador, posi1 + 1 )
        var posi3 = cadena.indexOf( separador, posi2 + 1 )
        this.anio = cadena.substring( posi1, posi2 )
        this.mes = cadena.substring( posi2 + 1, posi3 )
        this.dia = cadena.substring( posi3 + 1, cadena.length )
   } else {
        this.dia = 0
        this.mes = 0
        this.anio = 0   
   }
}
