function sides_calc() {
  sidesp = 0 ;
  sideso = 0 ;
  if(document.getElementById('sides_pop')) {
    len = parseFloat(document.odform.length.value) ;
    wid = parseFloat(document.odform.width.value) ;
    sides_open = parseFloat(document.odform.sides_open.value) ;
    if(document.getElementById('ext_gutter')) {
      document.getElementById('ext_gutter').innerHTML = '' ;
    }
    if( len > 0 && wid > 0 ) {
      sidesp = upto_p ;
      // extra length
      if( len > upto_l ) {
        sidesp = sidesp + ( Math.ceil( ( len - upto_l ) / extra_s ) * extra_p ) ;
      }
      // extra width
      if( wid > upto_w ) {
        sidesp = sidesp + ( Math.ceil( ( wid - upto_w ) / extra_s ) * extra_p ) ;
      }
      // sides
      if(isNaN(sides_open)) {
        document.odform.sides_open.value = 0 ;
      }
      else {          
        if(sides_open < 0) {
          sides_open = 0 ;
        }
        if(sides_open > 4) {
          sides_open = 4 ;
        }
        if(sides_open > 0) {
          sideso = open_p * sides_open ;
        }
      }
      document.baform.sides_open.value = sides_open ;
      // set guttering
      if( (document.odform['opt_'+sides_k].checked == true) && document.getElementById('ext_gutter') && document.odform['opt_'+gutt_k] ) {
        document.odform['opt_'+gutt_k].checked = true ; 
        document.getElementById('ext_gutter').innerHTML = ' (required with side frames)' ;
      }
    }
  }
  var outp = new Array(sidesp,sideso);
  return outp ;
}

function sides_opt() {
  if(document.getElementById('sides_pop')) {
    if(document.odform['opt_'+sides_k].checked == true) {
      document.getElementById('sides_opt').style.display='table-row';
    }
    else {
      document.getElementById('sides_opt').style.display='none';
      document.odform.sides_open.value = 0 ;
    }
  }
}

function buynow() {
  document.baform.action.value = 'buy';
  document.baform.submit();
  return false;
}

function printnow() {
  document.baform.action.value = 'print';
  document.baform.submit();
  return false;
}

function odsizec() {
  os = document.odform.odsize.options[document.odform.odsize.selectedIndex].value.split(",",2);
  document.odform.length.value = parseFloat(os[0]);
  document.odform.width.value = parseFloat(os[1]);
  odcalc();
}

function odchange() {
  len = parseFloat(document.odform.length.value);
  wid = parseFloat(document.odform.width.value);
  if(len > 0 && wid > 0) {
    odcalc();
  }
  else {
    odstart();
  }
}

function odclick(len,wid) {
  document.odform.length.value = len;
  document.odform.width.value = wid;
  odcalc();
}

function odclick_c(dia) {
  document.odform.diameter.value = dia;
  odcalc_c();
}

function odstart() {
  for ( n=0; n<opts.length; n++ ){
    o_arr = opts[n].split(",",5);
    txt = "txt_"+o_arr[0] ;
    if(typeof(document.getElementById(txt)) == "object") {
      document.getElementById(txt).innerHTML = '+ &pound;';
    }
  }
}

