var reset_area_http_request = null;
var lastarea = null;
var lastid = null;
var lastsrc = null;



function addLogoutButton(name)
{
	var token = getCookie("token");
	//if (token!=null && token.length > 0)
	if (name!=null && name.length > 0 && token!=null && token.length > 0)
	{
		var testB1 = document.getElementById('fcmslogoutbuttonholder')
		var testB2 = document.getElementById('fcmslogoutbutton')
		
		if (testB1!=null && testB2==null)
		{
			testB1.innerHTML = '<div id="fcmslogoutbutton"><div id="innerfcmslogoutbutton"><span style="font-weight:800">'+name+'</span>&nbsp;|&nbsp;<a href="/fcmsapplet/reviewer.html">My Documents</a>&nbsp;|&nbsp;<a href="#" onclick="deleteCookie(\'token\',\'/\');document.location.href=\'?logout=true\';">Sign Out</a></div></div>';
		}
	}
}

function closeWindow() {
	window.close();
}


function getElementsByClassName(fromElement, needle) {
   var my_array = fromElement.getElementsByTagName("*");
   var retvalue = new Array();
   var i;
   var j;

   for (i=0,j=0;i<my_array.length;i++) {
      var c = " " + my_array[i].className + " ";
      if (c.indexOf(" " + needle + " ") != -1) 
      {
      	
      	retvalue[j++] = my_array[i];
      	}
   }
   return retvalue;
} 

function openLocation(somelocation)
{
	popupWin = window.open(somelocation,"newloc");
	setTimeout('popupWin.focus()', 100);
}

function openFile(somefile)
{
	
	_wid="400";
	_hig="300";
	 _pos=getCenter(400,300);
	_sbr="no";
	_tbr="no";
	_lbr="no";
	_stt="no";
	_mbr="no";
	_rsz="no";	
	popupWin = window.open('',"newfileloc", "width=" + (_wid) + ",height=" + (_hig) + _pos + ",scrollbars=" + _sbr + ",toolbar=" + _tbr + ",location=" + _lbr + ",status=" + _stt + ",menubar=" + _mbr + ",resizable=" + _rsz);
	newDoc = popupWin.document;
	newDoc.write('<html><body style="margin:0px;padding:0px;"><div style="margin-top:100px;text-align:center;"><a href="'+somefile+'">DOWNLOAD THE FILE HERE</a><br><br><a href="#" onclick="window.close();">Close this window</a></div></body></html>');
	newDoc.close();
	setTimeout('popupWin.focus()', 100);
}


function approve(docareaid)
{
	if (confirm('Are you sure you want to approve this content?'))
	{
		_url="/fcmsapplet/action.html?id="+docareaid+"&action=approve"
		_ttl="popupactionw";
		_wid="400";
		_hig="300";
		 _pos=getCenter(400,300);
		_sbr="no";
		_tbr="no";
		_lbr="no";
		_stt="no";
		_mbr="no";
		_rsz="no";
		//+16w, + 16h
		popupWin = window.open(_url, _ttl, "width=" + (_wid) + ",height=" + (_hig) + _pos + ",scrollbars=" + _sbr + ",toolbar=" + _tbr + ",location=" + _lbr + ",status=" + _stt + ",menubar=" + _mbr + ",resizable=" + _rsz);
		setTimeout('popupWin.focus()', 100);
		return false;
	}
	
}



function login()
{
		_url="/fcmsapplet/action.html?action=login"
		_ttl="popuploginactionw";
		_wid="400";
		_hig="300";
		 _pos=getCenter(400,420);
		_sbr="no";
		_tbr="no";
		_lbr="no";
		_stt="no";
		_mbr="no";
		_rsz="no";
		//+16w, + 16h
		popupWin = window.open(_url, _ttl, "width=" + (_wid) + ",height=" + (_hig) + _pos + ",scrollbars=" + _sbr + ",toolbar=" + _tbr + ",location=" + _lbr + ",status=" + _stt + ",menubar=" + _mbr + ",resizable=" + _rsz);
		setTimeout('popupWin.focus()', 100);
		return false;
	
}

function loginSuccessful(newname)
{
	window.opener.window.location.reload();
	closeWindow();
}




