var is_gecko = /gecko/i.test(navigator.userAgent);
var is_ie = /MSIE/.test(navigator.userAgent);

<!--
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// -->

function getSelectionStart(varObject) {

	if (is_gecko)
		return varObject.selectionStart;
	var range = document.selection.createRange();
	var isCollapsed = range.compareEndPoints("StartToEnd", range) == 0;
	if (!isCollapsed)
		range.collapse(true);
	var b = range.getBookmark();
	return b.charCodeAt(2) - 2;
};

function IsEnterPressed(e)
{
    if(e == null)  return false;
    
    var code = 0;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;         
    if(code == 13)
        return true
    else
        return false;
}

function CheckChars(e, varObject, varEmptyReplaceValue)
{
    if(varObject && e != null)
    {    
        var code = 0, blnFlag = false, varKeyCode = 0, intSelectionStart = 0;
	    if (!e) var e = window.event;
	    if (e.keyCode) code = e.keyCode; //FOR SPECIAL CHARS   
	    else if (e.which) varKeyCode = e.which;	 // FOR DIGITS, CHARS, AND OTHER CHARS
	    
	    intSelectionStart = getSelectionStart(varObject);
	    if(is_ie)
	    {
	        varKeyCode = code;
	        code = 0;
	    }
	    
	    if(BrowserDetect.browser == "Safari" || BrowserDetect.browser == "Mozilla")
	    {
	        if((code >= 48 && code <= 57) || (varKeyCode == 46 || code == 116 || code == 46 || code == 8 || code == 6 || code == 37 || code == 38 || code == 39 || code == 40 || code == 9 || code == 44 || code == 35 || code == 36 ))
	        {   
	            blnFlag = UpdateInputElementIfEmpty(code, varEmptyReplaceValue, varObject, intSelectionStart);    
            }
	    }
	    else
	    {
	        if((varKeyCode >= 48 && varKeyCode <= 57) || (varKeyCode == 46 || code == 116 || code == 46 || code == 8 || code == 6 || code == 37 || code == 38 || code == 39 || code == 40 || code == 9 || varKeyCode == 44 || code == 35 || code == 36 ))
	        {
	            blnFlag = UpdateInputElementIfEmpty(varKeyCode, varEmptyReplaceValue, varObject, intSelectionStart);
            }
        }
        
        if(blnFlag)
            return true;
        else
        {
            e.returnValue=false;
            return false;
        }
    }
}

function UpdateInputElementIfEmpty(keyCode, emptyReplaceValue, inputElement, intSelectionStart)
{
    if(inputElement.value == "") inputElement.value = emptyReplaceValue;
    
    return true;
}

function CalculateNetValue(e, varObject, strID)
{
    if(varObject)
    {   
        if(varObject.value == "") varObject.value = "0.00";
            
        if(((varObject.value).indexOf(".") != -1) && (((varObject.value).length-(varObject.value).indexOf(".")) > 3 ))
        {
            varObject.value = FixedDecimals(varObject.value,2,true);
        }
        
        var StartingValue = 0.00, sBrokerage = "0.00", sCommision = "0.00", sNetValue = "0.00";        
        StartingValue= parseFloat(FixedDecimals(Math.abs(varObject.value),2,true));        
        sNetValue=(FixedDecimals((StartingValue * 0.7),2,true)); // parseFloat((StartingValue * 0.7).toFixed(2));
        
        sCommision=FixedDecimals(((FixedDecimals((StartingValue - parseFloat(sNetValue)),2,true))/2),2,true);

        sBrokerage=(FixedDecimals((StartingValue-(parseFloat(sNetValue) + parseFloat(sCommision))),2,true));
        
        window.document.getElementById("tdBrokerageValue"+strID).innerHTML="£" + (sBrokerage);
        window.document.getElementById("tdCommisionValue"+strID).innerHTML="£" + (sCommision);
        window.document.getElementById("tdNetValue"+strID).innerHTML="£" + sNetValue;        
    }
}

function IncrementDigitByDigit(sValue)
{
    //alert(sValue);
    if(sValue == "")
        return "0";
    else
    {
        var intCtr, sOutPut = "", IsIncrement = true;
        //alert(sValue.length);
        for(intCtr = (sValue.length-1); intCtr >= 0;intCtr--)
        {
            //alert(sValue.substr(intCtr,1));
            if(IsIncrement)
            {
                if((Number(sValue.substr(intCtr,1)) + 1) > 9)
                {
                    IsIncrement = true;
                    sOutPut = (new String(Number(sValue.substr(intCtr,1)) + 1)).substr(1,1) + sOutPut;
                }
                else
                {
                    IsIncrement = false;
                    sOutPut = new String(Number(sValue.substr(intCtr,1)) + 1)+ sOutPut;
                }
            }
            else
                sOutPut = sValue.substr(intCtr,1) + sOutPut;            
        }
            //alert(sValue + '\n' + sOutPut);
        if(IsIncrement)
            sOutPut= "1." + sOutPut;
        return sOutPut;
    }
}

