//	Creator: Jonathan Higley
//	Created: November 23, 2008


///////////////////////////////////////////////////////// Table Selector Handlers
function hover(row)
{ 
	var currentclass = row.className;

	if (currentclass=='result_row_selected') {
//	alert("This row IS selected");
	} else {
	    row.className = 'result_row_over';
//	alert("This row is not selected");
	}
}

function dehover(row)
	{
		var currentclass = row.className;
		
		if (currentclass=='result_row_selected') {
//If selected, do nothing...
		} else {
		    row.className = 'result_row';
		}
	}

var selectedRow = null;
function selectRow(whichrow,targetdiv)
{
	row = whichrow;
	
  while (row && row.nodeName != 'TR') row = row.parentNode;
  if (!row) return false;
  if (selectedRow) selectedRow .className = 'result_row';
  row.className = 'result_row_selected';
  selectedRow = row;
  
  if (row.className == 'result_row_slected') {
    row.className = 'result_row_selected';
  }
}
///////////////////////////////////////////////////////// End Table Selector Handlers

function toggleTransactionPage(pageToRequest,node) {

    var loadDiv = document.getElementById("transactionlist_loading");
    var resultsDiv = document.getElementById("tran_inner_cont");

    loadDiv.style.display='block';
    resultsDiv.style.display='none';

    new Ajax.Request("functions/results_transactions.html?transactionToggle=true&page=" + pageToRequest,
    {
        method: 'post',
        onComplete: function(req){
            loadDiv.style.display='none';
            resultsDiv.style.display='block';

            // Set the html from the response.
            resultsDiv.innerHTML = req.responseText;
        }
    });
}

function toggleVendorPage(pageToRequest,node) {

    var loadDiv = document.getElementById("vendorlist_loading");
    var resultsDiv = document.getElementById("vendor_inner_cont");

    loadDiv.style.display='block';
    resultsDiv.style.display='none';

    new Ajax.Request("functions/results_vendor.html?vendorToggle=true&node="+node+"&page=" + pageToRequest,
    {
        method: 'post',
        onComplete: function(req){
            loadDiv.style.display='none';
            resultsDiv.style.display='block';

            // Set the html from the response.
            resultsDiv.innerHTML = req.responseText;
        }
    });
}

