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];}
}

// Rutinas AJAX
function call_ajax(url,tag,formulario)
{
	if (formulario!=""){
		argumentos = $('#'+formulario).formSerialize(); 
	} else {
		argumentos="";
	}

	$('#'+tag).load(url, argumentos);	
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function add_carro(url,id,num,tag)
{
	// Obtenemos el id del color	
	color = eval("document.listado.color"+id);
	idcolor = getCheckedValue(color);

	// Obtenemos los ids de las tallas	
	notalla=true;
	i=0;
	tallas="";
	while (i<num) {
		if (eval("document.listado.talla"+id+"_"+i).value!=""){
			notalla=false;
			tallas+= eval("document.listado.talla"+id+"_"+i).value
		} else {
			tallas+=0;
		}
		i++;
		if (i!=num){
			tallas += ';';
		}
	}

	vacio=false;
	error="";
	if (idcolor==""){
		error = "Debe seleccionar un color.\n";
		vacio=true;
	}
	if (notalla==true){
		error += "Debe seleccionar alguna talla.\n";
		tallas = "";
		vacio=true;
	}

	if (vacio==true){
		alert(error);
	} else {
		ruta = url+'&color='+idcolor+'&tallas='+tallas;
		$('#'+tag).load(ruta);
		alert("Producto añadido");
		// Inicializo los campos
		setCheckedValue(eval("document.listado.color"+id), '0');
		j=0;
		while (j<num) {
			eval("document.listado.talla"+id+"_"+j).value="";
			j++;
		}
	}
	
}

function add_pedido(url,id,num,tag)
{
	// Obtenemos el id del color	
	color = eval("document.listado.color"+id);
	idcolor = getCheckedValue(color);

	// Obtenemos los ids de las tallas	
	notalla=true;
	i=0;
	tallas="";
	while (i<num) {
		if (eval("document.listado.talla"+id+"_"+i).value!=""){
			notalla=false;
			tallas+= eval("document.listado.talla"+id+"_"+i).value
		} else {
			tallas+=0;
		}
		i++;
		if (i!=num){
			tallas += ';';
		}
	}

	vacio=false;
	error="";
	if (idcolor==""){
		error = "Debe seleccionar un color.\n";
		vacio=true;
	}
	if (notalla==true){
		error += "Debe seleccionar alguna talla.\n";
		tallas = "";
		vacio=true;
	}

	if (vacio==true){
		alert(error);
	} else {
		ruta = url+'&color='+idcolor+'&tallas='+tallas;
		$('#'+tag).load(ruta);
		alert("Producto añadido");
		// Inicializo los campos
		setCheckedValue(eval("document.listado.color"+id), '0');
		j=0;
		while (j<num) {
			eval("document.listado.talla"+id+"_"+j).value="";
			j++;
		}
	}
	
}

function add_regalaacosta(url,id,tag)
{
	// Obtenemos el id del color	
	color = eval("document.listado.color"+id);
	idcolor = getCheckedValue(color);

	// Obtenemos el id de la talla	
	idtalla = eval("document.listado.talla"+id).value;

	// Obtenemos el numero de unidades	
	elemento="unidades"+id;	
	num_unidades = document.getElementById(elemento).value;

	vacio=false;
	error="";
	if (idcolor==""){
		error = "Debe seleccionar un color.\n";
		vacio=true;
	}
	if (idtalla==0){
		error += "Debe seleccionar una talla.\n";
		vacio=true;
	}
	if (num_unidades==""){
		error += "Debe elegir el número de unidades.";
		vacio=true;
	}

	if (vacio==true){
		alert(error);
	} else {
		ruta = url+'&unidades='+num_unidades+'&color='+idcolor+'&talla='+idtalla;
		$('#'+tag).load(ruta);
		alert("Producto añadido");
		setCheckedValue(eval("document.listado.color"+id), '0');
		eval("document.listado.talla"+id).value=0;
		document.getElementById(elemento).value="";
	}
	
}

function check_form(form,nombreform)
{
	error="";
	
	switch (nombreform) {
		case 'datosclientes' :
			fields=	new Array('razonsocial');
			texts=	new Array("Razón Social");
			break;
		case 'datoscontacto' :
			fields=	new Array('nombre','poblacion','telefono','asunto');
			texts=	new Array("Nombre","Población","Teléfono","Asunto");
			break;			
		case 'datosdistribuidor' :
			fields=	new Array('nombre','poblacion','telefono','codigopostal','asunto');
			texts=	new Array("Nombre","Población","Teléfono","Código Postal","Asunto");
			break;		
		case 'datoscv' :
			fields=	new Array('nombre','telefono','poblacion');
			texts=	new Array("Nombre","Teléfono","Población");
			break;			
		case 'datosclientes_tienda' :
			fields=	new Array('razonsocial','domicilio','codigopostal','localidad','provincia','pais');
			texts=	new Array("Nombre","Domicilio","Codigo Postal","Localidad","Provincia","Pais");
			break;			
	}

	if (nombreform=='datosclientes'){

		for (i=0;i<fields.length;i++) {
			v=eval('document.datosclientes.'+fields[i]+'.value');
			if (v==""){
				error+="Por favor, compruebe campo "+texts[i]+"\n";
			}
		}
	
		email_cliente=eval('document.datosclientes.email.value');

    	if (!email_cliente.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)){ 
	    	error+="Por favor, compruebe campo Email.\n";
	    }	
	
	}
	
	if (nombreform=='datoscontacto'){

		for (i=0;i<fields.length;i++) {
			v=eval('document.datoscontacto.'+fields[i]+'.value');
			if (v==""){
				error+="Por favor, compruebe campo "+texts[i]+"\n";
			}
		}
	
		email_contacto=eval('document.datoscontacto.email.value');

    	if (!email_contacto.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)){ 
	    	error+="Por favor, compruebe campo Email.\n";
	    }	
	
	}	
	
	if (nombreform=='datosdistribuidor'){

		for (i=0;i<fields.length;i++) {
			v=eval('document.datosdistribuidor.'+fields[i]+'.value');
			if (v==""){
				error+="Por favor, compruebe campo "+texts[i]+"\n";
			}
		}
	
		email_distribuidor=eval('document.datosdistribuidor.email.value');

    	if (!email_distribuidor.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)){ 
	    	error+="Por favor, compruebe campo Email.\n";
	    }	
	}		
	
		if (nombreform=='datoscv'){

		for (i=0;i<fields.length;i++) {
			v=eval('document.datoscv.'+fields[i]+'.value');
			if (v==""){
				error+="Por favor, compruebe campo "+texts[i]+"\n";
			}
		}
	
		email_cv=eval('document.datoscv.mail.value');

    	if (!email_cv.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)){ 
	    	error+="Por favor, compruebe campo Email.\n";
	    }	
	
	}	
	
	if (nombreform=='datosclientes_tienda'){

		for (i=0;i<fields.length;i++) {
			v=eval('document.datosclientes_tienda.'+fields[i]+'.value');
			if (v==""){
				error+="Por favor, compruebe campo "+texts[i]+"\n";
			}
		}
	
		email_cliente_tienda=eval('document.datosclientes_tienda.email.value');

    	if (!email_cliente_tienda.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)){ 
	    	error+="Por favor, compruebe campo Email.\n";
	    }	
	
	}	

	if (error!=""){
		alert(error);
	}

	document.estatus = (error=="");
}