// JavaScript Document<script language=javascript>

/***************************************************************************************************

* Java script file 
* 
* @ Author 			Muhammad A. Noman
* @ Version			1.6
*
* @ Created			August 20, 2006
*
* @ Last modified	
* ===============
* 
* October 5, 2006
* Added WinPDF_e & WinPDF_f functions
* Added toggle table functinality (require id for table)
*
* Nov 01, 2006
* Added email validation function
* Added Add image function
***************************************************************************************************/

//--------------------- HEADER SEARCH RELATED FUNCTIONS---------------------------------------------
//-- search submit function 
function doSubmit()
{
	if(document.search_template.search_criteria.value=="ci") {
		submit_google_search();
	} else if(document.search_template.search_criteria.value=="CIG") {
		submit_globalfund_search_fid();
	} else if(document.search_template.search_criteria.value=="fname") {
		submit_globalfund_search_name("FUNDNAME");	//FUNDNAME or FUNDCODE
	}
}
//-- search form (via name search)
function submit_globalfund_search_name(param2)
{
	document.globalfund_search_name.fr_param1.value	=document.search_template.search_text.value;
	document.globalfund_search_name.fr_mode.value	=param2;
//	document.globalfund_search.action=	'http://fund.ci.com/gishome/plsql/cif<%=(gbl_isEnglishCurrentLang)?(""):("_f")%>.process_fr';	// defiend in header.jsp
	document.globalfund_search_name.submit();
}
//-- search form (via fund code) 
function submit_globalfund_search_fid()
{
	document.globalfund_search_fid.fundsymbol.value	= document.search_template.search_text.value.replace(/[a-zA-Z]$/, "");
	document.globalfund_search_fid.submit();
}

//-- google search form 
function submit_google_search()
{
	document.google_search.q.value	=document.search_template.search_text.value;
//	document.google_search.action ="http://www.google.com/custom";	// defiend in header.jsp
	document.google_search.submit();
}

function AddPre()
{

	if (document.search_template.search_criteria.value =="CIG")
	{
		document.search_template.search_text.value="CIG";
		var stxtf=document.search_template.search_text;
		var pos=3; 
	
		setCaretTo(document.search_template.search_text, pos);
	}
	else
		document.search_template.search_text.value="";
	document.search_template.search_text.focus();
	return;
}	

function insertAtCaret(obj, text) 
{
	if(document.selection) 
	{
		obj.focus();
		var orig = obj.value.replace(/\r\n/g, "\n");
		var range = document.selection.createRange();

		if(range.parentElement() != obj) 
			return false;

		range.text = text;
		
		var actual = tmp = obj.value.replace(/\r\n/g, "\n");

		for(var diff = 0; diff < orig.length; diff++) 
			if(orig.charAt(diff) != actual.charAt(diff)) 
				break;

		for(var index = 0, start = 0; 
			tmp.match(text) 
				&& (tmp = tmp.replace(text, "")) 
				&& index <= diff; 
			index = start + text.length
		) 
		{
			start = actual.indexOf(text, index);
		}
	} 
	else if(obj.selectionStart) 
	{
		var start = obj.selectionStart;
		var end   = obj.selectionEnd;

		obj.value = obj.value.substr(0, start) 
			+ text 
			+ obj.value.substr(end, obj.value.length);
	}
	
	if(start != null) 
	{
		setCaretTo(obj, start + text.length);
	} else 
	{
		obj.value += text;
	}
}
	
function setCaretTo(obj, pos) 
{
	if(obj.createTextRange) 
	{
		var range = obj.createTextRange();
		range.move('character', pos);
		range.select();
	} else if(obj.selectionStart) 
	{
		obj.focus();
		obj.setSelectionRange(pos, pos);
	}
}