function refreshPage()
{
	var d = new Date();
	var testV = window.opener.document.location.href;
	var qL = testV.indexOf("?");
	if (qL > -1)
	{
		testV = testV.substring(0,qL);
	}
	
	window.opener.document.location.href= testV + "?v=" + Date.parse(d);
}


function reject(docareaid)
{
	if (confirm('Are you sure you want to reject this content?'))
	{
		_url="/fcmsapplet/action.html?id="+docareaid+"&action=reject"
		_ttl="popupactionw";
		_wid="400";
		_hig="300";
		 _pos=getCenter(400,300);
		_sbr="no";
		_tbr="no";
		_lbr="no";
		_stt="no";
		_mbr="no";
		_rsz="no";
		//+16w, + 16h
		popupWin = window.open(_url, _ttl, "width=" + (_wid) + ",height=" + (_hig) + _pos + ",scrollbars=" + _sbr + ",toolbar=" + _tbr + ",location=" + _lbr + ",status=" + _stt + ",menubar=" + _mbr + ",resizable=" + _rsz);
		setTimeout('popupWin.focus()', 100);
		return false;
	}	
}


/*function showLiveArea(docid, areaid)
{
	//alert('testing here');
	
	var showingElement = document.getElementById('content' + docid + '-.-' + areaid);
	var liveElement = document.getElementById('livecontent' + docid + '-.-' + areaid);
	var contentHolder = document.getElementById('contentholder' + docid + '-.-' + areaid);
	if (showingElement!=null && liveElement!=null)
	{
		
		liveElement.style.width = showingElement.offsetWidth + 'px';
		//contentHolder.style.height = liveElement.offsetHeight + 17 + 'px';
		showingElement.style.visibility='hidden';
		liveElement.style.visibility='visible';
	}
	var liveB = document.getElementById('showliveb' + docid + '-.-' + areaid);
	liveB.onclick = null;
	liveB.style.backgroundColor='yellow';
	
	
	var draftB = document.getElementById('showdraftb' + docid + '-.-' + areaid);
	draftB.onclick = new Function("showDraftArea('"+docid+"','"+areaid+"');");
	draftB.style.backgroundColor='white';	
	
	
	var editB = document.getElementById('editb' + docid + '-.-' + areaid);
	if (editB!=null)
	{
		editB.style.visibility='hidden';
	}
	var appB = document.getElementById('approveb' + docid + '-.-' + areaid);
	if (appB!=null)
	{
		appB.style.visibility='hidden';
	}
	var rejectB = document.getElementById('rejectb' + docid + '-.-' + areaid);
	if (rejectB!=null)
	{
		rejectB.style.visibility='hidden';
	}
	
	
}

function showDraftArea(docid, areaid)
{
	var showingElement = document.getElementById('content' + docid + '-.-' + areaid);
	var liveElement = document.getElementById('livecontent' + docid + '-.-' + areaid);
	if (showingElement!=null && liveElement!=null)
	{
		liveElement.style.visibility='hidden';
		showingElement.style.visibility='visible';
	}
	
	var liveB = document.getElementById('showliveb' + docid + '-.-' + areaid);
	liveB.onclick = new Function("showLiveArea('"+docid+"','"+areaid+"');");
	liveB.style.backgroundColor='white';
	
	
	var draftB = document.getElementById('showdraftb' + docid + '-.-' + areaid);
	draftB.onclick = null;
	draftB.style.backgroundColor='yellow';	
	
	
	var editB = document.getElementById('editb' + docid + '-.-' + areaid);
	if (editB!=null)
	{
		editB.style.visibility='visible';
	}
	var appB = document.getElementById('approveb' + docid + '-.-' + areaid);
	if (appB!=null)
	{
		appB.style.visibility='visible';
	}
	var rejectB = document.getElementById('rejectb' + docid + '-.-' + areaid);
	if (rejectB!=null)
	{
		rejectB.style.visibility='visible';
	}	
	
}*/


function resetArea(id,area,src)
{
	//alert('reseting area for ' + docareaid);
	reset_area_http_request = setupXMLHTTPRequest();
	lastarea = area;
	lastid = id;
	lastsrc = src;
	reset_area_http_request.onreadystatechange = loadArea;
	reset_area_http_request.open('GET', '/fcms-area-content?id=' + id + '-.-' + area, true);
	reset_area_http_request.send(null);	
	
}

