MM_preloadImages('../img_tienda/ajax-loader.gif','../img_tienda/ico_ok.gif')

function getOffsets (evt) {
  var target = evt.target;
  if (typeof target.offsetLeft == 'undefined') {
    target = target.parentNode;
  }
  var pageCoords = getPageCoords(target);
  var eventCoords = { 
    x: window.pageXOffset + evt.clientX,
    y: window.pageYOffset + evt.clientY
  };
  var offsets = {
    offsetX: eventCoords.x - pageCoords.x,
    offsetY: eventCoords.y - pageCoords.y
  }
  return offsets;
}

function getPageCoords (element) {
  var coords = {x : 0, y : 0};
  while (element) {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
    element = element.offsetParent;
  }
  return coords;
}
ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
ns6 = (document.getElementById)? true:false

function showHint(hintname, evt)
{
if(ie4)   // si es explorer el browser
{
	document.all(hintname).style.pixelLeft = evt.clientX-evt.offsetX-2
	document.all(hintname).style.pixelTop = 168;
	document.all(hintname).style.visibility="visible";
	}
	else if(ns4 || ns6)
	{
	document.getElementById(hintname).style.left=evt.pageX-getOffsets(evt).offsetX+0+"px";
	document.getElementById(hintname).style.top = 168+"px";
	document.getElementById(hintname).style.visibility="visible";
	}
}
function showHint2(hintname, event)
{
document.getElementById(hintname).style.visibility="visible";
}
function hideHint(hintname)
{
document.getElementById(hintname).style.visibility="hidden";
}
function tienda()
{
	document.location.href = "../tienda/index.asp"	
}
function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
}
function hideLevel( _levelId) {
	var thisLevel = document.getElementById( _levelId );
	thisLevel.style.display = "none";
	}
function showLevel( _levelId) {
	var thisLevel = document.getElementById( _levelId );
	if ( thisLevel.style.display == "none") {
		hideAll();
		thisLevel.style.display = "block";
		}	
	else {
		hideLevel( _levelId);		
		}		
	}
