
	// site object
	var site = {};
	
	// md5
	var MD5 = function (string) { function RotateLeft(lValue, iShiftBits) {  return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));  }  function AddUnsigned(lX,lY) {  var lX4,lY4,lX8,lY8,lResult;  lX8 = (lX & 0x80000000);  lY8 = (lY & 0x80000000);  lX4 = (lX & 0x40000000);  lY4 = (lY & 0x40000000);  lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);  if (lX4 & lY4) {  return (lResult ^ 0x80000000 ^ lX8 ^ lY8);  }  if (lX4 | lY4) {  if (lResult & 0x40000000) {  return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);  } else {  return (lResult ^ 0x40000000 ^ lX8 ^ lY8);  }  } else {  return (lResult ^ lX8 ^ lY8);  }  }  function F(x,y,z) { return (x & y) | ((~x) & z); }  function G(x,y,z) { return (x & z) | (y & (~z)); }  function H(x,y,z) { return (x ^ y ^ z); }  function I(x,y,z) { return (y ^ (x | (~z))); }  function FF(a,b,c,d,x,s,ac) {  a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));  return AddUnsigned(RotateLeft(a, s), b);  };  function GG(a,b,c,d,x,s,ac) {  a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));  return AddUnsigned(RotateLeft(a, s), b);  };  function HH(a,b,c,d,x,s,ac) {  a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));  return AddUnsigned(RotateLeft(a, s), b);  };  function II(a,b,c,d,x,s,ac) {  a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));  return AddUnsigned(RotateLeft(a, s), b);  };  function ConvertToWordArray(string) {  var lWordCount;  var lMessageLength = string.length;  var lNumberOfWords_temp1=lMessageLength + 8;  var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;  var lNumberOfWords = (lNumberOfWords_temp2+1)*16;  var lWordArray=Array(lNumberOfWords-1);  var lBytePosition = 0;  var lByteCount = 0;  while ( lByteCount < lMessageLength ) {  lWordCount = (lByteCount-(lByteCount % 4))/4;  lBytePosition = (lByteCount % 4)*8;  lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<<lBytePosition));  lByteCount++;  }  lWordCount = (lByteCount-(lByteCount % 4))/4;  lBytePosition = (lByteCount % 4)*8;  lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);  lWordArray[lNumberOfWords-2] = lMessageLength<<3;  lWordArray[lNumberOfWords-1] = lMessageLength>>>29;  return lWordArray;  };  function WordToHex(lValue) {  var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;  for (lCount = 0;lCount<=3;lCount++) {  lByte = (lValue>>>(lCount*8)) & 255;  WordToHexValue_temp = "0" + lByte.toString(16);  WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);  }  return WordToHexValue;  };  function Utf8Encode(string) {  string = string.replace(/\r\n/g,"\n");  var utftext = "";  for (var n = 0; n < string.length; n++) {  var c = string.charCodeAt(n);  if (c < 128) {  utftext += String.fromCharCode(c);  }  else if((c > 127) && (c < 2048)) {  utftext += String.fromCharCode((c >> 6) | 192);  utftext += String.fromCharCode((c & 63) | 128);  }  else {  utftext += String.fromCharCode((c >> 12) | 224);  utftext += String.fromCharCode(((c >> 6) & 63) | 128);  utftext += String.fromCharCode((c & 63) | 128);  }  }  return utftext;  };  var x=Array();  var k,AA,BB,CC,DD,a,b,c,d;  var S11=7, S12=12, S13=17, S14=22;  var S21=5, S22=9 , S23=14, S24=20;  var S31=4, S32=11, S33=16, S34=23;  var S41=6, S42=10, S43=15, S44=21;  string = Utf8Encode(string);  x = ConvertToWordArray(string);  a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;  for (k=0;k<x.length;k+=16) {  AA=a; BB=b; CC=c; DD=d;  a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);  d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);  c=FF(c,d,a,b,x[k+2], S13,0x242070DB);  b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);  a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);  d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);  c=FF(c,d,a,b,x[k+6], S13,0xA8304613);  b=FF(b,c,d,a,x[k+7], S14,0xFD469501);  a=FF(a,b,c,d,x[k+8], S11,0x698098D8);  d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);  c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);  b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);  a=FF(a,b,c,d,x[k+12],S11,0x6B901122);  d=FF(d,a,b,c,x[k+13],S12,0xFD987193);  c=FF(c,d,a,b,x[k+14],S13,0xA679438E);  b=FF(b,c,d,a,x[k+15],S14,0x49B40821);  a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);  d=GG(d,a,b,c,x[k+6], S22,0xC040B340);  c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);  b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);  a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);  d=GG(d,a,b,c,x[k+10],S22,0x2441453);  c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);  b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);  a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);  d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);  c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);  b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);  a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);  d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);  c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);  b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);  a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);  d=HH(d,a,b,c,x[k+8], S32,0x8771F681);  c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);  b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);  a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);  d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);  c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);  b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);  a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);  d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);  c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);  b=HH(b,c,d,a,x[k+6], S34,0x4881D05);  a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);  d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);  c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);  b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);  a=II(a,b,c,d,x[k+0], S41,0xF4292244);  d=II(d,a,b,c,x[k+7], S42,0x432AFF97);  c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);  b=II(b,c,d,a,x[k+5], S44,0xFC93A039);  a=II(a,b,c,d,x[k+12],S41,0x655B59C3);  d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);  c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);  b=II(b,c,d,a,x[k+1], S44,0x85845DD1);  a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);  d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);  c=II(c,d,a,b,x[k+6], S43,0xA3014314);  b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);  a=II(a,b,c,d,x[k+4], S41,0xF7537E82);  d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);  c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);  b=II(b,c,d,a,x[k+9], S44,0xEB86D391);  a=AddUnsigned(a,AA);  b=AddUnsigned(b,BB);  c=AddUnsigned(c,CC);  d=AddUnsigned(d,DD);  }  var temp = WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);  return temp.toLowerCase();  }  
	
	// request function
	function request(plugin,parameters,handler){
		var pars = {};
		pars[plugin] = parameters;
		new Ajax.Request('/json.php',{
		    method:'post',
			parameters: {json: $H(pars).toJSON()},
		    onSuccess: function(transport,json){
				handler(transport,json[plugin],json)
			}
		});
	}
	
	// d
	function d(m){console.log(m)}
	function dd(m){console.dir(m)}
	
	document.observe('dom:loaded', function(){			
		$$('a.open-in-new-window').each(function(a){
			a.target = '_blank';
		});	
	});


