// ajaxfunc.js
// Ron Lau 2008
// Copyright Netcast Platform 2008
//
// Created       : 26/08/2008
// Last modified : 27/05/2010
// <script>

// Global String Trim Function
// Adopted from http://www.somacon.com/p355.php
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function trim(str)
{
	return str.replace(/^\s+|\s+$/g,"");
}

function ltrim(str)
{
	return str.replace(/^\s+/,"");
}

function rtrim(str)
{
	return str.replace(/\s+$/,"");
}

function getXMLHttpRequest()
{
    var reqt = null;
    if(typeof ActiveXObject != "undefined")
    {
        reqt = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
        reqt = new XMLHttpRequest();
    }
    return reqt;
}

function reqData(x_req, x_url, x_data, x_func, x_method, nocache)
{
    nocache = ((typeof(nocache) != 'undefined') ? nocache : false);
    if(x_method)
    {
        if(nocache)
            x_data += (x_data != '' ? '&' : '') + (new Date()).getTime();
        x_url += '?' + x_data;
    }
    else
    {
        x_method = "POST";
    }
    x_req.onreadystatechange = x_func;
    x_req.open(x_method, x_url, true);
    x_req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    if(x_method == "POST")
    {
        x_req.setRequestHeader("Content-length", x_data.length);
        x_req.setRequestHeader("Connection", "close");
    }
    x_req.send(x_data);
}

function addAttr(target, attrname, attrval)
{
    target.setAttribute(attrname, attrval);
}

function clearChildren(ele)
{
    for(var index = ele.childNodes.length - 1; index >= 0; --index)
    {
        ele.removeChild(ele.childNodes[index]);
    }
}

function getFirstChild(n)
{
	if(n == null)
        return n;
    if(n.firstElementChild)
		return n.firstElementChild;
    var x = n.firstChild;
    while(x != null && x.nodeType != 1)
    {
        x = x.nextSibling;
    }
    return x;
}

function getLastChild(n)
{
    if(n == null)
        return n;
    if(n.lastElementChild)
		return n.lastElementChild;
    var x = n.lastChild;
    while(x != null && x.nodeType != 1)
    {
        x = x.previousSibling;
    }
    return x;
}

function getLastSib(n)
{
    if(n == null)
        return n;
    if(n.previousElementSibling)
		return n.previousElementSibling;
    var x = n.previousSibling;
    while(x != null && x.nodeType != 1)
    {
        x = x.previousSibling;
    }
    return x;
}

getPrevSib = getLastSib;

function getNextSib(n)
{
    if(n == null)
        return n;
    if(n.nextElementSibling)
		return n.nextElementSibling;
    var x = n.nextSibling;
    while(x != null && x.nodeType != 1)
    {
        x = x.nextSibling;
    }
    return x;
}

function selectChild(ele, posstr)
{
	var arr = posstr.split(";");
	for(var i = 0; i < arr.length; ++i)
	{
		if(arr[i] >= 0)
		{
			ele = getFirstChild(ele);
			for(var i2 = 0; i2 < arr[i]; ++i2)
				ele = getNextSib(ele);
		}
		else
		{
			ele = getLastChild(ele);
			for(var i2 = -1; i2 > arr[i]; --i2)
				ele = getLastSib(ele);
		}
	}
	return ele;
}

// </script>

