//================== Shawno_24-Oct-07: Prevent <Enter> from submitting form =================//
var nav = window.Event ? true : false;
if (nav) {
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
   document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) {
  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false; }
  return true;
}

function MicrosoftEventHandler_KeyDown() {
  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')
    return false;
  return true;
}
//=============== end of Shawno_24-Oct-07: Prevent <Enter> from submitting form ==============//

function getQueryString(variable) {
  var query = window.location.search.substring(1);
  query = query.toLowerCase();
  variable = variable.toLowerCase();
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
}
function setMktPath(){
  var expires = new Date();
  expires.setTime (expires.getTime() + 1000 * 60 * 60 * 24 * 90); //set expiry date 90 days in the future
  var expiryDate = expires.toGMTString();
  var expiresNow = new Date("Jan 01, 1970 00:00:01 GMT");
  var path = "/";  
  var mpath = getQueryString("mpath");
  var masso = getPageInfo('asso');
  if(!masso) masso = getPageInfo('assoS');
  if (mpath){
        document.cookie='%mpath=' + mpath + '%; path=' + path + '; expires=' + expiryDate; 
      	document.cookie='%mkt= ; path=' + path + '; expires=' + expiresNow;
		document.cookie='masso=' + masso + '; path=' + path + '; expires=' + expiryDate; 
  } else {
     //-- SP(30-Mar-06): Add further check for "mkt=", in the event the user followed an e-mail campaign 
     //-- link that does not use a redirection url.
     var mkt = getQueryString("mkt");
     if (mkt){
        document.cookie='%mkt=' + mkt + '%; path=' + path + '; expires=' + expiryDate; 
		document.cookie='%mpath= ; path=' + path + '; expires=' + expiresNow; 
		document.cookie='masso=' + masso + '; path=' + path + '; expires=' + expiryDate; 
     } 
  }
  
}
setMktPath();
function getPageInfo(op){
	try { 
		var rtn	
		switch (op) {
		case "asso":
			rtn = associationName;
			break;
		case "assoS":
			rtn = associationNameS;
			break;
		case "lang":
			rtn = lang;
			break;
		case "pageId":
			rtn = pageId;
			break;
		case "pageType":
			rtn = pagetype;
			break;
		case "prdType":
			rtn = prdType;
			break;
		case "prdID":
			rtn = prdID;
			break;
		case "sid":
			rtn = sid;
			break;
		case "browser":
			var appName = navigator.appName;
			if (appName.indexOf('Microsoft')>-1) {
				rtn = 'IE';
			} else if (appName.indexOf('Netscape')>-1) {
				rtn = 'Mozilla';
			} else {
				rtn = appName;
			}
			break;
		case "all":
			rtn = getPageInfo('browser')+";"+getPageInfo('pageId')+";"
			break;
		default:
			rtn = eval(op);
			break;
		}
		return rtn;	
	} catch (e) { return '' }
}
Global = {
	logError:function(err){
		try {document.forms[0].wb_jsE.value = document.forms[0].wb_jsE.value 
			+ "#"+ err + "@"+getPageInfo('all'); } catch(e){}
	},
	markDone:function(){
		try {document.forms[0].wb_jsD.value ="@"+getPageInfo('sid'); } catch(e){}
	}	
}
function cLang(langCode){ //--- change lang
	var EWDB = 'affinity.nsf';
//1. get current page info : path, pagename, type and langCode
	var curURL = window.location.href;
	
	//-FIX FOR '&MPATH='.  If this parameters is contained in the query string, then remove it
	//-prior to flipping the language...
	var pos_mpath = curURL.toLowerCase().indexOf("&mpath=");
	if (pos_mpath != -1){
		// Extract everything BEFORE and AFTER the mpath part....
		var mpath = curURL.substring(pos_mpath + 1, curURL.length);
		var mpathBegin = curURL.substring(0, pos_mpath);

		var mpathEnd = "";
		var pos_mpathEnd = mpath.indexOf("&");
		
		if (pos_mpathEnd != -1){
			mpathEnd = mpath.substring(pos_mpathEnd, mpath.length);
		}
		
		curURL = mpathBegin + mpathEnd
	}
	//-END OF FIX FOR &MPATH=
	
	var p
	p = curURL.lastIndexOf('/');
	var curPath = curURL.substring(0, p);
	var curType = (curPath.toLowerCase().indexOf(EWDB)>0)? 'EW':'Form';
	var curPageReq = curURL.substring(p+1, curURL.length);
	p = curPageReq.indexOf('?');
	var curPage = (p>=0)? curPageReq.substring(0, p):curPageReq;
	var curReq = (p>=0)? curPageReq.substring(p+1, curPageReq.length): (curType=='EW')?'':'Open';
	var curLang, formLangP;
	var curAnchor='';
	switch (curType) {
	case 'EW':
		p = curPage.indexOf('#');
		if (p>=0) {
			curAnchor = curPage.substring(p, curPage.length);
			curPage = curPage.substring(0,p);
		}
		curLang = curPage.substring(0, 2)
		curLang = (curLang.toLowerCase() == 'f_')? 'F':'E';
		break;
	case 'Form':
		formLangP = curReq.toLowerCase().indexOf('&lang=');
		if (formLangP>=0) {
			curLang = curReq.substring(formLangP+6, formLangP+7);
			curLang = ( (curLang != 'E')&& (curLang !='F') )?'E':curLang;
		} else {			
			curLang = document.forms[0].lang.value;
			if (curLang=="" | curLang==null){
				curLang = lang;
			}
		}
		break;
	}
//2. change page lang
	if (!langCode) langCode = (curLang=='F')?'E':'F';
	if (langCode == curLang) return 0; //no change
	
	switch (curType) {
	case 'EW':
		curPage = (langCode=='F')? 'f_' + curPage:curPage.substring(2, curPage.length);
		
		//----New stuff for assocation name flip: Start ----
		// Attemp to get as code from query string first, otherwise use default assoCode....
		var qAsso = getQueryString("as");
		if (qAsso==null | qAsso==""){
			curAsso = associationName;
		} else {
		   curAsso = qAsso;
		}
		
		if (langCode=="E"){
		   if (curAsso.substring(0,2)=="f_"){
		      curAsso = curAsso.substring(2,curAsso.length);
		   }
		} else {
		   if (curAsso.substring(0,2) != "f_"){
		      curAsso = "f_" + curAsso
		   }
		}
		
		curReq = "open&as=" + curAsso;
		//----New stuff for assocation name flip: End ----
		
		break;
	case 'Form':

		//-- New method for handling language flip while in applications (CI, TL, GI).  If on the first 
		//-- page of the application process, then allow to flip back n' forth between languages.  However,
		//-- once the application process has been started (QueryString contains 'ParentUNID'), then reroute
		//-- back to site's homepage and flip language.
		curAsso = associationName;
		curPage = curAsso;
	
		if (langCode=="E"){


		   if (curAsso.substring(0,2)=="f_"){
		      curAsso = curAsso.substring(2,curAsso.length);
		      curPage = curAsso;
		   }
		} else {
		   if (curAsso.substring(0,2) != "f_"){
		      curAsso = "f_" + curAsso;
		      curPage = curAsso;
		   }
		}
		
		//==Start of DSP12-Jul-06: New coding for lanugage flip in applications....
		var flipType = ((curURL.toLowerCase().indexOf("parentunid")>0) | (curURL.toLowerCase().indexOf("/apply/")>0))? 'home':'flip';
        if (flipType=='flip'){
        	//-- First page of any automated form -- it's OK to flip language here.  Remove the as= from 
        	//-- the query string.  
        	var pos = curURL.toLowerCase().indexOf("&as=");
        	if (pos != -1){
        		var qsBegin = curURL.substring(0, pos);
        		var qsAsso = curURL.substring(pos+1, curURL.length);
        		var qsEnd = ""
        		pos = qsAsso.indexOf("&");
        		if (pos != -1){
        			qsEnd = qsAsso.substring(pos, qsAsso.length);
        		}
        	}
        	qsNew = qsBegin + qsEnd;

        	//-- Now, extract the path, page & parms into seperate variables...
			curPath = qsNew;
        	var pos = qsNew.toLowerCase().indexOf(".nsf");
			if (pos != -1){
        		curPath = qsNew.substring(0, pos+4);
			}

        	var posEnd = qsNew.toLowerCase().indexOf("?");
        	var curReq = "";
        	if (posEnd != -1){
        		curReq = qsNew.substring(posEnd+1, qsNew.length);
        		//-- Remove the '#' from the end of the qs...
        		if (curReq.substring(curReq.length-1, curReq.length) == "#"){
        			curReq = curReq.substring(0, curReq.length-1);
        		}

				curPage = qsNew.substring(pos+5,posEnd);
        	}
        	
			curReq = curReq + "&as=" + curAsso;        	
        } else {
            //--- already within application process - reroute back to home page of site...
            curPage = cLang_getHomePage(curAsso);
			curPath = "/Can/affinity/affinity.nsf/Public";
			curReq = "open&as=" + curAsso;
        }
		//==end of DSP12-Jul-06: New coding for lanugage flip in applications....
		
		//break;
	}
//	alert(curPath + '\n' + curPage + '\n'+ curReq + '\n'+ curType + '\n'+ curLang);
//	alert( "New page URL:" + curPath+'/'+curPage+ ((curReq=='')? '': ('?'+curReq)) + curAnchor );
	window.location = curPath+'/'+curPage+ ((curReq=='')? '': ('?'+curReq)) + curAnchor;
}