function initialLoading() {
		moveit(1,0);
		removeElement(0,0);
        document.getElementById('loading_first').style.display="block";
        document.getElementById('showresults0').style.display="none";
//        document.getElementById('first_filter').style.display="none";
		document.getElementById("movenext").innerHTML = "<a class='next_button_disabled'>&nbsp;</a>";	
		
		var errorDiv = document.getElementById('error_message');
		if (errorDiv.style.display != 'none') {
//			alert(Modalbox);
			if(Modalbox.initialized) {
				Modalbox.hide($(errorDiv));
			}
		}
		
// Send Search Info to Google Events
	googleEventSearchType = ""+document.getElementById('advanced').value+"";
	pageTracker._trackEvent("Search", "Initial", "Free", googleEventSearchType);
}












    //-->	AJAX Requests
    function loading(displaydiv,num) {
    //	alert(num);
    //	alert(displaydiv);
        
        if (num===0) {
            var loadingid = 'loading_first';
        } else {
            var loadingid = 'loading' + num;
        }
		
    //	alert(displaydiv);	
        document.getElementById(loadingid).style.display="block";
        document.getElementById(displaydiv).style.display="none";
    }
    
    function loaded(displaydiv,num) {
    
        if (num===0) {
            var loadingid = 'loading_first';
        } else {
            var loadingid = 'loading' + num;
        }
        
        document.getElementById(loadingid).style.display="none";
        document.getElementById(displaydiv).style.display="block";
    }
    
    function sendFilterRequest(v,displaydiv,num,nodenum,context,catid,fundid,orgid,vendorid,linename) {
        loading(displaydiv,num);
        new Ajax.Request("functions/results_" + v + ".html?vendorName=&linename="+linename+"&node="+nodenum+"&num="+num+"&from=1&to=10&context="+v+"&catid="+catid+"&fundid="+fundid+"&orgid="+orgid+"&vendorid"+vendorid,
                            { 
                            method: 'post', 
                            
                            onComplete: function(req){
                                            loaded(displaydiv,num);
                                            // alert(displaydiv);
											
							// alert("Context: "+v+" | Category: "+catid+"| Fund: "+fundid+" | Organization: "+orgid+" | Vendor: "+vendorid);
										
                                            $(''+displaydiv+'').innerHTML = req.responseText;
											// alert(linename);
                                            }					
                            });
        }

    function sendRequestVendor(displaydiv,num,nodenum,vpcvalue,linename,context,catid,fundid,orgid,vendorid) {
        //Kill All Trail Nodes and Set Next Class
        removeElement(nodenum,nodenum);
        document.getElementById("movenext").innerHTML = "<a href='#' class='next_button_disabled'>&nbsp;</a>";

        var testvar='fund_name';

        loading(displaydiv,num);
		
								
                    new Ajax.Request("functions/results_vendor.html?page=0&display=vendor&node="+nodenum+"&vendorName="+vpcvalue+"&linename="+linename+"&num="+num+"&from=1&to=10&context="+context+"&catid="+catid+"&fundid="+fundid+"&orgid="+orgid+"&context=vendor&startsWith=0",
                            {
                            method: 'post',

//                            postBody: 'vendorid='+ $F('fund_name'+nodenum),

                            onComplete: function(req){
											// return false;
                                            loaded(displaydiv,num);
                                            //alert("Vendor / Payee / Customer");
                                            $(displaydiv).innerHTML = req.responseText;

                                        }
                            });
                        }

    //--> START AJAX REQUEST FOR TRANSACTION DETAIL
    function sendRequestTD(payee,totalspent,entitytransid,onlinerecordid) {
        new Ajax.Request("functions/transaction_detail.html?payee="+payee+"&totalspent="+totalspent+"&entitytransid="+entitytransid+"&onlinerecordid="+onlinerecordid+"&from=1"+"&to=10",
                            {
                            method: 'post',
                            postBody: 'thevalue='+ $F('theValue'),
                            onComplete: function showResponseTD(req){$('transaction_detail').innerHTML=req.responseText;

							//Fix modal if advanced or no
							var isadvanced = document.getElementById('advanced').value;
							if (isadvanced==0) {
								// alert(isadvanced);
									Modalbox.show($('transaction_detail'), {title: 'Viewing Transaction Detail', width: 675});
								} else {
									Modalbox.show($('transaction_detail'), {title: 'Viewing Transaction Detail', width: 675,marginTopOverlay: 443,marginTopWindow: 489});
								}
							}
                            });
    }


    //<-- END AJAX REQUEST FOR TRANSACTION DETAIL


    ///////--------> Vendor / Payee / Customer Selection Effects
    function effects(selectedvalue,divname,selectorname,field) {

        if (selectedvalue=='vendor') {
//            effect_1 = Effect.BlindDown(divname,{duration:0.25});
 //           effect_1 = Effect.BlindUp(selectorname,{duration:0.25});

//	document.getElementById('filter_data').style.width='100px';
//	var moveamount = '100px';
//		var morphmove = 'width:100px;';

//	$('results_slider').morph(morphmove);

// alert(divname);

/*
new Effect.Morph(field, {
	style: 'width:192px;', // CSS Properties
	duration: 0.1, // Core Effect properties
	transition: Effect.Transitions.sinoidal}
);

effect_1 = Effect.Appear(divname,{duration:.75});

        } else {
//		alert("different")

new Effect.Morph(field, {
	style: 'width:404px;', // CSS Properties
	duration: 0.25, // Core Effect properties
	transition: Effect.Transitions.sinoidal}
);

*/


// document.getElementById(divname).style.display="none";

        }
    }

    function slideup(divname,selectorname,fieldname) {
            effect_1 = Effect.BlindUp(divname,{duration:0.25});
            effect_1 = Effect.BlindDown(selectorname,{duration:0.25});
            document.getElementById(fieldname).value = 'Search this Panel: Enter Person or Company';
            document.getElementById(fieldname).cleared = false;
            document.getElementById(fieldname).style.color = '#ccc';
    }

        //--> START Vendor / Payee / Customer Name Input Focus Handeling Scripts
        function clearDomainInput(e) {
                  if (e.cleared) {return;}
                  e.cleared = true;
                  e.value = '';
                  e.style.color = '#000';
                }


        function formSubmit() {
                  domain = document.getElementById('filter').value;
                  window.location = '/' + fund_name;
                  return false;
                }
        //<-- END Focus Handeling Scripts

    //<-- END Selection Effects

    //--> START Filter Drop-Down Request
    function requestor(v,displaydiv,nodenum,context,catid,fundid,orgid,vendorid,linename) {

	//SEND EVENT TO GOOGLE
	googleDropValueLabel = "Filter by "+v+"";
	pageTracker._trackEvent('Search', googleDropValueLabel, 'Free');
	
	
    //	alert(nodenum);

        var num = displaydiv;
		contentDisplayDiv = 'showresults' + displaydiv;
/*
        if (displaydiv==0) {
            contentDisplayDiv = 'showresults';
        } else {
            contentDisplayDiv = 'showresults' + displaydiv;
        };
*/

        if (v=="vvendor" || v=="default") {
        } else {
        //Kill the Next Indicator and Everything Beyond
        removeElement(nodenum,nodenum);
        document.getElementById("movenext").innerHTML = "<a href='#' class='next_button_disabled'>&nbsp;</a>";

    //	alert(v+', '+contentDisplayDiv+', '+num+', '+nodenum);

       sendFilterRequest(v,contentDisplayDiv,num,nodenum,context,catid,fundid,orgid,vendorid,linename);
//    function requestor(v,displaydiv,nodenum,context,fundid,catid,orgid,vendorid) {
        };
    };


