// JavaScript Document

var site_url = 'http://www.minitrapitos.com/';

function LoadModule(module,pars) {

	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/'+module+'/list.php?ms='+ rand + '&' +pars;
	
	new Ajax.Request(url,{
		method: 'post',
		onLoading: $('mainbar').innerHTML = '<table width=\"100%\" height=\"400\"><tr><td align=\"center\" valign=\"middle\"><img class=\"loading\" src=\"img/loading.gif\" /></td></tr></table>',
		onComplete: function(originalRequest) {
			$('mainbar').innerHTML = originalRequest.responseText;
		}
	});

}



function ShowSection(originalRequest) {

	$('section').innerHTML = originalRequest.responseText;

}

function LoadSection(module,section,parms) {

	var rand = Math.round(Math.random()*1000000000);
	
	var url = '../modules/' + module + '/' + section + '.php?ms='+ rand + '&' + parms;
	
	var opt = {
	
		method: 'get',
		onComplete: ShowSection,
		onSuccess: $('section').innerHTML = "<table width=\"100%\" height=\"200\"><tr><td align=\"center\" valign=\"middle\"><img class=\"loading\" src=\"img/loading.gif\" /></td></tr></table>"
		
	};
	
	new Ajax.Request(url,opt);
}


function updateBanner(field,id,value) {

	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/banner/update.php?rand='+ rand + '&name='+field+'&id='+id+'&value='+value;
	
	var opt = {
	
		method: 'get',
		onLoading: $('section')
		
	};
	
	new Ajax.Request(url,opt);

}

function updateFieldBanner(field,id,name) {

	var num = $(name).value.length;
	var value = $(name).value;
	
	switch (field) {
	
		case 'referencia':
		
			if ( num > 0 ) {
				
				updateBanner(field,id,value);
				
			} else {
			
				alert('El campo no puede estar vacío');
			
			}
			break;
			
		case 'link':
		
			if ( num > 0 ) {
				
				updateBanner(field,id,value);
				
			} else {
			
				alert('El campo no puede estar vacío');
			
			}
			break;
	
	}

}


function updateFieldProducto(field,id,name) {

	var num = $(name).value.length;
	var value = $(name).value;

	switch (field) {
	
		case 'nombre':

			if ( num > 1 ) {
				updateProducto(field,id,value);
			} else {
				alert('El campo no puede estar vacío.');
			}
			break;
			
		case 'descripcion':
		
			if ( num > 1 ) {
				updateProducto(field,id,value);
			} else {
				alert('El campo no puede estar vacío.');
			}
			break;
			
		case 'caracteristicas':
		
			updateProducto(field,id,value);
			break;
			
		case 'bimp':
		
			if ( value > 0.01 && value < 9999.99 ) {
				updateProducto(field,id,value);
			} else {
				alert('El campo no puede estar vacío.');
			}
			break;
			
		case 'id_iva':
		
			updateProducto(field,id,value);
			break;
			
		case 'oferta':
		
			if ( num > 1 ) {
				if ( value > 0.01 && value < 9999.99 ) {
					updateProducto(field,id,value);
				} else {
					alert('El campo no puede estar vacío.');
				}
			} else {
				updateProducto(field,id,0);
			}
			break;
			
		case 'imagen':
		
			updateProducto(field,id,value);
			break;
			
		case 'categoria':
		
			break;
	
	}

}


function updateProducto(field,id,value) {
	
	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/producto/update.php?rand='+rand+'&field='+field+'&id='+id+'&value='+value;
	
	var opt = {
	
		method: 'get',
		onLoading: $('section')
		
	};
	
	new Ajax.Request(url,opt);

}


function UpdateField(module,name,field,id) {

	if (field == 'nombre') {
	
		var num = $(name).value.length;
		var value = $(name).value;
	
		if ( num > 1 ) {
	
			Update(module,field,value,id);
			
		} else {
			
			alert('El campo no puede estar vacío.');
			
		}
	
	} else {
	
		var value = $(name).value;
		Update(module,field,value,id);
	
	}

}


function Update(module,field,value,id) {

	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/' + module + '/update.php?rand='+rand+'&field='+field+'&id='+id+'&value='+value;
	
	var opt = {
	
		method: 'get',
		onLoading: $('section')
		
	};
	
	new Ajax.Request(url,opt);

}


function checkUpdate(originalRequest) {

	alert(originalRequest.responseText);

}




