function trim(s) {
	if (s == undefined) return '';
	else if (s == '') return '';
	return s.toString().replace(/^(\s|\&nbsp;)*|(\s|\&nbsp;)*$/g,"");
}

function highlightField(f) {
	$(f).css("border", "1px solid #a00000");
	$(f).css("background-color", "#FAD7D7");
	$(f).css("color", "#000");

	$(f).unbind('focus');

	$(f).focus(function() {
		removeHighlightField(f);
		$('#erroresjs').hide();
	});
}

function removeHighlightField(f) {
	$(f).css("border", "");
	$(f).css("background-color", "");
	$(f).css("color", "");
}

function calculateAge(dateText) {
	dateText = dateText.split('/');
	Bdate = new Date(dateText[2], dateText[1] - 1, dateText[0]);
	var today = new Date(),
	age = new Date(today - Bdate).getFullYear() - 1970;
	$("#userage").html(age + " aņos");
}

function isUndefinedOrNothing(v) {
	v = trim(v);
	if (v == '') return true;
	if (v == undefined) return true;
	return false;
}

function generalDialog(titulo , texto , isError , isSuccess) {
	$("#generaldialog").attr("title", titulo);

	if (isError) {
		texto = '<img src="images/error_icon.gif" width="40" height="40" class="generaldialogimagen" />' + texto + '<div class="clear"></div>';
	}

	if (isSuccess) {
		texto = '<img src="images/success_icon.gif" width="40" height="40" class="generaldialogimagen" />' + texto + '<div class="clear"></div>';
	}

	$("#generaldialogtext").html(texto);
	$("#generaldialog").dialog({
		width: 500,
		minHeight: 140,
		minWidth: 500,
		modal: true,

		buttons: {
			Cerrar: function() {
				$(this).dialog('close');
			}
		}
	});
}

function isValidEMail(v) {
	var reg = /^[a-zA-Z0-9.][a-zA-Z0-9-_\.]+@[a-zA-Z0-9-].+\.[a-zA-Z]{2,5}$/;
	return reg.test(v);
}

function urlencode( str ) {

	if (str == undefined) return "";
	if (str == "") return "";

    var histogram = {}, tmp_arr = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    return ret;
}

function updateNewDocs() {
	$.getScript("controller.php?action=updatenewdocs");
}



//--------------------------------------------- TABS ---------------------------------------------
$(document).ready(function() {

	//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

	$("ul.tabs-contacto li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs-contacto li").click(function() {

		$("ul.tabs-contacto li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});

});







//--------------------------------------------- SITE MENU ---------------------------------------------
//Specify full URL to down and right arrow images (25 is padding-right to add to top level LIs with drop downs):
//var arrowimages={down:['downarrowclass', 'img/arrowb.gif', 25], right:['rightarrowclass', 'img/arrowa.gif']}
var arrowimages={down:['downarrowclass', '', 20], right:['rightarrowclass', '']}

var sitemenu={

fadesettings: {overduration: 300, outduration: 100}, //duration of fade in/ out animation, in milliseconds

buildmenu:function(menuid, arrowsvar){
	jQuery(document).ready(function($){
		var $mainmenu=$("#"+menuid+">ul")
		var $headers=$mainmenu.find("ul").parent()
		$headers.each(function(i){
			var $curobj=$(this)
			var $subul=$(this).find('ul:eq(0)')
			this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
			this.istopheader=$curobj.parents("ul").length==1? true : false
			$subul.css({top:this.istopheader? this._dimensions.h+"px" : 0})
			$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
				'<span'
				+' class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
				+ '" style="border:0;"></span>'
			)
			$curobj.hover(
				function(e){

					if (this._dimensions.w == 0)
						this._dimensions.w = 176;
					
					var $targetul=$(this).children("ul:eq(0)")
					this._offsets={left:$(this).offset().left, top:$(this).offset().top}
					var menuleft=this.istopheader? 0 : this._dimensions.w
					menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>$(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
					$targetul.css({left:menuleft+"px"}).fadeIn(sitemenu.fadesettings.overduration)
				},
				function(e){
					$(this).children("ul:eq(0)").fadeOut(sitemenu.fadesettings.outduration)
				}
			) //end hover
		}) //end $headers.each()
		$mainmenu.find("ul").css({display:'none', visibility:'visible'})
	}) //end document.ready
}
}

//build menu with ID="sitemenu" on page:
sitemenu.buildmenu("eposmenu", arrowimages);
