// $Id: TnmtPreRegLib.js,v 1.5 2011-07-01 16:26:46 jrixon Exp $

function TnmtPreRegLib(name) {

	var __pub = {
		name : name,
		params : {}
	}

	var Req = ReqLib(name, {
		reqType : 'tnmt_pre_reg',
		type : null,
		refreshNormal : null,
		refreshIdle : null,
		url : null,
		respType : 'arr',
		renderDelay : 1000
	});

	var _urls = {
		preReg : getBaseURL() + '?action=tnmt_pre_reg'
	};

	var _result = '';
	var _msg = '';
	var _els    = {};
	var _timeout = {
		ms : 15000,
		timer : null
	};

	var _midRequest   = false;


function makeURL() {

	var url = _urls.preReg;
	
	url += '&game_id=' + __pub.params.gameId + '&private=' + __pub.params.prvt;

	return url;
}

function makeId() {

	return null;
}

function canReq() {

	if (Req.canReq(true) && !_midRequest) {
		return true;
	}
	return false;
}


function req() {

	_timeout.timer = setTimeout(_timedout, _timeout.ms);

	_midRequest = true;

	Req.req();
	return false;
}


function render() {

	if (_timeout.timer != null) {
		clearTimeout(_timeout.timer);
	}

	_midRequest = false;
	
	if (Req.pub.items[0] == "OK") {
		if (Req.pub.items[1] == 0) {
			__pub.params.result = Req.pub.items[2];
		} else if (Req.pub.items[1] == 1) {
			__pub.params.result    = 'OK';
			__pub.params.pwdLmt    = Req.pub.items[2];
			__pub.params.buyIn     = Req.pub.items[3];
			__pub.params.tokAmount = __pub.params.ccyCode+Req.pub.items[4];
		} else {
			__pub.params.result    = 'OK';
			__pub.params.tokAmount = __pub.params.ccyCode+Req.pub.items[2];
			__pub.params.pwdLmt    = 'N';
		}
	} else {
		__pub.params.result = Req.pub.items[1];
	}

	dispLogin();

	hint('done');
}

function hint(which) {

	switch (which) {
		case 'update':
		case 'new':
			for (var i = 0; i < _els.buttons.length; i++) {
				_els.buttons[i].hide();
			}
			_els.msg.innerHTML = 'Please wait...';
			break;
		case 'done':
			if (__pub.params.result == 'OK') {

				sky.overlay.destroy();
				TnmtReg.showForm('S', __pub.params.gameId, __pub.params.gameName, __pub.params.buyIn, __pub.params.startDesc, __pub.params.tnmtType, __pub.params.prizePool, __pub.params.pwdLmt, __pub.params.tokAmount);

			} else {

				_els.msg.innerHTML = __pub.params.result;
				_els.buttons[0].show();
			}
			break;
	}
}

function _timedout() {

	if (_timeout.timer != null) {
		clearTimeout(_timeout.timer);
	}

	if (!_midRequest) {
		return;
	}
	_midRequest = false;
	_result = 'There was a problem contacting the server. Please try again.';
	hint('done');
}

function check(game_id, game_name, prvt, buy_in, start_desc, tnmt_type, prize_pool_total, ccy_code) {

	var overlayTxt = '';
	if (!canReq()) {
		return false;
	}

	if (typeof game_id != 'undefined') {
		__pub.params.gameId      = game_id;
		__pub.params.gameName    = game_name;
		__pub.params.prvt        = prvt;
		__pub.params.buyIn       = buy_in;
		__pub.params.startDesc   = start_desc;
		__pub.params.tnmtType    = tnmt_type;
		__pub.params.prizePool   = ccy_code + prize_pool_total;
		__pub.params.ccyCode     = ccy_code;
	}

	if (!chkLogin(TnmtPreReg.check)) {
		return false;
	}

	if (__pub.params.prvt == 'Y') {
		overlayTxt = 'Private Tournament Registration';
	}

	sky.overlay.getChoice(
		overlayTxt,
		'<span id="prvt_tnmt_msg">&nbsp;</span>',
		[
			sky.overlay.button(
				'OK'
			)
		]
	);

	_els = {
		msg : document.getElementById('prvt_tnmt_msg'),
		buttons : sky.overlay.getButtons()
	}

	req();

	return false;
}


	var _this = {
		pub : __pub,
		Req : Req,
		makeURL : makeURL,
		canReq : canReq,
		makeId : makeId,
		req : req,
		render : render,
		hint : hint,
		check : check
	};

	Req.regPar(_this);

	return _this;
}

