// JavaScript Document

//This function is used for highlighting navigation to save on template numbers
function highlight(nav) {
    if (document.getElementById(nav) != null){ 
        document.getElementById(nav).className = 'selected'; //selected
    }
}

function highlightTab(tab,nav) {
document.getElementById(tab).className = 'overnav'; //selected
document.getElementById(nav).className = 'selected'; //selected
}

function checkSignInCookie(memberPage) {
	if (document.cookie.indexOf("qazqsa29") > 0) {
		window.location.href  = memberPage;
	}
}

function getUsrGlbl() {
	var allCookies = document.cookie.toString();
	if ( allCookies.indexOf("10.1.7.115") > -1) {
		//alert (allCookies);
	}

	if(allCookies.indexOf("nebsuc") > -1) {
		var startAt			= allCookies.indexOf("nebsuc")+7; 
		var remainingPart 	= allCookies.substring(startAt);

		var usrGlbl = remainingPart.indexOf(";") >-1 	? remainingPart.substring(0,remainingPart.indexOf(";")) 
														: remainingPart ;
		if ( allCookies.indexOf("10.1.7.117") > -1) {
			//alert(decode(usrGlbl)); 
		}
		return decode(usrGlbl); 
		
	}
}

function dsplLgn() {
	var usrGlbl = getUsrGlbl();
	if (usrGlbl != null){

		var usrNm 	= usrGlbl.substring(0,usrGlbl.indexOf("~"));

		if ( usrNm != null && usrNm != '') { 

		
			if ( location.href.indexOf("group-registration")==-1 && (usrGlbl.indexOf("ORGANIZATION") > -1 || usrGlbl.indexOf("SUSTAINING") > -1 || usrGlbl.indexOf("SCHOOL") > -1 || usrGlbl.indexOf("DISTRICT")	> -1) ) {

				// W A R N I N G :  D O    N O T   A C T I V A T E   O N   P R O D U C T I O N 

				//location.href = '/members/group-registration.html';
			}

			/////  HEADER 
			var header ;
			if (location.href.indexOf(".hq.") > -1) {
				header= 'Logged In as ' + usrNm   + ' | <a href="http://www.hq.asq.org/log-out.html">Log Out</a> | <a href="http://www.hq.asq.org/store/shopping_cart.html">View Shopping Cart</a><br /><a href="http://www.hq.asq.org/pub/qualityprogress/">Quality Progress Magazine</a> ' ;
			}
			else {
				header= 'Logged In as ' + usrNm  + ' | <a href="http://www.asq.org/log-out.html">Log Out</a> | <a href="http://www.asq.org/store/shopping_cart.html">View Shopping Cart</a><br /><a href="http://www.asq.org/pub/qualityprogress/">Quality Progress Magazine</a> ' ;
			}


			/////  MEMBERSHIP RENEWAL  
			if ( location.href.indexOf("/members/") > -1) {

				if ( isGroup(usrGlbl) || isParticipant(usrGlbl) ) {
					if (document.getElementById('quality_img') != null) {
					document.getElementById('quality_img').alt 		= 'We are quality' ;
					document.getElementById('improvm_img').alt 		= 'We Can Inspire Improvement' ;
					document.getElementById('quality_img').src 		= '/img/templates/home/we-are-quality.gif' ;
					document.getElementById('improvm_img').src 		= '/img/templates/home/myasq-blurb-participant.jpg' ;
					document.getElementById('manage_URL').href 		= '/members/participant/manage-account.html' ;
					document.getElementById('callout3_img').alt 	= 'Join ASQ' ;
					document.getElementById('callout3_img').src 	= '/img/templates/join-asq-today.gif' ;
					document.getElementById('callout3_url').href	= '/members/participant/join-asq.html' ;
					document.getElementById('MySectionsBlurb').style.display 	= 'block' ;
					document.getElementById('MyNetworksBlurb').style.display 	= 'block' ;
					document.getElementById('MyForumsBlurb').style.display		= 'block' ;
					document.getElementById('MyBoardsBlurb').style.display 		= 'block' ;
					}
					
				}

				var message = getRenewMessage(usrGlbl) ; 
				if (message.length > 0) {
					header += ( ' | ' + message ) ; 
				}
				/////  BENEFITS LINK 
				if (document.getElementById('benefits_URL') != null) {
					//document.getElementById('quality_img').src = '/img/templates/home/we-are-quality.gif' ;
					if 		( usrGlbl.indexOf("ORG")>-1 || isParticipant(usrGlbl) ) {		
						document.getElementById('benefits_URL').href = '/members/participant/benefits-organizational.html' ;
					}
					else if 	( usrGlbl.indexOf("SUSTAINING")>-1 ) {		
						document.getElementById('benefits_URL').href = '/members/participant/benefits-sustaining.html' ;
					}
					else if 	( usrGlbl.indexOf("SCHOOL")>-1 ||  usrGlbl.indexOf("DISTRICT")>-1 ) {		
						document.getElementById('benefits_URL').href = '/members/participant/benefits-educational.html' ;
					}
					else if 	( usrGlbl.indexOf("NON-MEMBER")>-1 ) {		
						////document.getElementById('benefits_URL').href = '' ;
					}
				}

			}
			else if ( location.href.indexOf("/pub/") > -1) {
				//header +=  getPublMessage(usrGlbl) ;
			}
			
			if ( location.href.indexOf("/membership/") > -1) {
				if ( isGroup(usrGlbl) || isParticipant(usrGlbl) ) {
					// keep as is
				}
				else {
					if (document.getElementById('membershipTabs') != null) {
						document.getElementById('membershipTabs').innerHTML= '<table border="0" cellspacing="0" cellpadding="0"><tr><td><a href="/membership/members/your-benefits.html" id="ym">Your Membership<br />Benefits and Features</a></td></td><td><a href="/membership/members/did-you-know.html" id="dyk"><br />Did You Know...?</a></td><td><a href="/membership/members/renew.html" id="renew"><br />Renew</a></td><td><a href="/membership/members/get-involved.html" id="gi"><br />Get Involved</a></td><td><a href="/membership/members/upgrade.html" id="uym">Upgrade Your<br />Membership</a></td></tr></table>';
						document.getElementById('callouts').innerHTML= 		 '<table border="0" width="620" cellspacing="0" cellpadding="0"><tr><td width="160" align="left" valign="top" class="bluecell"><img src="/img/templates/membership/persfull-photo.jpg" alt="Jenny Persfull" width="160" height="190" /></td><td width="310" align="left" valign="top" class="whiterightborder" id="teasercell"><img src="/img/templates/membership/persfull-blurb.gif" alt="Quality is about people" width="310" height="190" /></td><td width="150" align="left" valign="top" class="bluecell"><a href="/members/account/manage-account.html"><img src="/img/templates/manage-your-membership-2.gif" alt="Manage your membership" width="150" height="64" border="0" /></a><a href="/keepintouch.html"><img src="/img/templates/sign-up-asq-weekly.gif" alt="Sign up for the ASQ Weekly e-newsletter" width="150" height="64" border="0" /></a><a href="/java_members/portlets/execute/referMemberSetup"><img src="/img/templates/refer-a-member.gif" alt="Refer a member" width="150" height="62" border="0" /></a></td></tr></table>';
					}
				}
			}

			document.getElementById('loginarea').innerHTML = header ; 


		}
		
	}
}