function procesar(form,module,Select,selectName) {

	//obtener valores del select
	if (Select && selectName) {
	
		var newSelect = $(Select);
		var opt = getOpt(newSelect);
	
	}

	var query = '';
	var i = 0;
	
	for ( i=0 ; i < form.length ; i++ ) {
		
		
	
		query += form.elements[i].name + '=' + form.elements[i].value + '&';
	
	}
	
	if (Select && selectName) {
	
		query += selectName + '=' + opt;
	
	}
	
	//alert(query);
	
	LoadSection(module,'add',query);

}


function procesarSection(form,module,section) {

	var query = '';
	var i = 0;
	
	for ( i=0 ; i < form.length ; i++ ) {
		
		
	
		query += form.elements[i].name + '=' + form.elements[i].value + '&';
	
	}
	
	LoadSection(module,section,query);

}

function addOption() {

	var SelectIn = $('optCateg1');
	var inIndex = SelectIn.selectedIndex;
	var inOptions = SelectIn.options;
	
	var SelectOut = $('optCateg2');
	var outOptions = SelectOut.options;
	var outLength = outOptions.length;
	
	if ( inIndex > -1 ) {
		
		outOptions[outLength] = new Option(inOptions[inIndex].text);
		outOptions[outLength].value = inOptions[inIndex].value;
		//Eliminar
		inOptions[inIndex] = null;
		
	}
	
	inOptions[0].selected = true;
	
}


function delOption() {

	var SelectIn = $('optCateg2');
	var inIndex = SelectIn.selectedIndex;
	var inOptions = SelectIn.options;
	
	var SelectOut = $('optCateg1');
	var outOptions = SelectOut.options;
	var outLength = outOptions.length;
	
	if ( inIndex > -1 ) {
		
		outOptions[outLength] = new Option(inOptions[inIndex].text);
		outOptions[outLength].value = inOptions[inIndex].value;
		//Eliminar
		inOptions[inIndex] = null;
		
	}
	
	inOptions[inIndex].selected = true;

}


function getOpt(Select) {
	
	var Select = $(Select);
	var num = Select.length;
	var opt = '';
	var i = 0;

	for (i=0;i<num;i++) {
		
		opt += Select.options[i].value + ',';
		
	}
	
	return opt;
	
}


function addImage(num,action) {

	if ( num > 0 ) {
	
		var popup = open(site_url+"include/upload_img/addimg.php?mod="+num+"&action=mod","img", "toolbar=no,directories=no,menubar=no,status=no,height=350,width=350");
		popup.focus();
		
	} else {
	
		var popup = open(site_url+"include/upload_img/addimg.php","img", "toolbar=no,directories=no,menubar=no,status=no,height=350,width=350");
		popup.focus();
	
	}

}


function addBanner() {

	var popup = open(site_url+"modules/banner/upimg.php","banner", "toolbar=no,directories=no,menubar=no,status=no,height=350,width=450");
	popup.focus();

}


function closeWin(value) {
	
	if ( value == 0 ) {
	
		window.close(this);
	
	} else {
		
		opener.document.frmAdd.image.value = value;
    	window.close(this);
	
	}
	
	
}




function modCantidad(line) {

	//Obtener cantidad
	var valor = $(line).value;

	if ( valor < 0 ) {
		
		alert('La cantidad de productos no puede ser negativa.');
	
	} else {
	
		location.href = 'carrito.php?action=mod&id='+line+'&num='+valor;
		
	}

}

//FORMULARIOS

function frm_datos_pedido() {
	
	var ok = true;
	
	//nombre
	if ( $('nombre').value == '' ) {
	
		alert('El campo "nombre" no puede estar vacio.');
		
		ok = false;
	
	}
	
	//apellidos
	if ( $('apellidos').value == '' ) {
	
		alert('El campo "apellidos" no puede estar vacio.');
		
		ok = false;
	
	}
	
	//Direccion
	if ( $('direccion').value == '' ) {
	
		alert('El campo "Direccion" no puede estar vacio.');
		
		ok = false;
	
	}
	
	if ( $('poblacion').value == '' ) {
	
		alert('El campo "Poblacion" no puede estar vacio.');
		
		ok = false;
	
	}
	
	if ( $('provincia').value == '' ) {
	
		alert('El campo "Provincia" no puede estar vacio.');
		
		ok = false;
	
	}
	
	if ( $('cp').value < 1 || $('cp').value > 99999 ) {
	
		alert('El campo "C.P. " no es valido.');
		
		ok = false;
	
	}

	if ( $('pais').value == '' ) {
	
		alert('El campo "pais" no puede estar vacio.');
		
		ok = false;
	
	}
	
	if ( $('telefono').value < 600000000 || $('telefono').value > 999999999 ) {
	
		alert('El campo "Telefono" no es valido.');
		
		ok = false;
	
	}


	var telefono = $('telefono').value;
	
	
	if ( ok ) {
		
		document.form.submit();

	}

}



