﻿var Br = new BrCheck()

function BrCheck() {
	this.VER = navigator.appVersion;
	this.AGENT = navigator.userAgent.replace(/[\/]/g, ' ');
	this.DOM = document.getElementById ? true : false;

	this.OP5 = this.AGENT.indexOf("Opera 5") > -1 ? true : false;
	this.OP6 = this.AGENT.indexOf("Opera 6") > -1 ? true : false;
	this.OP7 = this.AGENT.indexOf("Opera 7") > -1 ? true : false;
	this.OP8 = this.AGENT.indexOf("Opera 8") > -1 ? true : false;
	this.OP9 = this.AGENT.indexOf("Opera 9") > -1 ? true : false;
	this.OP = (this.OP5 || this.OP6 || this.OP7 || this.OP8 || this.OP9);

	this.IE4 = (document.all && !this.DOM && !this.OP) ? true : false;
	this.IE5 = (this.VER.indexOf("MSIE 5") > -1 && this.DOM && !this.OP) ? true : false;
	this.IE6 = (this.VER.indexOf("MSIE 6") > -1 && this.DOM && !this.OP) ? true : false;
	this.IE7 = (this.VER.indexOf("MSIE 7") > -1 && this.DOM && !this.OP) ? true : false;
	this.IE8 = (this.VER.indexOf("MSIE 8") > -1 && this.DOM && !this.OP) ? true : false;
	this.IE = (this.IE4 || this.IE5 || this.IE6 || this.IE7 || this.IE8);

	this.Gecko = this.AGENT.lastIndexOf('Gecko') > 0;
	this.NS4 = (document.layers && !this.DOM) ? true : false;
	this.NS7 = (this.DOM && parseInt(this.VER) >= 5 && this.AGENT.lastIndexOf('Netscape') < this.AGENT.lastIndexOf('7')) ? true : false;
	this.NS6 = (this.DOM && parseInt(this.VER) >= 5 && !this.NS7) ? true : false;
	this.NS = (this.NS4 || this.NS6 || this.NS7 || this.Gecko);

	return this;
}

// comes from prototype.js; this is simply easier on the eyes and fingers
function $(id) {
	var doc = document.getElementById(id);
	if (doc == 'undefined' || doc == null) {
		var docName = document.getElementsByName(id);
		if (docName != 'undefined' || doc != null || docName.length > 0)
			doc = docName;
	}
	return doc;
}

function isNotNull(object) {
	if (object != undefined && object != null && object != "")
		return true;
	else
		return false;
}

function cutUrlParameter(sURL, sParameter) {
	var nPosition = sURL.indexOf("&" + sParameter + "=")
	if (nPosition < 1) { nPosition = sURL.indexOf("?" + sParameter + "=") }
	if (nPosition > 0) {
		if (sURL.indexOf("&", nPosition + 1) > 0)
		{ return sURL.replace(sURL.substring(nPosition, sURL.indexOf("&", nPosition + 1)), "") }
		else
		{ return sURL.replace(sURL.substring(nPosition, sURL.length), "") }
	}
	return sURL;
}

function getUrlParameter(sURL, sParameter) {
	var nPosition = sURL.indexOf("&" + sParameter + "=")
	if (nPosition < 1) { nPosition = sURL.indexOf("?" + sParameter + "=") }
	if (nPosition > 0) {
		if (sURL.indexOf("&", nPosition + 1) > 0)
		{ return sURL.substring(nPosition + sParameter.length + 2, sURL.indexOf("&", nPosition + 1)) }
		else
		{ return sURL.substring(nPosition + sParameter.length + 2, sURL.length) }
	}
	return "";
}

function addUrlParameter(sURL, sParameter, sValue) {
	if (sURL.indexOf("?") < 1) {
		var lastChar = sURL.substring(sURL.length - 1, sURL.length);
		if (lastChar == "/" || lastChar == "\\")
			sURL = sURL.substring(0, sURL.length - 1);
		return sURL + "?" + sParameter + "=" + sValue;
	}
	else {
		return sURL + "&" + sParameter + "=" + sValue;
	}
}

function CloseBox(boxId) {
	if (window.top.document.getElementById(boxId) && window.top.document.getElementById('clona' + boxId)) {
		var clona1 = window.top.document.getElementById(boxId);
		var clona = window.top.document.getElementById('clona' + boxId);

		//		var opener = clona1.contents.opener;
		//		if(isNotNull(opener))
		//		{
		//			opener.body.focus();
		//		}

		window.top.document.body.removeChild(clona);
		window.top.document.body.removeChild(clona1);
	}
}

function GetOpener() {
	var frId = getUrlParameter(document.forms[0].action, "FameID");
	if (isNotNull(frId)) {
		if (window.top.document.getElementById(frId)) {
			var clona1 = window.top.document.getElementById(frId);
			var opener = clona1.contents.opener;
			return opener;
		}
	}
}

function CloseWindow() {
	var frId = getUrlParameter(document.forms[0].action, "FameID");
	if (isNotNull(frId)) {
		CloseBox(frId);
	}
	else {
		window.parent.close();
	}
}

function RefreshOpener() {
	var frId = getUrlParameter(document.forms[0].action, "FameID");
	if (isNotNull(frId)) {
		if (window.top.document.getElementById(frId)) {
			var clona1 = window.top.document.getElementById(frId);
			var opener = clona1.opener;
			if (isNotNull(opener)) {
				opener.location.reload();
			}
		}
	}
}

function RepleaceOpener(dir) {
	var frId = getUrlParameter(document.forms[0].action, "FameID");
	if (isNotNull(frId)) {
		if (window.top.document.getElementById(frId)) {
			var clona1 = window.top.document.getElementById(frId);
			var opener = clona1.opener;
			if (isNotNull(opener)) {
				var href = opener.location.href;
				href = cutUrlParameter(href, "Dir");
				href = addUrlParameter(href, "Dir", dir);
				opener.location.replace(href);
			}
		}
	}
}

// --- Events
function SetCommand(pCommandName) {
	if (pCommandName != null) {
		$('_CommandName').value = pCommandName;
	}
}

function ShowAlert(text, atrHeaderText, atrFooterText, atrCloseButtonTitle, atrWithHelp, atrHelpWindowHeaderText) {
	return show_alert_box(text, 300, 150, atrHeaderText, atrFooterText, atrCloseButtonTitle, atrWithHelp, atrHelpWindowHeaderText);
}
