function validateRequiredField(target, message)
{
    Sys.UI.DomElement.removeCssClass(target, 'validatorCalloutHighlight');
        
    if (_popupBehavior)
    {
        _popupBehavior.dispose();
        _popupBehavior = null;
    }
    
    if ($(target + "_popupTable")!=null)
    {
        Element.remove($(target + "_popupTable"));
    }
    
    if (target.value == "")
    {
        CreateValidator(target.id, message);
        Sys.UI.DomElement.addCssClass((target), 'validatorCalloutHighlight');
        return false;
    }
    return true;
}

function validateRegExField(target, regexStatement, message)
{   
    Sys.UI.DomElement.removeCssClass(target, 'validatorCalloutHighlight');
        
    if (_popupBehavior)
    {
        _popupBehavior.dispose();
        _popupBehavior = null;
    }
    
    if ($(target + "_popupTable")!=null)
    {
        Element.remove($(target + "_popupTable"));
    }
    
    var value = target.value;    
    var re = new RegExp(regexStatement);
    
    if (value.match(re))
    {        
        return true;
    }
    else
    {        
        CreateValidator(target.id, message);
        Sys.UI.DomElement.addCssClass((target), 'validatorCalloutHighlight');
        return false;
    }
    
    return true;
}

function ShowValidator(target, message)
{
    Sys.UI.DomElement.removeCssClass(target, 'validatorCalloutHighlight');
        
    if (_popupBehavior)
    {
        _popupBehavior.dispose();
        _popupBehavior = null;
    }
    
    
    CreateValidator(target.id, message);
    Sys.UI.DomElement.addCssClass((target), 'validatorCalloutHighlight');
    return false;
}


function closeClickHandler()
{
    var targ;

    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    
    var ind = targ.id.indexOf('_close');
    var name = targ.id.substring(0, ind);
    
    Element.remove($(name));
}

var _popupBehavior;

function CreateValidator(target, errorMessage)
{                
    //
    // create the DOM elements
    //
    var elementToValidate = this._elementToValidate = $(target);
    var popupTableBody = document.createElement("tbody");
    var popupTableRow = document.createElement("tr");
    var calloutCell = document.createElement("td");
    var calloutTable = document.createElement("table");
    var calloutTableBody = document.createElement("tbody");
    var calloutTableRow = document.createElement("tr");
    var iconCell = document.createElement("td");
    var closeCell = document.createElement("td");
    var popupTable = this._popupTable = document.createElement("table");
    var calloutArrowCell = this._calloutArrowCell = document.createElement("td");
    var warningIconImage = this._warningIconImage = document.createElement("img");
    var closeImage = this._closeImage = document.createElement("img");
    var errorMessageCell = this._errorMessageCell = document.createElement("td");
    //
    // popupTable
    //
    if ($(target + "_popupTable")==null)
    {
        popupTable.id = target + "_popupTable";
        popupTable.cellPadding = 0;
        popupTable.cellSpacing = 0;
        popupTable.border = 0;
        popupTable.width = '185px';
        //popupTable.style.display = 'none';
        popupTable.style.position="absolute";
        //
        // popupTableRow
        //
        popupTableRow.vAlign = 'top';
        popupTableRow.style.height = "100%";
        //
        // calloutCell
        //
        calloutCell.width = 20;
        calloutCell.align = "right";
        calloutCell.style.height = "100%";
        calloutCell.style.verticalAlign = "top";
        //
        // calloutTable
        //
        calloutTable.cellPadding = 0;
        calloutTable.cellSpacing = 0;
        calloutTable.border = 0;
        calloutTable.style.height = "100%";
        //
        // _calloutArrowCell
        //
        calloutArrowCell.align = "right";
        calloutArrowCell.vAlign = "top";
        calloutArrowCell.style.fontSize = "1px";
        calloutArrowCell.style.paddingTop = "8px";
        //
        // iconCell
        //
        iconCell.width = 20;
        iconCell.style.borderTop = "1px solid black";
        iconCell.style.borderLeft = "1px solid black";
        iconCell.style.borderBottom = "1px solid black";
        iconCell.style.padding = "5px";
        iconCell.style.backgroundColor = "#E5F0F5";
        //
        // _warningIconImage
        //
        warningIconImage.border = 0;
        warningIconImage.src ="../../images/alert-large.gif";
        //
        // _errorMessageCell
        //
        errorMessageCell.style.backgroundColor = "#E5F0F5";
        errorMessageCell.style.fontFamily = 'verdana';
        errorMessageCell.style.fontSize = '10px';
        errorMessageCell.style.padding = "5px";
        errorMessageCell.style.borderTop = "1px solid black";
        errorMessageCell.style.borderBottom = "1px solid black";
        errorMessageCell.width = '100%';        
        errorMessageCell.innerHTML = errorMessage;
        //
        // closeCell
        //
        closeCell.style.borderTop = "1px solid black";
        closeCell.style.borderRight = "1px solid black";
        closeCell.style.borderBottom = "1px solid black";
        closeCell.style.backgroundColor ="#E5F0F5";
        closeCell.style.verticalAlign = 'top';
        closeCell.style.textAlign = 'right';
        closeCell.style.padding = '2px';
        //
        // closeImage
        //
        closeImage.src = "../../images/close.gif";
        closeImage.style.cursor = 'pointer';
        //closeImage.onClick = "Element.remove($(\""+popupTable.id+"\");";    
        closeImage.id = popupTable.id+"_close";
        
        $addHandler(closeImage, "click", closeClickHandler);
        
        
        //
        // Create the DOM tree
        //
        $(target).parentNode.appendChild(popupTable)
        
        popupTable.appendChild(popupTableBody);
        popupTableBody.appendChild(popupTableRow);
        popupTableRow.appendChild(calloutCell);
        calloutCell.appendChild(calloutTable);
        calloutTable.appendChild(calloutTableBody);
        calloutTableBody.appendChild(calloutTableRow);
        calloutTableRow.appendChild(calloutArrowCell);
        popupTableRow.appendChild(iconCell);
        iconCell.appendChild(warningIconImage);
        popupTableRow.appendChild(errorMessageCell);
        popupTableRow.appendChild(closeCell);
        closeCell.appendChild(closeImage);
        //
        // initialize callout arrow
        //
        var div = document.createElement("div");
        div.style.fontSize = "1px";
        div.style.position = "relative";
        div.style.left = "1px";
        div.style.borderTop = "1px solid black";
        div.style.width = "15px";
        calloutArrowCell.appendChild(div);        
        for(var i = 14; i > 0; i--)
        {
            var line = document.createElement("div");
            line.style.width = i.toString() + "px";
            line.style.height = "1px";
            line.style.overflow = "hidden";
            line.style.backgroundColor = "#E5F0F5";
            line.style.borderLeft = "1px solid black";
            div.appendChild(line);
        }              
     }
     else
     {
        popupTable = $(target + "_popupTable");
     }
        //$(target + '_popupTable').style.display="block";
        
        //setupPopup(target); AjaxControlToolkit.PositioningMode.Absolute,
        
        _popupBehavior = $create(
            AjaxControlToolkit.PopupBehavior, 
            {
                id : target.id+'PopupBehavior',
                positioningMode : AjaxControlToolkit.PositioningMode.Absolute,
                parentElement : $(target)
            }, 
            null, 
            null,
            popupTable);           
            


        _popupBehavior.set_x($common.getSize($(target)).width);
        _popupBehavior.show();
        //$(target + '_popupTable').show();
    
        if (_popupBehavior) 
        {
            _popupBehavior.onShow();            
        }
}