function enable(name, vf, vt){
	var from = document.getElementById(vf);
	var to = document.getElementById(vt);
	if(((from==null) || (from.value=="")) && ((to==null) || (to.value==""))){
		document.getElementById(name).disabled = true;
	}else{
		document.getElementById(name).disabled = false;
		}
}

function validate(idFrom, idTo, idForm){
	var allOk = true;
	if(validateNumber(idFrom) && validateNumber(idTo)){
		document.getElementById(idForm).submit();
	}
	return allOk;
}

function validateNumber(id){
	var num = document.getElementById(id);
	var allOk = true;
	var expresion = "^([0-9]{1,})$";
		
	if(num!=null && num.value!=""){	
		if (!num.value.match(expresion)) {
			document.getElementById('messageError').style.display="block";
			allOk = false;
		}
	}
	return allOk;
}

function handleBrokenImages(imgId, section){
	if (section=="C"){
		document.getElementById(imgId).src = standardSectionNoImageFound;
	}
	if (section=="B"){
		document.getElementById(imgId).src = highlightedSectionNoImageFound;
	}
}

function changeListingImage(imageId, imageUrlTemplate, imageNumber){
	document.getElementById(imageId).src = imageUrlTemplate.replace("###", imageNumber);
	document.getElementById("photoNumberId" + imageId).innerHTML = imageNumber;
}

function moveNextListingImage(imageId, imageUrlTemplate, photosCount, oActualImageNumber){
	if (oActualImageNumber.value == photosCount){
		oActualImageNumber.value = 1;
	}
	else{
		oActualImageNumber.value++;
	}
	changeListingImage(imageId, imageUrlTemplate, oActualImageNumber.value);
}

function movePreviousListingImage(imageId, imageUrlTemplate, photosCount, oActualImageNumber){
	if (oActualImageNumber.value == 1){
		oActualImageNumber.value = photosCount;
	}
	else{
		oActualImageNumber.value--;
	}
	changeListingImage(imageId, imageUrlTemplate, oActualImageNumber.value);
}

function setContactFormDiv(jsonString, divPrefix, contactUri, visitsCounterUri){
	var jsonObj = eval('(' + jsonString + ')');
	document.getElementById(divPrefix + "title").innerHTML = jsonObj.title;
	document.getElementById(divPrefix + "photo").src = jsonObj.imagePath;
	document.getElementById(divPrefix + "photo").title = jsonObj.imageTitle;
	document.getElementById(divPrefix + "price").innerHTML = jsonObj.price;
	document.getElementById(divPrefix + "condition").innerHTML = jsonObj.condition;
	document.getElementById(divPrefix + "visits").innerHTML = jsonObj.visits;
	document.getElementById(divPrefix + "published").innerHTML = jsonObj.published;
	
	document.getElementById("iContacto").src = contactUri;
	loadInto(visitsCounterUri + '&rnd=' + parseInt(Math.random()*1000000000000));				

}

function closePopup(){
	document.getElementById("iContacto").src="";
	displayPopup('datosV','transparent');
	hidePopUp('transparent');
	hidePopUp('transparent');
	return false;
}

function fixFilters(){
	var ids = document.getElementById("filtersToHide").value.split(";");
	for (var i=0; i<ids.length; i++){
		if (ids[i]!=''){
			document.getElementById("C" + ids[i]).style.display='';
			document.getElementById("D" + ids[i]).style.display='none';
		}
	}
}

function $id(id){return document.getElementById(id)}

function setSizeDivTransparent()
{
	var arraySize = getPageSize();
	document.getElementById('transparent').style.height = arraySize[1] + "px";
	document.getElementById('transparent').style.width = arraySize[0]+ "px";
}

//
//getPageSize()
//Returns array with page width, height and window width, height
//Core code from - quirksmode.org
//Edit for Firefox by pHaez
//
function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function hideFilterComboboxes() {
	if (isIE()) {
		var filters=document.getElementsByTagName('select');
		for (var i=0; i < filters.length; i++) {
			filters[i].style.visibility='hidden';
		}
	}
}
function showFilterComboboxes() {
	if (isIE()) {
		var filters=document.getElementsByTagName('select');
		for (var i=0; i < filters.length; i++) {
			filters[i].style.visibility='visible';
		}
	}
}

function isIE() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function  notifyTitleMissing(url){
	$.ajax({ url:url,
			 async : true,
			 type:'GET'		
			}
	);
}

function spanishDate(d)
{
	var weekday=["Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado"];
	var monthname=["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
	return weekday[d.getDay()]+", "+d.getDate()+" de "+monthname[d.getMonth()]+" de "+d.getFullYear()
}