//--DSP05-01-2007: Additional function for cLang() function to validate 'irregular' home pages.  Homepages for old sites, 
//  such as CAA, have an underscore at the end of the page name (i.e. /Public/caa_) instead of it being just the same as
//  the assocation code.  The following function will determine if the site has an irregular homepage name and will fix
//  accordingly.
function cLang_getHomePage(curAsso){
	var curHomepage = curAsso;
	
	//- Check for CAA 2.0 club....
	if (curAsso.substring(0,3)=="caa"){
		curHomepage = "caa_";
		return curHomepage;
	}
	if (curAsso.substring(0,5)=="f_caa"){
		curHomepage = "f_caa_";
		return curHomepage;
	}
	return curHomepage;	
}
//-- dhtml functions
function toggle(id) {
	el = document.getElementById(id);
	if (el != null) {
		var display = el.style.display ? '' : 'none';
		el.style.display = display;
	}
}
function isHidden(id) {
	el = document.getElementById(id);
	bIsHidden = (el.style.display == 'none')? true:false;
	return bIsHidden;	
}
function hideTxt(id) {
	el = document.getElementById(id);
	if (el != null)	{ el.style.display = "none"; }
}
function hideTxt2(id) {
	el = document.getElementById(id);
	if (el != null)	{ el.style.visibility = "hidden"; }
}
function showTxt(id) {
	el = document.getElementById(id);
	if (el != null) el.style.display = '';
}
function showTxt2(id) {
	el = document.getElementById(id);
	if (el != null)	{ el.style.visibility = "visible"; }
}
function autoHideShowTxt(id){
    el = document.getElementById(id);
    sDisplayMode = el.style.display;
    if (sDisplayMode==""){
        hideTxt(id);
    } else {
        showTxt(id);
    }
}
function changeObjText(id, message) {
	var evalTxt;
	if (document.layers) { // browser is NN		 
		 evalTxt = "msgObj = window.document."+id;
	}
	else { //assume IE 
		 evalTxt = "msgObj = document.getElementById('"+id+"')";
	}	
	eval(evalTxt);
	if (msgObj) {
		msgObj.innerHTML = message;
	}	
}
function get_IndexFromSelect(objSelect, valueToFind){
   var objValue;
   var chkValue = "ppamcto";
   
   for (var i = 1; i < objSelect.length; i++){
      objValue = objSelect[i].value;
      if (objValue == valueToFind){
         return i;
      }
   }
   
   return 0;
}
function submitonce(theform) {//must used after submit
	if (document.all||document.getElementById){
		for (i=0; i<theform.length; i++){
			var tempobj = theform.elements[i];
			var objType = tempobj.type;
			if(objType!=null && objType!="") objType = objType.toLowerCase();
			switch(objType) {
			case "button":
			case "submit":
			case "reset":
			case "input":
			case "select":			
			case "text":
			case "radio":
			case "select-one":
				tempobj.disabled = true;				
			}
		}
	}
}
//--- open functions ---//
function op(pageurl){
	as = associationName;
	if (pageurl.toLowerCase().indexOf('?open')<0) pageurl = pageurl+"?Open";
	var rtn = pageurl+"&as="+as;
	window.location = rtn;
}
function op_NewWin(pageurl, winWidth, winHeight, winName){
	as = associationName;
	if (pageurl.toLowerCase().indexOf('?open')<0) pageurl = pageurl+"?Open";
	var rtn = pageurl+"&as="+as;
        openWindow( rtn, winWidth, winHeight, winName);
}
function openWindow( url, winWidth, winHeight, winName, openFullWindow) {
	var winTop = (screen.height / 2) - (winHeight / 2);
	var winLeft = (screen.width / 2) - (winWidth / 2);
	var winNM = winName? winName:"win";
	if ( openFullWindow && openFullWindow !="") {
		winFeatures = "";
	} else {
		winFeatures ="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,left=" + winLeft + ",top=" + winTop + ",width=" + winWidth + ",height=" + winHeight;
	}       
	var thisWin = window.open(url, winNM, winFeatures);
	thisWin.focus();
}
function openCrippledWindow( url, winWidth, winHeight, winName) {
       // This type of window has no features -- no scroll bars, no menus, not resizeable, etc -- untouchable.
       var winTop = (screen.height / 2) - (winHeight / 2);
       var winLeft = (screen.width / 2) - (winWidth / 2);
       if (winName==null | winName==""){
           var winNM = "win";
       } else {
           var winNM = winName;
       }
       winFeatures ="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,left=" + winLeft + ",top=" + winTop + ",width=" + winWidth + ",height=" + winHeight;
       win = window.open(url, winNM, winFeatures);
       win.focus();
}

function pageRedirect(pageurl){
    	// Opens the new URL and carries over the marketing code with it.  This is mainly used for sites with Omniture tagging
    	// activated, and is called from an old, obsolete landing page.  Once redirected, the marketing code is passed to the 
    	// new URL, thus allowing Omniture to automatically pick it up and assign it to the user's session.
    
	mkt = getQueryString('mkt');
	if (mkt==null | mkt==""){
		mkt = "";
	} else {
		mkt =  "&mkt=" + mkt;
	}
    
	as = associationName;
	if (pageurl.toLowerCase().indexOf('?open')<0) pageurl = pageurl+"?Open";
	var rtn = pageurl+"&as="+ as + mkt;

	window.location = rtn;
}

String.prototype.trim = function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}


