﻿function Product(id,default_sort_id,typeid,name,desc,img,mimg,mname,mid,price,shipping,shipprice,totalprice,breed,code,video,bestprice,rating,coupon,couponcode,couponURL,couponclass,outofstock,notes){
    this.id=id;
    this.typeid=typeid;
    this.name=name;
    this.desc=desc;
    this.img=img;
    this.mimg=mimg;
    this.mname=mname;
    this.mid=mid;
    this.price=price;
    this.shipping=shipping;
    this.shipprice=shipprice;
    this.totalprice=totalprice;
    this.breed=breed;
    this.code=code;
    this.video=video;
    this.bestprice=bestprice;
    this.rating=rating;
    this.coupon=coupon;
    this.couponcode=couponcode;
    this.couponURL=couponURL;
    this.couponclass=couponclass;
    this.outofstock=outofstock;
    this.notes=notes;
    this.default_sort_id=default_sort_id;
}
function Merchant(id,info,image){
    this.id=id;
    this.info=info;
    this.image=image;
}
function Coupons(pid,text,code,url){
    this.pid=pid;
    this.text=text;
    this.code=code;
    this.url=url;
}
function mmove(id,x,y){
	eval('div1.style.left ='+ (x+document.body.scrollLeft+20));
	eval('div1.style.top ='+ (y+document.body.scrollTop-70));
	eval('div2.style.left ='+ (x+document.body.scrollLeft+20));
	eval('div2.style.top ='+ (y+document.body.scrollTop-70));
	eval('divv'+id+'.style.left ='+ (x+document.body.scrollLeft+30));
	eval('divv'+id+'.style.top ='+ (y+document.body.scrollTop-60));
}
function mover(id){
	eval('div1.style.visibility="visible"');
	eval('div2.style.visibility="visible"');
	eval('divv'+id+'.style.visibility="visible"');
}
function mout(id){
	eval('div1.style.visibility="hidden"');
	eval('div2.style.visibility="hidden"');
	eval('divv'+id+'.style.visibility="hidden"');
}
function ShowCoupons(id,x,y){
var sc='';
    for(var i=0;i<Cpns.length;i++){
        if(Cpns[i].pid==id){
            if(Cpns[i].url!='')
                sc+="<a href='"+Cpns[i].url+"' rel='nofollow'>"+Cpns[i].text+"</a>";
            else
                sc+=Cpns[i].text;
            if(Cpns[i].code!='')                
                sc+='<br>Code: <i>'+Cpns[i].code+'</i>';
            sc+="<br><br>";    
        }
    }
    if(sc!=''){
        sc='<table cellspacing="2" cellpadding="3"><tr><td width="100%"><b>Coupons & Deals</b></td><td><a><img src="images/close-pop.gif" onclick="document.getElementById(\'divcpn\').style.visibility=\'hidden\'" style="cursor:hand;" width="15" height="14"></td></tr><tr><td colspan="2" class="coupon_pop"><hr size="1">Apply one of these coupon codes in merchant\'s shopping cart to get discount.</td></tr><tr><td class="coupon_pop">'+sc+'</td></tr></table>';
        document.getElementById('divcpn').style.visibility='visible';
        document.getElementById('divcpn').innerHTML=sc;
        document.getElementById('divcpn').style.left=(x+document.documentElement.scrollLeft-110)+"px";
        document.getElementById('divcpn').style.top=(y+document.documentElement.scrollTop+15)+"px";
    }
}
function ClickEvent(e){
    if (window.event)
    {//code for ie
        if(window.event.srcElement.id.indexOf('showc')>=0){
            if(divcpn.style.visibility=='visible'){divcpn.style.visibility='hidden';return;}
            ShowCoupons(window.event.srcElement.id.substring(5),event.clientX,event.clientY);
        }
    } 
    else
    {//code for firefox
        if(e.target.id.indexOf('showc')>=0){
            ShowCoupons(e.target.id.substring(5),e.clientX,e.clientY);
        }
    }

}
//Content.attachEvent('onclick', AA);