function FixedDecimals(dblValue, intNoOfDecimals, intIsRoundUp)
{
//the use of intNoOfDecimals has been removed from the below code bec most of time we need two digits only
    var strValue = new String(Math.abs(dblValue));
    if(strValue == "")
        strValue = "0.00";
    
    var strOPValue = "0.00";
    if(strValue.indexOf(".") == -1)
    {    
        strOPValue = strValue + ".00";
    }
    else
    {        
        var strNewDecimalString = "";
        var strNumber = strValue.split(".")[0];
        var strDecimalString = strValue.split(".")[1];
        if(strDecimalString.length > 2)
        {           
            if(intIsRoundUp)
            {
                var intThirdDigit = parseInt(strDecimalString.substr(2,1),10);
                //strNewDecimalString = strDecimalString.substr(0,1);
                var sTmpNo =  (strDecimalString.substr(0,2));
                //alert(sTmpNo);
                if(intThirdDigit >= 5)
                {
                    //intTmpNo++;
                    //alert(sTmpNo + '\n' + IncrementDigitByDigit((sTmpNo)));
                    sTmpNo = IncrementDigitByDigit((sTmpNo)); //Increment function is used because 04+1=returns 5 instead of 05
                    if(sTmpNo.indexOf(".") != -1)
                    {   strNumber = new String(Number(strNumber) + 1);
                        sTmpNo = sTmpNo.split(".")[1];
                    }
                    //sTmpNo = new String(parseInt(sTmpNo,10) + 1);
                    
                    if(sTmpNo.length > 2)
                    {
                        strNumber = new String(Number(strNumber) + 1);
                        strNewDecimalString = "00";
                    }
                    else                        
                        strNewDecimalString = sTmpNo;
                    
                }
                else
                    strNewDecimalString = strDecimalString.substr(0,1) + strDecimalString.substr(1,1);                    
            }
            else
                strNewDecimalString = strDecimalString.substr(0,2);
        }
        else
        {
            var intCtr;
            for(intCtr = strDecimalString.length;intCtr < 2 ; intCtr++)
                strDecimalString = strDecimalString + "0";
                
            strNewDecimalString = strDecimalString;
        }
        strOPValue = strNumber + "." + strNewDecimalString;
    }    
    return strOPValue;
}


function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function BoolToInt(_Object)
{
    if(_Object == true)
        return "1";
    else
        return "0";
}



function CheckInputCharsForDouble(e, varObject, varEmptyReplaceValue)
{

    if(varObject && e != null)
    {    
        var code = 0, blnFlag = false, varKeyCode = 0, intSelectionStart = 0;
	    if (!e) var e = window.event;
	    if (e.keyCode) code = e.keyCode; //FOR SPECIAL CHARS   
	    else if (e.which) varKeyCode = e.which;	 // FOR DIGITS, CHARS, AND OTHER CHARS
	    
	    intSelectionStart=getSelectionStart(varObject);
	    if(is_ie)
	    {
	        varKeyCode=code;
	        code=0;
	    }   

	    if(BrowserDetect.browser == "Safari" || BrowserDetect.browser == "Mozilla")
	    {
	        if((code >= 48 && code <= 57) || (varKeyCode == 46 || code == 116 || code == 46 || code == 8 || code == 6 || code == 37 || code == 38 || code == 39 || code == 40 || code == 9 ))
	        {       
                if(varObject.value == "")
                    varObject.value = varEmptyReplaceValue;
                var intTotalLength = (varObject.value).length;
                var intIndexOfDecimal =(varObject.value).indexOf(".");
                    
                if((code == 46) && (intIndexOfDecimal != -1))
                    blnFlag=false;
                else if((intIndexOfDecimal != -1) && (intTotalLength-intIndexOfDecimal > 2) && (code >= 48 && code <= 57) && (intSelectionStart > intIndexOfDecimal))
                    blnFlag=false;
                else
                    blnFlag=true;
                    
            }
	    }
	    else
	    {
	        if((varKeyCode >= 48 && varKeyCode <= 57) || (varKeyCode == 46 || code == 116 || code == 46 || code == 8 || code == 6 || code == 37 || code == 38 || code == 39 || code == 40 || code == 9 ))
	        {            
                if(varObject.value == "")
                    varObject.value = varEmptyReplaceValue;
                var intTotalLength = (varObject.value).length;
                var intIndexOfDecimal =(varObject.value).indexOf(".");
                    
                if((varKeyCode == 46) && (intIndexOfDecimal != -1))
                    blnFlag=false;
                else if((intIndexOfDecimal != -1) && (intTotalLength-intIndexOfDecimal > 2) && (varKeyCode >= 48 && varKeyCode <= 57) && (intSelectionStart > intIndexOfDecimal))
                    blnFlag=false;
                else
                    blnFlag=true;
            }
        }
        if(blnFlag)
            return true;
        else
        {
            e.returnValue=false;
            return false;
        }
    }
}

