
// =======================================================
// Written by: 
// =======================================================

// =======================================================
function submitForm(xForm,xText){
     
     xForm.stateAction.value=xText;
     xForm.submit();
  }

// =======================================================
function saveRecord(xForm){ 
  
  if (xForm.stateAction.value=="NEW")
    VerifyAndSubmit(xForm,"NEW");

  if (xForm.stateAction.value!="NEW")
    VerifyAndSubmit(xForm,"UPDATE");

}

// =======================================================
function deleteRecord(xForm){ 

   returnValue = window.confirm("Do you really want to delete this record ?");

   if (returnValue){
     xForm.stateAction.value="DELETE";
     xForm.submit();
   }
}

// =======================================================
function LoadPage(xPage){

   var d = new Date();

   var cf = (new Date()).getTime();

   this.document.location.href = xPage;
  
}


// =======================================================
function LoadPageNew(xPage){

   var d = new Date();

   var cf = (new Date()).getTime();

   this.document.location.href = xPage + "?cf=" + cf ;
  
}

// =======================================================

function backURL(){ 
    window.history.back();
}

// =======================================================
function VerifyAndSubmitForm(xForm,xText){
  var isSubmit = xForm.onsubmit();
  
  if ((isSubmit) || (null==isSubmit)){
    xForm.stateAction.value=xText;
    xForm.submit();
  }
}


// =======================================================
// Written by: 
// =======================================================

function fMenu(mItem){
 
  if (mItem=="Save")
  {
        VerifyAndSubmitForm(document.forms.theForm,"SAVE");
  }

  if (mItem=="Cancel")
  {
       submitForm(document.forms.theForm,"CANCEL");

  }

  if (mItem=="New")
  {
       submitForm(document.forms.theForm,"NEW");
  }
  
  
   if (mItem=="Update")
  {
        VerifyAndSubmitForm(document.forms.theForm,"UPDATE");
  }
   
  if (mItem=="Delete") 
  {
       deleteRecord(document.forms.theForm);
  }  

  if (mItem=="Back")
  {  
    backURL();
  }

}
// =======================================================


// =======================================================
function CurrentDate(dateStyle) {
  mm = Month(Now);
  if (mm < 10) mm = "0" + Month(Now);

  md = Day(Now)
    if (md < 10) md = "0" & Day(Now);
	  
  var s = "";
  if (dateStyle == "EU") s = md + "/" + mm + "/" + Year(now);
  if (dateStyle == "US") s = mm + "/" + md + "/" + Year(now);
  if (dateStyle == "SE") s = Year(now) + "-" + mm + "-" + md;
    
  return s;

  /* Date standards
   EU  dd/mm/yyyy
   US  mm/dd/yyyy
   SE  yyyy-mm-dd
  */

}

// =======================================================

// =======================================================

// ============================================================
//
//
// ============================================================


// ------------------------------------------------------------------------------------------------
function ISDateValidEU (saveDate)
{
         
  var dateError = false;
  var dateLen   = saveDate.length;

 			
  if (dateLen > 0)
  {

  var firstSlash  = saveDate.indexOf("/");
  var secondSlash = saveDate.lastIndexOf("/");

  if ( ( firstSlash == -1) || ( secondSlash == -1 ) || ( firstSlash == secondSlash ) )
  {
    dateError = true;
    return false;
  }
						
  var saveDay  = saveDate.substring(0,firstSlash);	
  var saveMonth = saveDate.substring(firstSlash+1,secondSlash);	
  var saveYear   = saveDate.substring(secondSlash+1);	

  for (i = 0; i < saveDay.length; i++)
  {
    var currchar = saveDay.charAt(i);
    if (! isDigit(currchar) )
    {
       return false;
    }
  }
						
  for (i = 0; i < saveMonth.length; i++)
  {
    var currchar = saveMonth.charAt(i);
    if (! isDigit(currchar) )
    {
       return false;
    }
  }

  for (i = 0; i < saveYear.length; i++)
  {
    var currchar = saveYear.charAt(i);
    if (! isDigit(currchar) )
    {
      return false;
    }
  }

  if (saveDay.length == 1) 
  {
    saveDay = "0" + saveDay;
  }

  if (saveMonth.length == 1) 
  {
    saveMonth = "0" + saveMonth;
  }


  if ((saveMonth.length > 2) || (saveDay.length > 2) || (saveYear.length < 2))
  {
    dateError = true;
    return false;
  }
								
					
  if (saveYear.length == 2)
  {
    if (saveYear < 51) 
    {
      saveYear = "20" + saveYear;
    }
    else
    {
      saveYear = "19" + saveYear;
    }
  }
			
  if (( saveMonth < 1 ) || ( saveMonth > 12 ) || (saveDay < 1) || (saveDay > 31) || (saveYear < 1900) || (saveYear > 2099) )
  {
    dateError = true;
    return false;
  }
					
  var numDays = getDays(saveMonth, saveYear);

  if (saveDay > numDays)
  {
    dateError = true;
    return false;
  }
  
  return true;
 }
}

