function _MatchPage(_pageCode)
{
	var _windowTimer = null;
	var _renderComplete = false;
	this.UpdateFormActions = _tsUpdateFormActions;
	this.PageCode = _pageCode;
	this.WindowTimer = _windowTimer;
	this.RenderComplete = _renderComplete;
	this.FormReplaceVar = "@FORMURL"
	this.MainForm = 'document.matchMainForm';
	this.SubmitForm = _submitForm;
	this.EnsureObjects = _ensureObjects;
	this.BlockEnterKey = false;
	this.recursiveIterations = 0;
	return this;
}

function _onloadEventHandler(args)
{
	if(jsMatchPage)
	{
		jsMatchPage.RenderComplete = true;
		jsMatchPage.EnsureObjects();
		if(jsMatchPage.MainForm)
		{
			if(jsMatchPage.MainForm.__FORMQUERYSTRING)
			{
				jsMatchPage.UpdateFormActions(jsMatchPage.MainForm.__FORMQUERYSTRING.value);
			}
		}
	}
	if(FireOnload){FireOnload(args);}
}

function _ensureObjects()
{
	if(typeof this.MainForm != 'object'){this.MainForm = eval(this.MainForm);}
}

function _submitForm(linkID)
{
	this.EnsureObjects();
	if(!this.MainForm){return;}
	var newFormUrl = this.MainForm.__FORMQUERYSTRING.value;
	var tempArray;
	var currentLinkID;
	var startPoint = -1;
	var endPoint = -1;
	if(linkID.toString().length > 0)
	{
		//This gets the current LID value from the QS.  This is required in case there is a LID from another link (typical)
		//So this handles replacing a dynamic LID value using RegEx
		startPoint = newFormUrl.indexOf("lid=");
		endPoint = newFormUrl.indexOf("&",newFormUrl.indexOf("lid="));
		startPoint = (startPoint == -1) ? 0 : startPoint + 4;
		endPoint = (endPoint == -1) ? newFormUrl.length : endPoint;
		currentLinkID = newFormUrl.substring(startPoint,endPoint);
		//Test the new QS for the existence of the LID param
		if(newFormUrl.indexOf("lid=") > -1)
		{
			var regExp = eval('/lid=' + currentLinkID + '/');
			newFormUrl = newFormUrl.replace(regExp,"lid=" + linkID.toString());
		}
		else	//No LID
		{
			if(newFormUrl.indexOf("?") > 0)
			{
				newFormUrl += '&lid=' + linkID.toString();
			}
			else
			{
				newFormUrl = '?lid=' + linkID.toString();
			}
		}
		this.UpdateFormActions(newFormUrl);
	}
	if(this.MainForm.onsubmit != null)
	{
		this.MainForm.onsubmit = null;
	}
}

function _tsUpdateFormActions(newUrl)
{
	if(this.WindowTimer){window.clearTimeout(this.WindowTimer);}
	if((newUrl == '') || (typeof newUrl== 'undefined') || (newUrl == null)){return false;}
	var tempArray;
	var formTarget;
	var regExp = eval("/" + this.FormReplaceVar + "\\?/");
	var loopCount;
	var useSSL = (newUrl.indexOf("http") > -1) ? true : false;
	if(document.forms)
	{
		for(loopCount = 0;loopCount < document.forms.length;loopCount++)
		{
			if(document.forms[loopCount].__FORMPOSTBACK)		//Ensure that the form we are parsing is the Match main form
			{
				if(useSSL)
				{
					formTarget = document.forms[loopCount].__FORMPOSTBACK.value + '?';
				}
				else
				{
					tempArray = document.forms[loopCount].action.split("?");
					formTarget = tempArray[0] + '?';
					tempArray = null;
				}
				document.forms[loopCount].action = (newUrl.indexOf(this.FormReplaceVar) > -1) ? newUrl.replace(regExp,formTarget) : newUrl;
			}
			document.forms[loopCount].onsubmit = preSubmitActions;
		}
	}
	else
	{
		if(this.recursiveIterations < 5)
		{
			this.WindowTimer = window.setTimeout('_tsUpdateFormActions("' + newUrl + '");',250);
			this.recursiveIterations++;
		}
	}
	return true;
}

function blockSubmit(){return false;}

function preSubmitActions()
{
	var returnValue = true;
	if(this)
	{
		if(this.action.indexOf("sid") > -1){unloadPopup = false;}
	}
	return returnValue;
}

var tourWinHeight = 395;
var tourWinWidth = 715;

function openMatchTour(tourUrl)
{
	window.location.href = tourUrl;
	//openMatchTourWin(tourUrl,tourWinHeight,tourWinWidth);
}

function openMatchTourWin(tourUrl,h,w)
{
	var windowFeatures = "height=" + h + ",width=" + w + ",innerHeight=" + h + ",innerWidth=" + w + ",hotkeys=0,location=0,menubar=0,personalbar=0,resizable=0,scrollbars=0,status=0,titlebar=1,toolbar=0";
	if(tourUrl.length > 0)
	{
		window.open(tourUrl,"_matchTour",windowFeatures);
	}
}

window.onerror = null;
