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);}
}


//Browser detection rountine with a few tweaks (Courtesy of http://www.quirksmode.org/js/detect.html)
var BrowserDetect = {
	initialized: false,
	
	init: function () {
		if (!this.initialized) {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
			
			this.initialized = true;
		}
	},
	
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};

// Function designed to handle the return key press behavior (Courtesy of Sphorium Technologies)
function ReturnAction(evt, targetEvent) {
    evt = (evt) ? evt : ((window.event) ? window.event : "")
    
    if(evt.which || evt.keyCode) {
        if ((evt.which == 13) || (evt.keyCode == 13)) {

			var _targetObj = document.getElementById(targetEvent);
			if (_targetObj != null) {
				BrowserDetect.init();
				if (BrowserDetect.browser == "Explorer") {
					event.returnValue = false;
					event.cancel = true;
				}
	       
				_targetObj.focus();
				_targetObj.click();
			}
			
            return false;
        }
    }
    else
        return true;
}

function OpenDialog(url, name, w, h, enableVerticalScrollbars, enableToolbars){
	var left = (screen.width/2) - w/2;
	var top = (screen.height/2) - h/2;
	var s = 'no';
	var _enableToolbars = 'no';

	if (enableVerticalScrollbars)
		s = 'yes';
	
	if (enableToolbars)
		_enableToolbars = 'yes';
		
	
	if ((name = window.open(url,name,'toolbar=' + _enableToolbars + ',location=' + _enableToolbars + ',directories=' + _enableToolbars + ',status='+s+',menubar=no,scrollbars='+s+',resizable=yes,copyhistory=no,width='+w+',height='+h+',left='+left+',top='+top+',screenX='+left+',screenY='+top)) != null)
	{
		if (window.focus) 
			name.focus();
	}
}


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);
	}
}
function hidePopUp(divHide,divHide1)
{	
	var HideDiv;
	HideDiv = document.getElementById(divHide);
	HideDiv.style.display = "none";
	var HideDiv1;
	HideDiv1 = document.getElementById(divHide1);
	HideDiv1.style.display = "none";
}
window.onerror = null;
