var SAME_AS_BILLINGING_ADDRESS_VALUE = '0';
var SELECT_VALUE = '-1';
var NEW_SHIPPING_ADDRESS_VALUE = '-2';
var quantity_timer_obj;
var GROUP_BUY_PRODUCT_TIMMER_OBJ;
var GROUP_BUY_NEXT_PRODUCTID;
var PRODUCT_ROTATION_TIME_LIMIT = 10000;
var GROUP_BUY_PRODUCT_DEAL_TYP_ID = 3;

function saveProductCatalog(status)
{
	if(!document.getElementById('productName').value)
	{
		alert(product_name);
		document.getElementById('productName').focus();	
		return;
	}
	/*if(document.getElementById('isProductCat').value == 'false')
	{
		if(document.getElementById('productCategory').value ==0)
		{
			alert(product_category);
			document.getElementById('productCategory').focus();
			return;
		}
	}*/
	if(!document.getElementById('productOurPrice').value)
	{
		alert(product_price);
		document.getElementById('productOurPrice').focus();	
		return;
	}
	if(!isPriceValue(document.getElementById('productOurPrice').value))
	{
		alert(product_price_value);
		document.getElementById('productOurPrice').value='';
		document.getElementById('productOurPrice').focus();	
		return;
	}
	if(document.getElementById('productListPrice').value)
	{
		if(!isPriceValue(document.getElementById('productListPrice').value))
		{
			alert(product_price_value);
			document.getElementById('productListPrice').value='';
			document.getElementById('productListPrice').focus();	
			return;
		}
		if(parseFloat(document.getElementById('productListPrice').value) < parseFloat(document.getElementById('productOurPrice').value))
		{
			alert(product_last_price_value);
			document.getElementById('productOurPrice').value='';
			document.getElementById('productOurPrice').focus();	
			return;
		}
	}
	/*if(document.getElementById('isProductCat').value == 'false')
	{
		if(document.getElementById('productCategory').value >0)
		{
			document.getElementById('productCategoryId').value=document.getElementById('productCategory').value;
		}
	}*/
	if(document.getElementById('AddImageDIV')== null)
	{
		if(document.getElementById('productImage').value == '')
		{
			alert(product_image);
			return false;
		}
		else if(document.getElementById('productImage').value != '')
		{
			alert(product_upload_image);
			return false;
		}
		else if(document.getElementById('addImage_table'))
		{
			if(document.getElementById('addImage_table').rows.length == 1)
			{	
				alert(product_image);
				return false;
			}
		}
	}
	else
	{
		if(document.getElementById('AddImageDIV').style.display=='block')
		{
			if(document.getElementById('addImage_table'))
			{
				if(document.getElementById('productImage').value != '' && document.getElementById('addImage_table').rows.length == 1)
				{
					alert(product_upload_image);
					return false;
				}
				else if(document.getElementById('addImage_table').rows.length == 1)
				{	
					alert(product_image);
					return false;
				}
			}
		}
	}
	if(!document.getElementById('altMessage').value)
	{
		alert(product_alt_message);
		document.getElementById('altMessage').focus();	
		return;
	}
	if(document.addProductCatalogForm.shipCarges[0].checked)
	{
		if(!parseFloat(document.getElementById('productWeight').value))
		{
			alert('Please enter the productWeight in numeric value.');
			document.getElementById('productWeight').value ='';
			document.getElementById('productWeight').focus();
			return false;
		}
	}


	var businessId=document.getElementById('businessId').value;
	document.getElementById('productCatalogMode').value = status;
	document.addProductCatalogForm.target="_self";
	document.addProductCatalogForm.method="post";
	document.addProductCatalogForm.action ="/addProductCatalog/"+businessId;
	document.addProductCatalogForm.submit();
}

function updateProductCatalog(status)
{
	if(!document.getElementById('productName').value)
	{
		alert(product_name);
		document.getElementById('productName').focus();	
		return;
	}
	if(document.getElementById('isProductCat').value == 'false')
	{
		if(document.getElementById('productCategory').value ==0)
		{
			alert(product_category);
			document.getElementById('productCategory').focus();
			return;
		}
	}
	if(!document.getElementById('productOurPrice').value)
	{
		alert(product_price);
		document.getElementById('productOurPrice').focus();	
		return;
	}
	if(!isPriceValue(document.getElementById('productOurPrice').value))
	{
		alert(product_price_value);
		document.getElementById('productOurPrice').value='';
		document.getElementById('productOurPrice').focus();	
		return;
	}
	if(document.getElementById('productListPrice').value)
	{
		if(!isPriceValue(document.getElementById('productListPrice').value))
		{
			alert(product_price_value);
			document.getElementById('productListPrice').value='';
			document.getElementById('productListPrice').focus();	
			return;
		}
		if(parseFloat(document.getElementById('productListPrice').value) < parseFloat(document.getElementById('productOurPrice').value))
		{
			alert(product_last_price_value);
			document.getElementById('productOurPrice').value='';
			document.getElementById('productOurPrice').focus();	
			return;
		}
	}
	if(document.getElementById('AddImageDIV')==null || document.getElementById('AddImageDIV').style.display=='block')
	{
		if(document.getElementById('productImage').value == '' && document.getElementById('AddImageDIV').style.display == 'none')
		{
			alert(product_image);
			return false;
		}
		else if(document.getElementById('productImage').value != '')
		{
			alert(product_upload_image);
			return false;
		}
		else if(document.getElementById('addImage_table'))
		{
			if(document.getElementById('addImage_table').rows.length == 1)
			{	
				alert(product_image);
				return false;
			}
		}
	}
	if(!document.getElementById('altMessage').value)
	{
		alert(product_alt_message);
		document.getElementById('altMessage').focus();	
		return;
	}
	if(document.editProductCatalogForm.shipCarges[0].checked)
	{
		if(!parseFloat(document.getElementById('productWeight').value))
		{
			alert('Please enter the productWeight in numeric value.');
			document.getElementById('productWeight').value ='';
			document.getElementById('productWeight').focus();
			return false;
		}
	}

	var businessId=document.getElementById('businessId').value;
	document.getElementById('productCatalogMode').value = status;
	var productCatalogId=document.getElementById('productCatalogId').value;
	document.editProductCatalogForm.target="_self";
	document.editProductCatalogForm.method="post";
	document.editProductCatalogForm.action ="/editProductCatalog/"+businessId+"/"+productCatalogId;
	document.editProductCatalogForm.submit();
}

