// Declare namespace
function Rivkin() { }

Rivkin.pc = function() {
	// set/handle permanent cookie
	var popup = $('popup_wrapper');
	if (Rivkin.readCookie('rr_perm') != '1') {
		setTimeout(function(){
			if (popup) {
				// Show popup
				popup.style.display='block';
				// Set cookie
				document.cookie = 'rr_perm=1; expires=Tue, 1 Jan 2019 12:15:00 UTC; path=/';
			}
		},5000);
	} else {
		// Cookie already set
		if (popup) {
			popup.style.display='none';
		}
	}
}

Rivkin.readCookie = function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
Rivkin.openPrintableVersion= function(objLink) {
	var s_width = parseInt(parent.document.documentElement.clientWidth);
	if (s_width == 0) s_width = parseInt(parent.document.body.clientWidth);
	var s_height = parseInt(parent.document.documentElement.clientHeight);
	if (s_height == 0) s_height = parseInt(parent.document.body.clientHeight);

	var pWin = window.open(objLink.href,'_printVer','top=100,left=100,height=' + (s_height - 200) + ',width=' + (s_width - 200) + ',resizable=yes,scrollbars=yes,menubar=yes');
	pWin.focus();
	return false;
}

Rivkin.openInvestorCentreWindow = function(strHref) {
	var s_width = parseInt(parent.document.documentElement.clientWidth);
	if (s_width == 0) s_width = parseInt(parent.document.body.clientWidth);
	var s_height = parseInt(parent.document.documentElement.clientHeight);
	if (s_height == 0) s_height = parseInt(parent.document.body.clientHeight);

	var iWin = window.open(strHref,'_riv_icParallel','top=100,left=100,height=' + (s_height - 200) + ',width=' + (s_width - 200) + ',resizable=yes,scrollbars=yes');
	iWin.focus();
	return false;
}

Rivkin.openInvestorCentreParallel = function() {
	// Pre-schedule move to investor centre in source window
	setTimeout("document.location.href='" + CMS.appRoot + "InvestorCentre.aspx';",250);
	
	// Open investor centre window
	Rivkin.openInvestorCentreWindow(CMS.appRoot + 'ui.ashx?f=riv_investor_centre');
}

// Dropdown function overrides
// Close (conditionally)
Rivkin.ddCvx = function(strIdn) {
	strId = 'dm_' + strIdn;
	$(strId).dmEnable = 0;
	setTimeout('Rivkin.cVis(\'dm_'+strIdn+'\',$(\''+strId+'\').dmEnable)',100);
}

Rivkin.cVis = function(strId, dmx) {
	if (dmx != 1) { 
		var objTgt = $(strId);
		if (objTgt._hasIframe == 1) objTgt._iframe.style.display='none';
		objTgt.style.display='none';
	}
}
// Open DD
Rivkin.ddOvx = function(strIdn) {
	strId = 'dm_'+strIdn;
	strTopId = 'ml_'+strIdn;
	var objTgt = $(strId);
	objTgt.dmEnable = 1;
	objTgt.style.top=(findPosY($(strTopId)) + $(strTopId).offsetHeight + 1) + "px";
	objTgt.style.left=findPosX($(strTopId)) + "px";
	objTgt.style.display='block';
	if (document.selection) {
		// IE IFRAME underlay trick
		if (objTgt._hasIframe != 1) {
			var iFrame = document.createElement('IFRAME');
			iFrame.className = 'dd_iframe_underlay';
			iFrame.frameBorder = 'no';
			iFrame.style.display = 'none';
			iFrame.style.top = objTgt.style.top;
			iFrame.style.left = objTgt.style.left;
			iFrame.style.width = objTgt.offsetWidth + 'px';
			iFrame.style.height = objTgt.offsetHeight + 'px';
			objTgt._iframe = iFrame;
			objTgt.parentNode.appendChild(iFrame);
			objTgt._hasIframe = 1;
		}
		objTgt._iframe.style.display='block';
	}
}
// Sustain DD
Rivkin.ddSvx = function(strIdn) {
	strId = 'dm_'+strIdn;
	$(strId).dmEnable = 1;
}


// Filter recommendations
Rivkin.filterRec = function() {
	var table_obj = $('recommendation_tbl');
	var filterObjects = new Array($('r_filt_risk'),$('r_filt_type'),$('r_filt_sector'),$('r_filt_rec'));
	var filterColumns = new Array(2,3,5,6);
	var exactMatch = new Array(true,false,false,false);
	
	Rivkin.filterRecInner(table_obj, filterObjects, filterColumns, exactMatch);
}

Rivkin.filterRecInner = function(table_obj,filterObjects,filterColumns,exactMatch) {
	for (var i=0; i < table_obj.childNodes.length; i++) {
		var this_row = table_obj.childNodes[i];

		if (this_row.tagName == 'TR') {
			// we need to set it visible to make the reading of innerHTML behave
			if (document.selection) {
				// IE
				this_row.style.display = 'block';
			} else {
				// DOM
				this_row.style.display = 'table-row';	// default; make it visible
			}

			if (this_row.className == 'selected' || this_row.className == 'listhdr') {
				// formerly set-visible code
			} else {
				var foundMatch = true;

				for (var c=0; c < filterObjects.length; c++) {
					var val = filterObjects[c].value;
					if (val != null && val != '') {
						var this_cell = this_row.childNodes[filterColumns[c]];
						var innerText = this_cell.innerHTML;
						if (exactMatch[c] == true) {
							if (innerText.toLowerCase() != val.toLowerCase()) foundMatch = false;
						} else {
							if (innerText.toLowerCase().indexOf(val.toLowerCase()) == -1) foundMatch = false;
						}
					}
				}

				if (foundMatch == false) {
					this_row.style.display = 'none';
				}
			}
		} else if (this_row.tagName == 'TBODY') {
			Rivkin.filterRecInner(this_row, filterObjects, filterColumns, exactMatch);
		}
	}
}

