
// This variable is introduced to identify if the form has already been submitted for some action
// and to prevent any other action on the form till the page is refreshed.
var isFormSubmitted = false;

function AddProductToCart() {

	if (ControlToSubmit == "loginWidgetSubmit") {

		if (loginWidgetSetFocus())
			ControlToSubmit = "";

		return false;
	}
	
    if (isFormSubmitted)
        return false;
    if (!ValidatePersonalization())
        return false;
    if (!ValidateProductQuantity())
        return false;
    if (!ValidateProductSKUs())
        return false;

    isFormSubmitted = true;
    return true;
}

function ShowErrorMessage(message)
{
	var errorMessage;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) 
	{
		errorMessage = document.all["ProductErrorMessage"];
	}
	else 
	{
		errorMessage = document.getElementById("ProductErrorMessage");
	}
	
	errorMessage.innerHTML = message;
}

function AddProductToWishList() {
    if (isFormSubmitted)
        return false;

    if (!ValidatePersonalization())
        return false;
        
    if (!ValidateProductQuantity())
        return false;
    if (!ValidateProductSKUs())
        return false;

    isFormSubmitted = true;
    return true;
    //__doPostBack('AddToWishList:EventHandler','');
}

function AddProductToKit()
{
	if(isFormSubmitted)
		return false;	

	if(! ValidateProductQuantity())
		return false;
	if(! ValidateProductSKUs())	
		return false; 
		
	isFormSubmitted = true;
	return true;
}

function ValidateProductQuantity()
{
	if(isFormSubmitted)
		return false;	
	var qty = document.forms[0].elements["Quantity"].value;
	if(qty == "" || qty <= 0 || ! isNumeric(qty))
	{
		alert("Please enter a valid quantity.");
		document.forms[0].elements["Quantity"].focus();
		return false;
	}
	
	var minQty = parseInt(document.forms[0].elements["ProductValidationData$MinQty"].value,10);
	var maxQty = parseInt(document.forms[0].elements["ProductValidationData$MaxQty"].value,10);
	
	var inputQty = trim(qty)-0;
		
	if(inputQty >= minQty)
	{
		if(inputQty > maxQty)
		{
			alert("The entered quantity is more than the maximum quantity ("+maxQty+") allowed. You must edit the Quantity before you can continue.");
			document.forms[0].elements["Quantity"].focus();
			return false;
		}
	}
	else
	{
		alert("The entered quantity is less than the minimum quantity ("+minQty+") allowed. You must edit the Quantity before you can continue.");
		document.forms[0].elements["Quantity"].focus();
		return false;
	}
	
	return true;
}

function ValidateProductSKUs()
{
	if (ControlToSubmit == "txtSearchKeyword")
		return true;
		
	var bIsModelSeries = document.forms[0].elements["ProductValidationData$IsModelSeriesItem"].value;
				
	if(bIsModelSeries == "true")
	{
						
		for(var i=0;i<3;i++)
		{
			var AttID = "AttributeValue" + i + "_QProductList";
			if(document.forms[0].elements[AttID])
			{
				var selectedAttVal = document.forms[0].elements[AttID].value;
				if(selectedAttVal == "0")
				{
					var AttNameID = "ModelAttribute" + i + "_AttName";
					var selectAttName = document.forms[0].elements[AttNameID].value;
					alert("Please select " + selectAttName);
					document.forms[0].elements[AttID].focus();
					return false;
				}
			}
		}
	}
	return true;
} 