function getRenewMessage(usrGlbl) {
	var mths = new Array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var message = '';
	if (usrGlbl.indexOf("~RN") > -1 ) {
		var ren = usrGlbl.substring(usrGlbl.indexOf("~RN")+1,usrGlbl.indexOf("~RN")+14);

		if ( ren != null && ren.length > 0) {
					
			var arrayRnDt = (ren.substring(3)).split("/");
			if (arrayRnDt[0].substring(0,1) == '0') arrayRnDt[0] = arrayRnDt[0].substring(1);
			var formattedDate = mths[parseInt(arrayRnDt[0])] + ' ' + arrayRnDt[1] + ', ' + arrayRnDt[2] ;

			if ( ren.indexOf("RNL") > -1) {
				  message = 'Your membership expires ' + formattedDate + '. <a href="https://secure.asq.org/ecommerce/renewals/execute/loginSetup"><b>Renew Now!</b></a>' ;
			}
			else if ( ren.indexOf("RND") > -1 ) {
				  message = 'Membership Renewed ' + formattedDate + '. Thank you.';
			}
		}
	}
	if ( message.indexOf("undefined") > -1) { 
		message = '' ;
	}
	return message;
}


function getPublMessage(usrGlbl) {
	var publDirName = location.href.substring(location.href.indexOf("/pub/")+5,location.href.lastIndexOf("/") ) ; 
	var usrBnefName;
	if (publDirName == 'qualityprogress'){
		publDirname = 'QP';
	}
	else if (publDirName == 'sixsigma'){
		publDirname = 'SSF';
	}
	return '<br>' + publDirName;
}

