// DEFINE VARIABLES
var whitespace = " \t\n\r";    // whitespace characters

/****************************************************************/
// Check whether string s is empty.

function IsEmpty(s) { return ((s == null) || (s.length == 0)) }

/****************************************************************/
/******************************************************************/
// Is (s) whitespace
function IsWhitespace (s) {
    var i;
    // Is s empty?
    if (s == null) return true;

    // Search through string's characters one by one until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}  // END IsWhitespace()

// Does (s)have any whitespace
function IsNoWhitespace (s) {
    var i;
    // Is s empty?
    if (IsBlank(s)) {return true;}

    // Search through string's characters one by one until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
			// Check that current character isn't whitespace.
			var c = s.charAt(i);
			if (whitespace.indexOf(c) != -1) return true;
    }

    // All characters are whitespace.
    return false;
}  // END IsNoWhitespace()

/******************************************************************/
// Is (s) blank or just space
function IsBlank(s) {
  if ((s == null) || (s.length==0) || IsWhitespace(s)) {return true;}
  return false;
}  // END IsBlank()

/******************************************************************/
// Is (s) a valid number
function IsNumber(s) {
  if (IsBlank(s)) {return false}
  for(var i=0; i<s.length; i++) {
    var c = s.charAt(i);
    if ((c<'0') || (c>'9')) {return false;} // If Contains Other Than 0-9 >> Invalid
  }
  return true;
} // END IsNumber()

/******************************************************************/
// Is (s) a valid decimal number (not infallable! accepts more than 1 '.')
function IsDecimal(s) {
  if (IsBlank(s)) {return false}
  for(var i=0; i<s.length; i++) {
    var c = s.charAt(i);
    if ( ((c<'0') || (c>'9')) && (c!='.') ) {return false;} // If Contains Other Than 0-9 or . >> Invalid
  }
  return true;
} // END IsDecimal()

/******************************************************************/
// Is (s) a valid amount (number with commas like 1,000,000)
function IsAmount(s) {
  if (IsNumber(s)) {
    return true;
  } else {
    var okay=0;
    while (! okay) {
      var len=s.length;
      if (len > 4){ 
        var tmp=s.substring(len-4, len-1);
        if (tmp.charAt(0)!=',' || !(IsNumber(tmp.substring(1,4)))) {return false;}
        s = s.substring(0,len-4);
      } else {
        if (!IsNumber(s) && (s.charAt(0)!='0')) {return false;}
        okay++;
      }
    }
    return true;
  } // End If
} // END IsAmount()

/******************************************************************/
// Is (s) a valid date (not perfect)
function IsDate(s) {
  if (IsBlank(s)) {
    return false;
  } else {
    if (isNaN(Date.parse(s))) { return true; }
  }
} // END IsDate()

/****************************************************************/
// IsEmail (STRING s)
// 
// Email address must be of form a@b.c :
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required

function isEmail (s) {
    if (IsEmpty(s) ||IsWhitespace(s)) { return false; }
    
    // there must be at least 1 character before @
    // so start looking at character position 1
    var i = 1;
    var sLength = s.length;

    // look for "@"
    while ((i < sLength) && (s.charAt(i) != "@"))  { i++ }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for "."
    while ((i < sLength) && (s.charAt(i) != ".")) { i++ }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
} // END IsEmail()

// Open a URL in a window with the specified options, with given width/height and top/left
function OpenURLWindowAt(url,OptionStr,w,h,t,l) {
	if (OptionStr=='') {OptionStr='toolbar=no,directories=no,status=no,scrollbars=yes,resizeable=no,resize=no,menubar=no';}
	var WinName = new String("Window"+parseInt(Math.random()*1000));
	var newTop = (screen.height - h)/2;
	var newLeft = (screen.width - w)/2;
	window.open(url,WinName,OptionStr+",height="+h+",width="+w+",top="+newTop+",left="+newLeft);
}

	function CheckRegFields() {
		if (isEmail(document.RegistrationForm.register_email.value)) {return true;}
		else {window.alert('Please enter a valid email address \nto receive regular updates and information. \nThank You.'); return false;}
	}
	function SaveRegistration() {
		if (CheckRegFields()==true) {
			document.RegistrationForm.submit();
		}
	} // DoSubmit()