function validateImage(fieldId,logoImage)
{
	if(document.getElementById(fieldId)!= null && document.getElementById(fieldId).value.length == 0)
	{
		alert(product_image);
		document.getElementById(fieldId).focus();
		return false;
	}
	else if(document.getElementById(logoImage).value.length == 0)
	{
		alert(microsite_logo);
		document.getElementById(fieldId).focus();
		return false;
	}
	else if(validImgExten(fieldId,logoImage))
	{
		return true;
	}
}
function browseImageClick()
{
	var imgPath = document.getElementById('productImage').value;
	var source = document.getElementById('source').value;
	var micrositeId = document.getElementById('micrositeId').value;
	if(imgPath!="")
	{
		document.getElementById('productCatalogMode').value = 'addImageMode';
		var businessId = document.getElementById('businessId').value;
		document.addProductCatalogForm.method="post";
		document.addProductCatalogForm.action ="/addProductCatalog/"+businessId+"?source="+source+"&micrositeId="+micrositeId;
		document.addProductCatalogForm.submit();
	}
	else
	{
		alert(select_image_before_add);
		return;
	}
}


function selectedImageRow(obj,imgsrc)
{
	var trId=obj.id;
	var selectedRowId=document.getElementById('selectedRowId').value;
	if(selectedRowId=="")
	{
		document.getElementById(trId).style.backgroundColor="#C8DDDF";
		document.getElementById('selectedRowId').value=trId;
	}
	else
	{
		if(document.getElementById(selectedRowId))
		{
			document.getElementById(selectedRowId).style.backgroundColor="#FFFFFF";
		}
		document.getElementById(trId).style.backgroundColor="#C8DDDF";
		document.getElementById('selectedRowId').value=trId;
	}
//	alert(imgsrc);
	document.getElementById("imgDiv").innerHTML="<img src='"+imgsrc+"'>";
	//document.getElementById("selectedImage").src="file://"+imgsrc;
}


function deleteImage(imgName,rowIndex)
{
	var confirmObj = confirm("Are you sure,Do you want to delete this image?");
	if(confirmObj)
	{
		var xmlHttp = getXMLHTTP();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)
			{
				document.getElementById("addImage_table").deleteRow(rowIndex);
			}
		}
		xmlHttp.open("GET","/removeProductcatalogImage?imgName="+imgName,true);
		xmlHttp.send(null);
	}
	else
	{
		return false;
	}
}

function addImageInEditProductCatalog()
{
	var imgPath = document.getElementById('productImage').value;
	var source = document.getElementById('source').value;
	var micrositeId = document.getElementById('micrositeId').value;
	if(imgPath!="")
	{
		document.getElementById('productCatalogMode').value = 'addImageMode';
		var productCatalogId=document.getElementById('productCatalogId').value;
		var businessId=document.getElementById('businessId').value;
		document.editProductCatalogForm.method="post";
		document.editProductCatalogForm.action ="/editProductCatalog/"+businessId+"/"+productCatalogId+"?source="+source+"&micrositeId="+micrositeId;
		document.editProductCatalogForm.submit();
	}
	else
	{
		alert(select_image_before_add);
		return false;
	}
}

function previewMicroSitePage(formObj)
{
	var businessId=document.getElementById('businessId').value;
	var surveyId= "";
	var themeId= "";
	var pageName= "";
	if(document.getElementById('surveyId'))
	{
		surveyId=document.getElementById('surveyId').value;
	}
	if(document.getElementById('pageName'))
	{
		pageName=document.getElementById('pageName').value;
	}
	formObj.target="_blank";
	formObj.method="post";
	if(surveyId == '')
	{
		formObj.action ="/microSitePreview/"+businessId;
	}
	else
	{
		formObj.action ="/microSitePreview/"+businessId+"?pageName="+pageName+"&survey="+surveyId;
	}
	formObj.submit();
}


function textLimit(field,maxlen)
{
	if(field.value.length > maxlen)
	{
		while(field.value.length > maxlen)
		{
		field.value=field.value.replace(/.$/,'');
		}
		alert(short_desc_limit);
	}
}

function getProductsRelavantToCategory()
{
	document.productCatalogue.method="post";
	document.productCatalogue.action="/Product Catalogue";
	document.productCatalogue.submit();
}