// ------------------------------------------------------------------------------------------------
function ISDateValidUS (saveDate)
{
         
  var dateError = false;
  var dateLen   = saveDate.length;

 			
  if (dateLen > 0)
  {

  var firstSlash  = saveDate.indexOf("/");
  var secondSlash = saveDate.lastIndexOf("/");

  if ( ( firstSlash == -1) || ( secondSlash == -1 ) || ( firstSlash == secondSlash ) )
  {
    dateError = true;
    return false;
  }
						
  var saveMonth  = saveDate.substring(0,firstSlash);	
  var saveDay = saveDate.substring(firstSlash+1,secondSlash);	
  var saveYear   = saveDate.substring(secondSlash+1);	

  for (i = 0; i < saveDay.length; i++)
  {
    var currchar = saveDay.charAt(i);
    if (! isDigit(currchar) )
    {
       return false;
    }
  }
						
  for (i = 0; i < saveMonth.length; i++)
  {
    var currchar = saveMonth.charAt(i);
    if (! isDigit(currchar) )
    {
       return false;
    }
  }

  for (i = 0; i < saveYear.length; i++)
  {
    var currchar = saveYear.charAt(i);
    if (! isDigit(currchar) )
    {
      return false;
    }
  }

  if (saveDay.length == 1) 
  {
    saveDay = "0" + saveDay;
  }

  if (saveMonth.length == 1) 
  {
    saveMonth = "0" + saveMonth;
  }


  if ((saveMonth.length > 2) || (saveDay.length > 2) || (saveYear.length < 2))
  {
    dateError = true;
    return false;
  }
								
					
  if (saveYear.length == 2)
  {
    if (saveYear < 51) 
    {
      saveYear = "20" + saveYear;
    }
    else
    {
      saveYear = "19" + saveYear;
    }
  }
			
  if (( saveMonth < 1 ) || ( saveMonth > 12 ) || (saveDay < 1) || (saveDay > 31) || (saveYear < 1900) || (saveYear > 2099) )
  {
    dateError = true;
    return false;
  }
					
  var numDays = getDays(saveMonth, saveYear);

  if (saveDay > numDays)
  {
    dateError = true;
    return false;
  }
  
  return true;
 }
}

