/*
EAS JavaScript Client-Side Solution
Object Specific Functions  
20081101 

All Rights Reserved
Copyright 2008
Gerhard Computing, Inc.
	----------
	
This file is dependent on the frmConsolidate.js file. We re-display the strcuture after we sort
Need Object Data
	1. FP Number (server side this is needed to determine the sub dir)
	2. Collection of Items
			chkBoxId 		- to reference the check box per line; this is unique
		 	sOrd				-	the server side Print Order 
		 	subDirName	-	where the files are located on the server 
										(more for development / debug cycles)
		 	fileName		-	the name of the PDF file tol consolidate
		 	innerHTML		- the contents between  the TR table elements
				
														 
*/


var objClient = null;					//	This is the main Clinet Data Object
var mainDocumentIdx = null;		//	This is the document index in frames array of the frame named 'main'

//	This will initialize the data objects when the Index.asp is first loaded	
function initData( pClientNo, pRowInnerHTML ) {
	
	if (objClient == null) {
		objClient = new objClientData ( pClientNo, pRowInnerHTML );		
	}

	if (mainDocumentIdx == null) {
		for ( var x=0; x < top.frames.length; x++) {
			
			if (top.frames[x].name.toLowerCase() == 'main') {
				mainDocumentIdx = x;
			}
			
		}	//	-	end of for loop
		
		if (mainDocumentIdx == null) {
			alert(	'The main content frame not found.\n' +
							'Website damaged; cannot process !!');
		}
	}	//	-	end of mainDocument is null
	
}	//	-	end

//	----------
//	This will check to see if we need to check; an check box for a Year / Page
function chkAllItems ( pRowInnerHTML ) {
		
	if (objClient != null) {
	
		if  (objClient.tblHeader == null) {
			objClient.tblHeader = pRowInnerHTML;
		}

				//	need to get the id for this
		if ( objClient.objItems != null) {

			for (var x = 0; x < objClient.objItems.length; x++ ) {
			
				if ( objClient.objItems[x] != null) {
					var chkBoxList = top.main.document.getElementsByName(objClient.objItems[x].chkBoxId);
						
						//	getElementsByName returns an array. We expect only one element; 
						//	IE wil return two; one for the TR and one for the CHECKBOX
					for (var i=0; i < chkBoxList.length; i++ ) {
						if ( chkBoxList[i].type == "checkbox") {
								chkBoxList[i].checked = true
						}
					}	//	-	for-loop

				}	//	-	objClient.objItems[x] != null
			}	//	-	for loop
		}	//	-	objClient.objItems != null<B></B>
			
	}	//	-	objClient != null			
}	//	-	end

//	----------
//	This will add/remove an item from the array
function chkTheItem ( pChkBoxObj, pRowInnerHTML ) {
	if (objClient != null) {
				
		if (pChkBoxObj.checked) {				//	Adding 
			
			objClient.addItem( pChkBoxObj, pRowInnerHTML );
			
		}
		else {													//	Removing
			objClient.removeItem (pChkBoxObj);
		}
		
	}	//	- we have objClient	
}	//	-	end

//	----------
//	objClient class defines
function objClientData  ( pClientNo, pSMTP ) {

	//	These are object properties
	this.ClientNo = pClientNo;		//	Client sub directory ; required in back end
	this.SMTP = pSMTP;						//	Address to email to ; required in back end
	this.tblHeader = null;				//	This is set in other methods
	this.objItems = new Array();	//	

	//	These are object methods
	this.addItem = addItem;	
	this.removeItem = removeItem;	
	this.getClientNo = getClientNo; 
	
}	//	-	end

//	----------
function addItem (pChkBoxObj, pRowInnerHTML) {

	objClient.objItems [objClient.objItems.length] = 	
		new objItem( pChkBoxObj, pRowInnerHTML );
		
	objClient.objItems.sort(objItemSort);		//	Sort every time we add / remove.
	
}	//	-	end