var m_ProductName ='';
var m_ProductPrice ='';
var m_ProductId ='';
var m_productBusinessId ='';
var m_productCheckBox ='';
var m_isFromCart ='';
var m_BusinessId ='';
var m_ProductType ='';
function addToCartParameters(productName,productPrice,productId,productBusinessId,businessId,productCheckBox,isFromCart,productType)
{
	
	if(productName != '')
	{
		var index = productName.indexOf('*');
		if(index != -1)
		{
			PName = productName.replace("*","'");
		}
		else
		{
			PName = productName;
		}

		m_ProductName = PName;
	}
	m_ProductPrice = productPrice;
	m_ProductId = productId;
	if(productBusinessId != '')
	{
		m_productBusinessId = productBusinessId;
	}
	m_productCheckBox = productCheckBox;
	if(isFromCart != '')
	{
		m_isFromCart = isFromCart;
	}
	if(productType != '')
	{
		m_ProductType = productType;
	}

	
	m_BusinessId = businessId;
	addToCart(m_ProductName, m_ProductPrice, m_ProductId, m_productBusinessId, m_BusinessId, m_productCheckBox, m_isFromCart,m_ProductType);
}

function addToCart()
{
	clearTimeout(quantity_timer_obj);
	var url = "/product/getShoppingCart?from=ajax&productName="+m_ProductName+"&productPrice="+m_ProductPrice+"&productId="+m_ProductId+"&businessId="+m_productBusinessId+"&productCheckBox="+m_productCheckBox+"&isFromCart="+m_isFromCart+"&businessId="+m_BusinessId+"&productType="+m_ProductType;
	m_ProductName ='';
	m_ProductPrice='';
	m_ProductId='';
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4) 
			{
				if (xmlHttp.status == 200)
				{
					document.getElementById("replace").innerHTML = xmlHttp.responseText;
					quantity_timer_obj = setTimeout("addToCart()",9000);
					m_ProductName ="";
				}
			}
			
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}

function getProductsDetails(productName,businessId,productId)
{
	var index = productName.indexOf('*');
	if(index != -1)
	{
		PName = productName.replace("*","'");
	}
	else
	{
		PName = productName;
	}
	var url = "/product/getProductDetail?productName="+PName+"&businessId="+businessId+"&productId="+productId;
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4) 
			{
				if (xmlHttp.status == 200)
				{
					document.getElementById("ShopContent").innerHTML = xmlHttp.responseText;
				}
			}
			
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}

function showThisImage(img,caption,altText,index)
{
	var position = caption.indexOf('*');
	if(position != -1)
	{
		caption = changeCaption(caption);
	}
	else
	{
		caption = caption;
	}
	document.getElementById('thumbNail').src = img;
	if(altText)
	{
		document.getElementById('thumbNail').title = altText;
	}
	var selectedImageId = document.getElementById('selectedImage').value;
	document.getElementById('selectedImage').value=index;
	var obj=document.getElementById('remaining_'+selectedImageId);
	if(selectedImageId)
	{
		document.getElementById('remaining_'+selectedImageId).className = "image_border";
	}
	document.getElementById('remaining_'+index).className = "image_gallery";
	document.getElementById('caption').innerHTML = '<b>'+caption+'</b>';
}

function showThumbnailImage(id)
{
	var selectedImage = document.getElementById(id).src;
	if(selectedImage)
	{
		document.getElementById('thumbNail').src = selectedImage;
	}
}

function changeCaption(caption)
{
	caption = caption.replace("*","'");
	var position = caption.indexOf('*');
	if(position != -1)
	{
		caption = changeCaption(caption);
	}
	return caption;
}
function checkOutShipping(from)
{
	redirectVal=document.getElementById('pageName').value;
	businessId=document.getElementById('vendorId').value;
	cartDetailId=document.getElementById('cartDetailId').value;
	
	if(from == 'ShippingAddress')
	{
		var dealId=document.getElementById('dealId').value;
		var productPrice=document.getElementById('productPrice').value;
		if(document.getElementById('shipmentAddressId').value == SELECT_VALUE)
		{
			alert(select_valid_shipping_address);
			document.getElementById('shipmentAddressId').focus();
			return false;
		}		
		if(!validateAddress())
		{
			return false;
		}
		document.getElementById('actionName').value ="ShippingSummary";
		document.shipmentForm.method = "post";
		document.shipmentForm.action = "/"+redirectVal+"?businessId="+businessId+"&cartDetailId="+cartDetailId+"&dealId="+dealId+"&productPrice="+productPrice;
		document.shipmentForm.submit();
	}
	else
	{
		document.getElementById('actionName').value ="ShippingSummary";
		document.checkOut.method = "post";
		document.checkOut.action = "/"+redirectVal+"?businessId="+businessId+"&cartDetailId="+cartDetailId+"&dealId="+dealId+"&productPrice="+productPrice;
		document.checkOut.submit();
	}
}

