function validateProductSelection(qtyMessage, optionMsg, form, minQty) {

	var formObj = document.getElementById("mainForm");
	if(formObj.saveCartButton.value=='true')
		return false;

	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];
	  //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;
            }
			//alert("insaide qty if"+qtyValue);

        } else if (field.type == "hidden" ) {
            if(field.name == "option") {
              optValue =  field.value;
			  //alert("optValue is this:"+optValue+" && i="+i);
            } else if (field.name == "optionTypes") {
              optTypeValue = field.value;
			  //alert("optTypeValue:="+optTypeValue);
            } else if (field.name == "qty") {
              qtyValue = field.value;
		      isKit = true;
		    }

			//alert("in qrt againb");

		// 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('mainForm').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
         //alert(" qtyValue " + qtyValue + " optValue " + optValue + " optTypeValue " + optTypeValue );
		if (qtyValue != "" && optValue != "" &&  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];
		//alert("qtyValue  "+qtyValue);

        if (isAllDigits(qtyValue)) {
            iValue = parseInt(qtyValue);
            if (!isNaN(iValue)) {
                iTotalQty += iValue;
		//alert("iTotalQty  "+iTotalQty);
                qtyValid = true;
            }
        }

		//alert("1");
       // now check options selection
       if (qtyValid && (iValue > 0)) {
           qtyValid = false;
           optTypeValue = optionTypes[m];
           optValue = options[m];
           //alert("1a");
           if (optTypeValue > 0) {
               // product has options so check if any selected
               //alert("optValue:="+optValue);
               if(optValue != "none") {
                   // something was selected
                   // now check that number of options corresponds to selection
                   arr = optValue.split(":");
				   //alert("arr.length:="+arr.length+" && optTypeValue:="+optTypeValue);
                   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++;
                            }
                       }
					   //alert("validOptions:="+validOptions+" && optTypeValue:="+optTypeValue);
                       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
	//alert("2:iTotalQty:="+iTotalQty+" && minQty:="+minQty);
    // 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 {
       // alert("badOptions:="+badOptions);
        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;
            }
        }
    }
   // alert("3");
    return bValidationResult;

}

/*
function isAllDigits(argvalue) {
   argvalue = argvalue.toString();
   if(argvalue.length < 1) return false;

    var validChars = "0123456789";
    var startFrom = 0;
    if (argvalue.substring(0, 2) == "0x") {
       validChars = "0123456789abcdefABCDEF";
       startFrom = 2;
    } else if (argvalue.charAt(0) == "0") {
       validChars = "01234567";
       startFrom = 1;
    } else if (argvalue.charAt(0) == "-") {
        startFrom = 1;
    }

    for (var n = startFrom; n < argvalue.length; n++) {
        if (validChars.indexOf(argvalue.substring(n, n+1)) == -1) return false;
    }
    return true;
}
*/
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;

}