/* aform */

    
    var fields = {};

    document.observe('dom:loaded', function() {

        // only go on with an aform present
        if($$('.aform').length == 0)
            return;
            
        // shorthand, one form for starters
        var form = $$('.aform')[0];
        var form_id = form.select('input[name=form_id]')[0].value;
        
        // make the submit validate
        form.observe('submit',function(event){
            event.stop();
            form.fire('aform:validate');
        });
        
        // make enter validate
        form.select('input[type=text]').invoke('observe','keypress',function(event){
            form.fire('submit');
//          if(event.keyCode == Event.KEY_RETURN)
//              form.fire('aform:validate');
        });
        
        // single validates
        form.select('input').invoke('observe','focus',function(event){
            
            // skip perhaps?
            if(!this.hasClassName('instant_validation'))
                return;
                
            // otherwise validate it
            request('aform',{
                action: 'validate-single',
                name: this.name,
                value: this.value,
                form_id: form_id
            },function(json,errors){
                validationResults(this.name,errors);
            }.bind(this));          
        });

        // on prev or next, handle the verification, ajax style
        form.observe('aform:validate',function(event){
            
            // send the reuqest to validate and the values over     
            request('aform',{
                action: 'validate',
                values: form.serialize(true)
            },function(request,json){

                // error flag
                var has_errors = false;

                // go markup!
                json.fields.each(function(field){
                    if(validationResults(field.name,field.errors))
                        has_errors = true;
                });

                // go through?
                if (!has_errors)
                    form.submit();
            });         
        });     
        
        function validationResults(fieldname,errors){

            // container and error flag
            // remove the error class
            var container = $$('tbody.'+fieldname)[0];
            container.removeClassName('error');
            var has_errors = false;

            // get the feedback div, make it emtpy
            var feedback = container.select('.feedback')[0];
            feedback.childElements().invoke('remove');
    
            // are there errors? add the errors to the feedback container
            errors.each(function(error){
    
                // update the flag
                has_errors = true;
                
                // add the error div
                var line = new Element('div');
                feedback.insert(line);
                line.update(error); 
                
                // mark the input
                container.addClassName('error');
            });                 
            
            return has_errors;
        }
        
        
    });

/* Ausers */

	Event.observe(window, 'load', function(){

		// login initials
		if ($('login')) {
			$$('#login input[type=text]')[0].initial('Inlog code');
		}
	});

	Element.addMethods({
		initial: function(element,value){
			if(element.value == '')
				element.addClassName('initial');
			element.observe('focus',function(){
				if(element.value == value){
					element.removeClassName('initial');
					element.value = '';
				}
			});
			element.observe('blur',function(){
				if(element.value == ''){
					element.addClassName('initial');
					element.value = value;
				}
			});
			element.value = value;

			return element;
		}
	});

/* Home */

//tell a friend popup
function tell(){
	if(document.getElementById('darken').style.display=='none' && document.getElementById('tell-a-friend').style.display=='none'){
		document.getElementById('darken').style.display='';
		document.getElementById('tell-a-friend').style.display='';
	}
	else {
		document.getElementById('darken').style.display='none';
		document.getElementById('tell-a-friend').style.display='none';
	}
	
}

/* Search */


	document.observe('dom:loaded', function() {

		if(!$('refine-search'))
			return;
			
		$('set').observe('change',function(e){
			if(this.value == 'specials')
				$$('.sort')[0].show();
			else
				$$('.sort')[0].hide();
		});
	});

/* Uploads */

Event.observe(window, 'load', function(){

	// login initials
	if ($('login')) {
		$$('#login input[type=text]')[0].initial('Inlog code');
	}
});

Element.addMethods({
	initial: function(element,value){
		if(element.value == '')
			element.addClassName('initial');
		element.observe('focus',function(){
			if(element.value == value){
				element.removeClassName('initial');
				element.value = '';
			}
		});
		element.observe('blur',function(){
			if(element.value == ''){
				element.addClassName('initial');
				element.value = value;
			}
		});
		element.value = value;

		return element;
	}
});

