﻿
//------------------------------------------------------------------------------

function Alert_ShowMessage(prmMessage)
{
	var arrMessage = new Array();
	//var nodList;
	//var nodTemplate;
	var strType;
	var strText;
	var strMessage = "";
	var intLength = 0;
	
	//if(!Dialog_IsEnabled())
	//{
		if(typeof(prmMessage) == 'string')
		{
			arrMessage.push(prmMessage);
		}
		else
		{
			arrMessage = prmMessage;
		}

		//document.getElementById("DialogBody").lastChild.innerHTML = document.getElementById("MessageTemplate").innerHTML;
		//var nodTemplate = document.getElementById("DialogBody").lastChild.firstChild.firstChild;
		//var nodList = document.getElementById("DialogBody").lastChild.firstChild.lastChild;
		//ctlFocusedPriorDialog = false;

		for(X = 0; X < arrMessage.length; X++)
		{
			strType = arrMessage[X].substr(0, 3);
			strText = arrMessage[X].substr(4);
			switch (strType.toUpperCase())
			{
				case "INF":
				{
					//nodList.appendChild(nodTemplate.childNodes.item(0).cloneNode(true));
					strMessage = strMessage + strText + "\n"
					break;
				}
				case "WAR":
				{
					//nodList.appendChild(nodTemplate.childNodes.item(1).cloneNode(true));
					strMessage = strMessage + strText + "\n"
					break;
				}
				case "ERR":
				{
					//nodList.appendChild(nodTemplate.childNodes.item(2).cloneNode(true));
					//ctlFocusedPriorDialog = ctlFocused;
					strMessage = strMessage + strText + "\n"
					break;
				}
			}
			//nodList.lastChild.lastChild.innerHTML = strText;
			intLength = intLength + strText.length;
		}
		/*
		with(document)
		{
			getElementById("DialogTitle").innerHTML = title;
			getElementById("Mask").style.display = "block";
			if(intLength / 50 > 8)
			{
				getElementById("Dialog").className = "DialogMessageLarge";
				getElementById("DialogBody").className = "DialogBodyMessageLarge";
			}
			else
			{
				getElementById("Dialog").className = "DialogMessage";
				getElementById("DialogBody").className = "DialogBodyMessage";
			}
			getElementById("Dialog").style.display = "block";
			getElementById("Dialog").firstChild.focus();
			if(navigator.userAgent.indexOf("MSIE") != -1)
			{
				getElementById("DialogBody").lastChild.scrollIntoView();
			}
			else
			{
				getElementById("DialogBody").firstChild.scrollIntoView();
			}
		}
			*/
		alert(strMessage);	
	//}
}

//------------------------------------------------------------------------------

	var strApplicationPath = "/SPteam";
	var intActiveDHTMLHTTPRequestCount = 0;

//------------------------------------------------------------------------------

function DHTMLHTTPRequest()
{
	this.Callback = ""
	this.Name = "DHTMLHTTPRequest_" + Math.random();
	this.RootNode = document.getElementById("DHTMLHTTPRequest").contentWindow.document.getElementById("DHTMLHTTPRequest").appendChild(document.getElementById("DHTMLHTTPRequest").contentWindow.document.createElement("div"));
	this.RootNode.innerHTML = document.getElementById("DHTMLHTTPRequest").contentWindow.document.getElementById("DHTMLHTTPRequest").firstChild.innerHTML;
	this.RootNode.id = "ID_" + Math.random();
	this.Form = this.RootNode.getElementsByTagName("form").item(0);

	this.AddParameter = DHTMLHTTPRequest_AddParameter;
	this.Open = DHTMLHTTPRequest_Open;
	this.Send = DHTMLHTTPRequest_Send;
}

//------------------------------------------------------------------------------

