//// version: 1.0002.00

/// Namespace
KeystoneIS = new Object();

KeystoneIS.Validation = function()
{
	this.IsDate = function(dateStr)
    {
		var datePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2}|\d{4})$/;
		var matchArray = dateStr.match(datePattern);
		
		//Check valid format
		if (matchArray == null) { return false; }
		
		month = matchArray[1];
		day   = matchArray[3];
		year  = matchArray[5];
		
		// check month range
		if (month < 1 || month > 12) { return false; }
		
		//Check day range
		if (day < 1 || day > 31) { return false; }
		
		//Check months with 30 days
		if ((month==4 || month==6 || month==9 || month==11) && day>30) { return false; }
		
		//Check Feb days
		if (month == 2) {
		var leapYr = (year%4 == 0 && (year%100 != 0 || year%400 == 0));
		if (day > 29 || (day>28 && !leapYr)) { return false; }
		}
		
		return true;
    }

    this.IsTime = function(timeStr)
    {
        var TimePattern = /^[0-1]?[0-9]{1}:[0-5]{1}[0-9]{1} (AM|PM|am|pm)$/;
        var matchArray = timeStr.match(TimePattern);

        //Check valid format
        if (matchArray == null) { return false; }

        return true;
    }

    this.IsFloat = function(number)
    {
        var Pattern = /^([+-]?(((\d+(\.)?)|(\d*\.\d+))([eE][+-]?\d+)?))$/;
        var matchArray = number.match(Pattern);

        //Check valid format
        if (matchArray == null) { return false; }

        return true;
    }
}

/// Drag and Drop class
KeystoneIS.DragAndDrop = function()
{
    _GetMouseOffset = function(target, ev)
    {
        ev = ev || window.event;

        var docPos = _GetPosition(target);
        var mousePos = _MouseCoords(ev);
        return { x: mousePos.x - docPos.x, y: mousePos.y - docPos.y };
    }

    _GetPosition = function(e)
    {

        var left = 0;
        var top = 0;

        while (e.offsetParent)
        {
            left += e.offsetLeft - e.scrollLeft;
            top += e.offsetTop - e.scrollTop;
            e = e.offsetParent;
        }

        left += e.offsetLeft;
        top += e.offsetTop;

        return { x: left, y: top };
    }

    _MouseCoords = function(ev)
    {
        if (!document.body.scrollTop)
        {
            var ScrollLeft = document.documentElement.scrollLeft;
            var ScrollTop = document.documentElement.scrollTop;
        }
        else
        {
            var ScrollLeft = document.body.scrollLeft;
            var ScrollTop = document.body.scrollTop;
        }

        if (ev.pageX || ev.pageY)
        {
            return { x: ev.pageX, y: ev.pageY };
        }
        return {
            x: ev.clientX + ScrollLeft - document.body.clientLeft,
            y: ev.clientY + ScrollTop - document.body.clientTop
        };
    }

    var _DropTargets = [];

    this.AddDropTarget = function(dropTarget)
    {
        _DropTargets.push(dropTarget);
    }

    var CloneIndex = 0;

    _DragStart = function(ev)
    {
        ev = ev || window.event;

        var Target = ev.srcElement || ev.target;
        var Clone = null;

        while (typeof (Target.tagName) != 'undefined' && Target.tagName.toLowerCase() != "body" && dragObject == null)
        {
            if (Target.getAttribute("draggable"))
            {
                if (Target.getAttribute("draggable").toLowerCase() == "true")
                {
                    _DragFrom = Target.parentNode;

                    /// All dragged objects are actually cloned.
                    Clone = Target.cloneNode(true);
                    Clone.id = "Clone" + ++CloneIndex + "_" + Target.id;
                    Clone.style.display = "none";
                    document.body.appendChild(Clone);


                    mouseOffset = _GetMouseOffset(Target, ev);

                    if (Target.getAttribute("clone"))
                    {
                        /// if clone is true remove the original
                        if (Target.getAttribute("clone").toLowerCase() != "true")
                        {
                            //Target.style.display = "none";
                            Target.parentNode.innerHTML = "";
                        }
                    }

                    dragObject = Clone;
                }

                /// When the dragObject/Clone was appended to the body its position changed
                /// Reposition dragObject under the cursor.
                _Drag(ev);
            }

            if (dragObject == null)
            {
                Target = Target.parentNode;
            }

        }
        return false;
    }

    _Drag = function(ev)
    {
        ev = ev || window.event;
        mousePos = _MouseCoords(ev);

        if (dragObject)
        {
            dragObject.style.position = 'absolute';
            dragObject.style.display = "block";
            if (mouseOffset != null)
            {
                dragObject.style.top = (mousePos.y - mouseOffset.y) + "px";
                dragObject.style.left = (mousePos.x - mouseOffset.x) + "px";
            }
            return false;
        }

    }

    var _OnDrop = null;

    this.SetOnDrop = function(onDrop)
    {
        _OnDrop = onDrop
    }

    var _LastDropped = null;
    this.GetLastDropped = function()
    {
        return _LastDropped;
    }

    var _LastDropTarget = null;
    this.GetLastTarget = function()
    {
        return _LastDropTarget;
    }
    this.DragTo = function()
    {
        return _LastDropTarget;
    }

    var _DragFrom = null;
    this.DragFrom = function()
    {
        return _DragFrom;
    }

    _DragStop = function(ev)
    {
        ev = ev || window.event;

        if (dragObject != null)
        {
            var mousePos = _MouseCoords(ev);

            var Target = null;

            /// Find the target under the mouse cursor
            for (var i = 0; i < _DropTargets.length; i++)
            {
                var curTarget = _DropTargets[i];
                if (curTarget != null)
                {
                    var targPos = _GetPosition(curTarget);
                    var targWidth = parseInt(curTarget.offsetWidth);
                    var targHeight = parseInt(curTarget.offsetHeight);

                    if (
	            (mousePos.x > targPos.x) &&
	            (mousePos.x < (targPos.x + targWidth)) &&
	            (mousePos.y > targPos.y) &&
	            (mousePos.y < (targPos.y + targHeight)))
                    {
                        // dragObject was dropped onto curTarget!
                        Target = curTarget;
                    }
                }
            }

            if (Target == null)
            {
                /// missed the dropzone
                //alert(dragObject.getAttribute("clone") != null && dragObject.getAttribute("clone").toLowerCase() == "true");
                if (dragObject.getAttribute("clone") != null && dragObject.getAttribute("clone").toLowerCase() == "true")
                {
                    /// A clone, remove it.
                    dragObject.parentNode.removeChild(dragObject);
                }
                else
                {

                    /// Not a clone, put it back.
                    _DragFrom.appendChild(dragObject);
                    with (dragObject.style)
                    {
                        position = "relative";
                        top = "0px";
                        left = "0px";
                    }
                }
            }
            else
            {
                _LastDropped = dragObject;
                _LastDropTarget = Target;
                _OnDrop();
            }

            dragObject = null;
        }
    }

    var mousePos;
    var dragObject = null;
    var mouseOffset = null;

    document.onmousedown = _DragStart;
    document.onmousemove = _Drag;
    document.onmouseup = _DragStop;
}

