/**
 * @author Gonzalo Chumillas Velázquez
 */

/**
 * Set the focus
 *
 * @param String _id
 */
function setFocus(_id) {
	var element = document.getElementById(_id);
	element.focus();
	element.select();
}

/**
 * Open a new window
 *
 * @param String _url
 * @param String _target 
 * @param int _width
 * @param int _height
 */
function windowOpen(_url, _target, _width, _height) {
	var _left = Math.round((window.screen.availWidth - _width) / 2);
	var _top = Math.round((window.screen.availHeight - _height) / 2);
	window.open(_url, _target, "resizable=yes, scrollbars=no, width=" + _width + ", height=" + _height + ", left=" + _left + ", top=" + _top);
	return false;
}

/**
 * Send a request.
 * 
 * For example:
 * 
 * 	submitAction('form1', 'update', 125);
 * 	submitAction('form1', 'order', {by: 'name', sense: 'desc'});  // in this case we are sending a pair of values associated to the action 'order'
 * 	submitAction('form1', 'refresh'); // in this case we aren't sending a value for this action
 * 
 * @param String _form_id
 * @param String _action
 * @param String|Integer|Object _values (opcional)
 */
function submitAction(_form_id, _action, _values) {
	var _form = document.getElementById(_form_id);
	var info = _form.action.split("?");
	var url = info[0];
	var params = "?__action__=" + _action;
	
	if (typeof(_values) == "object") {
		var str = "";
		for (var id in _values) {
			if (str.length > 0)
				str += "&";
			str += id + "=" + escape(_values[id]);
		}
		params += "&__values__=" + escape(str);
	} else
	if (_values !== undefined) {
		params += "&__values__=" + escape(_values);
	}
	
	url += params;
	if (info.length > 1)
		url = url + "&" + info[1];
	
	_form.action = url;
	_form.submit();
	
	return false;
}

/**
 * Confirm a request before sending.
 * 
 * For example:
 *
 * 	confirmAction('form1', 'Are you sure?', 'delete', 125);
 * 	confirmAction('form1', 'Are you really sure?', 'delete_all');
 *
 * @param String _form_id
 * @param String _question
 * @param String _action
 * @param String|Integer|Object _values (opcional)
 */
function confirmAction(_form_id, _question, _action, _values) {
	var _form = document.getElementById(_form_id);
	if (window.confirm(_question))
		submitAction(_form_id, _action, _values);
	_form.blur();
	
	return false;
}

/**
 * Set a param value.
 * 
 * For example:
 *
 * 	setParam('form1', 'page', 1);
 * 
 * @param String _form_id
 * @param String _name
 * @param String|Integer|Object _value
 */
function setParam(_form_id, _name, _value) {
	var _ids = new Array();
	_ids["name"] = _name;
	_ids["value"] = _value;
	submitAction(_form_id, "__set_param__", _ids);
	
	return false;
}

/**
 * Advance to the given next
 * 
 * @param String _form_id
 * @param String _page
 */
function nextPage(_form_id, _page) {
	submitAction(_form_id, "__next_page__", _page);
	
	return false;
}

/**
 * Redirect to the previous page
 * 
 * @param String _form_id
 */
function priorPage(_form_id) {
	submitAction(_form_id, "__prior_page__");
	
	return false;
}

/**
 * @param String _anchor
 * @param String image
 */
function replaceLinkImage(_anchor, _image) {
	var imgs = _anchor.getElementsByTagName("img");
	var img = imgs[0];
	img.src = _image;
}

/**
 * @param HTMLElement element
 */
function absoluteLeft(element) {
	var offsetX = element.offsetLeft;
	var parent = element.offsetParent;
	if (parent != null) {
		do {
			offsetX += parent.offsetLeft;
			parent = parent.offsetParent;
		} while (parent == null);
	}
	return offsetX;
}

function submenuAdjustment() {
	var topMenu = document.getElementById("topMenu");
	var subMenu = document.getElementById("subMenu");
	var current = document.getElementById("topMenu_current");
	if (current == undefined) {
		return;
	}
	
	var w0 = topMenu.clientWidth;
	var w1 = current.clientWidth;
	var w2 = subMenu.clientWidth;
	var x0 = absoluteLeft(topMenu);
	var x1 = absoluteLeft(current);
	var x2 = Math.floor(x1 + (w1 - w2)/2);
	if (x2 < 0) {
		x2 = 0;
	} else
	if (x2 + w2 > w0) {
		x2 = w0 - w2 - 8;
	}
	
	subMenu.style.marginLeft = "" + x2 + "px";
	subMenu.className = "subMenu";
}

function another() {
	alert("bbb");
}

function modalWindowOpen(target, modalId) {
	var vph = viewPortHeight();
	var iframes = document.getElementsByTagName("iframe");
	var iframe = iframes[0];
	iframe.style.marginTop = "" + (window.scrollY + (vph - iframe.clientHeight)/2) + "px";
	
	var height = Math.max(vph, document.body.clientHeight);
	var el = document.getElementById(modalId);
	el.style.height = "" + height + "px";
	el.style.visibility = "visible";
	
	return false;
}

function modalWindowClose(target) {
	target.style.visibility = "hidden";
}

/**
 * Author: Andy Langton
 * Source: http://andylangton.co.uk/articles/javascript/get-viewport-size-javascript/
 */

function viewPortWidth() {
	var viewportwidth;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth;
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else
	if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth;
	}
	
	// older versions of IE
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
	}
	
	return viewportwidth;
}

function viewPortHeight() {
	var viewportheight;
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined') {
		viewportheight = window.innerHeight;
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	else
	if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
		viewportheight = document.documentElement.clientHeight;
	}
	
	// older versions of IE
	else {
		viewportheight = document.getElementsByTagName('body')[0].clientHeight;
	}
	
	return viewportheight;
}
