/*  
**  $URL: http://intranet.filife.corp/svn/filife/symfony/tags/9.0.10.2/web/js/moderation-popup.js $
**  $Id: moderation-popup.js 15837 2009-06-29 14:57:45Z ezra $ 
*/

/*
** Functions & Globals for the Moderation Interface
*/

var selectedRow;

function CheckLoggedIn(requestType){
  $.get("/auth/isloggedin",{},
    function(data){
      if (data == "true") {  //logged in
        // Nothing left to do
      } else {  //not logged in
        openLoginOverlay();
      }
    }
  );
  return false ;
}

function HighlightErrorForm(formField){
  formField.className = 'form_field_error' ;
}

/* Reviews
****************************************************/
function openModRev (i, type, page, messageId, userId, assetId, assettypeId) {
	document.getElementById("modopts").className="mod_admin_contain";
	
	// Grab the table tr that corresponds with the passed index
	var theRow = document.getElementById( "mod_results_table" ).getElementsByTagName("tr")[i] ;
	var messageText  = theRow.getElementsByTagName("td")[2].getElementsByTagName("span")[0].innerHTML ;
	selectedRow = theRow;

	selectedRow.style.color='#ff0000';
	
	// Populate the edit overlay
	var theOverlay = document.getElementById( "f_mod_edit" ) ;
	
	// Pass the 
	//  Discussion Headline, Discussion Message text, 
	//  Discussion Message ID, Message type, and the page number
	theOverlay.messageText.value = messageText ; 
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;
	
	// Populate the delete confirm
	var theOverlay = document.getElementById( "f_mod_delete" ) ;
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;
	
	showEditRev() ;
}

function closeModRev() {
	document.getElementById("modopts").className="mod_admin_contain hideit";
	
	// Revert tabs
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("delete_btn").className="delete_btn";
	
	// Revert layers
	document.getElementById("mod_edit_pop").className="hideit";
	
	selectedRow.style.color='#333';
}

function showDeleteRev() {
	document.getElementById("deleteconfirm").className="mod_admin_contain";
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("delete_btn").className="delete_btn_active";
}

function answerNoRev() {
	document.getElementById("deleteconfirm").className="mod_admin_contain hideit";
	document.getElementById("edit_btn").className="edit_btn_active";
	document.getElementById("delete_btn").className="delete_btn";
	
	document.getElementById("mod_edit_pop").className="";
}

function showEditRev() {
	// Update tabs
	document.getElementById("edit_btn").className="edit_btn_active";
	document.getElementById("delete_btn").className="delete_btn";

	// Update layers
	document.getElementById("mod_edit_pop").className="";
}


/* Discussions
****************************************************/
function openModDisc (i, type, page, messageId, userId, assetId, assettypeId, producttypeId) {
  if (typeof(producttypeId) == 'undefined') {
    producttypeId = null;
  }

	document.getElementById("modopts").className="mod_admin_contain";
	
	// Grab the table tr that corresponds with the passed index
	var theRow = document.getElementById( "mod_results_table" ).getElementsByTagName("tr")[i] ;
	var headline = theRow.getElementsByTagName("td")[2].getElementsByTagName("span")[0].innerHTML ;
	var messageText  = theRow.getElementsByTagName("td")[3].getElementsByTagName("span")[0].innerHTML ;
	selectedRow = theRow;

	selectedRow.style.color='#ff0000';
	
	// Populate the edit overlay - Pass the Discussion Headline, Discussion Message text, 
  //   Discussion Message ID, Message type, and the page number
	var theOverlay = document.getElementById( "f_mod_edit" ) ;
	theOverlay.headline.value = headline ;
	theOverlay.messageText.value = messageText ; 
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;
//  alert(producttypeId);
  if (type == 'A') {
    document.getElementById("prodTypeIdSpace").className="hideit";
  } else if (type == 'Q') {
    document.getElementById("prodTypeIdSpace").className="pop_field";
  }
  
  $("option[value=" + producttypeId + "]", "select#producttypeId").attr("selected","selected"); 
//  $('#theOverlay.producttypeId option[value=producttypeId]').attr("selected", "selected");

	// Populate the answer overlay
	var theOverlay = document.getElementById( "f_mod_answer" ) ;
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;
		
	document.getElementById("mod_answer_pop").getElementsByTagName("h3")[0].innerHTML = headline ;
	document.getElementById("mod_answer_pop").getElementsByTagName("p")[0].innerHTML = messageText ;
	
	// Populate the delete confirm
	var theOverlay = document.getElementById( "f_mod_delete" ) ;
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;
	
	showEditDisc() ;
}

