 





   var allcookies = document.cookie;
   var posaafpid  = allcookies.indexOf("aafpid");
   var posrmid  = allcookies.indexOf("RM");
   var aafpid     = '';
   var rmid     = '';
   var loggedin = false;
   
   var loginreferer = new String(window.location);
   //alert("loginreferer: " + loginreferer);
   
   var showlogindebugging = false;
   
   if(loginreferer.match("debugloginscript"+"$")){
   	showlogindebugging = true;
   }      
                                                                                                                             
   
   
   if (posaafpid != -1) {
       var start = posaafpid + 7;
       var end   = start + 7;
           aafpid    = allcookies.substring(start,end);
   }
   
   
   if (posrmid != -1) {
       var start = posrmid + 3;
       var end   = start + 7;
           rmid    = allcookies.substring(start,end);
   }
 
   var remembercheck = '';
   if (rmid) {
       remembercheck = 'CHECKED';
   }
   
   if (aafpid == '' || aafpid == '0000000') {
      if(rmid == ''){
      	rmid = 'ID Number';
      }
      
      ShowLogin();
   } else {
      NoLogin();
   }
   

function hideLoginElement(name) { 
    if (document.getElementById){
      document.getElementById(name).style.display = 'none'; 
    }
    else if (document.all) {
      document.all[name].style.display = 'none'; 
    }
} 

function showLoginElement(name) { 
    if (document.getElementById) 
      document.getElementById(name).style.display = 'inline'; 
    else if (document.all) 
      document.all[name].style.display = 'inline'; 
}  
function getFieldvalue(name) { 
    if (document.getElementById) 
      return document.getElementById(name).value; 
    else if (document.all) 
      return document.all[name].value; 
}    

function getImageUrl(argImagePath){
	var imagePath = new String(argImagePath);
	var referPage = new String(window.location);
	
	var referUrl = new String("http://www.aafp.org");
	if(referPage.indexOf("https://") > -1){
		referUrl = new String("https://secure.aafp.org");
	}	
	
	
	
	
	if(imagePath.indexOf("/") == 0){
		imagePath = referUrl + imagePath;
	}
	else if(imagePath.indexOf("http://") > -1 && referPage.indexOf("https://") == 0){
		imagePath = imagePath.replace(/http:/gi, "https:");
	}
	
	return imagePath;
}

function NoLogin () {
	var loginHeaderImageStr = getImageUrl('/online/etc/medialib/aafp_org/images/global_images/members-log-in.Par.0001.Image.gif');
	document.write('<p><img src="'+loginHeaderImageStr+'" alt="member log-in" id="loginHeaderImage" /><br />');
	document.write('<span id="loginarea"><a href="http://members.aafp.org/cgi-bin/logout.pl" class="loginlink">Log Out</a></span></p>');
	loggedin = true;
}

function ShowLogin () {
	showLoginAlert("1");
	var loginButtonImageStr = getImageUrl('/online/etc/medialib/aafp_org/images/global_images/arrows/redarrow.Par.0001.Image.gif');
	
	showLoginAlert(loginButtonImageStr);
	showLoginAlert("2");
	
	var loginHeaderImageStr = getImageUrl('/online/etc/medialib/aafp_org/images/global_images/members-log-in.Par.0001.Image.gif');
	
	showLoginAlert(loginHeaderImageStr);
	showLoginAlert("3");
	
	document.write('<form method="post" action="https://secure.aafp.org/login/" id="loginForm" name="loginForm1">');
	document.write('<input type="hidden" name="op" id="op" value="process_login" />');
	document.write('<input type="hidden" name="R" id="R" value="http://members.aafp.org/members" />');
	document.write('<input type="hidden" name="referer" value="https://nf.aafp.org/eweb/StartPage.aspx" />');
	document.write('<p><img src="'+loginHeaderImageStr+'" alt="member log-in" id="loginHeaderImage" /><br />');
	document.write('<span id="loginarea"><input type="text" name ="username" id="username" size="8" value="',rmid,'" class="inputtextgrey" onfocus="if(this.value == \'ID Number\'){this.value=\'\';this.className=\'inputtext\';}" onblur="if(this.value == \'\'){this.value = \'ID Number\';this.className=\'inputtextgrey\';}" TABINDEX="2"/>&nbsp;&nbsp;');
	document.write('<input type="text" name="passwordloginshow" id="passwordloginshow" size="20" value="Last Name/Password" class="inputtextgrey" onfocus="showLoginElement(\'passwordlogin\');hideLoginElement(\'passwordloginshow\');document.loginForm1.password.focus();" style="width:143px;" TABINDEX="3" /><input type="password" name="password" id="passwordlogin" size="20" class="inputtext" style="width:143px;" onfocus="" onblur="if(this.value == \'\'){hideLoginElement(\'passwordlogin\');showLoginElement(\'passwordloginshow\');}" TABINDEX="4"/>&nbsp;&nbsp;');
	document.write('<input tabindex="6" type="image" class="submit" src="'+loginButtonImageStr+'" alt="Log-in" onclick="javascript:document.loginForm1.submit();" id="loginButtonImage" /><br />');
	showLoginAlert("4");
	
	document.write('<input tabindex="5" type="checkbox" name="remember_me" id="remember_me" value="YES"',remembercheck,'  class="tinycheck" /><span class="smlogintext">');
	document.write('Remember Me &nbsp;&nbsp;&nbsp;');
	document.write('<a href="http://www.aafp.org/loginhelp.xml">Log-in Help</a></span></span></p>');
	document.write('</form>');
	showLoginAlert("5");

	if(rmid == 'ID Number'){
		hideLoginElement('passwordlogin');
	}
	else{
		hideLoginElement('passwordloginshow');
	}
	showLoginAlert("6");

}

