/**
 *JavaScript Shopping Cart
 *
 *(c) 2008 Bodhtree <http://bodhtree.com>
 *Coded by: Syed Abdul Baqi  
 *Date: 14th Nov 2008
 */   

    var newsletter = new Array();     //javascript array to keep track of newsletters added in cart
    
    function getProduct(id, name, met)     //get the list for products, based on category id
    {
      if(met === undefined)     //getting currency mode from back to cart
      {
        if(document.frmProduct.method[0].checked == true)     //check the currency mode
        {
          var method = 'inr';
        }
        else
        {
          var method = 'usd';
        }
      }
      else
      {
        var method = met;
      }
      document.getElementById('category_name').innerHTML = name;    //category name in the header

      //removing all the child elements from table (id=product)
      var TabPlane = document.getElementById("product").getElementsByTagName('tbody')[0];
      while (TabPlane.childNodes.length >= 1) 
      {
          TabPlane.removeChild(TabPlane.firstChild);
      }

      //removing all the child elements from table (id=product) which contains newsletter additional table data
      var TabPlane = document.getElementById("product").getElementsByTagName('tbody')[2];
      while (TabPlane.childNodes.length >= 1) 
      {
          TabPlane.removeChild(TabPlane.firstChild);
      }

      var url = 'common.php';
      var params = 'id=' + id + '&opt=getProduct&method=' + method;
      var ajax = new Ajax.Request(url, {method: "post", parameters: params, onLoading: loader, onComplete: product});
    }
    
    function product(data)    //populate the products
    {
      var i = 0;
      var output = data.responseText.parseJSON();
      var products = '';

      //removing class from section menu 
      var cat_len = output[2].length;
      for(i=0 ; i<cat_len ; i++)
      {
        var id = 'cat_' + output[2][i].id;
        document.getElementById(id).className = "";
      }
      var id = 'cat_' + output[1][1];
      
      //and highlighting the selected one
      document.getElementById(id).className = "select";
      
      //disable product loader      
      document.getElementById('loader').style.display = 'none';
      
      /*
      var TabPlane = document.getElementById("product").getElementsByTagName('tbody')[0];
      var newPlane = TabPlane.cloneNode(false); 
      TabPlane.parentNode.replaceChild(newPlane,TabPlane);
      */
      
      //header of products
      var optbl = document.getElementById('product').getElementsByTagName('tbody')[0];
      var newtr = document.createElement('tr');
      var newtd1 = document.createElement('th');
      var newtd2 = document.createElement('th');
      var newtd3 = document.createElement('th');
      var newtd4 = document.createElement('th');
      var newtd5 = document.createElement('th');
      
      newtd1.innerHTML = 'Product Details';
      
      //colspan=2 for magazines(subscription year) and delegates(no. of delegates)
      if(output[1][1] == 3)                               //3:Conferences
      {
        newtd5.innerHTML = 'No. of Delegate(s)';
      }
      else if(output[1][1] == 1)                          //1:Magazines
      {
        newtd5.innerHTML = 'No. of Year(s)';
      }
      else if(output[1][1] == 4 || output[1][1]== 5 ||output[1][1] == 6)      //4:CDs and 5:Conferences
      {
        newtd5.innerHTML = 'Quantity';
      }
      
      //newtd1.className = 'w186';
      //restoring currency mode after category is changed
      if(output[1][0] == 'inr')
      {
        newtd2.innerHTML = "<input type='radio' name='method' value='inr' onclick='return currencyMode(this.value);' checked/>Price (INR)";
        newtd3.innerHTML = "<input type='radio' name='method' value='usd'  onclick='return currencyMode(this.value);' />Price (USD)";
      }
      else
      {
        newtd2.innerHTML = "<input type='radio' name='method' value='inr' onclick='return currencyMode(this.value);'/>Price (INR)";
        newtd3.innerHTML = "<input type='radio' name='method' value='usd'  onclick='return currencyMode(this.value);' checked/>Price (USD)";
      }
      newtd4.innerHTML = 'Add to cart';

      //append child only for Magazines, Conferences, CDs, Reports
      newtr.appendChild(newtd1);
      if(output[1][1] == 1 || output[1][1] == 3 || output[1][1] == 4 || output[1][1]== 5 || output[1][1]== 6)
      {
        newtr.appendChild(newtd5);      
      }
      
      //appending childs to the product TBODY
      newtr.appendChild(newtd2);
      newtr.appendChild(newtd3);
      if(output[1][1] != 2)
      {
        newtr.appendChild(newtd4);
      }
      optbl.appendChild(newtr);
      
      //generating products using DOM
      var z = 0;      //variable is used to eliminate repetations of same product
      var j = 0;      //variable used as a counter for newsletter array
      for(i=0 ; i<output[0].length ; i++)
      {
        var optbl = document.getElementById('product').getElementsByTagName('tbody')[0];
        var newtr = document.createElement('tr');
        var newtd1 = document.createElement('td');
        var newtd2 = document.createElement('td');
        var newtd3 = document.createElement('td');
        var newtd4 = document.createElement('td');
        var newtd5 = document.createElement('td');
        
        newtd3.id = 'amt_inr_' + output[0][i].id;
        newtd4.id = 'amt_usd_' + output[0][i].id;
        
        if(output[1][1] == 3 && output[0][i].special_fee == 0)   //applying background color to conference (non-special)
        {
          newtd1.className = "confbg";
        }

        if(output[1][1] == 2)
        {
          newtd1.innerHTML = 'Any ' + output[0][i].sub_year;
        }
        else if(z == 0 || z != output[0][i].product)   //restrict repetation of products
        {
          newtd1.innerHTML = output[0][i].product;
          z = output[0][i].product;       //variable to avoid repetations
        }
        else
        {
          newtd1.innerHTML = '&nbsp;';
        }
        
        if(output[1][1] == 1)       //for magazines and conference append sub_year
        {
          newtd2.innerHTML = output[0][i].sub_year;
        }
        else if(output[1][1]== 3 || output[1][1] == 4)  //for CDs and Reports append static dropdown menu
        {
          newtd2.innerHTML = "<select id='qty_" + output[0][i].id + "' onchange='return loadAmount(" + output[0][i].id + "," + output[1][1] + ")'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option><option value='6'>6</option><option value='7'>7</option><option value='8'>8</option><option value='9'>9</option><option value='10'>10</option></select>";
        }
        else if(output[1][1]== 5 || output[1][1] == 6)   //for reports and directories
        {
          newtd2.innerHTML = "<input type='text' size='3' value='1' maxlength='3' id='qty_" + output[0][i].id + "' onkeypress='return numbersonly(event, false);' onkeyup='return loadAmount(" + output[0][i].id + "," + output[1][1] + ")' onblur=\"if (this.value == '' || this.value == '0') this.value=this.defaultValue;\"/>";
        }
        else
        {
          newtd2.innerHTML = '&nbsp;';
        }

        if(output[0][i].price_inr == 0)
          newtd3.innerHTML = '-';
        else
          newtd3.innerHTML = intToFormat(output[0][i].price_inr);

        if(output[0][i].price_usd == 0)
          newtd4.innerHTML = '-';
        else
          newtd4.innerHTML = intToFormat(output[0][i].price_usd);
        
        if(output[1][1] == 1 || output[1][1] == 3 || output[1][1] == 4 || output[1][1]== 5|| output[1][1]== 6)
        {        
          /*newtd5.innerHTML = "<a href='#' onclick='return addToCart(" + output[0][i].pid + ", " + output[0][i].id + ", " + output[0][i].cpid + ', "' + output[0][i].product.split("'").join() + '",' + output[1][1] + ");'><img src='grfx/cart.gif' width='19' height='15' style='border:none;'/></a>";*/
          newtd5.innerHTML = "<a href='javascript:addToCart(" + output[0][i].pid + ", " + output[0][i].id + ", " + output[0][i].cpid + ", \"" + output[0][i].product + "\"," + output[1][1] + ");'><img src='grfx/cart.gif' width='19' height='15' style='border:none;'/></a>";
        }
        else      //checkboxes for newsletetr
        {
          var len = newsletter.length;    //newsletter array
          if(len == 0)
          {
            newtd5.innerHTML = "<input type='checkbox' id='news_chk_" + i + "' name='news_chk_" + i + "' />";
          }
          else
          {
            if(newsletter[j] == i)    //if newsletter is added in cart, checked = TRUE 
            {
              newtd5.innerHTML = "<input type='checkbox' id='news_chk_" + i + "' name='news_chk_" + i + "' checked/>";
              j++;
            }
            else
            {
              newtd5.innerHTML = "<input type='checkbox' id='news_chk_" + i + "' name='news_chk_" + i + "' />";
            }
          }
        }
        newtr.appendChild(newtd1);
        if(output[1][1] == 1 || output[1][1] == 3 || output[1][1] == 4 || output[1][1]== 5 ||output[1][1] == 6)    //append only if magazines, conference, CDs, Reports
        {
          newtr.appendChild(newtd2);
        }
        newtr.appendChild(newtd3);
        newtr.appendChild(newtd4);
        if(output[1][1] != 2)
        {
          newtr.appendChild(newtd5);
        }
        optbl.appendChild(newtr);
      }
      
      //clear the newsletter quantity amount chart
      var TabPlane = document.getElementById("product").getElementsByTagName('tbody')[2];
      while (TabPlane.childNodes.length >= 1) 
      {
          TabPlane.removeChild(TabPlane.firstChild);
      }

      if(output[1][1] == 2)     //for newsletters
      {
        document.getElementById('newsletters').style.display = "";
        var optbl = document.getElementById('product').getElementsByTagName('tbody')[2];

        var newtr = document.createElement('tr');
        var newtd1 = document.createElement('th');
        var newtd2 = document.createElement('th');
        var newtd3 = document.createElement('th');
        var newtd5 = document.createElement('th');
        newtd1.innerHTML = 'I would like to subscribe to: ';
        newtd1.colSpan = "2";

        newtr.appendChild(newtd1);
        //newtr.appendChild(newtd2);
        //newtr.appendChild(newtd3);
        newtr.appendChild(newtd5);
        optbl.appendChild(newtr);

        var j = 0;      //variable used as a counter for newsletter array
        for(i=0 ; i<output[0].length ; i++)     //pricing options for newsletter (Any1, Any2, etc.)
        {
          var newtr = document.createElement("tr");
          var newtd1 = document.createElement("td");
          var newtd2 = document.createElement("td");
          var newtd3 = document.createElement("td");
          var newtd4 = document.createElement("td");
          var newtd5 = document.createElement("td");

          var len = newsletter.length;    //newsletter array
          if(len == 0)
          {
            newtd5.innerHTML = "<input type='checkbox' id='news_chk_" + i + "' name='news_chk_" + i + "' />";
          }
          else
          {
            if(newsletter[j] == i)    //if newsletter is added in cart, default checked attribute 
            {
              newtd5.innerHTML = "<input type='checkbox' id='news_chk_" + i + "' name='news_chk_" + i + "' checked/>";
              j++;
            }
            else
            {
              newtd5.innerHTML = "<input type='checkbox' id='news_chk_" + i + "' name='news_chk_" + i + "' />";
            }
          }

          newtd1.innerHTML = output[0][i].product;
          newtd1.width = "200px";
          newtd1.colSpan = "2";
          //newtd2.innerHTML = output[0][i].price_inr;
          //newtd3.innerHTML = output[0][i].price_usd;
          newtd2.innerHTML = '&nbsp;';
          newtd3.innerHTML = '&nbsp;';

          newtr.appendChild(newtd1);  
          //newtr.appendChild(newtd2);  
          //newtr.appendChild(newtd3);  
          newtr.appendChild(newtd5);  
          optbl.appendChild(newtr);      
        }
        
        var optbl = document.getElementById('product').getElementsByTagName('tbody')[2];
        var newtr = document.createElement('tr');
        var newtd1 = document.createElement('td');
        var newtd2 = document.createElement('td');
        var newtd3 = document.createElement('td');
        var newtd = document.createElement("td");
        newtd1.width = "200px";
        newtd1.colSpan = "2";
        //newtd.colSpan = 3;
		newtd1.innerHTML = ' Add to cart&nbsp;&nbsp;&nbsp;&nbsp;';
        newtd1.align ='right';
        newtd.innerHTML = "<a href='javascript:addToNewsletter(" + output[0].length + ");'><img src='grfx/cart.gif' width='19' height='15' style='border:none;'/></a>";
        newtr.appendChild(newtd1);  
        //newtr.appendChild(newtd2);  
        //newtr.appendChild(newtd3);  
        newtr.appendChild(newtd);  
        optbl.appendChild(newtr);      
      }
    }
    
    function addToCart(pid, id, cpid, product, category)       //add products to the shopping cart
    {
      spin_div('addedcart');                                //initiate loader on shopping cart
      if(document.frmProduct.method[0].checked == true)     //check the currency mode
      {
        var method = 'inr';
      }
      else
      {
        var method = 'usd';
      }

      //for CDs, Reports get qty element and this will be multiplied by price of product
      if(category == 3 || category == 4 || category == 5 || category == 6)
      {
        var qty = document.getElementById('qty_' + id).value;
      }
      else
      {
        var qty = 0;
      }
      var url = 'common.php';
      var params = 'pid=' + pid + '&id=' + id + '&cpid=' + cpid + '&opt=addToCart' + '&product=' + product + '&method=' + method + '&qty=' + qty + '&category=' + category;
      var ajax = new Ajax.Request(url, {method: "post", parameters: params, onComplete: addProduct});
    }
    
    function addProduct(data)       //populate products in shopping cart
    {
      var output = data.responseText.parseJSON();
      
      if(output.response == 0)      //test whether product already exists
      {
        alert('Product already selected');
      }
      else if(output.response == 2)     //if INR=0
      {
        alert('Product cannot be added into cart');
      }
      else if(output.response == 3)     //if USD=0
      {
        alert('Product cannot be added into cart');
      }
      else                          //if product does not exists in shopping cart add it
      {
        var table = document.getElementById("shoppingcart").getElementsByTagName('tbody')[0];
        var newRow = document.createElement("tr");
        
        //pro_[id] represents the name and qty of the product added to cart
        newRow.id = 'pro_' + output.id;
        var newCol1 = document.createElement("td");
        var newCol2 = document.createElement("td");
        
        //newsletter name in the shopping cart (any1, any2, any3, ....)
        if(output.category == 2)
        {
          //newCol1.innerHTML = 'Any ' + output.sub_year;
          var numP = output.product.length;
          var z = 0;
          var pro;
          for(z=0 ; z<numP ; z++)
          {
            if(z==0)
              pro = output.product[z];
            else
              pro += '<br />' + output.product[z];
          }
          newCol1.innerHTML = pro;
        }
        else
        {
          newCol1.innerHTML = output.product;
        }
        newRow.appendChild(newCol1);

        if(output.category == 1) //Magazines and Conferences
        {
          newCol2.innerHTML = output.sub_year;
        }
        else if(output.category == 3 || output.category == 4 || output.category == 5 || output.category == 6)   //CDs and Reports
        {
          //for this category the price will be multiple of qty
          //hence we are assigning the qty value to newCol2 with id qty_cart_[id]
          var qty = Math.floor(document.getElementById('qty_' + output.id).value);
          newCol2.id = 'qty_cart_' + output.id;
          newCol2.innerHTML = qty;
        }  
        else
          newCol2.innerHTML = '&nbsp;';  

        newRow.appendChild(newCol2);
        table.appendChild(newRow);        

        //adding products to the shopping cart
        var table = document.getElementById("price").getElementsByTagName('tbody')[0];
        var newRow = document.createElement("tr");
        
        //pri_[id] represents the price if the product and remove from cart button
        newRow.id = 'pri_' + output.id;
        var newCol1 = document.createElement("td");
        var newCol2 = document.createElement("td");
        var newCol3 = document.createElement("td");
        
        newCol1.id = 'inr_' + output.id;
        newCol3.id = 'usd_' + output.id;
        
        newCol1.innerHTML = intToFormat(output.price_inr);
        newCol3.innerHTML = intToFormat(output.price_usd);

        newCol2.id = output.id;
        newCol2.innerHTML = "<a href='#' id='" + output.id + "' onclick='return remove(this, this.id, " + output.category + ");' ><img src='grfx/close.gif' width='12' height='12' style='border:none;'/></a>";
        
        //appending childs to the shoppingcart TBODY
        //append USD or INR price based on method
        if(output.method == 'usd')
        { 
          newRow.appendChild(newCol3);
        }
        else
        {
          newRow.appendChild(newCol1);
        }
        newRow.appendChild(newCol2);
        table.appendChild(newRow);        
      }

      var amount = totalPrice(output.method);
      document.getElementById('amount').innerHTML = amount;

      document.getElementById('spin_div').style.display = 'none';   //disable loader on shopping CART

      if(output.response != 0 && output.response != 2 && output.response != 3)
        dynamicHeight(output.id, output.method);        //function for changing the TD height dynamically
    }
		function remove(elt, id, category_id)        //remove the product from the shopping cart
		{
      if(document.frmProduct.method[0].checked == true)   //currency mode
      {
        var method = 'inr';
      }
      else
      {
        var method = 'usd';
      }

      var url = 'common.php';
      var params = 'id=' + id + '&opt=removeProduct' + '&method=' + method + '&category_id=' + category_id;
      var ajax = new Ajax.Request(url, {method: "post", parameters: params, onComplete: removeProduct});
			
			var temp = 'pro_' + id;        //remove product name and (subscription years or delegates)
      var tmp = document.getElementById(temp);
      document.getElementById('shoppingcart').getElementsByTagName('tbody')[0].removeChild(tmp);
      
			var temp = 'pri_' + id;        //remove price (usd and inr) and remove button
      var tmp = document.getElementById(temp);
      document.getElementById('price').getElementsByTagName('tbody')[0].removeChild(tmp);
		}
		
		function removeProduct(data)    //get the revised total of products after removing product from shopping cart
		{
      var output = data.responseText.parseJSON();

      //get total price of products in shopping cart based on currency mode
      var amount = totalPrice(output.method);
      document.getElementById('amount').innerHTML = amount;
    }

    function currencyMode(method)     //get the total amount for selected product, in selected currency mode
    {
      var url = 'common.php';
      var params = 'opt=currencyMode' + '&method=' + method;
      var ajax = new Ajax.Request(url, {method: "post", parameters: params, onComplete: changeCurrency});
      
      //represents INR or USD
      document.getElementById('payment_method_head').innerHTML = method.toUpperCase();
      var amt = document.getElementById('amount').innerHTML;

      //if(amt != "0" && amt!= "Rs 0" && amt != "USD 0")
      if(amt != "0")
      {
        if(method == 'inr')
        {
          alert('You are now transacting in INR');
        }
        else
        {
          alert('You are now transacting in USD');
        }
      }
    }
    
    function changeCurrency(data)     //curreny mode INR or USD
    {
      var output = data.responseText.parseJSON();

      //get total price of products in shopping cart based on currency mode
      var amount = totalPrice(output.method);
      
      //switching between INR and USD
      var products = output.products;
      var len = output.products.length;
      var amt = document.getElementById('amount').innerHTML;
      
      //if(amt != "0" && amt!= "Rs 0" && amt != "USD 0")
      if(amt != "0")
      {
        for(i=0 ; i<len ; i++)
        {
          if(output.method == 'inr')                   //replace all USD with INR prices
          {
            var newTD = document.createElement('td');
            newTD.innerHTML = intToFormat(products[i].price_inr);

            newTD.id = 'inr_' + products[i].id;
            var tmp = 'usd_' + products[i].id;
            var oldTD = document.getElementById(tmp);
            oldTD.parentNode.replaceChild(newTD, oldTD);    //replace old TD with new TD
          }
          else                                         //replace all INR with USD prices
          {
            var newTD = document.createElement('td');
            newTD.innerHTML = intToFormat(products[i].price_usd);

            newTD.id = 'usd_' + products[i].id;
            var tmp = 'inr_' + products[i].id;
            var oldTD = document.getElementById(tmp);
            oldTD.parentNode.replaceChild(newTD, oldTD);    //replace old TD with new TD
          }
          dynamicHeight(products[i].id, output.method);     //function for changing the TD height dynamically
        }
      }      
      //get total price of products in cart based on currency mode
      var amount = totalPrice(output.method);      
      document.getElementById('amount').innerHTML = amount;     //change overall amount
    }

    function getCart(pid, method)     //back to cart feature
    {
      document.getElementById('payment_method_head').innerHTML = method.toUpperCase();

      var url = 'common.php';
      var params = 'opt=getCart' + '&pid=' + pid + '&method=' + method;
      var ajax = new Ajax.Request(url, {method: "post", parameters: params, onComplete: getCarts});
      getProduct(1, 'Magazines', method);     //by default, load magazine products
    }
    
    function getCarts(data)
    {
      var output = data.responseText.parseJSON();
      var length = output[0].length;
      var products = '';
      var i=0;
      for(i=0; i<length; i++)
      {
        //get shopping cart table
        var table = document.getElementById("shoppingcart").getElementsByTagName('tbody')[0];
        var newRow = document.createElement("tr");
        
        //pro_[id] represents product name and qty
        newRow.id = 'pro_' + output[0][i].id;
        var newCol1 = document.createElement("td");
        var newCol2 = document.createElement("td");
        
        if(output[0][i].category == 2)
        {
          var numP = output[0][i].newsletter_name.length;
          var z = 0;
          var pro;
          for(z=0 ; z<numP ; z++)
          {
            if(z==0)
              pro = output[0][i].newsletter_name[z];
            else
              pro += '<br />' + output[0][i].newsletter_name[z];
          }
          newCol1.innerHTML = pro;
        }
        else
        {
          newCol1.innerHTML = output[0][i].product;
        }
        
        newRow.appendChild(newCol1);
        if(output[0][i].category == 1) //for magazines and conference
        {
          newCol2.innerHTML = output[0][i].sub_year;
        }
        else if(output[0][i].category == 2)
        {
          newCol2.innerHTML = '&nbsp;';
        }
        else if(output[0][i].category == 3 || output[0][i].category == 4 || output[0][i].category == 5 || output[0][i].category == 6)   //for CDs and Reports
        {
          newCol2.id = 'qty_cart_' + output[0][i].id;   //assign qty_cart_[id], bcoz this value will be multiplied by product price
          newCol2.innerHTML = output[0][i].qty;
        }  
        else
          newCol2.innerHTML = '&nbsp;';  

        newRow.appendChild(newCol2);
        table.appendChild(newRow);        

        //generating products using DOM
        var table = document.getElementById("price").getElementsByTagName('tbody')[0];
        var newRow = document.createElement("tr");

        //pri_[id] represents price(INR or USD) and remove from cart button
        newRow.id = 'pri_' + output[0][i].id;
        var newCol1 = document.createElement("td");
        var newCol2 = document.createElement("td");
        var newCol3 = document.createElement("td");

        newCol1.innerHTML = intToFormat(output[0][i].price_inr);
        newCol3.innerHTML = intToFormat(output[0][i].price_usd);

        newCol1.id = 'inr_' + output[0][i].id;
        newCol3.id = 'usd_' + output[0][i].id;

        newCol2.id = output[0][i].id;
        newCol2.innerHTML = "<a href='#' id='" + output[0][i].id + "' onclick='return remove(this, this.id);'><img src='grfx/close.gif' width='12' height='12' style='border:none;'/></a>";
        if(output[1][1] == 'usd')
          newRow.appendChild(newCol3);
        else
          newRow.appendChild(newCol1);
        
        newRow.appendChild(newCol2);
        table.appendChild(newRow);  
        //break;      
        dynamicHeight(output[0][i].id, output[1][1]);     //function for changing the TD height dynamically
      }

      var amount = totalPrice(output[1][1]);
      document.getElementById('amount').innerHTML = amount;
    }

    function loader()   //load animation on ajax loading
    {
      document.getElementById('loader').style.display = 'block';
    }

    function unloader()   //unload animation after ajax process is completed
    {
      document.getElementById('loader').style.display = 'none';
    }
    
    function checkCart()    //function for checking whether any product is selected or not  
    {                       //before submitting the form
      var amt = document.getElementById('amount').innerHTML;
      if(amt != "0" && amt!= "Rs 0" && amt != "USD 0")
      {
        return true;
      }
      else
      {
        alert('Please select any product');
        return false;
      }
    }

    //function for getting the total price of products in shopping cart
    function totalPrice(method)
    {
      var tr = document.getElementById("price").getElementsByTagName('tbody')[0].getElementsByTagName('tr');
      
      //no. of products in cart
      var len = tr.length - 1;
      var amount = 0;
      for(i=1 ; i<=len ; i++)
      {
        amount += parseInt(formatToInt(tr[i].getElementsByTagName('td')[0].innerHTML));
      }
      
      if(method == 'inr')      //check the currency mode
      {
        var amount = 'Rs ' + intToFormat(amount);
      }
      else
      {
        var amount = 'USD ' + intToFormat(amount);
      }
      return amount;
    }

    //adding newsletters to the cart
    function addToNewsletter(num)
    {
      spin_div('addedcart');                                //initiate loader on shopping cart
      if(document.frmProduct.method[0].checked == true)     //check the currency mode
      {
        var method = 'inr';
      }
      else
      {
        var method = 'usd';
      }
      var i = 0;
      var j = 0;
      newsletter.length = 0;    //removing all elements from javascript array before adding to cart
      for(i=0 ; i<num ; i++)    //checking how many newsletters is selected
      {
        var tmp = 'news_chk_' + i;
        if(document.getElementById(tmp).checked == true)
        {
          newsletter.push(i);   //push the checked newsletter into javascript array(newsletter)
          j++;                  //newsletter count
        }
      }
      if(j > 0)                 //if any one newsletter is selected, add to cart
      {
        var url = 'common.php';
        var params = 'opt=addToNewsletter&method=' + method + '&cnt=' + j + '&newsletter=' + newsletter;
        var ajax = new Ajax.Request(url, {method: "post", parameters: params, onComplete: addProduct});
      }
      else                      //if no newsletter is selected
      {
        alert('Please select any newsletter!');
        document.getElementById('spin_div').style.display = 'none';   //disable loader on shopping CART
        //return false;
      }
    }
    
    function loadAmount(id, category)       //function for dynamically loading price(inr and usd) based on quantity
    {
      if(document.frmProduct.method[0].checked == true)     //check the currency mode
      {
        var method = 'inr';
      }
      else
      {
        var method = 'usd';
      }

      //for CDs, Reports get qty element and this will be multiplied by price of product
      if(category == 3 || category == 4 || category == 5 || category == 6)
      {
        var qty = document.getElementById('qty_' + id).value;
      }
      else
      {
        var qty = 0;
      }

      //loading animation for amount (inr and usd) upon quantity selection
      document.getElementById('amt_inr_' + id).innerHTML = "<img src='grfx/amt-loader.gif'>";
      document.getElementById('amt_usd_' + id).innerHTML = "<img src='grfx/amt-loader.gif'>";

      var url = 'common.php';
      var params = 'id=' + id + '&opt=LoadAmount' + '&method=' + method + '&qty=' + qty + '&category_id=' + category;
      var ajax = new Ajax.Request(url, {method: "post", parameters: params, onComplete: loadPrice});
    }

    function loadPrice(data)      //callback function for loading price based on quantity
    {
      var output = data.responseText.parseJSON();
      
      if(output.price_inr == 0)
        document.getElementById('amt_inr_' + output.id).innerHTML = '-';
      else
        document.getElementById('amt_inr_' + output.id).innerHTML = intToFormat(output.price_inr);

      if(output.price_usd == 0)
        document.getElementById('amt_usd_' + output.id).innerHTML = '-';
      else
        document.getElementById('amt_usd_' + output.id).innerHTML = intToFormat(output.price_usd);
    }
    
    //function for converting string into indian currency format
    function intToFormat(nStr)
    {
    	nStr += '';
    	x = nStr.split('.');
    	x1 = x[0];
    	x2 = x.length > 1 ? '.' + x[1] : '';
    	var rgx = /(\d+)(\d{3})/;
    	var z = 0;
      var len = String(x1).length;
      var num = parseInt((len/2)-1);
  
      while (rgx.test(x1)) 
      {
        if(z > 0)
        {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
    	  }
    	  else
    	  {
          x1 = x1.replace(rgx, '$1' + ',' + '$2');
    	  	rgx = /(\d+)(\d{2})/;
        }
        z++;
        num--;
        if(num == 0)
        {
          break;
        } 
      }
    	return x1 + x2;
    }

    //function for converting currency format into string
    function formatToInt(str)
    {
      var myRegExp = new RegExp(",","g");
      var myResult = str.replace(myRegExp, "");
      return myResult;
    }

    //function for removing the element from javascript array (NOT IN USE)
    Array.prototype.remove=function(s)
    {
      len = s.length;
      for(i=0 ; i<len ; i++)
      if(s==this[i]) this.splice(i, 1);
    }

    //function for dynamically changing the height of the price table TDs w.r.t to shoppingcart table TDs
    function dynamicHeight(id, method)
    {
      if(method == 'inr')
      {
        var tmp = "inr_" + id;
      }
      else
      {
        var tmp = "usd_" + id;
      }
      var tmp1 = "pro_" + id;
  
      if(window.ActiveXObject)    //IE
        document.getElementById(tmp).style.height=document.getElementById(tmp1).clientHeight-7+"px"
      else                        //other than IE
        document.getElementById(tmp).style.height=document.getElementById(tmp1).clientHeight+"px"
    }
    
    function spin_div(div_id)       //function to enable transparent css div on CART
    {
      var container = document.getElementById(div_id);
      document.getElementById('spin_div').style.position = 'absolute';
      document.getElementById('spin_div').style.width = container.offsetWidth+'px';
      document.getElementById('spin_div').style.height = container.offsetHeight+'px';
      document.getElementById('spin_div').style.display = 'block';
    }

    function numbersonly(e, decimal)    //function for accepting only numeric characters for reports 
    {                                   //http://snippets.dzone.com/posts/show/5223
      var key;
      var keychar;
      
      if (window.event) {
         key = window.event.keyCode;
      }
      else if (e) {
         key = e.which;
      }
      else {
         return true;
      }
      keychar = String.fromCharCode(key);
      
      if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
         return true;
      }
      else if ((("0123456789").indexOf(keychar) > -1)) {
         return true;
      }
      else if (decimal && (keychar == ".")) { 
        return true;
      }
      else
         return false;
    }

