
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function roundNumber(num, dec) {
	var result = Math.round( Math.round( num * Math.pow( 10, dec + 1 ) ) / Math.pow( 10, 1 ) ) / Math.pow(10,dec);
	return result;
}

function format_number(pnumber,decimals){
	    if (isNaN(pnumber)) { return 0};
	    if (pnumber=='') { return 0};
	    var snum = new String(pnumber);
	    var sec = snum.split('.');
	    var whole = parseFloat(sec[0]);
	    var result = '';
	    if(sec.length > 1){
	        var dec = new String(sec[1]);
	        dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
	        dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
	        var dot = dec.indexOf('.');
            if(dot == -1){
	            dec += '.';
	            dot = dec.indexOf('.');
	        }
	        while(dec.length <= dot + decimals) { dec += '0'; }
	        result = dec;
	    } else{
	        var dot;
	        var dec = new String(whole);
	        dec += '.';
	        dot = dec.indexOf('.');
	        while(dec.length <= dot + decimals) { dec += '0'; }
	        result = dec;
	    }
	    return result;
}

GB_show_noload = function(caption, url, /* optional */ width, height, callback_fn) {
    var options = {
        caption: caption,
        height: height || 30,
        width: width || 30,
        fullscreen: false,
        show_loading: false,
        callback_fn: callback_fn,
        reload_on_close: false,
        center_win: true
    }
    var win = new GB_Window(options);
    return win.show(url);
}

function close_confirm_popup()
{
    $('#confirm_popup_overlay').hide();
    $('#confirm_popup_window').hide();
}

function open_confirm_popup()
{
    $('#confirm_popup_overlay').show();
    $('#confirm_popup_window').show();
}

function confirm_popup()
{
    var background = '<div id="confirm_popup_overlay" style="display:none;position:absolute;background-color:#000;top:0;left:0;z-index:100;opacity: 0.7; width: '+ document.width +'px; height: '+ (document.height + 40) +'px; ">&nbsp;</div>';
    document.writeln(background);
    
    var popup = '<div id="confirm_popup_window" style="display:none;position:fixed;width:400px;left:35%;top:30%;z-index:150">\
                    <div>\
                        <table width="100%" style="background: url(\'greybox/header_bg.gif\');border:3px solid #ccc;border-bottom:1px solid #aaa;">\
                            <tr>\
                                <td style="font-weight:bold;font-size:135%">Email Invoice to Customer</td>\
                                <td style="text-align:right"><div onclick="close_confirm_popup()"><img src="greybox/w_close.html"><span style="cursor:pointer">Close</span></div></td>\
                            </tr>\
                        </table>\
                    </div>\
                    <div style="border:3px solid #ccc;border-top:0;padding:10px;background-color:#fff;font-size:160%">\
                        <div>You have not email the invoice. Are you sure you want to complete this ticket?</div>\
                        <br /><div>\
                            <div style="float:left"><input type="image" name="complete" src="templates/default/fe/images/button_yes.png" alt="Yes"></div>\
                            <div style="float:right"><a href="javascript:void(0)" onclick="close_confirm_popup()"><img src="templates/default/fe/images/button_no.png" alt="No" /></a></div>\
                            <div style="clear:both"></div>\
                        <div>\
                    </div>\
                </div>';
    
    document.getElementById('form').innerHTML += popup;
}

String.prototype.replaceAll = function(
	strTarget, // The substring you want to replace
	strSubString // The string you want to replace in.
	){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	 
	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		strText = strText.replace( strTarget, strSubString )
		 
		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf( strTarget );
	}
	 
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}