// --> Add New DIVS / LIs
function addElement(linename,linetotal,currentnode,context,catid,fundid,orgid,vendorid) {


//SEND EVENT TO GOOGLE
//	googleClickValueLabel = ""+linename+"";
	pageTracker._trackEvent('Search', 'Drill Down', 'Free');
	
	

//Remove Any Elements Beyond this Node & Kill the Next Indicator
	removeElement(currentnode,currentnode);
	document.getElementById("movenext").innerHTML = "<a href='#' class='next_button_disabled'>&nbsp;</a>";

//Establish all Variables
  var ni = document.getElementById('nulldiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  numi.value = num;

  var sendRequestName = 'sendRequest' + num;
  var showResponsename = 'showResponse' + num;
  var showResultsDivID = 'showresults' + num;
  var showResultsFilterID = 'resultfilter' + num;

  var newdiv = document.createElement('div');
//  var divIdName = 'result_container'+num;
  var divIdName = num;
  newdiv.setAttribute('id',divIdName);


//Check Browser if IE, Add new Divs with inline styles...
var browserName=navigator.appName;

if (browserName=="Microsoft Internet Explorer")
 {
  newdiv.style.cssText = "float:left;margin-left:5px;margin-right:12px;";
 } else {
  newdiv.style.cssText = "float:left;margin-left:5px;margin-right:3px;";
 }

  newdiv.setAttribute('float','left');
  newdiv.setAttribute('class','resultDivNode');

  var filterdivIdName = 'result_container'+num;


//START NODE Variables
  var d = document.getElementById("nulldiv");
  var numberofcurrentnodes = d.childNodes.length;
  var currentnodenum = numberofcurrentnodes + 1;



//Send AJAX Request for the Content Area...
function sendRequest(resultdivid) {

		var loadid='loading'+resultdivid;
//		var t=setTimeout("alert('loading')",50);
//		var t=setTimeout("loading('showresults1','1')",5);
/*
        document.getElementById(loadid).style.display="block";
        document.getElementById(showResultsDivID).style.display="none";
*/

// loading(resultdivid,resultdivid);
//Slide to show loading...
moveit(currentnodenum,numberofcurrentnodes);
document.getElementById("movenext").innerHTML = "<a href='#' class='next_button_disabled'>&nbsp;</a>";

linename = linename.replace(/&/,'%26');

new Ajax.Request("functions/results_"+context+".html?id="+showResultsDivID+"&node="+currentnodenum+"&num="+num+"&linetotal="+linetotal+"&from=1"+"&to=10&context="+context+"&catid="+catid+"&fundid="+fundid+"&orgid="+orgid+"&vendorid="+vendorid+"&linename="+linename,

					{
					method: 'post',

					postBody: 'thevalue='+ $F('theValue'),

                   onComplete: function(req){
											// return false;
                                            loaded(resultdivid,num);
                                            //alert("Vendor / Payee / Customer");
                                            $(showResultsDivID).innerHTML = req.responseText;
											
				   					}
					});

}



function showResponsen(req){
				// alert(req.responseText);
				// alert(context+" | "+id);
				$(showResultsDivID).innerHTML=req.responseText;
				moveit(currentnodenum,numberofcurrentnodes);
//				loaded('showresults1','1');

}


//Send AJAX REQUEST to retrieve the new Drop-Down Filter...
function sendRequestFilter(num,showResultsFilterID) {

new Ajax.Request("functions/ajax_result_filter.jsp?num="+num+"&node="+currentnodenum+"&linename="+linename+"&context="+context,
					{
					method: 'post',

					postBody: 'thevalue='+ $F('theValue'),

//					onComplete: showResponseFilter
					onComplete: function(req){
									// alert(req.responseText);
									//$('showsomeshi').innerHTML=req.responseText;
									$(showResultsFilterID).innerHTML=req.responseText;
									getnum(num);
									}

					});
}

//Fire!
sendRequest(num);
// TEMP HIDDEN FOR PANEL SHIFT: sendRequestFilter(num,showResultsFilterID);

// <a onclick="sendRequest('+showResultsDivID+');" href="#">Search</a></div></form>
// NUMBER INDICATOR: <span class=higher>'+num+'</span>

  newdiv.innerHTML = '<div class=result_window><div id="loading'+num+'" class="loading"><br /><img src="images/icons/loading_large.gif"/><br /><br />Loading...</div><div id='+showResultsDivID+'></div></div></div>';
  ni.appendChild(newdiv);


}

function removeElement(divName,currentnodenum) {

	var d = document.getElementById("nulldiv");

	if ( d.hasChildNodes() )
	{
		var numberofnodes = d.childNodes.length;
	//	alert(numberofnodes);

		while ( d.childNodes.length > currentnodenum )
		{
			d.removeChild( d.lastChild);
		}
	}

}

///////// MOVE Functions

function checknodes() {
	//Calculation of Total Current Nodes
	var d = document.getElementById("nulldiv");
	var numberofcurrentnodes = d.childNodes.length;
//	alert(numberofcurrentnodes);
}

function moveit(currentnodenum,totalnodes) {
	//Calculation of Total Current Nodes
	var d = document.getElementById("nulldiv");
	var numberofcurrentnodes = d.childNodes.length;
//	alert('Total Nodes:'+numberofcurrentnodes);

	var movepx = (currentnodenum-1)*-440;
	var suffix = "px";
	var moveamount = movepx+suffix;

//	alert(currentnodenum);
//	alert(moveamount);

	var morphmove = "margin-left:"+moveamount+";";
		
//	$('results_slider').morph(morphmove);	

new Effect.Morph('results_slider', {
	style: morphmove, // CSS Properties
	duration: 0.65, // Core Effect properties
	transition: Effect.Transitions.sinoidal}
);
	
	
	//PREVIOUS
	if (currentnodenum<=1) {
		var setpreviousclass='previous_button_disabled';
		var setpreviousactive = '0';
	} else {
		var setpreviousclass='previous_button';
		var setpreviousactive = '1';
	}
	
	var previousnodenum = currentnodenum-1;
	
	if (setpreviousactive>=1) {
	document.getElementById("moveprevious").innerHTML = "<a onclick='moveit("+previousnodenum+")' class="+setpreviousclass+">&nbsp;</a>";
	} else {
	document.getElementById("moveprevious").innerHTML = "<a class="+setpreviousclass+">&nbsp;</a>";
	}
	
	//END PREVIOUS
	
	//	alert('Previous Node:'+previousnodenum);
	
	//NEXT
	
	var variance = (previousnodenum+1)-numberofcurrentnodes;
	
	//	alert('Variance:'+variance); 
	
	if (variance==0) {
		//	alert('lessthanzero');
		var setnextclass='next_button_disabled';
		var setnextactive = '0';
	} else {
		var setnextclass='next_button';
		var setnextactive = '1';
	}
	
	var nextnodenum = currentnodenum+1;
	
	if (setnextactive>=1) {
		document.getElementById("movenext").innerHTML = "<a onclick='moveit("+nextnodenum+")' class="+setnextclass+">&nbsp;</a>";	
	} else {
		document.getElementById("movenext").innerHTML = "<a class="+setnextclass+">&nbsp;</a>";	
	}
	//END NEXT

}
///////// END MOVE FUNCTION ///////////////////////

function clearInput(inputId) {
	if (document.getElementById(inputId).value=='Search this Panel: Enter Person or Company') {
		document.getElementById(inputId).value = '';
		document.getElementById(inputId).className = '';
	}
}

function initializeInput(inputId) {
//		alert(inputId);
	if (document.getElementById(inputId).value=='') {
		document.getElementById(inputId).value = 'Search this Panel: Enter Person or Company';
		document.getElementById(inputId).className = 'graySearch';
	}
}
