function validateKitProductSelection(qtyMessage, optionMsg, form, minQty) {
	var iTotalQty = 0;
    var qtyErrorMsg = qtyMessage.toString();
    var optionErrorMsg = optionMsg.toString();
    var qtyValue = '';
    var optValue = '';
    var optTypeValue = '';
    var selectedKitItems = '';
    var iValue;
    var qty = new Array();
    var optionTypes = new Array();
    var options = new Array();
    var productWithNoOptions = 0;
    var productWithOptions = 0;
    var validOptions = 0;
    var j = 0;
    var badOptions = 0;
    var qtyValid = false;
    var isKit = false;
    var productPk = "";
    var bValidationResult = true;
	
/*	for(var i = 0; i < form.length; i++) 
	{
		var field = form.elements[i];
		if(field.name.indexOf("kitElementSelectOption")==0)
		{
			alert(field.name + ": " + field.value);
		}
	}
*/	
	
	for(var i = 0; i < form.length; i++) {

        var field = form.elements[i];

		// alert( ' field name: ' + field.name + ', field value ' + field.value + ' field type ' + field.type);
        if ((field.type == "text" || field.type == "select-one") && (field.name == "qty")) 
		{

            qtyValue = (field.type == "text")? field.value:field[field.selectedIndex].value;
            if( isNotDigitsPrompt(field, qtyValue, qtyMessage) ) 
			{
                return false;
            }

        } 
		else if (field.type == "hidden" ) 
		{
            if(field.name == "option") 
			{
              optValue =  field.value;
            } 
			else if (field.name == "optionTypes") 
			{
              optTypeValue = field.value;
            } 
			else if (field.name == "qty") 
			{
              qtyValue = field.value;
		      isKit = true;
			}
		// make sure that any javascript attached to a radio button gets triggered if its checked/selected.
        } else if (field.type == "radio" && field.checked){
            field.click();
        }

		if ( field.name == "productPk" ) {
			productPk = field.value;
	  	}
		if ( productPk.length > 0 && isKit  ) {
		 	var fieldName =  "document.getElementById('addToBasketForm').selectedKitItems_" + productPk;
			obj = eval( fieldName);
				if ( obj.value == undefined ) {
					var k=0;
					for ( k=0; k < obj.length ; k++ ) {
						if ( obj[k].checked ) {
							break;
						}
					}

					if ( k == obj.length ) {
						alert('select a kit');
						return false;
					} else {
					    bValidationResult = validateKitSingleProductOptions(productPk);
					}
				}
	  	}

        // if all fields values are collected, store them into the arrays
        if (qtyValue != "" && optValue != "" &&  optTypeValue != "") {
        // alert(" qtyValue " + qtyValue + " optValue " + optValue + " optTypeValue " + optTypeValue );
          qty[j] = qtyValue;
          options[j] = optValue;
          optionTypes[j] = optTypeValue;
          j++;
          optTypeValue = ""; qtyValue = ""; optValue = "";
        }
    }

	
      // alert("qty.length " + qty.length + " options.length " + options.length + " optionTypes.length " + optionTypes.length + " j " + j);
      // go through the arrays and validate
    for (var m = 0; m < j; m++) {
          // check qty
        qtyValue = qty[m];

        if (isAllDigits(qtyValue)) {
            iValue = parseInt(qtyValue);
            if (!isNaN(iValue)) {
                iTotalQty += iValue;
                qtyValid = true;
            }
        }
       // now check options selection
       if (qtyValid && (iValue > 0)) {
           qtyValid = false;
           optTypeValue = optionTypes[m];
           optValue = options[m];
           if (optTypeValue > 0) {
               // product has options so check if any selected
               if(optValue != "none") {
                   // something was selected
                   // now check that number of options corresponds to selection
                   arr = optValue.split(":");
                   if (arr.length == optTypeValue) {
                       // got the same number of options as the number of drop-down menus
                       // now check if a zero value is selected for each one of them
                       for(var n = 0; n < arr.length; n++) {
                            subArr = arr[n].split("=");
                            if ((subArr[1] != 0)) {
                                validOptions++;
                            }
                       }
                       if (validOptions == optTypeValue) {
                           productWithOptions++;
                       } else {
                           badOptions++;
                       }
                       // reset var
                       validOptions = 0;
                   } else {
                       badOptions++;
                   }
               } else {
                   badOptions++;
               }
           } else {
               // product has no options, increment counter.
               productWithNoOptions++;
           }
       } // end if valid qty and qty > 0
    } // end for
	var dropDownSelectionValidatedFlag = validateDropDowns(form);

	// validate quantity. On detail page, minQty is probably 1.
    // On basket, 0 is a valid qty -- means to remove
    if (iTotalQty < minQty) {
        alert(qtyErrorMsg);
        return false;
    } else {
        if (badOptions > 0) {
          alert(optionErrorMsg);
          return false;
        } else {
            // if nothing was selected for product with options and
            // there is no product without options
            // and there is a qty greater than minQty then there's an error
            if ( (productWithOptions < 1) && (productWithNoOptions < 1) && (iTotalQty > minQty) ) {
               alert(optionErrorMsg);
               return false;
            } else {
               return true && dropDownSelectionValidatedFlag;
            }
        }
    }
	
    return bValidationResult && dropDownSelectionValidatedFlag;

}

