// JavaScript Document
// Author: Adi Toal
// Created: July 2003
// Copyright European Software
// www.europeansoftware.co.uk - 024 76 531192
// Shared Javascript functions for inspiring money website

// Validate a date down to dd/mm/yyyy
function is_date(date_value){
var checkstr = "0123456789";
var date_temp = "";
var seperator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   /* Delete all chars except 0..9 */
   for (i = 0; i < date_value.length; i++) {
	  if (checkstr.indexOf(date_value.substr(i,1)) >= 0) {
	     date_temp = date_temp + date_value.substr(i,1);
	  }
   }
   date_value = date_temp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (date_value.length == 6) {
      date_value = date_value.substr(0,4) + '20' + date_value.substr(4,2); }
   if (date_value.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = date_value.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = date_value.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = date_value.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
     return true;
   } else {
   	 return false;
   }
}
// Simple function to check a piece of data is only numeric
function check_numeric(test_data) {
	var num_str = "0123456789.";
	var this_char;
	var counter = 0;

	for (var i=0; i < test_data.length; i++) {
		this_char = test_data.substring(i, i+1);
		if (num_str.indexOf(this_char) != -1)
			counter ++;	
	}
	return (counter == test_data.length);
}
// Validate the members registration details
function check_login() {
  var error = 0;
  var error_message = "";   
  var username      = document.login.username.value;
  var password      = document.login.userpassword.value;  
  if (username == "" || username.length < 3) {
    error_message = error_message + "* Enter your username (min 3 letters)\n";
    error = 1;
  } 
  if (password == "" || password.length < 3) {
    error_message = error_message + "* Enter your password (min 3 letters)\n";
    error = 1;
  }
  // If error = 1 then the validation has failed so return the errors  
  if (error == 1) {
  	alert(error_message);
    return false;
  } else {
  	return true;
  }    
}

function check_password_reminder() {
  var error = 0;
  var error_message = "";   
  var username      = document.password_reminder.user_name.value;
  var phone         = document.password_reminder.phone_number.value;  
  var email         = document.password_reminder.email_address.value;    
  if (username == "" || username.length < 3) {
    error_message = error_message + "* Enter your username (min 3 letters)\n";
    error = 1;
  } 
  if (phone == "") {
    error_message = error_message + "* Enter your phone number\n";
    error = 1;
  }
  if (email == "") {
    error_message = error_message + "* Enter your email address\n";
    error = 1;
  }
  
  // If error = 1 then the validation has failed so return the errors  
  if (error == 1) {
  	alert(error_message);
    return false;
  } else {
  	return true;
  }    
}



/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}
 


