﻿
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,drating){
    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;
    this.drating=drating;
}
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"];
var opn = false;



function SwitchType(type){
var O;
if(opn==null)
    if($("#typebag_"+type).html().length>100) O = false; else O = true;
else
    O=opn;    

if(!O) {
    //alert('close '+type.toString());
    tabarr[type][0] = $("#typebag_"+type).html();
    $("#typebag_"+type).html("");
    $("#img_"+type).css("background-position","-218px -10px");  
    $("#cb__"+type).css("visibility","hidden");
}
else
{
    //alert('open '+type.toString());
    if(tabarr[type][0].length<100) 
        BuildProds2(type,tabarr[type][1]);
    else
        $("#typebag_"+type).html(tabarr[type][0]);
    $("#cb__"+type).css("visibility","visible");
    $("#img_"+type).css("background-position","-233px -10px");
}
opn=null;
}

function OpenType(type){
    //alert('open func '+type.toString());
    opn = true;
    SwitchType(type);
}
function CloseType(type){
    //alert('close func '+type.toString());
    opn = false;
    SwitchType(type);
}



function BuildProds2(type,breed){
  if(type==0)return;
  //alert(type);
  var tid = type;
  if(type==99) type=15;
  currbreed=breed;
  tabarr[tid][1]=breed;
  var S='';
  var c__;
  for(i=0;i<Types[tid].length;i++){
    if(Types[tid][i].breed==breed || breed==0){
      
      if(i==Types[tid].length-1) c__="c__1";else c__="c__0";
      S+='<div class="celloffer" id="'+c__+'">';
      S+='<table border=0 cellpadding="4" cellspacing="0" width="908"><tr>';
      S+='<td width="60"><img src="'+Types[tid][i].img+'" width="60" height="60" class="oimg"/></td>';
      S+='<td width="530" class="productname2"><b>'+Types[tid][i].name+'</b><p>'+Types[tid][i].desc+'</p>';
      if(window.location.host=="www") S+=' <a target="_blank" href="javascript:if(window.confirm(\'Are you sure?\')) window.open(\'delprod.aspx?id='+Types[tid][i].id+'\',\'delprod\',\'\');this.focus();">delete</a>';	

      if(Types[tid][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+='</td>';
      
      S+='<td align="center" width="79">';
      if(Types[tid][i].mimg=='/images/')
        S+='<img src="/images/spacer.gif" width="79" height="1"/><br/><span class="mname">'+Types[tid][i].mname+'</span><br>';
      else
        S+='<img src="'+Types[tid][i].mimg+'" alt="'+Types[tid][i].mname+'" width="79" height="27"/><br>';
      
      if(Types[tid][i].rating==0)
        S+='<a href="merchant-'+Types[tid][i].mid+'.html#rate" rel="nofollow" class="r_notyet" title="Rate this store">not yet rated</a><br>';
      else{
        S+='<div class="r_full" style="width:'+Math.round((Types[tid][i].rating*56)/1000)+'px"></div><div class="r_clear" style="width:'+(56-Math.round((Types[tid][i].rating*56)/1000))+'px"></div><br>';
        S+='<a href="merchant-'+Types[tid][i].mid+'.html" rel="nofollow" class="r_store">store rating: '+Dig1(Types[tid][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(Types[tid][i].price=='$')
        S+='<span class="ship"><b>visit store<br/> to see price</b></span>';
      else
        S+='<span class="ship" id="pprice'+Types[tid][i].id+'"><b>price: '+Types[tid][i].price+'</b></span>';
      S+='</td></tr><tr><td align="right" nowrap="nowrap">';
      if(Types[tid][i].price!='$')
        S+='<span class="ship" id="ship'+Types[tid][i].id+'">'+Types[tid][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'+Types[tid][i].id+'" valign="top">';
      //if((Types[tid][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(Types[tid][i].price!='$')
        S+='<a href="buy/'+Types[tid][i].id+'.html" id="price'+Types[tid][i].id+'" class="price" rel="nofollow" target="_blank"><b>$'+Types[tid][i].totalprice+'</b></a>';
      S+='</td></tr>';

      if(Types[tid][i].coupon!=null){
          S+='<tr><td colspan="3" align="right"><table cellspacing="0" cellpadding="2" class="'+Types[tid][i].couponclass+'"><tr><td>';
          S+='<span class="minicoupon"><nobr>';
          if(Types[tid][i].couponURL!=''){
            S+='<a href="'+Types[tid][i].couponURL+'" target="_blank" rel="nofollow" class="minicurl">'+Types[tid][i].coupon+'</a>';
          }else{
            S+=Types[tid][i].coupon;
          }
          S+='</nobr>';
          if(Types[tid][i].couponcode!=''){
            S+='<br><nobr>Code: '+Types[tid][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((Types[tid][i].notes!=null)&&(Types[tid][i].notes!=''))
        S+='<br><span class="notes">'+Types[tid][i].notes+'</span>';
      S+='<br><a href="buy/'+Types[tid][i].id+'.html" rel="nofollow" target="_blank"><img src="images/shop.gif" border="0"/></a><br>';
      if(Types[tid][i].outofstock==1)
        S+='<span class="outofstock">Out Of Stock</span>';
      else
        S+='<span class="stock">In Stock</span>';
      S+='</td></tr></table></div>';
      //alert(Types[tid][i].typeid);
  }
  
  }
  //S+='';
  
  
  DrawBreedStr(tid,breed);
  
  $("#typebag_"+tid).html(S);
  
}

function DrawBreedStr(type,breed){
    var tid = type;
    if(type==99) type=15;
    var Sbreed='';
    var bA = new Array();
    var isbreed = false;
    //for(i=0;i<Items.length;i++){
    for(i=0;i<Types[tid].length;i++){
        if((Types[tid][i].typeid==type)||(type==0)){
            isbreed = false;
            for(j=0;j<bA.length;j++)if(bA[j]==Types[tid][i].breed){isbreed=true;break;}
            if(!isbreed) bA.push(Types[tid][i].breed);
        }
    }
    
  if(bA.length>1){
    bA.push(0);
    bA.sort();
    Sbreed="";
    for(j=0;j<bA.length;j++){
        if(bA[j]==breed)
            Sbreed+="<span>"+breeds[bA[j]]+"</span> ";
        else
            Sbreed+='<a href="javascript:BuildProds2('+tid+','+bA[j]+');" >'+breeds[bA[j]]+'</a> ';

            if(j!=bA.length-1) Sbreed+=' ';
    }
    Sbreed+="";
    $("#divbreed_"+tid).html(Sbreed);
  }else if(breed==0) {
    $("#divbreed_"+tid).html("");
  }
  
  //if(type==6){
  //  tdbreed.innerHTML="<div align='right'><img src='images/video_icon.gif'> <a href='#video'>Refill instructions</a> &nbsp;&nbsp;</div>";
  //  }
  
}


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.drating-a.drating;
}
function SortItems(type){
    if(currsort==0 || currsort==2) currsort = 1;
    else currsort=2;
    if(currsort==1)
        $("#psort"+type).css("background-position","-256px -18px");
    else
        $("#psort"+type).css("background-position","-273px -18px");
    
    $("#dsort"+type).css("background-position","-300px 0px");
    $("#rsort"+type).css("background-position","-300px 0px");
    
    Types[type].sort(eval('pricesort'+currsort));
    BuildProds2(type,tabarr[type][1]);
}

function DefaultSort(type){
    Types[type].sort(defaultsortprods);
    BuildProds2(type,tabarr[type][1]);
    currsort=0;
    $("#psort"+type).css("background-position","-300px 0px");
    $("#dsort"+type).css("background-position","-273px -18px");
    $("#rsort"+type).css("background-position","-300px 0px");
}
function RatingSort(type){
    Types[type].sort(ratingsortprods);
    BuildProds2(type,tabarr[type][1]);
    currsort=0;
    $("#psort"+type).css("background-position","-300px 0px");
    $("#dsort"+type).css("background-position","-300px 0px");
    $("#rsort"+type).css("background-position","-273px -18px");
    
}

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(typeid,index,price,ship,freeship){
	rprice = parseFloat(price.replace("$",""));
	if(ship!=null) ship = parseFloat(ship);
	
	if(document.getElementById('yourcountry').innerHTML=="USA")
		if(Types[typeid][index].shipprice=="free") ship=0;
		else
			if(Types[typeid][index].shipprice!="") ship = parseFloat(Types[typeid][index].shipprice.replace("$",""));
			
	if((ship==null)||(ship==-1))
		fullprice = -1;
	else
		fullprice = rprice+ship;
		
	price = price.replace("$","");
	if(ship==null){
		shiptext = "shipping: see site";
		Types[typeid][index].totalprice=price;
		}
	else{
		if(((rprice>=freeship)&&(freeship>0))||(ship==0)){
			Types[typeid][index].totalprice=price;
			shiptext = "free shipping";
		}else if(ship!=-1){
		    Types[typeid][index].totalprice=Dig2(fullprice);
			shiptext = "+shipping: $"+Dig2(ship);
			
		}else{
			Types[typeid][index].totalprice=price;
			shiptext = "<font color=red>No shipping to <br>"+document.getElementById('yourcountry').innerHTML+"</font>";
		}
	}
	Types[typeid][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(z=0;z<tabids.length;z++)
    for(i=0;i<Types[tabids[z]].length;i++){
        ChangePrice(tabids[z],i,Types[tabids[z]][i].price,getShip($('#yourcountry').html(),Types[tabids[z]][i].mid),getFreeShip($('#yourcountry').html(),Types[tabids[z]][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;
		}
	}
	*/

	for(z=0;z<tabids.length;z++){
	    if(tabarr[tabids[z]][2]==1)
	        BuildProds2(tabids[z],tabarr[tabids[z]][1]);
	}
}



