
//Google Analystics Event Tracking


function relatedMaterialClick() {
pageTracker._trackEvent('Related Materials', 'Link Clicked',$(this).html()+":"+$(this).attr('href'));
console.log('RM click');
}


function lNavClick() {
pageTracker._trackEvent('Navigation', 'Left Navigation Link Clicked',$(this).html());
console.log('lNav Fired');
}


function pNavClick() {
pageTracker._trackEvent('Navigation', 'Primary Navigation Link Clicked',$(this).attr('title'));
console.log('pNav Fired');
}


function flipClick() {
pageTracker._trackEvent('Callouts', 'Flipper Link Clicked',$(this).attr('href'));
console.log('Flipper Click Fired');
}


function calloutLinkMouseOver() {
pageTracker._trackEvent('Callouts', 'Link Mouse Over',$(this).attr('href'));
console.log('Callout Link Mouse Over Fired');
}

function calloutLinkMouseClick() {
pageTracker._trackEvent('Callouts', 'Link Mouse Click',$(this).attr('href'));

if($(this).find('h4').html()) {
pageTracker._trackEvent('Callouts', 'Link Mouse Click - By Callout Title', "H4 Title: "+ $(this).find('h4').html());
console.log('Callout Link Clicked by Callout Title Fired');
}
else if($(this).parent().find('h4').html()) {
pageTracker._trackEvent('Callouts', 'Link Mouse Click - By Callout Title', "H4 Title: "+ $(this).parent().find('h4').html());
console.log('Callout Link Clicked by Callout Title Fired');
}
else if($(this).parent().parent().find('h4').html()) {
pageTracker._trackEvent('Callouts', 'Link Mouse Click - By Callout Title', "H4 Title: "+ $(this).parent().parent().find('h4').html());
console.log('Callout Link Clicked by Callout Title Fired');
}
else if($(this).parent().parent().parent().find('h4').html()) {
pageTracker._trackEvent('Callouts', 'Link Mouse Click - By Callout Title', "H4 Title: "+ $(this).parent().parent().parent().find('h4').html());
console.log('Callout Link Clicked by Callout Title Fired');
}
else if($(this).parent().parent().parent().parent().find('h4').html()) {
pageTracker._trackEvent('Callouts', 'Link Mouse Click - By Callout Title', "H4 Title: "+ $(this).parent().parent().parent().parent().find('h4').html());
console.log('Callout Link Clicked by Callout Title Fired');
}




console.log('Callout Link Clicked Fired');
}

function calloutMouseOver() {
if($(this).find('h4').html()) {
pageTracker._trackEvent('Callouts', 'Mouse Over', "H4 Title: "+ $(this).find('h4').html());
console.log('Callout Mouse Over Fired');
}
}


// Preload images
member1=new Image();
member1.src= "/member/directory/images/button-saving.gif";



// Open all external links in a new window
function setupExternalLinks() {
	var links = document.getElementsByTagName('a');
	for (var i = links.length; i != 0; i--) {		
		var a = links[i-1];
		if (!a.href) continue;
		if (a.href.indexOf('http') != -1 && 
            a.href.indexOf('localhost') == -1 &&
            a.href.indexOf(window.location.hostname) == -1 &&
            a.href.indexOf('.cfr.org') == -1) {
            a.target = '_blank';
		}
	}
}


// show the survey screen
function show_survey() {

    var survey_page = readCookie("SURVEY_PAGE");
    if (survey_page) return;
    
    var pageviews = 0+readCookie("PAGEVIEWS");
    pageviews++;
    createCookie("PAGEVIEWS",pageviews);
    if (pageviews == 2) {
        window.location.href='/survey/?redirect=' + encodeURIComponent(window.location.href);
    }
}



/*
// rotate the quote
function rotate_quote() {
    // List of quote images
    var quotes = [
        ['/i/hdr-quote.gif',''],
        ['/i/hdr-quote1.gif',''],
        ['/i/hdr-quote2.gif',''],
        ['/i/hdr-quote3.gif',''],
        ['/i/hdr-quote4.gif',''],
        ['/i/hdr-quote5.gif',''],
        ['/i/hdr-quote6.gif',''],
        ['/i/hdr-quote7.gif',''],
        ['/i/hdr-quote8.gif',''],
        ['/i/hdr-quote9.gif',''],
        ['/i/hdr-quote10.gif',''],
        ['/i/hdr-quote11.gif',''],
        ['/i/hdr-quote12.gif','']
    ];

    var num = Math.floor(Math.random()*quotes.length);
    var quote = quotes[num];
    var img = document.images['quote'];
    if (img) {
        img.src = quote[0];
    }
}

*/

function rotate_sponsor() {
    // List of sponsor images
    var quotes = [
        ['/i/cfr-hdr_home_sponsor_ms.gif',''],
	['/i/cfr-hdr_home_sponsor_ms.gif','']
    ];

    var num = Math.floor(Math.random()*quotes.length);
    var quote = quotes[num];
    var img = document.images['quote'];
    if (img) {
	img.src = quote[0];
    }
}