function normalN1(obj) {
	obj.style.color="#000000";
	obj.style.background="#EAF0F7";
	obj.style.cursor="hand";	
}
function rolloverN1(obj) {
	obj.style.color="#ffffff";
	obj.style.background="#DDEEFF";	
	obj.style.cursor="hand";
}
function normalN2(obj) {
	obj.style.color="#000000";
	obj.style.background="#F4F8FB";
	obj.style.cursor="hand";	
}
function rolloverN2(obj) {
	obj.style.color="#ffffff";
	obj.style.background="#DDEEFF";	
	obj.style.cursor="hand";
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function GP_popupConfirmMsg(msg) { //v1.0
  document.MM_returnValue = confirm(msg);
}
function verifyrut(rut, dv)
{
  var dvr = '0'
  var midv= dv.toUpperCase()
  suma = 0
  mul  = 2
 for (i= rut.length-1; i >= 0; i--)
    {
      suma = suma + parseInt(rut.charAt(i)) * mul
        if (mul == 7)
          mul = 2
        else
          mul++
   }
  res = suma % 11
  if (res==1)
  dvr = 'k'
  else if (res==0)
    dvr = '0'
 else
    {
      dvi = 11-res
      dvr = dvi + ""
    }
  if ( dvr != midv.toLowerCase() )
    { return false; }
  else
    { return true }
}
function url()
{

document.location.href="index.asp"	
}
function validate(emailad) {
var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
	if(((emailad.search(exclude) != -1)||(emailad.search(check)) == -1)||(emailad.search(checkend) == -1))
{
 return true
  }
 else  return false
}
function verifydigit(texto)
{
  largo = texto.length;
  for (i=0; i < largo ; i++ )
    {
   if ( texto.charAt(i) != "0" && 
        texto.charAt(i) != "1" && texto.charAt(i) != "2" && 
        texto.charAt(i) != "3" && texto.charAt(i) != "4" && 
        texto.charAt(i)!= "5" && texto.charAt(i) != "6" && 
        texto.charAt(i) != "7" && texto.charAt(i)!= "8" && 
        texto.charAt(i) != "9" && texto.charAt(i)!= "-")
        {
          return false;
        }
    }
    return true;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
var submitcount=0;

function conta() {
   if (submitcount == 0)
      {
      submitcount++;
      return true;
      }
   else 
      {
      alert("Los datos del formulario se están procesando..");
      return false;
      }
   }
function agregarcarro(id,cant)
{
	if(document.enviacarro.cantidad.value == "")
	{
	alert("Ingrese Cantidad")
	document.enviacarro.cantidad.focus()
	}
	else if(isNaN(document.enviacarro.cantidad.value))
	{
	alert("Ingrese Solo Numeros")
	document.enviacarro.cantidad.focus()
	}
	else if(document.enviacarro.cantidad.value > cant)
	{
	alert("La Cantiadad Ingreseda supera el máximo que existe en stock que es:\n "+cant)
	document.enviacarro.cantidad.focus()
	}
	else
	{
	document.enviacarro.Id_Producto.value = id
	document.enviacarro.submit()
	}
}

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

function agregarcarros(id)
{
document.enviacarro.Id_Producto.value = id
document.enviacarro.submit()
}
function agregarlista(id)
{
document.enviacarro.Id_Producto.value = id
document.enviacarro.action = 'agregar_lista_deseos.asp';
document.enviacarro.submit()
}
function sacarlista(id)
{
document.enviacarro.Id_Producto.value = id
document.enviacarro.action = 'sacar_lista_deseos.asp';
document.enviacarro.submit()
}

function buscar(forma)
{
	forma = document.busca_top;
	/*if(forma.tipo.options[forma.tipo.selectedIndex].value == 0)
	{
        alert("Seleccione Tipo de Busqueda")
		forma.tipo.focus();
		return false
	}*/
	if(forma.txtbusca.value == "")
	{
		alert("Ingrese Texto a buscar")	
		forma.txtbusca.focus()
	}
	else
	{
		
	forma.action='busqueda.asp'	
	forma.submit()	
	}
}

function check_contacto(forma)
{
   if ( forma.nombre.value == "") {
      alert("Ingrese su nombre")
      forma.nombre.focus();
      return false
      }
    if ( forma.email.value == "") {
      alert("Ingrese su email")
      forma.email.focus();
      return false
      }
 if (validate(forma.email.value)) {
      alert("Debe contener email valido.")
	  forma.email.focus();
      return false
      }

   /*if ( forma.telefono.value == "") {
      alert("Ingrese su teleéfono")
      forma.telefono.focus();
      return false
      }
  if (!verifydigit(forma.telefono.value)) {
      alert("El teléfono debe contener números solamente.")
	  forma.telefono.focus();
      return false
      }*/
return conta()
return true
}
function check_boletin(forma)
{
	var chequeados = 0;
	with (forma)
	{
		for(i=0; i < elements.length; i++)
			if (elements[i].type == "checkbox")
				if (elements[i].checked)
					chequeados++;	
	}
	if (chequeados == 0)
		{
			alert("Debe elegir al menos una categoría de interés.");
			return false
		}
	else
	{
		return true		
	}
}
function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function cargarComunas() {
	var codRegion	= document.form2.region.value	
	if(codRegion == 0) {
		document.getElementById("comunas").innerHTML = "<select name='comuna' id='comuna' class='normal'><option value='0' >Seleccione Comuna</option></select>"

		
	} else {
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/carga_comunas.asp?Id_Region="+codRegion+"&tipo=1&blo=0", true);
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1) {

				combo = document.getElementById("comuna");
				combo.length = 0;
				var nuevaOpcion = document.createElement("option"); 
				nuevaOpcion.value = 0; 
				nuevaOpcion.innerHTML = "<img src='../img_tienda/ajax-loader.gif'>";
				combo.appendChild(nuevaOpcion); 
				combo.disabled = true;	
			}			
			if (ajax.readyState==4){ 
				results= unescape(ajax.responseText);
				cadenafinal=results.replace(/\+/gi," ");
				document.getElementById("comunas").innerHTML = cadenafinal;
			} 			
		}		
		ajax.send(null);
	}
}



