// JavaScript Document
var xmlHttp=null;

/* Magic Box Modules */
function showproduct_MagicBox(catalogid,quantity,freeproduct)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="getproduct_MagicBox.asp";
	url=url+"?catalogid="+catalogid;
	url=url+"&quantity="+quantity;
	url=url+"&action=insert";	
	url=url+"&freeproduct="+freeproduct;		
	url=url+"&sid="+Math.random();
//	url=url+"&src=";
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);

}

function fn_MBox_submit()
{
	document.frm_quickorder.method="post";
	document.frm_quickorder.action="loginout.asp";
	document.frm_quickorder.submit();	
}

function shop_showproduct_MBox(catalogid,quantity,totalfreeproducts,freeproductschosen,freeproduct)
{ 
	if (freeproduct=='Y')
	{
		if ((parseInt(quantity))>parseInt(totalfreeproducts))	
		{
			{
				alert("You can choose "+ totalfreeproducts +" FREE Product only");
				return;
			}
		}
		else
		{
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
			  {
			  alert ("Your browser does not support AJAX!");
			  return;
			  } 
			var url="getproduct_MagicBox.asp";
			url=url+"?catalogid="+catalogid;
			url=url+"&quantity="+quantity;
			url=url+"&action=update";	
			url=url+"&sid="+Math.random();
			url=url+"&freeproduct=Y";
			xmlHttp.onreadystatechange=stateChanged;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(url);
		}
	}
	else if (freeproduct=='N')
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Your browser does not support AJAX!");
		  return;
		  } 
		var url="getproduct_MagicBox.asp";
		url=url+"?catalogid="+catalogid;
		url=url+"&quantity="+quantity;
		url=url+"&action=update";	
		url=url+"&sid="+Math.random();
		url=url+"&freeproduct=N";
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("POST",url,true);
		xmlHttp.send(url);
	}
}

function deleteproduct_MBox(catalogid)
{
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="getproduct_MagicBox.asp";
	url=url+"?catalogid="+catalogid;
	url=url+"&action=delete";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);
	
}

/* End of Magic Box Modules */




function fn_checkupsel(prodid,qty)
{
		showproduct(prodid,qty,'N');	
}

function deleteproduct(catalogid)
{	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="getproduct.asp";
	url=url+"?catalogid="+catalogid;
	url=url+"&action=delete";
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);	
}

function showproduct(catalogid,quantity,freeproduct)
{ 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="getproduct.asp";
	url=url+"?catalogid="+catalogid;
	url=url+"&quantity="+quantity;
	url=url+"&action=insert";	
	url=url+"&freeproduct="+freeproduct;		
	url=url+"&sid="+Math.random();
//	url=url+"&src=";
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);
	
//	document.getElementById("txtHint").innerHTML;
}

function fn_checkfreeproducts(totfreeproducts,freeproductschosen)
{
	var catalogid,quantity;
	catalogid=document.getElementById("special_product_name").value;
	quantity=document.getElementById("special_quantity").value;
	if (totfreeproducts=="")
	{
		totfreeproducts=0;
	}
	if (freeproductschosen=="")
	{
		freeproductschosen=0;	
	}
	
	if (catalogid!="" && quantity !="")
	{
		if ((parseInt(freeproductschosen)+parseInt(quantity)) > parseInt(totfreeproducts))
		{
//			alert("You can choose " + totfreeproducts +" FREE product only");
			alert("Please choose "+ parseInt(totfreeproducts-freeproductschosen) +" FREE product to add to cart");
			return;
			
		}
		showproduct(catalogid,quantity,'Y');
	}
	else if (catalogid=="")
	{
		alert("Select any product");
		document.getElementById("special_product_name").focus();
		return;
	}
	else if (quantity=="")
	{
		alert("Please select the Quantity");
		document.getElementById("special_quantity").focus();
		return;
	}
}

function fn_test()
{
	var selected_val;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	  
	for(i=1;i<=document.getElementById("hid_shipmethodcnt").value;i++)
	{
		if(document.getElementById("rad_shipmethods"+i).checked==true)
		{
		    selected_val=document.getElementById("rad_shipmethods"+i).value;			
		}
	}

	var url="getproduct.asp";
//	url=url+"?catalogid="+catalogid;
	url=url+"?shipcost="+selected_val;
	url=url+"&action=ship";	
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);
	document.getElementById("txtHint").innerHTML;
	  
	  

}

