function fCheckQty($iid, $qty, $buyflag, $outstocktext){
  //Boton de Comprar Segun Disponibilidad
	objbuy=document.getElementById("buylink_"+$iid);
	objreserve=document.getElementById("reservelink_"+$iid);	
	objoutstock=document.getElementById("outstocklink_"+$iid);
  objoutstocktext=document.getElementById("outstocktext_"+$iid);	
  
  //alert($buyflag);
  if ($qty<=0){
		if ($buyflag==3){//Reservar
			objbuy.style.display="none";
			objreserve.style.display="block";
			objoutstock.style.display="none";
      objoutstocktext.style.display="none";		
		}else{//OutStock
			objbuy.style.display="none";
			objreserve.style.display="none";
			objoutstock.style.display="block";		
      objoutstocktext.style.display="block";			
		}									  
	}else{  	  
    if ($buyflag==1) {//Buy
			objbuy.style.display="block";
			objreserve.style.display="none";
			objoutstock.style.display="none";
			objoutstocktext.style.display="none";
		}
		if ($buyflag==3) {//Reservar
			objbuy.style.display="none";
			objreserve.style.display="block";
			objoutstock.style.display="none";
			objoutstocktext.style.display="none";
		}
		if ($buyflag==2) {//OutStock		  
			objbuy.style.display="none";
			objreserve.style.display="none";
			objoutstock.style.display="block";
			objoutstocktext.style.display="block";			
		}
	}
  if ($outstocktext==0) objoutstocktext.style.display="none"; 

}

function buyNoOptions($iid, $qty, $buyflag){
  fCheckQty($iid, $qty, $buyflag, 0); //No incluye texto de OutStock
  switchimages(0);
}

function buyOptions(id1, id2){
	var sel1 = document.getElementById(id1);
	var sel2 = document.getElementById(id2);
	var qty;
	var buyflag;
  
  if(sel1.selectedIndex<0) sel1.selectedIndex=0;
  //alert(id1);
  
  if(sel1){
    if(sel1.selectedIndex>=0){
      var val1=sel1.options[sel1.selectedIndex].value;    
    	var qty1=sel1.options[sel1.selectedIndex].getAttribute("qty"); //Sin Opciones o Una Opcion  	
    	var buyflag1=sel1.options[sel1.selectedIndex].getAttribute("buyflag"); //Sin Opciones o Una Opcion    
      var iid=sel1.options[sel1.selectedIndex].getAttribute("iid"); //iid item      
      /*sobj=document.getElementById("OPT1");
    	var qty1=sobj.options[sobj.selectedIndex].getAttribute("qty"); //Sin Opciones o Una Opcion
    	var buyflag1=sobj.options[sobj.selectedIndex].getAttribute("buyflag"); //Sin Opciones o Una Opcion
      var iid=sobj.options[sobj.selectedIndex].getAttribute("iid"); //iid item
      */    
    }
  }
                         
	if (sel2){
    if (sel2.selectedIndex>=0){          
      var val2=sel2.options[sel2.selectedIndex].value;	
      var sobj=document.getElementById("OPT2");
    	var qty2=sobj.options[sobj.selectedIndex].getAttribute("qty");; //Con dos Opciones
  	  var buyflag2=sobj.options[sobj.selectedIndex].getAttribute("buyflag"); //Sin Opciones o Una Opcion
  	  
    }else{
      val2=0;
    }	        
  }	
  
  //Verifica Disponibilidad del Item
	if (val1>0 && val2>0){											 
		//Dos Opciones
		qty=qty2;
		buyflag=buyflag2;
	}else{
		qty=qty1;
		buyflag=buyflag1;
	}			    
	
  //Boton de Comprar Segun Disponibilidad
  fCheckQty(iid, qty, buyflag, 1); //Incluye texto de OutStock
	  
  if(sel1){
    //alert(sel1.options[sel1.selectedIndex].value);
    //alert(sel1.selectedIndex);
    if(sel1.selectedIndex>=0) switchimages(sel1.options[sel1.selectedIndex].value);
  }
  /*
  //alert("buylink_"+iid)
	objbuy=document.getElementById("buylink_"+iid);
	objreserve=document.getElementById("reservelink_"+iid);	
	objoutstock=document.getElementById("outstocklink_"+iid);
	
	if (qty<=0){
		if (buyflag==2){//reservar
			objbuy.style.display="none";
			objreserve.style.display="block";
			objoutstock.style.display="none";		
		}else{
			objbuy.style.display="none";
			objreserve.style.display="none";
			objoutstock.style.display="block";		
		}									  
	}else{
		if (buyflag==1) {
			objbuy.style.display="block";
			objreserve.style.display="none";
			objoutstock.style.display="none";
		}
		if (buyflag==2) {
			objbuy.style.display="none";
			objreserve.style.display="block";
			objoutstock.style.display="none";
		}
		if (buyflag==3) {
			objbuy.style.display="none";
			objreserve.style.display="none";
			objoutstock.style.display="block";
		}
	} */	
} 