function VerifyProductSKUandSubmit(e,ClientID)
{
	if(isFormSubmitted)
		return false;	
		
	
	for(var i=0;i<3;i++)
	{
		var AttID = "AttributeValue" + i + "_QProductList";
		if(document.forms[0].elements[AttID])
		{
			var selectedAttVal = document.forms[0].elements[AttID].value;
			if(selectedAttVal == "0")
			{
				//return false;
			}
		}/*isFormSubmitted = true;*/
	}
	if (window.event) e = window.event;
	var srcEl = e.srcElement ? e.srcElement : e.target;
	if (srcEl.tagName == "SELECT") {
	
//	    if(event.srcElement.value != "0" )
//	    {
	    __doPostBack(ClientID, srcEl.value + "," + srcEl.id);
//	    }
	}
	else
	{
	    __doPostBack(ClientID,'');
	}
}
function HandleOnblurOfStyleItemQuantity()
{

    if(isFormSubmitted)
		    return false;	
	for(var i=0;i<3;i++)
	{
		var AttID = "AttributeValue" + i + "_QProductList";
		if(document.forms[0].elements[AttID])
		{
			var selectedAttVal = document.forms[0].elements[AttID].value;
			if(selectedAttVal == "0")
			{
				return false;
			}
		}
	}
	
	
    var oHiddenField = document.forms[0].elements["hdnRequestedQty"];
    var oQtyField    = event.srcElement;
    if(oHiddenField && oQtyField && parseInt(oHiddenField.value) != parseInt(oQtyField.value))
    {
       isFormSubmitted = true;	 
       __doPostBack('QuantityChanged','');
    }
   
}
function HandleOnblurOfItemQuantity()
{
   var oHiddenField = document.forms[0].elements["hdnRequestedQty"];
   var oQtyField    = event.srcElement;
   if(oHiddenField && oQtyField && parseInt(oHiddenField.value) != parseInt(oQtyField.value))
   {
        __doPostBack('QuantityChanged','');
   }
   
}
function VerifyProductSKUandSubmit_Old()
{
	if(isFormSubmitted)
		return false;	
		
	var noOfAtt = document.forms[0].elements["NoOfAttributes"].value;
			
	for(var i=0;i<noOfAtt;i++)
	{
		var AttID = "ChrAtt_" + i;
		var selectedAttVal = document.forms[0].elements[AttID].value;
										
		if(selectedAttVal == "")
		{
			return false;
		}
	}

	isFormSubmitted = true;	

	__doPostBack('btnProductAttSelected','');
}

//---------------------------------------------------------------//
//			Javascript Functions for the Item Controls			 //
//---------------------------------------------------------------//

//Define the Item object.
function QItem()
{
	this.Id			= null;
	this.MinQty		= 1;
	this.MaxQty		= 1;
	this.IsModel	= false;
	
	//Client side IDs of product controls
	this.QtyControl_ID = "";

	this.SKU_Details	= new Array();
}

//Define the SKU Detail object.
function SKUDetailObject()
{
	this.SKUControl_ID = "";
	this.SKU_Label = "";
}

//ValidateItem function validates the item quantity
//and the item SKUs. This function is called before
//adding the item to the shopping bag or to wishlist.
function ValidateItem()
{
	if(isFormSubmitted)
		return false;	

	if(!ValidateItemQuantity())
		return false;
	
	if(!ValidateItemSKUs())	
		return false; 
	
	isFormSubmitted = true;
	return true;
}

//ValidateItemQuantity function ensures that the 
//item quantity entered is of proper type and is
//within the min max limits.
function ValidateItemQuantity()
{
	if(objItem == null)
		return false;
	
	//get the item qty
	var qtyControl	= document.forms[0].elements[objItem.QtyControl_ID];
	var qty			= qtyControl.value;
	if(qty == "" || qty <= 0 || ! isNumeric(qty))
	{
		alert("Please enter a valid quantity.");
		qtyControl.focus();
		return false;
	}
	
	//get the min-max qty
	var minQty	 = objItem.MinQty;
	var maxQty	 = objItem.MaxQty;
	var inputQty = trim(qty)-0;
		
	if(inputQty >= minQty)
	{
		if(inputQty > maxQty)
		{
			alert("The entered quantity is more than the maximum quantity ("+maxQty+") allowed. You must edit the Quantity before you can continue.");
			qtyControl.focus();
			return false;
		}
	}
	else
	{
		alert("The entered quantity is less than the minimum quantity ("+minQty+") allowed. You must edit the Quantity before you can continue.");
		qtyControl.focus();
		return false;
	}
	
	return true;
}

//ValidateItemSKUs function ensures that the 
//different SKUs of item are properly selected.
function ValidateItemSKUs()
{
	if(objItem == null)
		return false;

	if(objItem.IsModel)
	{
		for(var i=0; i<objItem.SKU_Details.length; i++)
		{
			var skuControl = document.forms[0].elements[objItem.SKU_Details[i].SKUControl_ID];
			var skuLabel   = objItem.SKU_Details[i].SKU_Label;
			
			if(skuControl.value ==  "0")
			{
				alert("Please select " + skuLabel + ".");
				skuControl.focus();
				return false;
			}	
		}
	}
	
	return true;
}

//ValidateItemSKUs function ensures that the 
//different SKUs of item are properly selected.
function ValidateItemSKUsAndSubmit()
{
	if(isFormSubmitted)
		return false;	

	if(objItem == null)
		return false;
	
	if(objItem.IsModel)
	{
		for(var i=0; i<objItem.SKU_Details.length; i++)
		{
			var skuControl = document.forms[0].elements[objItem.SKU_Details[i].SKUControl_ID];
			
			if(skuControl.value ==  "0")
				return false;
		}
	}
	
	isFormSubmitted = true;
	return true;
}