function Consignment(consignmentId, sellerNumber, consignmentName, priceIncrement, hasDiscountFieldDisabled, hasDonateFieldDisabled ) {
   this.consignmentId = consignmentId;
   this.sellerNumber = sellerNumber;
   this.consignmentName = consignmentName;
   this.priceIncrement = priceIncrement;
   this.hasDiscountFieldDisabled = hasDiscountFieldDisabled;
   this.hasDonateFieldDisabled = hasDonateFieldDisabled;
}


function priceChanged( element ) {
   var priceField = element.form.elements['price'];
   priceField.value = element.value;
}

function setPrice( form ) {

  var consignment = getConsignment( form.elements['consignmentId'].value );

   if ( consignment!= null ) {

	  if ( consignment.priceIncrement == "NA" ) {
	     form.elements["price"].value = form.elements["textNa.price"].value;

	  } else if ( consignment.priceIncrement == "25" ) {
	     form.elements["price"].value = form.elements["select25.price"].value;

	  } else if ( consignment.priceIncrement == "50" ) {
	     form.elements["price"].value = form.elements["select50.price"].value;

	  } else if ( consignment.priceIncrement == "75" ) {
	     form.elements["price"].value = form.elements["select75.price"].value;
	  } else if ( consignment.priceIncrement == "1") {
	     form.elements["price"].value = form.elements["select1.price"].value;
	  }

   }

}

function displayItems( element ) {

    var consignment = getConsignment( element.value );

    if ( consignment != null ) {
       element.form.elements["sellerNumber"].value = consignment.sellerNumber;
       element.form.elements["consignmentName"].value = consignment.consignmentName;

    } else {
       element.form.elements["sellerNumber"].value = "";
       element.form.elements["consignmentName"].value = "";
    }

    if ( consignment != null ) {
       var discountField = element.form.elements['discountAllowed'];
       if ( consignment.hasDiscountFieldDisabled == "true" ) {
	  discountField.checked = false;
	  discountField.disabled = true;
	  element.form.elements['isDiscountFieldDisabled'].value = true;
       }

       if (consignment.hasDiscountFieldDisabled == "false" ) {
	  discountField.checked = false;
	  discountField.disabled = false;
	  element.form.elements['isDiscountFieldDisabled'].value = false;
       }
       var donateField = element.form.elements['donate'];
       if ( consignment.hasDonateFieldDisabled == "true" ) {
	  donateField.checked = false;
	  donateField.disabled = true;
	  element.form.elements['isDonateFieldDisabled'].value = true;
       }

       if (consignment.hasDonateFieldDisabled == "false" ) {
	  donateField.checked = false;
	  donateField.disabled = false;
	  element.form.elements['isDonateFieldDisabled'].value = false;
       }


    }

    if ( consignment!= null ) {

       if ( consignment.priceIncrement == "NA" ) {
	  document.getElementById("priceNA").style.display = "none";
	  document.getElementById("price25").style.display = "none";
	  document.getElementById("price50").style.display = "none";
	  document.getElementById("price75").style.display = "none";
  	  document.getElementById("price1").style.display = "none";

	  document.getElementById("priceNA").style.display = "";
	  element.form.elements["priceIncrement"].value = consignment.priceIncrement;
	  element.form.elements["price"].value = element.form.elements["textNa.price"].value;

       } else if ( consignment.priceIncrement == "25" ) {
	  document.getElementById("priceNA").style.display = "none";
	  document.getElementById("price25").style.display = "none";
	  document.getElementById("price50").style.display = "none";
	  document.getElementById("price75").style.display = "none";
	  document.getElementById("price1").style.display = "none";

	  document.getElementById("price25").style.display = "";
	  element.form.elements["priceIncrement"].value = consignment.priceIncrement;
	  element.form.elements["price"].value = element.form.elements["select25.price"].value;

       } else if ( consignment.priceIncrement == "50" ) {
	  document.getElementById("priceNA").style.display = "none";
	  document.getElementById("price25").style.display = "none";
	  document.getElementById("price50").style.display = "none";
	  document.getElementById("price75").style.display = "none";
	  document.getElementById("price1").style.display = "none";

	  document.getElementById("price50").style.display = "";
	  element.form.elements["priceIncrement"].value = consignment.priceIncrement;
	  element.form.elements["price"].value = element.form.elements["select50.price"].value;

       } else if ( consignment.priceIncrement == "75" ) {
	  document.getElementById("priceNA").style.display = "none";
	  document.getElementById("price25").style.display = "none";
	  document.getElementById("price50").style.display = "none";
	  document.getElementById("price75").style.display = "none";
	  document.getElementById("price1").style.display = "none";

	  document.getElementById("price75").style.display = "";
	  element.form.elements["priceIncrement"].value = consignment.priceIncrement;
	  element.form.elements["price"].value = element.form.elements["select75.price"].value;
       } else if ( consignment.priceIncrement == "1" ) {
	   document.getElementById("priceNA").style.display = "none";
	   document.getElementById("price25").style.display = "none";
	   document.getElementById("price50").style.display = "none";
	   document.getElementById("price75").style.display = "none";
	   document.getElementById("price1").style.display = "none";

	   document.getElementById("price1").style.display = "";
	   element.form.elements["priceIncrement"].value = consignment.priceIncrement;
	   element.form.elements["price"].value = element.form.elements["select1.price"].value;
       }

    }

}

