/*
 * The following JavaScript variables are set in PageBuilder page custom_pagewrapper_head, which is included in the tpt for every page wrapper:
 *
 * appId:              The numeric application ID rendered with the S4 tag.
 * secCatId:           The numeric security category ID rendered with the S12 tag.
 * centerId:           The numeric Center ID rendered with the S83 tag.
 * centerAbbr:         The security category abbreviation rendered with the E53 tag.
 * isLoggedIn:         true or false based on a conditional test of S1:user_name.
 * isPrinterFriendly:  Boolean based on a conditional test for the query string printer_friendly=1.
 * apiHost:            Secure or non-secure hostname based on document.location.protocol
 * apiKey:             Convio API key
 *
 * Please add notes on any changes made to this JavaScript file in sfdc 281817.
 */

if((appId=='81'||appId=='82')&&secCatId=='0'){
	//if scid is 0, redirect to scid 1041, the Realtor Action Center
	var currentUrl=window.location.href.replace('?scid=0&amp;','?').replace('?scid=0&','?').replace('?scid=0','');
	if(currentUrl.indexOf('?')==-1)
		window.location=currentUrl+'?scid=1041';
	else
		window.location=currentUrl.replace('&scid=0','').replace('&amp;scid=0','')+'&scid=1041';
}

addOnLoadHandler(function(){
	var i;
	var allLinks=document.getElementsByTagName('a');
	var allForms=document.getElementsByTagName('form');
	var allULs=document.getElementsByTagName('ul');
	var allLIs=document.getElementsByTagName('li');
	var allPs=document.getElementsByTagName('p');
	var allLabels=document.getElementsByTagName('label');
	for(i in allLinks){
		if(allLinks[i].href&&centerAbbr&&allLinks[i].href.indexOf('pagename=privacypolicy&center_abbr=')!=-1)
			//change links to the privacy policy to point to the page for the current Center
			allLinks[i].parentNode.innerHTML=allLinks[i].parentNode.innerHTML.replace(/pagename=privacypolicy/g,'pagename='+centerAbbr+'privacypolicy').replace(/&amp;printer_friendly=1/g,'');
	}
	for(i in allLinks){
		if(allLinks[i].href&&(allLinks[i].href.indexOf('UserLogin')!=-1||allLinks[i].href.indexOf('ConsProfileUser')!=-1||allLinks[i].href.indexOf('ConsInterestsUser')!=-1||allLinks[i].href.indexOf('/site/CO')!=-1||allLinks[i].href.indexOf('TellAFriend')!=-1||allLinks[i].href.indexOf('ServiceCenter')!=-1)&&allLinks[i].href.indexOf('scid=')==-1){
			//force scid on any link to UserLogin, ConsProfileUser, ConsInterestsUser, CO, ServiceCenter, and TellAFriend, to insure that the user stays in the correct Center
			if(!isLoggedIn&&allLinks[i].href.indexOf('UserLogin')==-1&&allLinks[i].href.indexOf('TellAFriend')==-1)
				allLinks[i].href='UserLogin?NEXTURL='+escape(allLinks[i].href)+'&scid='+secCatId;
			else{
				if(allLinks[i].href.indexOf('?')==-1)
					allLinks[i].href+='?scid='+secCatId;
				else
					allLinks[i].href+='&scid='+secCatId;
			}
			allLinks[i].href=allLinks[i].href.replace('scid=0','scid=1041');
		}
	}
	for(i in allForms){
		if(allForms[i].action&&(allForms[i].action.indexOf('UserLogin')!=-1||allForms[i].action.indexOf('ConsProfileUser')!=-1||allForms[i].action.indexOf('ConsInterestsUser')!=-1||allForms[i].action.indexOf('ServiceCenter')!=-1)){
			//force scid on UserLogin, ConsProfileUser, and ConsInterestsUser form actions
			if(secCatId!='0'){
				if(allForms[i].action.indexOf('?')==-1)
					allForms[i].action+='?scid='+secCatId;
				else
					allForms[i].action+='&scid='+secCatId;
				if(document.getElementById('NEXTURL')&&document.getElementById('NEXTURL').value.indexOf('ConsProfileUser')!=-1&&document.getElementById('NEXTURL').value.indexOf('scid=')==-1){
					if(document.getElementById('NEXTURL').value.indexOf('?')==-1)
						document.getElementById('NEXTURL').value=document.getElementById('NEXTURL').value+'?scid='+secCatId;
					else
						document.getElementById('NEXTURL').value=document.getElementById('NEXTURL').value+'&scid='+secCatId;
				}
			}
		}
	}
	if(!isPrinterFriendly&&(appId=='81'||appId=='82')){
		//add "elected officials" link in ConsProfileUser and ConsInterestsUser, and hide the Special Interests link
		for(i in allULs){
			if(allULs[i].className&&allULs[i].className.indexOf('cons-nav')!=-1)
				allULs[i].innerHTML+='<li><a href="PageServer?pagename='+centerAbbr+'electedOfficials">Elected Officials</a></li>';
		}
		for(i in allLIs){
			if(allLIs[i].innerHTML&&allLIs[i].innerHTML.indexOf('Special Interests')!=-1)
				allLIs[i].style.display='none';
		}
	}
	if(appId=='2'&&document.getElementById('rsvp_cancel')&&centerAbbr!=''){
		//when the user clicks on the cancel button, take them to the current Center's homepage instead of the site homepage
		document.getElementById('rsvp_cancel').parentNode.innerHTML=document.getElementById('rsvp_cancel').parentNode.innerHTML.replace('window.self.location=\'http://www2.realtoractioncenter.com/site/PageServer\'','window.self.location=\'http://www2.realtoractioncenter.com/site/PageServer?pagename='+centerAbbr+'homepage\'');
	}
	if(appId=='81'){
		//add text to encourage users not to change their username
		if(document.getElementById('user_name')){
			document.getElementById('user_name').parentNode.parentNode.getElementsByTagName('td')[2].innerHTML='<p class="Hint">We recommend that your user name be the same as your email address.</p>'; 
		}
		document.getElementById('EditCons').style.display='block';
	}
	if(appId=='82'){
		//change the text on ConsInterestsUser
		for(i in allPs){
			if(allPs[i].innerHTML&&allPs[i].innerHTML.indexOf('I would like to receive email about the following selected topics')!=-1)
				allPs[i].innerHTML='Please tell us what topics and email interests you have by checking/unchecking the boxes below. As your interests change, you can update your interests at any time.';
		}
		for(i in allLabels){
			if(allLabels[i].htmlFor&&(allLabels[i].htmlFor=='centerOptIn'||allLabels[i].htmlFor=='accepts_email')){
				allLabels[i].innerHTML=allLabels[i].innerHTML.replace('. ','.');
				var optInDisclaimer=document.createElement('span');
				optInDisclaimer.innerHTML='&nbsp;If you\'d like to unsubscribe from the '+allLabels[i].innerHTML.split('receive email from ')[1].split('.')[0]+' please uncheck this box.';
				allLabels[i].parentNode.insertBefore(optInDisclaimer,allLabels[i].nextSibling);
			}
		}
		document.getElementById('ConsConfigInterests').style.display='block';
	}
	if(!isPrinterFriendly&&(appId=='9'||appId=='1044')){
		//add Verisign logo on Donation2 forms
		document.getElementById('payment_typecc_exp_date_MONTH').parentNode.parentNode.innerHTML+='<div id="vs_seal" style="padding-right:0;padding-left:0;padding-bottom:6px;">'+
			'<a href="javascript:vrsn_splash();"><img alt="Click to Verify - This site has chosen a VeriSign SSL Certificate to improve Web site security" oncontextmenu="return false;" src="https://seal.verisign.com/getseal?at=0&amp;sealid=1&amp;dn=realtorparty.realtoractioncenter.com&amp;lang=en" name="seal" /></a><br />'+
			'<a style="font-size:8px;" target="_blank" id="about_ssl" href="http://www.verisign.com/ssl/ssl-information-center/">ABOUT&nbsp;SSL&nbsp;CERTIFICATES</a>'+
		'</div>';
	}
	getAlerts('active');
	getAlerts('archived');
	getEvents();
});
var sopener;
function vrsn_splash(){
	if(sopener&&!sopener.closed){
		sopener.focus();
	}
	else{
		var tbar="location=yes,status=yes,resizable=yes,scrollbars=yes,width=560,height=500";
		var sw=window.open('https://trustsealinfo.verisign.com/splash?form_file=fdf/splash.fdf&dn=realtorparty.realtoractioncenter.com&lang=en','VRSN_Splash',tbar);
		if(sw){
			sw.focus();
			sopener=sw;
		}
	}
}
function getAlerts(alertStatus){
	//get a list of action alerts
	YUI().use('io-base','json-parse','node',function(Y){
		if(Y.one('.centerActionAlerts-current')){
			function onComplete(id,o){
				var response=Y.JSON.parse(o.responseText);
				if(response.getAdvocacyAlertsResponse.alertCount!=0){
					var advocacyAlerts=response.getAdvocacyAlertsResponse.alert;
					if(alertStatus=='active'){
						var alertContainer=Y.all('.centerActionAlerts-current');
						var alertHeadline='Take Action';
					}
					else{
						var alertContainer=Y.all('.centerActionAlerts-past');
						var alertHeadline='Our Past Campaigns';
					}
					if(advocacyAlerts.length){
						//array
						alertContainer.set('innerHTML','<h3>'+alertHeadline+'</h3>'+
						'<ul></ul>');
						var i;
						for(i in advocacyAlerts){
							var advocacyAlert=Y.Node.create('<li><a href="Advocacy?id='+advocacyAlerts[i].alertId+'">'+advocacyAlerts[i].title+'</a></li>');
						if(alertStatus=='active'){
								Y.one('.centerActionAlerts-current ul').appendChild(advocacyAlert);
						} else {
								Y.one('.centerActionAlerts-past ul').appendChild(advocacyAlert);
							}
						}
					}
					else{
						//only 1 alert
						alertContainer.set('innerHTML','<h3>'+alertHeadline+'</h3>'+
						'<ul>'+
							'<li><a href="Advocacy?id='+advocacyAlerts.alertId+'">'+advocacyAlerts.title+'</a></li>'+
						'</ul>');
					}
				}
			};
			Y.on('io:complete',onComplete,Y);
			var request=Y.io(apiHost+'CRAdvocacyAPI?method=getAdvocacyAlerts&api_key='+apiKey+'&v=1.0&response_format=json&alert_status='+alertStatus+'&alert_type=action&list_category_id='+secCatId);
		}
	});
}
function getEvents(){
	//get a list of events
	YUI().use('io-base','json-parse','node',function(Y){
		if(Y.one('.centerEvents-current')){
			function onComplete(id,o){
				var response=o.responseText;
				var eventContainer=Y.all('.centerEvents-current');
				var eventHeadline='Upcoming Events';
				var tempEventList=Y.Node.create('<div id="centerEvents-temp" style="display:none;">'+response+'</div>');
				Y.one('body').appendChild(tempEventList);
				var i;
				var numEvents=0;
				var eventLinks=document.getElementById('eventListThisMonth').getElementsByTagName('a');
				for(i in eventLinks){
					if(eventLinks[i].href&&eventLinks[i].href.indexOf('view=Detail')!=-1){
						var today=new Date();
						if(eventLinks[i].parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('tr')[2].getElementsByTagName('td')[0].innerHTML.split(', ')[0].split(' ')[1]>=today.getDate()){
							//event is in the future
							numEvents++;
							if(numEvents==1)
								eventContainer.set('innerHTML','<h3>'+eventHeadline+'</h3>'+
								'<ul></ul>');
							var eventLink=Y.Node.create('<li><a href="'+eventLinks[i].href+'">'+eventLinks[i].parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('tr')[0].getElementsByTagName('td')[0].innerHTML.split('>')[1]+'</a></li>');
							Y.one('.centerEvents-current ul').appendChild(eventLink);
						}
					}
				}
				var eventLinks=document.getElementById('eventListFuture').getElementsByTagName('a');
				for(i in eventLinks){
					if(eventLinks[i].href&&eventLinks[i].href.indexOf('view=Detail')!=-1){
						var today=new Date();
						if(eventLinks[i].parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('tr')[2].getElementsByTagName('td')[0].innerHTML.split(', ')[0].split(' ')[1]>=today.getDate()){
							//event is in the future
							var eventLink=Y.Node.create('<li><a href="'+eventLinks[i].href+'">'+eventLinks[i].parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('tr')[0].getElementsByTagName('td')[0].innerHTML.split('>')[1]+'</a></li>');
							Y.one('.centerEvents-current ul').appendChild(eventLink);
						}
					}
				}
				Y.all('#centerEvents-temp').set('innerHTML','');
			};
			Y.on('io:complete',onComplete,Y);
			var request=Y.io('PageServer?pagename=upcomingEvents&pgwrap=n&scid='+secCatId);
		}
	}); 
}