// text size image change

function setAsize(idName) {
    if (document.images['small']) {
    	if (idName == 'Medium Text') {
    		document.images['small'].src="/i/xxsm_off.gif";
    		document.images['medium'].src="/i/xsm_on.gif";
    		document.images['large'].src="/i/sm_off.gif";
    	} else if (idName == 'Large Text') {
    		document.images['small'].src="/i/xxsm_off.gif";
    		document.images['medium'].src="/i/xsm_off.gif";
    		document.images['large'].src="/i/sm_on.gif";
    	} else {
    		document.images['small'].src="/i/xxsm_on.gif";
    	    document.images['medium'].src="/i/xsm_off.gif";
    		document.images['large'].src="/i/sm_off.gif";
        }
    }
}


// main navigation functions

{
	// allow for remote usage of nav images
	var cfr_url = window.location.protocol + 
			'//' + window.location.hostname + (
				window.location.port != 80 && window.location.port != 443 ?':'+window.location.port:''
			);
	if (window.location.hostname == 'blogs.cfr.org') {
		cfr_url = window.location.protocol + '//www.cfr.org';
	} else if (window.location.hostname == 'dev-blogs.cfr.org') {
		cfr_url = window.location.protocol + '//stage.cfr.org';
	} 

    region0=new Image();
    region0.src= cfr_url + "/i/nav-region-off.gif";
    region1=new Image();
    region1.src= cfr_url + "/i/nav-region-on.gif";
    region2=new Image();
    region2.src= cfr_url + "/i/nav-region-select.gif";
    issue0=new Image();
    issue0.src= cfr_url + "/i/nav-issue-off.gif";
    issue1=new Image();
    issue1.src= cfr_url + "/i/nav-issue-on.gif";
    issue2=new Image();
    issue2.src= cfr_url + "/i/nav-issue-select.gif";
    publication0=new Image();
    publication0.src= cfr_url + "/i/nav-publication-off.gif";
    publication1=new Image();
    publication1.src= cfr_url + "/i/nav-publication-on.gif";
    publication2=new Image();
    publication2.src= cfr_url + "/i/nav-publication-select.gif";
    thinktank0=new Image();
    thinktank0.src= cfr_url + "/i/nav-thinktank-off.gif";
    thinktank1=new Image();
    thinktank1.src= cfr_url + "/i/nav-thinktank-on.gif";
    thinktank2=new Image();
    thinktank2.src= cfr_url + "/i/nav-thinktank-select.gif";
    media0=new Image();
    media0.src= cfr_url + "/i/nav-media-off.gif";
    media1=new Image();
    media1.src= cfr_url + "/i/nav-media-on.gif";
    media2=new Image();
    media2.src= cfr_url + "/i/nav-media-select.gif";
    educators0=new Image();
    educators0.src= cfr_url + "/i/nav-educators-off.gif";
    educators1=new Image();
    educators1.src= cfr_url + "/i/nav-educators-on.gif";
    educators2=new Image();
    educators2.src= cfr_url + "/i/nav-educators-select.gif";
    about0=new Image();
    about0.src= cfr_url + "/i/nav-about-off.gif";
    about1=new Image();
    about1.src= cfr_url + "/i/nav-about-on.gif";
    about2=new Image();
    about2.src= cfr_url + "/i/nav-about-select.gif";
	support0=new Image();
    support0.src= cfr_url + "/i/nav-support-off.gif";
    support1=new Image();
    support1.src= cfr_url + "/i/nav-support-on.gif";
    support2=new Image();
    support2.src= cfr_url + "/i/nav-support-select.gif";
	
}

function navSelect(imgName,imgState) {
	if (document.images) {
		document.images[imgName].src = eval(imgState + '.src');
	}
}

function navOff(imgName,imgState) {
	if (document.images) {
		if (document.images[imgName].src != eval(imgName + '2.src')) {
			document.images[imgName].src = eval(imgState + '.src');
		}
	}
}

function navOn(imgName,imgState) {
	if (document.images) {
		if (document.images[imgName].src != eval(imgName + '2.src')) {
			document.images[imgName].src = eval(imgState + '.src');
		}
	}
}


// left navigation first level functions

function lnavaselect(navName,linkName) {
	if (document.getElementById) {
    	document.getElementById(navName).style.backgroundColor = "#FFF";
    	//document.getElementById(linkName).style.fontWeight = "bold";
    	//document.getElementById(linkName).style.textDecoration = "none";
	}
}

function lnavaon(navName,linkName) {
	if (document.getElementById) {
		if (document.getElementById(linkName).style.textDecoration != "none") {
    		document.getElementById(navName).style.backgroundColor = "#FFF"
    	}
	}
}

function lnavaoff(navName,linkName) {
	if (document.getElementById) {
		if (document.getElementById(linkName).style.textDecoration != "none") {
    		document.getElementById(navName).style.backgroundColor = "#F5F1E8"
    	}
	}
}


// left navigation second level functions

