//Portions are Copyright (c) 2000-2005, Mobular Technologies&#174;, Inc.  All Rights Reserved.
//Mobular and Mobular Technologies are Registered Trademarks of Mobular
//Technologies, Inc.
//All rights reserved.  Unauthorized reproduction prohibited.
//http://www.mobular.com/
//

var zoomInEnable = {
	_obj 			: "zoomInImg",
	src 			: "images/zoom_in.gif",
	alt 			: "Zoom in"
}

var zoomInDisable = {
	_obj 			: "zoomInImg",
	src 			: "images/zoom_in_dis.gif",
	alt 			: ""
}

var zoomOutEnable = {
	_obj 			: "zoomOutImg",
	src 			: "images/zoom_out.gif",
	alt 			: "Zoom out"
}

var zoomOutDisable = {
	_obj 			: "zoomOutImg",
	src 			: "images/zoom_out_dis.gif",
	alt 			: ""
}

var firstEnable = {
	_obj 			: "first",
	src 			: "images/rewind.gif",
	alt 			: "First page"
}

var firstDisable = {
	_obj 			: "first",
	src 			: "images/rewind_dis.gif",
	alt 			: ""
}

var previousEnable = {
	_obj 			: "previous",
	src 			: "images/previous.gif",
	alt 			: "Previous page"
}

var previousDisable = {
	_obj 			: "previous",
	src 			: "images/previous_dis.gif",
	alt 			: ""
}

var previousDetailEnable = {
	_obj 			: "previousDetail",
	src 			: "images/previous.gif",
	alt 			: ""
}

var previousDetailDisable = {
	_obj 			: "previousDetail",
	src 			: "images/previous_dis.gif",
	alt 			: ""
}

var nextEnable = {
	_obj 			: "next",
	src 			: "images/next.gif",
	alt 			: "Next page"
}

var nextDisable = {
	_obj 			: "next",
	src 			: "images/next_dis.gif",
	alt 			: ""
}

var nextDetailEnable = {
	_obj 			: "nextDetail",
	src 			: "images/next.gif",
	alt 			: ""
}

var nextDetailDisable = {
	_obj 			: "nextDetail",
	src 			: "images/next_dis.gif",
	alt 			: ""
}

var nextDocEnable = {
	_obj 			: "nextdoc",
	src 			: "images/fastforw.gif",
	alt 			: "Next document"
}

var nextDocDisable = {
	_obj 			: "nextdoc",
	src 			: "images/fastforw_dis.gif",
	alt 			: ""
}

var singlePageDisable = {
	_obj 			: "singlepage",
	src 			: "images/cs_1page_dis.gif",
	alt 			: ""
}

var singlePageEnable = {
	_obj 			: "singlepage",
	src 			: "images/cs_1page.gif",
	alt 			: "Single Page View"
}

var twoPageDisable = {
	_obj 			: "twopage",
	src 			: "images/cs_2page_dis.gif",
	alt 			: ""
}

var twoPageEnable = {
	_obj 			: "twopage",
	src 			: "images/cs_2page.gif",
	alt 			: "Double Page View"
}


var uiElements = {

	imgSettings : [
					"_obj",
					"src",
					"alt"
				  ],
	processImg: function(settings, alt_override) {

		var collection = uiElements.imgSettings;
		var _obj = settings[collection[0]];
		var obj = document.getElementById(_obj);
		if (obj) {
			for ( var n=1; n < collection.length; n++ ) {
				var setting = collection[n];
				try {
					var _setting = settings[setting];
					if (alt_override && setting == "alt") { _setting = alt_override }
					obj.attributes[setting].value = _setting;
				} catch (e) { }
			}
		}
	},
	_disableAll: function() {
		// anything else is probably not in the book.
    	this.processImg(zoomInDisable);
	    this.processImg(zoomOutDisable);
	    this.processImg(firstDisable);
	    this.processImg(previousDisable);
		this.processImg(previousDetailDisable);
	    this.processImg(nextDisable);
		this.processImg(nextDetailDisable);
	    this.processImg(nextDocDisable);
		this.processImg(singlePageDisable);
		this.processImg(twoPageDisable);
	}
};