// LiveValidation 1.3 (standalone version)
// Copyright (c) 2007-2008 Alec Hill (www.livevalidation.com)
// LiveValidation is licensed under the terms of the MIT License

/*********************************************** LiveValidation class ***********************************/

/**
 *	validates a form field in real-time based on validations you assign to it
 *	
 *	@var element {mixed} - either a dom element reference or the string id of the element to validate
 *	@var optionsObj {Object} - general options, see below for details
 *
 *	optionsObj properties:
 *							validMessage {String} 	- the message to show when the field passes validation
 *													  (DEFAULT: "Thankyou!")
 *							onValid {Function} 		- function to execute when field passes validation
 *													  (DEFAULT: function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); } )	
 *							onInvalid {Function} 	- function to execute when field fails validation
 *													  (DEFAULT: function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); })
 *							insertAfterWhatNode {Int} 	- position to insert default message
 *													  (DEFAULT: the field that is being validated)	
 *              onlyOnBlur {Boolean} - whether you want it to validate as you type or only on blur
 *                            (DEFAULT: false)
 *              wait {Integer} - the time you want it to pause from the last keystroke before it validates (ms)
 *                            (DEFAULT: 0)
 *              onlyOnSubmit {Boolean} - whether should be validated only when the form it belongs to is submitted
 *                            (DEFAULT: false)						
 */
var LiveValidation = function(element, optionsObj){
  	this.initialize(element, optionsObj);
}

LiveValidation.VERSION = '1.3 standalone';

/** element types constants ****/

LiveValidation.TEXTAREA 		= 1;
LiveValidation.TEXT 			    = 2;
LiveValidation.PASSWORD 		= 3;
LiveValidation.CHECKBOX 		= 4;
LiveValidation.SELECT = 5;
LiveValidation.FILE = 6;

/****** Static methods *******/

/**
 *	pass an array of LiveValidation objects and it will validate all of them
 *	
 *	@var validations {Array} - an array of LiveValidation objects
 *	@return {Bool} - true if all passed validation, false if any fail						
 */
LiveValidation.massValidate = function(validations){
  var returnValue = true;
	for(var i = 0, len = validations.length; i < len; ++i ){
		var valid = validations[i].validate();
		if(returnValue) returnValue = valid;
	}
	return returnValue;
}

/****** prototype ******/