function cargarComunas2() {
	var codRegion	= document.form2.region_envio.value	
	if(codRegion == 0) {
		document.getElementById("comunas_envio").innerHTML = "<select name='comuna_envio' id='comuna_envio' class='normal'><option value='0' >Seleccione Comuna</option></select>"

		
	} else {
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/carga_comunas.asp?Id_Region="+codRegion+"&tipo=2&blo=0", true);
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1) {

				combo = document.getElementById("comuna_envio");
				combo.length = 0;
				var nuevaOpcion = document.createElement("option"); 
				nuevaOpcion.value = 0; 
				nuevaOpcion.innerHTML = "<img src='../img_tienda/ajax-loader.gif'>";
				combo.appendChild(nuevaOpcion); 
				combo.disabled = true;	
			}			
			if (ajax.readyState==4){ 
				document.getElementById("comunas_envio").innerHTML = ajax.responseText;
			} 			
		}		
		ajax.send(null);
	}
}
function cargarComunas_mod() {
	var codRegion	= document.form2.region.value	
	if(codRegion == 0) {
		document.getElementById("comunas").innerHTML = "<select name='comuna' id='comuna' class='normal'><option value='0' >Seleccione Comuna</option></select>"

		
	} else {
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/carga_comunas.asp?Id_Region="+codRegion+"&tipo=1&blo=1", true);
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1) {

				combo = document.getElementById("comuna");
				combo.length = 0;
				var nuevaOpcion = document.createElement("option"); 
				nuevaOpcion.value = 0; 
				nuevaOpcion.innerHTML = "<img src='../img_tienda/ajax-loader.gif'>";
				combo.appendChild(nuevaOpcion); 
				combo.disabled = true;	
			}			
			if (ajax.readyState==4){ 
				results= unescape(ajax.responseText);
				cadenafinal=results.replace(/\+/gi," ");
				document.getElementById("comunas").innerHTML = cadenafinal;
			} 			
		}		
		ajax.send(null);
	}
}
function cargarComunas_edi(comu,comu2) {
	var codRegion	= document.form2.region.value	
	if(codRegion == 0) {
		document.getElementById("comunas").innerHTML = "<select name='comuna' id='comuna' class='normal'><option value='0' >Seleccione Comuna</option></select>"

		
	} else {
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/carga_comunas2.asp?Id_Region="+codRegion+"&comu="+comu+"&tipo=1&blo=0", true);
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1) {

				combo = document.getElementById("comuna");
				combo.length = 0;
				var nuevaOpcion = document.createElement("option"); 
				nuevaOpcion.value = 0; 
				nuevaOpcion.innerHTML = "<img src='../img_tienda/ajax-loader.gif'>";
				combo.appendChild(nuevaOpcion); 
				combo.disabled = true;	
			}			
			if (ajax.readyState==4){ 
				results= unescape(ajax.responseText);
				cadenafinal=results.replace(/\+/gi," ");
				document.getElementById("comunas").innerHTML = cadenafinal;
				cargarComunas_edi2(comu2)
			} 			
		}		
		ajax.send(null);
	}
}



