﻿/* ALERT BOX STYLE */

// constants to define the title of the alert and button text.
var ALERT_TITLE = 'System besked:';
var ALERT_BUTTON_TEXT = 'OK';

// constants to define the colors of the box
var boxTopOffset = '270px';
var boxLeftOffset = "64px";

var btnBdrClr = '#d9d9d9';
var btnBGClr = '#ffffff';
var btnTxtClr = '#333333';

var boxBdrClr = '#d9d9d9';
var boxBGClr = '#ffffff';
var boxTxtClr = '#333333';

// Custom string.replaceAll function.
String.prototype.replaceAll = function(pcFrom, pcTo) {
    var i = this.indexOf(pcFrom);
    var c = this;

    while (i > -1) {
        c = c.replace(pcFrom, pcTo);
        i = c.indexOf(pcFrom);
    }
    return c;
}

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
if (document.getElementById) {
    window.alert = function(txt) {
        createCustomAlert(txt);
    }
}

var tmpEnterHandle = null;

function createCustomAlert(txt) {
    // if the modalContainer object already exists in the DOM, bail out.
    if (document.getElementById('FonqiAlert')) return;

    // create the modalContainer div as a child of the BODY element
    var mObj = document.getElementById('main').appendChild(document.createElement('div'));
    mObj.id = 'FonqiAlert';
    mObj.setAttribute("class", "alert"); 
	mObj.style.zIndex = '1010';
    if (window.XMLHttpRequest) {
        mObj.style.position = 'fixed';
        mObj.style.top = boxTopOffset;
    }
    else {
        mObj.style.position = 'absolute';
        mObj.style.top = '0px';
        mObj.style.marginTop = boxTopOffset;
    }
    mObj.style.left = boxLeftOffset;
    mObj.style.width = '100%';

    // create the DIV that will be the alert 
    var alertObj = mObj.appendChild(document.createElement('div'));
    alertObj.id = 'alertBox';
    alertObj.style.width = '200px';
    alertObj.style.border = 'solid 1px ' + boxBdrClr;
    alertObj.style.margin = '0px auto 10px auto';
    alertObj.style.padding = '10px 10px 10px 10px';
    alertObj.style.backgroundColor = boxBGClr;
    //alertObj.style.backgroundImage = "url('/images/site/alertBG.jpg')";
    //alertObj.style.backgroundRepeat = 'no-repeat';

    // create a paragraph element to contain the txt argument
    var header = alertObj.appendChild(document.createElement('h2'));
    header.style.width = '100%';
    header.style.textAlign = 'center';
    header.style.font = 'bold 12px "Lucida Sans Unicode" , "Lucida Grande" , Arial, Helvetica, sans-serif';
    header.style.color = '#dd0000';
    header.style.margin = '0 0 3px 0';
    header.style.padding = '0';
    header.innerHTML = ALERT_TITLE;
    
    var msg = alertObj.appendChild(document.createElement('p'));
    msg.innerHTML = txt.replaceAll('\r\n', '<br />').replaceAll('\n', '<br />');
	msg.style.color = boxTxtClr;

    // create an anchor element to use as the confirmation button.
    var placementDiv = document.createElement("div");
    placementDiv.style.width = '48px';
    placementDiv.style.height = '22px';
    placementDiv.style.margin = '20px auto 5px auto';

    var btn = document.createElement("input");;
    btn.id = 'closeBtn';
    btn.setAttribute('type', 'button');
    btn.value = ALERT_BUTTON_TEXT;
    btn.style.width = '48px';
    btn.style.height = '22px';
    btn.style.border = 'solid 1px ' + btnBdrClr;
    btn.style.backgroundColor = btnBGClr;
    btn.style.cursor = 'pointer';
    btn.style.verticalAlign = 'middle';
    btn.style.textAlign = 'center';
    btn.style.color = btnTxtClr;
    btn.style.outline = '0';
    // set up the onclick event to remove the alert when the anchor is clicked
    btn.onclick = function() { removeCustomAlert(); return false; }
    placementDiv.appendChild(btn);
    alertObj.appendChild(placementDiv);
    tmpEnterHandle = document.onkeypress;
    document.onkeypress = null;
    document.getElementById('closeBtn').focus();
}

// removes the custom alert from the DOM
function removeCustomAlert() {
    document.getElementById('main').removeChild(document.getElementById('FonqiAlert'));
    if(tmpEnterHandle != null)
        document.onkeypress = tmpEnterHandle;
}
/*
function checkEnter(event){
        if (event.which || event.keyCode){
        if ((event.which == 13) || (event.keyCode == 13)){
            removeCustomAlert()
            return false;
        }
    }        
}*/