LiveValidation.prototype = {

    validClass: 'LV_valid',
    invalidClass: 'LV_invalid',
    messageClass: 'LV_validation_message',
    validFieldClass: 'LV_valid_field',
    invalidFieldClass: 'LV_invalid_field',

    /**
     *	initialises all of the properties and events
     *
     * @var - Same as constructor above
     */
    initialize: function(element, optionsObj){
      var self = this;
      if(!element) throw new Error("LiveValidation::initialize - No element reference or element id has been provided!");
    	this.element = element.nodeName ? element : document.getElementById(element);
    	if(!this.element) throw new Error("LiveValidation::initialize - No element with reference or id of '" + element + "' exists!");
      // default properties that could not be initialised above
    	this.validations = [];
      this.elementType = this.getElementType();
      this.form = this.element.form;
      // options
    	var options = optionsObj || {};
    	this.validMessage = options.validMessage || '';
    	var node = options.insertAfterWhatNode || this.element;
		this.insertAfterWhatNode = node.nodeType ? node : document.getElementById(node);
      this.onValid = options.onValid || function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); };
      this.onInvalid = options.onInvalid || function(){ this.insertMessage(this.createMessageSpan()); this.addFieldClass(); };	
    	this.onlyOnBlur =  options.onlyOnBlur || false;
    	this.wait = options.wait || 0;
      this.onlyOnSubmit = options.onlyOnSubmit || false;
      // add to form if it has been provided
      if(this.form){
        this.formObj = LiveValidationForm.getInstance(this.form);
        this.formObj.addField(this);
      }
      // events
      // collect old events
      this.oldOnFocus = this.element.onfocus || function(){};
      this.oldOnBlur = this.element.onblur || function(){};
      this.oldOnClick = this.element.onclick || function(){};
      this.oldOnChange = this.element.onchange || function(){};
      this.oldOnKeyup = this.element.onkeyup || function(){};
      this.element.onfocus = function(e){ self.doOnFocus(e); return self.oldOnFocus.call(this, e); }
      if(!this.onlyOnSubmit){
        switch(this.elementType){
          case LiveValidation.CHECKBOX:
            this.element.onclick = function(e){ self.validate(); return self.oldOnClick.call(this, e); }
          // let it run into the next to add a change event too
          case LiveValidation.SELECT:
          case LiveValidation.FILE:
            this.element.onchange = function(e){ self.validate(); return self.oldOnChange.call(this, e); }
            break;
          default:
            if(!this.onlyOnBlur) this.element.onkeyup = function(e){ self.deferValidation(); return self.oldOnKeyup.call(this, e); }
      	    this.element.onblur = function(e){ self.doOnBlur(e); return self.oldOnBlur.call(this, e); }
        }
      }
    },
	
	/**
     *	destroys the instance's events (restoring previous ones) and removes it from any LiveValidationForms
     */
    destroy: function(){
  	  if(this.formObj){
		// remove the field from the LiveValidationForm
		this.formObj.removeField(this);
		// destroy the LiveValidationForm if no LiveValidation fields left in it
		this.formObj.destroy();
	  }
      // remove events - set them back to the previous events
	  this.element.onfocus = this.oldOnFocus;
      if(!this.onlyOnSubmit){
        switch(this.elementType){
          case LiveValidation.CHECKBOX:
            this.element.onclick = this.oldOnClick;
          // let it run into the next to add a change event too
          case LiveValidation.SELECT:
          case LiveValidation.FILE:
            this.element.onchange = this.oldOnChange;
            break;
          default:
            if(!this.onlyOnBlur) this.element.onkeyup = this.oldOnKeyup;
      	    this.element.onblur = this.oldOnBlur;
        }
      }
      this.validations = [];
	  this.removeMessageAndFieldClass();
    },
    
    /**
     *	adds a validation to perform to a LiveValidation object
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Object} - the LiveValidation object itself so that calls can be chained
     */
    add: function(validationFunction, validationParamsObj){
      this.validations.push( {type: validationFunction, params: validationParamsObj || {} } );
      return this;
    },
    
	/**
     *	removes a validation from a LiveValidation object - must have exactly the same arguments as used to add it 
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Object} - the LiveValidation object itself so that calls can be chained
     */
    remove: function(validationFunction, validationParamsObj){
	  var found = false;
	  for( var i = 0, len = this.validations.length; i < len; i++ ){
	  		if( this.validations[i].type == validationFunction ){
				if (this.validations[i].params == validationParamsObj) {
					found = true;
					break;
				}
			}
	  }
      if(found) this.validations.splice(i,1);
      return this;
    },
    
	
    /**
     * makes the validation wait the alotted time from the last keystroke 
     */
    deferValidation: function(e){
      if(this.wait >= 300) this.removeMessageAndFieldClass();
    	var self = this;
      if(this.timeout) clearTimeout(self.timeout);
      this.timeout = setTimeout( function(){ self.validate() }, self.wait); 
    },
        
    /**
     * sets the focused flag to false when field loses focus 
     */
    doOnBlur: function(e){
      this.focused = false;
      this.validate(e);
    },
        
    /**
     * sets the focused flag to true when field gains focus 
     */
    doOnFocus: function(e){
      this.focused = true;
      this.removeMessageAndFieldClass();
    },
    
    /**
     *	gets the type of element, to check whether it is compatible
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     */
    getElementType: function(){
      switch(true){
        case (this.element.nodeName.toUpperCase() == 'TEXTAREA'):
        return LiveValidation.TEXTAREA;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'TEXT'):
        return LiveValidation.TEXT;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'PASSWORD'):
        return LiveValidation.PASSWORD;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'CHECKBOX'):
        return LiveValidation.CHECKBOX;
      case (this.element.nodeName.toUpperCase() == 'INPUT' && this.element.type.toUpperCase() == 'FILE'):
        return LiveValidation.FILE;
      case (this.element.nodeName.toUpperCase() == 'SELECT'):
        return LiveValidation.SELECT;
        case (this.element.nodeName.toUpperCase() == 'INPUT'):
        	throw new Error('LiveValidation::getElementType - Cannot use LiveValidation on an ' + this.element.type + ' input!');
        default:
        	throw new Error('LiveValidation::getElementType - Element must be an input, select, or textarea!');
      }
    },
    
    /**
     *	loops through all the validations added to the LiveValidation object and checks them one by one
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Boolean} - whether the all the validations passed or if one failed
     */
    doValidations: function(){
      	this.validationFailed = false;
      	for(var i = 0, len = this.validations.length; i < len; ++i){
    	 	var validation = this.validations[i];
    		switch(validation.type){
    		   	case Validate.Presence:
                case Validate.Confirmation:
                case Validate.Acceptance:
    		   		this.displayMessageWhenEmpty = true;
    		   		this.validationFailed = !this.validateElement(validation.type, validation.params); 
    				break;
    		   	default:
    		   		this.validationFailed = !this.validateElement(validation.type, validation.params);
    		   		break;
    		}
    		if(this.validationFailed) return false;	
    	}
    	this.message = this.validMessage;
    	return true;
    },
    
    /**
     *	performs validation on the element and handles any error (validation or otherwise) it throws up
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validate.Presence )
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     * @return {Boolean} - whether the validation has passed or failed
     */
    validateElement: function(validationFunction, validationParamsObj){
      	var value = (this.elementType == LiveValidation.SELECT) ? this.element.options[this.element.selectedIndex].value : this.element.value;     
        if(validationFunction == Validate.Acceptance){
    	    if(this.elementType != LiveValidation.CHECKBOX) throw new Error('LiveValidation::validateElement - Element to validate acceptance must be a checkbox!');
    		value = this.element.checked;
    	}
        var isValid = true;
      	try{    
    		validationFunction(value, validationParamsObj);
    	} catch(error) {
    	  	if(error instanceof Validate.Error){
    			if( value !== '' || (value === '' && this.displayMessageWhenEmpty) ){
    				this.validationFailed = true;
    				this.message = error.message;
    				isValid = false;
    			}
    		}else{
    		  	throw error;
    		}
    	}finally{
    	    return isValid;
        }
    },
    
    /**
     *	makes it do the all the validations and fires off the onValid or onInvalid callbacks
     *
     * @return {Boolean} - whether the all the validations passed or if one failed
     */
    validate: function(){
      if(!this.element.disabled){
		var isValid = this.doValidations();
		if(isValid){
			this.onValid();
			return true;
		}else {
			this.onInvalid();
			return false;
		}
	  }else{
      return true;
    }
    },
	
 /**
   *  enables the field
   *
   *  @return {LiveValidation} - the LiveValidation object for chaining
   */
  enable: function(){
  	this.element.disabled = false;
	return this;
  },
  
  /**
   *  disables the field and removes any message and styles associated with the field
   *
   *  @return {LiveValidation} - the LiveValidation object for chaining
   */
  disable: function(){
  	this.element.disabled = true;
	this.removeMessageAndFieldClass();
	return this;
  },
    
    /** Message insertion methods ****************************
     * 
     * These are only used in the onValid and onInvalid callback functions and so if you overide the default callbacks,
     * you must either impliment your own functions to do whatever you want, or call some of these from them if you 
     * want to keep some of the functionality
     */
    
    /**
     *	makes a span containg the passed or failed message
     *
     * @return {HTMLSpanObject} - a span element with the message in it
     */
    createMessageSpan: function(){
        var span = document.createElement('span');
    	var textNode = document.createTextNode(this.message);
      	span.appendChild(textNode);
        return span;
    },
    
    /**
     *	inserts the element containing the message in place of the element that already exists (if it does)
     *
     * @var elementToIsert {HTMLElementObject} - an element node to insert
     */
    insertMessage: function(elementToInsert){
      	this.removeMessage();
      	if( (this.displayMessageWhenEmpty && (this.elementType == LiveValidation.CHECKBOX || this.element.value == ''))
    	  || this.element.value != '' ){
            var className = this.validationFailed ? this.invalidClass : this.validClass;
    	  	elementToInsert.className += ' ' + this.messageClass + ' ' + className;
            if(this.insertAfterWhatNode.nextSibling){
    		  		this.insertAfterWhatNode.parentNode.insertBefore(elementToInsert, this.insertAfterWhatNode.nextSibling);
    		}else{
    			    this.insertAfterWhatNode.parentNode.appendChild(elementToInsert);
    	    }
    	}
    },
    
    
    /**
     *	changes the class of the field based on whether it is valid or not
     */
    addFieldClass: function(){
        this.removeFieldClass();
        if(!this.validationFailed){
            if(this.displayMessageWhenEmpty || this.element.value != ''){
                if(this.element.className.indexOf(this.validFieldClass) == -1) this.element.className += ' ' + this.validFieldClass;
            }
        }else{
            if(this.element.className.indexOf(this.invalidFieldClass) == -1) this.element.className += ' ' + this.invalidFieldClass;
        }
    },
    
    /**
     *	removes the message element if it exists, so that the new message will replace it
     */
    removeMessage: function(){
    	var nextEl;
    	var el = this.insertAfterWhatNode;
    	while(el.nextSibling){
    	    if(el.nextSibling.nodeType === 1){
    		  	nextEl = el.nextSibling;
    		  	break;
    		}
    		el = el.nextSibling;
    	}
      	if(nextEl && nextEl.className.indexOf(this.messageClass) != -1) this.insertAfterWhatNode.parentNode.removeChild(nextEl);
    },
    
    /**
     *	removes the class that has been applied to the field to indicte if valid or not
     */
    removeFieldClass: function(){
      if(this.element.className.indexOf(this.invalidFieldClass) != -1) this.element.className = this.element.className.split(this.invalidFieldClass).join('');
      if(this.element.className.indexOf(this.validFieldClass) != -1) this.element.className = this.element.className.split(this.validFieldClass).join(' ');
    },
        
    /**
     *	removes the message and the field class
     */
    removeMessageAndFieldClass: function(){
      this.removeMessage();
      this.removeFieldClass();
    }

} // end of LiveValidation class

