﻿String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
        function isNumberKey(evt)
        {
            var charCode = (evt && evt.which) ? evt.which : event.keyCode
            if (charCode == 13 || charCode == 10 || (charCode > 31 && (charCode < 48 || charCode > 57)))
            {
                return false;
            }

            return true;
        }

        function isDeleteKey(evt)
        {
            var charCode = (evt && evt.which) ? evt.which : event.keyCode
            if (charCode == 127 || charCode == 8) //delete or backspace
            {
                var thisTextField = (!firefox) ? evt.srcElement : evt.target; 
                thisTextField.focus();
                thisTextField.select();
                return true;
            }

            return false;
        }
        function isNumberKeyWithRange(evt)
        {
            var validNumberPressed = true;
            var charCode = (evt && evt.which) ? evt.which : event.keyCode
            if (charCode == 13 || charCode == 10 || (charCode > 31 && (charCode < 48 || charCode > 57)))
            {
                validNumberPressed = false;
            }
            if (validNumberPressed)
            {
                if (!firefox) evt = event; 
                var element = (!firefox) ? evt.srcElement : evt.target; 
                var elementValidator = getElByID(element.id + "_validator");
                if (elementValidator != null)
                {
                    var minMax = elementValidator.innerText.split("|");
                    if (minMax != null && minMax.length == 2)
                    {
                        var newValue = element.value + charCode.toString();
                        if (parseInt(newValue) > parseInt(minMax[0]) || parseInt(newValue) > parseInt(minMax[1]))
                        {
                            validNumberPressed = false;
                            alert(numericValidatorRangePrompt.replace("%%MIN%%", minMax[0]).replace("%%MAX%%", minMax[1]));
                            //"Please enter a whole number between '" + minMax[0] + "' and '" + minMax[1] + "'");
                        }
                    }
                }
            }
            return validNumberPressed;
        }
        function isDecimalKey(evt)
        {
            var charCode = (evt && evt.which) ? evt.which : event.keyCode
            if (charCode == 13 || charCode == 10 || (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46 && charCode != 45 && charCode != 44))
            {
                return false;
            }

            return true;
        }
        function isCurrencyKey(evt)
        {
            var isNumber = isNumberKey(evt);
            if (!isNumber) 
            {
                var charCode = (evt && evt.which) ? evt.which : event.keyCode;
                if (charCode == 13 || charCode == 10 || (charCode != 46 && charCode != 44 && charCode != 36 && charCode != 45)) //period, $ and - characters
                {
                    return false;
                }
            }
            return true;
        }
        function ignoreEnterKey(evt)
        {
            var charCode = (evt && evt.which) ? evt.which : event.keyCode
            if (charCode == 13 || charCode == 10)
            {
                return false;
            }

            return true;
        }
        function ValidateCurrency(event)
        {
            var txtbox = event.srcElement ? event.srcElement : event.target;
            var rex = /^-?[\$£€]?[0-9]{0,3}(,?[0-9]{3})*(\.[0-9]{0,2})?$/;
            var value = txtbox.value.toString().trim();
            if (value != "$0.00" && value != "0.00" && !rex.test(value)) //Not a valid currency value
            {
                alert("The value entered must be a valid currency string in the form $#,###.##");
                txtbox.focus();
                return false;
            }
            txtbox.value = formatCurrency(value);
            return true;
        }
        function ValidateDecimal(event)
        {
            var txtbox = event.srcElement ? event.srcElement : event.target;
            var rex = /^-?[0-9]{0,3}(,{0,1}[0-9]{3})*(\.[0-9]{0,10})?$/;
            var value = txtbox.value.toString().trim();
            if (value != "0.00" && !rex.test(value)) //Not a valid decimal value
            {
                alert("The value entered must be a valid decimal number string in the form #.#");
                txtbox.focus();
                return false;
            }
            txtbox.value = formatCurrency(value).replace("$", '');
            return true;
        }
        function formatCurrency(num) 
        {
            while (num.toString().indexOf(',') > -1 || num.toString().indexOf('$') > -1) 
            {
                num = num.toString().replace(/\$|\,/,'');
            }
            if(isNaN(num))
            {
                num = "0";
            }
            sign = (num == (num = Math.abs(num)));
            num = Math.floor(num*100+0.50000000001);
            cents = num%100;
            num = Math.floor(num/100).toString();
            if (cents < 10)
            {
                cents = "0" + cents;
            }
            for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            {
                num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));
            }
            return (((sign)?'':'-') + "$" + num + '.' + cents);
        }
        
function PrintMainPage()
{
	// Create a random name for the print frame.
	var strFrameName = ("printer-" + (new Date()).getTime());
 
	// Create an iFrame with the new name.
	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
 
	// Hide the frame (sort of) and attach to the body.
	jFrame
		.css( "width", "1px" )
		.css( "height", "1px" )
		.css( "position", "absolute" )
		.css( "left", "-9999px" )
		.appendTo( $( "body:first" ) )
	;
 
	// Get a FRAMES reference to the new frame.
	var objFrame = window.frames[ strFrameName ];
 
	// Get a reference to the DOM in the new frame.
	var objDoc = objFrame.document;
 
	// Grab all the style tags and copy to the new
	// document so that we capture look and feel of
	// the current document.
 
	// Create a temp document DIV to hold the style tags.
	// This is the only way I could find to get the style
	// tags into IE.
	var jStyleDiv = $( "<div>" ).append(
		$( "style" ).clone()
		);
 
	// Write the HTML for the document. In this, we will
	// write out the HTML of the current element.
	objDoc.open();
	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
	objDoc.write( "<html>" );
	objDoc.write( "<body>" );
	objDoc.write( "<head>" );
	objDoc.write( "<title>" );
	objDoc.write( document.title );
	objDoc.write( "</title>" );
	objDoc.write( jStyleDiv.html() );
	objDoc.write( "</head>" );
	objDoc.write( $('#mainContent').html() );
	objDoc.write( "</body>" );
	objDoc.write( "</html>" );
	objDoc.close();
 
	// Print the document.
	objFrame.focus();
	objFrame.print();
 
	// Have the frame remove itself in about a minute so that
	// we don't build up too many of these frames.
	setTimeout(
		function(){
			jFrame.remove();
		},
		(60 * 1000)
		);
}

function EmailMainPage()
{
    mail_str = "mailto:?subject=Check out the " + document.title;
    mail_str += "&body=I thought you might be interested in the " + document.title;
    mail_str += ". You can view it at, " + location.href.toString(); 
    location.href = mail_str;
}