function enableFields( form ) {

    if ( bCancel ) {
      return true;
    }

   var consignmentName = form.elements['consignmentId'].value;
   var priceInc = form.elements['priceIncrement'].value;
   if ( consignmentName == null || consignmentName.length == 0 ) {
     alert("Consignment cannot be empty");
     form.elements['consignmentId'].focus();
     return false;
   }

     var priceValue = form.elements['price'].value;

     if ( priceValue == null || priceValue.length == 0 ) {
      alert("The Price field cannot be empty");
      var consignmentId = form.elements['consignmentId'].value;
	   form.elements['price'].focus();	
	     return false;
	   }

//   var description = form.elements['description'].value;
//   if ( description == null || description.length == 0 ) {
//     alert("Description 1 cannot be empty");
//     form.elements['description'].focus();
//     return false;
//   }

    var priceRegEx = /^([0-9]{0,5})?([.][0-9]{1,2})?$/;
   
   //var priceRegEx = /(\\d{0,5})([.]\\d{1,2})?$/;
   if (!priceRegEx.test(priceValue)) {
     alert("The price field needs to be a number with no comas and two decimal places.");
     return false;
   }
   
   var newPriceVal = parseFloat(priceValue);
   if (priceInc == "NA" && (newPriceVal < minimumPriceValue)) {
   
      if (allowZeroValue && newPriceVal > 0) {
         alert("The minimum price allowed is: " + minPriceMsg);
         form.elements['price'].focus();
         return false;
      } 
      
      if (!allowZeroValue) {
         alert("The minimum price allowed is: " + minPriceMsg);
         form.elements['price'].focus();
         return false;      
      }
      
      
      
   } 
   
   var newMaxPriceVal = parseFloat(priceValue);
   if (priceInc == "NA" && (newMaxPriceVal > maximumPriceValue)) {
      alert("The maximum price allowed is: " + maxPriceMsg);
      form.elements['price'].focus();
      return false;
   }    
   

   if ( priceValue < .01 && !confirm( "You have entered a price with a $0.00. \r Is this correct?" ) ) {
      return false;
   }   
   
   
   form.elements["price"].value = priceValue;
   
   var elements = form.elements;

   for ( var i = 0; i < elements.length; ++i ) {
      elements[ i ].disabled = false;
   }

}


function selectDeselectItems( checkbox ) {

   for ( var i = 0; i < checkbox.form.elements.length; ++i ) {
      var elementName = checkbox.form.elements[ i ].name;

      if (elementName == "selectedItems" ) {
	 checkbox.form.elements[ i ].checked = checkbox.checked;
      }

   }

}

function checkAll( element ) {

   var mForm = element.form;

   mForm.elements["selectAll"].checked = true;

   for ( var i = 0; i < mForm.elements.length; ++i ) {

      var elementName = mForm.elements[ i ].name;

      if (elementName == "selectedItems" ) {
	 mForm.elements[ i ].checked = true;
      }

   }

}

function clearAll( element ) {

   var mForm = element.form;

   mForm.elements["selectAll"].checked = false;


   for ( var i = 0; i < mForm.elements.length; ++i ) {

      var elementName = mForm.elements[ i ].name;

      if (elementName == "selectedItems" ) {

	 mForm.elements[ i ].checked = false;
      }

   }

}