function checkForWait(fnc, flg) {
	if ( window.eengine ) {
		if ( flg == 0 ) {
			eval(fnc);
		}else{
			// this is only necessary because we have a mousedown in the page.
			if ( ( navigator.appVersion.indexOf("Safari") == -1 ) && ( navigator.appName == "Netscape" ) && (navigator.appVersion.indexOf("Mac")!= -1) && (parseInt(navigator.appVersion.charAt(0)) < 6) ) {
			// do nothing ... macRun takes care of this.
			}else{
				eval(fnc);
			}
		}
	}else{
		if (window.loadWarn) {
			alert(loadWarn);
		} else {
			alert("Please wait while the Interactive Document loads");
		}
	}
	return false;
}

// Event model listeners
function onEngineIdle() {
	if ( document.getElementById('toc_placeholder') ) {
		var _div = document.getElementById('toc_placeholder');
		var _title = binder.kitData.description;
		var _param = {
			buttonText: ''+_title,
			id: 'toc_placeholder',
			buttonTemplate: "<span class=\"dijit dijitReset dijitLeft dijitInline\" dojoAttachEvent=\"onmouseenter:_onMouse,onmouseleave:_onMouse,onmousedown:_onMouse,onclick:_onDropDownClick,onkeydown:_onDropDownKeydown,onblur:_onDropDownBlur,onkeypress:_onKey\" waiRole=\"presentation\"><div class='dijitReset dijitRight' waiRole=\"presentation\"><button class=\"dijitReset dijitStretch dijitButtonNode dijitButtonContents\" type=\"${type}\"dojoAttachPoint=\"focusNode,titleNode\" waiRole=\"button\" waiState=\"haspopup-true,labelledby-${id}_label\"><div class=\"dijitReset dijitInline dijitButtonText\"  dojoAttachPoint=\"containerNode,popupStateNode\" waiRole=\"presentation\"id=\"${id}_label\">${label}</div></button></div></span>"
		};
		new display.dropDownTOC.dropDownTOC(_param, _div);
	}

	document.getElementById('active_page_count').innerHTML = "";
	dijit.byId('tocDDbutton').containerNode.innerHTML = '[+] Click to select';

	uiElements._disableAll();
	
	recreateShowShow();
}
eengineConfig.subscribe( "onEngineIdle()", "eengine.postProcess.engineidle");

// this is a workaround until the full themes system can be implemented.
function injectStyle(widget, mySession) {
	if ( mySession.event == -900 ) {
		/* add stylesheet code here like so:
			var css = '.rssCrumbBaseMiddle { background-color: #CCC; }';
		*/
		var css = '';
		css += '.rssWidgetContainer { width:  150px; height: 350px;	}';

		// css strings may be entered
		if ( css != '' ) {
			//dojo.html.insertCssText(css);
		}
	}
}
eengineConfig.subscribe( "injectStyle", "rssCrumb" );

function onDisplayS2F() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayS2F()", "eengine.postProcess.s2fUI" );

function onDisplayS2D() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayS2D()", "eengine.postProcess.s2dUI" );

function onDisplayPrint() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayPrint()", "eengine.postProcess.displayprintUI" );

function onDisplayHelp() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayHelp()", "eengine.postProcess.helpUI" );