function odcalc() {
  len = parseFloat(document.odform.length.value);
  wid = parseFloat(document.odform.width.value);
  if(wid>len) {
    ltmp = len ;
    len = wid ;
    wid = ltmp ;
    document.odform.length.value = len;
    document.odform.width.value = wid;
  }
  price = 0 ;
  for ( n=main.length-1; n>=0; --n ){
    m_arr = main[n].split(",",4);
    if(len<=m_arr[1] && wid<=m_arr[2] && len>=minl && wid>=minw) {
      price = parseFloat(m_arr[3]) ;
    }
  }
  if(price==0) {
    alert('Dimensions entered are invalid or outside the minumum and maximum sizes');
    return false ;
  }
  total = price ;
  optc = "";
  optf = "";
  for ( n=0; n<opts.length; n++ ){
    o_arr = opts[n].split(",",9);
    opt = "opt_"+o_arr[0] ;
    txt = "txt_"+o_arr[0] ;
    min_l = parseFloat(o_arr[5]) ;
    min_w = parseFloat(o_arr[6]) ;
    max_l = parseFloat(o_arr[7]) ;
    max_w = parseFloat(o_arr[8]) ;
    optv = 0;
    optx = 0 ;
    if(document.getElementById('sides_pop') && (o_arr[0]==sides_k)) {
      // lantern sides special calc
      sides_open = document.baform.sides_open.value ;
      optarr = sides_calc();
      optv = optarr[0]; // main cost
      optx = optarr[1]; // side openers
      o_arr[4] = 'Side Frames';
      if( sides_open > 0) {
        o_arr[4] = o_arr[4] + '<br>(' + sides_open + ' opener';
        if(sides_open > 1) {
          o_arr[4] = o_arr[4] + 's';
        }
        o_arr[4] = o_arr[4] + ')';
      }
    }
    else if(o_arr[2]>0) {
      // fixed
      optv = parseFloat(o_arr[2]) ;
    }
    else if(o_arr[3]>0) {
      // percent
      optv = parseFloat( price * o_arr[3] / 100 ) ;
    }
    if(o_arr[1]) {
      // dropdown
      tp = o_arr[1].split("|",2) ;
      ts = "sel_" + tp[0];
      tn = tp[1];
      if( len < min_l || wid < min_w || ( max_l > 0 && len > max_l ) || ( max_w > 0 && wid > max_w  ) ) {
        mc = false ;
        tv = o_arr[4] + " (N/A)" ;
      }
      else {
        tv = o_arr[4].replace("&amp;", "&") + " (£" + optv.toFixed(2) + ")" ;
        mc = true ;
      }
      fm = document.odform[ts].options ;
      if(tn == 0) {
        ti = fm.selectedIndex ;
        fm.length = 0 ;
      }
      tx = false ;
      if(ti == tn) {
        if(mc) {
          tx = true ;
          total = total + optv ;
          optc = optc + "<DIV CLASS=\"o_price\">\+ " + optv.toFixed(2) + "</DIV><DIV CLASS=\"o_optsv\">" + o_arr[4] + "</DIV>";
          if(optf != "") { optf = optf + ','; }
        }
        optf = optf + o_arr[0];
      }
      fm[tn]=new Option(tv, tn, tx, tx) ;
    }
    else if(typeof(document.getElementById(txt)) == "object") {
      if( len < min_l || wid < min_w || ( max_l > 0 && len > max_l ) || ( max_w > 0 && wid > max_w  ) ) {
        // min/max size
        document.getElementById(opt).checked = false;
        document.getElementById(txt).innerHTML = 'N/A';
      }
      else {
        document.getElementById(txt).innerHTML = "+ &pound;"+optv.toFixed(2);
        if(document.getElementById(opt).checked) {
          // checkbox
          total = total + optv + optx ;
          optc = optc + "<DIV CLASS=\"o_price\">\+ " + (optv+optx).toFixed(2) + "</DIV><DIV CLASS=\"o_optsv\">" + o_arr[4] + "</DIV>";
          if(optf != "") { optf = optf + ','; }
          optf = optf + o_arr[0];
        }
      }
    }
  }
  document.getElementById("qorder").style.display = "none";
  document.getElementById("selectopt").style.display = "block";
  document.getElementById("o_dims").innerHTML = len + "x" + wid + " mm" ;
  document.getElementById("o_main").innerHTML = price.toFixed(2) ;
  document.getElementById("o_opts").innerHTML = optc;
  document.getElementById("o_totc").innerHTML = "TOTAL:";
  document.getElementById("o_toti").innerHTML = "(including VAT @ 20%)";
  document.getElementById("o_totv").innerHTML = "&pound; " + total.toFixed(2);
  document.baform.length.value = len;
  document.baform.width.value = wid;
  document.baform.options.value = optf;
  document.getElementById("o_buttons").style.display = "block";
  // MM_showHideLayers('o_buttons','','show');
}

