﻿function ScrollToElement(theElement){

  var selectedPosX = 0;
  var selectedPosY = 0;
              
  while(theElement != null){
    selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }
                        		      
 window.scrollTo(selectedPosX,selectedPosY);

}

function popupWindowNoScroll(url,name,width,height) {
    newwindow4 = window.open(url,name, "width="+width+",height="+height+",toolbar=0,directories=0,menubar=0,status=0,resizable=0,location=0,scrollbars=0,copyhistory=0,left=100,top=100");
    newwindow4.focus();
	}	
	
function popupWindowScroll(url,name,width,height) {
    newwindow = window.open(url,name, "width="+width+",height="+height+",toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=1,copyhistory=0,left=162,top=200");
    newwindow.window.focus();
	}
	
function add(priceid, altfee, width, height, widthnumber, heightnumber){
    document.getElementById('price_id').value = priceid;   
    if(document.getElementById('qty_'+priceid)!=null){
        document.getElementById('add_qty').value = document.getElementById('qty_'+priceid).value;
        }

    document.getElementById('alt_fee').value = altfee;
    document.getElementById('width').value = width;
    document.getElementById('height').value = height;    
    document.getElementById('widthnumber').value = widthnumber;
    document.getElementById('heightnumber').value = heightnumber;
    document.getElementById('addForm').submit();
    }

function addaccessory(priceid){
    document.getElementById('price_id').value = priceid;
    document.getElementById('add_qty').value = document.getElementById('qty_'+priceid).value;;
    document.getElementById('addForm').submit();
    }

function showHideDiv(id) {
    var objDiv = document.getElementById(id);
    
    if (objDiv != null) {
        if (objDiv.style.display == "block"){
            objDiv.style.display = "none";
        } 
        else {
            objDiv.style.display = "block";
        }
    }
}

function hideDiv(id) {
    var objDiv = document.getElementById(id);
    if (objDiv != null) {
        objDiv.style.display = "none";
    } 
}

function updateDimDisplay(otext){
    if(otext!=""){
	    if(document.getElementById("dunitsw")){document.getElementById("dunitsw").innerHTML = otext;}
	    if(document.getElementById("dunitsh")){document.getElementById("dunitsh").innerHTML = otext;}
	}
}

function changeText(id, text) {
    if(document.getElementById(id)!=null){
        document.getElementById(id).innerHTML = text;
    }
}

/*Made to Measure Curtains/Voiles*/
var arrVoileLinedSurcharge = new Array();
var arrEyeletSurcharge = new Array();
var arrPinchpleatSurcharge = new Array();
var arrTabtopSurcharge = new Array();
var arrTmpGatherLbounds = new Array();
var arrTmpGatherUbounds = new Array();
var arrTmpDropLbounds = new Array();
var arrTmpDropUbounds = new Array();
var arrTmpPriceArray = new Array();
var varTmpPrice = new Array();
var intTmpGatherNumber;
var intTempDropNumber;
var surcharge;
var price;
var fullprice;
var discountmultiplier;
var myTd;
var intMaximumDrop;
intMaximumDrop = 274;

function setDiscount(multi) {
    discountmultiplier = (multi * 1)
}

function CheckWidthDrop() {
    intTmpGatherNumber = -1
    intTempDropNumber = -1
    
    checkNumeric('txtWidth', 0);
    var intWidth = convertDimensions('txtWidth');
    intWidth = intWidth * 1;
    
    for (i=0; i<5; i++) {     
        if(arrTmpGatherLbounds[i] <= intWidth && intWidth <= arrTmpGatherUbounds[i]) {
                intTmpGatherNumber = i;
            }
        }
    
    checkNumeric('txtDrop', 0);
    var intDrop = convertDimensions('txtDrop');
    intDrop = intDrop * 1;

    for (i=0; i<4; i++) {        
        if((arrTmpDropLbounds[i] * 1) <= intDrop && intDrop <= (arrTmpDropUbounds[i] * 1)) {
                intTempDropNumber = i;
            }
        }

    if(intTmpGatherNumber==-1 || intTempDropNumber==-1) {
       // alert('Quote' + intTmpGatherNumber + ' ' + intTempDropNumber);
        showQuoteText();
        }
    else {
        
        document.getElementById('hidHeight').value = intTmpGatherNumber;
        document.getElementById('hidWidth').value = intTempDropNumber;

        if((intWidth*1)==0 || (intDrop*1)==0){
            fullprice = 0;
            document.getElementById('hidPrice').value = '';
            showPriceText();
            }
        else {
            setPrices();
            }    
        }

        myTd = document.getElementById('td'+intTmpGatherNumber+'_'+intTempDropNumber);
        if(myTd!=null && fullprice*1 > 0){
            for (i=0; i<5; i++) {     
                for (j=0; j<5; j++) {     
                        if(document.getElementById('td'+i+'_'+j)!=null){
                            document.getElementById('td'+i+'_'+j).className = ''
                        }
                        intTmpGatherNumber = i;
                    }
                }  
            myTd.className = 'price_over'
        }

    }

