// JavaScript EnlaceCanada

//Funcion para eliminar caracteres especiales
function trim(s) { 
	return s.replace( /^\s*/, "" ).replace( /\s*$/, "" ).replace(/\'/," ").replace(/\"/," "); 
} 


//Funcion para tomar variables pasadas por metodo POST
function verifica(objeto,mensaje){
	var res = false;
	if(objeto.value.length>0){
		res=true;
	}else{
		res=false;
		alert("Por Favor, "+mensaje);
		objeto.focus();
	}
	return res;
}
//FUncion para mostrar info de recordatorio
function Ver_Tipo(a){
	aux = a.me_mes_type.value;
	if(aux==2){
		document.getElementById("fec_reco").style.display = '';
	}else{
		document.getElementById("fec_reco").style.display = 'none';
	}
}

//Funcion para permitir cambiar el color de las filas
function cambiacolor(a,b){
	if(b==1){
		document.getElementById(a).style.backgroundColor="#66CCCC";
	}
	if(b==2){
		document.getElementById(a).style.backgroundColor="#FFFFFF";
	}
}
//function para desplegar tabla para subir y ver archivos de carpeta
function VerBox(muestra,oculta,variable,valor){
	document.getElementById(oculta).style.display = 'none';
	document.getElementById(muestra).style.display = '';
	document.getElementById(variable).value= valor;
	if(valor==0){
		document.getElementById("ba_mas_caja").style.display = 'none';
	}
	if(valor==1){
		document.getElementById("ba_mas_caja").style.display = '';	
	}
}
function VerBuscador(muestra,oculta,variable,valor,ver){
	document.getElementById(oculta).style.display = 'none';
	document.getElementById(muestra).style.display = '';
	//document.getElementById(variable).style.display = valor;
	parent.taman(ver);


}


function valida_busca_sise(a){
	var f1="";
	var f2="";
	var bien=false;
	var rango1=false;
	var rango2=false;
	
	a.einfo_id.value="-1";
	//a.rowsold.value="-1";

	
	if(a.ba_mas.value=="1"){
		if( ((a.busca_anoi.value!="0") && (a.busca_mesi.value!="0") && (a.busca_diai.value!="0")) || ((a.busca_anof.value!="0") && (a.busca_mesf.value!="0") && (a.busca_diaf.value!="0"))){
			var b1=false;
			var b2=false;
			if( (a.busca_anoi.value!="0") && (a.busca_mesi.value!="0") && (a.busca_diai.value!="0") ){
				f1 = a.busca_mesi.value+"/"+a.busca_diai.value+"/"+a.busca_anoi.value;
				if(isDate(f1)){
					rango1=true;
					b1=true;
				}else{
					alert("Ingrese una Fecha de Inicio Valida");	
					b1=false;
				}
			}else{
				b1=true;	
			}
			if( (a.busca_anof.value!="0") && (a.busca_mesf.value!="0") && (a.busca_diaf.value!="0") ){
				f2 = a.busca_mesf.value+"/"+a.busca_diaf.value+"/"+a.busca_anof.value;
				if(isDate(f2)){
					rango2=true;
					b2=true;
				}else{
					alert("Ingrese una Fecha de Fin Valida");	
					b2=false;
				}
			}else{
				b2=true;	
			}
			if(b1 && b2){
				if(rango1 && rango2){
					var ano1=(a.busca_anoi.value)*1;
					var ano2=(a.busca_anof.value)*1;
					var mes1=(a.busca_mesi.value)*1;
					var mes2=(a.busca_mesf.value)*1;
					var dia1=(a.busca_diai.value)*1;
					var dia2=(a.busca_diaf.value)*1;
					
					if(ano1==ano2){
						if(mes1==mes2){
							if(dia1<=dia2){
								bien=true;
							}else{
								alert("El Rango de Fechas es Incorrecto");
								bien=false;
							}	
						}else{
							if(mes1<mes2){
								bien=true;
							}else{
								alert("El Rango de Fechas es Incorrecto");
								bien=false;
							}	
						}	
					}else{
						if(ano1<ano2){
							bien=true;
						}else{
							alert("El Rango de Fechas es Incorrecto");
							bien=false;
						}	
					}
				}else{
					bien=true;	
				}
			}else{
				bien=false;	
			}
		}else{
			bien=true;	
		}
	}else{
		bien=true;	
	}
	if(bien){
		a.target="iframppal";
//		alert(a.target);
		a.submit();	
	}
}




// Validar FECHAS
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}


