var popWidth = 300;
var popHeight = 300;
var popResizable = true;
var popMinimizable = true;
var popMaximizable = true;

var win;

function killOverlay()
{
	// For some reason the overlay doesn't get removed when the parent window does
  if ($ID('overlay_modal')) {
	  $ID('overlay_modal').remove();
	}
	return true;
}

function popContent( sTitle, sParams, sWidth, sHeight, sResize, sMinimize, sMaximize )
{

  if ($ID('gte_popup_window')) {
		//win = $('gte_popup_window');
		win.showCenter(true);
	} else {
	
		win = new Window( 'gte_popup_window',
		{
			className: "gte",
			title: sTitle,
			width:( sWidth == null ) ? popWidth : sWidth,
			height:( sHeight == null ) ? popHeight : sHeight,
			resizable: ( sResize == null ) ? popResizable : sResize,
			minimizable: ( sMinimize == null ) ? popMinimizable : sMinimize,
			maximizable: ( sMaximize == null ) ? popMaximizable : sMaximize,
			showEffectOptions: {duration:0.5},
			hideEffectOptions: {duration:0.3}
		});
		win.setDestroyOnClose();
		win.showCenter(true);
		win.setHTMLContent('<div id="popup_content"></div>');
		win.setCloseCallback(killOverlay);
		
	}

  var myAjax = new Ajax.Updater(
		'popup_content',
		'gte.php',
		{
			method:'post',
			parameters: sParams,
			asynchronous:true,
			evalScripts:true
		}
  );
  return false;
}

function displayMessages( blockID )
{
	var container_id = blockID + '_messages_container';
   	new Effect.Appear( container_id  );
}

function saveHighlight( areaID )
{
	new Effect.Highlight( areaID, { startcolor:'#3a75b0', endcolor:'#ffffff', duration: 1 } );
}

function $FR(formName, elementName)
{
  return $F(Form.getInputs(formName,'radio',elementName).find(function(aRadio){return aRadio.checked;}))
}


function stopEvent(event)
{
	if(event&&event.stopPropagation){event.stopPropagation();}
	if(window.event){window.event.cancelBubble=true;}
}

function processTodo( t, p, caller )
{
	if ( $ID('task_grid_todo_'+t+'_label') != undefined )
	{
		$ID('task_grid_todo_'+t+'_label').style.display='none';
	}
	var ajaxTodo = new Ajax.Request( 'gte.php', {
		parameters: 'instantiate=negotiation_common&id=' + p + '&action=task_grid_todo&task_id=' + t + '&todo=' + ($('task_grid_todo_'+t).checked?'1':'')
	});
}

function processDone( t, p, isChecked )
{
	var ajaxDone = new Ajax.Request( 'negotiation.php', {
		parameters: 'instantiate=negotiation_common&id=' + p + '&action=task_grid_done&task_id=' + t + '&done=' + isChecked
	});
}

function CheckLoss(thisElement)
{
	if(thisElement == "loginPassword")
	{
		if($F(thisElement) == "")
		{
			$ID('loginPassword').style.display = 'none';
			$ID('tempPassword').style.display = '';
		}
	} else {
		if($F(thisElement) == "" || $F(thisElement) == null)
		{
			switch(thisElement)
			{
				case "loginUsername":
					$ID(thisElement).value = "Username";
					break;
				case "newsletterFirst" :
					$ID(thisElement).value = "First Name";
					break;
				case "newsletterLast" :
					$ID(thisElement).value = "Last Name";
					break;
				case "newsletterEmail" :
					$ID(thisElement).value = "Email Address";
					break;
			}
		}
	}
}

function CheckFocus(thisElement)
{
	if(thisElement == "tempPassword")
	{
		$ID('loginPassword').style.display = '';
		$ID('loginPassword').focus();
		$ID('tempPassword').style.display = 'none';
	} else {
		if($F(thisElement) == "Username" || $F(thisElement) == "Email Address" || $F(thisElement) == "First Name" || $F(thisElement) == "Last Name")
		{
			$ID(thisElement).value = "";
		}
	}
}