/****************************************/
//new stuff


function _CF_onError(form_object, input_object, object_value, error_message)
    {
	alert(error_message);
       	return false;	
    }



function _CF_checkdate(object_value)
    {
    //Returns true if value is a date format or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)
		return false;

    sMonth = object_value.substring(0, isplit);

	if (sMonth.length == 0)
        return false;

	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

    sDay = object_value.substring((sMonth.length + 1), isplit);

	if (sDay.length == 0)
        return false;

	sYear = object_value.substring(isplit + 1);

	if (!_CF_checkinteger(sMonth)) //check month
		return false;
	else
	if (!_CF_checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!_CF_checkinteger(sYear)) //check year
		return false;
	else
	if (!_CF_checkrange(sYear, 0, 9999)) //check year
		return false;
	else
	if (!_CF_checkinteger(sDay)) //check day
		return false;
	else
	if (!_CF_checkday(sYear, sMonth, sDay)) // check day
		return false;
	else
		return true;
    }



function _CF_checkday(checkYear, checkMonth, checkDay)
    {

	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
			checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return _CF_checkrange(checkDay, 1, maxDay); //check day
    }



function _CF_checkinteger(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)
    //Was it a decimal?
    if (check_char < 1)
	return _CF_checknumber(object_value);
    else
	return false;
    }



function _CF_numberrange(object_value, min_value, max_value)
    {
    // check minimum
    if (min_value != null)
	{
        if (object_value < min_value)
		return false;
	}

    // check maximum
    if (max_value != null)
	{
	if (object_value > max_value)
		return false;
	}
	
    //All tests passed, so...
    return true;
    }



function _CF_checknumber(object_value)
    {
    //Returns true if value is a number or is NULL
    //otherwise returns false	

    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks

		}
	        else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
    //All tests passed, so...
    return true
    }



function _CF_checkrange(object_value, min_value, max_value)
    {
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;


    if (!_CF_checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (_CF_numberrange((eval(object_value)), min_value, max_value));
	}
	
    //All tests passed, so...
    return true;
    }

function _CF_hasValue(obj, obj_type)
    {
    if (obj_type == "TEXT" || obj_type == "PASSWORD")
	{
    	if (obj.value.length == 0) 
      		return false;
    	else 
      		return true;
    	}
    else if (obj_type == "SELECT")
	{
        for (i=0; i < obj.length; i++)
	    	{
		if (obj.options[i].selected)
			return true;
		}

       	return false;	
	}
    else if (obj_type == "SINGLE_VALUE_RADIO" || obj_type == "SINGLE_VALUE_CHECKBOX")
	{

		if (obj.checked)
			return true;
		else
       		return false;	
	}
    else if (obj_type == "RADIO" || obj_type == "CHECKBOX")
	{

        for (i=0; i < obj.length; i++)
	    	{
		if (obj[i].checked)
			return true;
		}

       	return false;	
	}
	}



function  _CF_checkCFForm_1(_CF_this)

    {

    if  (!_CF_checkdate(_CF_this.date1.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.date1, _CF_this.date1.value, "Please enter date in correct format"))

            {

            return false; 

            }

        }


    if  (!_CF_checkdate(_CF_this.date2.value))

        {

        if  (!_CF_onError(_CF_this, _CF_this.date2, _CF_this.date2.value, "Please enter date in correct format"))

            {

            return false; 

            }

        }


    return true;

    }


function  _CF_checkCFForm_2(_CF_this)

    {

    if  (!_CF_hasValue(_CF_this.Contact, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.Contact, _CF_this.Contact.value, "Please enter your contact name!"))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.Address, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.Address, _CF_this.Address.value, "Please enter your address!"))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.Zip, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.Zip, _CF_this.Zip.value, "Please enter your PostCode!"))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.Phone, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.Phone, _CF_this.Phone.value, "Please enter your Phone Number!"))

            {

            return false; 

            }

        }


    if  (!_CF_hasValue(_CF_this.login, "TEXT" )) 

        {

        if  (!_CF_onError(_CF_this, _CF_this.login, _CF_this.login.value, "Please enter your Phone Number!"))

            {

            return false; 

            }

        }


    return true;

    }




