function rtrim(argvalue) {
  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }
  return argvalue;
}

function ltrim(argvalue) {
  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }
  return argvalue;
}

function trim(argvalue) {
  var tmpstr = ltrim(argvalue);
  return rtrim(tmpstr);
}

function Check(tMe, tField) {
  switch (parseFloat(document.all[tField].value)) {
    case 0 :
      document.all[tMe].src = '../sysImages/smallgreen_edit.gif';
      document.all[tField].value = -1;
      break;
    case -1 :
      document.all[tMe].src = '../sysImages/smallgray_edit.gif';
      document.all[tField].value = 0;
      break;
  }
}

function moveMe(where) {
  document.location.replace(where);
  return false;
}

function suycCalendar(m,y)
{
    
    if (typeof(_calendar_prototype_called) == 'undefined')
    {
        _calendar_prototype_called = true ;
        
        // Object methods
        this.BuildCalendar = _create ;
        this.createCanvass = _createCanvass ;
        this.showCalendar = _showCalendar ;
        this.goToCurrent = _goToCurrent ;
        this.toggleCalendar = _toggleCalendar ;
        this.moveTo = _positionCanvass ;
        this.hide = _hide ;
        this.show = _show ;
        this.init = _init ;
    }
    
        // Object properties
        this.name = 'default' ;
        this.rowBGColor = 'palegoldenrod' ;
        this.currentDay = 0 ;
        this.currentMonth = 0 ;
        this.currentYear = 0 ;
        this.visible = false ;
        this.posX = 10 ;
        this.posY = 10 ;
        this.isIE = '';
        this.isNav4 = '';
        this.isNav6 = '';
        
		// If you set hasEvents fo FALSE then the calendar face is dumbed out.
        this.hasEvents = true ;
        
        this.canvass = '' ; // The DIV || LAYER that we display the calendar on.
        this.bindToElement = '' ;  // Bind to an ELEMENT on the page.
    
    // Array of day names
    this.days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
    // Array of month names
    this.months = new Array("January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November", "December");
    // Array of total days in each month
    this.totalDays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    
    // Call the Initialize() event.
    this.init(m,y) ;
}

function _show()
// This function displays the objects canvass.
{
    if(this.isNav4)
    {
        this.canvass.visibility = 'show' ;
        this.canvass.left = this.posX ;
        this.canvass.top = this.posY ;   
    } else {
        this.canvass.style.visibility = 'visible' ;
        this.canvass.style.left = this.posX ;
        this.canvass.style.top = this.posY ;
    }
    this.visible = true ; 
}

function _hide()
// This function hides the objects canvass.
{
	if(this.isNav4)
    {
        this.canvass.visibility = 'hide' ;   
    } else {
        this.canvass.style.visibility = 'hidden' ;
    }
    this.visible = false ;
}

function _init(m,y)
{
    if (parseInt(navigator.appVersion.charAt(0)) >= 4)
    //Browser check.
    {
        //this.isNav4 = (navigator.appName == "Netscape") ? true : false ;
        //this.isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false ;
        this.isNav4 = document.layers ? true : false ;
		this.isNav6 = (document.getElementById && !document.all) ? true : false ;
        this.isIE = document.getElementById ? true : false ;
	}
    
    // Populate the current Day|Month|Year properties
    var obj = new Date();
    this.currentDay = obj.getDate();
	this.currentMonth = obj.getMonth() + 1;
    this.currentYear = (obj.getYear() < 1000) ? obj.getYear() + 1900 : obj.getYear();
    
    /* 
        The constructor optionally accepts m && y parameters
        if none are supplied, the calendar defaults to the current
        month 
    */
	this.month = m || this.currentMonth ;
    this.year = y || this.currentYear ;
    
    // Create the canvass that we will be displaying the calendar on
    this.createCanvass() ;
    obj = null ;
  
}

function _createCanvass()
{
    // Create canvass for NN4+
    if (this.isNav4) 
    { 
        this.canvass = new Layer(200) ;
        this.canvass.left = this.posX ;
        this.canvass.top = this.posY ;
    }
    
    // Create canvass for IE4+
    if (this.isIE || this.isNav6)
    { 
        var objDiv = document.createElement("<DIV STYLE='position: absolute;' name='counter 'id='counter'>") ;
        document.body.appendChild (objDiv) ;
        this.canvass = objDiv ;
        this.canvass.style.left = this.posX ;
        this.canvass.style.top = this.posY ;
    }
}

