var SC_Item = function (id)
{
  // initialize the member variables for this instance
  this.id = id;
  this.attributes = new Array();
  
  // initialize the member function references 
  // for the class prototype
  _SC_Item_prototype_called = true;
  SC_Item.prototype.getId = getId;
  SC_Item.prototype.setId = setId;
  SC_Item.prototype.getAllAttributes = getAllAttributes;
  SC_Item.prototype.setAllAttributes = setAllAttributes;
  SC_Item.prototype.getAttributeByName = getAttributeByName;
  SC_Item.prototype.getNumericAttributeValueByName = getNumericAttributeValueByName;
  SC_Item.prototype.extractNumbers = extractNumbers;
  SC_Item.prototype.addAttribute = addAttribute;
  SC_Item.prototype.delAttribute = delAttribute;


  // define a Item's method ID
  function getId()
  {
     return this.id;
  }
  function setId(value)
  {
     this.id = value;
  }
  //------------------------
  
  // define a Item's method ATTRIBUTES
  function getAllAttributes()
  {
     return this.attributes;
  }
  function setAllAttributes(value)
  {
     this.attributes = value;
  }
  //------------------------
  
  // define a Item's method ATTRIBUTES BY ID
  function getAttributeByName(name)
  {
     for (var i = 0; i<this.attributes.length;i++){
     	var attrib = this.attributes[i];
     	if (attrib.getName() == name) {
     		return attrib; 		
     	}
     }
  }
  //------------------------
  
  function getNumericAttributeValueByName(name){
  	 for (var i = 0; i<this.attributes.length;i++){
     	var attrib = this.attributes[i];
     	if  (name == 'bulbs' && attrib.getName() == name){
     		var minbs = "";
     		minbs = attrib.getValue().split(" ")[0];
     		if ( minbs != null ){
				var minb = parseInt(this.extractNumbers(minbs));
     			return minb +2;
 			}
	 		return -1;
 		} else if (attrib.getName() == name) {
 			return this.extractNumbers(attrib.getValue()); 	
 		}
     }
  }
  
  function extractNumbers(str){
 	try {
	  	var str2 = "";
	  	var strNumber = 0;
	  	for ( var i=0; i<str.length; i++){
			var strNumber = str.charAt(i);
			if( IsNumeric(strNumber)){
				str2 += strNumber;
			}
	  	}
	  	return parseFloat(str2);
  	} catch ( e) {}
  }
  
  function IsNumeric(sChar)
  {
	var ValidChars = "0123456789.";
	var IsNumber=true;
	if (ValidChars.indexOf(sChar) == -1) 
	{
		return false;
	}
	return true;
  }





  
  
  
  // define a Item's method ADD ATTRIBUTES
  // if the attribute exist it changes the value
  function addAttribute(id, name, value, uniqueValue)
  {
  	var i=0;
  	value = ""+value;
  	if ( name == "Price"){
  		var justPrice = value;
		justPrice = justPrice.split('$')[1]
		while(justPrice.length<9){
			justPrice = "0" + justPrice;
		}
		value = "$"+justPrice;
	}
	value = value.toUpperCase();
    for ( i = 0; i<this.attributes.length;i++){
     	var attrib = this.attributes[i];
     	if (attrib.getName() == name) {
			attrib.setValue(value);
			attrib.setUnique(uniqueValue);
     		return 0; 		
     	}
    }
     var newAttrib = new SC_ItemAttribute(id,name, value, uniqueValue);
     this.attributes[this.attributes.length] = newAttrib;
     return 1;
  }
  //------------------------
  
  //---------DELETE ATTRIBUTE--------------
  function delAttribute(name){
	for (var i = 0; i<this.attributes.length;i++){
     	var attrib = this.attributes[i];
     	if (attrib.getName() == name) {
			this.attributes.splice(i,1);; 		
			return 0;
     	}
    }  	
    return 1;
  }
  //----------------------------------------

}