//Comprar sin ir al carrito

function addProducto(id) {

	var rand = Math.round(Math.random() * 100000000000); 

	var url = 'carrito.php?action=add&rand='+rand+'&id='+id;
	
	var opt = {
	
		method: 'get',
		onComplete: alert("Producto añadido al 'Carrito'.")
		
	};
	
	new Ajax.Request(url,opt);

}


function back_datos() {

	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/tramite/back_datos.php?rand='+rand;
	
		var opt = {
	
		method: 'post',
		onComplete: location.href='tramitar_datos.php'
		
	};
	
	new Ajax.Request(url,opt);

}


function destacar(checkbox,id) {

	if ( checkbox.checked ) {
		
		var des = 'checked';
		
	} else {
		
		var des = 'nochecked';
	
	}

	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/producto/destacar.php?rand='+rand+'&des='+des+'&id='+id;
	
		var opt = {
	
		method: 'get'
		
	};
	
	new Ajax.Request(url,opt);

}

function enviado(checkbox,id) {

	if ( checkbox.checked ) {
		
		var des = 'checked';
		
	} else {
		
		var des = 'nochecked';
	
	}

	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/pedido/enviado.php?rand='+rand+'&des='+des+'&id='+id;
	
		var opt = {
	
		method: 'get'
	};
	
	new Ajax.Request(url,opt);

}

function updateOferta(id) {

	var popup = window.open("../modules/producto/up_oferta.php?id="+id,"Actualizar_Oferta","toolbar=no,directories=no,menubar=no,status=no,height=280,width=320");
	
	popup.focus();

}


function updateCategorias(id) {

	//Obtener categorias
	var newSelect = $('optCateg2');
	var cats = getOpt(newSelect);
	
	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/producto/up_cat.php?rand='+rand+'&cats='+cats+'&id='+id;
	
		var opt = {
	
		method: 'get',
		onLoading: $('section')
		
	};
	
	new Ajax.Request(url,opt);

}




