function FormSubmit (form, who, msg, request, val, url ) {
  var submitOK = true;
  if ( url ) { form.action = url; }
  if ( !( val ) ) { val = ''; }
  if ( request )  { submitOK = confirm( '\n' + msg ); }
  if ( submitOK ) {
    form.dummy.value=val;
    form.dummy.name=who;
    form.submit();
  }
}

function setCheckboxesRange(mform, flag, prefix, fromRec, toRec) {
	for (var i = fromRec; i <= toRec; i++) {
		MyField = eval("mform." + prefix + i);
		MyField.checked = flag;
	}
}

function DeleteCheckedRecord(mform,prefix) {
	var checked_count=0;
	for (var i = 1; i <= mform.TOTAL_RECORD.value; i++) {
		field = eval("mform." + prefix + i);
		if (field.checked) checked_count++;
	}	

	if (checked_count > 0) {
		FormSubmit(mform,'button','Are you sure to delete all checked record(s)?',true,'DELETE_CHECKED_RECORD');
	} else {
		alert('Please select at least one record.');
		return false;
	}
}

function select_all(formName, elementName, selectAllName) {
  if(document.forms[formName].elements[selectAllName].checked) {
    for(var i = 0; i < document.forms[formName].elements[elementName].length; i++)
      document.forms[formName].elements[elementName][i].checked = true;
    document.forms[formName].elements[elementName].checked = true;
  } else {
    for(var i = 0; i < document.forms[formName].elements[elementName].length; i++)
      document.forms[formName].elements[elementName][i].checked = false;
    document.forms[formName].elements[elementName].checked = false;
  }
}

function popup(url, width, height, winName, opt){
	if (!opt) {
		opt = "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no";
	}
	
	nw  = window.open(url,winName,opt+",width=" + width + ",height=" + height);
	nw.focus();
}


function IsInt(s) {
	var i;
    if (isEmpty(s)) 
		return false;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}

// Check whether string s is empty.
function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c) {
	return ((c >= "0") && (c <= "9"))
}

function toChar(n) {
	var str = " ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	n = str.substr(n, 1);
	return(n);
}

function emailValid(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
		return true;
	else {
		return false;
	}
}

function resetDate() {
  document.MyForm.FromMonth[0].selected=1;
  document.MyForm.FromDay[0].selected=1;
  document.MyForm.FromYear[0].selected=1;
  document.MyForm.ToMonth[0].selected=1;
  document.MyForm.ToDay[0].selected=1;
  document.MyForm.ToYear[0].selected=1;
}

function DateSelected () {
  var form = document.MyForm;

  if (form.FromYear.selectedIndex > form.ToYear.selectedIndex) {
    form.ToYear.selectedIndex = form.FromYear.selectedIndex;
  }
  if (form.FromMonth.selectedIndex > form.ToMonth.selectedIndex) {
    if (form.FromYear.selectedIndex == form.ToYear.selectedIndex) {
      form.ToMonth.selectedIndex = form.FromMonth.selectedIndex;
    }
  }
  if (form.FromDay.selectedIndex > form.ToDay.selectedIndex) {
    if (form.FromYear.selectedIndex == form.ToYear.selectedIndex && form.FromMonth.selectedIndex == form.ToMonth.selectedIndex) {
      form.ToDay.selectedIndex = form.FromDay.selectedIndex;
    }
  }
}

function DateSelected (f_prefix,t_prefix) {
  var form = document.MyForm;
  var f_year = eval("form." + f_prefix + "Year");
  var f_month = eval("form." + f_prefix + "Month");
  var f_day = eval("form." + f_prefix + "Day");
  var t_year = eval("form." + t_prefix + "Year");
  var t_month = eval("form." + t_prefix + "Month");
  var t_day = eval("form." + t_prefix + "Day");

  if (f_year.selectedIndex > t_year.selectedIndex) {
    t_year.selectedIndex = f_year.selectedIndex;
  }
  if (f_month.selectedIndex > t_month.selectedIndex) {
    if (f_year.selectedIndex == t_year.selectedIndex) {
      t_month.selectedIndex = f_month.selectedIndex;
    }
  }
  if (f_day.selectedIndex > t_day.selectedIndex) {
    if (f_year.selectedIndex == t_year.selectedIndex && f_month.selectedIndex == t_month.selectedIndex) {
      t_day.selectedIndex = f_day.selectedIndex;
    }
  }
}

function checkDayofMonth (f_prefix) {
  var form=document.MyForm;
  var dayOfMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var d_field = eval("form." + f_prefix + "Day");
  var m_field = eval("form." + f_prefix + "Month");
  var y_field = eval("form." + f_prefix + "Year");
  
  if (y_field) {
  	if (y_field.options[y_field.selectedIndex].value % 4 == 0) {
	    dayOfMonth[1] = 29;
  	} else {
    	dayOfMonth[1] = 28;
  	}
  } else {
	  dayOfMonth[1] = 29;
  }

  if (d_field && m_field) {
  	if (d_field.options[d_field.selectedIndex].value > dayOfMonth[m_field.selectedIndex]) {
	  	var daynum = dayOfMonth[m_field.selectedIndex];
	  	var dif = d_field.options[d_field.selectedIndex].value - dayOfMonth[m_field.selectedIndex];
	  	
	  	d_field.options[d_field.selectedIndex-dif].selected=true;
  	}
  }
}

var marked_row = new Array;

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function testing (i) {
	alert(i);
}