
function IsInteger(strValue)
{
  var strNumbers = "0123456789";
  var IsInt = true;
  
  for (var i = 0; i < strValue.length; i++)
    if (strNumbers.indexOf(strValue.charAt(i)) == -1) {
      IsInt = false;
      break;
    }  
  return IsInt;
}

function IsFloat(strValue)
{
  var strNumbers = "0123456789.";
  var IsFlt = true;
  var PointCount = 0;
  
  for (var i = 0; i < strValue.length; i++)
    if (strValue.charAt(i) == ".") PointCount++;
  if (PointCount > 1) return false;
    
  for (var i = 0; i < strValue.length; i++)
    if (strNumbers.indexOf(strValue.charAt(i)) == -1) {
      IsFlt = false;
      break;
    }
    
  return IsFlt;
}

function IsEMail(strValue)
{
  var AtIndex = strValue.indexOf("@");
  return ((AtIndex > -1) && (strValue.indexOf(".", AtIndex + 1) > -1));
}

function GetDateTimeFromField(objForm, FieldName)
{
  var objDay = GetElement(objForm, "DTED_" + FieldName);
  if (!objDay) DayVal = 1; else DayVal = parseInt(objDay.options[objDay.selectedIndex].value);
  var objMonth = GetElement(objForm, "DTEM_" + FieldName);
  if (!objMonth) MonthVal = 0; else MonthVal = parseInt(objMonth.options[objMonth.selectedIndex].value) - 1;
  var objYear = GetElement(objForm, "DTEY_" + FieldName);
  if (!objYear) YearVal = 1900; else YearVal = parseInt(objYear.options[objYear.selectedIndex].value);

  var objHour = GetElement(objForm, "TIH_" + FieldName);
  if (!objHour) HourVal = 0; else HourVal = parseInt(objHour.options[objHour.selectedIndex].value);
  var objMin = GetElement(objForm, "TIM_" + FieldName);
  if (!objMin) MinVal = 0; else MinVal = parseInt(objMin.options[objMin.selectedIndex].value);
  var objSec = GetElement(objForm, "TIS_" + FieldName);
  if (!objSec) SecVal = 0; else SecVal = parseInt(objSec.options[objSec.selectedIndex].value);
  
  return new Date(YearVal, MonthVal, DayVal, HourVal, MinVal, SecVal);
}

function GetDateTimeFromString(strDateTime)
{
  if (strDateTime.length < 19) new Date();

  var YearVal = parseInt(strDateTime.substr(0, 4));
  var MonthVal = parseInt(strDateTime.substr(5, 2)) - 1;
  var DayVal = parseInt(strDateTime.substr(8, 2));
  var HourVal = parseInt(strDateTime.substr(11, 2));
  var MinVal = parseInt(strDateTime.substr(14, 2));
  var SecVal = parseInt(strDateTime.substr(17, 2));
 
  return new Date(YearVal, MonthVal, DayVal, HourVal, MinVal, SecVal);
}

function IndexOfFormElementTrueName(objForm, ElementName)
{
  var res = -1;
  var FieldName = ElementName;
  
  for (var i = 0; i < objForm.elements.length; i++) {      
    if (objForm.elements[i].name == FieldName) {
      res = i;    
      break;
    }
  } 
  return res;
} 

function IndexOfFormElement(objForm, ElementName)
{
  return IndexOfFormElementTrueName(objForm, 'Values[' + ElementName + ']');
} 

function GetDaysInMonth(iMonth, iYear) 
{
  var dPrevDate = new Date(iYear, iMonth, 0);
  return dPrevDate.getDate();
}

function GetElement(objForm, FieldName)
{
  var ElementIdx;
  
  ElementIdx = IndexOfFormElement(objForm, FieldName);
  if (ElementIdx > -1) return objForm.elements[ElementIdx];
}

function CheckRequired(objForm, FieldName, ErrorMsg)
{
  var IsError = false;
  
  var objElement = GetElement(objForm, FieldName);
  if (!objElement) return;
  
  switch(objElement.type) {
    case "textarea":
    case "text":
    case "file":
    case "password":
      IsError = (objElement.value == "");
      break;
    case "select-one":
    case "select":
	  var selValue = objElement.options[objElement.selectedIndex].value;
      IsError = (selValue == "0") || (selValue == "__INFOTEXT");
      break;
  }
  
  if (IsError) {
    alert(ErrorMsg);
    objElement.focus();
  }
  
  return IsError;
}
 
function CheckEMail(objForm, FieldName, ErrorMsg)
{
  var objText = GetElement(objForm, FieldName);
  if (!objText) return;

  var strValue = objText.value;
  if (strValue == "") return true;
  
  var IsValid = IsEMail(strValue);

  if (!IsValid) {
    alert(ErrorMsg);
    objText.value = objText.defaultValue; 
    objText.focus();
  }

  return IsValid;
}

function CheckInteger(objForm, FieldName, ErrorMsg)
{
  var objText = GetElement(objForm, FieldName);
  if (!objText) return;

  var strValue = objText.value;
  
  if (strValue == "") return true;
  
  var IsValid = IsInteger(strValue);
  if (!IsValid) {
    alert(ErrorMsg);
    objText.value = objText.defaultValue; 
    objText.focus();
  }
  return IsValid;
}