//Funcion para seleccionar y mostrar informacion del aplicante etapa3
function Resalta_dos(a,posicion,einfo_id){
	var old = a.rowsold.value;
	if(old!=-1){
		old=""+old;
	}
	document.getElementById("rows"+posicion).style.backgroundColor="#66CCCC";
	a.rowsold.value=posicion;
	a.einfo_id.value=einfo_id;
//	a.action="index.jsp";
//	a.action="resultado.jsp";
//	a.target="iframresul";
//	alert(a.target);
//	a.submit();
//	parent.frames['iframresul'].location.href="resultado.jsp?einfo_id="+einfo_id;
	window.open('resultado.jsp?einfo_id='+einfo_id);
}

function Borrar_usuario(a){
	if(confirm("żEsta seguro que desea Eliminar a este Usuario?\n\nRecuerde que toda la informacion relacionada con el mismo se eliminara\n\nżDesea Continuar?")){
		a.submit();	
	}	
}

function ver_res_qb_ca(ver,a){
	if(ver==1){
		a.action="res_quebec.jsp";	
	}	
	if(ver==2){
		a.action="res_canada.jsp";	
	}	
	a.submit();
}

//Funcion para validar al enviar un mensaje
function valida_mensaje(a,donde){
	a.me_mes_title.value=trim(a.me_mes_title.value);
	a.me_mes_body.value=trim(a.me_mes_body.value);

  	var selectedArray = new Array();
  	var selObj = a.copia_o;
  	var i;
  	var count = 0;
  	for (i=0; i<selObj.options.length; i++) {
    	if (selObj.options[i].selected) {
      		selectedArray[count] = selObj.options[i].value;
      		count++;
    	}
  	}
  a.copia.value = selectedArray;
	
	if(verifica(a.me_mes_title,"introduzca el Titulo del Mensaje")){
		a.action=donde;
		a.submit();
	}
}

function ver_res_sedav(a){
	document.getElementById(a).style.display = '';
}

function valida_cambio_res(a){
	if(a.ap_ppal.value!=-1){
		if(a.ap_cate.value!=-1){
			if(a.ap_dest.value!=-1){
				if(a.ap_condicion.value!=-1){
					if(a.ap_honorario.value.length>0){
						if(!isNaN(a.ap_honorario.value)){
							a.submit();	
						}else{
							alert("Por favor, ingrese solo numeros en el campo honorarios, utilice el . como separador de decimales");
							a.ap_honorario.focus();
						}
					}else{
						a.submit();	
					}	
				}else{
					alert("Por favor, seleccione la condicion");
					a.ap_condicion.focus();
				}	
			}else{
				alert("Por favor, seleccione el destino");
				a.ap_dest.focus();
			}	
		}else{
			alert("Por favor, seleccione la categoria");
			a.ap_cate.focus();
		}	
	}else{
		alert("Por favor, seleccione quien sera el aplicante principal");
		a.ap_ppal.focus();
	}	
}

function Reactivar(user){
	if(confirm("Esta seguro que desea reactivar el cuestionario del usuario "+user+" ?")){
		window.open('rehabilita.jsp?lc_user_login='+user,'','width=500,height=200');	
	}	
}
function CartaInver(a){
	tipo=a.re_email.value;
	if(tipo==1){
		if(confirm("Esta seguro que desea enviar la carta de aceptacion como inversionista a esta persona ?")){
			a.submit();
		}
	}else{
		if(confirm("Esta seguro que desea enviar la carta de aceptacion como Trabajador a esta persona ?")){
			window.open('carta_trab.jsp?lc_user_login='+a.lc_user_login.value+'&id='+a.idt.value+'&principal='+a.principal.value+'&destino='+a.destt.value+'&condicion='+a.condt.value,'','width=500,height=200');	
		}	
	}
}
function valida_env_carta(login,id,a,b,c){
	if(confirm("Esta seguro que desea enviar la carta de aceptacion como Trabajador a esta persona ?")){
		window.open('carta_trab.jsp?lc_user_login='+login+'&id='+id+'&principal='+a+'&destino='+b+'&condicion='+c,'','width=500,height=200');	
	}	
}

function envia_club(a){
	a.action="club.jsp";
	a.submit();
}
function envia_conf(a){
	a.action="confe.jsp";
	a.submit();
}

function ver_estat(a){
	var veo = a.busca_dest.value;
	if(veo==2){
		document.getElementById("st_ca").style.display="";
		document.getElementById("st_qb").style.display="none";
	}
	if(veo==1){
		document.getElementById("st_ca").style.display="none";
		document.getElementById("st_qb").style.display="";
	}
}
