// REGEX LIBRARY RegExLib.com  //

// UK Vehicle Registration System. //
// ^([A-Z]{3}\s?(\d{3}|\d{2}|d{1})\s?[A-Z])|([A-Z]\s?(\d{3}|\d{2}|\d{1})\s?[A-Z]{3})|(([A-HK-PRSVWY][A-HJ-PR-Y])\s?([0][2-9]|[1-9][0-9])\s?[A-HJ-PR-Z]{3})$
// Matches  	ABC 123 A | A 123 ABC | AB 12 ABC   //
// Non-Matches  AB 123 C | A 123 AB | AB 12 AB   //
// http://regexlib.com/DisplayPatterns.aspx?cattabindex=3&categoryId=4 //

// Matches any string between 4 and 8 characters in length. Limits the length of a string //
//   ^.{4,8}$    //

// Matches any snon-digit, disallows numerals //
//  /^[\D]*$/;  //

// Good for stripping all tags from a string. <b> | </b> | <p><b>some text</b></p> // 
//   >(?:(?<t>[^<]*)) //

//  NO malicious code or specified characters are passed through user input.This will allow you to input any characters except those specified.  // 
//  The expression does not allow user input of &lt;&gt;`~!/@\#}$%:;)(_^{&amp;*=|'+. Input as many invalid characters you wish to deny.  // 
//   ^[^<>`~!/@\#}$%:;)(_^{&*=|'+]+$  // 


// Simple telephone number validation on keyup.  //
// script makes sure the user enters only numbers, brackets, hyphens, or spaces.  //
// A plus (+) may be entered as the first character.  //
// This script and many more are available free online at //
// http://javascript.internet.com //
// Created by: Rob Sheldrake  http://www.sites4biz.co.uk  //
//
function validatephone(xxxxx) {
	 var maintainplus = '';
 	var numval = xxxxx.value
 	if ( numval.charAt(0)=='+' ){ var maintainplus = '+';}
 	curphonevar = numval.replace(/[\\A-Za-z!"£$%^&*+_={};:'@#~,.¦\/<>?|`¬\]\[]/g,'');
 	xxxxx.value = maintainplus + curphonevar;
 	var maintainplus = '';
 	xxxxx.focus;
}


// www.leigeber.com/2008/04/dynamic-inline-javascript-form-validation //
// form validation function //
function validate(form) {
  var YourName = form.YourName.value;
  var YourEmailaddress = form.YourEmailaddress.value;
  var Telephone = form.Telephone.value;
  var YourInfo = form.YourInfo.value;
// NO GOOD as rejects French locale versions of letter E  var YourNameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/; used below to reject just multiple digits // 
  var YourNameRegex = /^[\D]*$/;
  var YourNameLengthRegex = /^.{2,50}$/;
  var YourEmailaddressRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
// var TelephoneRegex = /^\+?[0-9 ()-]+[0-9]+[0-9]+[0-9]+[0-9]$/;  //
  var TelephoneRegex = /^(.|\n){3,16}$/;
  var YourInfoRegex = new RegExp(/<\/?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/gim);
  
  if(YourName == "") {
    inlineMsg('YourName','<strong>Your Name is a MUST have!</strong>',6);
    return false;
  }
  if(!YourName.match(YourNameRegex)) {
    inlineMsg('YourName','Something is wrong here.',6);
    return false;
  }
    if(!YourName.match(YourNameLengthRegex)) {
      inlineMsg('YourName','We need to know your name.',6);
      return false;
  }
  if(YourEmailaddress == "") {
    inlineMsg('YourEmailaddress','<strong>Important</strong><br />Your Email address is a MUST have!',6);
    return false;
  }
  if(!YourEmailaddress.match(YourEmailaddressRegex)) {
    inlineMsg('YourEmailaddress','<strong>Are you sure?</strong><br />Need to get it right.',6);
    return false;
  }
// if(Telephone == "") {
//   inlineMsg('Telephone','<strong>Please provide your Telephone number.</strong>',4);
//    return false;
//  }
//   if(!Telephone.match(TelephoneRegex)) {
//     inlineMsg('Telephone','<strong>Number Wrong.</strong> Something is not quite right here.',3);
//     return false;
//   }
    if(YourInfo == "") {
      inlineMsg('YourInfo','What\'s your message?',60);
      return false;
    }
    if(YourInfo.match(YourInfoRegex)) {
      inlineMsg('YourInfo','<strong>Please amend.</strong><br />Something is not right here.');
      return false;
  }
  
  return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
//  var MSGOFFSET = 3; position of message offset to left or right of input - default is 3  //
//  See #msgcontent in css file for width paramenters of message  //
var MSGOFFSET = -120;
var MSGHIDE = 6;


// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the MESSAGE box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}
