﻿YAHOO.namespace("Wageroo.AJAX");
var initialized;

function showBuddyDialogModal(userName)
{

	var buddyDialogModal = document.getElementById("buddyDialogModal");
	var buddyDialogModal_header = document.getElementById("buddyDialogModal_header");
	var buddyDialogModal_content = document.getElementById("buddyDialogModal_content");

	var buddyDialogModalWrapper = document.getElementById('buddyDialogModalWrapper');
	if (buddyDialogModalWrapper)
	{
		buddyDialogModalWrapper.style.width = '400px';
		buddyDialogModalWrapper.style.height = '150px';
	}
	
	var callback =
	{
		success: function(o)
		{
			buddyDialogModal.className = "BuddyDialogModalLoaded";
			buddyDialogModal_content.innerHTML = o.responseText;

			var hidBuddyHeader = document.getElementById('hidBuddyHeader');
			if (hidBuddyHeader != null)
			{
				buddyDialogModal_header.innerHTML = hidBuddyHeader.value;
			}
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	if (YAHOO.Wageroo.AJAX.buddyDialogModal != null)
	{
		YAHOO.Wageroo.AJAX.buddyDialogModal.show();
		YAHOO.util.Connect.asyncRequest("GET", "Methods/BuddyActions.aspx?u=" + userName, callback, null);
	}
}

function showDeleteBuddyDialogModal(userName, header)
{
	var buddyDialogModal = document.getElementById("buddyDialogModal");
	var buddyDialogModal_header = document.getElementById("buddyDialogModal_header");
	var buddyDialogModal_content = document.getElementById("buddyDialogModal_content");

	var buddyDialogModalWrapper = document.getElementById('buddyDialogModalWrapper');
	if (buddyDialogModalWrapper)
	{
		buddyDialogModalWrapper.style.width = '300px';
		buddyDialogModalWrapper.style.height = '95px';
	}
	
	var callback = {
		success: function(o)
		{
			buddyDialogModal.className = "BuddyDialogModalLoaded";
			buddyDialogModal_content.innerHTML = o.responseText;
			buddyDialogModal_header.innerHTML = header;
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	if (YAHOO.Wageroo.AJAX.buddyDialogModal)
	{
		YAHOO.Wageroo.AJAX.buddyDialogModal.show();
		YAHOO.util.Connect.asyncRequest("GET", "Methods/BuddyActions.aspx?u=" + userName + "&action=delete", callback, null);
	}
}

function showWagerDialogModal(wagerId, choice, url)
{
	var wagerDialogModal = document.getElementById("wagerDialogModal");
	var wagerDialogModal_header = document.getElementById("wagerDialogModal_header");
	var wagerDialogModal_content = document.getElementById("wagerDialogModal_content");

	var callback = {
		success: function(o)
		{
			wagerDialogModal.className = "BuddyDialogModalLoaded";
			wagerDialogModal_content.innerHTML = o.responseText;
			wagerDialogModal_header.innerHTML = "Make a Wager";
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	if (YAHOO.Wageroo.AJAX.wagerDialogModal)
	{
		YAHOO.Wageroo.AJAX.wagerDialogModal.show();
		YAHOO.util.Connect.asyncRequest("GET", "Methods/WagerActions.aspx?wagerId=" + wagerId + "&choice=" + choice + "&returnUrl=" + url, callback, null);
	}
}

function showWagerOutcomeDialogModal(wagerId, header)
{
	var wagerDialogModal = document.getElementById("wagerDialogModal");
	var wagerDialogModal_header = document.getElementById("wagerDialogModal_header");
	var wagerDialogModal_content = document.getElementById("wagerDialogModal_content");


	var callback = {
		success: function(o)
		{
			wagerDialogModal.className = "BuddyDialogModalLoaded";
			wagerDialogModal_content.innerHTML = o.responseText;
			wagerDialogModal_header.innerHTML = header;
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	if (YAHOO.Wageroo.AJAX.wagerDialogModal)
	{
		YAHOO.Wageroo.AJAX.wagerDialogModal.show();
		YAHOO.util.Connect.asyncRequest("GET", "Methods/WagerActions.aspx?wagerId=" + wagerId + "&choice=outcome", callback, null);
	}
}

function showAffinityEducation(header)
{
	try
	{
		var buddyDialogModal = document.getElementById("buddyDialogModal");
		var buddyDialogModal_header = document.getElementById("buddyDialogModal_header");
		var buddyDialogModal_content = document.getElementById("buddyDialogModal_content");

		var buddyDialogModalWrapper = document.getElementById('buddyDialogModalWrapper');
		if (buddyDialogModalWrapper)
		{
			buddyDialogModalWrapper.style.width = '450px';
			buddyDialogModalWrapper.style.height = '460px'
		}

		var callback = {
			success: function(o)
			{
				buddyDialogModal.className = "BuddyDialogModalLoaded";
				buddyDialogModal_content.innerHTML = o.responseText;

				buddyDialogModal_header.innerHTML = header;
			},
			failure: function(o)
			{
				alert('failure');
			}
		}

		if (YAHOO.Wageroo.AJAX.buddyDialogModal)
		{
			YAHOO.Wageroo.AJAX.buddyDialogModal.show();
			YAHOO.util.Connect.asyncRequest("GET", "Methods/AffinityEducation.aspx", callback, null);
		}
	}
	catch (e)
	{
		alert(e.message);
	}
}


function submitEPWagerDialogModal(wagerId, choice, url)
{
	var wagerDialogModal = document.getElementById("wagerDialogModal");
	var wagerDialogModal_content = document.getElementById("wagerDialogModal_content");

	var callback = {
		success: function(o)
		{
			wagerDialogModal.className = "BuddyDialogModalSubmitted";
			wagerDialogModal_content.innerHTML = o.responseText;
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	YAHOO.util.Connect.asyncRequest("POST", "Methods/WagerActions.aspx", callback, "wagerId=" + wagerId + "&choice=" + choice + "&returnUrl=" + url);
}

function submitOTOWagerDialogModal(wagerId, choice, url)
{
	var wagerDialogModal = document.getElementById("wagerDialogModal");
	var wagerDialogModal_content = document.getElementById("wagerDialogModal_content");

	var callback = {
		success: function(o)
		{
			wagerDialogModal.className = "BuddyDialogModalSubmitted";
			wagerDialogModal_content.innerHTML = o.responseText;
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	var kickerField = document.getElementById('txtKicker');
	var roosField = document.getElementById('txtRoos');
	var oddsField = document.getElementById('ddlOdds');

	var params = "wagerId=" + wagerId + "&choice=" + choice + "&returnUrl=" + url;

	if (kickerField != null)
	{
		params += "&kicker=" + kickerField.value;
	}

	if (roosField != null)
	{
		params += "&roos=" + roosField.value;
	}

	if (oddsField != null)
	{
		params += "&odds=" + oddsField.value;
	}

	YAHOO.util.Connect.asyncRequest("POST", "Methods/WagerActions.aspx", callback, params);
}

function sendBuddyMessageModal(sendMessUrl)
{
	YAHOO.Wageroo.AJAX.buddyDialogModal.hide();
	window.location.href = sendMessUrl;
}

function submitBuddyDialogModal(userName, action)
{
	var buddyDialogModal = document.getElementById("buddyDialogModal");
	var buddyDialogModal_header = document.getElementById("buddyDialogModal_header");
	var buddyDialogModal_content = document.getElementById("buddyDialogModal_content");

	var callback = {
		success: function(o)
		{
			buddyDialogModal.className = "BuddyDialogModalSubmitted";
			buddyDialogModal_header.innerHTML = "Buddy Status";
			buddyDialogModal_content.innerHTML = o.responseText;
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	var params = "u=" + userName;
	if (action != null)
	{
		params += "&action=" + action;
	}

	resetLoadingModal("buddyDialogModal", "buddyDialogModal_header", "buddyDialogModal_content");
	YAHOO.util.Connect.asyncRequest("POST", "Methods/BuddyActions.aspx", callback, params);
}

function resetLoadingModal(modal, header, content)
{
	var dialogModal = document.getElementById(modal);
	var dialogModal_header = document.getElementById(header);
	var dialogModal_content = document.getElementById(content);

	dialogModal.className = "DialogModalWaiting";
	dialogModal_header.innerHTML = "Loading, please wait...";
	dialogModal_content.innerHTML = "<center><img src=\"http://us.i1.yimg.com/us.yimg.com/i/us/per/gr/gp/rel_interstitial_loading.gif\" alt=\"Loading\" /></center>";
}

function hideWagerDialogModal()
{
	YAHOO.Wageroo.AJAX.wagerDialogModal.hide();
	resetLoadingModal("wagerDialogModal", "wagerDialogModal_header", "wagerDialogModal_content");
}

function hideBuddyDialogModal()
{
	YAHOO.Wageroo.AJAX.buddyDialogModal.hide();
	resetLoadingModal("buddyDialogModal", "buddyDialogModal_header", "buddyDialogModal_content");
}

function showSimpleDialogModal(header, body)
{
	try
	{
		var simpleDialogModal_header = document.getElementById("simpleDialogModal_header");
		var simpleDialogModal_content = document.getElementById("simpleDialogModal_content");

		simpleDialogModal_header.innerHTML = header;
		simpleDialogModal_content.innerHTML = body;

		YAHOO.Wageroo.AJAX.simpleDialogModal.show();
	}
	catch (e)
	{
		alert(e.message);
	}
}

var handleSimpleDialogModalOK = function()
{
	this.hide();
}

function showRegistrationDialogModal()
{
	var registrationDialogModal = document.getElementById("registrationDialogModal");
	var registrationDialogModal_header = document.getElementById("registrationDialogModal_header");
	var registrationDialogModal_content = document.getElementById("registrationDialogModal_content");

	var callback = {
		success: function(o)
		{
			registrationDialogModal.className = "RegistrationDialogModalLoaded";
			registrationDialogModal_header.innerHTML = "Security check";
			registrationDialogModal_content.innerHTML = o.responseText;
		},
		failure: function(o)
		{
			alert('failure');
		}
	}

	if (YAHOO.Wageroo.AJAX.registrationDialogModal)
	{
		YAHOO.Wageroo.AJAX.registrationDialogModal.show();
		YAHOO.util.Connect.asyncRequest("GET", "Methods/RegistrationActions.aspx", callback, null);
	}
}

function hideRegistrationDialogModal()
{
	YAHOO.Wageroo.AJAX.registrationDialogModal.hide();
	resetLoadingModal("registrationDialogModal", "registrationDialogModal_header", "registrationDialogModal_content");
}

function reloadRegistrationDialogModal()
{
	resetLoadingModal("registrationDialogModal", "registrationDialogModal_header", "registrationDialogModal_content");
	showRegistrationDialogModal();
}

function submitRegistrationDialogModal(imageId, captchaFieldName)
{
	var registrationDialogModal = document.getElementById("registrationDialogModal");
	var registrationDialogModal_content = document.getElementById("registrationDialogModal_content");

	var callback = {
		success: function(o)
		{
			registrationDialogModal.className = "RegistrationDialogModalSubmitted";
			registrationDialogModal_content.innerHTML = o.responseText;
		},
		failure: function(o)
		{
			console.log('o', o);
			alert('failure');
		}
	}

	var userNameField = document.getElementById(RegistrationFields[0]);
	var emailField = document.getElementById(RegistrationFields[1]);
	var passwordField = document.getElementById(RegistrationFields[2]);
	var dayField = document.getElementById(RegistrationFields[3]);
	var monthField = document.getElementById(RegistrationFields[4]);
	var yearField = document.getElementById(RegistrationFields[5]);

	var captchaField = document.getElementById(captchaFieldName);
	var postData = "g=" + imageId + "&captcha=" + captchaField.value + "&userName="
        + userNameField.value + "&email=" + emailField.value + "&password=" + passwordField.value
        + "&day=" + dayField.value + "&month=" + monthField.value + "&year=" + yearField.value;

	YAHOO.util.Connect.asyncRequest("POST", "Methods/RegistrationActions.aspx", callback, postData);
}

function textboxMultilineMaxNumber(txt, maxLen)
{
	try
	{
		if (txt.value.length > (maxLen - 1))
			return false;
	} catch (e)
	{
	}
}

function resetRegistrationFields()
{
	var userNameField = document.getElementById(RegistrationFields[0]);
	var emailField = document.getElementById(RegistrationFields[1]);
	var passwordField = document.getElementById(RegistrationFields[2]);
	var dayField = document.getElementById(RegistrationFields[3]);
	var monthField = document.getElementById(RegistrationFields[4]);
	var yearField = document.getElementById(RegistrationFields[5]);
	var confirmPasswordField = document.getElementById(RegistrationFields[6]);
	var termsCheckbox = document.getElementById(RegistrationFields[7]);

	userNameField.value = '';
	emailField.value = '';
	passwordField.value = ''
	dayField.value = '0';
	monthField.value = '0';
	yearField.value = '0';
	confirmPasswordField.value = '';
	termsCheckbox.checked = false;
}

function popupValidation(registrationGroup, method)
{
	var body = ManuallyValidateHtml(registrationGroup);

	if (Page_IsValid)
	{
		if (method != null)
		{
			eval(method);
		}
	}
	else
	{
		showSimpleDialogModal("Yo! Hold on!", body);
	}
}

function ManuallyValidateHtml(registrationGroup)
{
	var body = "";

	Page_ClientValidate(registrationGroup);
	if (!Page_IsValid)
	{
		var pre = "<li>";
		var post = "</li>";

		body += "<span style='color:red;'><ul>";
		for (i = 0; i < Page_Validators.length; i++)
		{
			if (!Page_Validators[i].isvalid && typeof (Page_Validators[i].errormessage) == "string")
			{
				body += pre + Page_Validators[i].errormessage + post;
			}
		}
		body += "</ul></span>";
	}

	return body;
}

function popupModal_init()
{
	if (initialized != true)
	{
		YAHOO.Wageroo.AJAX.buddyDialogModal = new YAHOO.widget.Dialog("buddyDialogModalWrapper", {
			fixedcenter: true,
			constraintoviewport: true,
			width: "400px",
			modal: true,
			close: false,
			visible: false,
			//effect: { effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.75 },
			draggable: false
		});

		YAHOO.Wageroo.AJAX.buddyDialogModal.render();

		YAHOO.Wageroo.AJAX.simpleDialogModal = new YAHOO.widget.SimpleDialog("simpleDialogModalWrapper", {
			fixedcenter: true,
			constraintoviewport: true,
			width: "300px",
			modal: true,
			close: false,
			visible: false,
			draggable: false,
			buttons: [{ text: "OK", handler: handleSimpleDialogModalOK}]
		});

		YAHOO.Wageroo.AJAX.simpleDialogModal.render();

		YAHOO.Wageroo.AJAX.registrationDialogModal = new YAHOO.widget.Dialog("registrationDialogModalWrapper", {
			fixedcenter: true,
			constraintoviewport: true,
			width: "300px",
			modal: true,
			close: false,
			visible: false,
			draggable: false
		});

		YAHOO.Wageroo.AJAX.registrationDialogModal.render();

		YAHOO.Wageroo.AJAX.wagerDialogModal = new YAHOO.widget.Dialog("wagerDialogModalWrapper", {
			fixedcenter: true,
			constraintoviewport: true,
			width: "300px",
			modal: true,
			close: false,
			visible: false,
			draggable: false
		});

		YAHOO.Wageroo.AJAX.wagerDialogModal.render();

		initialized = true;
	}
}