﻿// JScript File

// phong.nguyen have modified this code.

var offsetxpoint = -60; //Customize x offset
var offsetypoint = 20; //Customize y offset
var ie = document.all;
var ns6 = document.getElementById && !document.all;

String.prototype.trim = function(){
    return this.replace(/^\s*/,"").replace(/\s*$/,"");
}

function ietruebody()
{
	return (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
}
//Should use this function
function SetupCalendar(strInputField, strButton, strCulture)
{
    var formatDate;
    
    strCulture = strCulture.trim().toLowerCase();
    switch(strCulture)
    {
        case "1":
            formatDate = "%d/%m/%Y";
        break;
        case "2":
            formatDate = "%m/%d/%Y";
        break;
        default:
            formatDate = "%d/%m/%Y";
        break;
    }
    
    Calendar.setup({
      inputField    : strInputField,
      ifFormat      : formatDate,
      daFormat      : formatDate,
      button        : strButton,  
      align         : "Lr" 
    });
    
    //Gets position of the pointer.
    document.onmousemove = PositionCalendar;
}

function setupVNCalendar(strInputField, strButton) 
{   
    Calendar.setup({
      inputField    : strInputField,
      ifFormat      : "%d/%m/%Y",
      daFormat      : "%d/%m/%Y",   
      button        : strButton,  
      align         : "Lr" 
    });
}

function showCalendar(strInputField, strButton)
{
    var pos = findPos(document.getElementById(strButton));
    Calendar.setup({
      inputField    : strInputField,
      ifFormat      : "%d/%m/%Y",
      daFormat      : "%d/%m/%Y",   
      button        : strButton,  
      align         : "Lr",
      position      : new Array(pos.X, pos.Y)      
    });    
}

function findPos(obj)
{
    this.X = obj.offsetLeft;
    this.Y = obj.offsetTop;
    while(obj.offsetParent)
    {
        this.X=this.X+obj.offsetParent.offsetLeft;
        this.Y=this.Y+obj.offsetParent.offsetTop;
        if(obj==document.getElementsByTagName('body')[0])
        {
            break
        }
        else
        {
            obj=obj.offsetParent;
        }
    }
    return this
}
function PositionCalendar(e)
{	    
	var curX = (ns6) ? e.pageX : event.clientX+ietruebody().scrollLeft;
	var curY = (ns6) ? e.pageY : event.clientY+ietruebody().scrollTop;
	
	//Find out how close the mouse is to the corner of the window
	var rightedge  = ie&&!window.opera ? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20;
	var bottomedge = ie&&!window.opera ? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20;

	var leftedge = (offsetxpoint<0) ? offsetxpoint*(-1) : -1000;

	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge < 50)
	{
		//move the horizontal position of the menu to the left by it's width
		calX = ie ? ietruebody().scrollLeft + event.clientX-50 : window.pageXOffset + e.clientX - 50;
	}
	else if (curX < leftedge)
	{
		calX = "5";
	}
	else
	{
		//position the horizontal position of the menu where the mouse is positioned
		calX = curX + offsetxpoint;
	}

	//same concept with the vertical position
	if (bottomedge < 50)
	{
		calY = ie ? ietruebody().scrollTop + event.clientY-50 - offsetypoint : window.pageYOffset + e.clientY - 50 - offsetypoint;
	}
	else
	{
		calY = curY + offsetypoint;
	}
}