var breeds = ["all types","original","remanufactured","compatible"];

function BuildProds(type,breed){
  currbreed=breed;
  var S='';
  var Sbreed='';
  var bA = new Array();
  var isbreed = false;
  for(i=0;i<Items.length;i++){
    if(((Items[i].typeid==type)||(type==0))  &&   ((Items[i].breed==breed)||(breed==0))){
      S+='<div class="celloffer">';
      S+='<table border=0 cellpadding="4" cellspacing="0" width="908"><tr>';
      S+='<td width="60"><img src="'+Items[i].img+'" width="60" height="60" class="oimg"/></td>';
      S+='<td width="530"><a href="buy/'+Items[i].id+'.html" class="productname" rel="nofollow" target="_blank">'+Items[i].name+'</a><br><small>'+Items[i].desc;
      if(Items[i].video==1) S+='<nobr><img src="images/video_icon.gif" width="9" height="8" border="0" style="position:relative; top:1px;"/> <a href="#video">Video instructions</a></nobr>';
      S+='</small><br/></td>';
      
      S+='<td align="center" width="79">';
      if(Items[i].mimg=='/images/')
        S+='<img src="/images/spacer.gif" width="79" height="1"/><br/><span class="mname">'+Items[i].mname+'</span><br>';
      else
        S+='<img src="'+Items[i].mimg+'" alt="'+Items[i].mname+'" width="79" height="27"/><br>';
      
      if(Items[i].rating==0)
        S+='<a href="merchant-'+Items[i].mid+'.html#rate" class="r_notyet" title="Rate this store">not yet rated</a><br>';
      else{
        S+='<div class="r_full" style="width:'+Math.round((Items[i].rating*56)/1000)+'px"></div><div class="r_clear" style="width:'+(56-Math.round((Items[i].rating*56)/1000))+'px"></div><br>';
        S+='<a href="merchant-'+Items[i].mid+'.html" class="r_store">store rating: '+Dig1(Items[i].rating/100) +'</a>';
      }
      S+='</td>';
      
      S+='<td align="center" valign="top"><img src="/images/spacer.gif" width="80" height="1"><br><table width="80" cellspacing="1" cellpadding="0"><tr><td align="right" nowrap="nowrap">';
      if(Items[i].price=='$')
        S+='<span class="ship"><b>visit store<br/> to see price</b></span>';
      else
        S+='<span class="ship" id="pprice'+Items[i].id+'"><b>price: '+Items[i].price+'</b></span>';
      S+='</td></tr><tr><td align="right" nowrap="nowrap">';
      if(Items[i].price!='$')
        S+='<span class="ship" id="ship'+Items[i].id+'">'+Items[i].shipping+'</span>';
      S+='</td></tr><tr><td><img src="/images/spacer.gif" width="1" height="5"/></td></tr><tr><td align="right"><table cellspacing="0" cellpadding="0" border=0><tr><td nowrap="nowrap" class="bestpricetitle" id="bestprice'+Items[i].id+'" valign="top">';
      //if((Items[i].bestprice==1)&&(type!=0)&&(Items.length>1)) S+='Best price';
      S+='</td><td><img src="/images/spacer.gif" width="5" height="1"></td><td align="right">';
      if(Items[i].price!='$')
        S+='<b><span class="price" id="price'+Items[i].id+'">$'+Items[i].totalprice+'</span></b>';
      S+='</td></tr>';

      if(Items[i].coupon!=null){
          S+='<tr><td colspan="3" align="right"><table cellspacing="0" cellpadding="2" class="'+Items[i].couponclass+'"><tr><td>';
          S+='<span class="minicoupon"><nobr>';
          if(Items[i].couponURL!=''){
            S+='<a href="'+Items[i].couponURL+'" target="_blank" rel="nofollow" class="minicurl">'+Items[i].coupon+'</a>';
          }else{
            S+=Items[i].coupon;
          }
          S+='</nobr>';
          if(Items[i].couponcode!=''){
            S+='<br><nobr>Code: '+Items[i].couponcode+'</nobr>';
          }
          S+='</span></td></tr></table></td></tr>';
      }
      S+='</table></td></tr></table></td>';
      S+='<td align="center"><img src="/images/spacer.gif" width="80" height="1">';
      if((Items[i].notes!=null)&&(Items[i].notes!=''))
        S+='<br><span class="notes">'+Items[i].notes+'</span>';
      S+='<br><input type="image" onclick="u('+Items[i].id+');return false;" border="0" src="images/shop.gif"/><br>';
      if(Items[i].outofstock==1)
        S+='<span class="outofstock">Out Of Stock</span>';
      else
        S+='<span class="stock">In Stock</span>';
      S+='</td></tr></table></div>';
  }
  }
  //S+='';
  
    for(i=0;i<Items.length;i++){
        if((Items[i].typeid==type)||(type==0)){
            isbreed = false;
            for(j=0;j<bA.length;j++)if(bA[j]==Items[i].breed){isbreed=true;break;}
            if(!isbreed) bA.push(Items[i].breed);
        }
    }
    
  if(bA.length>1){
    bA.push(0);
    bA.sort();
    Sbreed="";
    for(j=0;j<bA.length;j++){
        if(bA[j]==breed)
            Sbreed+=breeds[bA[j]];
        else
            Sbreed+='<a href="javascript:BuildProds('+type+','+bA[j]+');" >'+breeds[bA[j]]+'</a>';

            if(j!=bA.length-1) Sbreed+=' | ';
    }
    Sbreed+="";
    //tdbreed.className="cellbreed";
    document.getElementById('divbreed').innerHTML=Sbreed;
  }else if(breed==0) {
    document.getElementById('divbreed').innerHTML='';
    //tdbreed.className="cellnobreed";
  }
  //if(type==6){
  //  tdbreed.innerHTML="<div align='right'><img src='images/video_icon.gif'> <a href='#video'>Refill instructions</a> &nbsp;&nbsp;</div>";
  //  }
 document.getElementById('Content0').innerHTML=S;
}

