function xf_isproject(value)
{
    return value.value==5;

}

function xf_locationUrl(value)
{
    return '/location/' + value;
}

function xf_recordUrl(value)
{
    return '/record/' + value;
}

function xf_resultUrl(value)
{
    for (var k in value)
    {
        return ['/', k, '/', value[k].uri].join('');
    }
}

function xf_setupFunction()
{
	//Set timeout since the content takes some time to load
	window.setTimeout("setFocusOnLoad()", 100);
}

function setFocusOnLoad()
{
	if(document.getElementById('username') != null);
	{
		try	
		{
			document.getElementById('username').focus();
		}
		catch(e)	//it should not go there, but you never know
		{}		
	}
}

function xf_recordSearchUrl(value)
{
    if (value && value.length > 0)
    {
        return '/search/?q=' + escape(value);
    }
    else
    {
        return '/search';
    }
}

function xf_etc(value)
{
    if (value && value.length > 30)
    {
        return value.substring(0, 26) + "...";
    }
    else
    {
        return value;
    }
}

function xf_etcsummaryname(value)
{
    if (value && value.length > 80)
    {
        var x = value.lastIndexOf('(');
        var number = value.substring(x, value.length);

        return value.substring(0, 78 - number.length) + "... " + number;
    }
    else
    {
        return value;
    }
}

function xf_etcslider(value)
{
    if (value && value.length > 15)
    {
        return value.substring(0, 8) + "...";
    }
    else
    {
        return value;
    }
}

function xf_mailto(value)
{
    return 'mailto:' + value;
}

function xf_feedbackMailto()
{
    return 'mailto:grant.allen@towersoft.com.au?subject=[tremble-feedback] ' + window.location;
}

function xf_wiki(value)
{
    return 'http://tremblevpc1:8000/mywiki.' + value;
}

/* Extract the authors form the record field. Each returned search record will have the record title and authors
*/
function xf_extractAuthors(results)
{
    var result = {};
	
    if (isArray(results))
    {
        for (var i in results)
        {
			
            var authors = getObjectValue(results[i], "record.authors");
			
            if (isArray(authors) && authors.length > 0)	//the author is array and must not be empty.
            {				
                for (var j in authors)
                {
                    //var name = getObjectValue(authors[j], "formattedname");	it doesnt know what formatted name is
                    var name = getObjectValue(authors[j], "sortname");					
					
                    if (result[name])
                    {
                        result[name].count++;
                    }
                    else
                    {
                        result[name] = {name: name, count: 1, uri:getObjectValue(authors[j], 'uri')};
                    }
                }
            }
        }
    }


    return objectToArray(result);
}

function xf_extractRecordTypes(results)
{
    var recordTypes = {};

    if (isArray(results))
    {
        for (var i in results)
        {
            var name = getObjectValue(results[i], "record.recordtype.name");

            if (name)
            {
                if (recordTypes[name])
                {
                    recordTypes[name].count++;
                }
                else
                {
                    recordTypes[name] = {name: name, count: 1};
                }
            }
        }
    }

    return objectToArray(recordTypes);
}

var g_dayTable = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var g_mons = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

// This fucntion is used by the recordSearch html
function xf_datetime(date)
{
    if (date)
    {
        return g_dayTable[date.getDay()] + " " + xf_date(date) + " " + xf_time(date);
    }
    else
    {
        return "";
    }
}

function xf_date(date)
{
    var mon = g_mons[date.getMonth()];
    if (date)
    {
        return [date.getDate() + " " + mon + " " + date.getFullYear()].join('');
    }
    else
    {
        return "";
    }

}

function __xfhelper_padDigits(value)
{
    if (value < 10)
    {
        return "0" + value;
    }
    else
    {
        return value;
    }
}

function xf_time(date)
{
    if (date)
    {
        if (date.getSeconds() > 0)
        {
            return [date.getHours(), ":", __xfhelper_padDigits(date.getMinutes()), ":", __xfhelper_padDigits(date.getSeconds())].join('');
        }
        else if (date.getHours() > 0 || date.getMinutes() > 0)
        {
            return [date.getHours(), ":", __xfhelper_padDigits(date.getMinutes())].join('');
        }
        else
        {
            return "";
        }
    }
    else
    {
        return "";
    }

}

//
// Thism if the best transform to use. It caters for dates without times
function xf_formDate(date)
{
    if (date)
    {
        if (date.getHours() > 0 || date.getMinutes() > 0 || date.getSeconds() > 0)
        {
            return xf_date(date) + " " + xf_time(date);
        }
        else
        {
            return xf_date(date);
        }
    }
    else
    {
        return '';		
    }
}

function xf_uri(uri)
{
    if (uri)
    {
        return uri;
    }
    else
    {
        return 0;
    }
}