function setEyeletSurcharge(str) {
        arrEyeletSurcharge = str.split(",");
    }

function setVoileLinedSurcharge(str) {
        arrVoileLinedSurcharge = str.split(",");
    }
function setPinchpleatSurcharge(str) {
        arrPinchpleatSurcharge = str.split(",");
    }
    
function setTabtopSurcharge(str) {
        arrTabtopSurcharge = str.split(",");
    }

function setPriceArray(prices1,prices2,prices3,prices4) {
    varTmpPrice[0]= new Array();
    varTmpPrice[1]= new Array();
    varTmpPrice[2]= new Array();
    varTmpPrice[3]= new Array(); 
    
    varTmpPrice[0] = prices1.split(",");
    varTmpPrice[1] = prices2.split(",");
    varTmpPrice[2] = prices3.split(",");
    varTmpPrice[3] = prices4.split(",");    
    }

function setPrices() {
    var selected = document.getElementById('ddlHeading').options[document.getElementById('ddlHeading').selectedIndex].value;    
    surcharge = 0;
    price = 0;
    switch (selected) {
	    case "Pencil Pleat":
	        surcharge = 0;
	        break
	    case "Eyelet":
	        if (intTmpGatherNumber>-1) {surcharge = arrEyeletSurcharge[intTmpGatherNumber];}
	        break
	    case "Pinchpleat":
	        if (intTmpGatherNumber>-1) {surcharge = arrPinchpleatSurcharge[intTmpGatherNumber];}
	        break
	    case "Tabtop":
	        if (intTmpGatherNumber>-1) {surcharge = arrTabtopSurcharge[intTmpGatherNumber];}
	        break
        }
    
    var sel = document.getElementById('ddlLining')
    var multiplier
    var additionalSurcharge
    
    if(sel.options[sel.selectedIndex].value=='Lined') {
        additionalSurcharge = arrVoileLinedSurcharge[intTmpGatherNumber]
        multiplier = 1
        }
    else if(sel.options[sel.selectedIndex].value=='Unlined') {
        additionalSurcharge = 0
        multiplier = 1
        }
    else{
        additionalSurcharge = 0
        multiplier = sel.options[sel.selectedIndex].value * 1
        }
    
    price = varTmpPrice[intTempDropNumber][intTmpGatherNumber];
    
    if(price=='N/A'){
        showQuoteText();
        }
    else{
        fullprice = ((price * 1) * multiplier) + ((surcharge * 1) + (additionalSurcharge * 1)) * multiplier;
        fullprice = fullprice.toFixed(2)

        document.getElementById('hidPrice').value = ((price * 1) * multiplier).toFixed(2);
        document.getElementById('hidSurcharge').value = (((surcharge * 1) + (additionalSurcharge * 1)) * multiplier).toFixed(2);
        
        showPriceText();
        }
    //alert("surcharge "+ intTmpGatherNumber +" : " + surcharge);
    //alert('price ['+ intTempDropNumber +']['+ intTmpGatherNumber +']: '+ price);
    //alert('fullprice : '+ fullprice);
    
    }

function setUpperAndLowerDrops() {
    var strLBounds= '0,137.01,183.01,228.01'
    var strUBounds= '137,183,228,' + intMaximumDrop
    
    arrTmpDropLbounds = strLBounds.split(',')
    arrTmpDropUbounds = strUBounds.split(',')
    }

