function alterna(type)
{
	if(type==1)
	{
		document.getElementById('elemento2').style.display='none';
		document.getElementById('texto').innerHTML='Video';
	}
	else
	{
		document.getElementById('elemento2').style.display='';
		document.getElementById('texto').innerHTML='Audio';
	}
}

function checkFiles(archivo1, archivo2, tipo, button)
{
	var images = new Array('gif', 'jpg', 'png', 'jpeg');
	var audio = new Array('mp3');
	var video = new Array('avi','divx','mov','mpeg','flv','mpg','wmv');

	var result = false;

	if(archivo2!="")
	{

		var type = document.getElementById(tipo).value;

		if(type==1)
		{
			result = fileFilter(document.getElementById(archivo1).value,  video, true);
		}
		else
		{
			result  = fileFilter(document.getElementById(archivo1).value,  audio, true);
			result &= fileFilter(document.getElementById(archivo2).value,  images, true);
		}
	}
	else if(tipo=="multimedia")
	{
			result  = fileFilter(document.getElementById(archivo1).value,  audio, false);
			result |= fileFilter(document.getElementById(archivo1).value,  video, false);
	}
	else if(tipo=="imagenes")
	{
			result  = fileFilter(document.getElementById(archivo1).value,  images, true);
	}
	
	if(result)
	{
		document.getElementById(button).disabled=true;
		document.getElementById(button).value='Procesando...';
		return true;
	}
	else if(archivo2=="" && tipo=="")
	{
		alert("Fichero no valido");
	}
	
	return false;
}

function fileFilter(fileName, fileTypes, showErrors)
{
	var allowSubmit = false;
	var types = "";
	
	if (!fileName && showErrors)
	{
		alert('El fichero no puede estar vacio');
		return false;
	}
	
	ext = fileName.slice(fileName.lastIndexOf(".")+1).toLowerCase();

	for (var i = 0; i < fileTypes.length; i++)
	{
		types += ", "+fileTypes[i];
		if (fileTypes[i] == ext)
		{
			allowSubmit = true;
		}
	}

	if(!allowSubmit && showErrors)
	{
		alert("Tipo de fichero "+ext+" no es valido para este campo\r\nPor favor elige uno de la lista"+types);
		return false;
	}

	return allowSubmit;
}

function checkVideoFiles(archivo1, button)
{
	
	var video = new Array('avi','divx','mov','mpeg','flv','mpg','wmv');

	var result = false;

	result = fileFilter(document.getElementById(archivo1).value,  video, true);
	
	if(result)
	{
		document.getElementById(button).disabled=true;
		document.getElementById(button).value='Procesando...';
	}
	return result;
}

function compruebaYEnviaVideo(idFormulario, archivo1, button)
{
	if(checkVideoFiles(archivo1, button))
	{
		document.getElementById(idFormulario).submit();
	}
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
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, emptyDate)
{
	if(emptyDate==true && dtStr.length==0)
	{
		return true;
	}
	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)
	{
		alert("El formato de fecha es: dd/mm/yyyy");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12)
	{
		alert("El mes es invalido");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert("El dia es invalido");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
	{
		alert("Introduce un aņo de cuatro digitos entre "+minYear+" y "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert("Fecha incorrecta, introduzca una fecha correcta");
		return false;
	}
	now = new Date();
	thisDate = new Date(year,month,day);
	return thisDate>=now;
}

function contratoValido(contratados, actual)
{
		if(contratados==0 || contratados-actual>=0)
		{
			return true;
		}
		return false;
}

function isActived()
{
	validDate=isDate(document.getElementById('tiempo').value, true);
	validVisual=contratoValido(document.getElementById('visual_contratados').value, document.getElementById('visual').value);
	validClicks=contratoValido(document.getElementById('clicks_contratados').value, document.getElementById('clicks').value);

	if(validDate && validVisual && validClicks)
	{
		valid="Si, este banner estara activo";
	}
	else
	{
		valid="No, el banner no estara activo";
	}
	document.getElementById('activo').value=valid;

}

function refreshEliminarContenido()
{
	//Comprobacion estandar para comprobar si se ha intercambiado la info por ajax correctamente
	if (xmlHttpReq.readyState == 4 && xmlHttpReq.status == 200)
	{
		var xmldoc = xmlHttpReq.responseXML;
		var messageArray = getMessage(xmldoc);
		var code = messageArray[0];
		var message = messageArray[1];
		if(code<0)
		{
			alert(message);
		}
		else
		{
			document.getElementById("preliminar_"+code).style.display = 'none';
			document.getElementById("eliminar_"+code).style.display = 'none';
			alert("Contenido borrado correctamente");
		}
	}
	//Si hemos terminado de intercambiar la info pero algo ha fallado
	else if(xmlHttpReq.readyState == 4 && xmlHttpReq.status != 200)
	{
		alert("No se ha podido realizar la peticion, por favor contacte con edina y digales que ha fallado al intentar borrar un elemento multimedia (imagen, video, audio...) de su web\nDisculpe las molestias.");
	}
}

function eliminar_contenido(tipo, id, idFamilia, idRama)
{
	var strURL = "ajax/deleteContent.php";
	var datos = "contenido="+tipo+"&id="+id+"&famId="+idFamilia+"&ramaId="+idRama;
	doStandardAjaxRFN(strURL, datos, 'refreshEliminarContenido');
}
