function xIsEmpty (s)
{
  return ((s == null) || (s.length == 0));
}

function xTrim (s)
{
  if(s == null)
    return '';

  while (s.substr(0,1) == ' ') s = s.substr(1);
  while (s.substr(s.length-1,s.length) == ' ') s = s.substr(0,s.length-1);
  return s;
}

function xValidatestr(str, vtype, range, regex)
{
  // see notes in server side function

  if((str == null || vtype == null)
  || (regex != null && regex.length > 0 && str.search(new RegExp(regex)) == -1))
    return false;

  if(range != null && range.length > 0)
  {
    var bound = range.split('-');
    var lbound = isNaN(parseFloat(bound[0])) ? 0 : parseFloat(bound[0]);
    var ubound = isNaN(parseFloat(bound[1])) ? Math.pow(2,64) : parseFloat(bound[1]);

    if((vtype == 'text' || vtype == 'alphanum' || vtype == 'textbox')
    && (str.length < lbound || str.length > ubound))
      return false;

    if(vtype == 'number' || vtype == 'decnum' || vtype == 'intnum' || vtype == 'date' || vtype == 'day' || vtype == 'datetime' || vtype == 'datemmdd' || vtype == 'datemmyy' || vtype == 'time')
    {
      if(str.length == 0)
        str = '0';
      if(parseFloat(str) < lbound || parseFloat(str) > ubound)
        return false;
    }

    if(vtype == 'email')
    {
      var tmp = str.split(',');
      if((lbound > 0 && str.length == 0)
      || tmp.length < lbound || tmp.length > ubound)
        return false;
    }

    if(vtype == 'selectlist' || vtype == 'multiselect')
    {
      var tmp = str.split(',');
      var found = 0;
      for(var i=0; i<tmp.length; i++)
        found += tmp[i].search(/[^0]+/) != -1 ? 1 : 0;
      if(found < lbound || found > ubound)
        return false;
    }
  }

  if(vtype == 'time')
  {
    if((str.length == 0 || parseFloat(str) == 0)
    && (range == null || range.length == 0))
      return true;
    if(str.search(/^.{8}([0-5][0-9]){3}$/) == -1)
      return false;
  }
  if(vtype == 'date' || vtype == 'day' || vtype == 'datetime' || vtype == 'datemmdd')
  {
    if((str.length == 0 || parseFloat(str) == 0)
    && (range == null || range.length == 0))
      return true;

    var yy = parseFloat(str.substr(0,4), 10);

    if(str.search(/^[0-9]{4}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])([0-5][0-9]){3}$/) == -1
    || str.search(/^.{4}(04|06|09|11)31.{6}$/) != -1
    || str.search(/^.{4}(02)3[01].{6}$/) != -1
    || (str.search(/^.{4}(02)29.{6}$/) != -1 && (yy%4 > 0 || (yy%100 ==0 && yy%400 > 0))))
      return false;

    if(vtype == 'date' || vtype == 'datetime' || vtype == 'datemmdd')
    {
      return true;
    }
    else if(vtype == 'day')
    {
      var mth, day;
      var dd = parseFloat(str.substr(6,2), 10);
      var mm = parseFloat(str.substr(4,2), 10);
      var yr1 = yy - 401;
      var firstday = parseFloat((1 + yr1 + (yr1/4) - (yr1/100) + (yr1/400)) % 7);

      for(mth=1; mth<mm; mth++)
      {
        if(mth == 4 || mth == 6 || mth == 9 || mth == 11)
          firstday += 30;
        else if((mth == 2) && (((yy%4 == 0) && yy%100 != 0) || yy%400 == 0))
          firstday += 29;
        else if(mth == 2)
          firstday += 28;
        else
          firstday += 31;
      }
      return ((firstday + dd - 1) % 7) + 1;
    }
  }
  if(vtype == 'bankcard' || vtype == 'creditcard')
  {
    var digit,check,position;

    str = str.replace(/\-/g, '');

    for(check=0,position=1; position<=str.length; position++)
    {
      if(isNaN(digit=parseInt(str.charAt(str.length-position),10)))
        return false;
      if(!(position % 2))
        digit=parseInt('0246813579'.charAt(digit),10);
      check+=digit;
    }
    if((check % 10) != 0)
      return false;

    if(vtype == 'bankcard')
      return true;
    else if(str.search(/^5[1-5].{14}$/) != -1)
      return 'master';
    else if(str.search(/^4.{15}$|^4.{14}$|^4.{13}$|^4.{12}$/) != -1)
      return 'visa';
    else if(str.search(/^3[47].{13}$/) != -1)
      return 'amex';
    else if(str.search(/^6011.{12}$/) != -1)
      return 'discover';
    else if(str.search(/^30[0-5].{11}$|^3[68].{12}$/) != -1)
      return 'diners';
    else if(str.search(/^2014|2149.{11}$/) != -1)
      return 'enroute';
    else if(str.search(/^3.{15}$|^2131|1800.{11}$/) != -1)
      return 'jcb';
    else
      return 'unknown';
  }

  if((vtype == 'text' && str.search(/^.|\n*$/) == -1)
  || (vtype == 'textbox' && str.search(/^.|\n*$/) == -1)
  || (vtype == 'email' && str.search(/(^(,*[0-9a-zA-Z]([-_.]?[0-9a-zA-Z])*@[0-9a-zA-Z]([-.]?[0-9a-zA-Z])*\.(com|edu|biz|org|gov|int|info|mil|net|name|museum|coop|aero|[a-z][a-z]))+$)|(^$)/i) == -1)
  || (vtype == 'alphanum' && str.search(/(^[0-9a-zA-Z]+$)|(^$)/) == -1)
  || (vtype == 'number' && str.search(/(^[0-9]*\.*[0-9]*$)|(^$)/) == -1)
  || (vtype == 'decnum' && str.search(/(^[0-9]*\.[0-9]*$)|(^$)/) == -1)
  || (vtype == 'intnum' && str.search(/(^[0-9]+$)|(^$)/) == -1))
    return false;

  return true;
}

