 function setCookie(NameOfCookie, value, expiredays) {
   var ExpireDate = new Date ();
   ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
   document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}


function saveForm() {
  var form = document.forms[0];
  if (parent.failNull (form.LName, 'Please enter your Last Name.','text')) {return};
  if (parent.failNull (form.FName, 'Please enter your First Name.','text')) {return};
  if (parent.failNull (form.Password, 'Please enter a Password.','text')) {return};
  if (parent.failNull (form.Email, 'Please enter an Email.','text')) {return};
  if (!isNumber(form.Rate1)) { alertBox (form.Rate1, 'Please enter only valid numbers for the Rate.', 'text'); return; };
  if (!isDate(form.BirthDate)) { alertBox (form.BirthDate, 'Please enter a valid date dd/mm/yyyy', 'text'); return; };
  if (!isDate(form.AvailableDate1)) { alertBox (form.AvailableDate1, 'Please enter a valid date dd/mm/yyyy', 'text'); return; };
  if (!isDate(form.AvailableDate2)) { alertBox (form.AvailableDate2, 'Please enter a valid date dd/mm/yyyy', 'text'); return; };

  form.submit();
}

// ---------------------------------------------
// --- Field Validation                       --
// ---------------------------------------------

function failNull ( vField, vMessage, vType) { 

//this function will stop the submit if a field is null or contains all spaces 

//get the field value...
theValue = getFieldValue (vField, vType); 

//if the field value is null, we fail and return true...
if ( theValue == "" ) { 

   alertBox ( vField, vMessage, vType ); 

return ( true ); 

} 

//remove any spaces from the value 

trimField = trimBlanks( theValue ); 

//if the field value is all spaces, fail and return true...
if ( trimField =="" ) { 

   alertBox ( vField, vMessage, vType ); 

return ( true ); 

} 

//otherwise continue... 

return ( false ); 

}

function getFieldValue ( theField, vType ) { 

//this function will return the field value (or value list) based on the element type 

theValue = ""; 
sep = ""; 
hits = 0; 

//text is the user-entered value as a string
if ( vType == "text" ) return ( theField.value ); 

//textarea is the user-entered value as a string array of one element
if ( vType == "textarea" ) return ( theField[0].value ); 

//checkboxes & radio buttons are not so simple
if ( vType == "checkbox" || vType == "radio" ) { 

if ( theField.value == null ) { 

//if we're here, we are validating a radio button or a nn multi-element checkbox 

for ( i = 0; i < theField.length; i++ ) { 
if ( theField[i].checked ) { 
hits++; 
if ( hits > 1 ) {
sep = "; ";
} 
theValue += sep + theField[i].value; 
} 
}
} 
return ( theValue );

} else { 

//if we are here, must be an ie checkbox, or nn with a one-element checkbox) 

if ( navigator.appName == "Microsoft Internet Explorer" ) { 

//ie. return some data so we can validate on the server; 
return ("can't validate on client")
} 

//nn one-element checkbox, see if its checked ...
if (theField.checked ) { 
return ( theField.value ); 

} else {
return ( "" ); 
} 
} 

//select is an array of selection pointers to an array of strings representing the choices
if ( vType == "select" ) { 

for ( i = 0; i < theField.options.length; i++ ) {
if ( theField.options[i].selected ) theValue += theField.options[i].text 
} 
return ( theValue );
}
}

 function trimBlanks ( theString, repChar ) { 

//this function replaces the spaces in a string with the provided character 

trimString=""; 

for ( i = 0; i < theString.length; i++) {
theChar=theString.substring ( i, i+1);
if ( theChar == " ") { 

trimString += repChar 

} else { 

trimString+=theString.substring ( i, i+1); 

}
}
return (trimString);
}

function alertBox (vField, vMessage, vType ) { 

//if we pass a null message, we are doing a multiple validation test. return so we can check other conditions.
if ( vMessage == "null" ) return; 

//otherwise, display the error message
alert ( "Some entries are not correct:\n\n" + vMessage ) 

//and set focus (plus select the text if a text element) 
if ( vType == "text" ) { 

vField.focus(); 

vField.select(); 

} else { 

vField.focus(); 

} 

return; 

}


function data_change(field)
     {
          var check = true;
          var value = field.value; //get characters
          //check that all characters are digits, ., -, or ""
          for(var i=0;i < field.value.length; ++i)
          {
               var new_key = value.charAt(i); //cycle through characters
               if(((new_key < "0") || (new_key > "9")) && !(new_key == "") && !(new_key == "."))
               {
                    check = false;
                    break;
               }
          }
          //apply appropriate colour based on value
          if(!check)
          {
               field.style.backgroundColor = "red";
          }
          else
          {
               field.style.backgroundColor = "white";
          }
     }

//Function to validate number input
function isNumber(vField) 
{
     //If anything in our string is not a number, fail validation
     theValue = getFieldValue (vField, 'text'); 
     if (!theValue.match(/^\d*\.?\d*$/))
     //if (!theValue.match(/^\d*(\.|\,)?\d*$/))
        { return false; }
     return true;
}

function otherisNumber(vField) {
    theValue = getFieldValue (vField, 'text'); 
    trimField = trimBlanks( theValue ); 
    if ( trimField =="" ) { return true };
    if (trimField >= 0) { return true };
    return false; 
} 

//Function to validate date input
function isDate(vField) 
{
    //If anything in our string is not a date, fail validation
    theValue = getFieldValue (vField, 'text'); 
    trimField = trimBlanks( theValue ); 
    if ( trimField =="" ) { return true };

    if (!theValue.match(/(^((((0[1-9])|([1-2][0-9])|(3[0-1]))|([1-9]))\x2F(((0[1-9])|(1[0-2]))|([1-9]))\x2F(([0-9]{2})|(((19)|([2]([0]{1})))([0-9]{2}))))$)/))
       { return false; }
    return true;
}