// ------------------------------------------------------------------------------------------------
function ISDateValidSE (saveDate)
{
         
  var dateError = false;
  var dateLen   = saveDate.length;

  if (dateLen > 0)
  {

  var firstSlash  = saveDate.indexOf("-");
  var secondSlash = saveDate.lastIndexOf("-");

  if ( ( firstSlash == -1) || ( secondSlash == -1 ) || ( firstSlash == secondSlash ) )
  {
    dateError = true;
    return false;
  }
						
  var saveYear  = saveDate.substring(0,firstSlash);	
  var saveMonth = saveDate.substring(firstSlash+1,secondSlash);	
  var saveDay   = saveDate.substring(secondSlash+1);	

  for (i = 0; i < saveDay.length; i++)
  {
    var currchar = saveDay.charAt(i);
    if (! isDigit(currchar) )
    {
       return false;
    }
  }
						
  for (i = 0; i < saveMonth.length; i++)
  {
    var currchar = saveMonth.charAt(i);
    if (! isDigit(currchar) )
    {
       return false;
    }
  }

  for (i = 0; i < saveYear.length; i++)
  {
    var currchar = saveYear.charAt(i);
    if (! isDigit(currchar) )
    {
      return false;
    }
  }

  if (saveDay.length == 1) 
  {
    saveDay = "0" + saveDay;
  }

  if (saveMonth.length == 1) 
  {
    saveMonth = "0" + saveMonth;
  }


  if ((saveMonth.length > 2) || (saveDay.length > 2) || (saveYear.length < 2))
  {
    dateError = true;
    return false;
  }
								
					
  if (saveYear.length == 2)
  {
    if (saveYear < 51) 
    {
      saveYear = "20" + saveYear;
    }
    else
    {
      saveYear = "19" + saveYear;
    }
  }
			
  if (( saveMonth < 1 ) || ( saveMonth > 12 ) || (saveDay < 1) || (saveDay > 31) || (saveYear < 1900) || (saveYear > 2099) )
  {
    dateError = true;
    return false;
  }
					
  var numDays = getDays(saveMonth, saveYear);

  if (saveDay > numDays)
  {
    dateError = true;
    return false;
  }
  
  return true;
 }
}
	
//********************TIME************************
function ISTimeValidUS(saveDate)
{
	var dateError = false;
	var dateLen   = saveDate.length;
 			
  	if (dateLen != 8)
  	{
		return false;
	}

	var firstSlash  = saveDate.indexOf(":");

	if ( firstSlash == -1)
	{
		dateError = true;
 		return false;
	}
			
	var saveHour  = saveDate.substring(0,firstSlash);	
	var saveMinute = saveDate.substring(firstSlash+1,firstSlash+3);	
	var typen = saveDate.substring(dateLen-2,dateLen) ;

	for (i = 0; i < saveHour.length; i++)
	{
 		var currchar = saveHour.charAt(i);
 		if (! isDigit(currchar) )
 		{
    			return false;
 		}
	}
			
	for (i = 0; i < saveMinute.length; i++)
	{
 		var currchar = saveMinute.charAt(i);
 		if (! isDigit(currchar) )
 		{
    			return false;
 		}
	}

	if (!((typen="AM") || (typen="PM") ))
	{
  		return false;
	}
	if ((saveHour<1) || (saveHour>12) || (saveMinute>59) || (saveMinute<0))
	{
  		return false;
	}

  
  return true;
 
}
//************************************************

function getDays(month, year) 
  {
			        
    var days = new Array(13);
				        
    days[0] = 0;	  // 
    days[1] = 31;  // January
    days[2] = (leapYear(year)) ? 29 : 28 ; // February
    days[3] = 31;  // March
    days[4] = 30 ; // April
    days[5] = 31 ; // May
    days[6] = 30 ; // June
    days[7] = 31 ; // July
    days[8] = 31 ; // August
    days[9] = 30 ; // September
    days[10] = 31 ;// October
    days[11] = 30 ;// November
    days[12] = 31 ;// December

  return days[parseFloat(month)] ;

  }

//************************************************

function leapYear(year) 
  {
    if (year % 4 == 0)
    { 
      return true;  // is leap year
    }

    return false;   // is not leap year
  }
//************************************************

function isDigit (c)
{  
 return ((c >= "0") && (c <= "9"));
}


/* ======================================================================
FUNCTION:  	trim_string

INPUT:		

RETURN:  	
====================================================================== */
function trim_string(){

   var ichar, icount;
   var strValue = this;

   ichar = strValue.length -1;
   icount = -1;

   while (strValue.charAt(ichar)==' ' && ichar > icount )
    -- ichar;

   if (ichar!=(strValue.length-1))
      strValue = strValue.slice(0,ichar+1);

   ichar = 0;
   icount = strValue.length -1;
   
   while (strValue.charAt(ichar)==' ' && ichar < icount )
    ++ ichar;

   if (ichar!=0)
     strValue = strValue.slice(ichar,strValue.length);

   return strValue;
}