function getDbDateFormat(dateValue)
{
  currentDate = new Date();
  var cYear = currentDate.getFullYear();

  if(dateValue.length<4)
		return false;
	else if(dateValue.length == 4)
		dateValue = cYear + "-" + dateValue.substr(2,2) + "-" + dateValue.substr(0,2);
	else if(dateValue.length < 6)
		return false;
	else if(dateValue.length == 6)
		dateValue = cYear.toString().substr(0,2) + dateValue.substr(4,2) + "-" + dateValue.substr(2,2) + "-" + dateValue.substr(0,2);
	else if(dateValue.length < 8)
		return false;
  else if(dateValue.length == 8)
   	dateValue = dateValue.substr(4,4) + "-" + dateValue.substr(2,2) + "-" + dateValue.substr(0,2);
  else if(dateValue.length != 8)
   	return false;

	return dateValue;
}

function LeapYear(intYear)
{
	if (intYear % 100 == 0)
	{
		if (intYear % 400 == 0)
		{
			return true;
		}
	}
	else
	{
		if ((intYear % 4) == 0)
		{
			return true;
		}
	}
	return false;
}

/*
function validateDate(selDate)
{
	var intMonth;
	var intDay;
	var intYear;

	intYear = selDate.substr(0,4);
	intMonth = selDate.substr(5,2);
	intDay = selDate.substr(8,2);
  if(intMonth>12 || intMonth<1)
  {
    return false;
  }
  if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1))
  {
    return false;
  }
  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1))
  {
    return false;
  }
  if (intMonth == 2)
  {
    if (intDay < 1)
    {
      return false;
    }
    if (LeapYear(intYear) == true)
    {
      if (intDay > 29)
      {
        return false;
      }
    }
    else
    {
      if (intDay > 28)
      {
        return false;
      }
    }
  }

	return true;
}
*/

function validateDate(intDay, intMonth, intYear)
{
  var validateFlag = true;
  if (!isNumber(intYear) || !isNumber(intMonth) || !isNumber(intDay))
  {
    validateFlag = false;
  }

  if(intYear>9999 || intYear<1000)
  {
    validateFlag = false;
  }

  if(intMonth>12 || intMonth<1)
  {
    validateFlag = false;
  }
  if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1))
  {
    validateFlag = false;
  }
  if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30 || intDay < 1))
  {
    validateFlag = false;alert (3);
  }
  if (intMonth == 2)
  {
    if (intDay < 1)
    {
      validateFlag = false;
    }
    if (LeapYear(intYear) == true)
    {
      if (intDay > 29)
      {
        validateFlag = false;
      }
    }
    else
    {
      if (intDay > 28)
      {
        validateFlag = false;
      }
    }
  }
  return validateFlag;
}

function compareDate(startdate,enddate)
{
	var sd = startdate.split("-",3);
	var ed = enddate.split("-",3);
	sd[1] -= 1;
	ed[1] -= 1;

	var sdate = new Date(sd[0],sd[1],sd[2]);
 	var edate = new Date(ed[0],ed[1],ed[2]);

 	if (edate.getTime() >= sdate.getTime())
 	{
 		return true;
 	}
 	else
 	{
 		return false;
 	}
}