function odcalc_c() {
  dia = parseFloat(document.odform.diameter.value);
  price = 0 ;
  for ( n=main.length-1; n>=0; --n ){
    m_arr = main[n].split(",",3);
    if(dia<=m_arr[1] && dia>=mind) {
      price = parseFloat(m_arr[2]) ;
    }
  }
  avg = dia ;
  if(price==0) {
    alert('Diameter entered is invalid or outside the minumum and maximum sizes');
    return false ;
  }
  total = price ;
  optc = "";
  optf = "";
  for ( n=0; n<opts.length; n++ ){
    o_arr = opts[n].split(",",9);
    opt = "opt_"+o_arr[0] ;
    txt = "txt_"+o_arr[0] ;
    min_d = (parseFloat(o_arr[5]) + parseFloat(o_arr[6]))/2 ;
    max_d = (parseFloat(o_arr[7]) + parseFloat(o_arr[8]))/2 ;
    optv = 0;
    if(o_arr[2]>0) {
      optv = parseFloat(o_arr[2]) ;
    }
    else if(o_arr[3]>0) {
      optv = parseFloat( price * o_arr[3] / 100 ) ;
    }
    if(o_arr[1]) {
      // dropdown
      tp = o_arr[1].split("|",2) ;
      ts = "sel_" + tp[0];
      tn = tp[1];
      tv = o_arr[4] + " (£" + optv.toFixed(2) + ")" ;
      fm = document.odform[ts].options ;
      if(tn == 0) {
        ti = fm.selectedIndex ;
        fm.length = 0 ;
      }
      tx = false ;
      if(ti == tn) {
        if( avg < min_d || ( max_d > 0 && avg > max_d ) ) {
          // min/max size
          tx = false ;
          tv = o_arr[4] + " (N/A)" ;
        }
        else {
          tx = true ;
          total = total + optv ;
          optc = optc + "<DIV CLASS=\"o_price\">\+ " + optv.toFixed(2) + "</DIV><DIV CLASS=\"o_optsv\">" + o_arr[4] + "</DIV>";
          if(optf != "") {
            optf = optf + ',';
          }
        }
        optf = optf + o_arr[0];
      }
      fm[tn]=new Option(tv, tn, tx, tx) ;
    }
    else if(typeof(document.getElementById(txt)) == "object") {
      if( avg < min_d || ( max_d > 0 && avg > max_d ) ) {
        // min/max size
        document.getElementById(opt).checked = false;
        document.getElementById(txt).innerHTML = 'N/A';
      }
      else {
        document.getElementById(txt).innerHTML = "+ &pound;"+optv.toFixed(2);
        if(document.getElementById(opt).checked) {
          total = total + optv ;
          optc = optc + "<DIV CLASS=\"o_price\">\+ " + optv.toFixed(2) + "</DIV><DIV CLASS=\"o_optsv\">" + o_arr[4] + "</DIV>";
          if(optf != "") {
            optf = optf + ',';
          }
          optf = optf + o_arr[0];
        }
      }
    }
  }
  document.getElementById("qorder").style.display = "none";
  document.getElementById("selectopt").style.display = "block";
  document.getElementById("o_dims").innerHTML = dia + " mm diameter" ;
  document.getElementById("o_main").innerHTML = price.toFixed(2) ;
  document.getElementById("o_opts").innerHTML = optc;
  document.getElementById("o_totc").innerHTML = "TOTAL:";
  document.getElementById("o_toti").innerHTML = "(including VAT @ 20%)";
  document.getElementById("o_totv").innerHTML = "&pound; " + total.toFixed(2);
  document.baform.diameter.value = dia;
  document.baform.options.value = optf;
  // MM_showHideLayers('o_buttons','','show');
  document.getElementById("o_buttons").style.display = "block";
}

op = 0 ;
scr = 0 ;
sor = 0 ;
sp = 10 ;

function odclose() {
  if(scr) {
    scr = 0 ;
    clearTimeout(sct);
  }
  lp = 0 ;
  op = 0 ;
  MM_showHideLayers('selectopt','','show');
  slideclose();
}

function odopen() {
  if(checkBrowser() < 999) {
    sp = 15 ;
  }
  if(sor) {
    sor = 0 ;
    clearTimeout(sot);
  }
  MM_showHideLayers('ordsel1','','show');
  if(op==1) {
    return;
  }
  document.getElementById("ordsel2").style.left="334px";
  lp = 334 ;
  MM_showHideLayers('selectopt','','hide');
  slideopen();
  sides_opt();
}

function slideopen() {
  lp = lp - sp ;
  if(lp < 0) {
    document.getElementById("ordsel2").style.left=0;
    op = 1 ;
    return ;
  }
  document.getElementById("ordsel2").style.left=lp+"px";
  sor = 1 ;
  sot = setTimeout("slideopen()",1);
}

function slideclose() {
  lp = lp + sp ;
  if(lp > 334) {
    document.getElementById("ordsel2").style.left="334px";
    MM_showHideLayers('ordsel1','','hide');
    return ;
  }
  document.getElementById("ordsel2").style.left=lp+"px";
  scr = 1 ;
  sct = setTimeout("slideclose()",1);
}

function getStyle(el,styleProp){
  var x = document.getelementbyid(el);
  if (x.currentStyle)
  var y = x.currentStyle[styleProp];
  else if (window.getComputedStyle)
  var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
  return y;
}

function callc(f,v) {
  if(f.value == v) {
    f.value = '';
  }
}

function runonload() {
  // dtime();
}

