var tag_id = '';
function SelectDate(x)
{
	tag_id = x;

	if(!document.getElementById(tag_id))
		alert('Tag ID does not exists');
	else
	{
		 D = document.getElementById(tag_id).value;
		 if(D)
			  D = D.split('-');
		 else
		 {
			  Dat = new Date();
			  D = new Array( Dat.getFullYear(),Dat.getMonth()+1,Dat.getDay());
		 }
		MakeDate(D[0],D[1],D[2]);
	}
}

function SetDate(Day, Month, Year)
{
    d = new Date();

    if(Day < 10)
    	Day = '0'+Day;

    if(Month < 10)
    	Month = '0'+Month;

    document.getElementById(tag_id).value =  Year+ '-' + Month + '-' + Day;
}
ReturnFunc = '';
function Calendar(iYear, iMonth, iDay, ContainerId, ClassName)
{
    MonthNames = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    //If no parameter is passed use the current date.
    oDate = new Date();
    Year = (iYear == null) ? oDate.getFullYear() : iYear;

    Month = (iMonth == null) ? oDate.getMonth() : iMonth - 1;

    while(Month < 0){Month += 12;Year--}
    while(Month >= 12){Month -= 12;Year++}
    Day = (iDay == null) ? 0 : iDay;
    oDate = new Date(Year, Month, 1);
    NextMonth = new Date(Year, Month + 1, 1);
    WeekStart = oDate.getDay();
    // Get the number of months in current month
    MonthDays = Math.round((NextMonth.getTime() - oDate.getTime()) / 86400000) + 1;
    // Check whether the Container Id is null
    if(ContainerId != null)
    {
        ContainerId = ContainerId;
        Container = document.getElementById(ContainerId);
        // If an element doesnot exists with the given ContainerId then create it
        if(!Container)
            document.write('<div id="' + ContainerId + '">&nbsp;</div>');
    }
    else
    {
        // Loop until a unique id is obtained for the container
        do
        {
            ContainerId = 'tblCalendar' + Math.round(Math.random() * 1000);
        }
        while(document.getElementById(ContainerId));
        // create an element with the new id
        document.write('<div id="' + ContainerId + '">&nbsp;</div>');
    }
    Container = document.getElementById(ContainerId);
    ClassName = (ClassName == null) ? 'tblCalendar' : ClassName;
    HTML = '<table class="' + ClassName + '" cellspacing="0">';
    // Title bar
    HTML += '<tr class="TitleBar"><td class="Nav"><a href="javascript:void(0)" onMouseDown="Calendar(' + Year + ', ' + Month + ', ' + Day+', \''+ContainerId+'\', \''+ClassName+'\');">&laquo;</a></td><td colspan="5" class="Title">' + MonthNames[Month] + ' ' + Year + '</td><td class="Nav"><a href="javascript:void(0)" onMouseDown="Calendar(' + Year + ', ' + (Month + 2) + ', ' + Day+', \''+ContainerId+'\', \''+ClassName+'\');">&raquo;</a></td></tr>';
    // Week Names
    HTML += '<tr class="WeekName"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>';
    HTML += '<tr class="Days">';
    // Fill the previous month days with space
    for(DayCounter = 0; DayCounter < WeekStart; DayCounter++)
    {
        HTML += '<td>&nbsp;</td>';
    }
    // Populate current month
    for(DayCounter = 1; DayCounter < MonthDays; DayCounter++)
    {
        if((DayCounter + WeekStart) % 7 == 1) HTML += '<tr class="Days">';
        if(DayCounter == Day)
            HTML += '<td class="SelectedDay"><a href="javascript:ReturnDate(' + DayCounter + ')">' + DayCounter + '</a></td>';
        else HTML += '<td><a href="javascript:ReturnDate(' + DayCounter + ')">' + DayCounter + '</a></td>';
        if((DayCounter + WeekStart) % 7 == 0) HTML += '</tr>';
    }
    // Fill the next month days with space
    for(j = (42 - (MonthDays + WeekStart)), DayCounter = 0; DayCounter <= j; DayCounter++)
    {
        HTML += '<td>&nbsp;</td>';
        if((j - DayCounter) % 7 == 0) HTML += '</tr>';
    }
    HTML += '</table>';

    Container.innerHTML = HTML;
    // Returns Id of the element containing the calendar
    return ContainerId;
}
function ReturnDate(Day)
{
    document.getElementById('calendar').style.visibility = 'hidden';
    document.getElementById('close_calendar_button').style.visibility = 'hidden';
	document.getElementById('calendar_div').style.left = '-1000px';
	document.getElementById('calendar_div').style.top = '-1000px';

    SetDate(Day, Month+1, Year);
}

function MakeDate(iYear, iMonth, iDay, fn)
{
    var o  = document.getElementById('calendar_div');
    getMyXY(o);
    document.getElementById('calendar').style.visibility = 'visible';
    document.getElementById('calendar').style.visibility = 'visible';
    document.getElementById('close_calendar_button').style.visibility = 'visible';

    D = new Date();
    Year = (typeof(iYear) != 'undefined') ? iYear : D.getFullYear();
    Month = (typeof(iMonth) != 'undefined') ? iMonth : D.getMonth();
    Day = (typeof(iDay) != 'undefined') ? iDay : D.getDate();
    ReturnFunc = fn;
    id = Calendar(Year, Month, Day, 'calendar', 'CalendarRed');
}

function addBank()
{
	var b = document.getElementById('bank_name').value;
	if(b == '')
		alert('Please enter the banks name!!');
	else
	{
		if(confirm('Add bank : '+b))
			window.location = 'index.php?rc_reg_client_property++++'+b;
	}
}

function formatnumber(o)
{
  var number = o.value;
  if(number.length == 10)
	number = number.substring(0,3)+" "+number.substring(3,6)+" "+number.substring(6,10);

  o.value = number;
}

function closeCalendar()
{
	document.getElementById('calendar_div').style.left = '-1000px';
	document.getElementById('calendar_div').style.top = '-1000px';

	document.getElementById('calendar').style.visibility = 'hidden'
	document.getElementById('close_calendar_button').style.visibility = 'hidden';
}
