function xfhelper_tobool(value)
{
    if (isArray(value))
    {
        value = value.length;
    }
    else if (isString(value))
    {
        value = (value != "");
    }

    return (value ? true : false);
}

function xf_boolean(value)
{
    return xfhelper_tobool(value);
}

function xf_boolint(value)
{
    return (xfhelper_tobool(value) ? 1 : 0);
}

/*function xf_intDisplay(value)
{
    return (xfhelper_tobool(value) ? null : 'none');
}*/

function xf_not(value)
{
    return !xfhelper_tobool(value);
}

function xf_count(value)
{
    if (isArray(value))
    {
        return value.length;
    }
    else
    {
        return 0;
    }
}

function xf_today(date)
{
	if(date)
	{
		return xf_formDate(date)
	}
	else	//return today, used for date registered.
	{	
		var now = new Date();
		return xf_formDate(now);
	}

}

function objectToArray(object)
{
    var result = [];
    
    for (var k in object)
    {
        result.push(object[k]);
    }
    
    return result;
}

function _d(document, id)
{
    return document.getElementById(id);
}

function _dd(document, id, value)
{
    var element = document.getElementById(id);
    
    if (element && element.style)
    {
        element.style.display = (xfhelper_tobool(value) ? '' : 'none');
    }
}

function _b(value)
{
    return xfhelper_tobool(value);
}

function _bn(value)
{
    return !xfhelper_tobool(value);
}

function _dh(document, id, value)
{
    var element = document.getElementById(id);
        
    if (element)
    {
        element.innerHTML = value;
    }
}

function _da(document, id, attribute, value)
{
    var element = document.getElementById(id);
    
    if (element)
    {
        if (element.setAttribute)
        {
            element.setAttribute(attribute, value);
        }
        else
        {
            element[attribute] = value;
        }
    }
}

function _c(source, field)
{
    return getObjectValueAsString(source, field);
}

function _h(field)
{
    return getHashVariable(field);
}

function _he(hash)      // enable
{		
	focusTitle();
    replaceHashVariable(hash, hash);
	
}

function _hd(hash)      // disable
{
    replaceHashVariable(hash, "-" + hash);
}

function _ht(hash)      // toggle
{
    if (getHashVariable(hash))
    {
        _hd(hash);
    }
    else
    {
        _he(hash);
    }
}

function getObjectValueAsString(o, field)
{
    var v = getObjectValue(o, field);
    
    if (isNull(v))
    {
        return "";
    }
    else if (isUndefined(v))
    {
        return "";
    }
    else
    {
        return v;       // not necessarily a string, but better than null
    }
}

function getObjectValue(o, field)
{
    var parts = field.split('.');
    
    for (var i in parts)
    {
        if (o == null)
        {
            return null;
        }
        
        var part = parts[i];
        
        if (part == '')
        {
        }
        else if (o[part] != null)
        {
            o = o[part];
        }
        else
        {
            o = null;
            break;
        }
    }
    
    return o;
}

function getHashVariable(field)
{
    var fieldName;
    var def;
    
    if (field.substring(0, 1) == '-')
    {
        fieldName = field.substring(1);
        def = false;
    }
    else
    {
        fieldName = field;
        def = true;
    }
    
    var parts = window.location.hash.substring(1).split(',');
    
    for (var i in parts)
    {
        if (parts[i] == fieldName)
        {
            return true;
        }
        else if (parts[i] == '-' + fieldName)
        {
            return false;
        }
    }
    
    return def;
}

/* todo:tong add some docco in here, why do we need to rplace hash variabl???  */
function replaceHashVariable(field, replacement)
{	
	
    var fieldName;
    var replaced = false;
    
    if (field.substring(0, 1) == '-')
    {
        fieldName = field.substring(1);
    }
    else
    {
        fieldName = field;
    }
    
    var parts = window.location.hash.substring(1).split(',');
    
	
    for (var i in parts)
    {
        if (parts[i] == fieldName || parts[i] == '-' + fieldName)
        {
            parts[i] = replacement;
            replaced = true;
        }
    }
    
    if (!replaced)
    {
        if (parts[0] == '')
        {
            parts[0] = replacement;
        }
        else
        {
            parts.push(replacement);
        }
    }
    	
    window.location.href = "#" + parts.join(',');	
}