function validateAddress()
{
	
	if(trim(document.getElementById('billingCustName').value) == "")
	{
		alert(enter_your_name);
		document.getElementById('billingCustName').focus();
		return false;
	}
	else if(trim(document.getElementById('billingCustAddr').value) == "")
	{
		alert(enter_your_address);
		document.getElementById('billingCustAddr').focus();
		return false;
	}
	else if(trim(document.getElementById('billingCustCity').value) == "")
	{
		alert(enter_city_name);
		document.getElementById('billingCustCity').focus();
		return false;
	}
	else if(trim(document.getElementById('billingCustState').value) == "" || trim(document.getElementById('billingCustState').value) == 0)
	{
		alert(enter_state_name);
		document.getElementById('billingCustState').focus();
		return false;
	}
	else if(trim(document.getElementById('billingCustCountry').value) == "")
	{
		alert(enter_country_name);
		document.getElementById('billingCustCountry').focus();
		return false;
	}
	else if(document.getElementById('billingCustPin'))
	{
		if(trim(document.getElementById('billingCustPin').value) =="")
		{
			alert(enter_pincode);
			document.getElementById('billingCustPin').focus();
			return false;
		}
		else if(!validatePin(trim(document.getElementById('billingCustPin').value)))
		{
			alert (enter_valid_pincode);
			document.getElementById('billingCustPin').value="";
			document.getElementById('billingCustPin').focus();
			return false;
		}
		
	}
	if(document.getElementById('billingCustMobile'))
	{
		if(trim(document.getElementById('billingCustMobile').value) =="")
		{
			alert (enter_your_mobile_number);
			document.getElementById('billingCustMobile').focus();
			return false;
		}
		else if(!validateMobile(trim(document.getElementById('billingCustMobile').value)))
		{
			alert (enter_your_valid_mobile_number);
			document.getElementById('billingCustMobile').value="";
			document.getElementById('billingCustMobile').focus();
			return false;
		}
		
	}
	if(trim(document.getElementById('ShippingCustName').value) == "")
	{
		alert(enter_your_name);
		document.getElementById('ShippingCustName').focus();
		return false;
	}
	if(trim(document.getElementById('ShippingCustAddr').value) == "")
	{
		alert(enter_your_address);
		document.getElementById('ShippingCustAddr').focus();
		return false;
	}
	if(trim(document.getElementById('ShippingCustCity').value) == "")
	{
		alert(enter_city_name);
		document.getElementById('ShippingCustCity').focus();
		return false;
	}
	if(trim(document.getElementById('ShippingCustState').value) == "")
	{
		alert(enter_state_name);
		document.getElementById('ShippingCustState').focus();
		return false;
	}
	if(trim(document.getElementById('ShippingCustCountry').value) == "")
	{
		alert(enter_country_name);
		document.getElementById('ShippingCustCountry').focus();
		return false;
	}
	if(document.getElementById('ShippingCustPin'))
	{
		if(trim(document.getElementById('ShippingCustPin').value) =="")
		{
			alert(enter_pincode);
			document.getElementById('ShippingCustPin').focus();
			return false;
		}
		else if(!validatePin(trim(document.getElementById('ShippingCustPin').value)))
		{
			alert(enter_valid_pincode);
			document.getElementById('ShippingCustPin').value="";
			document.getElementById('ShippingCustPin').focus();
			return false;
		}
		
	}
	if(document.getElementById('ShippingCustMobile'))
	{
		if(trim(document.getElementById('ShippingCustMobile').value) =="")
		{
			alert(enter_your_mobile_number);
			document.getElementById('ShippingCustMobile').focus();
			return false;
		}
		else if(!validateMobile(trim(document.getElementById('ShippingCustMobile').value)))
		{
			alert(enter_your_valid_mobile_number);
			document.getElementById('ShippingCustMobile').value="";
			document.getElementById('ShippingCustMobile').focus();
			return false;
		}
		
	}
	
	return true;
	
}

function validatePin(contactPin)
{
	var pattern = /^[0-9]{3,6}$/;
	if(!pattern.test(contactPin))
	{
		return false;
	}
	return true;
}

function validateMobile(mobileNumber)
{
	var pattern = /^[0-9]{10,12}$/;
	if(!pattern.test(mobileNumber))
	{
		return false;
	}
	return true;
}
function changePageContent(pageNum)
{
	document.getElementById('actionName').value = "CheckOut";
	document.productCatalogue.pageNum.value = pageNum;
	redirectVal=document.getElementById('pageName').value;
	document.productCatalogue.method = "post";
	document.productCatalogue.action = "/"+redirectVal;
	document.productCatalogue.submit();
}

function checkOut()
{
	var from = document.getElementById('checkOutShippingAddressDetails').value;
	if(from == '')
	{
		document.checkOutOrderSummary.method = "post";
		document.checkOutOrderSummary.action = "/Product Catalogue";
		document.checkOutOrderSummary.submit();
	}
	if(from == 'checkOutShippingDetailsFromCart')
	{
		document.checkOut.method = "post";
		document.checkOut.action = "/Product Catalogue";
		document.checkOut.submit();
	}
}

function deleteProduct(cartId,productId,productDataRow)
{
	var url = "/product/deleteProduct?removeWithCartId="+cartId+"&removeWithProductId="+productId;
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
			{
				if (xmlHttp.status == 200)
				{
					document.getElementById("orderTable").innerHTML = xmlHttp.responseText;
				}
			}
			
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}

function initialCheckOut()
{
	if(document.getElementById('totalAmount').value < 1)
	{
		alert('Please select atleast one product');
		return false;
	}
	else
	{
		redirectVal=document.getElementById('pageName').value;
		document.checkOut.method = "post";
		document.checkOut.action = "/"+redirectVal;
		document.checkOut.submit();
	}
}

function selectedCheckBoxValues(productCheckBox,productName,productPrice,ProductId,productBusinessId,businessId,userId)
{
	clearTimeout(quantity_timer_obj);
	var productCheckBox = document.getElementById(productCheckBox).checked;
	var productName = productName;
	var productPrice = productPrice;
	var productId = ProductId;
	var productBusinessId = productBusinessId;
	addToCartParameters(productName,productPrice,productId,productBusinessId,businessId,productCheckBox,'false');
	if(userId != '')
	{
		document.checkOut.method = "post";
		document.checkOut.action = "/product/deleteItemByUnCheck";
		document.checkOut.submit();
	}
}

