// $Id: util.js 15421 2009-05-19 11:37:19Z dave $
// (c) Ecube 2007

// Remove all commas from supplied string
function stripCommas(strval) {
    var CommaRegExp = new RegExp(/[0-9.]+,[0-9.]/);

    while (CommaRegExp.test(strval)) {
        strval = strval.replace(',','');
    }

    return strval;
}

// Format numbers with commas before the decimal point
function addCommas( strValue ) {
    var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');
    var RegExp2 = new RegExp(/([0-9]+)\.([0-9]+)/);
		
    var strBefore = '';
    var strAfter = '';
		
    if (RegExp2.test(strValue)) {
        strBefore = strValue.replace(RegExp2,"$1");
        strAfter = strValue.replace(RegExp2,"$2");
    } else {
        strBefore = strValue;
    }
		
    //check for match to search criteria
    while(objRegExp.test(strBefore)) {
        strBefore = strBefore.replace(objRegExp, '$1,$2');
    }
    if (strAfter.length > 0)
        strValue = strBefore + '.' + strAfter;
    else
        strValue = strBefore;
    
    return strValue;
}

function formatWithCommas(strval) {
    return addCommas(stripCommas(strval));
}


// Cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko 
// By Scott Andrew
function addEvent(elm, evType, fn, useCapture) {
    if (elm.addEventListener) {
        elm.addEventListener(evType, fn, useCapture);
        return true;
    } else if (elm.attachEvent) {
        var r = elm.attachEvent('on' + evType, fn);
        return r;
    } else {
        elm['on' + evType] = fn;
    }
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getRadioButtonValue(radioObj) {
    if(!radioObj) {
        return "";
    }

    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked) {
            return radioObj.value;
        } else {
            return "";
        }
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setRadioButtonValue(radioObj, newValue) {
    if(!radioObj) {
        return;
    }

    var radioLength = radioObj.length;
    if(radioLength == undefined) {
        radioObj.checked = (radioObj.value == newValue.toString());
        return;
    }
    for(var i = 0; i < radioLength; i++) {
        radioObj[i].checked = false;
        if(radioObj[i].value == newValue.toString()) {
            radioObj[i].checked = true;
        }
    }
}

// Given a display FX rate determine what to do to make it a rate we can 
// multiply
// This must only be changed in conjuction with  FxRate.convert
function fxConvert(amount, fromCcy, toCcy, fxrate) {
    if (fromCcy == toCcy) {
        return amount;
    } else if (fxrate > 0.0) {
        if (fromCcy == "GBP") {
            return amount * fxrate;
        } else if (toCcy == "GBP") {
            return amount / fxrate;
        } else if (fromCcy == "EUR") {
            return amount * fxrate;
        } else if (toCcy == "EUR") {
            return amount / fxrate;
        } else if (fromCcy == "USD") {
            return amount * fxrate;
        } else if (toCcy == "USD") {
            return amount / fxrate;
        } else {
            return amount / fxrate;
        }
    } else {
        return 0.0;
    }
}

/*
**  Returns the caret (cursor) position of the specified text field.
**  Return value range is 0-oField.length.
*/
function doGetCaretPosition (oField) {
	 // Initialize
	var iCaretPos = 0;
	 // IE Support
	if (document.selection) {
	   // Set focus on the element
	   oField.focus ();

	   // To get cursor position, get empty selection range
	   var oSel = document.selection.createRange ();

	   // Move selection start to 0 position
	   oSel.moveStart ('character', -oField.value.length);

	   // The caret position is selection length
	   iCaretPos = oSel.text.length;
	}// Firefox support
	else if (oField.selectionStart || oField.selectionStart == '0'){
	   iCaretPos = oField.selectionStart;
	}
	 // Return results
	 return (iCaretPos);
}


/*
**  Sets the caret (cursor) position of the specified text field.
**  Valid positions are 0-oField.length.
*/
function doSetCaretPosition (oField, iCaretPos) {
	 // IE Support
	 if (document.selection) { 

	   // Set focus on the element
	   oField.focus ();

	   // Create empty selection range
	   var oSel = document.selection.createRange ();

	   // Move selection start and end to 0 position
	   oSel.moveStart ('character', -oField.value.length);

	   // Move selection start and end to desired position
	   oSel.moveStart ('character', iCaretPos);
	   oSel.moveEnd ('character', 0);
	   oSel.select ();
	 }// Firefox support
	 else if (oField.selectionStart || oField.selectionStart == '0') {
	   oField.selectionStart = iCaretPos;
	   oField.selectionEnd = iCaretPos;
	   oField.focus ();
	 }
}

var Research = {
    m_over : function(el) {
	if (el.className.indexOf(" over") == -1)
	    el.className += " over";
    },
 
    m_out: function (el) {
	el.className = el.className.replace(/ over/, "");
    },

    m_showpdf: function (id, f) {
	var w = window.open("/x/research?id=" + encodeURIComponent(id) + "&f=" + encodeURIComponent(f));
	if (w) w.focus();
    },

    m_showtxt: function (id) {
	var w = window.open("/x/research-display.html?rid=" + encodeURIComponent(id));
	if (w) w.focus();
    },
    
    company_link: function(id) {
	location.href = "research-company.html?cid=" + encodeURIComponent(id);
    },
	sector_link: function(id) {
	location.href = "research-sector.html?sid=" + encodeURIComponent(id);
    }
}