/*************************************** LiveValidationForm class ****************************************/
/**
 * This class is used internally by LiveValidation class to associate a LiveValidation field with a form it is icontained in one
 * 
 * It will therefore not really ever be needed to be used directly by the developer, unless they want to associate a LiveValidation 
 * field with a form that it is not a child of
 */

/**
   *	handles validation of LiveValidation fields belonging to this form on its submittal
   *	
   *	@var element {HTMLFormElement} - a dom element reference to the form to turn into a LiveValidationForm
   */
var LiveValidationForm = function(element){
  this.initialize(element);
}

/**
 * namespace to hold instances
 */
LiveValidationForm.instances = {};

/**
   *	gets the instance of the LiveValidationForm if it has already been made or creates it if it doesnt exist
   *	
   *	@var element {HTMLFormElement} - a dom element reference to a form
   */
LiveValidationForm.getInstance = function(element){
  var rand = Math.random() * Math.random();
  if(!element.id) element.id = 'formId_' + rand.toString().replace(/\./, '') + new Date().valueOf();
  if(!LiveValidationForm.instances[element.id]) LiveValidationForm.instances[element.id] = new LiveValidationForm(element);
  return LiveValidationForm.instances[element.id];
}

LiveValidationForm.prototype = {
  
  /**
   *	constructor for LiveValidationForm - handles validation of LiveValidation fields belonging to this form on its submittal
   *	
   *	@var element {HTMLFormElement} - a dom element reference to the form to turn into a LiveValidationForm
   */
  initialize: function(element){
  	this.name = element.id;
    this.element = element;
    this.fields = [];
    // preserve the old onsubmit event
	this.oldOnSubmit = this.element.onsubmit || function(){};
    var self = this;
    this.element.onsubmit = function(e){
      return (LiveValidation.massValidate(self.fields)) ? self.oldOnSubmit.call(this, e || window.event) !== false : false;
    }
  },
  
  /**
   *	adds a LiveValidation field to the forms fields array
   *	
   *	@var element {LiveValidation} - a LiveValidation object
   */
  addField: function(newField){
    this.fields.push(newField);
  },
  
  /**
   *	removes a LiveValidation field from the forms fields array
   *	
   *	@var victim {LiveValidation} - a LiveValidation object
   */
  removeField: function(victim){
  	var victimless = [];
  	for( var i = 0, len = this.fields.length; i < len; i++){
		if(this.fields[i] !== victim) victimless.push(this.fields[i]);
	}
    this.fields = victimless;
  },
  
  /**
   *	destroy this instance and its events
   *
   * @var force {Boolean} - whether to force the detruction even if there are fields still associated
   */
  destroy: function(force){
  	// only destroy if has no fields and not being forced
  	if (this.fields.length != 0 && !force) return false;
	// remove events - set back to previous events
	this.element.onsubmit = this.oldOnSubmit;
	// remove from the instances namespace
	LiveValidationForm.instances[this.name] = null;
	return true;
  }
   
}// end of LiveValidationForm prototype