function editProductQuantityAndUpdateGrandTotal(quantityId,productName,productPrice,productId,productCheckBox,productBusinessId)
{
	clearTimeout(quantity_timer_obj);
	var quantity = document.getElementById(quantityId).value;
	if(trim(quantity) == "")
	{
		document.getElementById(quantityId).focus();
		return false;
	}
	if(isNaN(quantity))
	{
		alert(enter_only_digits);
		document.getElementById(quantityId).value=1;
		document.getElementById(quantityId).focus();
		return false;
	}
	var url = "/product/getShoppingCart?from=ajax&productName="+productName+"&productPrice="+productPrice+"&productId="+productId+"&businessId="+productBusinessId+"&productCheckBox="+productCheckBox+"&isFromCart=editQty&quantity="+quantity;
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4) 
			{
				if (xmlHttp.status == 200)
				{
					document.getElementById("replace").innerHTML = xmlHttp.responseText;
					quantity_timer_obj = setTimeout("addToCart()",9000);
				}
			}
			
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}

function removeCartDetailById(selectedRowId, productCartDetlId)
{
	var url = "/product/removeCartDetailById?selectedRowId="+selectedRowId+"&productCartDetlId="+productCartDetlId;
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4) 
			{
				if (xmlHttp.status == 200)
				{
					alert(xmlHttp.responseText);
				}
			}
			
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}
function checkQuantityValid(textId)
{
		var noOfItems =document.getElementById(textId).value;
		if(isNaN(noOfItems) || trim(noOfItems) == "")
		{
			document.getElementById(textId).value =0;
			document.getElementById(textId).focus();
			return false;
		}
	
}
function updatePrice(e,textId,productBasicCost)
{
	
	var key = (window.event) ? event.keyCode : e.keyCode;
		if(key == 8)
		{
			if(document.getElementById(textId).value =="")
			{
				document.getElementById(textId).focus();
			}
			return true;
		}
	 var noOfItems =document.getElementById(textId).value;
		if(isNaN(noOfItems) || trim(noOfItems) == "")
		{
			txtIdSplit = textId.split('_');
			priceTdId = "td_"+txtIdSplit[1]+"_"+4;
			var price = document.getElementById(priceTdId).innerHTML;
			var noOfItems = parseFloat(price)/parseFloat(productBasicCost);
			alert(enter_only_digits);
			document.getElementById(textId).value=noOfItems;
			document.getElementById(textId).focus();
			return false;
		}
		else
		{
			var price = parseFloat(noOfItems) * parseFloat(productBasicCost);
			txtIdSplit = textId.split('_');
			priceTdId = "td_"+txtIdSplit[1]+"_"+4;
			document.getElementById(priceTdId).innerHTML = price.toFixed(2);
			updateTotalPrice();
			setShippingCharges(document.getElementById('buyInsurance'));
		}
}

function updateTotalPrice()
{
	var tableObj = document.getElementById("orderTable");
	var rows = tableObj.rows;
	var totalPrice = 0;
	var sno = 1;
	for(var ind= 0; ind < rows.length; ind++)
	{
		var trobj = rows[ind];
		if(trobj.id)
		{
			var trId = trobj.id;
			var trIdSplit = trId.split('_');
			var price = document.getElementById("td_"+trIdSplit[1]+"_4").innerHTML;
			document.getElementById("td_"+trIdSplit[1]+"_1").innerHTML = sno++;
			totalPrice +=  parseFloat(price);
		}
	}
	document.getElementById('cartTotalPrice').innerHTML = totalPrice.toFixed(2);
}

function setShippingCharges(obj,check)
{
	var productDetails = createOrderSummeryPacket(isEdited);
	var totalPrice=document.getElementById('cartTotalPrice').innerHTML;
	var insurance=document.getElementById('buyInsurance').checked;
	var checkedValue = '';
	if(document.getElementById('shipped').checked)
	{
		checkedValue = document.getElementById('shipped').value;
	}
	else
	{
		checkedValue = document.getElementById('shipped').value;
	}
	if(obj)
	{
		var val=obj.checked;
		if(val!=undefined)
		{
			var insurance=val;
		}
	}
	var url = "/product/updateShippingCharges?productDetails="+productDetails+"&totalPrice="+totalPrice+"&insurance="+insurance+"&checkedValue="+check;
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4) 
			{
				if (xmlHttp.status == 200)
				{
					document.getElementById("shippingChargesDiv").innerHTML = xmlHttp.responseText;
					document.getElementById("cartTotalPrice").innerHTML=document.getElementById("shippingTotalAjax").value;
					document.getElementById("NoteInfo").innerHTML=document.getElementById("inStorePicedUpNote").value;
				}
				else 
				{
					alert('error');
				}
			}
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}

function deleteRow(selectedRowId, productCartDetlId)
{
	var response = confirm('Are you sure you want to remove this item from your shopping cart?');
	if(response)
	{
		var tableObj = document.getElementById("orderTable");
		var rows = tableObj.rows;
		var ind=0
		for(; ind < rows.length; ind++)
		{
			rowObj = rows[ind];
			if(rowObj.id)
			{
				if(selectedRowId == rowObj.id)
				{
					break;
				}
			}
		}
		document.getElementById('orderTable').deleteRow(ind);
		updateTotalPrice();
		setShippingCharges(document.getElementById('buyInsurance'));
		removeCartDetailById(selectedRowId, productCartDetlId);
	}
	else
	{

	}
}

function clearDataBasedOnDate(date)
{
	var response = confirm('Are you sure you want to empty your shopping cart?');
	if(response)
	{
		document.checkOutOrderSummary.method = "post";
		document.checkOutOrderSummary.action = "/product/clearDataByDate";
		document.checkOutOrderSummary.submit();
	}
	else
	{
		
	}
}

