﻿// JScript File
	function calculate(n,prodSFperUNIT,prodSF,qty)
	{
	
	  var totalctns;
	  var minctns = 5;
	  var minorder = Math.ceil(prodSFperUNIT * minctns)
	
	 if(document.getElementById(prodSF).value == "")
	  {
	    alert("Please enter sq.footage in numbers only");
	    document.getElementById(prodSF).value="";
	    document.getElementById(prodSF).focus();
	    return false;
	  }
	  if(isNaN(document.getElementById(prodSF).value))
	  {
	    alert("Please enter sq.footage in numbers only");
	    document.getElementById(prodSF).value="";
	    document.getElementById(prodSF).focus();
	    return false;
	  }
	
	  
	
	  if(document.getElementById(prodSF).value < minorder)
	  {
	  	document.getElementById(qty).value = minctns;
		alert("You must order at least 5 cartons.");
	  }
	  else
	  {
		totalctns = Math.ceil(document.getElementById(prodSF).value/prodSFperUNIT);
		document.getElementById(qty).value = totalctns;
	  }
	
	  return false;
	}
	
function DoAddToCart(qty){

//var objForm = document.orderAform<%=varCnt%>
var valid = "0123456789"
var ok = "yes";
var temp;

//-- check for blanks --\\
if(document.getElementById(qty).value == ""){
 alert("Please enter a carton amount.\nIf you have entered your amount in Sq. Ft.,\nplease click the 'Calculate' button.");
 document.getElementById(qty).focus();
 return false;
}

//-- check for numbers --\\
for (var i=0; i<document.getElementById(qty).value.length; i++) {
  temp = "" + document.getElementById(qty).value.substring(i, i+1);
  if (valid.indexOf(temp) == "-1") ok = "no";
}

if (ok == "no") {
  alert("Please enter a valid carton amount");
  document.getElementById(qty).focus();
  return false;
}

//-- check for at least 1 --\\
if(Number(document.getElementById(qty).value) < 1){
 alert("Please enter a carton amount greater than 0.\nIf you have entered your amount in Sq. Ft.,\nplease click the 'Calculate' button.");
 document.getElementById(qty).focus();
 return false;
}

return true;

}
function fnAddtoCart(id,qtyid,callbackfunction,minimumorder,flag)
{

    if(flag==0 || DoAddToCart(qtyid)){
    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
}