function closeModDisc() {
	document.getElementById("modopts").className="mod_admin_contain hideit";
	
	// Revert tabs
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("answer_btn").className="answer_btn";
	document.getElementById("delete_btn").className="delete_btn";
	
	// Revert layers
	document.getElementById("mod_edit_pop").className="hideit";
	document.getElementById("mod_answer_pop").className="hideit";
	
	selectedRow.style.color='#333';
}

function showDeleteDisc() {
	document.getElementById("deleteconfirm").className="mod_admin_contain";
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("answer_btn").className="answer_btn";
	document.getElementById("delete_btn").className="delete_btn_active";
}

function answerNoDisc() {
	document.getElementById("deleteconfirm").className="mod_admin_contain hideit";
	document.getElementById("edit_btn").className="edit_btn_active";
	document.getElementById("answer_btn").className="answer_btn";
	document.getElementById("delete_btn").className="delete_btn";
	
	document.getElementById("mod_edit_pop").className="";
	document.getElementById("mod_answer_pop").className="hideit";
}

function showEditDisc() {
	// Update tabs
	document.getElementById("edit_btn").className="edit_btn_active";
	document.getElementById("answer_btn").className="answer_btn";
	document.getElementById("delete_btn").className="delete_btn";

	// Update layers
	document.getElementById("mod_edit_pop").className="";
	document.getElementById("mod_answer_pop").className="hideit";
}

function showAnswerDisc() {
	// Update tabs
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("answer_btn").className="answer_btn_active";
	document.getElementById("delete_btn").className="delete_btn";

	// Update layers
	document.getElementById("mod_edit_pop").className="hideit";
	document.getElementById("mod_answer_pop").className="";
}

/* Descriptions
****************************************************/
function openModDesc (
i, type, page, messageId, userId, assetId, assettypeId, orderBy, sortDirection, md_search, md_user, dateBegin_mm, dateBegin_dd, dateBegin_yyyy, dateEnd_mm, dateEnd_dd, dateEnd_yyyy) {

	document.getElementById("modopts").className="mod_admin_contain";
	
	// Grab the table tr that corresponds with the passed index
	var theRow = document.getElementById( "mod_results_table" ).getElementsByTagName("tr")[i] ;
	var messageText  = theRow.getElementsByTagName("td")[3].getElementsByTagName("span")[0].innerHTML ;
	selectedRow = theRow;

	selectedRow.style.color='#ff0000';

	// Populate the edit overlay
	var theOverlay = document.getElementById( "f_mod_edit" ) ;
	
	
	
	// Pass the 
	//  Discussion Headline, Discussion Message text, 
	//  Discussion Message ID, Message type, and the page number
	theOverlay.messageText.value = messageText ; 
	theOverlay.type.value = type;
	theOverlay.page.value = page;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;	
	
	theOverlay.orderBy.value = orderBy ;
	theOverlay.sortDirection.value = sortDirection ;	
	theOverlay.md_search.value = md_search ;	
	theOverlay.md_user.value = md_user ;	
	theOverlay.dateBegin_mm.value = dateBegin_mm ;	
	theOverlay.dateBegin_dd.value = dateBegin_dd ;	
	theOverlay.dateBegin_yyyy.value = dateBegin_yyyy ;	
	theOverlay.dateEnd_mm.value = dateEnd_mm ;	
	theOverlay.dateEnd_dd.value = dateEnd_dd ;	
	theOverlay.dateEnd_yyyy.value = dateEnd_yyyy ;	

	
	// Populate the delete confirm
	var theOverlay = document.getElementById( "f_mod_delete" ) ;
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;

	theOverlay.orderBy.value = orderBy ;
	theOverlay.sortDirection.value = sortDirection ;	
	theOverlay.md_search.value = md_search ;	
	theOverlay.md_user.value = md_user ;	
	theOverlay.dateBegin_mm.value = dateBegin_mm ;	
	theOverlay.dateBegin_dd.value = dateBegin_dd ;	
	theOverlay.dateBegin_yyyy.value = dateBegin_yyyy ;	
	theOverlay.dateEnd_mm.value = dateEnd_mm ;	
	theOverlay.dateEnd_dd.value = dateEnd_dd ;	
	theOverlay.dateEnd_yyyy.value = dateEnd_yyyy ;		


	
	// Populate the approve confirm
	var theOverlay = document.getElementById( "f_mod_approve" ) ;
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;
	
	theOverlay.orderBy.value = orderBy ;
	theOverlay.sortDirection.value = sortDirection ;	
	theOverlay.md_search.value = md_search ;	
	theOverlay.md_user.value = md_user ;	
	theOverlay.dateBegin_mm.value = dateBegin_mm ;	
	theOverlay.dateBegin_dd.value = dateBegin_dd ;	
	theOverlay.dateBegin_yyyy.value = dateBegin_yyyy ;	
	theOverlay.dateEnd_mm.value = dateEnd_mm ;	
	theOverlay.dateEnd_dd.value = dateEnd_dd ;	
	theOverlay.dateEnd_yyyy.value = dateEnd_yyyy ;		

	
	// Populate the deny confirm
	var theOverlay = document.getElementById( "f_mod_deny" ) ;
	theOverlay.type.value = type;
	theOverlay.page.value = page ;
	theOverlay.messageId.value = messageId ;
	theOverlay.userId.value = userId ;
	theOverlay.assetId.value = assetId ;
	theOverlay.assettypeId.value = assettypeId ;

	theOverlay.orderBy.value = orderBy ;
	theOverlay.sortDirection.value = sortDirection ;	
	theOverlay.md_search.value = md_search ;	
	theOverlay.md_user.value = md_user ;	
	theOverlay.dateBegin_mm.value = dateBegin_mm ;	
	theOverlay.dateBegin_dd.value = dateBegin_dd ;	
	theOverlay.dateBegin_yyyy.value = dateBegin_yyyy ;	
	theOverlay.dateEnd_mm.value = dateEnd_mm ;	
	theOverlay.dateEnd_dd.value = dateEnd_dd ;	
	theOverlay.dateEnd_yyyy.value = dateEnd_yyyy ;		
	
	
	showEditDesc();
}