function checkRemoveSelected( element ) {

   var mForm = element.form;
   var selectedItems = 0;

   for ( var i = 0; i < mForm.elements.length; ++i ) {

      if (mForm.elements[ i ].name == "selectedItems" ) {

	  if ( mForm.elements[ i ].checked ) {
	     ++selectedItems;
	  }

      }

   }

   if (selectedItems == 0) {
      alert("Please select at least one Item to Remove");
      return false;
   } else {
     return confirmDelete();
   }

}

function checkEditSelected( element ) {

   var mForm = element.form;
   var selectedItems = 0;

   for ( var i = 0; i < mForm.elements.length; ++i ) {

      if (mForm.elements[ i ].name == "selectedItems" ) {

	  if ( mForm.elements[ i ].checked ) {
	     ++selectedItems;
	  }

      }

   }

   if (selectedItems == 1) {
      return true;
   }

   if (selectedItems > 1 ) {
    alert("You can only Edit one Item at a time");
    return false;
   }

   if (selectedItems == 0) {
      alert("Please select one Item to edit");
      return false;
   }

}

function confirmDelete() {
    return confirm("Do you want to permanently delete this Item from your inventory?");
}

function getConsignment( consignmentId ) {

   for ( var i = 0; i < consignments.length; ++i ) {

      if ( consignments[ i ].consignmentId == consignmentId ) {
	 return consignments[i];
      }

   }

   return null;
}


function checkDonateFieldDonateDiscountRules() {

    var forceDisWhenDonValue = $('[name=forceDiscWhenDonValue]').val();

    if (forceDisWhenDonValue == "true") {
        
    	if ($('[name=discountFieldRule] option').size() > 1) {
        	if ($('[name=donateFieldRule]').val() == "DY") {
         	   $('[name=discountFieldRule] option:eq(0)').remove();
         	   $('[name=discountFieldRule] option:eq(1)').remove();
         	}    		
        }
    	
    	if ($('[name=discountFieldRule] option').size() == 1) {
        	if ($('[name=donateFieldRule]').val() == "DN" || $('[name=donateFieldRule]').val() == "SC") {
        		$('[name=discountFieldRule]').prepend("<option value='SC'>No Default - Remembers Seller Choice</option>");
        		$('[name=discountFieldRule]').append("<option value='DN'>Default No</option>");        		
         	}    		
        }
    	
    }
    

}

function checkDonateDiscountRules(element) {

    var forceDisWhenDonValue = $('[name=forceDiscWhenDonValue]').val();

    if (forceDisWhenDonValue == "true") {
        
    	// Remove Force options
        $('[name=donateFieldRule] option:eq(0)').remove();
        $('[name=donateFieldRule] option:eq(0)').remove();
        
        // Remove Force options
    	$('[name=discountFieldRule] option:eq(0)').remove();
    	$('[name=discountFieldRule] option:eq(0)').remove();
    	
    	// Remove DN and SC from Discount Field
    	if ($('[name=donateFieldRule]').val() == "DY") {
    	   $('[name=discountFieldRule] option:eq(0)').remove(); // SC Option
    	   $('[name=discountFieldRule] option:eq(1)').remove(); // DN Option
    	}
    	
    }
    
    if (forceDisWhenDonValue == "false") {
        
        $('[name=discountFieldRule]').prepend("<option value='FN'>Force No - Block Seller from Changing</option>");
        $('[name=discountFieldRule]').prepend("<option value='FY'>Force Yes - Block Seller from Changing</option>");
        
        var foundScOption = false;
        $('[name=discountFieldRule] option').each(function() { 
        	if ($(this).val() == "SC") {
        		foundScOption = true;
        	}
        })
        
        if (foundScOption == false) {        	
        	$('[name=discountFieldRule] option:eq(2)').before("<option value='SC'>No Default - Remembers Seller Choice</option>");	        		
        }
        
        var foundOption = false;
        $('[name=discountFieldRule] option').each(function() { 
        	if ($(this).val() == "DN") {
        		foundOption = true;
        	}
        })

        if (foundOption == false) {
        	$('[name=discountFieldRule]').append("<option value='DN'>Default No</option>");	        		
        }
        
        $('[name=donateFieldRule] option:eq(0)').before("<option value='FN'>Force No - Block Seller from Changing</option>");
        $('[name=donateFieldRule] option:eq(0)').before("<option value='FY'>Force Yes - Block Seller from Changing</option>");                
        
    }
}
