﻿var ourprice = 0.0;
var listprice = 0.0;

function TotalPrice(id)
{
	var pr = eval(document.f.Price.value);
	var pcode = "";
   	var iOpt = document.f.iOptions.value;
	var OpListPrice = eval(document.f.LPrice.value);
	
	var showlistprice=OpListPrice;
	var prcode="";
    var ctype, obj, checkb, radiob, divid, i;
    
    ourprice = eval(pr);
    listprice = eval(OpListPrice);
    
    if (iOpt>0)
    {
	    for(i=1;i<=iOpt;i++)
	    {
	       ctype = eval("document.f.CatType"+i+".value");
	       
	        
	       if (ctype=="2")
	       {
	             checkb = eval("document.f.CheckBox"+i+".value");
	             UpdateCheck(checkb,i);
	       } 
	       else if (ctype=="3")
	       {
	             radiob = eval("document.f.Radio"+i+".value");
	             
	             UpdateRadio(radiob,i);
	       }    
	       else
	       {
	            obj = eval("document.f.Option"+i+".options[document.f.Option"+i+".selectedIndex].value");
	           
	            objs = obj.split("|");
               
                ourprice = eval(ourprice) + eval(objs[1]);
                listprice = eval(listprice) + eval(objs[3]);
                
	       }
                
                
          if (i != id)
          {
	        divid = "Opdiv"+i ;
	        opacity(divid,10,0,0); 
	       }
	     }  
	     
	     
	     divid = "Opdiv"+id;
	     document.getElementById(divid).innerHTML = "<strong>Total: $"+ roundOff(ourprice,2)+"</strong>";
	     opacity(divid,0,90,300);
	     
	     
	     //Update List Price
	      if (document.all)
	        {
		        if (listprice !=0)
			    {
				    ListPrice.innerText =roundOff(listprice,2);
			    }
	        }
	      else
	        {       
	 	        document.f.ListPrice.value=roundOff(listprice,2);
	        }
	        
	     //Update Our Price
	     
	      if (document.all)
	        {
		        if (ourprice !=0)
			    {
				    price1.innerText =roundOff(ourprice,2);
			    }
	        }
	      else
	        {       
	 	        document.f.price1.value=roundOff(ourprice,2);
	        }
     
	     
	     setTimeout("hidediv('"+divid+"')",10000);
	     
	     return roundOff(ourprice,2);
	         
    } 
     else 
        {
            if(document.all)
	        {
		        price1.innerText =roundOff(pr,2);
		        ListPrice.innerText =roundOff(OpListPrice,2);
	        }
	        else
	        {
		        document.f.price1.value=roundOff(pr,2);
		        document.f.ListPrice.value=roundOff(OpListPrice,2);
	        }
            
            return roundOff(pr,2);
        }   
}
function hidediv(id)
{
    document.getElementById(id).style.display = 'none';
}

function UpdateCheck(cnt,nm)
{
  var nam = nm;
  var obj, objs, objss;
  
    if (cnt>0)
    {
       for(j=0;j<=cnt;j++) 
       {
           obj = eval("document.f.Option"+nam+"_"+j+".checked");
           
           if (obj==true)
           {
               objs = eval("document.f.Option"+nam+"_"+j+".value");
               
               objss = objs.split("|");
               
               ourprice = eval(ourprice) + eval(objss[1]);
               listprice = eval(listprice) + eval(objss[3]);
              
           }
       }
    }        
}

