// JavaScript Document


// -- UTILITIES --//

function dsp_save(on,msg){
	
	if(on) {
		$("message").innerHTML = "";
		$("btn_save").style.display = "none";
		$("please_wait").style.display = "block";
	} else {
		$("message").innerHTML = msg;
		$("btn_save").style.display = "";
		$("please_wait").style.display = "none";
	}		
}

function swapComboItem(comboOrigen, comboDestino) {
	
	var sel1=$(comboOrigen)
	var sel2=$(comboDestino)
	
	var op = sel1.options[sel1.selectedIndex]
	
	sel1.removeChild(op)
	sel2.appendChild(op)
	
	return true
}

function comboValuesToFormField(combo, field) {

	var c = $(combo)
	var f = $(field)
	var s ='' // cadena para almacenar los valores 'v1, v2, v3, ...'
	var o // variable para for each option in select

	for (i=0; i<c.options.length; i++) {
		o=c.options[i]
		s += o.value + ','
	}
	if (s.length>0) { s=s.substr(0, s.length-1) }
	f.value=s
}

function rndP() {
	return "&rndp=" + Math.random();
}
// -- LOGIN -- //

function login() 
{
	//$("errordiv").style.visibility = "hidden";
	if ( !checkSubmit() ) { return false }

	var pars = Form.serialize("frm_login") + rndP();
	var url = "includes/login_fn.php";
	var a = new Ajax.Request(url, { 
		method: 'post'	, 
		parameters: pars, 
		onComplete: function (req) {
			//alert(req.responseText);
			var msg = JSON.parse(req.responseText);
			if(msg.msg!="") {
				alert(msg.msg);
			} else {
				document.location.replace("?");
			}			
		}
	});

}

function checkSubmit() 
{                
	if( $F('email') =="" || $F('password') == "" )  {
		alert("Please, provide both email and password");
		//$("login_message").innerHTML = "Por favor ingrese su dirección de email y clave, y luevo presione Login.";
		return false;
   }
   return true;
}

function change_password() {
	if($F("email") =="") {
		alert("Please, provide a valid email address"); return false;
		//$("login_message").innerHTML = "Por favor, ingrese una dirección de email en el campo usuario";
	} else {
		//$("login_message").innerHTML = "Por favor, espere..."
	}

	var pars = Form.serialize("frm_login");// + rndP();
	var url = "change_password.php";
	var a = new Ajax.Request(url, 
		{ method: 'post', 
		parameters: pars,
		onComplete:function(req) {
			//alert(req.responseText);
			var msg = JSON.parse(req.responseText);
			$("login_message").innerHTML = msg.message;
		}
	});		
	
}

function logout() {
	var args = {
		parameters: "rndParam="+Math.random(),
		method: "post",
		onComplete: function (req) {
			alert(req.responseText);
			document.location.replace("?");
		}
	}
	new Ajax.Request( "includes/logout_fn.php", args);		
}

// -- CASINOS -- //
function casinos_del(id, casino) {
	var msg = "This will delete this casino. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/casinos_del_fn.php";
		var pars = "id_casino="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "?page=casinos";
				 }
			 }
		 }); 		
	}
}

function casinos_save() {
	
	dsp_save(true,"");
	var url = "funciones/casinos_am_fn.php";
	if (!casinos_validate()) { return 0 };
	
	var pars = Form.serialize("frm_casinos")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							 //myLightbox.end();
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 //alert(obj.old);
								 if(obj.old==0) {
									document.location.href ="?page=casinoam&id=" + obj.neu;
								 } else {
								 	dsp_save(false, "Record Saved");
								 }

							 }
						 }
					 }); 
}

function casinos_validate() {
	return true;
}

// -- ADMINS -- //
function affiliates_del(id, admin) {
	var msg = "This will delete this admin. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/admins_del_fn.php";
		var pars = "id_admin="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "admins_list.php";
				 }
			 }
		 }); 		
	}
}

function affiliates_save() {
	
	dsp_save(true,"");
	var url = "includes/affiliates_am_fn.php";
	if (!affiliates_validate()) { return 0 };
	
	var pars = Form.serialize("frm_affiliates")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							 //myLightbox.end();
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 dsp_save(false, "Record Saved");
							 }
						 }
					 }); 
}