function DHTMLHTTPRequest_AddParameter(prmName, prmValue)
{
	with(this.RootNode.getElementsByTagName("form").item(0))
	{
		appendChild(firstChild.cloneNode(true));
		lastChild.innerHTML = "<textarea name=\"" + prmName.replace(/"/, "&quot;") + "\">" + prmValue + "</textarea>"
	}
}

//------------------------------------------------------------------------------

function DHTMLHTTPRequest_Open(prmMethod, prmURL)
{
	with(this.Form)
	{
		method = prmMethod;
		action = strApplicationPath + "/" + prmURL;
		target = this.Name;
	}
}

//------------------------------------------------------------------------------

function DHTMLHTTPRequest_Send()
{
intActiveDHTMLHTTPRequestCount++;
var strCallback = (this.Callback == "") ? "" : "parent." + this.Callback.replace(/document\./, "parent.document.").replace(/\);/, "") + ", this.contentWindow.document);";
if(navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Opera" || navigator.appVersion.indexOf("Safari") != -1 || navigator.appVersion.indexOf("Chrome") != -1)
{
	this.RootNode.lastChild.innerHTML = "<iframe name=\"" + this.Name + "\" src=\"" + strApplicationPath + "/Common/Component/WebControls/Blank.html\" onload=\"if(this.contentWindow.location.pathname.slice(-11) != '/Blank.html' && this.contentWindow.location.pathname != '/blank') { try { " + strCallback + " } catch(e) { alert(e) ;} finally { this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); parent.intActiveDHTMLHTTPRequestCount--; };}\"></iframe>";
}
else
{
	this.RootNode.lastChild.innerHTML = "<iframe name=\"" + this.Name + "\" onload=\"try { " + strCallback + " } catch(e) { } finally { setTimeout('document.getElementById(' + String.fromCharCode(34) + 'DHTMLHTTPRequest' + String.fromCharCode(34) + ').removeChild(document.getElementById(' + String.fromCharCode(34) + this.parentNode.parentNode.id + String.fromCharCode(34) + '))', 0); parent.intActiveDHTMLHTTPRequestCount--; }\"></iframe>";
}
this.Form.submit();
}

//------------------------------------------------------------------------------

function DisplayMessage(doc)
{
	var colObject = doc.getElementsByName("OBJECT");
	var colType = doc.getElementsByName("MESSAGE_TYPE");
	var colText = doc.getElementsByName("MESSAGE_TEXT");
	var intMinMessageType = 3;
	var arrMessage = new Array();

	var X;
	var ctl;
	var str = "";

	for(X = 0; X < colType.length; X++)
	{
		if(colType[X].value == "1")
		{
			if(colObject.length > 0)
			{
				ctl = eval(colObject[X].value);
				if(ctl)
				{
					ctl.className = "ControlError";
				}
			}
		}
		if(colType[X].value < intMinMessageType)
		{
			intMinMessageType = colType[X].value;
		}
		if(colText[X].value != "")
		{
			switch (colType[X].value)
			{
				case "1":
				{
					str = "ERR:";
					break;
				}
				case "2":
				{
					str = "WAR:";
					break;
				}
				case "3":
				{
					str = "INF:";
				}
			}
			str += colText[X].value;
			arrMessage.push(str);
		}
	}
	if(intMinMessageType == 1)
	{
		Mask_Disable();
	}
	if(arrMessage.length > 0)
	{
		Alert_ShowMessage(arrMessage);
	}
	return intMinMessageType
}

//------------------------------------------------------------------------------

function FormBlock_Reset(ctl)
{
	var col;
	var col2
	var X;
	var Y;

	col = ctl.getElementsByTagName("textarea");
	for(X = 0; X < col.length; X++)
	{
		SetValue(col.item(X), col.item(X).defaultValue);
	}

	col = ctl.getElementsByTagName("select");
	for(X = 0; X < col.length; X++)
	{
		col.item(X).selectedIndex = -1;
		col2 = col.item(X).options;
		for(Y = 0; Y < col2.length; Y++)
		{
			if(col2.item(Y).defaultSelected)
			{
				col.item(X).selectedIndex = Y;
				break;
			}
		}
	}

	col = ctl.getElementsByTagName("input");
	for(X = 0; X < col.length; X++)
	{
		switch(col.item(X).type)
		{
			case "checkbox":
			{
				col.item(X).checked = col.item(X).defaultChecked;
				break;
			}
			case "hidden":
			{
				SetValue(col.item(X), col.item(X).defaultValue);
				break;
			}
			case "radio":
			{
				col.item(X).checked = col.item(X).defaultChecked;
				break;
			}
			case "text":
			{
				SetValue(col.item(X), col.item(X).defaultValue);
				break;
			}
		}
	}

}

//------------------------------------------------------------------------------

	function GenerateCaptchaImage()
	{
		if (!Mask_IsEnabled())
		{
			Mask_Enable();			
			var http = new DHTMLHTTPRequest();
			with(http)
			{
				Open("GET", "Pages/Main/CaptchaImage.aspx");
				Callback = "GenerateCaptchaImage_Callback(0);";
				Send();
			}
			document.getElementById("CaptchaImageBlock").innerHTML = "";
		}
		else
		{
			Mask_QueueAdd("GenerateCaptchaImage()");
		}
	}

//------------------------------------------------------------------------------
	
	function GenerateCaptchaImage_Callback(prm, doc)
	{
		document.getElementById("CaptchaImageBlock").innerHTML = doc.body.innerHTML;
		Mask_Disable();
	}

//------------------------------------------------------------------------------

	function Logout()
	{
		if (!Mask_IsEnabled())
		{
			Mask_Enable();			
			var http = new DHTMLHTTPRequest();
			with(http)
			{
				Open("POST", "Pages/Login/ProcessLogout.aspx");
				Callback = "Logout_Callback(0);";
				Send();
			}
		}
		else
		{
			Mask_QueueAdd("Logout()");
		}
	}
	
	function Logout_Callback(prm, doc)
	{
		Mask_Disable();	
	}

//------------------------------------------------------------------------------

	var arrMaskQueue = new Array();

//------------------------------------------------------------------------------

	function Mask_IsEnabled()
	{
		if(document.getElementById("Mask"))
		{
			return (document.getElementById("Mask").style.display == "block");
		}
		else
		{
			return false;
		}
	}

//------------------------------------------------------------------------------

	function Mask_Disable()
	{
		if(document.getElementById("Mask"))
		{
			document.getElementById("Mask").style.display = "none";
		}
		if(arrMaskQueue.length > 0)
		{
			eval(arrMaskQueue.shift());
		}
	}

//------------------------------------------------------------------------------

	function Mask_Enable()
	{
		if(document.getElementById("Mask"))
		{
			document.getElementById("Mask").style.display = "block";
		}
	}

//------------------------------------------------------------------------------

	function Mask_QueueAdd(prmCall)
	{
		arrMaskQueue.push(prmCall);
	}

//------------------------------------------------------------------------------

	function ValidateCommentForm()
	{
		var msg = "";
		var str = "";
		
		with (document)
		{			
			//alert ('fee, fie, foe, fum'.replace (/\s+/g, ''));
			//str = getElementsByName("AUTHOR_NAME").item(0).value.replace(/\s+/g, '');
			if (getElementsByName("AUTHOR_NAME").item(0).value.replace(/\s+/g, '') == "")
			{	
				msg = msg + Message_MissingAuthor;
			}
			
			if (getElementsByName("COMMENT_TEXT").item(0).value.replace(/\s+/g, '') == "")
			{
				msg = msg + Message_MissingCommentText;			
			}

			if (getElementsByName("CAPTCHA_CODE").item(0).value.replace(/\s+/g, '') == "")
			{
				msg = msg + Message_MissingCaptchaCode;			
			}

			if (!msg == "")
			{
				alert(msg);
				return false;
			}
			else
			{
				return true;
			}
		}
	}	

//------------------------------------------------------------------------------

	function ProcessLogin()
	{
		if (!Mask_IsEnabled())
		{
			Mask_Enable();			
			var http = new DHTMLHTTPRequest();
			with(http)
			{
				Open("POST", "Pages/Login/ProcessLogin.aspx");
				Callback = "ProcessLogin_Callback(0);";
				AddParameter("USER", document.getElementsByName("USER").item(0).value);
				AddParameter("PASSWORD", document.getElementsByName("PASSWORD").item(0).value);
				Send();
			}
		}
		else
		{
			Mask_QueueAdd("ProcessLogin()");
		}
	}
	
//------------------------------------------------------------------------------

	function ProcessLogin_Callback(prm, doc)
	{
		if(DisplayMessage(doc) > 1)
		{
			Mask_Disable();
			document.getElementById("LoginForm").submit();
		}
	}

