/*
 * The Physical Initiative
 * Useful Javascript Functions
 * Modified version of the dewsolutions useful javascript functions file v1.1
 * By Kevin Dew
 * Copyright Dew Solutions 2006-2007 All Rights Reserved
 * www.dewsolutions.co.uk
 */

/*
 * id function
 * quicker way of accessing document.getElementById
 * arg: string of an element id
 * return: object or null if it can't be found
 */
function id(id)
{
    return document.getElementById(id);

}
/*
 * trim function
 * takes a string and strips blank characters at start and end
 * arg: string
 * return: trimmed string
 */
function trim(string)
{
    while(string.charAt(0) == ' ')
    {
        string = string.substring(1,string.length);
    }
    while(string.charAt(string.length - 1) == ' ')
    {
        string = string.substring(0,string.length-1);
    }
    return string;
}

/*
 * Ajax Class
 * vars: XMLHttp, canAjax
 * methods: getRequest, postRequest
 */
function ajaxClass()
{

    this.XMLHttp = false;
    try
    {
        this.XMLHttp = new XMLHttpRequest();
        this.canAjax = true;
    }
    catch(e){}
    try
    {
        this.XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        this.canAjax = true;
    }
    catch(e){}

    if(!this.XMLHttp)this.canAjax = false;

    this.getRequest = function(url,stateChange)
    {
        if(typeof stateChange != 'function')return false;
        try
        {
            this.XMLHttp.onreadystatechange = stateChange;
            this.XMLHttp.open("GET", url, true);
            this.XMLHttp.setRequestHeader('REFERER', location.href);
            this.XMLHttp.send("");
            return true;
        }
        catch(e)
        {
            return false;
        }
    }

    this.postRequest = function(url, stateChange, data)
    {
        if(typeof stateChange != 'function')return false;
        try
        {
            this.XMLHttp.onreadystatechange = stateChange;
            this.XMLHttp.open("POST", url, true);
            this.XMLHttp.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
            this.XMLHttp.setRequestHeader('REFERER', location.href);
            this.XMLHttp.send(data);
            return true;
        }
        catch(e)
        {
            return false;
        }
    }
}