function showLoading(document)
{
    document.body.innerHTML += "<div style='position: fixed; padding: 20px; background: white; border: solid 10px black; left: 40%; top: 200px; width: 20%; height: 100px;'><center>Loading...<br/><img src='/static/grrr.png' /></center></div>";
}

function xf_nl2br(value)
{
    if (value)
    {
        return value.replace(/\n/gi, '<br />');
    }
    else
    {
        return value;
    }
}

function xf_htmlescape_matchevaluator(match)
{
    switch (match)
    {
        case '"':
            return '&quot;';
            
        case "'":
            return '&#39;';     // IE doesn't support &apos;. Apparently it's not in the HTML standard, just XHTML; IE doesn't really support XHTML.
            
        case '<':
            return '&lt;';
            
        case '>':
            return '&gt;';
            
        case '&':
            return '&amp;';
            
        default:
            return match;
    }
}

function _e(value)
{
    return xf_htmlescape(value);
}

function xf_urlencode(value)
{
    return encodeURIComponent(value);
}

function xf_lowercase(value)
{
	return value.toString().toLowerCase();
}

String.prototype.htmlescape = function()
{
    return xf_htmlescape(this);
}

/* THE DODGY STUFF

    All this is done because Safari doesn't actually support string.replace with a function.
    
    So we do a simple check on load, and then assign the right function to the name xf_htmlescape.
    
    Yurgh!
*/

var xf_htmlescape;

if ("x" == "y".replace(/./g, function() { return "x"; }))
{
    xf_htmlescape = function(value)
    {
        if (value)
        {
            return value.toString().replace(/[\<\>\"\'&]/g, xf_htmlescape_matchevaluator);
        }
        else
        {
            return value;
        }
    };
}
else
{
    xf_htmlescape = function(value)
    {
        if (value)
        {
            return value.toString()
                .replace(/&/g, "&amp;")
                .replace(/\</g, "&lt;")
                .replace(/\>/g, "&gt;")
                .replace(/\"/g, "&quot;")
                .replace(/\'/g, "&#39;");
        }
        else
        {
            return value;
        }
    };
}


/*
Another DODGY Stuff
	
	This is done to set initial value to firefox when the record is in the edit mode. IE picks up the default value just fine but not firefox.
	
*/

//This transformer sets all combo box on the page to it's initial value
var initSelCount = 0;
function xf_initSelect(o)
{
	
	
	
	combos = document.getElementsByTagName("select");
	
	var val = '';
	
	for(var i = 0; i < combos.length; i++)
	{		
		
		options = combos[i].options;
		
		for(var k = 0 ; k < options.length; k++)
		{			
	
			//for some reason it work differently on data set, sometime the caption available.
			var temp = '';
			if(o[combos[i].name].caption)
			{
				temp = o[combos[i].name].caption;
			}
			else
			{
				temp = o[combos[i].name]
			}
			
			
			//if(o[combos[i].name].caption == options.item(k).text)
			if(temp == options.item(k).text)
			{					
				options.item(k).selected = true;
				
				//we could do a return here, but it wont work if there are more than 1 combo box this should fix up IE... 
				val = options.item(k).value;	
			}
		}
	}
	
	return val;
	
	
}

/* 
This transformer determines what to put in the assignee field
If the assignee field is the same with the home location return the location 
value. If the record is new record (uri = 0) then return nothing.
*/
function xf_initAssignee(o)
{	
	if(o.uri == 0)
	{
		return '';
	}
	
	if(o.currentloc == null && o.homeloc != null)
	{
		return o.homeloc.formattedname;
	}
	else if(o.currentloc != null)
	{
		return o.currentloc.formattedname;
	}
	else if(o.currentloc == null && o.homeloc == null)
	{
		return '';
	}
		
	
}