function lnavbselect(navName,linkName) {
	if (document.getElementById) {
    	document.getElementById(navName).style.backgroundColor = "#FFF";
    	//document.getElementById(linkName).style.fontWeight = "bold";
    	//document.getElementById(linkName).style.textDecoration = "none";
	}
}

function lnavbon(navName,linkName) {
	if (document.getElementById) {
		if (document.getElementById(linkName).style.textDecoration != "none") {
    		document.getElementById(navName).style.backgroundColor = "#FFF"
    	}
	}
}

function lnavboff(navName,linkName) {
	if (document.getElementById) {
		if (document.getElementById(linkName).style.textDecoration != "none") {
    		document.getElementById(navName).style.backgroundColor = "#F5F1E8"
    	}
	}
}

// map image change

{
	var mapoff = new Image();
	mapoff.src = "/i/map_off.gif";
	var africa = new Image();
	africa.src = "/i/map_africa.gif";
	var americas = new Image();
	americas.src = "/i/map_americas.gif";
	var asia = new Image();
	asia.src = "/i/map_asia.gif";
	var europe = new Image();
	europe.src = "/i/map_europe.gif";
	var mideast = new Image();
	mideast.src = "/i/map_mideast.gif";
}

function changeMap(imgDocID,imgObjName) {
	document.images[imgDocID].src = eval(imgObjName + ".src");
}


function emailCheck (emailStr) {
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);
if (matchArray==null) {
	alert("Email address seems incorrect (check @ and .'s)");
	return false;
}
var user=matchArray[1];
var domain=matchArray[2];
if (user.match(userPat)==null) {
    alert("The username doesn't seem to be valid.");
    return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!");
		return false;
	    }
    }
    return true;
}
var domainArray=domain.match(domainPat);
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.");
    return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if ((domArr[domArr.length-1] != "/info") &&
	(domArr[domArr.length-1] != "name") &&
	(domArr[domArr.length-1] != "arpa") &&
	(domArr[domArr.length-1] != "coop")) {
		if (domArr[domArr.length-1].length<2 || 
	    	domArr[domArr.length-1].length>3) {
		   		alert("The address must end in a three-letter domain, or two letter country.");
		   		return false;
		}
}
if (len<2) {
   var errStr="This address is missing a hostname!";
   alert(errStr);
   return false;
}
return true;
}
function UPTvalidateform(thisform) {
	if (emailCheck(thisform.email.value))
	{	
		
		return false;
	}
	else
	{
		return true;
	}
}

function presslist_signupSetState(){
	if(document.getElementById){
		country = document.getElementById('country');
		state   = document.getElementById('state');
		if(country.value != 'United States'){
			state.value = '';
		}
	}
}

flipper_cur = null;
function flipper_rotate(max) {
    setTimeout('_flipper_rotate('+max+')',4000);
}
function flipper_current(max) {
   var set = null;
    for (var i=0; i<max; i++) {
        elem=document.getElementById('flipper'+i);
        if (elem.className == 'highlight') {
            set = i;
        }
    }
   return set;
}
function flipper_set(set,max) {
    for (var i=0;i<max; i++) {
        elem=document.getElementById('flipper'+i);
        elem.className = (i==set?"highlight":"");
    }
    img = document.getElementById('flipper-image');
    img.src = document.getElementById('flipper-image' + set).src;
    url = document.getElementById('flipper-link' + set).href;
    image_url = document.getElementById('flipper-image-link');
    image_url.href = url;
}
function _flipper_rotate(max) {
    if (!document.getElementById) return;
    if (flipper_cur == null) {
    	set = flipper_current(max);
    	if (set == null) set = 0;
    	else set = (1+set)%max;
    	flipper_set(set,max);
    }
    flipper_rotate(max);

}
function flipper_over(set,max) {
    flipper_cur = flipper_current(max); 
    flipper_set(set,max);
}
function flipper_out(set,max) {
    flipper_set(flipper_cur,max);
    flipper_cur = null;
}



/// <name>insertMotionGraph</name>
/// <param name="spreadsheetID">takes the googledoc spreadsheet id found in the url after "key"</param>
/// <param name="topLeftCell">the starting cell of the selection for the motion graph</param>
/// <param name="bottomRightCell">the ending cell of the selection for the motion graph</param>
/// <param name="stateString">the state of the motion graph from the advanced tab in the graph</param>
function insertMotionGraph(spreadsheetID,topLeftCell,bottomRightCell,stateString) {
jQuery('#interactive').html('<img class="inline" src="/i/ajax-loader.gif"></img><p class="inline">Loading Google  Motion Chart</p>');
jQuery('#interactive').height(502);
google.load('visualization', '1', {packages: ['motionchart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {};
options['width'] = 860;
options['height'] = 502;
if (stateString!=''){
options['state'] = stateString;
 }
 new google.visualization.Query( 'http://spreadsheets.google.com/tq?key='+spreadsheetID+'&range='+topLeftCell+':'+bottomRightCell+'&pub=1').send(
function(response) {
        new google.visualization.MotionChart(
            document.getElementById('interactive')).
            draw(response.getDataTable(), options  );
      });
}

}