function showLoginAlert(str){
	if(showlogindebugging){
		alert(str);
	}	
}


function LaunchPopup(url,width,height,toolbar,posX,posY) {
  if (!posX) {
    posX = 5;
  }
  if (!posY) {
    posY = 5;
  }
  var winName = "genPopup";
  var window_specifics = 'toolbar='+toolbar+',location='+toolbar+',directories='+toolbar+',status='+toolbar+','+
                         'menubar='+toolbar+',scrollbars=1,resizable=1,copyhistory=0,'+
                         'width='+width+',height='+height;

  var helpwin = open('', winName, window_specifics);
  helpwin.moveTo(posX,posY);
  helpwin.document.write('<html><head><title>Loading... Please Wait</title></head>' +
   '<body bgcolor=#FFFFFF><p><b>Loading... Please Wait</b></p>' +
   '</body></html>');
  helpwin.location=url;
  helpwin.opener = self;
  helpwin.opener.name = "genParent";
}

  function LaunchWin(url,width,height,toolbar) {
    window_specifics = 'toolbar='+toolbar+',location='+toolbar+',directories='+toolbar+',status='+toolbar+','+
                       'menubar='+toolbar+',scrollbars=1,resizable=1,copyhistory=0,'+
                       'width='+width+',height='+height;
    this.childWin = window.open(url,'childWin', +
    "" +window_specifics);
    this.childWin.opener = self;
  }

showLoginAlert("7");
// This is Javascript that will determine if the page is loaded or not
// ********************************************************************
if(/Safari/i.test(navigator.userAgent)){ //Test for Safari
  showLoginAlert("7.1");
  var _timer=setInterval(function(){
  if(/loaded|complete/.test(document.readyState)){
    clearInterval(_timer)
    doLoginInit() // call target function
  }}, 10)
  showLoginAlert("7.2");
}
else if (document.addEventListener){
  showLoginAlert("7.3");
  document.addEventListener("DOMContentLoaded", function(){doLoginInit();}, false);
  showLoginAlert("7.4");  
}
else if (document.all && !window.opera){
  showLoginAlert("7.5");  
  
  logintrythisimg = getImageUrl('/online/images/spacer.gif');
  
  showLoginAlert("logintrythisimg: " + logintrythisimg);
  
  document.write("<img id='contentloadtag' src='"+logintrythisimg+"' width='0' height='0' />");
  
  showLoginAlert("7.6");  
  var contentloadtag=document.getElementById("contentloadtag");
  showLoginAlert("7.7");  
  contentloadtag.onreadystatechange=function(){
    showLoginAlert("7.7.1"); 	
    if (this.readyState=="complete"){
    	showLoginAlert("7.7.2");
      doLoginInit();
    }
  }
  showLoginAlert("7.8");  
}
// ********************************************************************
showLoginAlert("8");

function doLoginInit() {
      
      showLoginAlert("aafpid: '" + aafpid + "'");  
      if (aafpid == '' || aafpid == '0000000') {
      	      try{
	      
	      	      showLoginAlert("9"); 
		      if(document.loginForm1.username.value != 'ID Number'){
		      	showLoginAlert("10"); 
		      	document.loginForm1.username.className='inputtext';
		      }
	      
	      	      showLoginAlert("11"); 
		      
      
		      /* function body goes here */      
		      
		      showLoginAlert("12"); 
		      
		      var pass = getFieldvalue('passwordlogin');
		      
		      showLoginAlert("13 - pass: '" + pass + "'"); 
		      
		      if(rmid == 'ID Number'){
		      	showLoginAlert("14"); 
			showLoginElement('passwordloginshow');	
			hideLoginElement('passwordlogin');
		      }
		      else if(pass != '' && pass != 'Last Name/Password'){
		      	showLoginAlert("15"); 
			hideLoginElement('passwordloginshow');	
			showLoginElement('passwordlogin');
		      }
		      else{
		      	showLoginAlert("16"); 
			showLoginElement('passwordloginshow');	
			hideLoginElement('passwordlogin');
		      }
	      }
	      catch(e){
	      }
      }
      else{
	showLoginAlert("No AAFPID");  
      }
      
      
} 

function loginAjax(url, vars, callbackFunction) {
 
 request = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
 request.open("GET", url, true);
 request.onreadystatechange = function() {
    
    if (request.readyState == 4 && request.status == 200) {
	    if (request.responseText) {
	       callbackFunction(request.responseText);
	    }
  }};
  request.send(vars);
}

function loginShowInstitution(json) {
  
  var customer;
 
  try{
  	customer = eval('(' + json + ')');
  }
  catch(eeee){
  }
	
  try{ 
	  name = "";
	  if(customer){
	 	if(customer.name && customer.name != ""){
	 		name = customer.name;
		}
	  }
	   
	  if(name != ""){
		var txt = '<span class="lglogintext">Institution: ' + name + '</span>';
		if(!loggedin){
			txt = '<span class="lglogintext" style="PADDING-RIGHT: 24px">Institution: ' + name + '</span>';
		}
		document.getElementById('loginarea').innerHTML = txt;
	  }
  
  }
  catch(eeee){
  }
  
}

function createUrl(argUrl){
	showLoginAlert("IN createUrl: " + argUrl);

	
	url = '/online/en/home/publications/news/news-now/archive/ehrsystems.proxy.html?p=HomepageLogin&a=InstitutionURL'; 
	
	hostname = window.location.hostname;
	if(hostname == 'www.aafp.org' || hostname == 'secure.aafp.org'){
		url = argUrl;
	}
	
	showLoginAlert("createUrl: " + url);
	return url
}


