/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var flyingSpeed = 10;
var url_addProductToBasket = rootUrl+'fly-to-basket/addProduct.php';
var url_removeProductFromBasket = rootUrl+'fly-to-basket/removeProduct.php';
var txt_totalPrice = 'Total: ';


var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x = false;
var shopping_cart_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;

var ajaxObjects = new Array();


function shoppingCart_getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}
	

function addToBasket(categoryId, productId, defaultPdDetailsId)
{
	//alert(productId);
	var pdDetailsId = document.getElementById("EachPriceDetailsId"+productId).value;
	if(pdDetailsId==""){
		pdDetailsId = defaultPdDetailsId;
	}
	
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	
	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);

	currentProductDiv = document.getElementById('slidingProduct' + productId);
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='';
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	
	var elmsQty = document.getElementsByName("txtQty[]");
	var newHeight = "auto";
	if(elmsQty.length > 0){
		newHeight = "auto";
	} else {
		newHeight = "100px";
	}
	$("#shopping_cart").css({"width":"230px", "height":newHeight});
	$("#shopping_cart").mask("Cart Loading...");
	flyToBasket(categoryId, productId, pdDetailsId);
	
}


function flyToBasket(categoryId, productId, pdDetailsId)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';	
	
	
	if(moveX>0 && currentXPos > shopping_cart_x){
		flyingDiv.style.display='none';		
	}
	if(moveX<0 && currentXPos < shopping_cart_x){
		flyingDiv.style.display='none';		
	}
		
	if(flyingDiv.style.display=='block')
		setTimeout('flyToBasket("' + categoryId + '", "' + productId + '", "' + pdDetailsId + '")',10); 
	else 
		ajaxAddProduct(categoryId, productId, pdDetailsId);	
}

function showAjaxBasketContent(ajaxIndex)
{
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('shopping_cart_items');
	
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
	//alert(productItems[3]+" "+productItems);
	
	if(document.getElementById('shopping_cart_items_product' + productItems[0])){	// A product with this id is allready in the basket - just add number items
		var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
		var items = row.cells[0].innerHTML /1;
		items = items + 1;
		row.cells[0].innerHTML = items;
	}else{	// Product isn't allready in the basket - add a new row
		var tr = itemBox.insertRow(-1);
		tr.id = 'shopping_cart_items_product' + productItems[0]
		
		var td = tr.insertCell(-1);
		td.innerHTML = '1'; 	// Number of items
		
		var td = tr.insertCell(-1);
		td.innerHTML = productItems[1]; 	// Description
		
		var td = tr.insertCell(-1);
		td.style.textAlign = 'right';
		td.innerHTML = productItems[2]; 	// Price	
		
		var td = tr.insertCell(-1);
		var a = document.createElement('A');
		td.appendChild(a);
		a.href = '#';
		a.onclick = function(){ removeProductFromBasket(productItems[0]); };
		var img = document.createElement('IMG');
		img.src = 'fly-to-basket/images/remove.gif';
		a.appendChild(img);
		//td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';	
	} 
	document.getElementById("shopping_cart_items").style.display="none";

	//updateTotalPrice();
	//alert(productItems[4]);
	var productId = productItems[0];
	var categoryId = productItems[3];
	var pdDetailsId = productItems[4];
	
	DisplayShoppingCartLink(productId, categoryId, pdDetailsId);
	
	ajaxObjects[ajaxIndex] = false;		
	
}

function updateTotalPrice()
{
	var itemBox = document.getElementById('shopping_cart_items');
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	if(document.getElementById('shopping_cart_totalprice')){
		for(var no=1;no<itemBox.rows.length;no++){
			totalPrice = totalPrice + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].innerHTML);
			
		}		
		document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2);
		
	}	
	
}

function removeProductFromBasket(productId)
{
	var productRow = document.getElementById('shopping_cart_items_product' + productId);
	
	var numberOfItemCell = productRow.cells[0];
	if(numberOfItemCell.innerHTML == '1'){
		productRow.parentNode.removeChild(productRow);	
	}else{
		numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;
	}
	updateTotalPrice();
	ajaxRemoveProduct(productId);	
}

function ajaxValidateRemovedProduct(ajaxIndex)
{
	if(ajaxObjects[ajaxIndex].response!='OK')alert('Error while removing product from the database');
	
}

function ajaxRemoveProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(categoryId, productId, pdDetailsId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('categoryId',categoryId);
	ajaxObjects[ajaxIndex].setVar('productId',productId);
	ajaxObjects[ajaxIndex].setVar('pdDetailsId',pdDetailsId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}



function DisplayShoppingCartLink(productId, categoryId, pdDetailsId)
{
	//alert("MANOHAR");	
	
	/*jQuery('#right-section').showLoading(
		 {
			'afterShow': 
			function() {
				setTimeout( "jQuery('#right-section').hideLoading()", 5000 );
			}

		
						
		 }
		);*/
	//jQuery('#right-section').showLoading();
	//jQuery('#right-section').load(
		$.post(rootUrl+"include/display_cart.php", {productId: productId, categoryId: categoryId, pdDetailsId: pdDetailsId},
		   function(data)
		   {
				//alert("Data Loaded: "+data);	
				$("#ShoppingCartResults").html(data);
				//jQuery('#right-section').hideLoading();
				
				$("#shopping_cart").css({"width":"auto", "height":"auto"});
				$("#shopping_cart").unmask();
		   }
		);
	//);
}

function removeProductFromCart(CartId, productId, categoryId)
{	
	var agree=confirm("Are you sure you want to delete?");
	if (agree)
	{
		/*jQuery('#right-section').showLoading();
		jQuery('#right-section').load(*/
									  
			$("#shopping_cart").mask("Cart Deleting...");
			$.post(rootUrl+"include/delete_cart.php", {ct_id: CartId, productId: productId, categoryId: categoryId},
				function(data)
				{
					$("#shippingCost_Total").show("slow");
					$("#beforeUpdatedDiv").html(data);
					$("#shopping_cart").unmask();
					//jQuery('#right-section').hideLoading();
				}
			);
		//);		
	}
	else{
		return false ;
	}
}


function showAttrPrice(pdId, AttrValue, DropDownId)
{
	var AttrDDValue = $("#ddlAttrValue"+AttrValue+pdId).val();
	//var pdDetailsId = $("#EachPriceDetailsId"+pdId).val();
	//alert(pdDetailsId);
	var hiddenImpAttrTitle = $("#hiddenImpAttrTitle"+pdId).val();
	var AttrTitle = hiddenImpAttrTitle.split("|");
	var FinalTitle = "";
	for (var k=0; k<AttrTitle.length; k++)
	{
		//alert(AttrValue+" != "+AttrTitle[k]);
		if(AttrValue != AttrTitle[k])
		{
			FinalTitle = AttrTitle[k];
		}
	}
	//alert(FinalTitle);
	//alert(pdId+" "+AttrValue+" "+DropDownId);
	var formName = "attr_form_"+pdId;
	var formElms = document.getElementsByTagName("form");
	
	//alert(formElms.length);
	for (var i = 1; i<formElms.length; i++){
		//alert(formElms[i].name);
		if(formElms[i].name==formName)
		{
			var selectArray = formElms[i].getElementsByTagName("select");
			//alert(selectArray.length);
			var ddValues = "";
			var selectedValue = "";
			var selectedDDName = "";
			for (var j=0; j<selectArray.length; j++){
				selectedValue += selectArray[j].value+"|";
				selectedDDName += selectArray[j].name+"|";
			}
			//alert(selectedValue);
		}
	}
	//alert(selectedValue);
	//alert(selectArray.length);
	//alert(selectedDDName);
	
	var ddsCount = 0;			
	//alert(FinalTitle+" "+AttrValue+" "+AttrDDValue+" "+selectedValue);
	
	

	/*jQuery('#p'+pdId).showLoading(
		 {
			'afterShow': 
			function() {
				setTimeout( "jQuery('#p"+pdId+"').hideLoading()", 5000 );
			}

		
						
		 }
		);*/

  
	jQuery('#p'+pdId).showLoading();
	jQuery('#p'+pdId).load(
		$.post(rootUrl+"include/show-price.php", { pdId: pdId, AttrValue: AttrValue, AttrDDValue: AttrDDValue, ddsCount: selectArray.length, EachDDValue: selectedValue, FinalTitle: FinalTitle }, 
			function(data)
			{
				//alert(data);
				var result = data.split("|");
				//var displayDDname = selectedDDName.split("|");
				$("#EachPriceDiv"+pdId).html(result[0]);
				if(ddsCount!=1 && result[1]!="")
				{
					$("#ddlAttrValue"+FinalTitle+pdId).html(result[1]);
					//return false;				
				}			
				$("#EachPriceDetailsId"+pdId).val(result[2]);
				jQuery('#p'+pdId).hideLoading();
			}
		)
	);
	//$("#SpanEachPriceDetailsId"+pdId).html('<input type="text" name="EachPriceDetailsId'+pdId+'" id="EachPriceDetailsId'+pdId+'" value="'+pdDetailsId+'" />');
}


function showAttrPrice111(pdId, AttrValue, DropDownId)
{
	var hiddenImpAttrTitle = $("#hiddenImpAttrTitle"+pdId).val();
	var AttrDDValue = $("#ddlAttrValue"+AttrValue+pdId).val();
	//alert(hiddenImpAttrTitle);
	var AttrTitle = hiddenImpAttrTitle.split("|");
	//alert(DropDownId+" "+$("#"+DropDownId).val());
	var FinalTitle = "";
	for (var k=0; k<AttrTitle.length; k++)
	{
		//alert(AttrValue+" "+AttrTitle[k]);
		if(AttrValue != AttrTitle[k])
		{
			FinalTitle = AttrTitle[k];
		}
	}	
	//alert(FinalTitle);
	
	//alert(pdId+" "+AttrValue+" "+DropDownId);
	var formName = "attr_form_"+pdId;
	var formElms = document.getElementsByTagName("form");
	//var selectArray = document.getElementsByTagName("select");
	//alert(formArray.length+" "+selectArray.length);
	//alert(formElms.length);
	
	for (var i = 1; i<formElms.length; i++){
		//alert(formElms[i].name);
		if(formElms[i].name==formName)
		{
			var selectArray = formElms[i].getElementsByTagName("select");
			//alert(selectArray.length);
			var ddValues = "";
			var selectedValue = "";
			for (var j=0; j<selectArray.length; j++){
				//alert(DropDownId+"!="+selectArray[j].name);
				//var selectId = selectArray[j].name;
				selectedValue += selectArray[j].value+"|";
			}
			//alert(selectedValue);
			var ddsCount = 0;			
			
			$.post(rootUrl+"include/show-price.php", { pdId: pdId, AttrValue: AttrValue, AttrDDValue: AttrDDValue, ddsCount: selectArray.length, FinalTitle: FinalTitle, EachDDValue: selectedValue }, 
				function(data)
				{
					//alert(data);
					var result = data.split("|");
					$("#EachPriceDiv"+pdId).html(result[0]);
					if(ddsCount!=1 && result[1]!="")
					{
						$("#ddlAttrValue"+FinalTitle+pdId).html(result[1]);
						return false;
					}
				}
			);
			
			/*if(selectArray.length!=1)
			{
				
			}
			else
			{
				$.post("include/show-price.php", { pdId: pdId, AttrValue: AttrValue, AttrDDValue: AttrDDValue}, 
					function(data)
					{
						//alert(data);
						var result = data.split("|");
						//$("#"+selectId).html(result[0]);
						$("#EachPriceDiv"+pdId).html(result[0]);
					}
				);
			}*/
		}
	}
	/*
	$.post("include/show-price.php", { pdId: pdId, AttrValue: AttrValue, AttrDDValue: AttrDDValue}, 
		function(data1)
		{
			//alert(data1);		
			//$("#ajax_loader"+pdId).hide("slow");
			//$("#hiddenAttr"+TitleDiv+pdId).val(AttrValue);
			$("#EachPriceDiv"+pdId).html(data1);
			
			//$("#SpanEachPriceDetailsId"+pdId).html('<input type="hidden" name="EachPriceDetailsId'+pdId+'" id="EachPriceDetailsId'+pdId+'" value="'+pdDetailsId+'" />');		
		}
	);*/
	
	/*
	var hiddenAttrValue = $("#hiddenAttrValue"+AttrValue+pdId).val();
	var AttrDDValue = $("#ddlAttrValue"+AttrValue+pdId).val();
	//alert(AttrDDValue+" "+hiddenAttrValue);
	var hiddenAttrTitle = document.getElementsByName("hiddenAttr"+pdId+"[]");
	//alert(hiddenAttrTitle.length);
	
	for(var i=0; i<hiddenAttrTitle.length; i++)
	{
		alert(hiddenAttrTitle[i].value);
	}
	
	if(hiddenAttrTitle.length!=1)
	{
		$.post("include/display-attribute-dds.php", { pdId: pdId, AttrValue: AttrValue, AttrDDValue: AttrDDValue}, 
			function(data)
			{
				alert(data);
				$("#ddlAttrValue"+AttrValue+pdId).html(data);
			}
		);
	}
	
	$.post("include/show-price.php", { pdId: pdId, AttrValue: AttrValue, AttrDDValue: AttrDDValue}, 
		function(data1)
		{
			alert(data1);		
			//$("#ajax_loader"+pdId).hide("slow");
			//$("#hiddenAttr"+TitleDiv+pdId).val(AttrValue);
			$("#EachPriceDiv"+pdId).html(data1);
			
			//$("#SpanEachPriceDetailsId"+pdId).html('<input type="hidden" name="EachPriceDetailsId'+pdId+'" id="EachPriceDetailsId'+pdId+'" value="'+pdDetailsId+'" />');		
		}
	);*/
}


function showCorrectPrice(pdDetailsId, pdId, TitleDiv, AttrValue)
{
	$("#hiddenAttr"+TitleDiv+pdId).val(AttrValue);
	var AlreadyAttrValue = $("#hiddenAttr"+TitleDiv+pdId).val();
	//alert(AlreadyAttrValue);
	var elms = document.getElementsByName("hiddenAttr"+pdId+"[]");
	var hiddenElemValues = "";
	//alert(elms.length);
	var n = 0;
	if(elms.length>2)
	{
		n = 2;
	}
	else
	{
		n = elms.length;
	}
	//alert(n);
	for(var i=0; i<n; i++)
	{
		//alert(elms[i].value);
		hiddenElemValues += elms[i].value+"||";
	}
	//alert(hiddenElemValues);
	//$("#ajax_loader"+pdId).show("slow");
	$.post(rootUrl+"include/show-price.php", { pdDetailsId: pdDetailsId, pdId: pdId, AttrValue: AttrValue, AlreadyAttrValue: AlreadyAttrValue, hiddenElemValues: hiddenElemValues}, 
		function(data)
		{
			//alert(data);		
			//$("#ajax_loader"+pdId).hide("slow");
			//$("#hiddenAttr"+TitleDiv+pdId).val(AttrValue);
			$("#EachPriceDiv"+pdId).html(data);
			
			$("#SpanEachPriceDetailsId"+pdId).html('<input type="hidden" name="EachPriceDetailsId'+pdId+'" id="EachPriceDetailsId'+pdId+'" value="'+pdDetailsId+'" />');		
		}
	);
}


function showEachPrice(pdDetailsId, pdId, TitleDiv, AttrValue)
{
	//alert(pdDetailsId+" "+pdId+" "+TitleDiv+" "+AttrValue);
	//alert("#hiddenAttr"+TitleDiv+pdId);
	var AlreadyAttrValue = $("#hiddenAttr"+TitleDiv+pdId).val();
	$("#hiddenAttr"+TitleDiv+pdId).val(AttrValue);
	//alert(AlreadyAttrValue);
	var SelectedAttrValue = $("#hiddenAttr"+TitleDiv+AlreadyAttrValue).val();
	//if(SelectedAttrValue)
	//alert(SelectedAttrValue+" "+AttrValue+" "+AlreadyAttrValue);
	
	showCorrectPrice(pdDetailsId, pdId, TitleDiv, AttrValue);
	
	/*if(AlreadyAttrValue==SelectedAttrValue)
	{
		
	}*/
	/*$.post("include/show-price.php", { pdDetailsId: pdDetailsId, pdId: pdId, AttrValue: AttrValue, AlreadyAttrValue: AlreadyAttrValue}, 
		function(data)
		{
			alert(data);
			$("#hiddenAttr"+TitleDiv+pdId).val(AttrValue);
			$("#EachPriceDiv"+pdId).html(data);
			$("#EachPriceDetailsId"+pdId).val(pdDetailsId);
		}
	);*/
}


function trim1(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}


function updateCartForm(checkFlag)
{
	var ProducIds = document.getElementsByName("hidProductId[]");
	var txtQtys = document.getElementsByName("txtQty[]");
	var CartIds = document.getElementsByName("hidCartId[]");
	var ProductDetIds = document.getElementsByName("hidProductDetId[]");
	
	var arrPdIds = new Array();
	var arrQtys = new Array();
	var arrCartIds = new Array();
	var arrProductDetIds = new Array();
	
	for(var i=0; i<ProducIds.length; i++){
		arrPdIds[i] = ProducIds[i].value;
		arrQtys[i] = txtQtys[i].value;
		arrCartIds[i] = CartIds[i].value;
		arrProductDetIds[i] = ProductDetIds[i].value;
	}
	
	$("#shopping_cart").mask("Cart Updating...");
	$.post(rootUrl+"include/update-cart.php", { arrPdIds: arrPdIds, arrQtys: arrQtys, arrCartIds: arrCartIds, arrProductDetIds: arrProductDetIds }, 
		function(data){		
			//alert(data)
			$("#shippingCost_Total").show("slow");
			//$("#beforeUpdatedDiv").hide("slow");
			$("#beforeUpdatedDiv").html(data);
			$("#shopping_cart").unmask();
		}
	);
	/*if(checkFlag==0){
		return false;
	}*/
	return false;
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


function StoreCouponCode()
{
	//var appCoupon = trim1($("#txtAppCoupon").val().toUpperCase());
	var appCoupon = trim1($("#txtAppCoupon").val());
	//alert(appCoupon);
	var appCouponCaps = appCoupon.toUpperCase();
	//alert(appCouponCaps)
	var ShippingCost = $("#txtShippingCost").val();
	var SubTotal = $("#txtSubTotal").val();
	var Total = $("#txtTotal").val();
	var ProducIds = document.getElementsByName("hidProductId[]");
	var txtQtys = document.getElementsByName("txtQty[]");
	var flag1 = 0;
	var flag2 = 0;
	var flag3 = 0;
	var checkFlag = 0;
	var PreviousQty = 0;
	
	for(var q=0; q<txtQtys.length; q++){
		PreviousQty = $("#txtPreviousQty"+ProducIds[q].value).val();
		if(PreviousQty!=txtQtys[q].value){
			checkFlag = checkFlag+1;
			$("#txtPreviousQty"+ProducIds[q].value).val(txtQtys[q].value);
		}
	}
	
	if(checkFlag!=0){
		/*alert("Please update the quantity.");
		checkFlag=0;
		return false;*/
		updateCartForm(checkFlag);
		checkFlag=0;
	} 

/*$('#right-section').showLoading();
	$('#right-section').load(	
		
		
	$('#right-section').hideLoading();			
);*/
	if(appCouponCaps=="")
	{
		alert("Please enter Coupon code.");
		$("#txtAppCoupon").focus();
		return false;
	}
	else if(appCouponCaps!="POWER" && appCouponCaps!="SAVETEN"){
		alert("Enter valid coupon code.");
		$("#txtAppCoupon").val('');
		$("#txtAppCoupon").focus();
		$("#defaultDiscount").show("slow");
		$("#shippingCost_Total").hide("slow");
		//$("#shippingCost_Total").html('<hr /><strong>Total:</strong> &nbsp; <span id="priceAlign">$'+roundNumber(Total,2)+'</span><hr />');
		//$("#shippingCost_Total").show("slow");
		return false;
	}
	else if(appCouponCaps=="POWER")
	{			
		var allFlags = new Array();

		for(var i=0; i<ProducIds.length; i++){
			if(ProducIds[i].value==102){
				if(txtQtys[i].value>=1){
					allFlags[i] = txtQtys[i].value;
					flag1 = txtQtys[i].value;
				}
			}
			if(ProducIds[i].value==113){
				if(txtQtys[i].value>=1){
					allFlags[i] = txtQtys[i].value;
					flag2 = txtQtys[i].value;
				}
			}
			if(ProducIds[i].value==337){
				if(txtQtys[i].value>=1){
					allFlags[i] = txtQtys[i].value;
					flag3 = txtQtys[i].value;
				}
			}
		}
		
		var my_array = allFlags;
		my_array = my_array.filter(emptyElement);
		
		Array.prototype.min = function() {
		  return Math.min.apply(null, this)
		}
		
		var minValCount = my_array.min();
		
		 if(minValCount!=0 && flag1!=0 && flag2!=0 && flag3!=0)
		 {
			 
			//$("#shopping_cart").css({"width":"230px", "height":newHeight});
			$("#shopping_cart").mask("Validating...");
			/*$('#right-section').showLoading();
			$('#right-section').load(*/
				$.post(rootUrl+"include/apply-coupon.php", { CouponCode: appCouponCaps, ShippingCost: ShippingCost, SubTotal: SubTotal, finalCountingVal: minValCount },
					function(data)
					{
						//$("#discountAmntDiv").hide("slow");
						$("#defaultDiscount").hide("slow");
						$("#shippingCost_Total").show("slow");
						$("#shippingCost_Total").html(data);
						//$('#right-section').hideLoading();
						$("#shopping_cart").unmask();
					}
				);
			//);
		} else {
			alert("Not valid coupon for this product(s).");
			//$("#defaultDiscount").show("slow");
			//$("#shippingCost_Total").hide("slow");
			return false;
		}
	}
	else if(appCouponCaps=="SAVETEN")
	{
		/*$('#right-section').showLoading();
		$('#right-section').load(*/
			 $("#shopping_cart").mask("Validating...");
			$.post(rootUrl+"include/apply-coupon.php", { CouponCode: appCouponCaps, ShippingCost: ShippingCost, SubTotal: SubTotal, finalCountingVal: "" },
				function(data)
				{
					//alert(data)
					//$("#discountAmntDiv").hide("slow");
					$("#defaultDiscount").hide("slow");
					$("#shippingCost_Total").show("slow");
					$("#shippingCost_Total").html(data);
					//$('#right-section').hideLoading();
					 $("#shopping_cart").unmask();
				}
			);
		//);
	}
	/*else if(appCoupon!="")
	{
		$('#right-section').showLoading();
		$('#right-section').load(
			$.post(rootUrl+"include/apply-gc-coupon.php", { CouponCode: appCoupon },
				function(data)
				{
					//alert(data)
					if(data==0){
						alert("Enter valid coupon code.");
						$("#txtAppCoupon").val('');
						$("#discountAmntDiv").hide("slow");
						$("#defaultDiscount").show("slow");
						$("#shippingCost_Total").hide("slow");
						return false;
					} else {
						$("#defaultDiscount").hide("slow");
						$("#shippingCost_Total").hide("slow");
						$("#discountAmntDiv").show("slow");
						$("#discountAmntDiv").html(data);
					}
					$('#right-section').hideLoading();
					//$("#defaultDiscount").hide("slow");
					//$("#shippingCost_Total").show("slow");
					//$("#shippingCost_Total").html(data);
				}
			)
		);
	}*/
	
}



function emptyElement(element) {
	//Removes nulls, zeros (also falses), text version of false, and blank element
	if (element == null || element == 0 || element.toString().toLowerCase() == 'false' || element == '')
		return false;
	else 
		return true;
}


function giftCouponCode(){
	var appCoupon = trim1($("#txtAppCoupon").val());
	if($("#txtDisAmnt").val()==""){
		var DiscountAmnt = 0;
	} else {
		var DiscountAmnt = $("#txtDisAmnt").val();
	}
	if($("#hidDisTotal").val()==""){
		var DisTotal = 0;
	} else {
		var DisTotal = $("#hidDisTotal").val();
	}
	var ShippingCost = $("#hidShippingCost").val();
	var SubTotal = $("#hidSubTotal").val();
	var SalesTax = $("#hidSalesTax").val();
	var Total = 0;
	
	if(appCoupon=="")
	{
		alert("Please enter a valid coupon.");
		$("#txtAppCoupon").val('');
		$("#txtDisAmnt").val('');
		$("#txtAppCoupon").focus();
		return false;
	}
	else {
		$('#giftCoupon').showLoading();
		$('#giftCoupon').load(
			$.post(rootUrl+"include/apply-gc-coupon.php", { CouponCode: appCoupon },
				function(data)
				{					
					//alert(data)
					if(data==0){
						alert("Enter valid coupon code.");
						$("#txtAppCoupon").val('');
						$("#txtDisAmnt").val('');
						$("#disAmountRow").hide("slow");
						$("#discountAmntDiv").hide("slow");
						$("#displayAmountDiv").hide("slow");
						$("#disAmountRow").hide("slow");
						
						Total = parseFloat(SubTotal) + parseFloat(ShippingCost) + parseFloat(SalesTax) - parseFloat(DiscountAmnt) - parseFloat(DisTotal);
						$("#disAmount").html("$"+parseFloat(DiscountAmnt).toFixed(2));
						$("#totalAmount").html("$"+parseFloat(Total).toFixed(2));
						//return false;
					} else if(data=="error"){
						alert("Please enter valid gift certificate coupon code.");
						$("#txtAppCoupon").val('');
						$("#txtDisAmnt").val('');
						$("#disAmountRow").hide("slow");
						$("#discountAmntDiv").hide("slow");
						$("#displayAmountDiv").hide("slow");
						$("#disAmountRow").hide("slow");
						DiscountAmnt = 0;
						Total = parseFloat(SubTotal) + parseFloat(ShippingCost) + parseFloat(SalesTax) - parseFloat(DiscountAmnt) - parseFloat(DisTotal);
						$("#disAmount").html("$"+parseFloat(DiscountAmnt).toFixed(2));
						$("#totalAmount").html("$"+parseFloat(Total).toFixed(2));
						//return false;
						
					} else {
						$("#txtDisAmnt").val('');
						$("#disAmountRow").hide("slow");
						$("#discountAmntDiv").show("slow");
						$("#displayAmountDiv").show("slow");
						$("#displayAmountDiv").html(data);
						
						Total = parseFloat(SubTotal) + parseFloat(ShippingCost) + parseFloat(SalesTax) - parseFloat(DiscountAmnt) - parseFloat(DisTotal);
						$("#disAmount").html("$"+parseFloat(DiscountAmnt).toFixed(2));
						$("#totalAmount").html("$"+parseFloat(Total).toFixed(2));
					}
					$('#giftCoupon').hideLoading();
				}
			)
		);
	}
}


function giftCertificateCouponCode()
{	
	var appCoupon = trim1($("#txtAppCoupon").val());
	var DiscountAmnt = $("#txtDisAmnt").val();
	var ShippingCost = $("#hidShippingCost").val();
	var SubTotal = $("#hidSubTotal").val();
	var SalesTax = $("#hidSalesTax").val();
	var DisTotal = $("#hidDisTotal").val();
	var Total = "";
	if(DiscountAmnt=="")
	{
		alert("Please enter amount.");
		$("#txtDisAmnt").focus();
		return false;
	}
	
	$('#giftCoupon').showLoading();
	$('#giftCoupon').load(
		$.post(rootUrl+"include/apply-gc-coupon.php", { CouponCode: appCoupon, DiscountAmnt: DiscountAmnt, ShippingCost: ShippingCost, SubTotal: SubTotal, SalesTax: SalesTax },
			function(data)
			{
				//alert(data);
				if(data=="success"){
					Total = parseFloat(SubTotal) + parseFloat(ShippingCost) + parseFloat(SalesTax) - parseFloat(DiscountAmnt) - parseFloat(DisTotal);
					$("#disAmountRow").show("slow");
					$("#disAmount").html("$"+parseFloat(DiscountAmnt).toFixed(2));
					$("#totalAmount").html("$"+parseFloat(Total).toFixed(2));
					$("#hidTotal").val(parseFloat(Total).toFixed(2));
				} else if(data=="error"){
					alert("Please enter valid gift certificate coupon code.");
					//return false;
				} else {
					alert(data);
					$("#txtDisAmnt").val('');
					//$("#disAmount").html("$0.00");
					//$("#totalAmount").html("$"+parseFloat(Total).toFixed(2));
				}
				//$("#discountAmntDiv").show("slow");
				//$("#discountAmntDiv").html(data);
				$('#giftCoupon').hideLoading();
				//$("#txtDisAmnt").val('');
				//$("#txtDisAmnt").focus();
				/*if(data==0){
					alert("Your Gift Certificate value");
				} else {
					$("#defaultDiscount").show("slow");
					$("#defaultDiscount").html(data);
				}*/
			}
		)
	);
}


function isFloat3to5d0to2(str) {
    /* Verify float 3 to 5 digits, decimal max 3 digits
     *    if decimal point must have at least 1 decimal digit
     * Return boolean
     */
    str = str.replace(/^\s+|\s+$/g, '');
	var testValid = /^[-+]?\d{1,5}(\.\d{1,2})?$/.test(str);
    if(testValid==false){
		alert("Please enter valid amount.");
		$("#txtDisAmnt").val('');
		//$("#txtDisAmnt").focus();
		return false;
	}
	
}//eof - isFloat3to5d10to3