function onDisplayPage(widget, mySession){
	var action = mySession.event;

	if (action == 902) {
		var _page_count = binder.kitData.documents[binder.kitData.current.doc].pages.length
		document.getElementById('active_page_count').innerHTML = "of " + _page_count;

		// adjust zoom first
		switch (eengine.zoomLvl) {
			case 1:
				uiElements.processImg(zoomInEnable);

				// last check buildSettings... if _startingLayout is 0, then don't let user drop below single page mode.
				if ( eengineConfig.buildSettings._startingLayout == "0" ) {
					// only display if they are allowed to return to two page mode.
					uiElements.processImg(zoomOutDisable);
				} else {
					uiElements.processImg(zoomOutEnable);
				}

				break;
			case 2://
				uiElements.processImg(zoomInDisable);
				uiElements.processImg(zoomOutEnable);
				//if zoomed in all the way - hide sidebar
                hideSidebar();
				break;
			default:
				// zoomLvl 0
				uiElements.processImg(zoomInEnable);
				uiElements.processImg(zoomOutDisable);
				break;
		}

		//setup single/two page buttons
		if (binder.kitData.current.layout == 1) {
				uiElements.processImg(twoPageDisable);
				uiElements.processImg(singlePageEnable);
		}else{
				uiElements.processImg(twoPageEnable);
				uiElements.processImg(singlePageDisable);
		}

		//setup navigation buttons
		if (isAtKitStart()) {
			uiElements.processImg(firstDisable);
			uiElements.processImg(previousDisable);
			uiElements.processImg(previousDetailDisable);

			uiElements.processImg(nextEnable);
			uiElements.processImg(nextDetailEnable);
			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt)
			}
		} else if (isAtKitEnd()) {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(previousDetailEnable);
			uiElements.processImg(nextDisable);
			uiElements.processImg(nextDetailDisable);
			uiElements.processImg(nextDocDisable);
		} else if (isAtDocStart()) {
			_alt = "Previous document - " + binder.kitData.documents[binder.kitData.current.doc-1].title;
			uiElements.processImg(firstEnable, _alt);

			_alt = "Previous document - " + binder.kitData.documents[binder.kitData.current.doc-1].title + " (last page)";
			uiElements.processImg(previousEnable, _alt);
			uiElements.processImg(previousDetailEnable, _alt);

			uiElements.processImg(nextEnable);
			uiElements.processImg(nextDetailEnable);

			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt);
			}
		} else if (isAtDocEnd()) {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(previousDetailEnable);

			_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
			uiElements.processImg(nextEnable, _alt);
			uiElements.processImg(nextDetailEnable, _alt);

			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt);
			}
		} else {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(previousDetailEnable);

			uiElements.processImg(nextEnable);
			uiElements.processImg(nextDetailEnable);
			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt);
			}
		}
	}
}
eengineConfig.subscribe( "onDisplayPage", "display.book.book");

function onDislayCover() {

	var event = eengine.reporting.getLatest();
	var action = parseInt( event.action, 10 );
	if ( action == -95 ) {
		document.getElementById('active_page_count').innerHTML = "";
		uiElements._disableAll();
	}
	//ensure sidebar is visible
	showSidebar();
}
eengineConfig.subscribe( "onDislayCover()", "eengine.postProcess.coverUI" );

function isAtDocStart() {
	var _current = binder.kitData.current;
	if ((_current.layout == 1 && _current.page <= 1) || (_current.layout == 0 && _current.page == 0)) {
		return true;
	} else {
		return false;
	}
}

function isAtKitStart() {
	var _current = binder.kitData.current;
	if (isAtDocStart() && _current.doc == 1) {
		return true;
	} else {
		return false;
	}
}

function isAtDocEnd() {
	var _kit = binder.kitData;
	var _current = _kit.current;
	if ((_current.layout == 1 && _current.page >= _kit.documents[_current.doc].pages.length - 1) || (_current.layout == 0 && _current.page == _kit.documents[_current.doc].pages.length - 1)) {
		return true;
	} else {
		return false;
	}
}

function isAtKitEnd() {
	var _kit = binder.kitData;
	var _current = _kit.current;
	if (isAtDocEnd() && _current.doc == _kit.documents.length - 1) {
		return true;
	} else {
		return false;
	}
}

function isLastDoc() {
	var _doc = binder.kitData.current.doc;
	if (_doc == binder.kitData.documents.length - 1) {
		return true;
	} else {
		return false;
	}
}