function setUpperAndLowerWidths() {
    var selected = document.getElementById('ddlHeading').options[document.getElementById('ddlHeading').selectedIndex].value;    

    /*
	Replace 20/04/2009 - Requested By David via Email
	switch (selected) {
	    case "Pencil Pleat":
	        var strLBounds= '0,147,229,311,389'
            var strUBounds= '146,228,310,388,470'
	        break
	    case "Eyelet":
	        var strLBounds= '0,147,229,311,389'
            var strUBounds= '146,228,310,388,470'
	        break
	    case "Pinchpleat":
	        var strLBounds= '0,128,191,253,316'
            var strUBounds= '127,190,252,315,378'
	        break
	    case "Tabtop":
	        var strLBounds= '0,209,324,438,528'
            var strUBounds= '208,323,437,527,614'
	        break
        }
	*/
	
	switch (selected) {
	    case "Pencil Pleat":
	        var strLBounds= '0,147,220,289,371'
            var strUBounds= '146,219,289,370,420'
	        break
	    case "Eyelet":
	        var strLBounds= '0,147,220,289,371'
            var strUBounds= '146,219,289,370,420'
			break
	    case "Pinchpleat":
	        var strLBounds= '0,121,178,239,305'
            var strUBounds= '120,178,238,305,356'
	        break
	    case "Tabtop":
	        var strLBounds= '0,209,324,438,528'
            var strUBounds= '208,323,437,527,614'
	        break
        }

    arrTmpGatherLbounds = strLBounds.split(",")
    arrTmpGatherUbounds = strUBounds.split(",")

    }
    
function showQuoteText() {
    if(document.getElementById('pricetext')!=null){
        document.getElementById('pricetext').innerHTML = 'We cannot cater for your size online. Please contact us directly for a quote on (01953) 603529'
    }
    
    if(document.getElementById('btnAddBasket')!=null){
        document.getElementById('btnAddBasket').style.visibility='hidden';
    }
    
    document.getElementById('hidPrice').value = '';
}

function showPriceText() {
    if(document.getElementById('pricetext')!=null){
        if(discountmultiplier>0&&discountmultiplier<1&&fullprice>0){
            document.getElementById('pricetext').innerHTML = 'Price: <span class=spanOfferSlash>&pound;'+ fullprice +'</span> &pound;' + (fullprice * discountmultiplier).toFixed(2);
        } else {
            document.getElementById('pricetext').innerHTML = 'Price: &pound;' + fullprice;
        }
    }
    
    if(document.getElementById('btnAddBasket')!=null){
        document.getElementById('btnAddBasket').style.visibility='visible';
    }    
}

function showErrorText() {
    if(document.getElementById('pricetext')!=null){
        document.getElementById('pricetext').innerHTML = 'please complete all fields in the form before adding to basket'
    }
}

function convertDimensions(id){
    var selObj = document.getElementById('ddlDimensions');
    var convertTo = selObj.options[selObj.selectedIndex].value;

    if (convertTo == 'inches') {
        var inchesResult = document.getElementById(id).value * 2.54
        return Math.round(inchesResult);
    }
    else {
        return document.getElementById(id).value;
    }
}

function checkForm() {
    var hidPrice = document.getElementById('hidPrice');
    if(hidPrice.value=='' || hidPrice.value=='0'){
        showErrorText();
    }
    else {
        document.getElementById('productForm').submit();
    }
}

function calcGather() {
    var elem = document.getElementsByTagName('span')
    var calced
    for(var i = 0; i < elem.length; i++) {
        if(elem[i].id.substring(0,5)=='size_') {
            calced = ((elem[i].id.substring(5,elem[i].id.length) * 2)/(document.frmGather.txtWidth.value * 1)).toFixed(1)
            elem[i].innerHTML = calced + ' times'
            elem[i].className = ''
            
            document.getElementById('td_'+elem[i].id).className = ''
            
            if(calced >= 1.7 && calced <= 2.3){
                elem[i].className = 'chosenCalc'
                document.getElementById('td_'+elem[i].id).className = 'chosenCalcTD'
                }
            }
        }
    }

function calcFabricPrice(price, multiplier) {
    checkNumeric('txtMetres', 1);
    
    var txtMetres = document.getElementById('txtMetres');
    var hidPrice = document.getElementById('hidPrice');
    var pricetext = document.getElementById('pricetext');
    var finalprice = (txtMetres.value * 1) * (price * 1);
    
    if(multiplier>0&&multiplier<1){
        pricetext.innerHTML = '<span class=spanOfferSlash>&pound;'+ finalprice.toFixed(2) +'</span> &pound;' + (finalprice*multiplier).toFixed(2);
        } 
    else {
        pricetext.innerHTML = '&pound;' + (finalprice*multiplier).toFixed(2);
        }

    hidPrice.value = finalprice*multiplier;
    }
    
function checkNumeric(id, resetto) {
    if(document.getElementById(id)!=null){
        if(IsNumeric(document.getElementById(id).value)!=true){
            document.getElementById(id).value = resetto;
        }
    }
}