function calculateshipping(shipval)
{
	var selected_val,id;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 

/*	for (i=0;i<=document.getElementById("hid_shipmethodcnt").value;i++)
	{
		if(document.getElementById("rad_shipmethods"+cnt).checked==true)
		{*/
			//selected_val=document.getElementById("rad_shipmethods"+cnt).value;	
/*		}
		
	}*/
	var url="getproduct.asp";
//	url=url+"?shipcost="+selected_val;
	url=url+"?shipmethodid="+shipval;	
	url=url+"&action=ship";	
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);
	document.getElementById("txtHint").innerHTML;
}

function shop_showproduct(catalogid,quantity,totalfreeproducts,freeproductschosen,freeproduct)
{ 
//alert("Catalogid="+catalogid);
//alert("Quantity="+quantity);
//alert("Totalfreeproducts="+totalfreeproducts);
//alert("Free Proudcts Chosen="+freeproductschosen);
//alert("Free Product="+freeproduct);
//return;

	if (freeproduct=='Y')
	{
//		alert(parseInt(quantity)+parseInt(freeproductschosen));
//		alert(parseInt(totalfreeproducts));
		
		if ((parseInt(quantity))>parseInt(totalfreeproducts))	
		{
/*			if (freeproductschosen==totalfreeproducts)
			{
				alert("You have already chosen "+ totalfreeproducts +" FREE products.  click the Continue button to proceed");
				return;
			}
			else
*/			
			{
				alert("You can choose "+ totalfreeproducts +" FREE Product only");
				return;
			}
		}
		else
		{
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
			  {
			  alert ("Your browser does not support AJAX!");
			  return;
			  } 
			var url="getproduct.asp";
			url=url+"?catalogid="+catalogid;
			url=url+"&quantity="+quantity;
			url=url+"&action=update";	
			url=url+"&sid="+Math.random();
			url=url+"&freeproduct=Y";
			xmlHttp.onreadystatechange=stateChanged;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(url);
		}
	}
	else if (freeproduct=='N')
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		  alert ("Your browser does not support AJAX!");
		  return;
		  } 
		var url="getproduct.asp";
		url=url+"?catalogid="+catalogid;
		url=url+"&quantity="+quantity;
		url=url+"&action=update";	
		url=url+"&sid="+Math.random();
		url=url+"&freeproduct=N";
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("POST",url,true);
		xmlHttp.send(url);
	}
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
//		var xmlDoc=xmlHttp.responseXML.documentElement;
		var a,b;
		a=xmlHttp.responseText;
		b=a.split("--");
		document.getElementById('txtHint').innerHTML=b[0];
		document.getElementById('items').innerHTML=b[1];
		document.getElementById('totcost').innerHTML=b[2];	
		document.getElementById('totitems').innerHTML=b[1];					
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}


function fn_submit(totalfreeproducts,freeproductschosen,shipmethodid)
//function fn_submit(shipmethodid)
{
	var shipmentflag;
	shipmentflag=false;
	if (freeproductschosen=="")
	{
		freeproductschosen=0;	
	}
	if (parseInt(freeproductschosen) < parseInt(totalfreeproducts))
	{
		alert("Please select "+ parseInt(totalfreeproducts-freeproductschosen) + " FREE product(s) to continue");
		return;
	}
	else if (parseInt(totalfreeproducts)==parseInt(freeproductschosen))
	{    // To comment the above coding once the free products module is closed
		
		var x,i;
		x=document.getElementsByName("shipmentoptions");
		for (i=0;i<x.length;i++)
		{
			//if(x[i].checked==true)
			if(document.getElementsByName("shipmentoptions")[i].checked==true)
			{
				shipmentflag=true;
				break;
			}
		}
		if (shipmentflag==true)
		{				
			document.frm_quickorder.method="post";
			document.frm_quickorder.action="loginout.asp";
			document.frm_quickorder.submit();
		}
		else
		{
			alert("Select the mode of shipment");
			//document.getElementById("shipmsg").style.display="block";
			document.getElementsByName("shipmentoptions")[0].focus();			
			document.getElementById("hidshipoptions").focus();
			return;
		}

	} /* to comment from this line once free product offer is closed */
	else if (parseInt(freeproductschosen) > parseInt(totalfreeproducts))
	{
		alert("Please remove the FREE products to continue");
		return;
	}
}

function show_stockupdate(catalogid)
{

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="StockUpdate_ProductDetails.asp";
	url=url+"?catalogid="+catalogid;
//	alert(url);
	xmlHttp.onreadystatechange=stateChanged_StockUpdate;	
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);
}
function stateChanged_StockUpdate() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var a,b;
		a=xmlHttp.responseText;