function zoomIn() {
	// First part of the if statement is a work around, I should just be able to call eengine.getZoom(0) to
	// zoom out.  Once all zoom issues are worked out the getZoom calls should go directly into the eesearch.html
	// template file and the zoomIn and zoomOut functions can be deleted.
	if (binder.kitData.current.layout == 1) {
		eengine.switchLayout();
	} else if (binder.kitData.current.layout == 0) {
		eengine.getZoom(0);
	}

	//if zoomed in all the way - hide sidebar
	if(eengine.zoomLvl == 2) {
		hideSidebar();
	}
}

function zoomOut() {
	eengine.getZoom(1);
	//ensure sidebar is visible
	showSidebar();
}

function goFirst() {
	var _doc = binder.kitData.current.doc;
	if (isAtDocStart() && !isAtKitStart()) {
		_doc--;
	}
	if (_doc > 0) {
		eengine.writePage(_doc, 0);
	}
}

function goPrevious() {
	var _doc = binder.kitData.current.doc;
	if (isAtDocStart() && !isAtKitStart()) {
		_doc--;
		var _page = binder.kitData.documents[_doc].pages.length - 1;
		eengine.writePage(_doc, _page);
	} else {
		eengine.writePage(-65536);
	}
}

function goNext() {
	var _doc = binder.kitData.current.doc;
	if (isAtDocEnd() && !isAtKitEnd()) {
		_doc++;
		eengine.writePage(_doc, 0);
	} else {
		eengine.writePage(65536);
	}
}

function goNextDoc() {
	var _doc = binder.kitData.current.doc;
	_doc++;
	if (_doc < binder.kitData.documents.length) {
		eengine.writePage(_doc, 0);
	}
}

function goSinglePage() {
	if(eengine.kitData.current.layout==1){
		eengine.switchLayout();
		uiElements.processImg(singlePageDisable);
		uiElements.processImg(twoPageEnable);
	}

}

function goTwoPage() {
	if(eengine.kitData.current.layout==0){
		eengine.switchLayout();
		uiElements.processImg(twoPageDisable);
		uiElements.processImg(singlePageEnable);
		showSidebar();
	}

}

function showSidebar(){
	var sidebar = document.getElementById('sidebar1');
	sidebar.style.visibility = 'visible';
	sidebar.style.display = 'inline';
}

function hideSidebar(){
	var sidebar = document.getElementById('sidebar1');
	sidebar.style.visibility = 'hidden';
	sidebar.style.display = 'none';
}

function recreateShowShow() {
}

function I2U_showEmailMatsToSelf() {

	// store actual s2f form
//	var backupForm = eengine.eeUtility.s2f;

	// replace with the about form
//	eengine.eeUtility.s2f = eengine.eeUtility.about;

	// display form
	//eengine.showForward();
    new display.microview.microview({title: 'E-mail the Interactive Document', type:'s2f', width: '710px', height: '500px',contentTemplate:'ui/showabout.html',responseTemplate:'ui/requestsuccess.html'});

	// return forms to original positions
//	eengine.eeUtility.s2f = backupForm;
}

function I2U_showRequestPrinted() {

    new display.microview.microview({title: 'Request Printed Proxy Materials', type:'s2f', width: '710px', height: '550px',contentTemplate:'ui/requestprinted.html',responseTemplate:'ui/requestsuccess.html'});
	
	setCtlNum();

}

//This function required because at times the view has not loaded and ctlnum element cannot be accessed yet - throws an error.
function setCtlNum(){
	var ctlnum = gup('ctlnum');
	if (ctlnum != ""){
		if (document.getElementById('ctlnum')){
			var txb_ctlnum = document.getElementById('ctlnum');
			txb_ctlnum.value = ctlnum;
			txb_ctlnum.disabled = true;
		}else{
			setTimeout('setCtlNum()', 500);
		}
	}

}