var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
function decode(encStr) {
  var bits, decOut = '', i = 0;
  for(; i<encStr.length; i += 4){
    bits =
     (base64s.indexOf(encStr.charAt(i))    & 0xff) <<18 |
     (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | 
     (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
      base64s.indexOf(encStr.charAt(i +3)) & 0xff;
    decOut += String.fromCharCode(
     (bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
    }
  if(encStr.charCodeAt(i -2) == 61)
    undecOut=decOut.substring(0, decOut.length -2);
  else if(encStr.charCodeAt(i -1) == 61)
    undecOut=decOut.substring(0, decOut.length -1);
  else undecOut=decOut;
  
  return unescape(undecOut);		//line add for chinese char
}

var gAutoPrint = true; // Flag for whether or not to automatically call the print function
function printFriendly()
{
	if (document.getElementById != null)
	{
		var html = '<HTML>\n<HEAD><link rel=\"stylesheet\" href=\"/includes/global.css\" type=\"text/css\"><link rel=\"stylesheet\" href=\"/includes/audience.css\" type=\"text/css\">\n';

		html += '\n</HEAD>\n<BODY style=\"font: 11px/13px verdana, arial, sans-serif\"><table border=0 cellspacing=0 cellpadding=0 align=right><tr><td width=69 valign=top><img src=\"/img/templates/shared/asq_logo_s.gif\" border=0></td></tr></table>\n';
		
		var printPageElem = document.getElementById("printFriendly");
		
		if (printPageElem != null)
		{
				html += printPageElem.innerHTML;
		}
		else
		{
			alert("Could not find the printFriendly section in the HTML");
			return;
		}
			
		html += '\n<table width=95% border=0 cellspacing=0 cellpadding=5 align=center><tr><td><hr>Copyright ©2006 American Society for Quality. All rights reserved.<br>Republication or redistribution of ASQ content is expressly prohibited without the prior written consent.<br>ASQ shall not be liable for any errors in the content, or for any actions taken in reliance thereon.</td></tr></table></BODY>\n</HTML>';
		
		var printWin = window.open("","printFriendly");
		printWin.document.open();
		printWin.document.write(html);
		printWin.document.close();
		if (gAutoPrint)
			printWin.print();
	}
	else
	{
		alert("Sorry, the printer friendly feature works\nonly in javascript enabled browsers.");
	}
}

function openNewWindow(theURL,winName,features) {
    			window.open(theURL,winName,features);
}

function isGroup(usr){
	if 	( 	usr.indexOf("ORG") 			> -1	|| 
			usr.indexOf("SCHOOL") 		> -1 	|| 
			usr.indexOf("SUSTAINING") 	> -1	||
			usr.indexOf("DISTRICT") 	> -1 ) {
		return true;
	}
}

function isParticipant(usr) {
	if 	( usr.indexOf("PARTICIPANT") >-1 ) {
		return true;
	}
}

