

var T=top
var W=window
var D=document
var allLoaded=0

function num2str(num) {
  var str=""+Math.round(num*100)
  var l=str.length-2
  return str.substr(0,l)+"."+str.substr(l,2)
  }

function checkInt(str) {
  var l=str.length
  if (!l) return null
  for (var i=0; i<l; i++) {
    var ch=str.charAt(i)
    if (ch<'0' || ch>'9') return null
    }
  return eval(str)
  }

function AssocArray() {
  var obj=this.window ? new Object : this
  var argv=AssocArray.arguments
  var l=argv.length
  for (var i=0; i<l; i+=2) obj[argv[i]]=argv[i+1]
  return obj
  }

function displayBlock(id,pos) {
  var el=D.getElementById(id)
//alert(id+' '+el+' '+pos)
  if (el) el.style.display=pos ? 'block' : 'none'
  }
function writeBlock(id,code,pos) {
  D.write("<div ID='",id,"' style='padding-left:10;margin-bottom:5;display:",(pos ? 'block' : 'none'),";'>",code,"</div>")
  }

function checkFilled(field,alertMessage) {
  if (! field.value.length) {
    alert((alertMessage!=null) ? alertMessage : "Empty value is no allowed!")
    field.focus()
    return false
    }
  return true
  }

// ----------------------\
// Make Pull-Down Window
// ------------------------------------\
function makePullDown(source,name,width,height,simple) {
  var win_prop=(simple) ?
	"location=no,toolbar=no,directories=no,menubar=no,status=no,"+
	"scrollbars=no,resizable=no,dependent=no,width="+width+",height="+height :
	"location=no,toolbar=no,directories=no,menubar=yes,status=yes,"+
	"scrollbars=yes,resizable=yes,dependent=no,width="+width+",height="+height
  if (window.screen) {
    var x=Math.floor((screen.width-width)/2)
    var y=Math.floor((screen.height-height)/2)
    win_prop+=",screenX="+x+",screenY="+y+",left="+x+",top="+y
    }
  var win=open(source,name,win_prop)
  win.focus()
  return win
  }
// ------------------------------------/

// ----------------------\
// Check if already submitted
// ------------------------------------\
function formSubmitOnce(f,formCorrect,period) {
  if (formCorrect===false) return false

  if (period<1) period=5
  if (!f.SubmittedFormID) f.SubmittedFormID=Math.round(Math.random()*1000000)
  var a='Submitted'+f.SubmittedFormID

  if (document[a]) {
    alert('Form is already submitted. If you are still on this page, wait about '+period+' sec and try again.')
    return false
    }

  document[a]=1
  setTimeout('document["'+a+'"]=0',period*1000)
  return true
  }
// ------------------------------------/

// -----------------------\
// Special features array
// --------------------------------------\
if (!window.ATTRIBUTES) ATTRIBUTES=Array()
if (!window.PRICING) PRICING=Array()
// --------------------------------------/

// -------------------------------\
// Check and calculate order form
// ------------------------------------\
function calcOrderForm(f,check) {
  if (!checkInt(f.quantity.value)) {
    alert('Incorrect quantity')
    f.total.value=''
    f.quantity.focus()
    return false
    }
  var attributes=ATTRIBUTES[f.product.value]
  var pricing=PRICING[f.product.value]
  var quantity=eval(f.quantity.value)
  var max_quantity=eval(f.max_quantity.value)

  if (max_quantity && max_quantity!=1000000 && quantity>max_quantity) {
    alert('Max quantity = '+max_quantity)
    f.quantity.value=f.max_quantity.value
    f.quantity.focus()
    calcOrderForm(f)
    return false
    }

  for (var opt in pricing)
    if (quantity>=opt && pricing[opt]) {
	f.price.value=pricing[opt]
	break;
	}
  var total=eval(f.price.value)
  for (var i in attributes) {
    var attr=attributes[i]
    var t=attr["type"]
    var options=attr["options"]
    if (t<2) {
      var el=f.elements["attr["+i+"]"]
      var ind=el.selectedIndex
      var opts=el.options
      var l=opts.length
      for (var j=1; j<l; j++) displayBlock(opts[j].id+'-block',j==ind)
      if (ind) total+=options[el.options[ind].value]
      else if (!t && check) {
	alert('Please choose all required features!')
	el.focus()
	return false
	}
      }
    else
      for (var j in options) {
        var el=f.elements["attr["+i+"]["+j+"]"]
        displayBlock(el.id+'-block',el.checked)
        if (el.checked) total+=options[j]
        }
      }
  if (f.total) f.total.value=num2str(total*quantity)
  }


//--------------------- SITE --------------------//

function switchCorner(el) {
  var name=el.id || el.name
  var block,bs
  if (!((block=D.getElementById(name+'b')) && (bs=block.style))) return
  var off=(bs.display=='block')
  D.images[name].src=off ? TreeCornerImgClosed : TreeCornerImgOpen
  bs.display=off ? 'none' : 'block'
  return false
  }

//-----------------------------------------------//

function checkTopSF(f) {
  if (!f.search_text.value.length || f.search_text.value=='Search product...') {
	alert('Enter text for the search')
	f.search_text.focus()
	return false
	}
  }