function refine(axis, name, content)
{
    if (content.querystring)
    {
        return xf_recordSearchUrl(content.querystring + " " + axis + ":" + name);
    }
}

function xf_refineAuthorUrl(name, content)
{
    return refine("author", name, content);
}

function xf_excludeAuthorUrl(name, content)
{
    return refine("-author", name, content);
}

function xf_refineRecordTypeUrl(name, content)
{
    return refine("rtyfilter", name, content);
}

function xf_excludeRecordTypeUrl(name, content)
{
    return refine("-rtyfilter", name, content);
}

function xfhelper_decimalround(x)
{
    return Math.round(x * 10.0) / 10.0;
}

function xf_filesize(size)
{
	if (size == 0 )
	{
		return "no document";
	}
    else if (size < 1000)
    {
        return size + " bytes";
    }
    else if (size < 1000000)
    {
        return xfhelper_decimalround(size / 1024.0) + " Kb";
    }
    else if (size < 1000000000)
    {
        return xfhelper_decimalround(size / 1048576.0) + " Mb";
    }
    else
    {
        return xfhelper_decimalround(size / 1073741824.0) + " Gb";
    }
}

function xf_haslabel(labelUri, content)
{
    var labels;

    if (content && content.result && content.result.userlabels)
    {
        labels = content.result.userlabels;
    }
    else
    {
        return false;
    }

    for (var i in labels)
    {
        if (labels[i].uri == labelUri)
        {
            return true;
        }
    }

    return false;
}

function xf_officelink(filename)
{
    if (window.ActiveXObject && filename)
    {
        if (filename.toLowerCase().endsWith('.doc'))
        {
            return 'javascript:openWord("' + window.location.protocol + "//" + window.location.host + "/drafts/" + filename + '")';
        }
        else if (filename.toLowerCase().endsWith('.xls'))
        {
            return 'javascript:openExcel("' + window.location.protocol + "//" + window.location.host + "/drafts/" + filename + '")';
        }
        else
        {
            return '';
        }
    }
    else
    {
        return '';
    }
}

function xf_isme(value, content)
{
    if (content.me && content.me.uri && value)
    {
        return content.me.uri == value;
    }
    else
    {
        return false;
    }
}

function xf_objecteditclass(value)
{
    if (value)
    {
        return '';
    }
    else
    {
        return 'objectedit-ok';
    }
}

// this function overrides the standard Date ToString function
function toNiceDateString()
{
	return xf_formDate(this);
}

Date.prototype.toString = toNiceDateString;

var _timezoneCount = 0;
//Sets the Time Zone Off Set Value on the Server on page load.
function xf_setTimezoneOffset()
{
	
	var now  =  new Date();
	var timezoneOffset = now.getTimezoneOffset();
	
	//Async. Post the time zone off set value to the server on page load
	if(_timezoneCount == 0)
	{
		postAsyncHttpStruct("/backend/timezoneoffset?value=" + timezoneOffset, '', function(status, struct)
		{
		});
	}	
	_timezoneCount++;

}

/**
*Hack for setting the focus on the search fiels once the popup has loaded.
*/
function xf_searchPageLoad()
{
	document.getElementById('q').focus();
	autoComplete = false;
}

/** HACK FOR SAFARI TABBING.
* This is a hack for safari. Safari doesnt like adding and removing the current focused element, hence when it setup the autopopup
* in popwidget.cs it take away the focus. Hence tabbing through the record entry form never works. This only happen for the first time.
* Once the field is set up, tabbing through is ok again. However we can not afford to do that. So here is the hack to make it always works
* on the first time around. NOTE: THIS ONLY FOR SAFARI
*/
var _xf_setupCount = 0;
function xf_setupFieldPopup()
{
	
	if(!bw.mac  )
		return;
		
	if(_xf_setupCount >0)
		return;
		
	//THIS IS FOR SAFARI ONLY. FF AND IE dont use this.
	var eles = document.getElementsByTagName("input");	
		
	for(var i = 0; i < eles.length; i++)
	{
		var ele = eles[i];
		
		if(ele.type == 'text')
		{
			ele.focus();
		}
	}
	focusTitle();
	_xf_setupCount++;
	
	
	
}


/*
* This is a fix for the popup not visible on active label edit page. In IE we need to 
* insert a nonprintable charcter (space) to the innerHTML for the popbutton to be displayed
* Our template engine doesnt like the &nbsp in the static HTML -> throws error
* This javascript is to bypass the template engine.
* TODO: Need to fix up the template engine so that it compile the &nbsp too.
*/
function xf_activeLabelPopup()
{
	var eles = document.getElementsByTagName("span");	
	
	for(var i = 0; i < eles.length; i++)
	{
		var ele = eles[i];		
		
		if(ele.className == 'autopopup-down')
		{
			ele.innerHTML = "&nbsp;";			
		}
	}	
	
}