function closeModDesc() {
	document.getElementById("modopts").className="mod_admin_contain hideit";
	
	// Revert tabs
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("delete_btn").className="delete_btn";
	document.getElementById("approve_btn").className="approve_btn";
	document.getElementById("deny_btn").className="deny_btn";
	
	// Revert layers
	document.getElementById("mod_edit_pop").className="hideit";
	
	selectedRow.style.color='#333';
}

function showDeleteDesc() {
	document.getElementById("deleteconfirm").className="mod_admin_contain";
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("delete_btn").className="delete_btn_active";
	document.getElementById("approve_btn").className="approve_btn";
	document.getElementById("deny_btn").className="deny_btn";
}

function showApproveDesc() {
	document.getElementById("approveconfirm").className="mod_admin_contain";
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("delete_btn").className="delete_btn";
	document.getElementById("approve_btn").className="approve_btn_active";
	document.getElementById("deny_btn").className="deny_btn";
}

function showDenyDesc() {
	document.getElementById("denyconfirm").className="mod_admin_contain";
	document.getElementById("edit_btn").className="edit_btn";
	document.getElementById("delete_btn").className="delete_btn";
	document.getElementById("approve_btn").className="approve_btn";
	document.getElementById("deny_btn").className="deny_btn_active";
}

function answerNoDesc() {
	document.getElementById("deleteconfirm").className="mod_admin_contain hideit";
	document.getElementById("denyconfirm").className="mod_admin_contain hideit";
	document.getElementById("approveconfirm").className="mod_admin_contain hideit";
	document.getElementById("edit_btn").className="edit_btn_active";
	document.getElementById("delete_btn").className="delete_btn";
	document.getElementById("approve_btn").className="approve_btn";
	document.getElementById("deny_btn").className="deny_btn";
	
	document.getElementById("mod_edit_pop").className="";
}

function showEditDesc() {
	// Update tabs
	document.getElementById("edit_btn").className="edit_btn_active";
	document.getElementById("delete_btn").className="delete_btn";
	document.getElementById("approve_btn").className="approve_btn";
	document.getElementById("deny_btn").className="deny_btn";

	// Update layers
	document.getElementById("mod_edit_pop").className="";

}

function addDescription(assetId, assettypeId, maxLength) {


	$(document).ready (function() {
	
   		CheckLoggedIn('description');

		var descriptionId = 'text_for_description';
		var descriptionFormId = 'description_form';
		var description = $.trim(document.getElementById(descriptionId).value); 
	
		if ((description.length) == 0) {
			descriptionError (descriptionFormId, descriptionId, 'Missing description');
			return false;
		}

		if ((description.length) > maxLength) {
			descriptionError (descriptionFormId, descriptionId, 
				'The length of the description is longer than ' + maxLength + ' characters');
			return false;
		}
			
		var encoded = encodeURIComponent(description);
		
		$.ajax({
		   type: "POST",
		   datatype: "json",
		   url: "/descriptionservice/add",
		   contentType: "application/x-www-form-urlencoded",
		   data: "&description=" + encoded
		       + "&assetId=" + assetId 
		       + "&assettypeId=" + assettypeId,
		   success: function(data) {
				data = $.trim(data);
	        	if (data == "OK") {
			   		showTyFE(); return false;
	        	}
	        	if (data == "NLI") {
	        		CheckLoggedIn('description');
	        	}
	        	else {
					descriptionError (descriptionFormId, descriptionId, data);
	        	}
	       }
	     });
	}); 
	
	return false;
}