function dynamicSelect(id1, id2) {
	// Browser and feature tests to see if there is enough W3C DOM support	
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_mac = (agt.indexOf("mac") != -1);
	
	if (!(is_ie && is_mac) && document.getElementById && document.getElementsByTagName) {
		// Obtain references to both select boxes
		var sel1 = document.getElementById(id1);
		var sel2 = document.getElementById(id2);
		if (sel2){
      // Clone the dynamic select box
		  var clone = sel2.cloneNode(true);
		  // Obtain references to all cloned options 
		  var clonedOptions = clone.getElementsByTagName("option");
		}
	  // Onload init: call a generic function to display the related options in the dynamic select box
	  refreshDynamicSelectOptions(sel1, sel2, clonedOptions, id1);
		// Onchange of the main select box: call a generic function to display the related options in the dynamic select box
    
    sel1.onchange = function() {
			refreshDynamicSelectOptions(sel1, sel2, clonedOptions, id1);
		};
	}
}

function refreshDynamicSelectOptions(sel1, sel2, clonedOptions, id1) { 	
	// Delete all options of the dynamic select box
  if (sel2){
    while (sel2.options.length) {
		  sel2.remove(0);
    }	
  	// Create regular expression objects for "select" and the value of the selected option of the main select box as class names
  	var pattern1 = /( |^)(select)( |$)/;
  	var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].value + ")( |$)");
  	// Iterate through all cloned options
  	for (var i = 0; i < clonedOptions.length; i++) {
  		// If the classname of a cloned option either equals "select" or equals the value of the selected option of the main select box
  		if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
  			// Clone the option from the hidden option pool and append it to the dynamic select box
  			sel2.appendChild(clonedOptions[i].cloneNode(true));
  		}
  	} 
  }

  if (id1!="pais"){		
    switchimages(sel1)
    if (sel2) {
      sel2.onchange = function() {
        checkqty(sel1, sel2);
      };
    }
    checkqty(sel1, sel2);
  }
} 

function switchimages(sel1val){
  //alert(sel1.options[sel1.selectedIndex].value);
	//alert(sel1.options[sel1.selectedIndex].value);	 
	var tables=document.getElementsByTagName("TABLE") 
	for (var i=0; i<tables.length; i++){ //BEGIN FOR LOOP
		if (tables[i].getAttribute("rel")=="imagesopt"){
			if (tables[i].getAttribute("rev")==sel1val){
					tables[i].style.display="block" 	
					var showcontainer=document.getElementById("imgz");
					var images = tables[i].getElementsByTagName("img");
					if (images[0]){
						//Para que muestre la imagen grande sin cambiar su tamaņo
						var $im=images[0].src; //
						$im=$im.substr($im.indexOf("imgpath=")+8);						
            var imageHTML='<img id="mainimg" src="'+$im+'" style="border-width: 0" />';
						//imageHTML='<a href="'+dest+'">'+imageHTML+'</a>';
						//alert(imageHTML);
						//alert($img);
            showcontainer.innerHTML=imageHTML
						//this.featureImage=showcontainer.getElementsByTagName("img")[0] //Reference enlarged image itself
						//this.featureImage.onload=function(){ //When enlarged image has completely loaded
						//if (thumbnailviewer2.iefiltercapable) //Is this an IE browser that supports filters?
						//	showcontainer.filters[0].Play()
					}
			}else{ 
				tables[i].style.display="none" 	
			}
		}
	}					
} 

function checkqty(sel1, sel2){
	var qty;
	var buyflag;
	
	val1=sel1.options[sel1.selectedIndex].value;
  sobj=document.getElementById("OPT1");
	var qty1=sobj.options[sobj.selectedIndex].getAttribute("qty"); //Sin Opciones o Una Opcion
	var buyflag1=sobj.options[sobj.selectedIndex].getAttribute("buyflag"); //Sin Opciones o Una Opcion
  var iid=sobj.options[sobj.selectedIndex].getAttribute("iid"); //iid item    

  //alert(iid);
  
	if (sel2){
    val2=sel2.options[sel2.selectedIndex].value;	
    sobj=document.getElementById("OPT2");
  	var qty2=sobj.options[sobj.selectedIndex].getAttribute("qty");; //Con dos Opciones
	  var buyflag2=sobj.options[sobj.selectedIndex].getAttribute("buyflag"); //Sin Opciones o Una Opcion
  }else{
    val2=0;
  }	
	//Verifica Disponibilidad del Item
	if (val1>0 && val2>0){											 
		//Dos Opciones
		qty=qty2;
		buyflag=buyflag2;
	}else{
		qty=qty1;
		buyflag=buyflag1;
	}			
	//Boton de Comprar Segun Disponibilidad
	//alert("buylink_"+iid)
	objbuy=document.getElementById("buylink_"+iid);
	objreserve=document.getElementById("reservelink_"+iid);	
	objoutstock=document.getElementById("outstocklink_"+iid);
	
	if (qty<=0){
		if (buyflag==2){//reservar
			objbuy.style.display="none";
			objreserve.style.display="block";
			objoutstock.style.display="none";		
		}else{
			objbuy.style.display="none";
			objreserve.style.display="none";
			objoutstock.style.display="block";		
		}									  
	}else{
		if (buyflag==1) {
			objbuy.style.display="block";
			objreserve.style.display="none";
			objoutstock.style.display="none";
		}
		if (buyflag==2) {
			objbuy.style.display="none";
			objreserve.style.display="block";
			objoutstock.style.display="none";
		}
		if (buyflag==3) {
			objbuy.style.display="none";
			objreserve.style.display="none";
			objoutstock.style.display="block";
		}
	}
}

