var gridDetails;
var dpDetails;
var loader;
/*inits the tournament table*/
function showGrid(){
	grid = new dhtmlXGridObject('gridbox');
	grid.setImagePath("../codebase/imgs/");
	var division = "Junior";
	if(team) division = "Juniors";
	grid.setHeader("Name-Home State,"+division+",Location,Big Fish,Top Four,Total");
	grid.setInitWidthsP("39,9,14,11,13,14");
   	grid.setColAlign("left,center,left,right,right,right");
  	grid.setColSorting("str,str,str,sort_lb,sort_lb,sort_lb");
	grid.setColTypes("ro,ro,ro,ro,ro,ro");
    grid.init();
	grid.enableAutoHeight(true,500);
	grid.setSkin("modern");
	grid.enableColSpan(1);
	loadData();
}

/*loads data in to tournament table*/
function loadData(){
	grid.loadXML("../php/actions.php?event="+tournament+"&type=results"+(team?"&team=1":""),function(){
		if(!grid.getRowsNum()){
			
			grid.addRow("new","There are no registered participants on this tournament");
			grid.setColspan("new",0,5);
		}
		else{
			grid.sortRows(5, "sort_lb", "desc");
			grid.setSortImgState(true, 5, "desc");
		}
	});
}

/*shown angler's (team's) results*/
function showAllResults(){
	gridDetails = new dhtmlXGridObject('details');
	gridDetails.setImagePath("../codebase/imgs/");
	gridDetails.setHeader("Delete,LB,OZ,Date,Location,Details,Photo");
	gridDetails.setInitWidthsP("8,5,5,21,21,30,10");
   	gridDetails.setColAlign("center,center,center,left,left,left,center");
  	gridDetails.setColSorting("int,int,int,str,str,str,na");
	gridDetails.setColTypes("ch,ed,ed,ro,ed,txt,link");
    gridDetails.init();
	gridDetails.setColumnIds("delete,lb,oz,d,location,details,im");
	gridDetails.setSkin("modern");
	gridDetails.enableColSpan(1);
	gridDetails.loadXML("../php/actions.php?id="+angler+"&event="+tournament+"&type=resultdetails"+(team?"&team=1":""),function(){
	});
	dpDetails = new dataProcessor("");
	dpDetails.setUpdateMode("off");
	dpDetails.enableDataNames(true);
	dpDetails.init(gridDetails);
	gridDetails.attachEvent("onAfterUpdate", function(sid, action, tid){
		if(action=="delete"){
			gridDetails.clearAll();
			gridDetails.loadXML("../php/actions.php?id="+angler+"&event="+tournament+"&type=resultdetails"+(team?"&team=1":""));
		}
		loadData();
		return true;
	})
}

function sort_lb(a,b,order){
	a = getValues(a);
	b = getValues(b);
	if(a[0]==b[0]){
		if(order=="asc")
			return a[1]>b[1]?1:-1;
		else
			return a[1]<b[1]?1:-1;
	}
	else{
		if(order=="asc")
			return a[0]>b[0]?1:-1;
		else
			return a[0]<b[0]?1:-1;
	}
}
function getValues(str){
	var arr = new Array();
	lb_oz = str.split("lbs");
	if(lb_oz.length == 1){
		lb_oz = str.split("lb");
		if(lb_oz.length < 2){
			arr[0]=0;
			arr[1]=0;
			return arr
		}
	}
	arr[0] = eval(lb_oz[0]);
	oz = lb_oz[1].split("oz");
	arr[1] = eval(oz[0]); 
	return arr;
}
	
/*executes login*/
function login(){
	if(document.getElementById("login_button").value == "Logout"){
		dhtmlxAjax.post("../php/actions.php","type=logout",function(){
			document.location.href = document.location.href;
		});
	}
	else
	loader = dhtmlxAjax.post("../php/actions.php","type=checkregistration&login=" + document.getElementById("login").value + "&password=" + 	document.getElementById("pass").value+"&event="+tournament+(team?"&team=1":""),auth);
}


function auth() { 
	var res = loader.doXPath("//status");
	if(res[0]){
		var names =""
		var val = res[0].getAttribute("value");
		if(res[0].firstChild.data)
			var names = res[0].firstChild.data;
		switch (val){
			case "3":
					document.location.href = "results.php?name="+names+"&rnum="+(new Date).valueOf()+"&event="+tournament+(team?"&team=1":"")+(edit_mode?"&edit=1":"");
				break;
			case "2":
					alert("Dear "+names+", your registration hasn't been confirmed yet.");
				break;
			case "1":
				var mes = "Dear "+names+", you are not registered on this tournament. Do you do want to send registration request to the administarator ?";
				if(window.confirm(mes)){
					sendRegRequest(document.getElementById("login").value,document.getElementById("pass").value);
					alert("The request has been sent")
				}
				break;
			default:
				alert("Incorrect login or password. Please try again.");
				break;
		}
		document.getElementById("login").value ="";
		document.getElementById("pass").value ="";
	}
}

function sendRegRequest(login,passw){
	dhtmlxAjax.post("../php/actions.php","type=register&login="+login+"&password=" +passw+(team?"&team=1":"")+"&event="+tournament);
}

function showImages(id){
	window.open("photos.php?id="+id+(team?"&team=1":"")+"&event="+tournament,"photos","height=270,width=743,resizable=0");
}

function showDeatils(e){
	var display_state = document.getElementById('allres').style.display;
	document.getElementById('allres').style.display=(display_state==""?"none":"");
	if(angler!=""&&!gridDetails) 
		showAllResults();
	(e.srcElement||e.target).innerHTML = (display_state==""?"Show all of my results":"Hide Results");
}

function saveDetails(){
	dpDetails.serverProcessor = "../php/actions.php?type=editresult&id="+angler+"&event="+tournament+(team?"&team=1":"");
	gridDetails.editStop();
	if(window.confirm('Do you want to save changes ?'))
		dpDetails.sendData();
	
}

function showForget(){
document.getElementById("forget").style.display = (document.getElementById("forget").style.display==""?"none":"");
}

function getPassword(){
	var loader = dhtmlxAjax.post("../php/actions.php","type=getpassword&login=" + document.getElementById("login_confirm").value+(team?"&team=1":""),passwordResponse);
}

function passwordResponse(loader){
	var res = loader.doXPath("//status");
	if(res[0]){
		var status = res[0].getAttribute("value");
		if(status=="1") alert("The new password is sent to your email.");
		if(status=="0") alert("Error: the password can not be got!");
	}
}


function validateOzValue(){
	var val = parseInt(document.getElementById("oz").value,10);
	if(val<16 && val>=0) 
		return true;
	alert("Please check oz value (its range is 0-15)");
	return false;
}
	
	
	
	