function SetMinMax()
{
	mm=$$('td.minmax');
	for(i=0;i<mm.length;++i)
	{
		mm[i].onmouseover = function()
			{
				Element.addClassName($ID(this),'hover');
			}
		mm[i].onmouseout = function()
			{
				Element.removeClassName($ID(this),'hover');
			}
		mm[i].onclick = function()
			{
				b = $ID(this).parentNode.parentNode.parentNode.parentNode;
				cn = document.getElementsByClassName('outer_content', b)[0];
				Effect.toggle(cn,'slide',{afterFinish:function(){setIcon(cn, b);}});
			}
	}

	mm=$$('td.solid-minmax');
	for(i=0;i<mm.length;++i)
	{
		mm[i].onmouseover = function()
			{
				Element.addClassName($ID(this),'hover');
			}
		mm[i].onmouseout = function()
			{
				Element.removeClassName($ID(this),'hover');
			}
		mm[i].onclick = function()
			{
				b = $ID(this).parentNode.parentNode.parentNode.parentNode;
				cn = document.getElementsByClassName('outer_content', b)[0];
				Effect.toggle(cn,'slide',{afterFinish:function(){setIcon(cn, b);}});
			}
	}
}

function myScrollTo (varScrollTo)
{
	if($ID('content_'+varScrollTo).style.display=='none')
	{
		cn = $('content_'+varScrollTo);
		b = cn.parentNode;
		Effect.toggle(cn,'slide',{afterFinish:function(){setIcon(cn, b);}});
	}
	new Effect.ScrollTo(varScrollTo);
}

function setIcon (divID, parentID)
{
	if(divID.style.display=='none')
		Element.addClassName(parentID, 'minimized');
	else
		Element.removeClassName(parentID, 'minimized');
}

function reverse( inp ) {
  var outp = 0

  for (i = 0; i <= inp.length; i++) {
    outp = inp.charAt (i) + outp
  }

	return outp;
}

function launchCommonQuestion( content_id )
{
	var params = 'instantiate=quick_list&action=ajax_question&title=Common%20Questions&tag=&name=questionscontent_id=' + content_id;

	popContent( "Tip", params, 400, 200, true, true, true );
}

function loadTip( content_id )
{
	var params = 'instantiate=quick_list&action=ajax_tip&name=tips&title=Tips%20and%20Tactics&tag=&content_id=' + content_id;
	
	popContent( "Tip", params, 400, 200, true, true, true );
}

function selectAllCheckboxes(form, prefix, check) {
	for (var c = 0; c < form.elements.length; c++) {
		input = form.elements[c];
		match = '^' + prefix;
    if (input.type == 'checkbox' && input.name.match(match)) {
      input.checked = check;
    }
	}
}

function toggleWorkArea(toggle, id) {
	Effect.toggle(id, 'Slide'); 
	
	if(toggle.className=='plus') {
		toggle.className='minus';
	} else { 
		toggle.className='plus'; 
	} 
	
	toggle.blur();
	return false;
}

function closeAjaxPanel(panel) {
	ancestors = $(panel).ancestors()
	for ( x in ancestors ) {
		node = ancestors[x];
		if ( node.id && node.id.match('container')) {
			Effect.SlideUp( node.id ); 
			break;
		}
	}
	
	return false;
}

function populateCounterpartsForPlan(plan_select_id, counterpart_select_id) {
	
	var plan_select = $ID(plan_select_id);
	
	var params = {};
	
	params['action'] = 'listCounterpartsForPlan';
	params['negotiation_plan_id'] = plan_select.options[plan_select.selectedIndex].value;
	
	new Ajax.Updater(
		counterpart_select_id,
		'overview.php',
		{
			method: 'post',
			parameters: params
		}
	);
	
}

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};


function termsWindow()
{
	window.open("http://expertnegotiator.com/terms.html");
}
