// P R E M    2 0 0 5
//
// make this NGO specific to your competition by choosing how it sorts and processes the data
function NonGenericObject(snapShotObj,model){
this._model = model;
this._snapShotObj = snapShotObj;
this._counter = 0;
this.matchFixtureNamesToResultNames();
//this.orderTable();
}
NonGenericObject.prototype.matchFixtureNamesToResultNames = function(){
// iterative processing...
for(var i=1;i<=this._snapShotObj._rowCount;i++){
	for(var j=1;j<=this._snapShotObj._numberOfRowsOfResults;j++){
		// match names for the teams on the LHS of the fixtures list
		if(this._snapShotObj.i_itemNameLeft[i] == this._snapShotObj.o_tablerow[j][1]){
			this.updateResults(i,j);
			}
		}
	}
// 'one shot' processing...
this.sortResults();	
this._model.transmit(this._snapShotObj);	
}
// modify results rows one by one
NonGenericObject.prototype.updateResults = function(i,j){
// get row number of results table for rhs fixtures by matching team name
this.RightRowNum = this.matchFixturesOnRight(i);
var mirrorValue = this.checkMirrors(i);
this.modifyPlayed(i,j,mirrorValue);
this.modifyGoalDifference(i,j,mirrorValue);
this.modifyPoints(i,j,mirrorValue);
this.modifyGoalsFor(i,j,mirrorValue);
}
NonGenericObject.prototype.sortResults = function(){
this.createSortArray();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// the SECOND argument sent to 'sortThese()' below is the category you want to sort by...
// 1 = team name
// 2 = played
// 3 = goal difference
// 4 = points 
// start for Prem by organising by points...
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Here is the format for this sort
// 1.	Teams are grouped according to points - highest first
// 2.	If any teams have the same points, they are grouped by goal difference - highest first
// 3	If any teams have the same goal difference, they are grouped by total goals scored - highest first
// 4.	If any teams have the same number of goals, goal difference and points they are listed alphabetically
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
this.sortThis(this.sortArray,this.sortArray.length,3,"A");
this.orderLikePointsResults((this.sortArray.length-1));
this.sortMinis(this.miniArray.length,2,"A");
this.putMinisBack(this.miniArray.length);
this.orderLikeGoalDifferenceResults((this.sortArray.length-1));
this.sortMinis(this.miniArray.length,4,"A");
this.putMinisBack(this.miniArray.length);
this.orderLikeGoalsForResults((this.sortArray.length-1));
this.sortMinis(this.miniArray.length,0,"B");
this.putMinisBack(this.miniArray.length);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
this.putSortedDataBack();
}
///////////////////////////////////////////////////////////////////////////////////////////// S O R T    P R O C E S S I N G    B E G I N S  //////////////////////////////////////
// see what we've got...
NonGenericObject.prototype.showThis = function(object,length,element) {
    var output = '<table cellspacing="0" cellpadding="0" border="1" width="300"><tr>';
    for(var i=1; i<=length; i++) {
        for(var j=0; j<=element; j++) {
            output += '<td>'+object[i][j] + '</td>';
        }
    output += '</tr>';
    }
output +='</table>';
window.document.write(output + '<BR>');
}
// take all the rows that have the SAME goal difference value and put them into mini arrays...
NonGenericObject.prototype.orderLikeGoalsForResults = function(length){
this.miniArray[0]="buffer array"
var x = 1;
var y = 1;
this.miniArray[1] = new Array();
this.miniArray[1][0] = 1;
for (var i=1; i<=length; i++)
if(this.sortArray[(i+1)]!=null){
	if(this.sortArray[i][4] == this.sortArray[(i+1)][4]&&this.sortArray[i][2] == this.sortArray[(i+1)][2]&&this.sortArray[i][3] == this.sortArray[(i+1)][3]){
		this.miniArray[x][y] = this.sortArray[i]
		y++
		}
		else{
		this.miniArray[x][y] = this.sortArray[i];
		y=1;
		x++;
		this.miniArray[x] = new Array();
		this.miniArray[x][0] = i+1;
		}
	}
	else{this.miniArray[x][y] = this.sortArray[i];}
}
// sort whatever you want according to the options 'A' or 'B'
NonGenericObject.prototype.sortThis = function(arrayName,length,element,option){
    for (var i=1; i<(length-1); i++){
        for (var j=i+1; j<length; j++){
		// Option A is a sort for numbers - puts the highest value FIRST
			if(option=="A"){
	            if(Number(arrayName[j][element]) > Number(arrayName[i][element])) {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
				}
			}	
			else{
		// Option B is a sort for strings - puts them alphebetically	
				 if (arrayName[j][element] < arrayName[i][element]) {
                var dummy = arrayName[i];
                arrayName[i] = arrayName[j];
                arrayName[j] = dummy;
				}
            }
		}	
	}	
}
// take all the rows that have the SAME points value and put them into mini arrays...
NonGenericObject.prototype.orderLikePointsResults = function(length){
this.miniArray = new Array();
this.miniArray[0]="buffer array";
var x = 1;
var y = 1;
this.miniArray[1] = new Array();
this.miniArray[1][0] = 1;
for (var i=1; i<=length; i++){
if(this.sortArray[(i+1)]!=null){
	if(this.sortArray[i][3] == this.sortArray[(i+1)][3]){
		this.miniArray[x][y] = this.sortArray[i];
		y++;
		}
		else{
		this.miniArray[x][y] = this.sortArray[i];
		y=1;
		x++;
		this.miniArray[x] = new Array();
		this.miniArray[x][0] = i+1;
		}
	}
	else{this.miniArray[x][y] = this.sortArray[i];}
	}
}
// a general sorter for mini arrays - passes them to 'sortThis' based upon option
NonGenericObject.prototype.sortMinis = function(length,element,option){
for (var i=1; i<length; i++){
	var len = this.miniArray[i].length;
	this.sortThis(this.miniArray[i],len,element,option);
	}
}
// mini arrays must be put back in the right place in the main array...
NonGenericObject.prototype.putMinisBack = function(length){
	for (var j=1; j<length; j++) {
		for (var k=1; k<this.sortArray.length; k++) {
			if(k == this.miniArray[j][0]){
				for(var n=1;n<this.miniArray[j].length;n++){
					this.sortArray[k] = this.miniArray[j][n]
					k++;
				}
			}
		}
	}
}
// take all the rows that have the SAME goal difference value and put them into mini arrays...
NonGenericObject.prototype.orderLikeGoalDifferenceResults = function(length){
this.miniArray[0]="buffer array"
var x = 1;
var y = 1;
this.miniArray[1] = new Array();
this.miniArray[1][0] = 1;
for (var i=1; i<=length; i++)
if(this.sortArray[(i+1)]!=null){
	if(this.sortArray[i][2] == this.sortArray[(i+1)][2]&&this.sortArray[i][3] == this.sortArray[(i+1)][3]){
		this.miniArray[x][y] = this.sortArray[i]
		y++;
		}
		else{
		this.miniArray[x][y] = this.sortArray[i];
		y=1;
		x++;
		this.miniArray[x] = new Array();
		this.miniArray[x][0] = i+1;
		}
	}
	else{this.miniArray[x][y] = this.sortArray[i];}
}
// Get data out of _snapShotObj - it needs to be put into a 'sort friendly' array...
NonGenericObject.prototype.createSortArray = function(){
this.sortArray = new Array();
// convert results values into useful arrays temporarily for sorting...
this.sortArray[0] = new Array("buffer","no value","no value","no value","no value","no value")
for(var i=1;i<=this._snapShotObj._numberOfRowsOfResults;i++){
	this.sortArray[i] = new Array(this._snapShotObj.o_tablerow[i][1],this._snapShotObj.o_tablerow[i][2],this._snapShotObj.o_tablerow[i][3],this._snapShotObj.o_tablerow[i][4],this._snapShotObj.o_tablerow[i][5])
	}
}
// After sorting is completed, all the data needs to be put back into _snapShotObj ready for updating the View
NonGenericObject.prototype.putSortedDataBack = function(){
for(var i=1;i<=this._snapShotObj._numberOfRowsOfResults;i++){
	this._snapShotObj.o_tablerow[i][1] = this.sortArray[i][0]
	this._snapShotObj.o_tablerow[i][2] = this.sortArray[i][1]
	this._snapShotObj.o_tablerow[i][3] = this.sortArray[i][2]
	this._snapShotObj.o_tablerow[i][4] = this.sortArray[i][3]
	this._snapShotObj.o_tablerow[i][5] = this.sortArray[i][4]
	}
}
///////////////////////////////////////////////////////////////////////////////////////////// S O R T    P R O C E S S I N G    E N D S  //////////////////////////////////////
// find out whether the number of games played should be updated...
NonGenericObject.prototype.modifyPlayed = function(i,j,mirrorValue){
if(mirrorValue == 0){playedValue=0}// no data entered into row - played not updated
else if(mirrorValue == 1){playedValue=1}// first data entered into row - played updated
else if(mirrorValue == 2){playedValue=-1}// no data entered (but data entered before) - played updated by -1
else if(mirrorValue == 3){playedValue=0}// data entered - but not for the first time - played not updated
this._snapShotObj.o_tablerow[j][2] = Number(this._snapShotObj.o_tablerow[j][2]) + playedValue;
this._snapShotObj.o_tablerow[this.RightRowNum][2] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][2]) + playedValue;
}
NonGenericObject.prototype.modifyGoalDifference = function(i,j,mirrorValue){
// Goal difference values must be adjusted under certain conditions ...
if(mirrorValue == 2 || mirrorValue == 3){
	if(this._snapShotObj.i_itemLeftMirror[i] > this._snapShotObj.i_itemRightMirror[i]){
	// if boxes HAVE been edited before and the old left value was greater than the right, reset the local GD ...
	this._snapShotObj.o_tablerow[j][3] = Number(this._snapShotObj.o_tablerow[j][3]) - (Number(this._snapShotObj.i_itemLeftMirror[i]) - Number(this._snapShotObj.i_itemRightMirror[i]));
	this._snapShotObj.o_tablerow[this.RightRowNum][3] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][3]) + (Number(this._snapShotObj.i_itemLeftMirror[i]) - Number(this._snapShotObj.i_itemRightMirror[i]));
	}
	else{
	// if boxes HAVE been edited before and the old right value was greater than the left, reset the local GD ...
	this._snapShotObj.o_tablerow[j][3] = Number(this._snapShotObj.o_tablerow[j][3]) + (Number(this._snapShotObj.i_itemRightMirror[i]) - Number(this._snapShotObj.i_itemLeftMirror[i]));
	this._snapShotObj.o_tablerow[this.RightRowNum][3] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][3]) - (Number(this._snapShotObj.i_itemRightMirror[i]) - Number(this._snapShotObj.i_itemLeftMirror[i]));
	}
}
if(this._snapShotObj.i_itemLeft[i] == this._snapShotObj.i_itemRight[i]){
	// a draw - GD does not change...
	}
	else{
		if(this._snapShotObj.i_itemLeft[i] > this._snapShotObj.i_itemRight[i]){
		// left team wins
		var goalDifference = (this._snapShotObj.i_itemLeft[i] - this._snapShotObj.i_itemRight[i]);
		this._snapShotObj.o_tablerow[j][3] = Number(this._snapShotObj.o_tablerow[j][3]) + goalDifference;
		this._snapShotObj.o_tablerow[this.RightRowNum][3] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][3]) - goalDifference;
		}
		else{
		// right team wins
		var goalDifference = (this._snapShotObj.i_itemRight[i] - this._snapShotObj.i_itemLeft[i]);
		this._snapShotObj.o_tablerow[j][3] = Number(this._snapShotObj.o_tablerow[j][3]) - goalDifference;
		this._snapShotObj.o_tablerow[this.RightRowNum][3] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][3]) + goalDifference;
		}
	}
}
NonGenericObject.prototype.modifyGoalsFor = function(i,j,mirrorValue){
	if(mirrorValue == 2 || mirrorValue == 3){
		this._snapShotObj.o_tablerow[j][5] = Number(this._snapShotObj.o_tablerow[j][5]) - Number(this._snapShotObj.i_itemLeftMirror[i]);
		this._snapShotObj.o_tablerow[this.RightRowNum][5] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][5]) - Number(this._snapShotObj.i_itemRightMirror[i]);
		this._snapShotObj.o_tablerow[j][5] = Number(this._snapShotObj.o_tablerow[j][5]) + Number(this._snapShotObj.i_itemLeft[i]);
		this._snapShotObj.o_tablerow[this.RightRowNum][5] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][5]) + Number(this._snapShotObj.i_itemRight[i]);
	}
	else{
		if(mirrorValue != 0){
		this._snapShotObj.o_tablerow[j][5] = Number(this._snapShotObj.o_tablerow[j][5]) + Number(this._snapShotObj.i_itemLeft[i]);
		this._snapShotObj.o_tablerow[this.RightRowNum][5] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][5]) + Number(this._snapShotObj.i_itemRight[i]);
		}
	}
}
NonGenericObject.prototype.modifyPoints = function(i,j,mirrorValue){
	if(mirrorValue == 2 || mirrorValue == 3){
		if(this._snapShotObj.i_itemLeftMirror[i] > this._snapShotObj.i_itemRightMirror[i]){
		// if boxes HAVE been edited before and the old left value was greater than the right, change each by 3 points...
		this._snapShotObj.o_tablerow[j][4] = Number(this._snapShotObj.o_tablerow[j][4]) - 3;
		}
		else if(this._snapShotObj.i_itemLeftMirror[i] < this._snapShotObj.i_itemRightMirror[i]){
		// if boxes HAVE been edited before and the old right value was greater than the left, change each by 3 points...
		this._snapShotObj.o_tablerow[this.RightRowNum][4] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][4]) - 3;
		}
		else{
		// old values were a draw - so subtract 1 from each...
		this._snapShotObj.o_tablerow[j][4] = Number(this._snapShotObj.o_tablerow[j][4]) - 1;
		this._snapShotObj.o_tablerow[this.RightRowNum][4] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][4]) - 1;
		}
	}
	if(mirrorValue != 0){
		if(this._snapShotObj.i_itemLeft[i] == this._snapShotObj.i_itemRight[i] && this._snapShotObj.i_itemLeft[i]!=''){
		// a draw ...
		this._snapShotObj.o_tablerow[j][4] = Number(this._snapShotObj.o_tablerow[j][4]) + 1;
		this._snapShotObj.o_tablerow[this.RightRowNum][4] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][4]) + 1;
		}
		else if(this._snapShotObj.i_itemLeft[i] == ''){
		// user emptied the text box...do nothing
		}
		else if(this._snapShotObj.i_itemLeft[i] > this._snapShotObj.i_itemRight[i]){
		// left team wins
		this._snapShotObj.o_tablerow[j][4] = Number(this._snapShotObj.o_tablerow[j][4]) + 3;
		this._snapShotObj.o_tablerow[this.RightRowNum][4] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][4]);
		}
		else{
		// right team wins
		this._snapShotObj.o_tablerow[j][4] = Number(this._snapShotObj.o_tablerow[j][4]);
		this._snapShotObj.o_tablerow[this.RightRowNum][4] = Number(this._snapShotObj.o_tablerow[this.RightRowNum][4]) + 3;
		}	
	}
}
NonGenericObject.prototype.checkMirrors = function(i){
// check the mirror values to see if both check boxes are empty - 
// if they are - it means that scores have been entered for the first time         
if(this._snapShotObj.i_itemLeftMirror[i] == ""){
	if(this._snapShotObj.i_itemLeft[i] == ""){
		// no data entered into row
		return 0;
		}
		else{
		// first data entered into row
		return 1;
		}
	}
	else{
		if(this._snapShotObj.i_itemLeft[i] == ""){
		// no data entered (but data entered before)
		return 2;
		}
		else{
		// data entered - but not for the first time
		return 3;
		}
	}
}
NonGenericObject.prototype.matchFixturesOnRight = function(i){
for(var j=1;j<=this._snapShotObj._numberOfRowsOfResults;j++){
		// match names for the teams on the LHS of the fixtures list
	if(this._snapShotObj.i_itemNameRight[i] == this._snapShotObj.o_tablerow[j][1]){
		return j;
		}
	}
}
NonGenericObject.prototype.orderTable = function(){
//alert('orderTable')
}