function SwitchTab(type,ltype,breed,obj){
currtype = type;
for(var i=0;i<tabarr.length;i++){
  if(type==tabarr[i]){
    if(obj) obj.blur();
    document.getElementById("a"+type).className='current';
    document.getElementById("li"+type).className='current';
    BuildProds(type,breed);
  }else{
    document.getElementById("a"+tabarr[i]).className='default';
    document.getElementById("li"+tabarr[i]).className='default';
  }
}
}

function pricesort1(a,b){
  return a.totalprice-b.totalprice;
}
function pricesort2(a,b){
  return b.totalprice-a.totalprice;
}
function defaultsortprods(a,b){
  return a.default_sort_id-b.default_sort_id;
}
function ratingsortprods(a,b){
  return b.rating-a.rating;
}
function SortItems(){
    if(currsort==0 || currsort==2) currsort = 1;
    else currsort=2;
    if(currsort==1)
        document.getElementById('img_price_sort').src='images/arr_sort_down.gif';
    else
        document.getElementById('img_price_sort').src='images/arr_sort_up.gif';
    document.getElementById('img_def_sort').src='images/spacer.gif';
    document.getElementById('img_rate_sort').src='images/spacer.gif';
    
    Items.sort(eval('pricesort'+currsort));
    BuildProds(currtype,currbreed);
}

function DefaultSort(){
    Items.sort(defaultsortprods);
    BuildProds(currtype,currbreed);
    currsort=0;
    document.getElementById('img_price_sort').src='images/spacer.gif';
    document.getElementById('img_rate_sort').src='images/spacer.gif';
    document.getElementById('img_def_sort').src='images/arr_sort_down.gif';
}
function RatingSort(){
    Items.sort(ratingsortprods);
    BuildProds(currtype,currbreed);
    currsort=0;
    document.getElementById('img_price_sort').src='images/spacer.gif';
    document.getElementById('img_def_sort').src='images/spacer.gif';
    document.getElementById('img_rate_sort').src='images/arr_sort_down.gif';
}