/* ======================================================================
FUNCTION:  	IsAlpha

INPUT:		str (string) - the string to be tested

RETURN:  	true, if the string contains only alphabetic characters false, otherwise.
====================================================================== */
function IsAlpha( str ) {
	
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;
	str += "";	

  	for (i = 0; i < str.length; i++) {
	
		if ( !( ((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z")) ) ) {
         				isValid = false;
         				break;
      			}
   } 
   	return isValid;
}  

/* ======================================================================
FUNCTION:	IsAlphaNum

INPUT:		str (string), A string that will be tested to ensure that
      		each character is a digit or a letter.

RETURN:  	true, if all characters in the string are a character from 0-9
     		or a-z or A-Z;  false, otherwise
====================================================================== */
function IsAlphaNum( str ) {
	
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;
   	str += "";	

   	for (i = 0; i < str.length; i++){
			
      	if (!(((str.charAt(i) >= "0") && (str.charAt(i) <= "9")) || 
      			((str.charAt(i) >= "a") && (str.charAt(i) <= "z")) ||
      			((str.charAt(i) >= "A") && (str.charAt(i) <= "Z"))))
			{
				isValid = false;
				break;
			}	
   	} 
   
   	return isValid;
}  

/* ======================================================================
FUNCTION:	IsBlank
 
INPUT:		val - the value to be tested

RETURN:  	true, if the string is null, undefined or an empty string, ""
      		false, otherwise.

CALLS:		IsNull(), IsUndef() 
====================================================================== */
function IsBlank( str ) {
	var isValid = false;

 	if ( IsNull(str) || IsUndef(str) || (str+"" == "") )
 		isValid = true;
		
	return isValid;
} 

/* ======================================================================
FUNCTION:  	IsInt
 
INPUT:  	numstr (string/number) 
			The string that will be tested to ensure that each character is a digit
		allowNegatives (boolean) 
			(optional) when true, allows numstr to be
			negative (contain a '-').  When false,	any negative number or a string starting
			with a '-' will be considered invalid.

RETURN:  	true, if all characters in the string are a character from 0-9,
		regardless of value for allowNegatives
		true, if allowNegatives is true and the string starts with a '-', and all other
		characters are 0-9. false, otherwise.
====================================================================== */
function IsInt( numstr, allowNegatives ) {
	
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	
	if (allowNegatives+"" == "undefined" || allowNegatives+"" == "null")	
		allowNegatives = true;

	var isValid = true;
	numstr += "";	

	for (i = 0; i < numstr.length; i++) {
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == "-"))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) || 
				(numstr.charAt(i) == "-" && !allowNegatives)) {
       	isValid = false;
       	break;
      }
         	         	       
   } 
   	return isValid;
}  

/* ======================================================================
FUNCTION:	IsNull
 
INPUT:		val - the value to be tested

RETURN:  	true, if the value is null; false, otherwise.
====================================================================== */
function IsNull( val ) {
	var isValid = false;

 	if (val+"" == "null")
 		isValid = true;
		
	return isValid;
}  

/* ======================================================================
FUNCTION:  	IsNum
 
INPUT:  	numstr (string/number) - the string that will be tested to ensure 
	        that the value is a number (int or float)

RETURN:  	true, if all characters represent a valid integer or float
 		false, otherwise.
====================================================================== */
function IsNum( numstr ) {
	
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	var isValid = true;
	var decCount = 0;		

	numstr += "";	

	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
				(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       

   }   
   	return isValid;
}  

/* ======================================================================
FUNCTION:	IsUndef
 
INPUT:		val - the value to be tested

RETURN:  	true, if the value is undefined
      		false, otherwise.
====================================================================== */
function IsUndef( val ) {
	var isValid = false;

 	if (val+"" == "undefined")
 		isValid = true;
		
	return isValid;
} 