function affiliates_validate() {
	return true;
}

// -- MESAS -- //
function mesas_del(id, mesa) {
	var msg = "This will delete this table. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/mesas_del_fn.php";
		var pars = "id_mesa="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "mesas_list.php";
				 }
			 }
		 }); 		
	}
}

function mesas_save() {

	dsp_save(true,"");
	
	var url = "funciones/mesas_am_fn.php";
	if (!mesas_validate()) { return 0 };
	
	var pars = Form.serialize("frm_mesas")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 dsp_save(false, "Record Saved");
							 }
						 }
					 }); 
}

function mesas_validate() {
	return true;
}

function mesas_range_save() {
	
	dsp_save(true,"");
	var url = "funciones/mesas_range_am_fn.php";
	if (!mesas_range_validate()) { return 0 };
	
	var pars = Form.serialize("frm_mesas_range")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							 //myLightbox.end();
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 dsp_save(false, "Record Saved");
							 }
						 }
					 }); 
}

function mesas_range_validate() {
	return true;
}

// -- TORNEOS -- //
function torneos_del(id, torneo) {
	var msg = "This will delete this tournament. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/torneos_del_fn.php";
		var pars = "id_torneo="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "?page=torneos";
				 }
			 }
		 }); 		
	}
}

function torneos_save() {
	
	dsp_save(true,"");
	var url = "funciones/torneos_am_fn.php";
	if (!torneos_validate()) { return 0 };
	
	var pars = Form.serialize("frm_torneos")
	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							 //myLightbox.end();
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 dsp_save(false, "Record Saved");
								 document.location.href="?page=torneos";
							 }
						 }
					 }); 
}

function torneos_validate() {
	return true;
}

// -- TORNEOS HORARIOS -- //
function horarios_del(id, horario) {
	var msg = "This will delete this event. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/torneos_horarios_del_fn.php";
		var pars = "id="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = obj.message;
				 }
			 }
		 }); 		
	}
}

function horarios_save() {
	
	dsp_save(true,"");
	var url = "funciones/torneos_horarios_am_fn.php";
	if (!horarios_validate()) { return 0 };
	
	//comboValuesToFormField($("as_tables"), $("quota"))
	var pars = Form.serialize("frm_torneos_horarios")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							 //myLightbox.end();
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
								 dsp_save(false, "");
							 } else {
								 dsp_save(false, "Record Saved");
								 //document.location.href = "?page=horarios&id="+obj.id;
							 }
						 }
					 }); 
}

function horarios_validate() {
	return true;
}

function excepciones_del(id, torneo) {
	var hid = $F("id_torneo_horario");
	var tid = $F("tid");
	var cid = $F("cid");
	
	var msg = "This will delete this exception. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/torneos_excepciones_del_fn.php";
		var pars = "id_torneo_excepcion="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					document.location.href = "?page=torneoham&cid="+cid+"&tid="+tid+"&hid="+hid;
				 }
			 }
		 }); 		
	}
}

function excepciones_save() {
	
	dsp_save(true,"");
	var hid = $F("id_torneo_horario");
	var tid = $F("tid");
	var cid = $F("cid");
	
	var url = "funciones/torneos_excepciones_am_fn.php";
	if (!excepciones_validate()) { return 0 };
	
	var pars = Form.serialize("frm_excepciones")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								dsp_save(false, "Record Saved");								 
								document.location.href = "?page=torneoham&cid="+cid+"&tid="+tid+"&hid="+hid;
							 }
						 }
					 }); 
}

function excepciones_validate() {
	return true;
}

// -- TORNEOS USUARIOS -- //
function usuarios_del(id, torneo) {
	var msg = "Esta seguro de que desea eliminar el Usuario '" + usuario + "' (id:" + id + ")";
	if(confirm(msg)) {
		var url ="funciones/usuarios_del_fn.php";
		var pars = "id_usuario="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "usuarios_list.php";
				 }
			 }
		 }); 		
	}
}