function cargarComunas_edi2(comu) {
	var codRegion2	= document.form2.region_envio.value	
	if(codRegion2 == 0) {
		document.getElementById("comunas_envio").innerHTML = "<select name='comuna_envio' id='comuna_envio' class='normal'><option value='0' >Seleccione Comuna</option></select>"

		
	} else {
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/carga_comunas_envio2.asp?Id_Region="+codRegion2+"&comu="+comu+"&tipo=2&blo=0", true);
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1) {

				combo = document.getElementById("comuna_envio");
				combo.length = 0;
				var nuevaOpcion = document.createElement("option"); 
				nuevaOpcion.value = 0; 
				nuevaOpcion.innerHTML = "<img src='../img_tienda/ajax-loader.gif'>";
				combo.appendChild(nuevaOpcion); 
				combo.disabled = true;	
			}			
			if (ajax.readyState==4){ 
				results= unescape(ajax.responseText);
				cadenafinal=results.replace(/\+/gi," ");
				document.getElementById("comunas_envio").innerHTML = cadenafinal;
				
				
			} 			
		}		
		ajax.send(null);
	}
}
function cargarComunas_edi3(comu) {
	var codRegion2	= document.form2.region_envio.value	
	if(codRegion2 == 0) {
		document.getElementById("comunas_envio").innerHTML = "<select name='comuna_envio' id='comuna_envio' class='normal'><option value='0' >Seleccione Comuna</option></select>"

		
	} else {
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/carga_comunas_envio2.asp?Id_Region="+codRegion2+"&comu="+comu+"&tipo=2&blo=1", true);
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1) {

				combo = document.getElementById("comuna_envio");
				combo.length = 0;
				var nuevaOpcion = document.createElement("option"); 
				nuevaOpcion.value = 0; 
				nuevaOpcion.innerHTML = "<img src='../img_tienda/ajax-loader.gif'>";
				combo.appendChild(nuevaOpcion); 
				combo.disabled = true;	
			}			
			if (ajax.readyState==4){ 
				results= unescape(ajax.responseText);
				cadenafinal=results.replace(/\+/gi," ");
				document.getElementById("comunas_envio").innerHTML = cadenafinal;
				
				
			} 			
		}		
		ajax.send(null);
	}
}
function calcular_envio(total) {
	var subtotal	= document.form2.GrandTotal.value
	var Servicio_Envio = document.form2.Serv.value
	var region = document.form2.region.value
	var GrandTotal1 = document.form2.GrandTotal1.value
	if(Servicio_Envio !="")
	{
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/costo_envio.asp?total="+total+"&Servicio_Envio="+Servicio_Envio+"&region="+region+"&subtotal="+subtotal, true);
		//alert("../includes/costo_envio.asp?total="+total+"&Servicio_Envio="+Servicio_Envio+"&region="+region)
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1) 
			{
				document.getElementById("costo").innerHTML = "<img src='../img_tienda/ajax-loader.gif'>";
				document.getElementById("estado_ok").value = 0;
				
			}			
			else if (ajax.readyState==4)
			{ 
				results= ajax.responseText.split(",");
				if (results[0] == 0)
				{
				document.getElementById("mensaje").innerHTML = "&nbsp;Costo de envío a definir";
				}
				document.getElementById("costo").innerHTML = results[0];
				document.getElementById("CostoEnvio").value = results[2];				
				document.getElementById("GrandTotal1").value = results[1];
				document.getElementById("estado_ok").value = 1;
			} 	
	}
	ajax.send(null);	
		
		
	}
}
function verifica_email() {
		
		var Email	= document.form2.Email.value
		if(Email !="")
		{
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/verifica_email.asp?Email="+Email, true);
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1)
			{
				document.getElementById("ok_").innerHTML="<img src='../img_tienda/ajax-loader.gif'/>";
				document.getElementById('Email').style.display="none";
				document.getElementById("imageField2").style.display="none";
		
				//document.getElementById("Email").value = "Verificando ...";
				//document.getElementById("imageField2").style.display="none"
			}			
			else if (ajax.readyState==4)
			{ 
				results = ajax.responseText.split(",");
				if(results[0]==1)
				{
				existe_email()
				document.getElementById('Email').style.display="block";
				document.getElementById('Email').value = ""
				document.getElementById('Email').focus()
				document.getElementById("imageField2").style.display="block";
				document.getElementById("ok_").innerHTML="";
	 			}
				else
				{
				document.getElementById('Email').style.display="block";
				document.getElementById('Email').value = results;
				document.getElementById("imageField2").style.display="block";
				document.getElementById("ok_").innerHTML="<img src='../img_tienda/ico_ok.gif'/>";
				}
				
			} 			
		}		
		ajax.send(null);
		}
	
}
function replace(texto,s1,s2){
	return texto.split(s1).join(s2);
}
function verifica_cod() {
		var cod	= document.form_cod.cod.value	
		var GrandTotal = document.form_cod.GrandTotal.value
		var desc = document.form_cod.desc.value
		var promo = document.form_cod.promo.value
		
		var GrandTotal_us = document.form_cod.GrandTotal_us.value
		var desc_us = document.form_cod.descu_us.value
		var promo_us = document.form_cod.promo_us.value
		
		
		if(cod =="")
		{
			document.getElementById('cod').value = "";
			document.getElementById('cod_2').value = "";
			document.getElementById("costo").innerHTML = "";
			document.getElementById("GrandTotal1").value = GrandTotal;
			document.getElementById("GrandTotal1_us").value = GrandTotal_us;
		}
		else
		{
		ajax=nuevoAjax();
		ajax.open("GET", "../includes/verifica_cod.asp?cod="+cod+"&GrandTotal="+GrandTotal+"&desc="+desc+"&promo="+promo+"&GrandTotal_us="+GrandTotal_us+"&desc_us="+desc_us+"&promo_us="+promo_us, true);
	//prompt("../includes/verifica_cod.asp?cod="+cod+"&GrandTotal="+GrandTotal+"&desc="+desc+"&promo="+promo+"&GrandTotal_us="+GrandTotal_us+"&desc_us="+desc_us+"&promo_us="+promo_us)
		ajax.onreadystatechange=function() { 
			if (ajax.readyState==1)
			{
				document.getElementById('pasar').value = 1;
				document.getElementById("cod").value = "Verificando ...";
				document.getElementById("oculta_boton").style.visibility="hidden"; 
				document.getElementById("oculta_boton").style.display="none";				
			}			
			else if (ajax.readyState==4)
			{ 
				results = ajax.responseText.split(",");
				if(results[0]==1)
				{
				document.getElementById('cod').value = "";
				document.getElementById('cod_2').value = "";
				document.getElementById("costo").innerHTML = "";
				document.getElementById("costo_us").innerHTML = "";
				document.getElementById("GrandTotal1").value = results[1];
				document.getElementById("GrandTotal1_us").value = replace(results[2],"~",",");
				document.getElementById("oculta_boton").style.visibility="visible"; 
				document.getElementById("oculta_boton").style.display="block";
				document.getElementById('pasar').value = 0;
				existe_cod()
	 			}
				else
				{
				document.getElementById('cod').value = results[0];
				document.getElementById('cod_2').value = results[0];
				document.getElementById("costo").innerHTML = results[1];
				document.getElementById("costo_us").innerHTML = replace(results[3],"~",",");
				document.getElementById("GrandTotal1").value = results[2];
				document.getElementById("GrandTotal1_us").value = replace(results[4],"~",",");
				document.getElementById("oculta_boton").style.visibility="visible"; 
				document.getElementById("oculta_boton").style.display="block"
				document.getElementById('pasar').value = 0;
				//document.getElementById("GrandTotal").value = results[3];
				}
				
			} 			
		}		
		ajax.send(null);
	}
	
}
function cheq_login( forma )
{
    if ( forma.Email.value == "") {
      alert("Ingrese su email")
      forma.Email.focus();
      return false
      }
 if (validate(forma.Email.value)) {
      alert("Debe contener email valido.")
	  forma.Email.focus();
      return false
      }

   if ( forma.Clave.value == "") {
      alert("Ingrese su clave")
      forma.Clave.focus();
      return false
      }

return true
}
function ver_estado()
{
	if(document.form_cod.pasar.value == 0)
	{
	document.login.submit()
	}
	else
	{
	alert("La pagina esta verificando el codigo")
	}
}
function existe_cod()
{
alert("El código ingresado es incorrecto o ya fue utilizado")	
}
function existe_email()
{
alert("El mail ingresado ya existe en nuestra base de datos")	
}
function ir(num)
{
	if(num == 1)
	{
	document.location.href="http://www.rileditores.com/tienda/"	
	}
}