//--------------------- OTHER FUNCTIONS ---------------------------------------------
//-- popup window open function
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_openCtrlBrWindow(theURL,features){
	  window.open(theURL,"",features);
}
//-- popup functions (legacy code)
function WinPDF_e(url) 
{
	if(confirm("CI Funds offered at this website are available only to Canadian residents. In order to download the document or receive any additional information, please click OK to confirm that you are a Canadian resident. If you are not a Canadian resident, click CANCEL."))
	   MM_openBrWindow(url,'','scrollbars=yes,resizable=yes');
}
//-- popup functions (legacy code)

function WinPDF_f(url) {

	if(confirm("Les fonds CI offerts sur ce site s'adressent uniquement aux résidents du Canada. Pour télécharger ce document ou recevoir des renseignements complémentaires, veuillez cliquer sur OK pour confirmer que vous êtes résident canadien. Si vous ne résidez pas au Canada, cliquez sur ANNULER."))

	   MM_openBrWindow(url,'','scrollbars=yes,resizable=yes');

}



//------------------ collapsale table--------------------------------------------------

//-- script for collapsible table

function getItem(id)

{

	var itm = false;

	if(document.getElementById)

	itm = document.getElementById(id);

	else if(document.all)

	itm = document.all[id];

	else if(document.layers)

	itm = document.layers[id];

	

	return itm;

}



function toggleItem(id)

{

	itm = getItem(id);

	

	if(!itm)

	return false;

	

	if(itm.style.display == 'none')

	itm.style.display = '';

	else

	itm.style.display = 'none';

	

	return false;

}



function toggle_All(doHideAll)

{

	var tbls = document.getElementsByTagName("table");



	for(var i=0; i<tbls.length; i++)

	{

		if(tbls[i].id != "" && tbls[i].id!=null)

		{

			//alert(tbls[i].id);

			var myel=document.getElementById(tbls[i].id);

			if(doHideAll)

				myel.style.display = 'none';

			else

				myel.style.display = '';

		}

	}

	

}





function collapse_All()

{

	toggle_All(true);

}





function expand_All()

{

	toggle_All(false);

}

//------------------ MISC functions --------------------------------------------------

//-- email validation function

function checkEmail(valv) 

{

	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valv))

		return (true)

	else

		return (false)

}

//-- submit via keyboard enter

function SubmitOnEnter(myfield,e)

{

	var keycode;

	if (window.event) keycode = window.event.keyCode;

	else if (e) keycode = e.which;

	else return true;

	

	if (keycode && keycode == 13) { // enter key = 13

		doSubmit();

		return false;

	} else {

		return true;

	}

}



//-- add image button function with rolloever

function addButton(ahref, onClick, bt_nid, img_norm, img_over, alt_txt)

{

	var bt2write="<a href='"+ahref+"' onClick='"+onClick+"' onMouseOut='MM_swapImgRestore()' onMouseOver='MM_swapImage(\""+bt_nid+"\",\"\",\""+img_norm+"\",1)'><img src='"+img_over+"' name='"+bt_nid+"' id='"+bt_nid+"' border='0' alt='"+alt_txt+"' ></a>";

	document.write(bt2write);

}



//--- MARKETS INCLUDE SCRIPTS

// set your interval in milliseconds

var reloadInterval = 300000;	// 5 min = 5 x 60 x 1000

// this will run when the document is fully loaded

function init() {

 setTimeout('reload()',reloadInterval);

}

// this reloads the iframe, and triggers the next reload interval

function reload() {

 var iframe = document.getElementById('reloader');

 if (!iframe) return false;

 iframe.src = iframe.src;

 setTimeout('reload()',reloadInterval);

 //alert('reloading');

}

// load the init() function when the page is fully loaded

//window.onload = init;

//----------------------------



function trim(s) 

{

  while (s.substring(0,1) == ' ') 

  {

    s = s.substring(1,s.length);

  }

  while (s.substring(s.length-1,s.length) == ' ') 

  {

    s = s.substring(0,s.length-1);

  }

  return s;

}

//-------------------------------

//-- popup window open function

function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);

}



function MM_openCtrlBrWindow(theURL,features){

	  window.open(theURL,"",features);

}