function usuarios_save() {
	
	//myLightbox.showLoading();
	var url = "funciones/usuarios_am_fn.php";
	if (!usuarios_validate()) { return 0 };
	
	var pars = Form.serialize("frm_usuarios")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							 //myLightbox.end();
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
							 	document.location.href = "?page=users";
							 }
						 }
					 }); 
}

function usuarios_validate() {
	return true;
}

// -- RULES -- //
function rules_del(id, torneo) {
	var msg = "This will delete this rule. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/quota_rules_del_fn.php";
		var pars = "id_torneo="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "quota_rules_list.php";
				 }
			 }
		 }); 		
	}
}

function rules_save() {

	dsp_save(true,"");
	var url = "funciones/quota_rules_am_fn.php";
	if (!rules_validate()) { return 0 };
	
	comboValuesToFormField($("as_tables"), $("quota"));
	var pars = Form.serialize("frm_rules")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							alert(req.responseText)
							var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 dsp_save(false, "Record Saved");								 
								//document.location.href = "?page=torneose";
							 }
						 }
					 }); 
}

function rules_validate() {
	return true;
}

// -- ON SALE -- //
function onsale_del(id, torneo) {
	var msg = "This will delete this rule. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/quota_rules_del_fn.php";
		var pars = "id_torneo="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "quota_rules_list.php";
				 }
			 }
		 }); 		
	}
}

function onsale_save() {

	dsp_save(true,"");
	var url = "funciones/onsale_am_fn.php";
	if (!rules_validate()) { return 0 };
	
	comboValuesToFormField($("as_tables"), $("quota"));
	var pars = Form.serialize("frm_rules")
	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							//alert(req.responseText)
							dsp_save(false, "Record Saved");	
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 dsp_save(false, "Record Saved");								 
								//document.location.href = "?page=torneose";
							 }
						 }
					 }); 
}

function rules_validate() {
	return true;
}

function send_request() {
	var url = "funciones/submit_request_fn.php";
	var pars = Form.serialize("frm_submit")
	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 alert("Your request has been sent");								 
								//document.location.href = "?page=torneose";
							 }
						 }
					 }); 	
	
}

function fees_del(id, mesa) {
	var msg = "This will delete this fee. It's not undoable. Are you sure?";
	if(confirm(msg)) {
		var url ="funciones/fees_del_fn.php";
		var pars = "id_fee="+id;
		new Ajax.Request(url,
		 {
			 parameters:pars,
			 method: 'post',
			 onComplete: function (req) {
				 //alert(req.responseText)
				 var obj = JSON.parse(req.responseText);
				 if(obj.has_errors==1) {
					 alert(obj.message);
				 } else {
					 document.location.href = "?page=fees";
				 }
			 }
		 }); 		
	}
}

function fees_save() {

	dsp_save(true,"");
	
	var url = "funciones/fees_am_fn.php";
	//if (!mesas_validate()) { return 0 };
	
	var pars = Form.serialize("frm_fees")

	new Ajax.Request(url,
					 {
						 parameters:pars,
						 method: 'post',
						 onComplete: function (req) {
							//alert(req.responseText)
							 var obj = JSON.parse(req.responseText);
							 if(obj.has_errors==1) {
								 alert( obj.message);
							 } else {
								 dsp_save(false, "Record Saved");
							 }
						 }
					 }); 
}

function filter_seats(id_mesa, list) {
	
	for(var i=0; i<list.options.length; i++) {
		var op = list.options[i];
		if(id_mesa == 0) {
			op.style.display = '';
		} else {
			op.style.display = id_mesa == op.getAttribute('mesa') ? 'block' : 'none';	
		}
	}
}

function select_full_table(mesa,cseats) {
	var chk = true;
	var chkName = "";
	for(var i=1; i<=cseats; i++) {
		chkName = "seat["+mesa+"]["+i+"]";
		chk = chk && $(chkName).checked;
	}
	for(var i=1; i<=cseats; i++) {
		chkName = "seat["+mesa+"]["+i+"]";
		if(! $(chkName).disabled) {$(chkName).checked = !chk;}
	}	
}