function addDescription2CompanyProducttypeLink(companyId, producttypeId, maxLength) {


	$(document).ready (function() {
	
   		CheckLoggedIn('description');

		var descriptionId = 'text_for_description';
		var descriptionFormId = 'description_form';
		var description = $.trim(document.getElementById(descriptionId).value); 
	
		if ((description.length) == 0) {
			descriptionError (descriptionFormId, descriptionId, 'Missing description');
			return false;
		}

		if ((description.length) > maxLength) {
			descriptionError (descriptionFormId, descriptionId, 
				'The length of the description is longer than ' + maxLength + ' characters');
			return false;
		}

		var encoded = encodeURIComponent(description);
			
		$.ajax({
		   type: "POST",
		   datatype: "json",
		   url: "/descriptionservice/add",
		   data: "&description=" + encoded 
		       + "&companyId=" + companyId 
		       + "&producttypeId=" + producttypeId,
		   success: function(data) {
				data = $.trim(data);
	        	if (data == "OK") {
			   		showTyFE(); return false;
	        	}
	        	if (data == "NLI") {
	        		CheckLoggedIn('description');
	        	}
	        	else {
					descriptionError (descriptionFormId, descriptionId, data);
	        	}
	       }
	     });
	}); 
	
	return false;
}



function descriptionError (formId, descriptionId, felony) {

	DisplayError(formId, felony, 'error', 'formdetail') ;
	HighlightErrorForm(document.getElementById(descriptionId)) ;
}



/*
** Functions for the Frontend Moderation
*/
function showEditFE() {

	// Make the overlay visible
	document.getElementById("d_t_contain").className="";
}


function hideEditFE() {

	// Hide the overlay
	document.getElementById("d_t_contain").className="hideit";
}



function closeFE() {

	// Hide the overlay
	document.getElementById("d_t_contain").className="hideit";
}



function showTyFE() {

	// show the overlay
	document.getElementById("d_t_mid_contain").className="hideit";
	document.getElementById("d_t_mid_contain_ty").className="";
}




/*
** The following functions are Public to all interfaces
*/
/*********************************************************************
 * You may use this code for free on any web page provided that      *
 * these comment lines and the following credit remain in the code.  *
 * Floating Div from http://www.javascript-fx.com                    *
 ********************************************************************/
function floatDiv(id, sx, sy) {
	
	var ns = (navigator.appName.indexOf("Netscape") != -1);
	var d = document;
	var el = d.getElementById ? d.getElementById(id) : d.all ? d.all[id]: d.layers[id];
	var px = document.layers ? "" : "px";
	
	window[id + "_obj"] = el;
	
	if (d.layers) el.style=el;
	
	el.cx = el.sx = sx;
	el.cy = el.sy = sy;
	
	el.sP = function(x,y) {
		this.style.left=x+px;
		this.style.top=y+px;
	};

	el.floatIt = function() {
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		
		document.documentElement && document.documentElement.clientWidth ? 
			document.documentElement.clientWidth : document.body.clientWidth;
		
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		
		document.documentElement.scrollTop : document.body.scrollTop;
		if (this.sy<0) { 
			pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight 
				? document.documentElement.clientHeight : document.body.clientHeight;
		}
		
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.floatIt()", 40);
	}
	
	return el;
}



function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	counter.className = 'char_count';
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('maxlength')) {
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span><strong>0</strong> characters left</span>';
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];

			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength() {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength)
		this.relatedElement.className = 'toomuch';
	else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.firstChild.nodeValue = (currentLength > maxLength)?'0' : maxLength - currentLength;
	// not innerHTML
}




function getScrollY() {

	var scrOfY = 0;
  	if( typeof( window.pageYOffset ) == 'number' ) {
    	// Netscape compliant
    	scrOfY = window.pageYOffset;
  	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    
    	// DOM compliant
    	scrOfY = document.body.scrollTop;
  	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    	// IE6 standards compliant mode
    	scrOfY = document.documentElement.scrollTop;
  	}
  	return scrOfY ;
}
  
  
  
function getWindowHeight(){
	
	var winH = 460;

	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winH = window.innerHeight;
		} else if (navigator.appName.indexOf("Microsoft")!=-1) {
			winH = document.body.offsetHeight;
		}
	}
	
	return winH ;
}



function DropLoc(){
	
	var primary = 300 + getScrollY() ;
	var bottomOffset = getWindowHeight() - 432 ;
	return (primary > bottomOffset)?getWindowHeight()-bottomOffset:primary ;
}



	