function saveAndLogOut()
{
	var productDetails = createOrderSummeryPacket();
	document.getElementById('editedProductDetails').value = productDetails;
	document.checkOutOrderSummary.method = "post";
	document.checkOutOrderSummary.action = "/product/saveAndLogout";
	document.checkOutOrderSummary.submit();
}
function getPaymentPage()
{
	var productDetails = createOrderSummeryPacket();
	var businessId = document.getElementById('vendorId').value;
	var dealProductId = document.getElementById('dealId').value;
	document.getElementById('editedProductDetails').value = productDetails;
	var totalPrice = document.getElementById('cartTotalPrice').innerHTML;
	document.getElementById('shippingTotal').value = document.getElementById('shippingTotalAjax').value;
	document.getElementById('shippingCharges').value = document.getElementById('shippingChargesAjax').value;
	document.getElementById('shippingServiceTax').value = document.getElementById('shippingServiceTaxAjax').value;
	document.getElementById('fuelSurcharge').value = document.getElementById('fuelSurchargeAjax').value;
	document.getElementById('insurance').value = document.getElementById('insuranceAjax').value;
	document.getElementById('totalPrice').value = totalPrice;
	document.checkOutOrderSummary.method = "post";
	document.checkOutOrderSummary.action = "/product/getPaymentPage?businessId="+businessId+"&dealProductId="+dealProductId;
	document.checkOutOrderSummary.submit();
}
function createOrderSummeryPacket(edited)
{
	var tableObj = document.getElementById("orderTable");
	var rows =  tableObj.rows;
	var i = 0;
	var productDetails = '';
	
	for(var ind= 0; ind < rows.length; ind++)
	{
		var trobj = rows[ind];
		if(trobj.id)
		{
			var trId = trobj.id;
			var trIdSplit = trId.split('_');
			var isEdited = document.getElementById("hid_"+trIdSplit[1]+"_3").value;
			if(edited== 'true')
			{
				var isEdited = 'true';
			}
			if(isEdited == 'true')
			{
				var productCartDetailId = document.getElementById("hid_"+trIdSplit[1]+"_1").value;
				var productCartId = document.getElementById("hid_"+trIdSplit[1]+"_2").value;
				var productId = document.getElementById("hid_"+trIdSplit[1]+"_4").value;
				var businessId = document.getElementById("hid_"+trIdSplit[1]+"_5").value;
				var productQuantity = document.getElementById("txt_"+trIdSplit[1]+"_3").value;
				var productPrice = document.getElementById("td_"+trIdSplit[1]+"_4").innerHTML;
				var productEditedDetails = productCartDetailId +"_"+ productCartId +"_"+ productId +"_"+ productQuantity +"_"+ productPrice+"_"+businessId;
				
				if(i == 0)
				{
					productDetails = productEditedDetails;
					i++;
				}
				else
				{
					productDetails = productDetails + " : " + productEditedDetails ;
				}
			}
		}
	}
	
	return productDetails;
}

function setEditFlag(hiddenId)
{
	document.getElementById(hiddenId).value="true";
}

function isPriceValue(str) 
{
	if(str.length==0)
	{
		return false;
	}
	var numdecs = 0;
	for (i = 0; i < str.length; i++)
	{
		mychar = str.charAt(i);
		if ((mychar >= "0" && mychar <= "9") || mychar == "." )
		{
			if (mychar == ".")
			numdecs++;
		}
		else 
		{
			return false;
		}
	}
	if (numdecs == 1)
	{
		isprice = /^\d+\.\d{2}$/;
		return isprice.test( str );
	}
	return true;
} 

function payNow()
{
	document.paymentGateway.method = "post";
	document.paymentGateway.submit();
}

function formatShipmentAddress()
{
	shippingAddressId = document.getElementById('shipmentAddressId').value;
	if(shippingAddressId == NEW_SHIPPING_ADDRESS_VALUE)
	{	
		document.getElementById('shippingAddressReplaceTd').disabled =false;
		document.getElementById('ShippingCustName').value		="";
		document.getElementById('ShippingCustAddr').value		="";
		document.getElementById('ShippingCustCity').value		="";
		document.getElementById('ShippingCustState').value		="";
		document.getElementById('ShippingCustCountry').value	="";
		document.getElementById('ShippingCustPin').value		="";
		document.getElementById('ShippingCustMobile').value		="";
			
	}
	else if(shippingAddressId == SELECT_VALUE)
	{
		document.getElementById('shippingAddressReplaceTd').disabled =true;
		document.getElementById('ShippingCustName').value		="";
		document.getElementById('ShippingCustAddr').value		="";
		document.getElementById('ShippingCustCity').value		="";
		document.getElementById('ShippingCustState').value		="";
		document.getElementById('ShippingCustCountry').value	="";
		document.getElementById('ShippingCustPin').value		="";
		document.getElementById('ShippingCustMobile').value		="";
			
	}
	else if(shippingAddressId == SAME_AS_BILLINGING_ADDRESS_VALUE)
	{
		document.getElementById('shippingAddressReplaceTd').disabled =false;
		document.getElementById('ShippingCustName').value		= document.getElementById('billingCustName').value;
		document.getElementById('ShippingCustAddr').value		= document.getElementById('billingCustAddr').value;
		document.getElementById('ShippingCustCity').value		= document.getElementById('billingCustCity').value;
		document.getElementById('ShippingCustState').value		= document.getElementById('billingCustState').value;
		document.getElementById('ShippingCustCountry').value	= document.getElementById('billingCustCountry').value;
		document.getElementById('ShippingCustPin').value		= document.getElementById('billingCustPin').value;
		document.getElementById('ShippingCustMobile').value		= document.getElementById('billingCustMobile').value;
	}
	else
	{
		var url = "/product/getShippingAddress?shippingAddressId="+shippingAddressId;
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4)
			{
				if (xmlHttp.status == 200)
				{
					document.getElementById("shippingAddressReplaceTd").disabled =false;
					document.getElementById("shippingAddressReplaceTd").innerHTML = xmlHttp.responseText;
				}
			}
			
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
	}
}
function saveShippingInformation(actionName)
{
	var businessId = document.getElementById('vendorId').value;
	document.getElementById('actionName').value=actionName;
	redirectVal=document.getElementById('pageName').value;
	redirecturl=document.getElementById('siteUrl').value;
	document.shipmentForm.method = "post";
	document.shipmentForm.action = redirecturl+"/"+redirectVal+"?businessId="+businessId;
	document.shipmentForm.submit();
}
function getShippingInformation(actionName)
{
	var businessId = document.getElementById('vendorId').value;
	document.getElementById('actionName').value=actionName;
	redirectVal=document.getElementById('pageName').value;
	document.checkOutOrderSummary.method = "post";
	document.checkOutOrderSummary.action = "/"+redirectVal+"?businessId="+businessId;
	document.checkOutOrderSummary.submit();
}
function formatShipmentField(event)
{
	shippingAddressId = document.getElementById('shipmentAddressId').value;
	
	if(shippingAddressId !="-1" && shippingAddressId =='0')
	{
		var textId = event.id;
		var shipId= "Shipping"+textId.substr(7);
		document.getElementById(shipId).value = document.getElementById(textId).value;
	}
}

