function validateProductSelection(qtyMessage, optionMsg, form, minQty, maxValueStatus, fromInStorePickup, zipEmptyMsg, zipInvalidMsg, action) 
{
	if(document.getElementById("qtys") != null)
	{
		document.getElementById("qtys").style.visibility='hidden';
	}
	if(document.getElementById("combo_1") != null)
	{
		document.getElementById("combo_1").style.visibility='hidden';
	}
	if(document.getElementById("combo_2") != null)
	{
		document.getElementById("combo_2").style.visibility='hidden';
	}



//	alert("In validate");

	var iTotalQty = 0;
	var valueOfQuantity;
    var qtyErrorMsg = qtyMessage.toString();
    var optionErrorMsg = optionMsg.toString();
    var qtyValue = '';
    var optValue = '1=0;2=0';
    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;

	var errMessage=optionMsg;

	var qtyResult = true;
	var optionResult = true;
	var strInvalidOptions = "";
	var i1=0;

	var qtyValidArray = new Array();    
	var counter=0;

	// Iterate over the form and count the number of quantity fields and hide the exclamation marks
	// This is for BASKET page and NOT for DETAIL page
	var number_of_qty_fields=0;
	for(var i = 0; i < form.length; i++) 
	{
		var field = form.elements[i];

		if ((field.type == "text" || field.type == "select-one") && (field.name == "qty")) 
		{
			if(document.getElementById(field.id+"s") != null)
			{
				document.getElementById(field.id+"s").style.visibility='hidden';
			}
			number_of_qty_fields++;
		}

	}

//	alert("Number of qty fields is "+number_of_qty_fields);



    for(var i = 0; i < form.length; i++) 
	{
      
		var field = form.elements[i];

		if(field.type == "text" && field.name == "qty")
		{
			valueOfQuantity = field.value;
			var maxtemp=3;
			if(field.alt>0)
			{
				maxtemp=field.alt;
			}
			if(valueOfQuantity>maxtemp || valueOfQuantity<minQty)
			{
				if(document.getElementById(field.id+"s") != null)
				{
					document.getElementById(field.id+"s").style.visibility='visible';
				}
				qtyErrorMsg=qtyErrorMsg+" "+maxtemp+" for "+field.title;
				qtyResult = false;
				
				//Omniture Event
				
				SendOmniEvent("Quantity Error","event32","Error Message");
				
				//Omniture Event
			}
		}
	  // 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) ) 
			{
				if(document.getElementById(field.id+"s") != null)
				{
					document.getElementById(field.id+"s").style.visibility='visible';
				}
				var maxtemp=3;
				if(field.alt>0)
				{
					maxtemp=field.alt;
				}
				qtyErrorMsg=qtyErrorMsg+" "+maxtemp+" for "+field.title;
                qtyResult = false;
                
                //Omniture Event
				
				SendOmniEvent("Quantity Error","event32","Error Message");
				
				//Omniture Event
			}

        } 
		else if (field.type == "hidden" ) 
		{

            if(field.name == "option") 
			{
//			  alert("field value is "+ field.value);

/*			  if(field.value == "none")
			  {
				  field.value = "1=0:2=0";
			  }
			  if(field.value.indexOf(":") == -1)
			  {
				  var t = field.value.substring(0,1);
//				  alert("First character is "+t);


				  if(t == 1)
				  {
					  field.value = field.value+":2=0";
				  }
				  if(t == 2)
				  {
					  field.value = field.value+":1=0";
				  }
			  }
*/			
              optValue =  field.value;
			  
//			  alert("field value is "+ 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('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 ) 
					{
						errMessage = "select a kit";
						bValidationResult = 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;
            }
     }
//		alert("qtyvalid is "+qtyValid);
//		alert("iValue is "+iValue);
       // now check options selection
      // if (qtyValid) 
		{
           qtyValid = false;
           optTypeValue = optionTypes[m];
           optValue = options[m];
//		   alert("optTypeValue is "+optTypeValue);
           if (optTypeValue > 0) 
		   {
               // product has options so check if any selected
//			   alert("optValue is "+optValue);
               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++) 
						{
//						   alert(arr[n]);
                            subArr = arr[n].split("=");
                            if ((subArr[1] != 0)) 
							{
                                validOptions++;
                            }
							else
							{
//								alert("Invalid option");
								strInvalidOptions = strInvalidOptions + subArr[0]+";";
								
								//Comments temp Omniture Event 
				
				                //SendOmniEvent("Invalid Option","event32","Error Message");
                				
				                //Omniture Event
								
							}
                       }
                       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

    // validate quantity. On detail page, minQty is probably 1.
    // On basket, 0 is a valid qty -- means to remove
    if (iTotalQty < minQty) {
        //errMessage = qtyErrorMsg;
        qtyResult = false;
        
        //Omniture Event
				
		SendOmniEvent("Quantity Error","event32","Error Message");
				
		//Omniture Event
        
    }
	if (badOptions > 0) 
	{
         //errMessage = optionErrorMsg;
          optionResult = 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) ) 
		{
           //errMessage = optionErrorMsg;
           optionResult = false;
        } 
		else
		{
           optionResult = true;
        }
    }
    

//alert("qtyResult = "+qtyResult);
//alert("optionResult = "+optionResult);
//alert("bValidationResult ="+bValidationResult);
	var result = qtyResult && optionResult && bValidationResult;
//alert("Final Result ="+result);
	
	/*if(result == true)
	{
		if(fromInStorePickup == true) {
			var openIPop = true;
			validateZipCode(document.getElementById('zipCodeforPickUp'), zipEmptyMsg, zipInvalidMsg, action, openIPop);
		} else {
			form.submit();
		}
	}*/
	if(result == true)
	{
		if(fromInStorePickup == true) {
			validateZipCode(document.getElementById('zipCodeforPickUp'), zipEmptyMsg, zipInvalidMsg, action);
		} else {
			form.submit();
	}
	}
	else
	{
		if(qtyResult == false)
		{
			if(document.getElementById("qtys") != null)
			{
				document.getElementById("qtys").style.visibility='visible';
			}
		}
		if(optionResult == false)
		{
			var tempArr = strInvalidOptions.split(";");
//			alert("length of invalid options array is "+tempArr.length); 
			var counter =0;
			for(counter=0;counter<tempArr.length-1;counter++)
			{
//				alert(tempArr[counter]);
				if(document.getElementById("combo_"+tempArr[counter]) != null)
				{
					document.getElementById("combo_"+tempArr[counter]).style.visibility='visible';
				}
			}
			
		}
		if(optionResult == true && qtyResult == false)
		{
			errMessage = qtyErrorMsg;
		}
		showMessageBox(errMessage);
	}
  

}

/*
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) ) {
//        errMessage = 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;

}