
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g, "" );
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/, "" );
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/, "" );
}


/* 	The following script is used by forms that
	would normally have an <input type="submit">
	button, but instead have a custom graphic.
	This script allows the form to still be submitted
	via the	<enter> key.

	This should be called from any input type field:
    <input type="password" name="password"
	value="" size='25'
	onKeyPress="checkEnter(event,'loginForm');" />

	*/

function checkEnter(event, form) {
	if( ( navigator.appName == "Microsoft Internet Explorer" ) &&
		(event.keyCode == 13) ) {
		checkForm(form);
	}

	if( (event.keyCode ==  event.DOM_VK_ENTER) ||
	(event.keyCode == event.DOM_VK_RETURN) ) {
		checkForm(form);
	}
}

function alphaWildcardOnly(stringToChg) {
    return stringToChg.replace(/[^a-zA-Z *]/g, "");
}

function alphaOnly(stringToChg) {
    return stringToChg.replace(/[^a-zA-Z ]/g, "");
}

function validEmail(emailAddr) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if ( filter.test(emailAddr) ) return true;

	return false;
}


/* **************************
Check for a Valid Date
************************** */
function validateDate( fld ) {
    var result = false;     // Assume failure
    var dateFormat = /^\d{4}[/-]\d{1,2}[/-]\d{1,2}$/
    
    // If the dateFormat entered is correct
    if( dateFormat.test(fld.value) ) {
        var fldDate = new Array();
        // Replace '-' with '/' for split, then split date
        fldDate = (fld.value.replace(/-/, "/")).split("/");
        
        // Try to convert fldDate into a Java Date object
        theDate = new Date( fldDate[0], fldDate[1] - 1, fldDate[2] );
        
        // If any of the year, month, or day do not match, inform the user
        // that the date they entered is not valid
        if( !(theDate.getFullYear() == fldDate[0])  || 
            !(theDate.getMonth() == fldDate[1] - 1) ||
            !(theDate.getDate() == fldDate[2]) ) 
        {
            //alert("Invalid date entered.\nPlease check the date.");
            result = -1;        // Set error code
        } else {
            result = true;      // Successful date entry
        }
    } else {    // The date format was not correct
        //alert("Invalid date format entered.\nPlease use the format yyyy/mm/dd or yyyy-mm-dd");
        result = 0;         // Set error code
    }
    
    if( result != true )   fld.focus();    // If the validation failed, focus on the field
    
    return result;
}

function dateIsAfter(fldStart, fldEnd) {
    var result = true;     // Assume success
    
    // If both fields have values in them, compare them
    if( (trim(fldStart.value) != "") && (trim(fldEnd.value) != "" ) ) {
        dateStart = new Date( fldStart.value.replace(/-/, "/").split("/")[0], 
                              fldStart.value.replace(/-/, "/").split("/")[1], 
                              fldStart.value.replace(/-/, "/").split("/")[2] );
        dateEnd = new Date( fldEnd.value.replace(/-/, "/").split("/")[0], 
                            fldEnd.value.replace(/-/, "/").split("/")[1], 
                            fldEnd.value.replace(/-/, "/").split("/")[2] );
        
        if( dateStart > dateEnd )
            result = false;
    }
    // Check for start field being empty but end field being set
    else if( (trim(fldStart.value) == "") && (trim(fldEnd.value) != "") ) {
        result = -1;
    }
    
    return result;
}


//function isInteger(s){
//	var i;
//    for (i = 0; i < s.length; i++){
//        // Check that current character is number.
//        var c = s.charAt(i);
//        if (((c < "0") || (c > "9"))) {
//            window.alert("Please enter an integer.");
//            return false;
//        }
//    }
//    // All characters are numbers.
//    return true;
//}

//function validDate(form, field) {
//    myForm = findDOM(form);
//    fieldValue = form.elements[field].value;
//    isInteger(fieldValue);
//}

/* **************************
* Check for a Valid Integer *
*************************** */
function CheckNumeric( event ) {
	// IE
	if( navigator.appName == "Microsoft Internet Explorer" ) {
	       //window.alert (event.keyCode);
		if( (event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode >= 35 && event.keyCode <= 40) || event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 16) {
			return true;
		} else {
			alert("Numeric input only.")
			return false;
		}
	}

	// Netscape
	if( navigator.appName == "Netscape Navigator" )	{
		if( (event.which >= 48 && event.which <= 57) || (event.which >= 96 && event.which <= 105) || (event.which >= 35 && event.which <= 40) || event.which == 46 || event.which == 8 || event.which == 9 || event.which == 16) {
				return true;
		} else {
			alert("Numeric input only.")
			return false;
		}

	}
}

/* **********************
* Toggle Row Visibility *
*********************** */

function toggleView(objName) {
	var myObject = findDOMStyle(objName);
	var imgName = objName + 'Img';
	var myImgObject = findDOM(imgName);

    //window.alert(myObject.display);
	if (myObject.display == 'none') {
		myObject.display = '';
		myImgObject.src='images/expBoxClose.gif';
	} else {
		myObject.display = 'none';
		myImgObject.src='images/expBoxOpen.gif';
	}

	if (navigator.appName != "Netscape") //due to bug 7993, cancelBubble could make netscape hang, so do not use it in netscape. IE is fine.
		window.event.cancelBubble = true;
	return false;
}

/* ****************
* Auto Tab Fields *
***************** */
function autoTab(startField, endField, maxSize) {
    myForm = findDOM("SearchIssues");
    window.alert("HEllo!");
    if (myForm.elements[startField].value.length == maxSize) {
        myForm.elements[endField].focus();
    }
}