//		alert(a);
		document.getElementById('show').innerHTML=a;
		document.getElementById('show').style.visibility="visible";
	}
}

function fn_bcsamplegiftpack(id)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	  {
	  alert ("Your browser does not support AJAX!");
	  return;
	  } 
	var url="getproduct.asp";
	url=url+"?catalogid=547";
	url=url+"&quantity=1";
	url=url+"&action=insert";	
//	url=url+"&freeproduct="+freeproduct;		
	url=url+"&sid="+Math.random();
//	url=url+"&src=";
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("POST",url,true);
	xmlHttp.send(url);
}


function fn_discount()
{
	if(spacecheck(document.getElementById("txt_discount").value)==false)
	{
		alert("Enter the Coupon");
		document.getElementById("txt_discount").focus();
		return;
	}
	else
	{
		/*document.frm_checkout.method="post";
		document.frm_checkout.action="quick_orderform_discount.asp";
		document.frm_checkout.submit();	*/
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		var url="getproduct.asp";
		url=url+"?action=discount";
		url=url+"&txt_discount="+document.getElementById("txt_discount").value;
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=stateChanged;
		xmlHttp.open("POST",url,true);
		xmlHttp.send(url);
	}
}

function fn_viewbalance_egiftcard(act)
{
	var res,eGCard,eGPin;
	eGCard=document.getElementById("txt_egc_cardnumber").value;
	eGPin=document.getElementById("txt_egc_pin").value;
	document.getElementById("eGiftCardMsg").innerHTML="";
	
	if (act=="balapp")
	{
		res=checkshipmentoptions();
	
		if (res==false)
		{
			alert("Select the mode of shipment");
			document.getElementsByName("shipmentoptions")[0].focus();			
			document.getElementById("hidshipoptions").focus();
			return;
		}			
	}
	if(spacecheck(document.getElementById("txt_egc_cardnumber").value)==false)
	{
		alert("Enter the eGift Card Number");
		document.getElementById("txt_egc_cardnumber").focus();
		return;
	}
	else if (isNaN(document.getElementById("txt_egc_cardnumber").value)==true)
	{
		alert("Only Numeric values are allowed");
		document.getElementById("txt_egc_cardnumber").value="";
		document.getElementById("txt_egc_pin").value="";
		document.getElementById("txt_egc_cardnumber").focus();				
		return;		
	}
	else if(spacecheck(document.getElementById("txt_egc_pin").value)==false)
	{
		alert("Enter the eGift Card Pin Number");
		document.getElementById("txt_egc_pin").focus();
		return;		
	}
	else if (isNaN(document.getElementById("txt_egc_pin").value)==true)
	{
		alert("Only Numeric values are allowed");
		document.getElementById("txt_egc_pin").value="";
		document.getElementById("txt_egc_pin").focus();				
		return;		
	}
	else if (eGCard.length!=16)
	{
		alert("Invalid eGift Card Number");
		document.getElementById("txt_egc_cardnumber").value="";
		document.getElementById("txt_egc_pin").value="";
		document.getElementById("txt_egc_cardnumber").focus();				
		return;	
	}
	else if (eGPin.length!=6)
	{
		alert("Invalid eGift Card Pin Number");
		document.getElementById("txt_egc_pin").value="";
		document.getElementById("txt_egc_pin").focus();				
		return;	
	}	
	else
	{
		document.getElementById("loader_egiftcard").style.display="block";
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		var url="eGiftCardBalance.aspx";
		url=url+"?action=balance";
		url=url+"&cardnumber="+document.getElementById("txt_egc_cardnumber").value;
		url=url+"&pinnumber="+document.getElementById("txt_egc_pin").value;		
		url=url+"&act="+act;
		xmlHttp.onreadystatechange=stateChanged_eGiftCard;
		xmlHttp.open("POST",url,true);
		xmlHttp.send(url);
	}
}

/*function fn_checkinput(val)
{
	if(isNaN(val)==true)
	{
		alert("Enter numeric values only");
		return;
	}
}*/

function stateChanged_eGiftCard() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var a,b;
		a=xmlHttp.responseText;
		var xmlDoc=xmlHttp.responseXML.documentElement;
		var result,getCurrentBatchNumber,getTransactionId,getCardNumber,getBalanceAmount,getExpiry,getApprovalCode,errormessage,act;
		getBalanceAmount=0;
		
		result=xmlDoc.getElementsByTagName("result")[0].childNodes[0].nodeValue;

		if(result=="0")
		{
			errormessage=xmlDoc.getElementsByTagName("errormessage")[0].childNodes[0].nodeValue;			
		}
		else
		{
			getCurrentBatchNumber=xmlDoc.getElementsByTagName("getCurrentBatchNumber")[0].childNodes[0].nodeValue;
			getTransactionId=xmlDoc.getElementsByTagName("getTransactionId")[0].childNodes[0].nodeValue;
			getCardNumber=xmlDoc.getElementsByTagName("getCardNumber")[0].childNodes[0].nodeValue;
			getBalanceAmount=xmlDoc.getElementsByTagName("getBalanceAmount")[0].childNodes[0].nodeValue;
			getExpiry=xmlDoc.getElementsByTagName("getExpiry")[0].childNodes[0].nodeValue;
			getApprovalCode=xmlDoc.getElementsByTagName("getApprovalCode")[0].childNodes[0].nodeValue;
			act=xmlDoc.getElementsByTagName("act")[0].childNodes[0].nodeValue;
			
			if (Trim(getBalanceAmount)=="")
			{
				getBalanceAmount=0;
			}
			if (Trim(getExpiry)=="")
			{
				getExpiry="";
			}					
		}
		if(result=="0")
		{
			document.getElementById("eGiftCardMsg").innerHTML=errormessage;
		}
		else if (result=="1")
		{
			document.getElementById("eGiftCardMsg").innerHTML="Card Balance 	-	Rs. "+ getBalanceAmount +"<BR>Card Expiry	-	"+getExpiry;
			document.getElementById("txt_eGiftCardBalanceAmount").value=getBalanceAmount;			
			if(act=="balapp")
			{
				if(parseFloat(getBalanceAmount)>0)
				{
					document.getElementById("cmd_egc_submit").disabled=false;
					document.getElementById("cmd_egc_submit").style.background="#00A5B6";
				}
			}
		}
		document.getElementById("loader_egiftcard").style.display="none";
		document.getElementById("txt_egc_cardnumber").disabled=true;
		document.getElementById("txt_egc_pin").disabled=true;
	
//		document.getElementById('show').innerHTML=a;
//		document.getElementById('show').style.visibility="visible";
	}	
}


function egc_reset()
{
	document.getElementById("eGiftCardMsg").innerHTML="";
	document.getElementById("txt_egc_cardnumber").disabled=false;
	document.getElementById("txt_egc_pin").disabled=false;	
	document.getElementById("txt_egc_cardnumber").value="";
	document.getElementById("txt_egc_pin").value="";
	document.getElementById("txt_egc_cardnumber").focus();
}


function fn_apply_egiftcard(shipmethodid)
{
		var res;
		res=checkshipmentoptions();
		if (res==false)
		{
			alert("Select the mode of shipment");
			document.getElementsByName("shipmentoptions")[0].focus();			
			document.getElementById("hidshipoptions").focus();
			return;
		}		
		if(Trim(document.getElementById("txt_eGiftCardBalanceAmount").value)=="")
		{
			alert("Click on the View Balance to check the view balance");
			document.getElementById("cmd_egc_viewbalance").focus();
			return;
		}		
		else
		{
			xmlHttp=GetXmlHttpObject();
			if (xmlHttp==null)
			{
				alert ("Your browser does not support AJAX!");
				return;
			} 
			var url="getproduct.asp";
			url=url+"?action=applyegiftcard";
			url=url+"&balanceamount="+document.getElementById("txt_eGiftCardBalanceAmount").value;
			url=url+"&cardnumber="+document.getElementById("txt_egc_cardnumber").value;
			url=url+"&cardpin="+document.getElementById("txt_egc_pin").value;
			xmlHttp.onreadystatechange=stateChanged;
			xmlHttp.open("POST",url,true);
			xmlHttp.send(url);		
		}
}

function checkshipmentoptions()
{
	var x,i,shipmentflag;
		shipmentflag=false;
		x=document.getElementsByName("shipmentoptions");
		for (i=0;i<x.length;i++)
		{
			if(document.getElementsByName("shipmentoptions")[i].checked==true)
			{
				shipmentflag=true;
				break;
			}
		}
		return shipmentflag;
/*		alert(shipmentflag);
		if (shipmentflag==false)
		{
			alert("Select the mode of shipment");
			document.getElementsByName("shipmentoptions")[0].focus();			
			document.getElementById("hidshipoptions").focus();
			return;
		}*/
}


