﻿// JScript File

// constants to define the title of the alert and button text.
var ALERT_TITLE = "Alert!";
var ALERT_BUTTON_TEXT = "Ok";
var ALERT_CANCEL_TEXT = "Cancel";
// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
if(document.getElementById) {
	window.alert = function(txt) {
		 return createCustomAlert(txt, false);
	}
}

if(document.getElementById) {
	window.doconfirm = function(txt, oktext, cantext) {
	   
		 return createCustomAlert(txt, true, false, oktext, cantext);
	}
}

if(document.getElementById) {
	window.dowait = function(txt) {
		 return createCustomAlert(txt, false, true);
	}
}

if(document.getElementById) {
	window.getEmoticon = function(txt) {
		 return createEmoticon(e);
	}
}
function  createEmoticon(e)
{
    d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "emoBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	//if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.top = document.documentElement.scrollTop + "px";
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode("Insert Emoticon"));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.id = "emoticon";
	for(var i = 1 ; i <= 39 ; i++)
	{
	    var img = d.createElement("img");
	    img.src = "../assets/emoticon/emotion-"+i+".gif";
	    img.onclick  = function()
	    {
	        e.InsertHtml('<img src="' +  this.src +'" />');
	        removeCustomAlert();
	    }
	    msg.appendChild(img);
	    if(i % 8== 0)
	    {
	        msg.appendChild(d.createElement("br"));
	    }
	}

	div = alertObj.appendChild(d.createElement("div"));
	div.id = "btnContainer";
	btn = div.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	var ok = 'Cancel'
	btn.appendChild(d.createTextNode(ok));
	btn.href = "#";
	btn.className =  'single';
     if(btn)
	btn.onclick = function() {
	 removeCustomAlert();
	 if(obj.onclick)
	 obj.onclick();
	 return false; 
	 
	 }
	 
	obj = {};
	alertObj.style.backgroundImage = '';
	return obj;
}
function createCustomAlert(txt, isConfirm, wait, oktext, cantext) {
	// shortcut reference to the document object
	d = document;

	// if the modalContainer object already exists in the DOM, bail out.
	if(d.getElementById("modalContainer")) return;

	// create the modalContainer div as a child of the BODY element
	mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
	mObj.id = "modalContainer";
	 // make sure its as tall as it needs to be to overlay all the content on the page
	mObj.style.height = document.documentElement.scrollHeight + "px";

	// create the DIV that will be the alert 
	alertObj = mObj.appendChild(d.createElement("div"));
	alertObj.id = "alertBox";
	// MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
	if(d.all && !window.opera) alertObj.style.top = document.documentElement.scrollTop + "px";
	// center the alert box
	alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";

	// create an H1 element as the title bar
	h1 = alertObj.appendChild(d.createElement("h1"));
	h1.appendChild(d.createTextNode(ALERT_TITLE));

	// create a paragraph element to contain the txt argument
	msg = alertObj.appendChild(d.createElement("p"));
	msg.appendChild(d.createTextNode(txt));

	div = alertObj.appendChild(d.createElement("div"));
	div.id = "btnContainer";
	
	if(!wait)
	{
	// create an anchor element to use as the confirmation button.
	btn = div.appendChild(d.createElement("a"));
	btn.id = "closeBtn";
	var ok = oktext ? oktext : ALERT_BUTTON_TEXT
	btn.appendChild(d.createTextNode(ok));
	btn.href = "#";
	btn.className = isConfirm ? 'double' : 'single';
	}
	
	var btn2;
	if(isConfirm)
	{
		btn2 = div.appendChild(d.createElement("a"));
		btn2.id = "cancelBtn";
		var can = cantext ? cantext : ALERT_CANCEL_TEXT
		btn2.appendChild(d.createTextNode(can));
		btn2.href = "#";
	}
	// set up the onclick event to remove the alert when the anchor is clicked
	obj = {};

if(!wait)
{
    if(btn)
	btn.onclick = function() {
	 removeCustomAlert();
	 if(obj.onclick)
	 obj.onclick();
	 return false; 
	 
	 }
	 }
	
	if(btn2)
	btn2.onclick = function()  { 
	removeCustomAlert();
	if(obj.oncancel)
	obj.oncancel();
	return false; }
	
	return obj;
}

// removes the custom alert from the DOM
function removeCustomAlert() {
	document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