function I2U_ProxyMaterials_Validate(form) {
	// validate fields.
	var txb_email = document.getElementById('Email');
	var txb_name = document.getElementById('Name');
	var txb_sender = document.getElementById('Sender');
	var txb_subject = document.getElementById('subject');
	var txb_ctlnum = document.getElementById('ctlnum');


	//hide invalid Message div in case it was shown earlier
	document.getElementById('invalidMessage').style.display = 'none';

	// email pattern
	var reEmail = /[\w\W]+\@[\w\W]+\.[a-zA-Z0-9]+/i;
	var subj = [];

	// check name
	if ( txb_name.value == '' ) {
		subj[subj.length] = 'Name';
	} else if ( txb_name.value.length > 20 ) {
		subj[subj.length] = 'Name';
	}

	// check email
	if ( txb_sender.value == '' ) {
		subj[subj.length] = 'Email address';
	} else if (! reEmail.test( txb_sender.value) ) {
		subj[subj.length] = 'Email address (invalid format)';
	}

	// check control number
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if ( txb_ctlnum.value == '' ) {
		subj[subj.length] = 'Control Number';
	} else if ( txb_ctlnum.value.length != 11 ) {
		subj[subj.length] = 'Control Number (invalid length)';
	} else if ( ! anum.test(txb_ctlnum.value) ){
		subj[subj.length] = 'Control Number (must be numeric)';
	}

	//get default ticker value
	txt_ticker = g_eKitTitle;
	var urlpath = window.location.pathname;
	var urlpath_arr = urlpath.split( '/' );
	var p_test = 999;

	for ( var i = 0; i < urlpath_arr.length; i++ ) {
		if (urlpath_arr[i] == "bnymellon"){  //look for 'bnymellon'
			p_test = i;
			break;
		}
	};

	if ( p_test != 999 && p_test < urlpath_arr.length ){
		p_test++; //get next element - the one after the one we are searching for.
		if (urlpath_arr[p_test] != "index.html" && urlpath_arr[p_test] != "index.php" && isNaN(urlpath_arr[p_test]) ){
			txt_ticker = urlpath_arr[p_test];
			//remove %20 space char if it is in string.
			txt_ticker=txt_ticker.replace(/%20/g," ");
		}
	}

/*
alert ( "ticker \t\t = " + txt_ticker + "\n\n" +
		"window.location.pathname = " + urlpath + "\n\n" +
		"p_test \t\t = " + p_test);
*/

	var msg = '';
	if ( subj.length > 0 ) {
		msg = 'Please enter a valid:<br \>';
		for ( var s=0; s < subj.length; s++ ) {
			if ( s > 0 ) {
				msg += '<br \>&nbsp;&nbsp;&nbsp;' + subj[s];
			} else {
				msg += '&nbsp;&nbsp;&nbsp;' + subj[s];
			}
		}
	}

	if( msg != '' ) {
		// required field blank.. warn user
		var obj = document.getElementById('invalidMessage');
		obj.innerHTML = msg;
		obj.style.display = 'block';
		return false;
	} else {
		// required fields good, xmit
		var cmt = document.getElementById('Comments');
		cmt.value += "\nRequest for Printed Proxy Materials has been made for:\n";
		cmt.value += "E-kit Name: " + g_eKitTitle + '\n';
		cmt.value += 'Name: ' + txb_name.value + '\n';
		cmt.value += 'Requesting Email: ' + txb_sender.value + '\n';
		cmt.value += 'Control Number: ' + txb_ctlnum.value + '\n';
		cmt.value += 'Ticker: ' + txt_ticker + '\n\n';

//Debug - cca
//txb_email.value = "canderson@mobular.com";

		return true;
	}
}

/*
 * Get GET values from URL
 */
function gup( name ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

function changecss(theClass,element,value) {
	//Last Updated on May 21, 2008
	//documentation for this script at
	//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	 var added = false;
	 for (var S = 0; S < document.styleSheets.length; S++){
	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    if(document.styleSheets[S][cssRules][R].style[element]){
	    document.styleSheets[S][cssRules][R].style[element] = value;
	    added=true;
		break;
	    }
	   }
	  }

	  if(!added){
	  if(document.styleSheets[S].insertRule){
			  document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'; }',document.styleSheets[S][cssRules].length);
			} else if (document.styleSheets[S].addRule) {
				document.styleSheets[S].addRule(theClass,element+': '+value+';');
			}
	  }
	 }
}

