function placeDate() {
	var mydate  = new Date();
	var year    = mydate.getFullYear();
	var day     = mydate.getDay();
	var month   = mydate.getMonth();
	var daym    = mydate.getDate();
	var hour    = mydate.getHours();
	var minutes = mydate.getMinutes(); 
	if (daym < 10) {
	  daym = '0'+daym;
	}
	if (minutes < 10) {
		minutes = '0'+minutes;
	}
	var dayarray = new Array("Domingo","Lunes","Martes","Miercoles","Jueves","Viernes","Sabado")
	var montharray = new Array("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre")
	var out = hour+':'+minutes+'&nbsp;hrs.&nbsp;'+dayarray[day]+" "+daym+" de "+montharray[month]+" del "+year;
	document.getElementById('date').innerHTML = out;
}
// Bookmarks this site made nearly crossbrowser
function bookmark(){
    var title="www.recomiendalos.com"
    var url="http://www.recomiendalos.com"

    if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if( window.opera && window.print ) {
    	var mbm = document.createElement('a');
    	mbm.setAttribute('rel','sidebar');
    	mbm.setAttribute('href',url);
    	mbm.setAttribute('title',title);
    	mbm.click();
    } else if( document.all ) {
		window.external.AddFavorite( url, title);
	}
}

// Function that prepopulates all needed dynamic boxes and sets needed attributes to elements
// typically used in body.onload
function populateBoxes () {
	populateCategories('categoryid', active_categories, -1);
	populateCountries('countryid', active_countries, -1);
	switchTab(); // add handlesrs
	placeDate();
}

// Functions that checks the quantity of options in select and hides it if there is no
// actual options
// selectId - is a DOM id of the select element
function setVisibility (selectId) {
	var select = document.getElementById(selectId);
	var parentTd = select.parentNode;
	if (select.options.length > 1) {
		parentTd.style.display = 'list-item';
	} else {
		parentTd.style.display = 'none';
	} 
}
// Function that populates categories select box 
// (/js/search_box_data.js should be included to html file for this function to work)
// elementDomId - id of a select element to populate
// categoriesArray - an array used to poulate categories box
// countryId - the initial selected category from a list 
// (should be -1 if no need to preselect any category) 
function populateCategories(elementDomId, categoriesArray, selectedId) {
	var select = document.getElementById(elementDomId);
//	alert(elementDomId);
	// clean previous data from element
	var options_qtty = select.options.length;
	for (var i = options_qtty; i > 0; i--) {
		select.options[i] = null;
	}

	// sort categories alphabetically
	var sortedCategoryNames = new Array();
	sortedCategoryNames     = sortedCategoryNames.concat(categoriesArray);
	sortedCategoryNames     = sortedCategoryNames.sort();
	var sortedCategories    = new Array();
	for (var catPosition = 0; catPosition < sortedCategoryNames.length; catPosition++) {
		for (var catId in categoriesArray) {
			if (categoriesArray[catId] == sortedCategoryNames[catPosition]) {
				sortedCategories[catPosition] = new Array(categoriesArray[catId], catId);
			}
		}
	} 
	// form element contents
	var i = 1;	
	for (var catPosition = 0; catPosition < sortedCategories.length; catPosition++) {
		select.options[i] = new Option(sortedCategories[catPosition][0], sortedCategories[catPosition][1]);
		if ((selectedId != -1) && (sortedCategories[catPosition][1] == selectedId)) {
			select.options[i].selected = true;
		}
		i++;
	}
}

// Function that populates subcategories select box based on category id
// (/js/search_box_data.js should be included to html file for this function to work)
// elementDomId - id of a select element to populate
// catId - number category id 
// selectedId - the initial selected category from a list 
// (should be -1 if no need to preselect any category) 
function populateSubcategories(elementDomId, subcategoriesArray, catId, selectedId) {
	var select = document.getElementById(elementDomId);
	// clean previous data from element
	var options_qtty = select.options.length;
	for (var i = options_qtty; i > 0; i--) {
		select.options[i] = null;
	}
	// form element contents
	var i = 1;
	for (var subcatId in subcategoriesArray[catId]) {
		select.options[i] = new Option(subcategoriesArray[catId][subcatId], subcatId);
		if ((selectedId != -1) && (subcatId == selectedId)) {
			select.options[i].selected = true;
		}
		i++;
	}
	// dispaly options
/*	var parentLi = select.parentNode;
	if (select.options.length > 1) {
		parentLi.style.display= 'block';
	} else {
		parentLi.style.display = 'none';
	} */
}