function checkNumericValue(value) {
    return IsNumeric(value);
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function checkShowEyelet() {
    var sel = document.getElementById('ddlHeading');
    if(sel.options[sel.selectedIndex].value=='Eyelet') {
        document.getElementById('ddlEyeletColour').style.display = "block";
        }
    else {
        document.getElementById('ddlEyeletColour').style.display = "none";
        }
    }
/*Made to Measure Blinds*/

var arrTmpWidthLbounds = new Array();
var arrTmpWidthUbounds = new Array();
var arrTmpHeightLbounds = new Array();
var arrTmpHeightUbounds = new Array();
var intTmpWidthNumber;
var intTempHeightNumber;

function CheckBlindWidthHeight() {
    intTmpWidthNumber = -1
    intTempHeightNumber = -1

    checkNumeric('txtWidth', 0);
    var intWidth = convertDimensions('txtWidth');
    intWidth = intWidth * 1;

    for (i=0; i<5; i++) {   		
		// removed 2/2/2010 - Problem with width of 66 and multiples. 
      //  if(arrTmpWidthLbounds[i] + 0.99 <= intWidth && intWidth <= arrTmpWidthUbounds[i]) {
        if(arrTmpWidthLbounds[i] <= intWidth && intWidth <= arrTmpWidthUbounds[i]) {
		intTmpWidthNumber = i;
            }
        }

	// override for zero
	if(intWidth==0) { intTmpWidthNumber = 0 }

    checkNumeric('txtDrop', 0);
    var intHeight = convertDimensions('txtHeight');
    intHeight = intHeight * 1;

    for (i=0; i<arrTmpHeightUbounds.length; i++) {     		
        if(arrTmpHeightLbounds[i] <= intHeight && intHeight <= arrTmpHeightUbounds[i]) {
                intTempHeightNumber = i;
            }
        }

    if(intTmpWidthNumber==-1 || intTempHeightNumber==-1) {
      // alert(intTmpWidthNumber);
        showQuoteText();
        }
    else {
        document.getElementById('hidHeight').value = intTempHeightNumber;
        document.getElementById('hidWidth').value = intTmpWidthNumber;

        if((intWidth*1)==0 || (intHeight*1)==0){
            fullprice = 0;
            document.getElementById('hidPrice').value = '';
            showPriceText();
            }
        else {
            setBlindPrices();
            }    
        }
    }

function setBlindUpperAndLowerWidths(str) {
    var arrTmp = new Array();
    arrTmp = str.split(",");
    
    for (i=0;i<arrTmp.length;i++){
        if(i==0){
            arrTmpWidthLbounds[0] = 0;
            arrTmpWidthUbounds[0] = arrTmp[0]
            }
        else {
            arrTmpWidthLbounds[i] = (arrTmpWidthUbounds[i-1] * 1) + 1;
            arrTmpWidthUbounds[i] = arrTmp[i];
            }       
        }
    }

function setBlindUpperAndLowerHeights(str) {
    var arrTmp = new Array();
    arrTmp = str.split(",");

    for (i=0;i<arrTmp.length;i++){
        if(i==0){
            arrTmpHeightLbounds[0] = 0;
            arrTmpHeightUbounds[0] = arrTmp[0]
            }
        else {
            arrTmpHeightLbounds[i] = (arrTmpHeightUbounds[i-1] * 1) + 1;
            arrTmpHeightUbounds[i] = arrTmp[i];
            }       
        }
    }
    
function setBlindPrices() {
    price = 0;
    
    var sel = document.getElementById('ddlLining')
    var multiplier = sel.options[sel.selectedIndex].value * 1

    price = varTmpPrice[intTempHeightNumber][intTmpWidthNumber];
    
    if(price=='N/A'){
        showQuoteText();
        }
    else{
        fullprice = (price*1) * multiplier;
        fullprice = fullprice.toFixed(2)
        document.getElementById('hidPrice').value = fullprice;
        showPriceText();
        }
    }

function checkAlterHeight(MaxHeight){
    if(checkNumeric('alterHeight', 0)==0){
        alert('Please enter a finished drop');
        return false;
    }

    var selObj = document.getElementById('units');
    var convertTo = selObj.options[selObj.selectedIndex].value;
    var MaxHeightInches =  MaxHeight / 2.54
    
    if((document.getElementById('alterHeight').value*1)>(MaxHeight*1) && convertTo=='cm'){
        alert('Maximum drop is '+ MaxHeight + 'cm ('+ MaxHeightInches.toFixed(0) +' inches)');
        return false;
    }
    if((document.getElementById('alterHeight').value*1)>(MaxHeightInches*1) && convertTo=='inches'){
        alert('Maximum drop is '+ MaxHeight + 'cm ('+ MaxHeightInches.toFixed(0) +' inches)');
        return false;
    }
    return true;
}