function checkForm()
{
	if(!document.getElementById('check').checked)
	{
		alert('No ha aceptado las condiciones de uso');
		return false;
	}
	else
		return true;
}

function editProduct(id)
{
	document.getElementById('pid').value = id;
	document.forms[0].action = 'productedit.php';
	document.forms[0].submit();
}

function lockProduct(id)
{
		if(confirm("&iquest;Desea terminar esta promoci&oacute;n?\n(El n&uacute;mero total de unidades ser&aacute;\nigual al n&uacute;mero actual unidad vendidas)"))
		{
			document.getElementById('pid').value = id;
			document.getElementById('sublockpromo').value="1";
			document.getElementById('subrelist').value="";
			document.getElementById('subdelprod').value="";
			document.getElementById('subactivate').value="";			
			document.forms[0].action = 'process.php';
			document.forms[0].submit();
		}
}

function deleteProduct(id)
{
	if(confirm("&iquest;Desea eliminar este producto?")){
		document.getElementById('pid').value = id;
		document.getElementById('sublockpromo').value="";
		document.getElementById('subrelist').value="";
		document.getElementById('subdelprod').value="1";
		document.getElementById('subactivate').value="";
		document.forms[0].action = 'process.php';
		document.forms[0].submit();
	}
}

function relistProduct(id)
{
	if(confirm("&iquest;Desea volver a poner en venta este producto?")){
		document.getElementById('pid').value = id;
		document.getElementById('sublockpromo').value="";
		document.getElementById('subrelist').value="1";
		document.getElementById('subdelprod').value="";
		document.getElementById('subactivate').value="";
		document.forms[0].action = 'process.php';
		document.forms[0].submit();
	}
}

function activateProduct(id)
{
	document.getElementById('pid').value = id;
	document.getElementById('sublockpromo').value="";
	document.getElementById('subrelist').value="";
	document.getElementById('subdelprod').value="";
	document.getElementById('subactivate').value="1";
	document.forms[0].action = 'process.php';
	document.forms[0].submit();
}

function deleteOrder(t, a, e, m)
{
	if(m=='s')
		var n = e.parentNode.parentNode.parentNode.firstChild.nextSibling.firstChild.innerHTML;
	else
		var n = e.parentNode.parentNode.parentNode.firstChild.firstChild.innerHTML;	

	if(confirm('&iquest;Deseas darte de baja de la oferta "' + n + '"?\n\nRecuerda que luego no te podr&aacute;s volver a apuntar a esta oferta para dar oportunidad a otros usuarios'))
	{
		document.getElementById('pid').value = t;
		document.getElementById('amount').value = a;
		document.getElementById('subcancelpromo').value = 1;
		document.getElementById('substatus').value = "";
		document.forms[0].submit();
	}
}

function banUser(i, n) {
	if(confirm('&iquest;Desea bloquear/desbloquear al usuario "' + n + '"?')){
		document.getElementById('uid').value = i;
		document.getElementById('subbanuser').value = "1";
		document.getElementById('subdeluser').value = "";
		document.forms[0].submit();
	}
}

function deleteUser(i, n) {
	if(confirm('&iquest;Desea borrar el usuario "' + n + '"?')){
		document.getElementById('uid').value = i;
		document.getElementById('subbanuser').value = "";
		document.getElementById('subdeluser').value = "1";
		document.forms[0].submit();
	}
}

function setStatus(s, i) {

	var o = s.parentNode.parentNode.firstChild;

	document.getElementById('pid').value = i;
	document.getElementById('stval').value = o.options[o.selectedIndex].value;
	document.getElementById('amount').value = 0;
	document.getElementById('subcancelpromo').value = '';
	document.getElementById('substatus').value = "1";
	document.forms[0].submit();
	
	//var n = i.parentNode.parentNode.firstChild.value;
	//alert(n);
}

function selectTab(t) {
	if(t.id == 't1') {
		t.src = 'images/t_oferta.jpg';
		document.getElementById('t2').src='images/t_caracteristicas_o.jpg';
	}
	else if(t.id == 't2') {
		t.src = 'images/t_caracteristicas.jpg';
		document.getElementById('t1').src='images/t_oferta_o.jpg';
	}
}

function alertMe(t) {
	document.getElementById('pid').value = t;
	document.forms[0].submit();
}

function filterOrders() {
	var p = document.getElementById('sprod');
	var u = document.getElementById('suser');
	var s = '';
	
	if(p.options[p.selectedIndex].text != '') {
		s += 'p=' + encode64(p.options[p.selectedIndex].text);
	}
	if(u.options[u.selectedIndex].text != '') {
		if(s != '') {
			s += '&';
		}
		s += 'u=' + encode64(u.options[u.selectedIndex].text);
	}
	
	document.location = 'orderlist.php?' + s;
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);

		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;

		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}

		output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
   }
   
   return output.toString();
}

function decode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	while (i < input.length) {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

		output.append(String.fromCharCode(chr1));

		if (enc3 != 64) {
			output.append(String.fromCharCode(chr2));
		}
		if (enc4 != 64) {
			output.append(String.fromCharCode(chr3));
		}
	}

	return output.toString();
}

var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf(" chrome/") >= 0 || ua.indexOf(" firefox/") >= 0 || ua.indexOf(' gecko/') >= 0) {
	var StringMaker = function () {
		this.str = "";
		this.length = 0;
		this.append = function (s) {
			this.str += s;
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.str = s + this.str;
			this.length += s.length;
		}
		this.toString = function () {
			return this.str;
		}
	}
} else {
	var StringMaker = function () {
		this.parts = [];
		this.length = 0;
		this.append = function (s) {
			this.parts.push(s);
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.parts.unshift(s);
			this.length += s.length;
		}
		this.toString = function () {
			return this.parts.join('');
		}
	}
}

function validatePM()
{
	var x1 = document.getElementById('cTPV');
	var x2 = document.getElementById('cPP');
	
	alert(x1.checked);
	return false;
}

function openCloseLayer(whichLayer, action)
{
 	var style = getE(whichLayer).style;
	if (!action)
		style.display = style.display == 'none' ? 'block' : 'none';
	else if (action == 'open')
		style.display = 'block';
	else if (action == 'close')
		style.display = 'none';
}

function getE(name)
{
	if (document.getElementById)
		var elem = document.getElementById(name);
	else if (document.all)
		var elem = document.all[name];
	else if (document.layers)
		var elem = document.layers[name];
	return elem;
}

function toggleLayer(whichLayer, flag)
{
	var style = getE(whichLayer).style;
	style.display = (flag == '') ? 'none' : 'block';
}

function checkDelBoxes(pForm, boxName, parent)
{
	for (i = 0; i < pForm.elements.length; i++)
		if (pForm.elements[i].name == boxName)
			pForm.elements[i].checked = parent;
}

