 /****************************************************************************************
 * 
 * FUNCIONES COMUNES PARA SNIPPETS.
 *
 * 1/ Funcionalidad:
 *    -> Contiene todas las funciones comunes para los snippets.
 * 
 * 2/ Requisitos:
 *    -> No tiene dependencias. 
 *
 * 3/ Ejemplo de utilizacion:
 *	  -> 
 *
 * 4/ Versiones 
 *    -> Fichero: 0.0.1
 *    -> API: 0.0.1
 *
 ****************************************************************************************/


/************************************** FUNCTIONS ***************************************/
/*
 * Functions for to know the type or value of the element.
 */
function isDefined(element) {
	return (element != undefined && element != null); 
}
function isEmpty(value) {
	if (value==null || value.length==0) return true;
	else return false;
}
function isInteger(value) {
	return value.match(/^\d+$/);
}
function isArray(obj) {
	return obj.constructor.toString().indexOf("Array") != -1;
}
function isSelect(htmlElement) {
	return (htmlElement.nodeName == 'SELECT');
}


/*
 * Functions for DOM Manipulation
 */
function get(id, documentReference) {
	var element = (documentReference == null)?document.getElementById(id):documentReference.document.getElementById(id);
	return element;
}
function getElementsGroup(name, documentReference) {
	var element = (documentReference == null)?document.getElementsByName(name):documentReference.document.getElementsByName(name);
	return element;
}
function getValue(id, documentReference) {
	var element = get(id, documentReference);
	return (element == null)?null:element.value;
}
function create(type, id, text) {
	var newElement = document.createElement(type);
	if (id != null) newElement.id = id;
	if (text != null) newElement.appendChild(document.createTextNode(text));
	return newElement;
}
function createOption(value, text, selected) {
	var opt = new Option(text, value);
    if (selected) opt.selected = true;
    else opt.selected = false;
	return opt;
}
function getBoolean(element) {
	return (element != null && element == true);
}

/*
 * Functions for layer visibility.
 */
function showHide(elementId, listElementsToShow) {
	var element = get(elementId);
	if (element == null) return;
	var visible = switchDisplay(element);
	if (listElementsToShow != null && visible) {
		for (var i = 0; i < listElementsToShow.length; i++) {
			hide(get(listElementsToShow[i]));
		}
	}
}
function switchDisplay(element) {
	var visible = false;
	if (element.style.display == "none" || element.style.display == "") { // Show Element
		show(element);
		visible = true;
	} else { // Hide Element
		hide(element);
		visible = false;
	}	
	return visible;
}
function show(element) {
	element.style.display = "block";
}
function hide(element) {
	element.style.display = "none";
}


/*
 * Functions for manage dates.
 */
function parseDate(str) {
	var _dateArray = str.split("/");
	if (isArray(_dateArray) && _dateArray.length == 3 && isInteger(_dateArray[2]) && _dateArray[2].length == 4 && isInteger(_dateArray[1]) && _dateArray[1].length <= 2 && isInteger(_dateArray[0]) && _dateArray[0].length <= 2) {
		var date = new Date(_dateArray[2], (parseInt(_dateArray[1],10)-1), _dateArray[0]);  // string months from 1-12
		initDate(date);
		return date;
	} else {
		alert('invalid date format');
		return null;
	}
}
function formatDate(date) {
	return date.getDate() + "/" + (date.getMonth()+1) + "/" + date.getFullYear();
}
function getComfortableDayOfTheWeekIndex(jsDateObjectIndex) {
	return (jsDateObjectIndex == 0) ?7:(jsDateObjectIndex);
}
function getToday(addDays){
	var today = new Date();
	initDate(today);
	if (addDays != null) today.setDate((today.getDate() + addDays));
	return today;
}
function initDate(date) {
	date.setHours(0);date.setMinutes(0);date.setSeconds(0);date.setMilliseconds(0);
	return date;
}
function getNumberOfNights(fromDate, toDate) {
	var timeElapsed = toDate.getTime() - fromDate.getTime();
	return (timeElapsed/1000/60/60/24).toFixed(0);
}


/*
 * Functions for object utils.
 */
function arrayToObject(array) {
	var object = new Object();
	for (var i=0; i<array.length; i++)
		object[array[i]] = true;
	return object;
}
function objectLength(object) {
	var length = 0;
	if (object == null) return length;  
	for (id in object) length++;
	return length;
}


/*
 * Reload an original text.
 */
function reloadOriginalText(object, text) {
	if(object.value == '') {
		object.value = text;
	}
}

/*
 * Clean a introduced text.
 */
function cleanText(object, text) {
	if(object.value == text) {
		object.value = '';
	}
}

/*
 * Insert a class in item. Native Method: addClass isn't support by IE.
 */
function addClass(ele,cls) {
	if (!this.hasClass(ele,cls)) ele.className += " "+cls;
}

/*
 * Remove a class in item. Native Method: removeClass isn't support by IE.
 */
function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

/*
 * Mark one combo as disabled.
 */
function markComboAsDisabled(htmlCombo) {
	htmlCombo.disabled = true;
	addClass(htmlCombo,"disabled");
}

/*
 * Mark one combo as enabled.
 */
function markComboAsEnabled(htmlCombo) {
	htmlCombo.disabled = false;
	removeClass(htmlCombo,"disabled");
}

/*
 * Native Method: hasClass of javascript isn't support by IE.
 */
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

/*
 * Mark one option in a select as selected.
 */
function markOptionAsSelected(combo, i) {
	combo.selectedIndex = i;
	combo.options[i].selected = true;
}