function checkMaxDateRange(startdate,enddate,maxday)
{
	var sd = startdate.split("-",3);
	var ed = enddate.split("-",3);

	sd[1] -= 1;
	ed[1] -= 1;

	var sdate = new Date(sd[0],sd[1],sd[2]);
 	var edate = new Date(ed[0],ed[1],ed[2]);

 	if ((edate.getTime() - sdate.getTime()) <= (maxday * 86400000))
 	{
 		return true;
 	}
 	else
 	{
 		return false;
 	}
}


function compareCurrentDate(startdate)
{
	var dDate = new Date();
	var dCurMonth = dDate.getMonth();
	var dCurDayOfMonth = dDate.getDate();
	var dCurYear = dDate.getFullYear();

	var sd = startdate.split("-",3);
	sd[1] -= 1;

	var cdate = new Date(dCurYear,dCurMonth,dCurDayOfMonth);
	var sdate = new Date(sd[0],sd[1],sd[2]);

 	if (sdate.getTime() >= cdate.getTime())
 	{
 		return true;
 	}
 	else
 	{
 		return false;
 	}
}


function compareCurrentDate2(startdate)
{
	var dDate = new Date();
	var dCurMonth = dDate.getMonth();
	var dCurDayOfMonth = dDate.getDate();
	var dCurYear = dDate.getFullYear();

	var sd = startdate.split("-",3);
	sd[1] -= 1;

	var cdate = new Date(dCurYear,dCurMonth,dCurDayOfMonth);
	var sdate = new Date(sd[0],sd[1],sd[2]);

 	if (sdate.getTime() > cdate.getTime())
 	{
 		return true;
 	}
 	else
 	{
 		return false;
 	}
}


function isPrice(fieldname,msg)
{
	if((fieldname.value.search(new RegExp("^([0-9]+)(\\.?)([0-9]{0,4})$","g")))<0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	if((fieldname.value.search(new RegExp("^([0-9]+)(\\.{1})$","g")))>=0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	return true;
}


function isPrice2 (fieldname, msg)
{
	if((fieldname.value.search(new RegExp("^([0-9]+)(\\.?)([0-9]{0,2})$","g")))<0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	if((fieldname.value.search(new RegExp("^([0-9]+)(\\.{1})$","g")))>=0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	return true;
}


function isPriceAllowNegative (fieldname,msg)
{
	if((fieldname.value.search(new RegExp("^(-?)([0-9]+)(\\.?)([0-9]{0,4})$","g")))<0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	if((fieldname.value.search(new RegExp("^(-?)([0-9]+)(\\.{1})$","g")))>=0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	return true;
}


function isPrice2AllowNegative (fieldname, msg)
{
	if((fieldname.value.search(new RegExp("^(-?)([0-9]+)(\\.?)([0-9]{0,2})$","g")))<0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	if((fieldname.value.search(new RegExp("^(-?)([0-9]+)(\\.{1})$","g")))>=0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	return true;
}

/*
function isNumber(fieldname,msg)
{
	if((fieldname.value.search(new RegExp("^([0-9]+)$","g")))<0)
	{
		alert(msg);
		fieldname.focus();
		fieldname.select();
		return false;
	}
	return true;
}
*/

function isNumber (value)
{
	if((value.search(new RegExp("^([0-9]+)$","g")))<0)
	{
		return false;
	}
	return true;
}

function checkEmptyField(theField,msg)
{
  if (xIsEmpty (xTrim (theField.value)))
  {
    alert(msg);
    theField.focus();
    return true;
  }
  return false;
}

function checkEmptyCheckbox (theField, msg)
{

  var i;

  for (i = 0; i < theField.length; i++)
    if (theField[i].checked)
      return false;

  alert (msg);
  theField[0].focus ();
  return true;

}

function checkSelectEmpty(theSelectedField,msg)
{
  if (theSelectedField.options[theSelectedField.selectedIndex].value == "" ||
      theSelectedField.options[theSelectedField.selectedIndex].value == 0)
  {
    alert(msg);
    theSelectedField.focus();
    return true;
  }
  return false;
}

// Added  MKONG 20020102
function openCenterWin (url, name, width, height)
{

  var winl = (screen.width - width) / 2;
  var wint = (screen.height - height) / 2;
  settings = 'width='+width+',height='+height+',top='+wint+',left='+winl+',resizable=yes,scrollbars=yes,menubar=no,status=no,directories=no,location=no,titlebar=no,toolbar=no,dependent=no';

  var Win = window.open (url, name, settings);

  // Create a new property for the newly open window
  //Win.creator = self;

  if (parseInt (navigator.appVersion) >= 4)
  {
    Win.window.focus ();
  }

}