// Function that populates countries box 
// (/js/search_box_data.js should be included to html file for this function to work)
// elementDomId - id of a select element to populate
//countriesArray - an array used to populate countries
//selectedId
function populateCountries(elementDomId, countriesArray, selectedId) {
	var select = document.getElementById(elementDomId);
	// clean previous data from element
	var options_qtty = select.options.length;
	for (var i = options_qtty; i > 0; i--) {
		select.options[i] = null;
	}
	// form element contents
//	countriesArray.sort();
	var nameSortArray = new Array();
	var usedId = new Array();
	var i = 1;
	for (var countryId in countriesArray) {
		nameSortArray[i] = countriesArray[countryId];
		i++;
	}
	nameSortArray.sort();
	var length = nameSortArray.length;
	for (var i = 1; i < length; i++) {
		if (id = search_in_obj(countriesArray, nameSortArray[(i-1)], usedId)) {
			usedId[(i-1)] = id;
			select.options[i] = new Option(countriesArray[id], id);
			if ((selectedId != -1) && (id == selectedId)) {
				select.options[i].selected = true;
			}
		}
	}
/*

	var i = 1;
	for (var countryId in countriesArray) {
		select.options[i] = new Option(countriesArray[countryId], countryId);
		if ((selectedId != -1) && (countryId == selectedId)) {
			select.options[i].selected = true;
		}
		i++;
	}
*/	
}

function search_in_obj (obj, search_value, usedId) {
	for (var id in obj) {
		if (obj[id] == search_value && search_in_array(usedId, id)) {
			return id;
		}
	}
	return false;
}

function search_in_array (search_array, search_value) {
	var length = search_array.length;
	for (var i = 0; i < length; i++) {
		if (search_array[i] == search_value) {
			return false;
		}
	}
	return true;
}
// Function that populates states select box based on country id
// (/js/search_box_data.js should be included to html file for this function to work)
// elementDomId - id of a select element to populate
// countryId - number country id 
// statesArray - an array used to populate states
// selectedId - id of a selected state, -1 if no state will be preselected
function populateStates(elementDomId, statesArray, countryId, selectedId) {
	var select = document.getElementById(elementDomId);
	// clean previous data from element
	var options_qtty = select.options.length;
	for (var i = options_qtty; i > 0; i--) {
		select.options[i] = null;
	}
	// form element contents
	var i = 1;
	for (var stateId in statesArray[countryId]) {
		select.options[i] = new Option(statesArray[countryId][stateId], stateId);
		if ((selectedId != -1) && (stateId == selectedId)) {
			select.options[i].selected = true;
		}
		i++;
	}
}

// Function that populates states select box based on country id
// (/js/search_box_data.js should be included to html file for this function to work)
// elementDomId - id of a select element to populate
// stateId - number country id 
// citiesArray - an array used to populate box
// selectedId - id of a selected city, -1 if no city will be preselected
function populateCities(elementDomId, citiesArray, stateId, selectedId) {
	var select = document.getElementById(elementDomId);
	// clean previous data from element
	var options_qtty = select.options.length;
	for (var i = options_qtty; i > 0; i--) {
		select.options[i] = null;
	}
	// form element contents
	var i = 1;
	for (var cityId in citiesArray[stateId]) {
		select.options[i] = new Option(citiesArray[stateId][cityId], cityId);
		if ((selectedId != -1) && (cityId == selectedId)) {
			select.options[i].selected = true;
		}
		i++;
	}
}

// Function used in admin interface to enable/disable alternative checkboxes for approve/delete
// of review administration interface
// reviewId - id of review
function switchReviewCheckboxes(reviewId) {
	var approve_checkbox = document.getElementById('review_approve_ids['+reviewId+']');
	var remove_checkbox  = document.getElementById('review_remove_ids['+reviewId+']');
	if (approve_checkbox.checked) {
		remove_checkbox.disabled = true;
	} else {
		remove_checkbox.disabled = false;
	}
	if (remove_checkbox.checked) {
		approve_checkbox.disabled = true;	
	} else {
		approve_checkbox.disabled = false;
	}
}

// MISC functions
function switchTab() {
	var lis = document.getElementsByTagName('li');
	for (i = 0; i < lis.length; i++) {
		if ((lis[i].className == "firstLink")) {
			var a = lis[i].firstChild;
			if (a.tagName == "A") {
				a.onclick = function() {
				if (this.className == "") {
						this.className = "active";
						var li = this.parentNode.nextSibling;
						while (li.className != "secondLink") {
						  li = li.nextSibling;
						}
						var a = li.firstChild;
						if (a.className == "active") {
							a.className = "";
						}
						var ul = li.parentNode;
						var div = ul.nextSibling;
						while (div.className != "firstTabContent") {
							div = div.nextSibling;
						}
						div.style.display = "block";
						while (div.className != "secondTabContent") {
							div = div.nextSibling;
						}
						div.style.display = "none";
					}
					return false;
				}
			}
		}
		if((lis[i].className == "secondLink")) {
			var a = lis[i].firstChild;
			if(a.tagName == "A"){
				a.onclick = function() {
					if(this.className == ""){
						this.className = "active";
						var li = this.parentNode.previousSibling;
						while(li.className != "firstLink"){
						  li = li.previousSibling;
						}
						var a = li.firstChild;
						if(a.className == "active"){
							a.className = "";
						}
						var ul = li.parentNode;
						var div = ul.nextSibling;
						while(div.className != "secondTabContent"){
							div = div.nextSibling;
						}
						div.style.display = "block";
						while(div.className != "firstTabContent"){
							div = div.previousSibling;
						}
						div.style.display = "none";
					}
					return false;
				}
			}
		}
	}
}

