/* funciones auxiliares */


//muestra una foto en grande
function mostrar_foto(src)
{
	ajax_load("programadoresfreelance.php","oper=mostrar_foto&src="+src,"contenedor");
	contenedor.style.left=100;
	contenedor.style.top=200;
	ocultar("contenedor","display");
}


function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}
	
function elegir_fecha(target)
{	
	ajax_load("programadoresfreelance.php","oper=seleccionar_fecha&fecha_target="+target,"contenedor");	
	var contenedor=document.getElementById("contenedor");
	//contenedor.style.display="block";	
	var destino=document.getElementById(target);	
	var pos=getPosition(destino);		
	contenedor.style.left=pos.x;
	contenedor.style.top=pos.y-5;		
	ocultar("contenedor","display");
}

function validar_fecha(fecha_target)
{	
	var separador="/";
	var formu=document.formu_fecha;
	var dia=formu.dia;
	var mes=formu.mes;
	var ano=formu.ano;
	if (dia.selectedIndex <= 0 || mes.selectedIndex <= 0 || ano.selectedIndex <= 0)
	{
		alert ("Por favor seleccione una fecha valida");
		return false;
	}
	var target=document.getElementById(fecha_target);
	target.value=dia.options[dia.selectedIndex].value + separador + mes.options[mes.selectedIndex].value + separador + ano.options[ano.selectedIndex].value;
	if (isDate(target.value))
		ocultar("contenedor","display");
	else
		alert ("La fecha ingresada no es valida");
	
}


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 strDay=dtStr.substring(0,pos1)
	var strMonth=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){		
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){		
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){		
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){		
		return false
	}
	return true
}

function recomendar_oferta(id_oferta,id_usuario,emails,target)
{	
	ajax_load("programadoresfreelance.php","oper=recomendar_oferta&id_oferta="+id_oferta+"&emails="+emails,"contenedor");	
	var contenedor=document.getElementById("contenedor");
	contenedor.style.display="block";	
	var destino=document.getElementById(target);	
	var pos=getPosition(destino);		
	contenedor.style.left=pos.x;
	contenedor.style.top=pos.y-5;		
}

//dado un id de un objeto html, un caracter a reemplazar y un caracter por el cual reemplazar busca en el objeto todas las ocurrencias del caracter y las reemplaza
function reemplazar(id,aguja,pajar)
{
	var obj=document.getElementById(id);
	var	texto=obj.value;
	//var def_texto= texto.replace(/\n/g, "<br />"); 
	var def_texto=texto.replace(aguja, pajar); 
	obj.value = def_texto; 	
}

function email_valido(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}

//valida un conjunto de emails ingresados en un textarea
function validar_emails(id)
{
	var obj=document.getElementById(id);
	var texto=obj.value;
	var texto2=texto.replace(/\n/g,",");
	var email="";
	
	var arr=texto2.split(",");	
	for (var i=0; i < arr.length; i++)
	{
		email=arr[i];
		if (!email_valido(email))
		{			
			var msj=email + " no es un email valido ingrese 1 email por linea sin espacios intermedios";
			alert (msj.replace(/\n/g,""));
			obj.focus();
			return false;
		} 
			
	}
	reemplazar(id,/\n/g,",");
	return true;
}

function validar_entrada_recomendacion()
{	
	var remitente=document.getElementById("remitente");	
	if (!email_valido(remitente.value))
	{
		alert (remitente.value + " no es un email valido");
		remitente.focus();
		return false;
	}
	return (validar_emails("emails"));			
}

function validar_longitud(obj,longitud_minima)
{
	if (obj.value.length < longitud_minima)
	{
		alert (obj.name + " debe tener al menos " + longitud_minima + " caracteres");
		obj.focus();
		return false;
	} else {
		return true;
	}
}