function dtime() {
  var d = new Date();
  document.getElementById('dt').innerHTML = d.formatDate("l jS F Y");
  setTimeout("dtime()", 10000);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function show(pn,pw,ph) {
  pshow = window.open(pn, "ppwin", "toolbar=no,location=no,menubar=no,directories=no,status=no,scrollbars=no,resizable=yes,width="+pw+",height="+ph+",left=0,top=0" );
  if ( ( typeof(pshow) != "object" ) || !pshow || pshow.closed ) {
   return false;
  }
  else {
    pshow.window.focus();
  }
}

function cshow(pn,pw,ph) {
  var aw = 12 ;
  var ah = 83 ;
  mw = screen.availWidth - aw ; 
  mh = screen.availHeight - ah ; 
  if( ph > mh ) {
    pw =  Math.round( mh * pw / ph ) ;
    ph = mh ;
  }
  if( pw > mw ) {
    ph =  Math.round( mw * ph / pw ) ;
    pw = mw ;
  }
	pl = Math.round( ( mw - pw - aw) / 2 ) ;
	pt = Math.round( ( mh - ph - ah) / 2 ) ;
	pshow = window.open(pn, "ppwin", "toolbar=no,location=no,menubar=no,directories=no,status=no,scrollbars=no,resizable=yes,width="+pw+",height="+ph+",left="+pl+",top="+pt );
	if ( ( typeof(pshow) != "object" ) || !pshow || pshow.closed ) {
		return false;
	}
	else {
		pshow.window.focus();
	}
}

function Blink(layerName,color1,color2,bspeed){
  if(color1 === undefined) { color1='#004E82' ; }
  if(color2 === undefined) { color2='#CF1F26' ; }
  if(bspeed === undefined) { bspeed=500 ; }
  tcol = document.getElementById(layerName).style.color ;
  if(tcol.indexOf('rgb')==0) {
    // convert firefox rgb(r,g,b) to hex
    tarr = tcol.substring(4,tcol.length-1).split(',') ;
    tcol = '#';
    for (i=0;i<3;i++) {
      tbit = (tarr[i]-0).toString(16) ;
      if(tbit.length==1) { tbit = '0' + tbit ; }
      tcol = tcol + tbit ;
    }
  }
  tcol = tcol.toUpperCase();
  if(tcol==color2) {
    document.getElementById(layerName).style.color=color1;
  }
  else {
    document.getElementById(layerName).style.color=color2;
  }
  setTimeout("Blink('"+layerName+"','"+color1+"','"+color2+"',"+bspeed+")",bspeed);
}

function listfiles(fpath,file,fname,filter) {
  pshow = window.open( "listfiles.php?fpath="+fpath+"&file="+file+"&fname="+fname+"&filter="+filter , "listfiles", "toolbar=no,location=no,menubar=no,directories=no,status=no,scrollbars=yes,resizable=yes,width=800,height=600,left=10,top=10" );
  if ( ( typeof(pshow) != "object" ) || !pshow || pshow.closed ) {
   return false;
  }
  else {
    pshow.window.focus();
  }
}

function onbrowse(fname) {
  upfile = document.eform['up_'+fname].value ;
  pos = upfile.lastIndexOf("\\");
  if(pos >= 0) {
    upfile = upfile.substring(pos+1);
  }
  // alert(fpath);
  flen = document.eform[fname].length ;
  for(i=1;i<=flen;i++) {
    fchk = document.eform[fname][i].value ;
    if(upfile == fchk) {
      ask = confirm("Overwrite file " + fchk + " ?");
      if(!ask) {
        document.eform['up_'+fname].value = '';
        elem = document.getElementById('id_up_'+fname) ;
        elem.parentNode.innerHTML = elem.parentNode.innerHTML;
      }
      break;
    }
  }
}

fldchg = 0 ;
function chkleave() {
  if(fldchg && ! confirm('Leave without saving changes?')) {
    return false ;
  }
  return true ;
}

function checkBrowser() {
  var rv = 999;
  if (navigator.appName == 'Microsoft Internet Explorer')  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null) {
      rv = parseFloat( RegExp.$1 );
    }
  }
  return rv ;
}

function addToFavorites(url,title) {
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} 
  else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); 
  }
}

function dtime() {
  var now = new Date();
  var d = Math.floor( (countdown - now) / 1000 / 60 / 60 / 24 );
  disp = counttext + "" + d + " day"; if(d != 1) { disp = disp + "s" }
  var h = Math.floor( (countdown - now) / 1000 / 60 / 60 - (24 * d) );
  disp = disp + " " + h + " hour"; if(h != 1) { disp = disp + "s" }
  var m = Math.floor( (countdown - now) / 1000 / 60 - (24 * 60 * d) - (60 * h) );
  disp = disp + " " + m + " min"; if(m != 1) { disp = disp + "s" }
  // var s = Math.round( (countdown - now) / 1000 - (24 * 60 * 60 * d) - (60 * 60 * h) - (60 * m) );
  // disp = disp + "<br>" + s + " sec"; if(s != 1) { disp = disp + "s" }
  if( now < countdown ) {
    document.getElementById('countd').innerHTML = disp;
  }
  else {
    document.getElementById('countd').innerHTML = "";
  }
  setTimeout("dtime()", 1000);
}