function UpdateRadio(cnt,nm)
{
  var nam = nm;
  var obj, objs, objss;
  
    if (cnt>0)
    {
       for(k=0;k<=cnt;k++) 
       {
           obj = document.getElementById("Option"+nam+"_"+k).checked;
           
           
           if (obj==true)
           {
               objs = document.getElementById("Option"+nam+"_"+k).value;
               
               objss = objs.split("|");
               
               ourprice = eval(ourprice) + eval(objss[1]);
               listprice = eval(listprice) + eval(objss[3]);
          
           }
       }
    }        
}
function roundOff(value, precision)
{
      var result = "0.00";
      precision = parseInt(precision);          // make sure roundoff is an int
      // alerts are only for debugging and expanation -- they may be deleted/commented
      //alert("Value before rounding is "+value)
      value = ""+Math.round(value * Math.pow(10, precision));  // take the integer part of value*10**precision
      //alert("Value is "+value+"\nLength of value is "+value.length+"\nPrecision is "+precision)
      // determine where to put decimal point
      if (value.length > precision)        // more digits in value than in precision (e.g., 999.99)
      {
         result = value.substring(0, value.length-precision);
         if (precision > 0 ) result += ".";
         result  += value.substring(value.length-precision, value.length);
      }
      else                        // number of digits <= precision and value less than 1.0)
      {
         result = "0.";
         if (value.length < precision)
         {
            for (var i = 0; i < precision - value.length; i++)
            {
               result += "0";                // pad fraction with leading zeros
            }
         }
         result += value;
      }                // end else
        return result;
}

function changecolor(cid)
{

var cn = cid;
if (document.all)
{
document.getElementById('Color').value=cid;
}
else
{
document.f.Color.value = cid;
}
//var cname="color"+document.f.Color.value;
//document.imgMain.src = document.f[cname].value;
}


//Opacity control for fade in and fade out

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(r = opacStart; r >= opacEnd; r--) { 
            setTimeout("changeOpac(" + r + ",'" + id + "','hide')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(r = opacStart; r <= opacEnd; r++) 
            { 
            setTimeout("changeOpac(" + r + ",'" + id + "','show')",(timer * speed)); 
            timer++; 
        } 
    } 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id, sh) { 
    
    if (sh =='hide')
    {
    document.getElementById(id).style.display = 'none';
    }
    else
    {
    document.getElementById(id).style.display = 'block';
    }
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}

//Function For independent products

function isCharsInBag (s, bag)
{  
  var i;
  // Search through string's characters one by one.
  // If character is in bag, append to returnString.

  for (i = 0; i < s.length; i++)
  {   
	  // Check that current character isn't whitespace.
	  var c = s.charAt(i);
	  if (bag.indexOf(c) == -1) return false;
  }
  return true;
}

function validateMe()
{
    var ioptions;
    ioptions=document.f.iOptions.value;
    var qcnt=0;
    var i,j;
    
    for (i=0;i<ioptions;i++)
    {
         optionscnt = eval("document.f.cntoptions"+i+".value");
         for (j=0;j<=optionscnt;j++)
         {
         
         qty="txtqty_"+i+"_"+j;
         if (document.f[qty].value !="")
          {
            if(isNaN(document.f[qty].value))
			  {
				alert("Quantity contains Invalid characters");
				//document.f[qty].focus();
				return 1;
				//selected=false
			  }
			 if(!isCharsInBag(document.f[qty].value, "0123456789"))
			  {
			   alert("Invalid Quantity");
			   //selected=false
			   return 2;
			  }
		 }
		 if(document.f[qty].value>0)
			{
				qcnt=qcnt+1;
			}
		 }	  
    }
    if (qcnt==0)
	{
		alert("Please Select Atleast One Product");
		return 4;
	}
    
    return 5;
} 

 function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    //if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode == 46) {
    if (charCode > 31 && (charCode < 48 || charCode > 57) || charCode == 46) {
           alert("Enter numerals only in this field.");

        return false;
    }
    return true;
}


//Global Function for add to cart
function addtocart()
{
  var p = document.f.PType.value;
  
  if (p=="true")
  {
        var tpr = TotalPrice(1);
        //document.f.Price.value=tpr;
        //alert(document.f.Price.value)
        return true;
        //document.f.action="/add_to_cart.htm?inde=de";
        //document.f.submit();
  }
  else
  {
  
        var val;
  
        if (document.f.iOptions.value >0)
        { 
            val=validateMe();
        }
        else
        {
            val="5";
        }    
        if (val=="5")
        {   
            return true;        
         //document.f.action="/add_to_cart.htm?inde=inde";
         //document.f.submit();
        }
 }
 return false;
} 

function addtoWishList()
{
   var p = document.f.PType.value;
   if (p=="true")
    {
        document.f.action="/Save_My_Wishlist.htm?type=De";

    }
    else
    {
        document.f.action="/Save_My_Wishlist.htm?type=Inde";
    }    
  
   document.f.submit();
}