//	----------
function removeItem (pChkBoxObj) {
	
	for (var x=0; x < objClient.objItems.length; x++ ) {
		if (objClient.objItems[x].chkBoxId == pChkBoxObj.name) {
		
			objClient.objItems[x] = null;					//	null out the entry
			objClient.objItems.sort(objItemSort);	//	sort the null to the end
			objClient.objItems.length--;					//	decrement the index
		
		}
	}
}	//	-	end

//	----------
function getClientNo () {

	return (this.ClientNo);

}	//	-	end


//	----------
//	The Compare Method to pass to the array.sort() method
//	if compareFunction(a, b) is less than zero, sort b to a lower index than a.
//	if compareFunction(a, b) returns zero, leave a and b unchanged with respect to each other, 
//	but sorted with respect to all different elements.
//	if compareFunction(a, b) is greater than zero, sort b to a higher index than a. 
function objItemSort (pElementA, pElementB) {
	var rVal = 0;

		//	For null values, it seems we are reversed. nulls gete sorted to the end here 
	if ( (pElementA == null) || (pElementA.sOrd == null)) {
		return(1);  
	}
	
	if ( (pElementB == null)  || (pElementB.sOrd == null)) {
		return (-1);  	
	}

	//	To sort a number we need to convert and the compare and retuen
	//	A - B will give us what we need
	return ( pElementA.sOrd - pElementB.sOrd );
}	//	-	end


//	----------
//	Destroys the entire structure
function KillObjects () {

	objClient = null;

}	//	-	end


//	----------
function objItem( pChkBoxObj, pRowInnerHTML )	{

		//	These are object properties
	this.chkBoxId = pChkBoxObj.name;		//	Required to re-check it when reloading a page
	
		//	This is the header for the table when validating
	this.tblHeader = '';
	
	this.sOrd = 10;

		//	Get last and add 10		
	if ( objClient.objItems.length > 0 ) {
		this.sOrd = 10 + ( objClient.objItems[ objClient.objItems.length - 1 ].sOrd );
	}


	this.subDirName = "20" + pChkBoxObj.name.substring(0,2);
	this.fileName = pChkBoxObj.name + ".pdf";
	
		//	Used to display the final list for approval and sort methody
		//	This is the ROW.innerHTML so we can build the table afterwards
		
		//	We need to replace the onclik element for the chckbox with the SetOrd function

	var pStart = pRowInnerHTML.indexOf('onclick="',0);
	var pStop = pRowInnerHTML.indexOf(';"', pStart);
	
	var pStr = pRowInnerHTML.substr(0,pStart) +
				'onclick="top.setSOrd( this )' +
				pRowInnerHTML.substr(pStop);

		//	IE will pass the vlaue of the checkbox as CHECKED. 
		//	If the string CHECKED is found, then we need to clear this
	pStart = pStr.indexOf('CHECKED',0);
	if (pStart > 0) {
		pStr = pStr.substr(0,pStart) + ' ' + pStr.substr(pStart + 7);
	}


	this.innerHTML = pStr;	

	//	These are object methods
	this.getFullFileName  = getFullFileName;	
		
}	//	-	end

//	----------
function getFullFileName () {
	
	return ( objClient.getClientNo() + '\\' + this.subDirName + '\\' + this.fileName ); 
	
}	//	-	end


//	----------
//	Set the Sort Order based on the passed identifier
function setSOrd ( pChkBoxObj ) {

	var tOrd = prompt('Give the sort order as an integer',10)

	if ( (tOrd != null) &&  (!isNaN(tOrd)) ) {
		//	update right element
		for (var x=0; x < objClient.objItems.length; x++ ) {
			if (objClient.objItems[x].chkBoxId == pChkBoxObj.name) {
		
				objClient.objItems[x].sOrd = tOrd;
				objClient.objItems.sort(objItemSort);	
		
			}
		}	
	}
	
	//	Resort and dump full Form Page
	frmConsolidate ();
	
}	//	-	end


//	----------
//	To manually sort the client structure. 
//	objCLient sorts after each sOrd Update
function sortClient(){
	objClient.objItems.sort(objItemSort);
}	//	-	end


/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */

