﻿// JScript File
function fnAddtoCart(id,qtyid,callbackfunction,minimumorder)
{
    if(document.getElementById(qtyid).value.length==0)
    {
        alert('Please enter number of '+document.getElementById(id+'_packageLabel').value+' for add to cart.');
        return false;
    }
     if(parseInt(document.getElementById(qtyid).value) < minimumorder)
    {
        alert('You must order at least '+minimumorder+' '+ document.getElementById(id+'_packageLabel').value +'');
        return false;
    }
    var productID=document.getElementById(id+'_productID').value;
    var departmentID=document.getElementById(id+'_departmentID').value;
    var customerID=document.getElementById(id+'_customerID').value;
    var createDate=document.getElementById(id+'_createDate').value;
    var SKU=document.getElementById(id+'_SKU').value;
    var colorName=document.getElementById(id+'_colorName').value;
    var cartDescription=document.getElementById(id+'_cartDescription').value;
    var brandName=document.getElementById(id+'_brandName').value;
    var unitLabel=document.getElementById(id+'_unitLabel').value;
    var packageLabel=document.getElementById(id+'_packageLabel').value;
    var unitsPerPackage=document.getElementById(id+'_unitsPerPackage').value;
    var unitPrice=document.getElementById(id+'_unitPrice').value;
    var quantity=document.getElementById(qtyid).value;
    var weight=document.getElementById(id+'_weight').value;
    var isParcel=document.getElementById(id+'_isParcel').value;
    var extendedPrice=document.getElementById(id+'_extendedPrice').value;
    
    var productOption="Not";
    if(document.getElementById(id+'_ProductOptionsID')!=null){
        var productOptionID=document.getElementById(id+'_ProductOptionsID').value;
        
       
        var arrOption=productOptionID.split(";");
        for(i=0;i<arrOption.length;i++)
        {
            if(i==0)
                productOption=document.getElementById(arrOption[i]).value.split(";")[0];
            else
                productOption=productOption+";"+document.getElementById(arrOption[i]).value.split(";")[0];
        }
    }
    var postData='ID='+ encodeURIComponent(id) +'&';
    postData=postData+'productID='+ encodeURIComponent(productID) +'&';
    postData=postData+'departmentID='+ encodeURIComponent(departmentID) +'&';
    postData=postData+'customerID='+ encodeURIComponent(customerID) +'&';
    postData=postData+'createDate='+ encodeURIComponent(createDate) +'&';
    postData=postData+'SKU='+ encodeURIComponent(SKU) +'&';
    postData=postData+'colorName='+ encodeURIComponent(colorName) +'&';
    postData=postData+'cartDescription='+ encodeURIComponent(cartDescription) +'&';
    postData=postData+'brandName='+ encodeURIComponent(brandName) +'&';
    postData=postData+'unitLabel='+ encodeURIComponent(unitLabel) +'&';
    postData=postData+'packageLabel='+ encodeURIComponent(packageLabel) +'&';
    postData=postData+'unitsPerPackage='+ encodeURIComponent(unitsPerPackage) +'&';
    postData=postData+'unitPrice='+ encodeURIComponent(unitPrice) +'&';
    postData=postData+'quantity='+ encodeURIComponent(quantity) +'&';
    postData=postData+'weight='+ encodeURIComponent(weight) +'&';
    postData=postData+'isParcel='+ encodeURIComponent(isParcel) +'&';
    postData=postData+'extendedPrice='+ encodeURIComponent(extendedPrice) +'&';
    postData=postData+'ProductOptionsID='+ encodeURIComponent(productOption);
    
    SendAjaxRequest(varWebsiteRoot+'/AddToCart.aspx',callbackfunction,postData);
    document.getElementById(id+'_AjaxLoader').style.display='block';
    
    return false;
}
function RoundDecimal(num)
{

var rnum = parseFloat(num);
var rlength = 2; // The number of decimal places to round to
if (rnum > 8191 && rnum < 10485) {
rnum = rnum-5000;
var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
newnumber = newnumber+5000;
} else {

var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
}
 newnumber;

  var s = "" + Math.round(newnumber * 100) / 100
  var i = s.indexOf('.')
  if (i < 0) return s + ".00"
  var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
  if (i + 2 == s.length) t += "0"
  return t


}
function SendAjaxRequest(FormName,CallBackFunc,Postdata)
{
    
    if(FormName.length==0)
	{
		alert ("Please specify form name.")
		return false;
	}
    xmlHttp=GetXmlHttpObject1();
    if(xmlHttp==null)
	{
		alert('Browser does not support ajax.');
		return false;
	}
	else
	{
		var url=FormName;
		xmlHttp.onreadystatechange=function() { CallBackFunc(xmlHttp) };
    	xmlHttp.open("POST",url,true)
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.send(Postdata)
		return true;
	}
	
	xmlHttp=null;
}
function GetXmlHttpObject1()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}