/*
function validateMinimumQuantity(dropDownObject, minQty)
{
	
}
*/

function isNotDigitsPrompt(field, qtyValue, qtyMessage) {
    if ( qtyValue == "" ) {
        field.value = "0";
        return false;
    } else if ( !isAllDigits(qtyValue) ) {
        alert(qtyMessage);
        return true;
    }
}
function isAllDigits(argvalue) {
   argvalue = argvalue.toString();
   if(!validateNotEmpty(argvalue)) return false;

   if(!validatePositiveInt(argvalue)) return false;

   return true;
}

function validateInteger( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}

function validatePositiveInt( strValue ) {
  var objRegExp  = /(^\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}

function validateNotEmpty( strValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}

function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function validateKitSingleProductOptions( productPk ) {
    // locate error message
    var selectedSkuFieldName =  "document.getElementById('mainForm').selectedKitSku";
    var selectedSkuFieldObj =  eval( selectedSkuFieldName );
    var selectedSkuPk = selectedSkuFieldObj.value;
    var errorMessageFieldName = "";
    var errorMessageFieldObj;
    var notDone = 1;

    for ( var j = 0; notDone == 1 ; j++ ) {
        errorMessageFieldName = "document.getElementById('mainForm').errorMessage_" +  productPk + "_" + selectedSkuPk + "_" + j;
        errorMessageFieldObj = eval( errorMessageFieldName );
        if ( errorMessageFieldObj != undefined ) {
            if ( errorMessageFieldObj.value != "" ) {
                alert(errorMessageFieldObj.value);
                notDone = 0;
                return false;
            }
        } else {
            notDone = 0;
        }
    }
    return true;

}

//declaring parentKitSku
var newParentKitSku="";
var oldParentKitSku="";

// Function for updating the hidden options field.
  function optionTypeValuesForKitConfigurationForm(oSelectObj)
  {
	
  	newParentKitSku = dropDownArray[0][oSelectObj.selectedIndex-1];
	//change the name of the kit element dropdowns that contain product list
	//alert("Old SKU Code: " + oldParentKitSku + "\n" + "New SKU Code: " + newParentKitSku);
	for(i=0; i<document.forms[2].elements.length; i++)
	{
		if(document.forms[2].elements[i].name.indexOf("kitElementSelectOption_")==0)
		{
			document.forms[2].elements[i].name = document.forms[2].elements[i].name.replace(oldParentKitSku, newParentKitSku);
			//alert(document.forms[2].elements[i].name)
			oldParentKitSku = newParentKitSku;
		}
	}
	///alert(parentKitSku)
  	addToBasketForm.selectedKitItems.value = oSelectObj.value;
    // string for holding the select form element's name.

	var sTypeName = oSelectObj.name;

    // object ref which points to the related options hidden field.
    var oOptionTypeValues = eval("document.getElementById('addToBasketForm').optionTypeValues_"+(sTypeName.split("_"))[1]);

    // string for holding the target field's ID.
    var sTargetFieldID = oOptionTypeValues.id;

    // string for holding the concatenated value of the optionTypePk and the optionPk.
    var sOptionTypeValue = (sTypeName.split("_"))[2] + "=" + oSelectObj[oSelectObj.selectedIndex].value;

    // update the MAIN_OPTIONS object to reflect additions or updates
    update_MAIN_OPTIONS(sTargetFieldID, sTypeName, sOptionTypeValue);

    // update the target field's value, for posting the data.
    oOptionTypeValues.value = MAIN_OPTIONS[sTargetFieldID].join(":");
	// ("oOptionTypeValues.value:" + oOptionTypeValues.value )

  }