function resetDraft(id,area,src,workflowid)
{
	//alert('reseting area for ' + docareaid);

	
	
	reset_area_http_request = setupXMLHTTPRequest();
	lastarea = area;
	lastid = id;
	lastsrc = src;
	reset_area_http_request.onreadystatechange = loadDraftArea;
	reset_area_http_request.open('GET', '/fcms-workflowarea-content?id=' + workflowid, true);
	reset_area_http_request.send(null);	
	
	
	
}



function loadArea()
{
	
	if (reset_area_http_request.readyState == 4) 
	{
	    // everything is good, the response is received
	} else 
	{
		//alert('not ready!:'+theRequest.readyState+', returning!');
	    return;
	}
	
	if (reset_area_http_request.status == 200) 
	{	
		//alert('got a good response!' + reset_area_http_request.responseText);
		
		var resp = reset_area_http_request.responseText;
		
		var contentStart = resp.indexOf('<result>');
		var contentEnd = resp.indexOf('</result>');
		if (contentStart > -1 && contentEnd > -1)
		{
			//alert('resetting area ' + 'content' + lastid + '-.-' + lastarea);
			var areaE = window.opener.document.getElementById('content' + lastid + '-.-' + lastarea);
			var innerS = resp.substring(contentStart+8,contentEnd);
			areaE.innerHTML = innerS.replace(/&apos;/g,"'");
			
		}
		else
		{
			alert('could not load area:\n bad markup');
		}
		
	}
	else
	{
		
		alert('could not load area:\n bad response from the server');
	}	
	
	
}

function loadDraftArea()
{
	
	if (reset_area_http_request.readyState == 4) 
	{
	    // everything is good, the response is received
	} else 
	{
		//alert('not ready!:'+theRequest.readyState+', returning!');
	    return;
	}
	
	if (reset_area_http_request.status == 200) 
	{	
		//alert('got a good response!' + reset_area_http_request.responseText);
		
		var resp = reset_area_http_request.responseText;
		
		var contentStart = resp.indexOf('<result>');
		var contentEnd = resp.indexOf('</result>');
		if (contentStart > -1 && contentEnd > -1)
		{
			var areaE = window.opener.document.getElementById('content' + lastid + '-.-' + lastarea);
			var innerS = resp.substring(contentStart+8,contentEnd);
			areaE.innerHTML = innerS.replace(/&apos;/g,"'");
			
			var docareaid = lastid + '-.-' + lastarea;
			var editButtons = window.opener.document.getElementById('editbuttons' + docareaid);
			var approveB = window.opener.document.getElementById('approveb'+docareaid);
			if (approveB==null)
			{
				
				var newEditB = '<a href="javascript:void(0);"><img alt="Edit Content" id="editbuttonimage'+docareaid+'" onclick="popupEditWindow(\''+lastid+'\',\''+lastarea+'\',\''+lastsrc+'\');" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;" class="toeditimage" src="/fcms-image/edit.gif"/></a>';
				var newHTML = newEditB + '<a href="javascript:void(0);"><img alt="Approve Content" id="approveb'+docareaid+'" onclick="approve(\''+docareaid+'\');" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;" src="/fcms-image/thumbup_small.gif"/></a><a href="javascript:void(0);"><img alt="Reject Content" id="rejectb'+docareaid+'" onclick="reject(\''+docareaid+'\');" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;" src="/fcms-image/thumbdown_small.gif"/></a>';
				editButtons.innerHTML = newHTML;
				var liveDraftButtons = window.opener.document.getElementById('livedraftbuttons' + docareaid);
				liveDraftButtons.innerHTML = '<a href="?viewlive='+docareaid+'#showliveb'+docareaid+'"><img alt="Show Live Content" id="showliveb'+docareaid+'" style="border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;" src="/fcms-image/live.gif"/></a><img alt="Draft Content" id="showdraftb'+docareaid+'" style="background-color:yellow;border-left:0px;border-top:0px;border-right:0px;border-bottom:0px;" src="/fcms-image/draft.gif"/>';	
			}
			
			
		}
		else
		{
			alert('could not load area:\n bad markup');
		}
		
	}
	else
	{
		
		alert('could not load area:\n bad response from the server');
	}	
	
	
}


