/**
 * common.js
 */

/**
 * Trims leading and trailing whitespace from string.
 */
function trim(str) { 
	while (str.substring(0,1) == " ") {
		str = str.substring(1, str.length);
	}
	while (str.substring(str.length-1,str.length) == ' ') {
		str = str.substring(0, str.length-1);
	}
	return str;
}

/**
 * Changes src path of image.
 */
function changeImgSrc(name,path) {
    eval("var image = document." + name);
    image.src = path;
}

/**
 * Performs case-insensitive lookup of select field using search field value.
 *
 * Args:
 * [element select_element]
 * [element search_text]
 *
 * Return:
 * [bool found]
 */
function selectLookup(select_element, search_text) {
    if (search_text.length >= 0) {
        for (var i=0; i < select_element.length; i++) {
            if (select_element.options[i].text.toUpperCase().indexOf(search_text.toUpperCase()) == 0) {
                select_element.options[i].selected = true;
                return true;
            }
        }
    }
    return false;
} // end fn

/**
 * Toggles readOnly property of passed form element.  Also sets foreground and background color.
 *
 * Args:
 * [form_element form_element]
 * [bool read_only]
 *
 * Return:
 * [void]
 */
function readOnly(form_element, read_only) {

    if (read_only) {
        form_element.readOnly = true;
        form_element.style.color = "windowtext";
        form_element.style.backgroundColor = "inactiveborder";
    } else {
        form_element.readOnly = false;
        form_element.style.color = "windowtext";
        form_element.style.backgroundColor = "window";
    }

} // end fn

/**
 * Formats passed var as currency (dollars.cents).
 *
 * Args:
 * [mixed str]
 *
 * Return:
 * [String str]
 */
function displayCurrency(str) {

	// convert to float
	amt = str/1;

	// zero
	if (amt == 0) {
		return "0.00";
	}

	// check if neg
	var neg = false;
	if (amt < 0) {
		amt *= -1;
		neg = true;
	}

	// round to 2 digits
	amt = Math.round(amt*100);

	// convert amt to string
	str = amt + "";

	// pad to precision
	if (str.length==1) {
		str = "0" + str;
	}
	// insert decimal
	if (str.indexOf(".")==-1) {
		str = str.substring(0,str.length-2) + "." + str.substring(str.length-2);
	}

	// insert thousands commas
        if (str.length>6) {
            for (var i=5; i<str.length; i++) {
        	if ((i-2) % 4 == 0) {
                	str = str.substring(0,str.length-i)+","+str.substring(str.length-i);
		}
            }
	}
        
	// remove leading comma
	if (str.indexOf(",")==0) {
		str = str.substring(1,str.length);
	}

	// insert leading zero
	if (str.indexOf(".")==0) {
		str = "0"+str;
	}

	// replace negative
	if (neg) {
		str = "-"+str;
	}
        
	return str;
} // end method

/**
 * Checks if string is a numeric value.
 */
function isNumeric(str) {
    var valid_chars = "0123456789.,-";
    if (typeof str == 'undefined' || str==null || str.length < 1) {
        return false;
    }
    for (var i=0; i<str.length; i++) {
        if (valid_chars.indexOf(str.charAt(i)) == -1) {
            return false;
        }
    }
    return true;
}

/**
 * Checks if string is an integer value.
 */
function isInt(str) {
    str = str.replace(",","");
    if (str.indexOf(".")!=-1 || isNaN(str)) {
        return false;
    }
    return true;
}

/**
 * Scrolls to bottom of page.
 */
function scrollBottom() {
	var docHeight = 10000;
    if (document.all) {
        docHeight = document.body.offsetHeight;
	} else if (document.layers) {
        docHeight = document.body.document.height;
	}
    window.scroll(0,docHeight);
}

/**
 * Launch window.
 */
function launchWindow(url,win_name,width,height,directories,location,resizable,scrollbars,status,toolbar) {
	if (typeof(directories)=="undefined") {
		directories = 0;
	}
	if (typeof(location)=="undefined") {
		location = 0;
	}
	if (typeof(resizable)=="undefined") {
		resizable = 0;
	}
	if (typeof(scrollbars)=="undefined") {
		scrollbars = 1;
	}
	if (typeof(status)=="undefined") {
		status = 0;
	}
	if (typeof(toolbar)=="undefined") {
		toolbar = 0;
	}
	var attributes  = ("width=" + width + ", height=" + height + ", directories=" + directories + ", location=" + location + ", resizable=" + resizable + ", scrollbars=" + scrollbars + ",status=" + status + ", toolbar=" + toolbar);
	new_win = window.open(url, win_name, attributes);
	setTimeout("new_win.focus();", 250);
}

/**
 * Left pad a string.
 */
function strPadLeft(str, len, padStr) {
	while (str.length < len) {
		str = padStr + str;
	}
	return str;
}

/**
 * Actively validates year, month, date fields.
 */
function fixDate(yearField,monthField,dateField) {
    var monthValue = parseInt(monthField.value);
    var yearValue = parseInt(yearField.value);
    var max = 31;
    if (monthValue==4 || monthValue==6 || monthValue==9 || monthValue==11) {
        max = 30;
    } else if (monthValue==2) {
        max = ( yearValue % 4 == 0 ? 29 : 28);
    }
    if (dateField.value > max) {
        dateField.value = max;
    }
}
