function replace(string,text,by) {
	// Replaces text with by in string
	var strLength, txtLength, newstr, i;
	
    strLength = string.length;
    txtLength = text.length;
    if ((strLength === 0) || (txtLength === 0)) {
        return string;
    }
    i = string.indexOf(text);
    if ((!i) && (text !== string.substring(0,txtLength))) {
        return string;
    }
    if (i === -1) {
        return string;
    }

    newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength) {
        newstr += replace(string.substring(i+txtLength,strLength),text,by);
    }
    return newstr;
}

/* Function to get a form's values in a string */
function getFormAsString(frmObj) {
    var query, i, j, element;
    query = "";
	for (i = 0; i < frmObj.length; i++) {
        element = frmObj.elements[i];
        if (element.type.indexOf("checkbox") === 0 || 
            element.type.indexOf("radio") === 0) { 
            if (element.checked) {
                query += element.name + '=' + encodeURIComponent(element.value) + "&";
            }
		} else if (element.type.indexOf("select") === 0) {
			for (j = 0; j < element.length ; j++) {
				if (element.options[j].selected) {
                    query += element.name + '=' + encodeURIComponent(element.value) + "&";
                }
			}
        } else {
            query += element.name + '=' + encodeURIComponent(element.value) + "&"; 
        }
    } 
    return query;
}

/* Function to hide form elements that show through
   the search form when it is visible */
function toggleForm(frmObj, iState) { // 1 visible, 0 hidden
	var i;
	for(i = 0; i < frmObj.length; i++) {
		if (frmObj.elements[i].type.indexOf("select") === 0 || frmObj.elements[i].type.indexOf("checkbox") === 0) {
            frmObj.elements[i].style.visibility = iState ? "visible" : "hidden";
		}
	} 
}

/* Helper function for re-ordering options in a select */
function Opt(txt,val,sel) {
    this.txt=txt;
    this.val=val;
    this.sel=sel;
}

/*  This function is to select all options in a multi-valued <select> */
function selectAll(elementId) {
	var element, len, i;
	
    element = document.getElementById(elementId);
	len = element.length;
	if (len !== 0) {
		for (i = 0; i < len; i++) {
			element.options[i].selected = true;
		}
	}
}

// This function is for stripping leading and trailing spaces
function trim(str) { 
    if (str !== null) {
        var i; 
        for (i=0; i<str.length; i++) {
            if (str.charAt(i)!==" ") {
                str=str.substring(i,str.length); 
                break;
            } 
        } 
    
        for (i=str.length-1; i>=0; i--) {
            if (str.charAt(i)!==" ") {
                str=str.substring(0,i+1); 
                break;
            } 
        } 
        
        if (str.charAt(0)===" ") {
            return ""; 
        } else {
            return str; 
        }
    }
}

