function save(){
var regularsale;

var disp_price2 = document.getElementById("dispprice");
if (disp_price2 !=null){
	disp_price=disp_price2.innerHTML.replace("$","").replace(",","");
}
var regular;
var regularObj = document.getElementById("regular");
if (regularObj != null)
{
regular = regularObj.innerHTML.replace("$","").replace(",","");
}
//alert('Hello1');
// if (regularObj != null && disp_price != null)

if (regularObj != null && disp_price != null)
{
//innerHTML.replace("$","").replace(",","");
retaildiscountrate2 =100 - Math.ceil(100* disp_price/regular);
discount= Math.floor(regular-disp_price);
}
document.getElementById("savenum").innerHTML='$' +addCommas(toDollarsAndCents(discount)) + ' (' + retaildiscountrate2 + '%)';
}
//setTimeout("save()",3000);
function tileSave(){
var regularsale;

var disp_price2 = document.getElementById("sale_box");
if (disp_price2 !=null){
	disp_price=disp_price2.innerHTML.replace("$","").replace(",","");
	disp_price=disp_price.substring(0,disp_price.indexOf("/"));

}
var regular;
var regularObj = document.getElementById("regular");
if (regularObj != null)
{
regular = regularObj.innerHTML.replace("$","").replace(",","");
}

//alert('Hello1');
// if (regularObj != null && disp_price != null)

if (regularObj != null && disp_price != null)
{
//innerHTML.replace("$","").replace(",","");
retaildiscountrate2 =100 - Math.ceil(100* disp_price/regular);
discount= Math.floor(regular-disp_price);
}
document.getElementById("savenum").innerHTML='$' +addCommas(toDollarsAndCents(discount)) + ' (' + retaildiscountrate2 + '%)';

}
var discountrate = 0;
var retaildiscountrate = 0;

function ModifyOptions()
        {
            var regularsale;

var regularsaleObj = document.getElementById("regularsale");
            var disp_price = document.getElementById("dispprice").innerHTML.replace("$","").replace(",","");
            
            //alert(regularsale/disp_price);
            //return;
			var regular;
var regularObj = document.getElementById("regular");

if (regularsaleObj != null)
			{
				regularsale = regularsaleObj.innerHTML.replace("$","").replace(",","");
			}


if (regularObj != null)
{
regular = regularObj.innerHTML.replace("$","").replace(",","");
}
//alert('Hello1');
// if (regularObj != null && disp_price != null)
if (regularObj != null && disp_price != null)
{
//innerHTML.replace("$","").replace(",","");
retaildiscountrate = disp_price/regular;
}
			if (regularsale != null && disp_price != null)
			{
				discountrate = disp_price/regularsale;
				var i;
				var j;
				var newPriceString;
				var selElements = document.getElementsByTagName("select")
				for (i=0;i<selElements.length;i++)
				{  
					
					for (j=0;j<selElements[i].options.length;j++)
					{
						//alert(selElements[i].options[j].value);
						newPriceString = GetModOptString(selElements[i].options[j].value,discountrate);
						selElements[i].options[j].value = newPriceString;
						selElements[i].options[j].text = newPriceString;
					}
				}
            }
        }
        
        function GetModOptString(optionString,percent)
        {
            var start = optionString.indexOf("(");
            var end = optionString.indexOf(")");
            var value;
            
            if (start != -1 && end != -1)
            {
				start +=1;
				end -=1;
                // get the value            
                value = optionString.toString().replace("$","").substring(start,end);    
                // make sure that it is a number within the parenths
                if (!isNaN(value))
                {
                    // make the number positive
                    if (value <0)
                        value = value * -1;
                    value  = Math.round(value * percent);
                    optionString = optionString.substring(0,start+2) + value + optionString.substring(end+1, 100);
                    //alert (optionString );
                }
                else
                {
					//alert(value);
                }
                
            }
            
            return optionString;
        }
        
//       setTimeout("ModifyOptions()",2000);
		//setTimeout("generateCode()",3000);


function generateCode(){
document.getElementById("sku").value=document.getElementById("sku2").innerHTML	
var yyyy=	document.forms[0].elements["Glass Thickness"];
if (yyyy !=null ){
var y= document.forms[0].elements["Glass Thickness"].selectedIndex;

var yy=document.forms[0].elements["Glass Thickness"].options[y].value
firstwo=yy.indexOf(' ');
first=yy.substring(0,firstwo);

//alert (last.length);
//alert(first);
code = document.getElementById("sku2").innerHTML;
//alert(code);
newcode=first;
document.getElementById("sku2").innerHTML=newcode;
document.getElementById("sku").value=newcode;
// replacing database item number 
var tee=document.getElementById("item-numb").value;

if (first!=null){
//alert(tee);
document.getElementById("item-numb").value=tee.substring(0,tee.lastIndexOf("shen"))+first.toLowerCase();
//alert(newcode);
}}
	
	
	}

