//$Id: TableHandling.js,v 1.2 2010/01/23 09:34:00 aramanathan Exp $

String.prototype.trim = function() {
var x=this;
x=x.replace(/^\s*(.*)/, "$1");
x=x.replace(/(.*?)\s*$/, "$1");
return x;
}
function resetNavigationState(id)
{
var uniqueId = getUniqueId(id);
updateState(uniqueId, "_FI", "1");      
updateState(uniqueId, "_TI", null);
updateState(uniqueId, "_PN", null);
stateData[uniqueId]["_VMD"]= '1';
}
function sortData(id, columnName){
var uniqueId = getUniqueId(id);
updateState(uniqueId, "_SB", columnName);
resetNavigationState(id);
var order = stateData[uniqueId]["_SO"];
if(order == "D"){
updateState(uniqueId, "_SO","A");
order = "ASC";
}
else{
updateState(uniqueId, "_SO","D");
order = "DESC";
}
stateData[uniqueId]["_MP"] = "_SB";
refreshSubView(uniqueId);
}
function showRange(id, fromIndex){
var uniqueId = getUniqueId(id);
var _pl = parseInt(stateData[uniqueId]["_PL"],10);
var _pn = 1;
_pn = parseInt(fromIndex / _pl, 10) + 1;
updateState(uniqueId, "_FI", fromIndex);
updateState(uniqueId, "_PN", _pn + "");
stateData[uniqueId]["_VMD"]= '1';
refreshSubView(uniqueId);
}
function showRangeForLength(id, pageLength){
var uniqueId = getUniqueId(id);
updateState(uniqueId, "_PL", pageLength);
resetNavigationState(uniqueId);
stateData[uniqueId]["_MP"] = "_PL";
refreshSubView(uniqueId);
}
function fetchSpotSearchData(el){
var uniqueId = DOMUtils.getParentWithAttr(el,"unique_id").getAttribute("unique_id");
var searchRow = TableModel.getInstance(uniqueId).getNamedEl("SEARCHROW");
var searchInputs = searchRow.getElementsByTagName("input");
if(!validateFormElements(searchInputs,searchRow)){
return false;
}
var searchColumns = "";
var searchValues = "";
var initial = true;
for(i = 0; i < searchInputs.length; i++){
var name = searchInputs[i].name;
var value = searchInputs[i].value;
if(value != null && value.trim() != ""){
if(!initial){
searchColumns = searchColumns + ",";
searchValues = searchValues + ",";
}
searchColumns = searchColumns + name;
searchValues = searchValues + value;
initial = false;
}
}
updateState(uniqueId, "SEARCH_COLUMN", searchColumns);
updateState(uniqueId, "SEARCH_VALUE", searchValues);
resetNavigationState(uniqueId);
refreshSubView(uniqueId);
return false;
}
function alternateRows(id) {
var even = false;
var evenClass = arguments[1] ? arguments[1] : "#fff";
var oddClass = arguments[2] ? arguments[2] : "#eee";
var table = document.getElementById(id);
if (! table) { return; }
var tbodies = table.getElementsByTagName("tbody");
for (var h = 0; h < tbodies.length; h++) {
var trs = tbodies[h].getElementsByTagName("tr");
for (var i = 0; i < trs.length; i++) {
var tds = trs[i].getElementsByTagName("td");
for (var j = 0; j < tds.length; j++) {
var mytd = tds[j];
mytd.setAttribute("class",even ? evenClass : oddClass);
}
even =  ! even;
}
}
}
function closeSearch(sourceEl)
{
var uniqueId = DOMUtils.getParentWithAttr(sourceEl,"unique_id").getAttribute("unique_id");
var tblDOMModel = TableModel.getInstance(uniqueId);  
var searchRow = tblDOMModel.getNamedEl("SEARCHROW");
tblDOMModel.getNamedEl("OSBTN").className = "tableSearchButton";
tblDOMModel.getNamedEl("CSBTN").className = "hide";
var isValPresent = false;
var inputs = searchRow.getElementsByTagName("input");
var size = inputs.length;
for(var count = 0; count < size; count++){
var element = inputs[count];
if((element.value != "")
&& (element.value != null)){
element.value = "";
isValPresent = true;
}
}
if(isValPresent){
fetchSpotSearchData(sourceEl);
}
else
{
searchRow.className = "hide";
}
}
function openSearch(sourceEl)
{
var uniqueId = DOMUtils.getParentWithAttr(sourceEl,"unique_id").getAttribute("unique_id");
var tblDOMModel = TableModel.getInstance(uniqueId);  
var searchRow = tblDOMModel.getNamedEl("SEARCHROW");
searchRow.className = "searchRow";  
tblDOMModel.getNamedEl("OSBTN").className = "hide";
tblDOMModel.getNamedEl("CSBTN").className = "tableSearchCloseButton";
var inputs = searchRow.getElementsByTagName("input");
inputs[0].focus();
}
function updateSearchData(id, select){
var uniqueId = getUniqueId(id);
searchColumn = select.name;
searchValue = select.value;
updateState(uniqueId, "SEARCH_COLUMN", searchColumn);
updateState(uniqueId, "SEARCH_VALUE", searchValue);
updateState(uniqueId, "_SB", null);
stateData[uniqueId]["_VMD"]= '1';
resetNavigationState(uniqueId);
refreshSubView(uniqueId);
return false;
}
function showCompleteMessage(curId){
var message = window[curId]["MSG"];
message = message.replace(/\n\n/gi,"<br>");
message = message.replace(/\n/gi,"<br>");
showCustomMessage(message, document.getElementById(curId), 'completeMessage');
}
function hideCompleteMessage(){
document.getElementById("completeMsgDiv").className = "hide";
}
function defaultTrimmedMessageDisplayer(trimmedMsg, completeMsg, insertionId, action, destDoc){
window[insertionId] = new Object();
window[insertionId]["MSG"] = completeMsg;
var data = "<a  href=\"javascript:showCompleteMessage('" + insertionId + "')\">" + trimmedMsg + "</a>";
destDoc.getElementById(insertionId).innerHTML = data;
}
function showTrimmedMessage(trimmedMsg, completeMsg, insertionId, action, destDoc){
var data = "";
if(action != null && action != "" && action != "null"){
data = data.concat("<a href='" + action + "'>");
}
data = data.concat(trimmedMsg);
if(action != null && action != "" && action != "null"){
data = data.concat("</a>");
}	
destDoc.getElementById(insertionId).innerHTML = data;
}
function showTrimmedMessageOnImageClick(trimmedMsg, completeMsg, insertionId, action, destDoc){
var data = "";
if(action != null && action != "" && action != "null"){
data = data.concat("<a href='" + action + "'>");
}
data = data.concat(trimmedMsg);
if(action != null && action != "" && action != "null"){
data = data.concat("</a>");
}	
window[insertionId] = new Object();
window[insertionId]["MSG"] = completeMsg;
data = data.concat("<button class='messageInvoker' onClick=\"javascript:showCompleteMessage('" + insertionId + "');return false\"></button>");
destDoc.getElementById(insertionId).innerHTML = data;
}
function showTrimmedMessageOnMouseOver(trimmedMsg, completeMsg, insertionId, action, destDoc){
var data = "";
if(action != null && action != "" && action != "null"){
data = data.concat("<a href='" + action + "'>");
}
data = data.concat(trimmedMsg);
if(action != null && action != "" && action != "null"){
data = data.concat("</a>");
}	
window[insertionId] = new Object();
window[insertionId]["MSG"] = completeMsg;
data = data.concat("<button class='messageInvoker' onMouseOver=\"javascript:showCompleteMessage('" + insertionId + "');return false\" onMouseOut=\"hideCustomMessage();return false;\"></button>");
destDoc.getElementById(insertionId).innerHTML = data;
}
function showMessageOnTextMouseOver(trimmedMsg, completeMsg, insertionId, action, destDoc){
var data = "";
if(action != null && action != "" && action != "null"){
data = data.concat("<a href='" + action + "'>");
}
window[insertionId] = new Object();
window[insertionId]["MSG"] = completeMsg;
data = data.concat("<span onMouseOver=\"javascript:showCompleteMessage('" + insertionId + "');return false\" onMouseOut=\"hideCustomMessage();return false;\">");
data = data.concat(trimmedMsg);
data = data.concat("</span>");
if(action != null && action != "" && action != "null"){
data = data.concat("</a>");
}	
//CHANGE FOR OPUTILS STARTS
//destDoc.getElementById(insertionId).innerHTML = data;
var t = document.createElement('span');
t.innerHTML = data;
destDoc.getElementById(insertionId).appendChild(t);
//CHANGE FOR OPUTILS ENDS
}
function displayColumnChooser(menuItemId, srcViewRefId, additionalParams,index) {
var menuItemObj = getMenuItemObj(menuItemId);
var url = getURLSuffixed(menuItemObj.ACTIONLINK) + additionalParams;
showURLInDialog(url, menuItemObj.WINPARAMS);
}
function selectFilter(id,filterCombo)
{
var uniqueId = getUniqueId(id);
updateState(uniqueId,'SELFILTER',filterCombo.value);
resetNavigationState(id);
refreshSubView(uniqueId);
}