/* ======================================================================
FUNCTION:  	IsValidEmail
 
INPUT:    	str (string) - an e-mail address to be tested

RETURN:  	true, if the string contains a valid e-mail address which is a string
		plus an '@' character followed by another string containing at least 
		one '.' and ending in an alpha (non-punctuation) character.
		false, otherwise

CALLS:		IsBlank(), IsAlpha() 
====================================================================== */
function IsValidEmail( str ) {
	
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	var isValid = true;

	str += "";

	namestr = str.substring(0, str.indexOf("@"));  // everything before the '@'
	domainstr = str.substring(str.indexOf("@")+1, str.length); 

	if (IsBlank(str) || (namestr.length == 0) || 
			(domainstr.indexOf(".") <= 0) ||
			(domainstr.indexOf("@") != -1) ||
			!IsAlpha(str.charAt(str.length-1)))
		isValid = false;
   
   	return isValid;
} 

// ====================================================================
// ====================================================================
// ====================================================================
// ====================================================================
// ====================================================================
function proto_nonValidEmail(item) {
	
    item.value.Trim();

    if (!IsValidEmail(item.value)){
      item.focus();
      alert("Field must contain a valid e-mail address.");
      return false;
    }
 
    return true;
}


// ============================================================
function proto_nonBlank(item){

    item.value.Trim();

    if (item.value.length==0){
      item.focus();
      alert("The field can not be empty.");
      return false;
    }
 
    return true;
}

// ============================================================
function proto_nonInt(item){

    item.value.Trim();

    if (!IsInt(item.value)){
      item.focus();
      alert("You must enter a number.");
      return false;
    }
 
    return true;
}

// ============================================================
function proto_nonNum(item){

    item.value.Trim();

    if (!IsNum(item.value)){
      item.focus();
      alert("Field must be numeric.");
      return false;
    }
 
    return true;
}

// ============================================================
function proto_nonAlpha(item){

    item.value.Trim();

    if (!IsAlpha(item.value)){
      item.focus();
      alert("Field must be alphabetic characters only.");
      return false;
    }
 
    return true;
}

// ============================================================
function proto_nonAlphaNum(item){

    item.value.Trim();

    if (!IsAlphaNum(item.value)){
      item.focus();
      alert("Field must be alphabetic or numeric characters only.");
      return false;
    }
 
    return true;
}

// ============================================================
function proto_nonValidDateSE(item){

    item.value.Trim();

    if (!ISDateValidSE(item.value)){
      item.focus();
      alert("The date is not correct. Valid format is YYYY-MM-DD");
      return false;
    }
 
    return true;
}

// ============================================================
function proto_nonValidDateUS(item){

    item.value.Trim();

    if (!ISDateValidUS(item.value)){
      item.focus();
      alert("The date is not correct. Valid format is MM/DD/YYYY . ");
      return false;
    }
 
    return true;
}

// ============================================================
function proto_nonValidDateEU(item){

    item.value.Trim();

    if (!ISDateValidEU(item.value)){
      item.focus();
      alert("The date is not correct. Valid format is DD/MM/YYYY . ");
      return false;
    }
 
    return true;
}

function proto_nonValidDateAndTime(item){

    cTime = item.substring(11,19);
	cDate = item.substring(0,10); 
    if ((item.length!=19) || (!ISTimeValidUS(cTime)) || (!ISDateValidUS(cDate))){
      alert("The date is not correct. Valid format is MM/DD/YYYY HH:MM PP . ");
      return false;
    }
 
    return true;
}


// ============================================================
function proto_nonValidDate(item){

    item.value.Trim();


      if (!ISDateValidSE(item.value)){
       item.focus();
       alert("The date is not correct. Valid format is YYYY-MM-DD");
       return false;
      }
   

    return true;
}


// ============================================================
// ============================================================
// ============================================================
function validation_setup(){
  
   this.nonBlank = proto_nonBlank;
   this.nonValidEmail = proto_nonValidEmail;
   this.nonInt = proto_nonInt;
   this.nonNum = proto_nonNum;
   this.nonAlpha = proto_nonAlpha;
   this.nonAlphaNum = proto_nonAlphaNum;
   this.nonValidDateSE = proto_nonValidDateSE;
   this.nonValidDateUS = proto_nonValidDateUS;
   this.nonValidDateEU = proto_nonValidDateEU;
   this.nonValidDate = proto_nonValidDate;  

   return this;

}

// -----------------------------------------------------------
  

  String.prototype.Trim = trim_string;

  var validation = new Object;
  validation = validation_setup();