/*************************************** Validate class ****************************************/
/**
 * This class contains all the methods needed for doing the actual validation itself
 *
 * All methods are static so that they can be used outside the context of a form field
 * as they could be useful for validating stuff anywhere you want really
 *
 * All of them will return true if the validation is successful, but will raise a ValidationError if
 * they fail, so that this can be caught and the message explaining the error can be accessed ( as just 
 * returning false would leave you a bit in the dark as to why it failed )
 *
 * Can use validation methods alone and wrap in a try..catch statement yourself if you want to access the failure
 * message and handle the error, or use the Validate::now method if you just want true or false
 */

var Validate = {

    /**
     *	validates that the field has been filled in
     *
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation 
     *													  (DEFAULT: "Can't be empty!")
     */
    Presence: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Mag niet leeg zijn!";
    	if(value === '' || value === null || value === undefined){ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
    /**
     *	validates that the value is numeric, does not fall within a given range of numbers
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							notANumberMessage {String} - the message to show when the validation fails when value is not a number
     *													  	  (DEFAULT: "Must be a number!")
     *							notAnIntegerMessage {String} - the message to show when the validation fails when value is not an integer
     *													  	  (DEFAULT: "Must be a number!")
     *							wrongNumberMessage {String} - the message to show when the validation fails when is param is used
     *													  	  (DEFAULT: "Must be {is}!")
     *							tooLowMessage {String} 		- the message to show when the validation fails when minimum param is used
     *													  	  (DEFAULT: "Must not be less than {minimum}!")
     *							tooHighMessage {String} 	- the message to show when the validation fails when maximum param is used
     *													  	  (DEFAULT: "Must not be more than {maximum}!")
     *							is {Int} 					- the length must be this long 
     *							minimum {Int} 				- the minimum length allowed
     *							maximum {Int} 				- the maximum length allowed
     *                         onlyInteger {Boolean} - if true will only allow integers to be valid
     *                                                             (DEFAULT: false)
     *
     *  NB. can be checked if it is within a range by specifying both a minimum and a maximum
     *  NB. will evaluate numbers represented in scientific form (ie 2e10) correctly as numbers				
     */
    Numericality: function(value, paramsObj){
        var suppliedValue = value;
        var value = Number(value);
    	var paramsObj = paramsObj || {};
        var minimum = ((paramsObj.minimum) || (paramsObj.minimum == 0)) ? paramsObj.minimum : null;;
        var maximum = ((paramsObj.maximum) || (paramsObj.maximum == 0)) ? paramsObj.maximum : null;
    	var is = ((paramsObj.is) || (paramsObj.is == 0)) ? paramsObj.is : null;
        var notANumberMessage = paramsObj.notANumberMessage || "Gebruik alleen getallen.";
        var notAnIntegerMessage = paramsObj.notAnIntegerMessage || "Moet een getal zijn!";
    	var wrongNumberMessage = paramsObj.wrongNumberMessage || "Moet " + is + " zijn!";
    	var tooLowMessage = paramsObj.tooLowMessage || "Mag niet minder dan " + minimum + " zijn!";
    	var tooHighMessage = paramsObj.tooHighMessage || "Mag niet meer dan " + maximum + " zijn!";
        if (!isFinite(value)) Validate.fail(notANumberMessage);
        if (paramsObj.onlyInteger && (/\.0+$|\.$/.test(String(suppliedValue))  || value != parseInt(value)) ) Validate.fail(notAnIntegerMessage);
    	switch(true){
    	  	case (is !== null):
    	  		if( value != Number(is) ) Validate.fail(wrongNumberMessage);
    			break;
    	  	case (minimum !== null && maximum !== null):
    	  		Validate.Numericality(value, {tooLowMessage: tooLowMessage, minimum: minimum});
    	  		Validate.Numericality(value, {tooHighMessage: tooHighMessage, maximum: maximum});
    	  		break;
    	  	case (minimum !== null):
    	  		if( value < Number(minimum) ) Validate.fail(tooLowMessage);
    			break;
    	  	case (maximum !== null):
    	  		if( value > Number(maximum) ) Validate.fail(tooHighMessage);
    			break;
    	}
    	return true;
    },
    
    /**
     *	validates against a RegExp pattern
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Not valid!")
     *							pattern {RegExp} 		- the regular expression pattern
     *													  (DEFAULT: /./)
     *             negate {Boolean} - if set to true, will validate true if the pattern is not matched
   *                           (DEFAULT: false)
     *
     *  NB. will return true for an empty string, to allow for non-required, empty fields to validate.
     *		If you do not want this to be the case then you must either add a LiveValidation.PRESENCE validation
     *		or build it into the regular expression pattern
     */
    Format: function(value, paramsObj){
      var value = String(value);
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Niet toegestaan!";
      var pattern = paramsObj.pattern || /./;
      var negate = paramsObj.negate || false;
      if(!negate && !pattern.test(value)) Validate.fail(message); // normal
      if(negate && pattern.test(value)) Validate.fail(message); // negated
    	return true;
    },
    
    /**
     *	validates that the field contains a valid email address
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Must be a number!" or "Must be an integer!")
     */
    Email: function(value, paramsObj){
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Ongeldig email adres!";
    	Validate.Format(value, { failureMessage: message, pattern: /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i } );
    	return true;
    },
    
    /**
     *	validates the length of the value
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							wrongLengthMessage {String} - the message to show when the fails when is param is used
     *													  	  (DEFAULT: "Must be {is} characters long!")
     *							tooShortMessage {String} 	- the message to show when the fails when minimum param is used
     *													  	  (DEFAULT: "Must not be less than {minimum} characters long!")
     *							tooLongMessage {String} 	- the message to show when the fails when maximum param is used
     *													  	  (DEFAULT: "Must not be more than {maximum} characters long!")
     *							is {Int} 					- the length must be this long 
     *							minimum {Int} 				- the minimum length allowed
     *							maximum {Int} 				- the maximum length allowed
     *
     *  NB. can be checked if it is within a range by specifying both a minimum and a maximum				
     */
    Length: function(value, paramsObj){
    	var value = String(value);
    	var paramsObj = paramsObj || {};
        var minimum = ((paramsObj.minimum) || (paramsObj.minimum == 0)) ? paramsObj.minimum : null;
    	var maximum = ((paramsObj.maximum) || (paramsObj.maximum == 0)) ? paramsObj.maximum : null;
    	var is = ((paramsObj.is) || (paramsObj.is == 0)) ? paramsObj.is : null;
        var wrongLengthMessage = paramsObj.wrongLengthMessage || "Moet uit " + is + " karakters bestaan!";
    	var tooShortMessage = paramsObj.tooShortMessage || "Mag niet uit minder dan " + minimum + " karakters bestaan!";
    	var tooLongMessage = paramsObj.tooLongMessage || "Mag niet uit meer dan " + maximum + " karakters bestaan!";
    	switch(true){
    	  	case (is !== null):
    	  		if( value.length != Number(is) ) Validate.fail(wrongLengthMessage);
    			break;
    	  	case (minimum !== null && maximum !== null):
    	  		Validate.Length(value, {tooShortMessage: tooShortMessage, minimum: minimum});
    	  		Validate.Length(value, {tooLongMessage: tooLongMessage, maximum: maximum});
    	  		break;
    	  	case (minimum !== null):
    	  		if( value.length < Number(minimum) ) Validate.fail(tooShortMessage);
    			break;
    	  	case (maximum !== null):
    	  		if( value.length > Number(maximum) ) Validate.fail(tooLongMessage);
    			break;
    		default:
    			throw new Error("Validate::Length - Length(s) to validate against must be provided!");
    	}
    	return true;
    },
    
    /**
     *	validates that the value falls within a given set of values
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Must be included in the list!")
     *							within {Array} 			- an array of values that the value should fall in 
     *													  (DEFAULT: [])	
     *							allowNull {Bool} 		- if true, and a null value is passed in, validates as true
     *													  (DEFAULT: false)
     *             partialMatch {Bool} 	- if true, will not only validate against the whole value to check but also if it is a substring of the value 
     *													  (DEFAULT: false)
     *             caseSensitive {Bool} - if false will compare strings case insensitively
     *                          (DEFAULT: true)
     *             negate {Bool} 		- if true, will validate that the value is not within the given set of values
     *													  (DEFAULT: false)			
     */
    Inclusion: function(value, paramsObj){
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Moet in de lijst voorkomen!";
      var caseSensitive = (paramsObj.caseSensitive === false) ? false : true;
    	if(paramsObj.allowNull && value == null) return true;
      if(!paramsObj.allowNull && value == null) Validate.fail(message);
    	var within = paramsObj.within || [];
      //if case insensitive, make all strings in the array lowercase, and the value too
      if(!caseSensitive){ 
        var lowerWithin = [];
        for(var j = 0, length = within.length; j < length; ++j){
        	var item = within[j];
          if(typeof item == 'string') item = item.toLowerCase();
          lowerWithin.push(item);
        }
        within = lowerWithin;
        if(typeof value == 'string') value = value.toLowerCase();
      }
    	var found = false;
    	for(var i = 0, length = within.length; i < length; ++i){
    	  if(within[i] == value) found = true;
        if(paramsObj.partialMatch){ 
          if(value.indexOf(within[i]) != -1) found = true;
        }
    	}
    	if( (!paramsObj.negate && !found) || (paramsObj.negate && found) ) Validate.fail(message);
    	return true;
    },
    
    /**
     *	validates that the value does not fall within a given set of values
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Must not be included in the list!")
     *							within {Array} 			- an array of values that the value should not fall in 
     *													  (DEFAULT: [])
     *							allowNull {Bool} 		- if true, and a null value is passed in, validates as true
     *													  (DEFAULT: false)
     *             partialMatch {Bool} 	- if true, will not only validate against the whole value to check but also if it is a substring of the value 
     *													  (DEFAULT: false)
     *             caseSensitive {Bool} - if false will compare strings case insensitively
     *                          (DEFAULT: true)			
     */
    Exclusion: function(value, paramsObj){
      var paramsObj = paramsObj || {};
    	paramsObj.failureMessage = paramsObj.failureMessage || "Mag niet in de lijst voorkomen!";
      paramsObj.negate = true;
    	Validate.Inclusion(value, paramsObj);
      return true;
    },
    
    /**
     *	validates that the value matches that in another field
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Does not match!")
     *							match {String} 			- id of the field that this one should match						
     */
    Confirmation: function(value, paramsObj){
      	if(!paramsObj.match) throw new Error("Validate::Confirmation - Error validating confirmation: Id of element to match must be provided!");
    	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "Komt niet overeen!";
    	var match = paramsObj.match.nodeName ? paramsObj.match : document.getElementById(paramsObj.match);
    	if(!match) throw new Error("Validate::Confirmation - There is no reference with name of, or element with id of '" + paramsObj.match + "'!");
    	if(value != match.value){ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
    /**
     *	validates that the value is true (for use primarily in detemining if a checkbox has been checked)
     *	
     *	@var value {mixed} - value to be checked if true or not (usually a boolean from the checked value of a checkbox)
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation 
     *													  (DEFAULT: "Must be accepted!")
     */
    Acceptance: function(value, paramsObj){
      	var paramsObj = paramsObj || {};
    	var message = paramsObj.failureMessage || "U moet akkoord gaan!";
    	if(!value){ 
    	  	Validate.fail(message);
    	}
    	return true;
    },
    
	 /**
     *	validates against a custom function that returns true or false (or throws a Validate.Error) when passed the value
     *	
     *	@var value {mixed} - value to be checked
     *	@var paramsObj {Object} - parameters for this particular validation, see below for details
     *
     *	paramsObj properties:
     *							failureMessage {String} - the message to show when the field fails validation
     *													  (DEFAULT: "Not valid!")
     *							against {Function} 			- a function that will take the value and object of arguments and return true or false 
     *													  (DEFAULT: function(){ return true; })
     *							args {Object} 		- an object of named arguments that will be passed to the custom function so are accessible through this object within it 
     *													  (DEFAULT: {})
     */
	Custom: function(value, paramsObj){
		var paramsObj = paramsObj || {};
		var against = paramsObj.against || function(){ return true; };
		var args = paramsObj.args || {};
		var message = paramsObj.failureMessage || "Ongeldig!";
	    if(!against(value, args)) Validate.fail(message);
	    return true;
	  },
	
    /**
     *	validates whatever it is you pass in, and handles the validation error for you so it gives a nice true or false reply
     *
     *	@var validationFunction {Function} - validation function to be used (ie Validation.validatePresence )
     *	@var value {mixed} - value to be checked if true or not (usually a boolean from the checked value of a checkbox)
     *	@var validationParamsObj {Object} - parameters for doing the validation, if wanted or necessary
     */
    now: function(validationFunction, value, validationParamsObj){
      	if(!validationFunction) throw new Error("Validate::now - Validation function must be provided!");
    	var isValid = true;
        try{    
    		validationFunction(value, validationParamsObj || {});
    	} catch(error) {
    		if(error instanceof Validate.Error){
    			isValid =  false;
    		}else{
    		 	throw error;
    		}
    	}finally{ 
            return isValid 
        }
    },
    
    /**
     * shortcut for failing throwing a validation error
     *
     *	@var errorMessage {String} - message to display
     */
    fail: function(errorMessage){
            throw new Validate.Error(errorMessage);
    },
    
    Error: function(errorMessage){
    	this.message = errorMessage;
    	this.name = 'ValidationError';
    }

}