function _positionCanvass(x,y)
{
    // if either x || y were not supplied, default to the current settings.
    if(x==null || y==null) { x=this.posX; y=this.posY ; return ;}
    
    if( isNaN(x) || isNaN(y) )
    {
        alert('You can only enter numbers for the x/y co-ordinates') ;
        return ;
    }
    
    // apply the new settings for NN
    if (this.isNav4) 
    { 
        this.canvass.left = this.posX = x ;
        this.canvass.top = this.posY = y ;
    }
    
    // apply the new settings for IE
    if (this.isIE || this.isNav6)
    { 
        this.canvass.style.left = this.posX = x ;
        this.canvass.style.top = this.posY = y ;
    }
    
}

function _goToCurrent()
{
    
    this.year = this.currentYear ;
    this.month = this.currentMonth ;
    this.BuildCalendar() ;
}

function _toggleCalendar(a, b)
{
    var currentMonth = this.month ;
    var currentYear = this.year;

    this.year = currentYear + b ;
	currentYear = this.year;
	
	if((currentMonth + a) == 0)
    { 
        this.year = currentYear-1 ; 
        this.month = 12 ; 
    }
    else if((currentMonth + a) == 13)
    { 
        this.year = currentYear+1 ; 
        this.month = 1 ; 
    }
    else
    {
        this.month = currentMonth + a ;
    }
    this.BuildCalendar() ;
}