function showGroupBuyProductDetails(productId,dealType,mode)
{
	/*var dealTypId = 0;
	if(mode == 'Previous' || mode == 'Next')
	{
		if(dealType =="groupBuyDeal")
		{
			
			clearTimeout(GROUP_BUY_PRODUCT_TIMMER_OBJ);
			dealTypId = GROUP_BUY_PRODUCT_DEAL_TYP_ID;
		}
	}
	else
	{
		if(dealType =="groupBuyDeal")
		{
			clearTimeout(GROUP_BUY_PRODUCT_TIMMER_OBJ);
			productId = GROUP_BUY_NEXT_PRODUCTID;
			dealTypId = GROUP_BUY_PRODUCT_DEAL_TYP_ID;
		}
	}*/

	var productId = document.getElementById('productId').value;
	var businessId = document.getElementById('businessId').value;
	var url = "/product/productGroupBuyProduct?productId="+productId+"&mode="+mode+"&businessId="+businessId;
	var xmlHttp = getXMLHTTP();
	if (xmlHttp) 
	{
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState == 4) 
			{
				if (xmlHttp.status == 200)
				{
					if(document.getElementById("groupBuyProduct"))
					{
						document.getElementById("groupBuyProduct").innerHTML = xmlHttp.responseText;
					}
					/* var nextProductId = document.getElementById('dealProductId').value;
					GROUP_BUY_NEXT_PRODUCTID = nextProductId;
					GROUP_BUY_PRODUCT_TIMMER_OBJ =setTimeout("showGroupBuyProductDetails('','groupBuyDeal','rotaion')",PRODUCT_ROTATION_TIME_LIMIT);*/
					
					if(COUNTDOWN_TIMER_OBJ != undefined)
					{
						clearTimeout(COUNTDOWN_TIMER_OBJ);
						var hoursLeft = document.getElementById('hoursLeft').value;
						var minutesLeft = document.getElementById('minutesLeft').value;
						var secondsLeft = document.getElementById('secondsLeft').value;
						var daysLeft = document.getElementById('daysLeft').value;
						var hoursAfterDays = document.getElementById('hoursAfterDays').value;
						countdown(hoursLeft,minutesLeft,secondsLeft,daysLeft,hoursAfterDays);
					}
				}
				else 
				{
					alert(problem_get_subCategories);
				}
			}
		}
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}
}
function stopSlideProducts(dealType)
{
	if(dealType=='groupBuyDeal')
	{
		//clearTimeout(GROUP_BUY_PRODUCT_TIMMER_OBJ);
	}
	
	
}
function showSlideProducts(dealProductId,dealType)
{
	if(dealType=='groupBuyDeal')
	{
		//GROUP_BUY_PRODUCT_TIMMER_OBJ=setTimeout("showGroupBuyProductDetails('','groupBuyDeal','')",PRODUCT_ROTATION_TIME_LIMIT);
	}
	
}

function showWeight(mode)
{
	if(mode == 'add')
	{
		if(document.addProductCatalogForm.shipCarges[0].checked)
		{
			document.getElementById('prodWeight').style.display = 'block';
		}
		else
		{
			document.getElementById('prodWeight').style.display = 'none';
		}
	}
	else
	{
		if(document.editProductCatalogForm.shipCarges[0].checked)
		{
			document.getElementById('prodWeight').style.display = 'block';
		}
		else
		{
			document.getElementById('prodWeight').style.display = 'none';
		}
	}
}