Rivkin.selectOnlyOneRow = function(obj) {
	var rowObj = cms_findParentByTag(obj,'TR');

	for (var idx=0; idx < rowObj.parentNode.childNodes.length; idx++) {
		rowObj.parentNode.childNodes[idx].className = '';
	}

	rowObj.className=obj.checked?'selected':'';
}

Rivkin.GeneratePassword = function() {
	var length=8;
	var sPassword = "";
	for (i=0; i < length; i++) {

		numI = Rivkin.getRandomNum();
		while (Rivkin.checkPunc(numI)) { numI = Rivkin.getRandomNum(); }

		sPassword = sPassword + String.fromCharCode(numI);
	}

	return sPassword;
}

Rivkin.getRandomNum = function() {
	var rndNum = Math.random()
	rndNum = parseInt(rndNum * 1000);
	rndNum = (rndNum % 94) + 33;
	return rndNum;
}
Rivkin.checkPunc = function(num) {
	if ((num >=33) && (num <=47)) { return true; }
	if ((num >=58) && (num <=64)) { return true; }
	if ((num >=91) && (num <=96)) { return true; }
	if ((num >=123) && (num <=126)) { return true; }
	return false;
}

Rivkin.startClock = function(tgt,countdownDate) {
	tgt.innerHTML = '[countdown]';
	tgt._countdownDate = countdownDate;
	try { Rivkin.tickClock(tgt); }
	catch(e) { }
	setInterval(function() { Rivkin.tickClock(tgt) },1000);
};

Rivkin.tickClock = function(tgt) {
	var diff = (tgt._countdownDate - new Date());
	var days = Math.floor(diff / (1000 * 60 * 60 * 24));
	var hours = Math.floor(diff / (1000 * 60 * 60));
	var minutes = Math.floor(diff / (1000 * 60));
	var seconds = Math.floor(diff / (1000));

	tgt.innerHTML = '<table>'
		+ '<tr class="top"><td>'+days+'</td><td class="c">:</td><td>'+(hours % 24)+'</td><td class="c">:</td><td>'+(minutes % 60)+'</td><td class="c">:</td><td>'+(seconds % 60)+'</td></tr>'
		+ '<tr class="btm"><td>days</td><td></td><td>hrs</td><td></td><td>min</td><td></td><td>sec</td></tr>'
		+ '</table>';
};

/*
	FlashReplace is developed by Robert Nyman, http://www.robertnyman.com, and it is released according to the
	Creative Commons Deed license (http://creativecommons.org/licenses/GPL/2.0/)
*/
// ---
var FlashReplace = {
	elmToReplace : null,
	flashIsInstalled : null,
	defaultFlashVersion : 7,
	replace : function (elmToReplace, src, id, width, height, wmode, flashvars, version, params){
		this.elmToReplace = $(elmToReplace);
		this.flashIsInstalled = this.checkForFlash(version || this.defaultFlashVersion);
		if(this.elmToReplace && this.flashIsInstalled){
			var obj = '<object' + ((window.ActiveXObject)? ' id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + src + '"' : '');
			obj += ' width="' + width + '"';
			obj += ' height="' + height + '"';
			obj += '>';
			param = '<param';
			param += ' name="movie"';
			param += ' value="' + src + '"';
			param += '>';
			if(wmode!=""){
				param += '<param';
				param += ' name="wmode"';
				param += ' value="' + wmode + '"';
				param += '>';
			}
			if(flashvars!=""){
				param += '<param';
				param += ' name="FlashVars"';
				param += ' value="' + flashvars + '"';
				param += '>';
			}
			param += '';
			var extraParams = '';
			var extraAttributes = '';
			for(var i in params){
				extraParams += '<param name="' + i + '" value="' + params[i] + '">';
				extraAttributes += ' ' + i + '="' + params[i] + '"';
			}
			var embed = '<embed id="' + id + '" src="' + src + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"';
			if(wmode!=""){
				embed += ' wmode="' + wmode + '"';
			}
			if(flashvars!=""){
				embed += ' flashvars="' + flashvars + '"';
			}
			embed += '"';
			var embedEnd = extraAttributes + '></embed>';
			var objEnd = '</object>';
			this.elmToReplace.innerHTML = obj + param + extraParams + embed + embedEnd + objEnd;			
		}
	},
	
	checkForFlash : function (version){
		this.flashIsInstalled = false;
		if(window.ActiveXObject){
			try{
				var flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
				this.flashIsInstalled = true;
			}
			catch(e){
				// Throws an error if the version isn't available			
			}
		}
		else if(navigator.plugins && navigator.mimeTypes.length > 0){
			var flash = navigator.plugins["Shockwave Flash"];
			if(flash){
				var flashVersion = navigator.plugins["Shockwave Flash"].description;
				var fvStrip = parseFloat(flashVersion.replace(/[^0-9]*((\d+)\.(\d+)).*/, "$1"));
				if(fvStrip >= version){
					this.flashIsInstalled = true;
				}
			}
		}
		return this.flashIsInstalled;
	}
};
// ---