var newWindow = null;

function closeWin(){
  if (newWindow != null){
    if(!newWindow.closed)
      newWindow.close();
  }
}

function popUpWin(url, type, strWidth, strHeight) {

  /*
   * type = [ standard | fullscreen | console ]
   */

  closeWin();

  if (type == "fullScreen"){
    strWidth = screen.availWidth - 10;
    strHeight = screen.availHeight - 160;
  }

  var tools="";
  if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
  if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left=0,top=0";
  newWindow = window.open(url, 'newWin', tools);
  newWindow.focus();
}

function changeDivContent(target, id, objstr) {
	Effect.BlindUp(target);
	setTimeout("updateDivContent('" + target + "', '" + id + "', '" + objstr + "')",1500);
}

function updateDivContent(target, id, objstr) {
	var obj = objstr.evalJSON();
	
	var container = document.getElementById(target);
	var links     = document.getElementById(target.toString() + "_links").getElementsByTagName('a');
	var sellink   = target.toString() + '_' + id;

	// id == 'cpa' ? partnerDate = '2004' : partnerDate = '2009';    /* REMOVE */
	/*
	for(var i = 0; i < links.length; i++) {
		links[i].id == sellink ? links[i].className = 'selected' : links[i].className = '';
	}
	*/
	
	/* Fix these items to point to the proper data for the selected item */
	/* Replace this whole block with actual text content for the selected item */
	if(target == 'profile') {
		container.getElementsByTagName('h1')[0].innerHTML = obj.name.toUpperCase();
		var paras = container.getElementsByTagName('p');
		
		content = '<h1>' + obj.name + '</h1>';
		content = content + '<p><span class="h2runin">' + obj.challengetitle + ": </span>" + obj.challenge + '</p>';
		content = content + '<p><span class="h2runin">' + obj.solutiontitle + ": </span>" + obj.solution + '</p>';
		content = content + '<p><span class="h2runin">' + obj.resulttitle + ": </span>" + obj.result + '</p>';
		
		container.innerHTML = content;
	} else if(target == 'employee') {
		var imgtag = document.getElementById('page_photo');
		imgtag.src = obj.photo;
		container.getElementsByTagName('h1')[0].innerHTML      = formatName(obj);
		container.getElementsByTagName('h2')[0].innerHTML      = obj.jobtitle;
		container.getElementsByClassName('since')[0].innerHTML = obj.description;
		document.getElementById('description').innerHTML       = obj.bio;
		// imgtag.setAttribute('alt') = formatName(obj);
		
		// Highlights selected name in right-hand list
		var rows = document.getElementById(target.toString() + "_links").getElementsByTagName('tr');
		for(var i = 0; i < rows.length; i++) {
			rows[i].className = '';
		}
		document.getElementById(sellink).parentNode.parentNode.className = 'selected';
	}

	Effect.BlindDown(target);
	return true;
}

function getBioContent(target, id) {
	var ajaxlink  = '../pages/ajax_get_bio_details.php';
	
	new Ajax.Request(ajaxlink, {
		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		parameters: {'id': id},
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			var jsonstr  = '(' + response + ')';

			changeDivContent(target, id, jsonstr);
		},
		onFailure: function(){ alert('An error occurred. Please try again.') }
	});
	
}

function getProfileContent(target, id) {
	var ajaxlink  = '../pages/ajax_get_profile_details.php';
	
	new Ajax.Request(ajaxlink, {
		method: 'get',
		requestHeaders: {Accept: 'application/json'},
		parameters: {'id': id},
		onSuccess: function(transport){
			var response = transport.responseText || "no response text";
			var jsonstr  = '(' + response + ')';

			changeDivContent(target, id, jsonstr);
		},
		onFailure: function(){ alert('An error occurred. Please try again.') }
	});
	
}

function swapReportImage(linkid, target, image) {
	document.getElementById(target).src = image;
	// document.getElementById(link).className = 'selected';
	var obj   = document.getElementById(linkid).parentNode.parentNode;
	var links = obj.getElementsByTagName('a');
	for(var i = 0; i < links.length; i++) {
		links[i].id == linkid ? links[i].className = 'selected' : links[i].className = '';
	}
}

function formatName(obj) {
	var namestr = '';
	namestr = obj.firstname;
	obj.middlename ? namestr += ' ' + obj.middlename : null;
	namestr += ' ' + obj.lastname;
	obj.suffix ? namestr += ', ' + obj.suffix : null;
		
	return namestr;
}

// My Portfolio Processing/Validation
function validateLogin() {
	var frm = document.getElementById('loginform');
	
	if(trim(frm.user_id.value) != '') {
		var btn = document.getElementById("btnSignin");
		btn.disabled = true;
		document.body.style.cursor = "wait";
		document.forms[0].user_id.focus();	
		return true;
	} else {
		alert('Error: A User ID is required.\n\nPlease enter a User ID.');
		document.forms[0].user_id.focus();
		return false;
	}
}

// My 401(k) Processing/Validation
function processSponsor() {
	var pageurl='https://www.retirementaccountlogin.net/tci/';
	
	document.loginform.LoginButton.value = 'true';
	document.loginform.action = pageurl + 'sponsor.aspx';
	document.loginform.submit();
}

// My 401(k) Processing/Validation
function processParticipant() {
	var pageurl='https://www.retirementaccountlogin.net/tci/';
	
	document.loginform.LoginButton.value = 'true';
	document.loginform.action = pageurl + 'default.aspx';
	document.loginform.submit();
}

// Utility function
function trim(str) { 
	str.replace(/^\s*/, '').replace(/\s*$/, ''); 
	return str;
}