var COUNTDOWN_TIMER_OBJ="";
function countdown(hrs,mins,secs,days,hrsAfterDays)
{
	if(days >=0)
	{
		if(secs >= 0 || mins >= 0 || hrsAfterDays < 0)
			{
				secs = secs - 1;
				if (secs < 0)
				{
					mins = mins - 1;
					if (mins < 0)
					{
						hrsAfterDays = hrsAfterDays - 1;
						if(hrsAfterDays < 0)
						{
							days = days-1;
							hrsAfterDays = 23;
						}
						mins = 59;
					}
					secs = 59;
				}
				if(days > 0)
				{
					if(document.getElementById("timerlabel"))
					{
						document.getElementById("timerlabel").innerHTML = days + " Days, " + hrsAfterDays + "<b>:</b>" + mins + "<b>:</b>" + secs + "<b></b>";
						COUNTDOWN_TIMER_OBJ = setTimeout("countdown("+hrs+","+mins+","+secs+","+days+","+hrsAfterDays+")", 1000);
					}
					else
					{
						document.getElementById("groupBuyTimerlabel").innerHTML = days + " Days" +"<br>"+ hrsAfterDays + " Hours" +"<br>"+ mins + " Minutes" +"<br>"+ secs + " Seconds";
						COUNTDOWN_TIMER_OBJ = setTimeout("countdown("+hrs+","+mins+","+secs+","+days+","+hrsAfterDays+")", 1000);
					}
				}
				else
				{
					if(document.getElementById("timerlabel"))
					{
						document.getElementById("timerlabel").innerHTML = hrsAfterDays + "<b>:</b>" + mins + "<b>:</b>" + secs + "<b></b>";
						COUNTDOWN_TIMER_OBJ = setTimeout("countdown("+hrs+","+mins+","+secs+","+days+","+hrsAfterDays+")", 1000);
					}
					else
					{
						document.getElementById("groupBuyTimerlabel").innerHTML = hrsAfterDays + " Hours" +"<br>"+ mins + " Minutes" +"<br>"+ secs + " Seconds";
						COUNTDOWN_TIMER_OBJ = setTimeout("countdown("+hrs+","+mins+","+secs+","+days+","+hrsAfterDays+")", 1000);
					}

				}
			}
	}
}

// For calculating the total amount using quantity in product groupBuy
function calculateAmount(e,prodOurPrice)
{
	var qty = document.getElementById('quantityOfProd').value;
	var totalAmount = (qty * prodOurPrice);
	var key=(window.event) ? event.keyCode : e.keyCode;
	if((key >=48 && key<=57) || key == 8)
	{
		if(key == 48)
		{
			if(document.getElementById('quantityOfProd').value != 0)
			{
				document.getElementById('totalAmount').value = totalAmount;
			}
			else
			{
				alert('Quantity cannot be zero.');
				document.getElementById('quantityOfProd').value='';
				document.getElementById('quantityOfProd').focus;
				return false;
			}
		}
		else
		{
			document.getElementById('totalAmount').value = totalAmount;
		}
	}
	else if((key>64 && key<91) ||(key==16) || (key==17) || (key==27) || (key>32 && key<41) || (key==45) || (key==46) || (key==144) || (key==145) || (key==19) || (key>111 && key<121) || (key>121 && key<127) || (key==parseInt(189)) || (key==parseInt(109)) || (key >=95 && key <=122))
	{
		alert("Please enter a number");
		document.getElementById('quantityOfProd').value='';
		document.getElementById('quantityOfProd').focus;
		return false;
	}
}

// For collecting details from user when he clicks on reserve or BuyNow
function collectDetails()
{
	var quantity = document.getElementById('quantityOfProd').value;
	if(quantity != '' || quantity != 0)
	{
		document.getElementById('detailsCollectingForm').style.display = "block";
	}
	else
	{
		alert('Please enter the quantity.');
		document.getElementById('quantityOfProd').value='';
		document.getElementById('quantityOfProd').focus;
		return false;
	}
}

// Validating details from user and proceding when user clicks on checkout
function gotoCheckoutWithDetails()
{
	var userName = document.getElementById('userName').value;
	var userEmail = document.getElementById('userEmail').value;
	var userPhone = document.getElementById('userPhone').value;
	var productOurPrice = document.getElementById('productOurPrice').value;
	var hotdealTitle = document.getElementById('hotdealTitle').value;
	if(userName == '')
	{
		alert("Please enter your name");
		return false;
	}
	else if(userEmail == '')
	{
		alert("Please enter your E-mail");
		return false;
	}
	else if(userPhone == '')
	{
		alert("Please enter your phone number");
		return false;
	}
	else if(!validateEmail('userEmail'))
	{
		return false;
	}
	else if(userPhone)
	{
		var pattern = /^[0-9]{10,12}$/;
		var phoneNumber = document.getElementById('userPhone');
		if(!pattern.test(phoneNumber.value))
		{
			alert("Please enter valid number");
			phoneNumber.value='';
			phoneNumber.focus();
			return false;
		}
	}
	document.groupBuyDealProduct.method = "post";
	document.groupBuyDealProduct.action = "/saveGroupDealProductDetails?productOurPrice="+productOurPrice+"&hotdealTitle="+hotdealTitle;
	document.groupBuyDealProduct.submit();
}

// For collecting details from user when he clicks on reserve or BuyNow
function backButtonInForm()
{
	document.getElementById('detailsCollectingForm').style.display = "none";
}

function snapTermsAndConditions(obj)
{
	var xypos = getPosition(document.getElementById(obj));
	var xyposarray = xypos.split('|');
	var xpos = xyposarray[0];
	var ypos = xyposarray[1];
	document.getElementById('termAndConditionsDiv').style.right = (parseInt(xpos)+10)+ "px";
	document.getElementById('termAndConditionsDiv').style.top = (parseInt(ypos)+310)+ "px";
	if(document.getElementById('termAndConditionsDiv').style.display == "none")
	{
		document.getElementById('termAndConditionsDiv').style.display = "block";
	}
}

function hideTermsAndConditions(obj)
{
	document.getElementById('termAndConditionsDiv').style.display = "none";
}


