

//mobilePhone is an OO class which models a mobile phone product from the Sonofon feed/webservice

function mobilePhone() 
{

} 

mobilePhone.prototype.name = "notdefined";
mobilePhone.prototype.manufacturer = "notdefined";
mobilePhone.prototype.formFactorID = "notdefined";
mobilePhone.prototype.description = "notdefined";
mobilePhone.prototype.shopUrl = "notdefined";
mobilePhone.prototype.featureIDsCommaSeparated = "notdefined";
mobilePhone.prototype.featureValuesSeparated = "notdefined";
mobilePhone.prototype.imageUrl = "notdefined";
mobilePhone.prototype.imageUrlThumbnail = "notdefined";
mobilePhone.prototype.price = "notdefined";
mobilePhone.prototype.priceTotalForBindingPeriod = "notdefined";

//returns an integer:
//  less than zero:    This mobilePhone is less than adslProductToCompare
//  zero:              This mobilePhone is equal to adslProductToCompare
//  greater than zero: This mobilePhone is greater than adslProductToCompare
mobilePhone.prototype.compareByName = function(mobileToCompare)
{
	if (this.name > mobileToCompare.name)
	{
		return 1;
	}
	else if (this.name < mobileToCompare.name)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}


mobilePhone.prototype.getFeatureValueByID = function(featureID)
{
	var resultValue = null;
	
	var featureValuesForPhone = this.featureValuesSeparated.split("###");
	var featureParts = null;
	for (var i = 0; i < featureValuesForPhone.length; i++)
	{
		featureParts = featureValuesForPhone[i].split("=");
		if (featureParts[0] == featureID)
		{
			resultValue = featureParts[1];
			break;
		}
	}

	return resultValue;
}


//mobilePhone is an OO class which models a list of mobile phone products

function mobilePhoneList() 
{
	this.mobilePhones = new Array()
}

mobilePhoneList.prototype.name = "notdefined";
mobilePhoneList.prototype.addPhone = function(phoneToAdd)
{
	this.mobilePhones[this.mobilePhones.length] = phoneToAdd;
}

mobilePhoneList.prototype.getPhoneByIndex = function(index)
{
	return this.mobilePhones[index];
}

mobilePhoneList.prototype.getPhoneByName = function(name)
{
	var matchedPhone = null;
	for (var k = 0; k < this.mobilePhones.length; k++)
	{
		if (this.mobilePhones[k].name == name)
		{
			matchedPhone = this.mobilePhones[k];
			break;
		}
	}
	return matchedPhone;
}

//--- sorts on name (performes a simple bubblesort) ---//
mobilePhoneList.prototype.sortByName = function(isSortDescending)
{
	var arrayToSort = this.mobilePhones;
	for (i = 0; i < arrayToSort.length ; i++ )
	{
		for (j = i + 1; j < arrayToSort.length ; j++)
		{
			if (isSortDescending)
			{
			//Sort descending (biggest number first)

				//If the current product is bigger than the one being compared, then swap them.
					if (arrayToSort[j].compare(arrayToSort[i]) > 0 ) 
					{
						swap(arrayToSort, j, i);
					}
			}
			else 
			{
			//Sort ascending (smallest number first)
				//If the current number/string is smaller than the one being compared, then swap them.
					if (arrayToSort[j].compare(arrayToSort[i]) < 0 )
					{
						swap(arrayToSort, j, i);
					}
			}
		}//next compare
	}//next element to bubble

}

function swap(theArray, index1, index2)
{
	var indexStore;
	//prevent index1 from being deleted, and swap the values
	indexStore = theArray[index1];
	theArray[index1] = theArray[index2];
	theArray[index2] = indexStore;
}

