var arrAdjuntosIds   = new Array();
var arrDescripciones = new Array();
var arrEpigrafes     = new Array();

function expandAdvertisment() {
	var newAd = document.getElementById("newAdvertisment");
	if (newAd) {
		if (newAd.style.display == "none") {
			newAd.style.display = "";
		}
		else {
			newAd.style.display = "none";
		}
	}
}

function collapseAdvertisment() {
	var newAd = document.getElementById("newAdvertisment");
	if (newAd) {
		newAd.style.display = "none";
	}
}

function insertarAdjunto() {
	document.getElementById("adjuntos").selectedIndex = 0;
	document.getElementById("input-Eliminar").style.display = "none";
	editarAdjunto();
}

function editarAdjunto() {
	indice = document.getElementById("adjuntos").selectedIndex;
	var form = document.forms["newAdjunto"];
	form.idAdjunto.value = arrAdjuntosIds[indice];
	if (indice == 0) {
		document.getElementById("ul-File").style.display = "block";
		document.getElementById("input-Eliminar").style.display = "none";
	}
	else {
		document.getElementById("ul-File").style.display = "none";
		document.getElementById("input-Eliminar").style.display = "block";
	}
	document.getElementById("ul-Tit").style.display = "block";
	document.getElementById("ul-Epi").style.display = "block";
}

function eliminarAdjunto(id) {
	indice = id;
	if (indice > 0) {
		var form = document.forms["infoAdvertisment"];
		form.accion.value = "delete";
		form.idAdjunto.value = id;
		form.submit();
	}
	else {
		alert("Selección no válida.")
	}
}

function validaAdjunto() {
	var form = document.forms["newAdjunto"];
	if (form.descripcion.value == "") {
		alert("Escriba el título de la imagen.");
		form.descripcion.focus();
		return false;
	}
	if (form.epigrafe.value == "") {
		alert("Escriba el epígrafe de la imagen.");
		form.epigrafe.focus();
		return false;
	}
	return true;
}

function validaAdvertisment() {
	var form = document.forms["infoAdvertisment"];
	if (form.title.value == "") {
		alert("Escriba el título de su anuncio.");
		form.title.focus();
		return false;
	}
	if (form.idAdvertismentType.value == 0) {
		alert("Seleccione el tipo de Clasificado");
		form.idAdvertismentType.focus();
		return false;
	}
	if (form.idCMSEstado.value == 0) {
		alert("Seleccione el estado de su anuncio.");
		form.idCMSEstado.focus();
		return false;
	}
	if (form.idCoreExchange.options.selectedIndex <= 0) {
		alert("Seleccione la moneda para el valor del anuncio.");
		form.idCoreExchange.focus();
		return false;
	}
	if (!/^([0-9])*$/.test(form.valor.value)) {
		alert("El valor " + form.valor.value + " no es un número válido");
		form.valor.focus();
		return false;
	}
	if (form.description.value == "") {
		alert("Escriba la descripción para su anuncio.");
		form.description.focus();
		return false;
	}
	return true;
}

function guardarAdjunto() {
	if (validaAdjunto()) {
		indice = document.getElementById("adjuntos").selectedIndex;
		var form = document.forms["newAdjunto"];
		form.accion.value = "save";
		form.submit();
	}
}

function guardarAdvertisment() {
	if (validaAdvertisment()) {
		var form = document.forms["infoAdvertisment"];
		form.submit();
	}
}

function resetForm() {
	var form = document.forms["infoAdvertisment"];
	form.idAdvertisment.value     = "";
	form.idAdvertismentType.value = "";
	form.title.value              = "";
	form.idCMSEstado.value        = "";
	form.addDate.value            = "";
	form.idCoreExchange.value     = "";
	form.valor.value              = "";
	form.description.value        = "";
	form.detail.value             = "";
	document.getElementById("ul-Nuevo").style.display = "block";
	document.getElementById("ul-Editar").style.display = "none";
}

function showemail() {
	document.getElementById("sendAdvertisment").style.display = "block";
}

function cancelemail() {
	document.getElementById("sendAdvertisment").style.display = "none";
}

function changepic(idAdjunto) {
	var id = "foto_"+idAdjunto,
		foto, minifoto, i;
	for (i = 0; i < arrAdjuntos.length; i++) {
		foto = document.getElementById("foto_"+arrAdjuntos[i]);
		minifoto = document.getElementById("minifoto_"+arrAdjuntos[i]);
		if (foto.id == id) {
			foto.style.display = "block";
			minifoto.style.opacity = "1";
			minifoto.style.MozOpacity = "1";
			minifoto.style.filter = "alpha(opacity=100)";
		}
		else {
			foto.style.display = "none";
			minifoto.style.opacity = "0.5";
			minifoto.style.MozOpacity = "0.5";
			minifoto.style.filter = "alpha(opacity=50)";
		}
	}
}