// Create and Display Calendar.
function _create()
{
    // Counters to count rows and days, String to store calendar output.
    var rowCount = 0 ;
    var numRows = 0 ;
    var sOut = new String() ;
    
    // Leap year correction
    if (this.year % 4 == 0 && (this.year % 100 != 0 || this.year % 400 == 0)) {
    	this.totalDays[1] = 29 ;
    } else {
    	this.totalDays[1] = 28 ;
	}
    
    var obj = new Date(this.year, this.month-1, 1) ;
    this.firstDayOfMonth = obj.getDay() ;
    obj.setDate(31) ;
    this.lastDayOfMonth = obj.getDay() ;
    obj = null ;
    
	var obj = new Date();
	this.TodaysDay = obj.getDate();
	this.TodaysMonth = obj.getMonth();
	this.TodaysYear = obj.getYear();
	obj = null;

	sOut = "<table border=1 cellpadding=0 cellspacing=0 bgcolor=\'#D4D0C8\'><tr><td>" ;
    sOut += "<table border=0 cellpadding=1 cellspacing=0>" ;
    /*  Write the TABLE header (month/year) */
    sOut += "<tr bgcolor=#3A6EA5><td colspan=6 align=left><font face=Verdana size=-1 color=#ffffff>&nbsp;<b>" + this.months[this.month-1] + " " + this.year + "</b></font></td>" ;
    sOut += "<td ALIGN='right'><A HREF=\"javascript:  ;\" onClick='calClose() ; return false ;' TITLE='Close'><img src='../sysImages/xclose.gif' border='0' alt='Close'></A></td>" ;
    sOut += "</tr>" ;

    /*  Write the abbreviated day names */
    sOut += "<tr>" ;
    for (x=0; x<7; x++) {
        sOut += "<td><font face=Verdana size=-2 color=#000000><b>" + this.days[x].substring(0,3) + "</b></font></td>" ;
    }
    sOut += "</tr>" ;

    /* Start of BODY */
    sOut += "<tr>" ;
    numRows++ ;
    
    if (this.firstDayOfMonth != 5){
    	for (x = 1; x <= this.firstDayOfMonth; x++) {
			/* pad the blank days at the beginning of the month. */
			rowCount++;
			sOut += "<td>&nbsp;</td>" ;
		}
	}
    
    
    /* Increment the current date */
    this.dayCount=1;
	while (this.dayCount <= this.totalDays[this.month-1]){
    	/* Display new row after each 7 day block.  */
    	if (rowCount % 7 == 0)
    	{
    	    sOut += "</tr>\n<tr>" ;
    	    numRows++ ;
		}
		
		if (this.dayCount == this.TodaysDay && this.month-1 == this.TodaysMonth && this.year == this.TodaysYear){
			bgColor = "bgcolor=#FFFFFF";
		} else {
			bgColor = "bgcolor=#D4D0C8";
		}
        
		/* Insert the Date. */
        sOut += "<td " + bgColor + " align=center><font face=Verdana size=-2><A HREF=\"javascript:  ;\""

        if (this.hasEvents) sOut += " onClick='clickhandler(" + this.dayCount + "," + this.month + "," + this.year + ") ;'" ;

        sOut += " CLASS='calClickable' onMouseOver=\"this.className = 'calClickable_hover';\" onMouseOut=\"this.className = 'calClickable';\" TITLE='" + this.dayCount + "." + this.month + "." + this.year + "'>" + this.dayCount + "</A></font>"  
        sOut += "</td>" ;
        this.dayCount++ ;
        rowCount++ ;
    }

    while (rowCount % 7 != 0) {
        /* pad the blank days at the end of the month. */
        rowCount++ ;
        sOut += "<td>&nbsp;</td>" ;
    }
    sOut += "</tr>" ;
    // End of BODY
    
    // Write the Calendar Navigator.
    if(this.hasEvents) {
        sOut += "<tr>" ;
        sOut += "<td align=left>" ;
        sOut += "<font face=Verdana size=-2><A HREF=\"javascript:  ;\" onClick='toggleCalendar(0, -1) ; return false ;' CLASS='calClickable'  onMouseOver=\"this.className = 'calClickable_hover';\" onMouseOut=\"this.className = 'calClickable';\" TITLE='Previous year' />&lt;&lt;</A></font>" ;
        sOut += "</td>" ;
        sOut += "<td align=left>" ;
        sOut += "<font face=Verdana size=-2><A HREF=\"javascript:  ;\" onClick='toggleCalendar(-1, 0) ; return false ;' CLASS='calClickable'  onMouseOver=\"this.className = 'calClickable_hover';\" onMouseOut=\"this.className = 'calClickable';\" TITLE='Previous month' />&lt;</A></font>" ;
        sOut += "</td>" ;
        sOut += "<td colspan=3 align=center>" ;
        sOut += "<font face=Verdana size=-2><A HREF=\"javascript:  ;\" onClick='toggleCurrent() ; return false ;' CLASS='calClickable'  onMouseOver=\"this.className = 'calClickable_hover';\" onMouseOut=\"this.className = 'calClickable';\" TITLE='Today's date' />[Today]</A></font>" ;
        sOut += "</td>" ;
        sOut += "<td align=right>" ;
        sOut += "<font face=Verdana size=-2><A HREF=\"javascript:  ;\" onClick='toggleCalendar(1, 0) ; return false ;' CLASS='calClickable'  onMouseOver=\"this.className = 'calClickable_hover';\" onMouseOut=\"this.className = 'calClickable';\" TITLE='Next month' />&gt;</A></font>" ;
        sOut += "</td>" ;
        sOut += "<td align=right>" ;
        sOut += "<font face=Verdana size=-2><A HREF=\"javascript:  ;\" onClick='toggleCalendar(0, 1) ; return false ;' CLASS='calClickable'  onMouseOver=\"this.className = 'calClickable_hover';\" onMouseOut=\"this.className = 'calClickable';\" TITLE='Next year' />&gt;&gt;</A></font>" ;
        sOut += "</td>" ;
        sOut += "</tr>" ;
    }
    
    sOut += "</table>" ;
    sOut += "</td></tr></table>" ;
    
    // Render the calendar
    this.showCalendar (sOut) ;
}

function _showCalendar (s)
{
    if(this.isNav4)
    {
        this.canvass.document.open() ;
        this.canvass.document.writeln(s) ;
        this.canvass.document.close() ;
    } else {
        this.canvass.innerHTML = s ;
	}
    this.show() ;
}

function cent(fld) {
  var amount = fld.value;
  if (isNaN(amount)){
    alert('Wrong entry!\n\nPossible reasons:\n- letters A-Z not allowed\n- multiple . not allowed \n- , as dec or mil separator not allowed');
    fld.select();
  } else {
    amount -= 0;
    amount = Math.abs(Math.round(amount * 100) / 100);
    fld.value = (amount == Math.floor(amount)) ? amount + '.00' : ( (amount * 10 == Math.floor(amount * 10)) ? amount + '0' : amount);
  }
}

function strReplace(str1, str2, str3) { 
 while(str1.indexOf(str2) != -1) {
   str1 = str1.replace(str2, str3);
 } 
 return str1;     
} 