Utilities = {};

Utilities.includeJS = function(_filepaths)
{
	for(var i=0; i<_filepaths.length; i++)
	{
		document.write('<script type="text/javascript" src="'+_filepaths[i]+'"></script>');
	}
}

Utilities.includeCSS = function(_filepaths)
{
	for(var i=0; i<_filepaths.length; i++)
	{
		document.write('<link href="'+_filepaths[i]+'" rel="stylesheet" type="text/css" />');
	}
}

Utilities.getElement = function(_id)
{
    return document.getElementById(_id);
}

Utilities.toggle = function(_id)
{
 	this.getElement(_id).style.display = (this.getElement(_id).style.display == '') ? 'none' : '';
}

Utilities.createElement = function(_e, _obj)
{
	var a = document.createElement(_e);
	for(prop in _obj)
	{
		a[prop] = _obj[prop];
	}
	return a;
}

Utilities.appendChild = function()
{
	if(this.appendChild.arguments.length > 1)
	{
		var a = this.appendChild.arguments[0];
		for(i=1; i<this.appendChild.arguments.length; i++)
		{
			if(arguments[i])
			{
				a.appendChild(this.appendChild.arguments[i]);
			}
		}
		return a;
	}
	else
	{
		return null;
	}
}

Utilities.trimText = function(_text)
{
    return _text.replace(/^\s+|\s+$/g,"");
}

Utilities.ltrimText = function(_text)
{
    return _text.replace(/^\s+/,"");
}

Utilities.rtrimText = function(_text)
{
    return _text.replace(/\s+$/,"");
}


//Get input data
Utilities.encodeNameAndValue =  function (sName, sValue) {
    var sParam = encodeURIComponent(sName);
    sParam += "=";
    sParam += encodeURIComponent(sValue);
    return sParam;
}


Utilities.getFormItems =  function (_FormId) {

    //array to hold the params
    var aParams = new Array();

    //get your reference to the form
    var oForm = document.getElementById(_FormId);

    //iterate over each element in the form
    for (var i=0 ; i < oForm.elements.length; i++) {

        //get reference to the field
        var oField = oForm.elements[i];

        //different behavior based on the type of field
        switch (oField.type) {

            //buttons - we don't care
            case "button":
            case "submit":
            case "reset":
                     break;

            //checkboxes/radio buttons - only return the value if the control is checked.
            case "checkbox":
                var chb_value=0
                if (oField.checked) {
                     chb_value =1;
                }
                aParams.push(Utilities.encodeNameAndValue(oField.name, chb_value));
                break;
            case "radio":
                if (!oField.checked) {
                    break;
                } //End: if

            //text/hidden/password all return the value
            case "text":
            case "hidden":
            case "password":
                oField.value = Utilities.trimText(oField.value);
                aParams.push(Utilities.encodeNameAndValue(oField.name, oField.value));
                break;

            //everything else
            default:
                switch(oField.tagName.toLowerCase()) {
                    case "fieldset":
                    case "div":
                    case "img":
                    case "p":
                        break;
                    case "select":
                        if (oField.options.length == 0){
                            aParams.push(Utilities.encodeNameAndValue(oField.name, 0));
                        }else{
                            aParams.push(Utilities.encodeNameAndValue(oField.name, oField.options[oField.selectedIndex].value));
                        }
                        break;

                    default:
                        aParams.push(Utilities.encodeNameAndValue(oField.name, oField.value));
                }
        }

    }

    return aParams.join("&");
}





