

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 treePrepare(el) {
	var els=el.getElementsByTagName('IMG');
	for (var i=0, l=els.length; i<l; i++) {
		if (els[i].id.length) els[i].onclick=treeSwitchCorner;
		}
	}

function treeSwitchCorner(el) {
	if (this.nodeName=='IMG') el=this;
	var id=el.id.substr(1);
	var block,bs;
	if (!((block=D.getElementById('n'+id+'b')) && (bs=block.style))) return false;
	var off=(bs.display=='block');
	el.src=off ? TreeCornerImgClosed : TreeCornerImgOpen;
	bs.display=off ? 'none' : 'block';
	if (!off) treeLoadCorner(id);
	return false;
	}

function treeLoadCorner(id) {
	var block;
	if (!(block=D.getElementById('n'+id+'b')) || block.firstChild) return false;
	block.innerHTML=TreeLoadingHtml;
	var iframe=D.createElement('IFRAME');
	iframe.frameBorder='0';
	iframe.scrolling='no';
	var s=iframe.style;
	s.position='absolute';
	s.left=s.top=s.width=s.height='0px';
	s.visibility='hidden';
	s.zIndex=-1;
	iframe.src=SITE_ROOT+'/menu_categories.php?catId='+id;
	D.body.appendChild(iframe);
	setTimeout(function () { if (!block.Loaded) iframe.src=iframe.src+'&'; },10000);
	}

function treeSetCorner(id,html) { // called from iframe
	var block;
	if (!(block=D.getElementById('n'+id+'b'))) return false;
	block.Loaded=true;
	block.innerHTML=html;
	treePrepare(block);
	}

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

var s_Categores={};

function s_setCategoryName(sel) {
	var ind=sel.selectedIndex;
	sel.form.elements[sel.name+'_name'].value=(ind>1) ? sel.options[ind].text : '';
	}

function s_loadCategories(sel1,sel2) {
	sel2.selectedIndex=0;
	sel2.options.length=2;
	sel2.onchange();
	sel2.disabled=true;

	var parId=sel1.options[sel1.selectedIndex].value;
	if (parId==0 || parId==1000000) return;

	var cat=s_Categores[parId];
	if (!cat) cat=s_Categores[parId]={};
	cat.sel1=sel1;
	cat.sel2=sel2;

	var scats=cat.children;
	if (scats) {
		var i=2;
		for (var id in scats) sel2.options[i++]=new Option(scats[id].name,id);
		sel2.disabled=false;
		sel2.focus();
		return;
		}

	sel2.options[2]=new Option('Loading...','1000000');
	sel2.selectedIndex=2;

	var iframe=D.createElement('IFRAME');
	iframe.frameBorder='0';
	iframe.scrolling='no';
	var s=iframe.style;
	s.position='absolute';
	s.left=s.top=s.width=s.height='0px';
	s.visibility='hidden';
	s.zIndex=-1;
	iframe.src=SITE_ROOT+'/s_categories.php?parId='+parId;
	D.body.appendChild(iframe);
	setTimeout(function () { if (!cat.children) iframe.src=iframe.src+'&'; },10000);
	}

function s_setCategories(parId,data) { // called from iframe
	var cat=s_Categores[parId];
	var scats=cat.children={};
	for (var id in data) s_Categores[id]=scats[id]={'name':data[id]};
	s_loadCategories(cat.sel1,cat.sel2);
	}

function s_init(f) {
	s_setCategoryName(f.s_brand);
	s_setCategoryName(f.s_series);
	s_setCategoryName(f.s_model);

	var childSel={'s_brand':'s_series', 's_series':'s_model'};
	f.s_brand.onchange=f.s_series.onchange=f.s_model.onchange=function() {
		s_setCategoryName(this);
		var n2=childSel[this.name];
		if (n2) s_loadCategories(this,this.form.elements[n2]);
		}

	f.search_text.onfocus=function() { if (this.value=='keyword') this.value=''; }
	f.search_text.onblur=function() { if (this.value=='') this.value='keyword'; }
	f.search_text.onblur();
	}

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

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
	}
  }


function popupIt(link) {
	makePullDown(link.href,'',500,400)
	return false
	}