function updateFieldCliente(name,id,field) {
	
	var value = $(field).value;
	var length = $(field).value.length;
	
	switch (name) {
		
		case 'nombre':
		
			if ( length > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
		
		case 'apellidos':
			
			if ( length > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
			
		case 'fecha_nac':
		
			if ( length > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
			
		case 'empresa':
		
			updateCliente(id,name,value);
			break;
			
		case 'cif':
		
			if ( lenght == 9 ) {
				updateCliente(id,name,value);
			} else {
				alert('Valor incorrecto. Vuelva a introducirlo.');
			}
			break;
		
		case 'direccion':
		
			if ( length > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
			
		case 'cp':
		
			if ( value > 0 && value < 99999 ) {
				updateCliente(id,name,value);	
			} else {
				alert('Valor incorrecto. Vuelva a introducirlo.');
			}
			break;
			
		case 'poblacion':
		
			if ( length > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
			
		case 'provincia':
		
			if ( length > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
			
		case 'pais':
		
			if ( length > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
			
		case 'telefono':
		
			if ( valor > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
			
		case 'fax':
		
			if ( valor > 0 ) {
				updateCliente(id,name,value);
			} else {
				alert('El campo no puede estar vacio');
			}
			break;
		
	}

}


function actCliente(id) {
	
	var rand = Math.round(Math.random()*1000000000);
	
	var url = '../modules/cliente/update.php?rand='+rand+'&name=baja&id='+id+'&value=1';
	
	var opt = {
	
		method: 'get',
		onComplete: setTimeout("LoadModule('cliente')",500)
		
	};
	
	new Ajax.Request(url,opt);
	
}

function desCliente(id) {
	
	var rand = Math.round(Math.random()*1000000000);
	
	var url = '../modules/cliente/update.php?rand='+rand+'&name=baja&id='+id+'&value=0';
	
	var opt = {
	
		method: 'get',
		onComplete: setTimeout("LoadModule('cliente')",500)
		
	};
	
	new Ajax.Request(url,opt);
	
}


function updateCliente(id,name,value) {
	
	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/cliente/update.php?rand='+rand+'&name='+name+'&id='+id+'&value='+value;
	
	var opt = {
	
		method: 'get'
		
	};
	
	new Ajax.Request(url,opt);

}


function showPedido(id) {
	
	var popup = open(site_url+"modules/pedido/show.php?id="+id,"", "toolbar=no,directories=no,menubar=no,status=no,height=700,width=500");
	popup.focus();
	
}

function showPedido2(id,fpago,total) {
	
	var popup = open(site_url+"modules/pedido/show2.php?id="+id+"&fpago="+fpago+"&total="+total,"", "toolbar=no,directories=no,menubar=no,status=no,height=700,width=500");
	popup.focus();
	
}

function actBanner(id) {
	
	var rand = Math.round(Math.random()*1000000000);
	
	var url = '../modules/banner/update.php?rand='+rand+'&name=visible&id='+id+'&value=1';
	
	var opt = {
	
		method: 'get',
		onComplete: setTimeout("LoadModule('banner')",500)
		
	};
	
	new Ajax.Request(url,opt);
	
}

function desBanner(id) {
	
	var rand = Math.round(Math.random()*1000000000);
	
	var url = '../modules/banner/update.php?rand='+rand+'&name=visible&id='+id+'&value=0';
	
	var opt = {
	
		method: 'get',
		onComplete: setTimeout("LoadModule('banner')",500)
		
	};
	
	new Ajax.Request(url,opt);
	
}


function LoadGaleria(id) {
	
	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/galeria/list.php?rand='+rand+'&id='+id;
	
	var opt = {
	
		method: 'get',
		onComplete: ShowGaleria,
		onSuccess: $('mainbar').innerHTML = "<table width=\"100%\" height=\"300\"><tr><td align=\"center\" valign=\"middle\"><img class=\"loading\" src=\"img/loading.gif\" /></td></tr></table>"
		
	};
	
	new Ajax.Request(url,opt);

}

function ShowGaleria(originalRequest) {

	$('mainbar').innerHTML = originalRequest.responseText;

}


function addImgGaleria(id) {

	var popup = open(site_url+"modules/galeria/add.php?id="+id,"galeria", "toolbar=no,directories=no,menubar=no,status=no,height=350,width=300");
	popup.focus();

}



function mod_desc(desc,id) {

	var rand = Math.round(Math.random()*1000000000);

	var url = '../modules/galeria/mod.php?rand='+rand+'&id='+id+'&text='+desc;
	
	var opt = {
	
		method: 'get'
		
	};
	
	new Ajax.Request(url,opt);

}


function showError(originalRequest) {

	alert(originalRequest.responseText);

}


function Observaciones(id) {

	var popup = open(site_url+"modules/pedido/observ.php?id="+id,"observaciones", "toolbar=no,directories=no,menubar=no,status=no,height=350,width=350");
	popup.focus();

}

function Enviar(id) {

	var popup = open(site_url+"modules/pedido/send.php?id="+id,"observaciones", "toolbar=no,directories=no,menubar=no,status=no,height=100,width=250");
	popup.focus();

}


function filtrar(element,pars) {

	var index = element.selectedIndex;
	var id = element.options[index].value;
	
	LoadModule('producto',pars+'&categoria='+id);

}

function filtrar2(element,pars) {

	var index = element.selectedIndex;
	var id = element.options[index].value;
	
	LoadModule('producto',pars+'&'+id+'='+id);

}


function actFpago(id) {
	
	var rand = Math.round(Math.random()*1000000000);
	
	var url = '../modules/configuracion/up_fpago.php?rand='+rand+'&name=activo&id='+id+'&value=1';
	
	var opt = {
	
		method: 'get',
		onComplete: setTimeout("LoadSection('configuracion','fpago','')",500)
		
	};
	
	new Ajax.Request(url,opt);
	
}

function desFpago(id) {
	
	var rand = Math.round(Math.random()*1000000000);
	
	var url = '../modules/configuracion/up_fpago.php?rand='+rand+'&name=activo&id='+id+'&value=0';
	
	var opt = {
	
		method: 'get',
		onComplete: setTimeout("LoadSection('configuracion','fpago','')",500)
		
	};
	
	new Ajax.Request(url,opt);
	
}