// Inicializar valores según el número de la vista de Front en carrusel
var actualGaleria = 0,
	ultimaGaleria = 6,
	primeraGaleria = 0;
function girarGaleria(accion) {
	switch (accion) {
		case "back":
			if (primeraGaleria > 0) {
				--ultimaGaleria;
				--primeraGaleria;
				document.getElementById("back").style.display = "block";
				document.getElementById("go").style.display = "block";
			}
			if (primeraGaleria == 0) {
				document.getElementById("back").style.display = "none";
				document.getElementById("go").style.display = "block";
			}
		break;

		case "go":
			if (ultimaGaleria < arrAdjuntos.length) {
				++ultimaGaleria;
				++primeraGaleria;
				document.getElementById("back").style.display = "block";
				document.getElementById("go").style.display = "block";
			}
			if (ultimaGaleria == arrAdjuntos.length) {
				document.getElementById("back").style.display = "block";
				document.getElementById("go").style.display = "none";
			}
		break;
	}
	if (primeraGaleria >= 0 && ultimaGaleria <= arrAdjuntos.length) {
		var otra, i;
		for (i = 0; i < arrAdjuntos.length; ++i) {
			otra = document.getElementById("minifoto_"+arrAdjuntos[i]);
			otra.style.display = ((i >= primeraGaleria && i < ultimaGaleria)? "": "none");
		}
	}
}

function closeprint() {
	window.parent.closePopUp();
}

function goprint() {
	window.print();
	window.close();
}

function buscar_advertisment() {
	var form = document.forms["filtro"];
	form.text.value = document.getElementById("buscador").value;
	form.dateOR.value = document.getElementById("dateOR").value;
	form.titleOR.value = document.getElementById("titleOR").value;
	form.submit();
}

function expandAbuso(idAviso) {
	document.getElementById("report-abuso_"+idAviso).style.display = "none";
	document.getElementById("abuso_"+idAviso).style.display = "block";
}

function reportAbuso(idAviso) {
	var src = "../_post/hc_washington/reportar_aviso.php?idAviso="+idAviso;
	window._post(src);
}

function collapseAbuso(idAviso) {
	document.getElementById("report-abuso_"+idAviso).style.display = "block";
	document.getElementById("abuso_"+idAviso).style.display = "none";
}
function PopUpGaleria(src, iframe_name) {
	if (typeof(window.parent.objAdministratorBar) == "undefined") {
		if (typeof(iframe_name) == "undefined") {
			iframe_name = (Math.random()*10000).toString();
		}
		window.scrollH = Number(document.documentElement.scrollTop || document.body.scrollTop);
		document.body.scroll = "no";
		block();
		var strFrame = isMSIE()? '<iframe name="'+iframe_name+'" frameborder="0" allowtransparency="true">': "iframe";
		objPopup = document.createElement(strFrame);
		objPopup.setAttribute("frameborder", "0");
		objPopup.setAttribute("scrolling", "no");
		objPopup.setAttribute("allowtransparency", "true");
		objPopup.name = iframe_name;
		objPopup.src = src;
		objPopup.style.position = "absolute";
		objPopup.style.height = "800px";
		objPopup.style.width = "100%";
		objPopup.style.top = "200px";
		objPopup.style.left = "600px";
		document.body.appendChild(objPopup);
	}
	else {
		window.location = src;
	}
}

function jsPopupEnviarClasificado(idClasificado) {
	nombre 			=  document.getElementById('formNombre');
	remitente 		= document.getElementById('formRemitente');
	destinatarios 	= document.getElementById('formDestinatarios');
	//subject 		= document.getElementById('formSubject');
	mensaje 		= document.getElementById('formMensaje');
	bool 			= true;
	
	if(bool && nombre.value == ""){
		alert("Por favor, ingrese su nombre");
		nombre.focus();
		bool = false;
	}
	
	if(bool && remitente.value == ""){
		alert("Por favor, ingrese un email como remitente");
		remitente.focus();
		bool = false;
	}
	
	var strRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if (bool && !strRegExp.test(remitente.value) ){
		alert("Por favor, ingrese un email valido como remitente");
		remitente.focus();
		bool = false;
	}
	
	if(bool && destinatarios.value == ""){
		alert("Por favor, ingrese al menos un destinatario");
		destinatarios.focus();
		bool = false;
	}
	
	if (bool && !strRegExp.test(destinatarios.value) ){
		alert("Por favor, ingrese un email valido como destinatario");
		destinatarios.focus();
		bool = false;
	}
	/*
	if(bool && subject.value == ""){
		alert("Por favor, ingrese un asunto al envio");
		subject.focus();
		bool = false;
	}
	*/
	if(bool){
		window._post("../_post/recomendar_clasificado.php?idClasificado="+idClasificado+"&email="+remitente.value+"&emailTo="+destinatarios.value+"&comentarios="+mensaje.value+"&nombre="+nombre.value);
	}
}