function updatePrice(frm,obj)
{
    // ----------for multi option any operation
 UpdateImage();
// generateCode();
    p =    document.getElementById("Base Price").value.replace(',','');
    document.getElementById("dispprice").innerHTML = p; 
    actual_price = p.substr(p.indexOf("$")+1, p.length);

    var price = parseFloat(actual_price);
	
    var pricevalAr = new Array(); 
    var direction = "add";
    
    for(i=0; i< frm.elements.length; i++)
    {
        e = frm.elements[i];

        if (e.type == "select-one")
        {
			var myindex  = e.selectedIndex;
			var SelValue = e.options[myindex].value.replace(',','');
			
            //if(e.checked )
            {
                //priceval = frm.elements[i].value;
                priceval = SelValue;
                if(priceval.indexOf("(+$") > -1) {
                    pricevalAr = priceval.split("(+$");
                    direction = "add"; 
                } else if(priceval.indexOf("(+") > -1) {
                    pricevalAr = priceval.split("(+");
                    direction = "add";
                } else if( priceval.indexOf("(-") > -1) {
                    pricevalAr = priceval.split("(-");
                    direction = "sub";
                }else if(priceval.indexOf("(-$") > -1) { 
                    pricevalAr = priceval.split("(-$");
                    direction = "sub";
                }else {pricevalAr = "";}
                

                if( pricevalAr.length > 1)
                {
                    priceval = pricevalAr[1].split(")");
                  //priceval = parseFloat(priceval[0]);

                    priceval = parseFloat(priceval[0].replace('$',''));
                }
                else
                {    priceval = 0;}
                
                
                    

                if(direction == "add")
                    price = price + priceval
                else if(direction == "sub") 
                    price = price - priceval
            }
			
        }
		  else {if (e.type=="checkbox"&&e.checked||e.type=="radio"&&e.checked)
				{			
				
				
			SelValue=e.parentNode.getElementsByTagName("span")[0].innerHTML
			   //priceval = frm.elements[i].value;
                priceval = SelValue.replace(',','');
			    if(priceval.indexOf("+$") > -1) {
                    pricevalAr = priceval.split("+$");
                    direction = "add"; 
                } else if(priceval.indexOf("+") > -1) {
                    pricevalAr = priceval.split("+");
                    direction = "add";
                } else if( priceval.indexOf("(-") > -1) {
                    pricevalAr = priceval.split("(-");
                    direction = "sub";
                }else if(priceval.indexOf("(-$") > -1) { 
                    pricevalAr = priceval.split("(-$");
                    direction = "sub";
                }else {pricevalAr = "";}
                if( pricevalAr.length > 1)
                {
                    priceval = pricevalAr[1].split(")");
                  //priceval = parseFloat(priceval[0]);
                    priceval = parseFloat(priceval[0].replace('$',''));
                }
                else
                {    priceval = 0;}
                if(direction == "add")
                    price = price + priceval
                else if(direction == "sub") 
                    price = price - priceval
            		
			
			
			}}
        //alert (price);
           document.getElementById("dispprice").innerHTML = "$" + addCommas(toDollarsAndCents(price));
		   if (document.getElementById("total") !=null){
		   document.getElementById("total").innerHTML = "$" + addCommas(toDollarsAndCents(price));}
		   if (document.getElementById("regularsale") != null)
				{
					document.getElementById("regularsale").innerHTML = "$" + addCommas(Math.round(toDollarsAndCents(price) * 1/discountrate)) + ".00";
				}
		if (document.getElementById("regular") != null)
			{
				document.getElementById("regular").innerHTML = "$" + addCommas(Math.round(toDollarsAndCents(price) * 1/retaildiscountrate)) + ".00";
			}

    }//for ends

save();	
  }
function toDollarsAndCents(n) {
  var s = "" + Math.round(n * 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 addCommas(nStr)
{
       nStr += '';
      var x = nStr.split('.');
      var x1 = x[0];
      var x2 = x.length > 1 ? '.' + x[1] : '';
       var rgx = /(\d+)(\d{3})/;
       while (rgx.test(x1)) {
               x1 = x1.replace(rgx, '$1' + ',' + '$2');
       }
       return x1 + x2;
}
//removes option's price addition or discount, because the other item is phisicaly added to the cart
function removeOptionsPrice(){
var yyyy=document.forms[0].elements["Glass Thickness"];
if (yyyy !=null ){
	var y= document.forms[0].elements["Glass Thickness"].selectedIndex;
	var yy=document.forms[0].elements["Glass Thickness"].options[y].value;
var last=yy.lastIndexOf('(')
	if (last>1){
	var newvalue = yy.substring(0,last)
	document.forms[0].elements["Glass Thickness"].options[y].value=newvalue;
		}						  
}


}