/// YUI Namespace - Yahoo User Interfaces
KeystoneIS.YUI = new Object();

	/// a helper object that supports multiple overlays on a single page
KeystoneIS.YUI.Overlay = function()
{
    /// Sample call to show an overlay
    ///ShowOverlay(1, 300, 'tl', 'tl')
    var OverlayCollection = new Array();

    /// <summary>
    /// Show an overlay
    /// </summary>
    /// <param name="overlayId">The overlay Id</param>
    /// <param name="overlayWidth">The width of the overlay</param>
    /// <param name="position">The corner of the overlay that is to be anchored</param>
    /// <param name="positionTo">The corner of the anchor that the overlay is to be anchored</param>
    this.Show = function(overlayId, overlayWidth, position, positionTo)
    {
        /// Only need to instantiate the overlay once
        //if (OverlayCollection[overlayId] == null)
        //{
        OverlayCollection[overlayId] = new YAHOO.widget.Overlay("overlay" + overlayId, { visible: false });
        OverlayCollection[overlayId].cfg.setProperty("width", overlayWidth + "px");
        //}

        /// The anchor may have moved, reset the postion
        OverlayCollection[overlayId].cfg.setProperty("context", ["overlay" + overlayId + "-anchor", position, positionTo]);

        /// Show the overlay
        OverlayCollection[overlayId].show();
    }

    /// <summary>
    /// Hide an overlay
    /// </summary>
    /// <param name="overlayId">The overlay Id</param>
    this.Hide = function(overlayId)
    {
        OverlayCollection[overlayId].hide()
    }


    /* 
    - The overlay DIV Id must use the naming convention: 'overlay' + overlayId
    - The overlay anchor naming convetion is: 'overlay' + overlayId + '-anchore'
		
			**SAMPLE OVERLAY**
		
				<div class="yui-skin-sam">
    <div id="overlay1" style="visibility:hidden; background-color:white;border:solid 1px black;">
    <div class="hd"></div>
    <div class="bd" style="padding:10px;">
    <div style="padding-bottom:10px;"><b>Edit Title</b></div>
    <asp:TextBox id="uiTitleEdit" width="275px" runat="server"></asp:TextBox>
    <br />
    <br />
    <div style="position:relative; left:180px;">
    <span onclick="YuiOverlayHelper.Hide(1)" class="edit">CANCEL</span>&nbsp;&nbsp;&nbsp;<asp:Button ID="uiTitleSave" OnClientClick="YuiOverlayHelper.Hide(1)" OnClick="uiEdit_Click" CommandName="Title" Text="Save" runat="server" />
    </div>
    </div>
    </div>
    </div>
		

    **SAMPLE ANCHOR**
			
    <span class="edit" id="overlay1-anchor" onclick="YuiOverlayHelper.Show(1, 300, 'tl', 'tl')">EDIT</span>
		
		*/
}


