//for price comparasement - preselected options 

function makeSelections(){

tt=window.location.href;
//getting values from url
var heightstartind=tt.indexOf("height=") + 7;
var widthlastind=tt.indexOf("width") + 6;
var colorlastind=tt.indexOf("color")-1;
var finishlastind=tt.indexOf("finish");

heightindex=parseFloat(tt.substring(heightstartind,heightstartind+2));
widthindex=parseFloat(tt.substring(widthlastind,widthlastind+2));
colorindex=parseFloat(tt.substring(colorlastind+7,colorlastind+9));
finishindex=parseFloat(tt.substring(finishlastind+7,finishlastind+9));
//variables that will be changed
	
var sel=new Array(document.f1.elements["vwattr0_Door_Height"],document.f1.elements["vwattr0_Door_Width"],document.f1.elements["vwattr0_Glass Type"],document.f1.elements["vwattr0_Finish Type"]);
 var h= new Array (heightindex,widthindex,colorindex,finishindex);
//if (h[2] !=null ) { alert ('here') } else {alert('nnan');}

//changing options

for (i=0; i<h.length; i++)
{
// alert (i);
// alert(sel[i])
	if (sel[i]!= null) 
	{
	// alert ('Select tag is found' + i)
	 if (!isNaN(h[i]))
	 	{
		if (i==0) {
		//alert ('generate width')
		setOptions(h[i]);
		sel[i].selectedIndex=h[i]
		}
		if (i==1) {
		sel[i].selectedIndex=h[i];
		//alert('update price')
	
	ModifyOptions();
		}
        if (i>1) {
			sel[i].selectedIndex=h[i];
			}
		}
}
//alert(document.f1.elements["Door_Height"])
}
//updating price after that

updatePrice(document.f1);

}
setTimeout ("makeSelections()",3000)

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(",","");
    }
if (regularObj != null && disp_price != null)
    {
    retaildiscountrate2 =100 - Math.ceil(100* disp_price/regular);
    discount= Math.floor(regular-disp_price);
    }
document.getElementById("savenum").innerHTML='$' + discount + ' (' + retaildiscountrate2 + '%)';
}


var discountrate = 0;
var retaildiscountrate = 0;

function ModifyOptions()
 { 
var dd2=document.f1.elements["vwattr0_Door_Width"];
   if (dd2!=null && dd2.selectedIndex>0){          
			
				var i;
				var j;
				var newPriceString;
				var selElements = document.getElementsByTagName("select")
				for (j=0;j<document.forms[0].elements["vwattr0_Glass Type"].length;j++)
					{
						//alert(selElements[i].options[j].value);			
						newPriceString = GetModOptString(document.forms[0].elements["vwattr0_Glass Type"].options[j].value,discountrate,j);
						//alert(numbr3);
						//alert(newPriceString);
						document.forms[0].elements["vwattr0_Glass Type"].options[j].value = newPriceString;
						document.forms[0].elements["vwattr0_Glass Type"].options[j].text = newPriceString;
					}
			fin=document.forms[0].elements["vwattr0_Finish Type"];
			for (j=1;j<fin.length;j++)
					{
						//alert(selElements[i].options[j].value);
						
						var pos =j*100
					//	alert (pos);
						//if (j==4) pos = j - 1;
						//if (j>=5 && j <=8) pos = 4;
						//if (j>8) pos = 5;						
						newPriceString = GetModOptString(fin.options[j].value,discountrate,pos);
						fin.options[j].value = newPriceString;
						fin.options[j].text = newPriceString;
										}
	
			}
		
	  }
			
			

        
        function GetModOptString(optionString,percent,pos)
        {
            var start = optionString.indexOf("(");
            var end = optionString.indexOf(")");
            var value;
			   var value2;
            
			//alert (quadrant2);
            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
           		  var dd = document.forms[0].elements["vwattr0_Door_Height"].selectedIndex;
				var dd2 = document.forms[0].elements["vwattr0_Door_Width"].selectedIndex;
	var dd3 = document.forms[0].elements["vwattr0_Finish Type"].selectedIndex;	
	var dd4 = document.forms[0].elements["vwattr0_Glass Type"].selectedIndex;
	
if (pos<100){
	value = Math.round(0.848*1.1*pricekk[dd][dd2][pos]);
			}
if (pos>=100) {
	value = Math.round(0.848*1.1*pricefin[dd][dd2][pos/100]);}
			
			
	                optionString = optionString.substring(0,start+2) + value + optionString.substring(end+1, 100);
                }
                
            }
            
            return optionString;
        }
setTimeout("UpdateImage()",2000);
setTimeout ("GenerateCode()",2000);
setTimeout ('document.getElementById("disp_price2").innerHTML = document.getElementById("dispprice").innerHTML',2000);