function CheckFloat(objForm, FieldName, ErrorMsg)
{
  var objText = GetElement(objForm, FieldName);
  if (!objText) return;

  var strValue = objText.value;
  
  if (strValue == "") return true;
  
  var IsValid = IsFloat(strValue);
  if (!IsValid) {
    alert(ErrorMsg);
    objText.value = objText.defaultValue; 
    objText.focus();
  }

  return IsValid;
}

function CheckDateTime(objForm, FieldName, ErrorMsg)
{
  var objDay = GetElement(objForm, "DTED_" + FieldName);
  if (!objDay) DayVal = 1; else DayVal = parseInt(eval(objDay.options[objDay.selectedIndex].value));
  var objMonth = GetElement(objForm, "DTEM_" + FieldName);
  if (!objMonth) MonthVal = 0; else	MonthVal = parseInt(eval(objMonth.options[objMonth.selectedIndex].value));
  var objYear = GetElement(objForm, "DTEY_" + FieldName);
  if (!objYear) YearVal = 1900; else YearVal = parseInt(eval(objYear.options[objYear.selectedIndex].value));
  
  testDate = new Date(YearVal, MonthVal - 1, DayVal);
  var IsValid = (testDate.getMonth() == (MonthVal - 1));

  if (!IsValid) {
    alert(ErrorMsg.replace(/%s/i, DayVal - testDate.getDate()));
    objDay.focus();
  }
  
  return IsValid;
}

function CheckEditMinLength(objForm, FieldName, MinLength, ErrorMsg)
{
  var objText = GetElement(objForm, FieldName);
  if (!objText) return;

  var IsValid = (objText.value.length >= MinLength);
  if (!IsValid) {
    alert(ErrorMsg);
    objText.focus();
  }
  return IsValid;
}

function CheckEditMin(objForm, FieldName, MinValue, ErrorMsg)
{
  var objText = GetElement(objForm, FieldName);
  if (!objText) return;

  var IsValid = (objText.value >= MinValue);
  if (!IsValid) {
    alert(ErrorMsg.replace(/%s/i, MinValue));
    objText.focus();
  }
  
  return IsValid;
}

function CheckEditMax(objForm, FieldName, MaxValue, ErrorMsg)
{
  var objText = GetElement(objForm, FieldName);
  if (!objText) return;

  var IsValid = (objText.value <= MaxValue);
  if (!IsValid) {
    alert(ErrorMsg.replace(/%s/i, MaxValue));
    objText.focus();
  }
  
  return IsValid;
}

function CheckDateTimeMin(objForm, FieldName, MinValue, ErrorMsg)
{
  var FieldVal = GetDateTimeFromField(objForm, FieldName);
  var CompVal = GetDateTimeFromString(MinValue); 

  var IsValid = (FieldVal.valueOf() >= CompVal.valueOf());
  if (!IsValid) {
    var objFocus = GetElement(objForm, "DTED_" + FieldName);
    if (!objFocus) objFocus = GetElement(objForm, "TIH_" + FieldName);
    if (!objFocus) return;
    
    alert(ErrorMsg);
    objFocus.focus();
  }
  
  return IsValid;
}

function CheckDateTimeMax(objForm, FieldName, MaxValue, ErrorMsg)
{
  var FieldVal = GetDateTimeFromField(objForm, FieldName);
  var CompVal = GetDateTimeFromString(MaxValue); 

  var IsValid = (FieldVal.valueOf() <= CompVal.valueOf());
  if (!IsValid) {
    var objFocus = GetElement(objForm, "DTED_" + FieldName);
    if (!objFocus) objFocus = GetElement(objForm, "TIH_" + FieldName);
    if (!objFocus) return;
    
    alert(ErrorMsg);
    objFocus.focus();
  }
  
  return IsValid;
}

// BrowserCheck function
function BrowserCheck() {
	var b = navigator.appName;
	if (b=='Netscape') this.b = 'ns';
	else if (b=='Microsoft Internet Explorer') this.b = 'ie';
	else this.b = b;
	this.v = parseInt(navigator.appVersion);
	this.ns = (this.b=='ns' && this.v>=4);
	this.ns4 = (this.b=='ns' && this.v==4);
	this.ns5 = (this.b=='ns' && this.v==5);
	this.ie = (this.b=='ie' && this.v>=4);
	this.ie4 = (navigator.userAgent.indexOf('MSIE 4')>0);
	this.ie5 = (navigator.userAgent.indexOf('MSIE 5')>0);
	if (this.ie5) this.v = 5;
	this.min = (this.ns||this.ie);
}
// automatically create the "is" object
BrowserIs = new BrowserCheck();

function FormParent(fld)
{
	for (i=0;i<document.forms.length;i++)
		for (j=0;j<document.forms[i].elements.length;j++)
			if (document.forms[i].elements[j].name==fld.name)
				return document.forms[i];
}

function ElementByName(strName)
{
	for (i=0;i<document.forms.length;i++)
		for (j=0;j<document.forms[i].elements.length;j++)
			if (document.forms[i].elements[j].name==strName)
				return document.forms[i].elements[j];
}

//LayerWriteText function
function LayerWriteText(Doc,DivId,text) {
	if (BrowserIs.ns) {
		ThisForm = FormParent(DivId) ;		
		ThisForm[DivId].value = text ;
	}
	else if (BrowserIs.ie) Doc.all[DivId].innerHTML = text;
}