function setCookie(c_name,value,expiredays){
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie = c_name + "=" + escape(value)+((expiredays==null) ? "" : ";expires=" + exdate.toGMTString())
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

function Dig2(n){
	var s = n.toString();
	if(s.indexOf(".")<=0) return n.toString()+'.00';
	s1 = s.substring(s.indexOf(".")+1,s.length);
	if(s1.length==2) return n.toString();
	s1 = s1.substring(0,2)+"."+s1.substring(2,s1.length);
	fl = Math.round(parseFloat(s1));
	if(fl<10) fl=fl*10;
	return s.substring(0,s.indexOf("."))+"."+fl;
}
function Dig1(n){
	var s = n.toString();
	if(s.indexOf(".")<=0) return n.toString()+'.0';
	s1 = s.substring(s.indexOf(".")+1,s.length);
	if(s1.length==1) return n.toString();
	//s1 = s1.substring(0,2)+"."+s1.substring(2,s1.length);
	//fl = Math.round(parseFloat(s1));
	//if(fl<10) fl=fl*10;
	//return s.substring(0,s.indexOf("."))+"."+fl;
}

function ChangePrice(index,price,ship,freeship){
	rprice = parseFloat(price.replace("$",""));
	if(ship!=null) ship = parseFloat(ship);
	
	if(document.getElementById('yourcountry').innerHTML=="USA")
		if(Items[index].shipprice=="free") ship=0;
		else
			if(Items[index].shipprice!="") ship = parseFloat(Items[index].shipprice.replace("$",""));
			
	if((ship==null)||(ship==-1))
		fullprice = -1;
	else
		fullprice = rprice+ship;
		
	price = price.replace("$","");
	if(ship==null){
		shiptext = "shipping: see site";
		Items[index].totalprice=price;
		}
	else{
		if(((rprice>=freeship)&&(freeship>0))||(ship==0)){
			Items[index].totalprice=price;
			shiptext = "free shipping";
		}else if(ship!=-1){
		    Items[index].totalprice=Dig2(fullprice);
			shiptext = "+shipping: $"+Dig2(ship);
			
		}else{
			Items[index].totalprice=price;
			shiptext = "<font color=red>No shipping to <br>"+document.getElementById('yourcountry').innerHTML+"</font>";
		}
	}
	Items[index].shipping=shiptext;
}

function getShip(loc,merid){
	for(j=0;j<mar.length;j++){
		if((mar[j][1]==loc)&&(mar[j][0]==merid)) {
		return mar[j][2];}
	}
}
function getFreeShip(loc,merid){
	for(j=0;j<mar.length;j++){
		if((mar[j][1]==loc)&&(mar[j][0]==merid)) {
		return mar[j][3];}
	}
}
function ChangeShip(){
    for(i=0;i<Items.length;i++){
        ChangePrice(i,Items[i].price,getShip(document.getElementById('yourcountry').innerHTML,Items[i].mid),getFreeShip(document.getElementById('yourcountry').innerHTML,Items[i].mid));
    }
	var min=999999;
	var dtprice=0;
	for(i=0;i<Items.length;i++){
		dtprice = parseInt(Items[i].totalprice.replace(".",""));
		if((Items[i].typeid==currtype)&&(dtprice<min)&&(dtprice>0)) 
		    min = dtprice;
		    Items[i].bestprice = 0;
		
	}
	for(i=0;i<Items.length;i++){
	    dtprice = parseInt(Items[i].totalprice.replace(".",""));
		if((Items[i].typeid==currtype)&&(dtprice==min)) {
			Items[i].bestprice = 1;
			break;
		}
	}
	BuildProds(currtype,currbreed);
}