function updatePrice(frm)
{

    // ----------for multi option any operation
UpdateImage();
 
// creating dropdowns for custom widht //
//check if all neccessary items were selected ; remove unnecess;
DropValidation();

// end	




	var dd4 = document.forms[0].elements["vwattr0_Door_Height"].selectedIndex;
	
				var dd5 = document.forms[0].elements["vwattr0_Door_Width"].selectedIndex;
			    				if (dd4<=2) 
				{   var quadrant=1;
				    if (dd5<3) {quadrant =  quadrant + 1}
				
				}
				if (dd4>2) {
					var quadrant = 3;
				    if (dd5>4) {quadrant = quadrant + 1}  
				}
			
				//alert(quadrant);
				
			ModifyOptions();	
			
	
    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=2; 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));
		         document.getElementById("disp_price2").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/0.8)) + ".00";
			}

    }//for ends
// pasting real sku to cart and to the inner HTML;
GenerateCode();	
	
  }
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;
}
function GenerateCode (){
	var basesku;
	var basesku2;
	var basesku3S;
	
	var dd7 = document.forms[0].elements["vwattr0_Door_Width"];
	basesku = dd7.options[dd7.selectedIndex].value.substring(dd7.options[dd7.selectedIndex].value.indexOf(":"),dd7.options[dd7.selectedIndex].value.indexOf("]"));
	var dd8 = document.forms[0].elements["vwattr0_Glass Type"];
	basesku2  = dd8.options[dd8.selectedIndex].value.substring(0,dd8.options[dd8.selectedIndex].value.indexOf(" "));
	glass_code = dd8.options[dd8.selectedIndex].value.substring(0,dd8.options[dd8.selectedIndex].value.indexOf("("));
	
	var dd9 = document.forms[0].elements["vwattr0_Finish Type"];
	basesku3 = dd9.options[dd9.selectedIndex].value.substring(0,dd9.options[dd9.selectedIndex].value.indexOf(" "));
	finish_code = dd9.options[dd9.selectedIndex].value.substring(0,dd9.options[dd9.selectedIndex].value.indexOf("("));
	var dd10 = document.forms[0].elements["vwattr0_Door_Height"];
	var dd11 =dd10.options.length-2;
	
	if (dd10.options.selectedIndex >= dd11 && dd10.options[dd10.selectedIndex].value.indexOf("Custom")>=0){ basesku = 'CUS'}
	document.getElementById("sku").value = basesku + '-' + basesku2 + '-' + basesku3;
	document.getElementById("sku2").innerHTML = basesku + '-' + basesku2 + '-' + basesku3;
	document.getElementById("glass-code").innerHTML = glass_code;
	document.getElementById("finish-code").innerHTML = finish_code;
	itemcode=basesku + '-' + basesku2 + '-' + basesku3;
checkShip(itemcode);
	
	//alert(dd7)
}
//var quckship=new Array();
//var quckship=new Array('2150-64S-OB-SL','6150-60S-SL-OB');
function opennn(){
	popup('http://site.bathroom-glass-vanities.com/quick-ship.html');
	}
function checkShip(itemcode){
var itemcode2=itemcode.substring(2,itemcode.length);
//alert(itemcode2);
if (quckship.length>0){
//alert(quckship.length);
for (i=0;i<quckship.length; i++){
//alert(quckship[i]);
if (itemcode2==quckship[i]){
	
	document.getElementById("quick-ship").innerHTML='<img onClick="opennn()" src="http://www.luxaris.com/bathshowerstore/quick-ship.gif" class="nobord">&nbsp;';
			document.getElementById("availability").innerHTML='<font color="#FF0000">Usually ships within 2 business days</font>';
i=quckship.length;
}
else {
	document.getElementById("quick-ship").innerHTML='';
	document.getElementById("availability").innerHTML='Usually ships within 2-3 weeks';
					}

}
}
}
function updatePrice2(frm)

{

// ----------for multi option any operation
UpdateImage();
// creating dropdowns for custom widht //
//check if all neccessary items were selected ; remove unnecess;
// end	



	
    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=2; i< frm.elements.length; i++)
    {
        e = frm.elements[i];
        if (e.type == "select-one")
        {
			var myindex  = e.selectedIndex;
			//alert(myindex);
			//alert(e.options[myindex].value);
			var SelValue = e.options[myindex].value;
			
            //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
            }
        }
		    
        //alert (price);
           document.getElementById("dispprice").innerHTML = "$" + addCommas(toDollarsAndCents(price));
		   document.getElementById("disp_price2").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/0.8)) + ".00";
			}

    }//for ends
// pasting real sku to cart and to the inner HTML;
GenerateCode();	
if (document.forms[0].elements["vwattr0_Upgrade Towel Bar"] != null){ 
	var lk= document.forms[0].elements["vwattr0_Upgrade Towel Bar"].options.selectedIndex;
	var lk2=document.forms[0].elements["vwattr0_Upgrade Towel Bar"].options[lk].value;
	var lk3=lk2.indexOf("Side");
if ( lk3 >0) {
	
	var showerheadlist = new Array ("Left", "Right");
var showerheadvalue = new Array ("Left", "Right");
	//alert('create');
	document.getElementById("showerhead").innerHTML='<p class="shortbr3">Please Specify Shower Head Side<span class="smalldesc"> Can be determined by standing on the outside of the shower and looking into it </span></p><span id="showerheadDrop"></span>';
		CreateDD('showerheadDrop', showerheadlist, showerheadvalue, 0,1, "showerhead" );
	}
else document.getElementById("showerhead").innerHTML='';

}
	
  }
