﻿//new version for use with formValidator.js

function errorBox(positionCode) {
    this.e = $('errorBox');
    this.mousePosX = 0;
	this.mousePosY = 0;
	this.numErrors = 0;
	this.pcode = positionCode;
	
	this.setOffsets(positionCode);
	this.init();
	
	eBoxControl = this;
}

errorBox.prototype.setOffsets = function(positionCode) {
    switch (positionCode) {
        case "tl":
            //top left
            this.xOffset = -this.e.offsetWidth;
	        this.yOffset = -this.e.offsetHeight;
            
            break;
            
        case "bl":
            //bottom left
            this.xOffset = -this.e.offsetWidth;
	        this.yOffset = 0;
            
            break;
        
        case "tr":
            this.xOffset = 0;
	        this.yOffset = -this.e.offsetHeight;
        
            break;
        
        case "br":
            this.xOffset = 0;
	        this.yOffset = 0;
        
            break;
            
        default:
            //defaults to bottom left
            this.xOffset = -this.e.offsetWidth;
	        this.yOffset = 0;
            
            break;
    }
}

errorBox.prototype.init = function() {
    if(b.mozilla) {
        dndMgr.registerDraggable( new Rico.Draggable('ebox','errorBox') );
    }
    
    cc = $('errorClose');
    cc.onclick = function(e) {
        //need to find away around using eBoxControl
        eBoxControl.close();
        return false;
    }
    
    this.hide();
}

errorBox.prototype.show = function(event) {
    this.e.style.display = "block";
    this.setOffsets(this.pcode);
    
    if (!b.mozilla) {
		this.mousePosX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		this.mousePosY = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	}
	if (b.mozilla) {
		this.mousePosX = event.clientX + window.scrollX;
		this.mousePosY = event.clientY + window.scrollY;
	}
    	
    this.e.style.top = this.mousePosY+ this.yOffset +'px';
    this.e.style.left = this.mousePosX+ this.xOffset +'px';
    
    if(!b.mozilla) {
        //hideSelects();
    }
    
}

errorBox.prototype.hide = function() {    
    this.e.style.top = -10000+'px';
    this.e.style.left = -10000+'px';
    
    if(!b.mozilla) {
        //showSelects();
    }
    
}

errorBox.prototype.close = function() {
    this.hide();
    this.clearErrors();
}

errorBox.prototype.addError = function(errorId,msg) {
    errorList = $("errorContainer");
    
    newError = document.createElement("div");
    newError.className = "error";
    newError.setAttribute("id",errorId);
    newError.innerHTML = msg;


    errorList.appendChild(newError);
    this.numErrors++;
}

errorBox.prototype.clearErrors = function() {
    errorList = $("errorContainer");
    errorList.innerHTML = "";
}

errorBox.prototype.removeError = function(errorId) {
    errorList = $("errorContainer");
    
    try {
        errorList.removeChild($(errorId));
        this.numErrors--;
        if (this.numErrors = 0) {
            this.close();
        }
   }
   catch(e) {}
}

function hideSelects() {
    sArray = document.getElementsByTagName("select");
    
    for(var x = 0; x < sArray.length; x++) {
        sArray[x].style.display = "none";
    }
}

function showSelects() {
    sArray = document.getElementsByTagName("select");
    
    for(var x = 0; x < sArray.length; x++) {
        sArray[x].style.display = "";
    }
}