function setupXMLHTTPRequest()
{
	var requestObj = null;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
	    requestObj = new XMLHttpRequest();
		if (requestObj.overrideMimeType) {
	                requestObj.overrideMimeType('text/xml');
            }	    
	} else if (window.ActiveXObject) { // IE
            try {
                requestObj = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    requestObj = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
	}
	
        if (!requestObj) {
            alert('Cannot create an XMLHTTP instance');
            return null;
        }	
	return requestObj
	
}


function resetToken(newToken)
{
	setCookie("token",newToken,30,"/");
	var token = getCookie("token");
	addLogoutButton();
}

// Modified from Bill Dortch's Cookie Functions (hidaho.com) 
// (found in JavaScript Bible)
function setCookie(name,value,days,path,domain,secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

// Modified from Jesse Chisholm or Scott Andrew Lepera ?
// (found at both www.dansteinman.com/dynapi/ and www.scottandrew.com/junkyard/js/)
function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

// from Bill Dortch's Cookie Functions (hidaho.com) 
function deleteCookie(name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}


function getQstringParam(paramName) {

//Retrieve Document location and tear off the QueryString values for processing.
var toReturn = "";
var url = document.location + '';
q=url.split('?');
if (q[1]) {
//Get all Name/Value pairs from the QueryString
var pairs = q[1].split('&');
for (i=0;i<pairs.length;i++) {

//Get the Name from given Name/Value pair
var keyval = pairs[i].split('=');

if (keyval[0] == paramName) {
//Get the Value from given Name/Value pair and set to the return ID
toReturn = keyval[1];
}

}
}

return toReturn;
}


function getCenter(boxWidth,boxHeight) {
	x = boxWidth; y = 50;
	if (window.screen) {
		if(screen.availWidth) {
			x = screen.availWidth / 2 - boxWidth/2;
			y = screen.availHeight / 2 - boxHeight/2;
		} else if (screen && screen.width) {
			x = screen.width / 2 - boxWidth/2;
			y = screen.height / 2 - boxHeight/2;
		}
	}
	if (x < 0) { x = 10; }
	if (y < 0) { y = 10; }
	return ',screenX='+x+ ',left='+x+ ',screenY='+y+ ',top='+y;
}

function popupEditWindow(id, area, src, _url, _ttl, _wid, _hig, _lft, _top, _sbr, _tbr, _lbr, _stt, _mbr, _rsz) {
	if(!_url) _url="/fcmsapplet/author.html?id="+id+"&area=" + area + "&src=" + src;
	//alert('url is ' + _url);
	if(!_ttl) _ttl="popupw";
	if(!_wid) _wid="800";
	if(!_hig) _hig="600";
	if(!_lft) _pos=getCenter(800,600);
	else _pos=",screenX=" + _lft + ",left=" + _lft + ",screenY" + _top + ",top=" + _top;
	if(!_sbr) _sbr="no";
	if(!_tbr) _tbr="no";
	if(!_lbr) _lbr="no";
	if(!_stt) _stt="no";
	if(!_mbr) _mbr="no";
	if(!_rsz) _rsz="no";
	//+16w, + 16h
	popupWin = window.open(_url, _ttl, "width=" + (_wid) + ",height=" + (_hig) + _pos + ",scrollbars=" + _sbr + ",toolbar=" + _tbr + ",location=" + _lbr + ",status=" + _stt + ",menubar=" + _mbr + ",resizable=" + _rsz);
	//newDoc = popupWin.document;
	//newDoc.write('<html><body style="margin:0px;padding:0px;"><img src="'+_url+'"/></body></html>');
	//newDoc.close();
	//popupWin = window.open(_url, _ttl, "width=656,height=499" + _pos + ",scrollbars=" + _sbr + ",toolbar=" + _tbr + ",location=" + _lbr + ",status=" + _stt + ",menubar=" + _mbr + ",resizable=" + _rsz);
	setTimeout('popupWin.focus()', 100);
	return false;
}