// form validation function //
function input_sanitize(text) {
				text=text.replace("|","");
				text=text.replace("&","");
				text=text.replace(";","");
				text=text.replace("$","");
				text=text.replace("%","");
				text=text.replace("'","");
				text=text.replace('"',"");
				text=text.replace("\\","");
				text=text.replace("<","");
				text=text.replace(">","");
				text=text.replace("(","");
				text=text.replace(")","");
				text=text.replace("+","");
				text=text.replace("?","");
				return text
			}
function validateParcel(form) {
  var parcel=input_sanitize(form.parcelbox.value);
	if (parcel.length<7) {		
    	inlineMsg('parcel','Your reference must be at least 7 characters long.',2);
    return false;
  	}
	/*document.getElementById('trackImage').innerHTML = '&nbsp;';*/
	document.getElementById('trackingImage').style.visibility = 'hidden';
  	return true;
}

function validateParcelTwo(form) {
  var parcel=input_sanitize(form.parcelbox.value);
	if (parcel.length<7) {		
    	inlineMsg('parcel2','Your reference must be at least 7 characters long.',2);
    return false;
  	}
  	return true;
}

function validateParcelThree(form) {
  var parcel=input_sanitize(form.parcelbox.value);
	if (parcel.length<7) {		
    	inlineMsg('parcel3','Your reference must be at least 7 characters long.',2);
    return false;
  	}
  	return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 25;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// 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 * 2500));
}

// 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) {
  if(target.id == "parcel2") {
		var left = -30;
	}
	else if(target.id == "emailAddress") {
		var left = -150;
	}
	else if(target.id == "drop") {
		var left = -350;
	}
	else if(target.id == "searchBox3") {
		var left = -350;
	}
	else if(target.id == "returnItem") {
		var left = -200;
	}
	else if(target.id == "error") {
		var left = -30;
	}
	else {
		var left = -5;
	}
  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) {
	if(target.id == "searchBox") {
		var top = 10;
	}
	else if(target.id == "parcel2") {
		var top = -5;
	}
	else if(target.id == "drop") {
		var top = 25;
	}
	else if(target.id == "returnItem") {
		var top = -50;
	}
	else if(target.id == "parcel3") {
		var top = 0;
	}
	else if(target.id == "emailAddress") {
		var top = -5;
	}
	else if(target.id == "error") {
		var top = 40;
	}
	else {
		var top = 15;
	}
  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 = "../img/msg_arrow.gif"; 
}




var	ie=document.all;
	var	dom=document.getElementById;

	var	ns4=document.layers;
	
if(!self.getObj) {

            function getObj(objName) {

                        var obj=(dom)?document.getElementById(objName) : ie?eval("document.all."+objName) :eval("document."+objName);

                        return obj;

            }

            function getObjStyle(objName) {

                        var obj=(dom)?document.getElementById(objName).style : ie?eval("document.all."+objName) :eval("document."+objName);

                        return obj;

            }

}

function validatePostcode(field, wrapper, callback, errorDiv)
	{
	
	//getObjStyle(field).border = "none";
	
	var toCheck = getObj(field).value;

	var pcode_length = toCheck.length;		
	var last_chars = (pcode_length - 3);
	var space_char = (pcode_length - 4);

	//check if a space exists in the postcode otherwise put it there
	if(toCheck.substr(space_char, 1) == " "){
		toCheck = toCheck;
	} else {
		var postcode_start = toCheck.substr(toCheck, last_chars);
		var postcode_end = toCheck.substr(last_chars);
		getObj(field).value = postcode_start + " " + postcode_end;
		toCheck = postcode_start + " " + postcode_end;
	}

	var errorsRet = checkPostCode(toCheck);
	if (errorsRet == true){
		inlineMsg('searchBox','Your Post Code is incomplete or incorrect.<br/>Please enter a full UK Post Code.',2);
		// getObj(field).scrollIntoView(true);
	} else {
		loadMap(field, wrapper, callback);
	}
	return false;
}

function validatePostcodeNoMap(field, wrapper, callback, errorDiv)
	{
	var toCheck = getObj(field).value;
	var pcode_length = toCheck.length;		
	var last_chars = (pcode_length - 3);
	var space_char = (pcode_length - 4);

	//check if a space exists in the postcode otherwise put it there
	if(toCheck.substr(space_char, 1) == " "){
		toCheck = toCheck;
	} else {
		var postcode_start = toCheck.substr(toCheck, last_chars);
		var postcode_end = toCheck.substr(last_chars);
		getObj(field).value = postcode_start + " " + postcode_end;
		toCheck = postcode_start + " " + postcode_end;
	}

	var errorsRet = checkPostCode(toCheck);
	if (errorsRet == true){
		inlineMsg('searchBox','Your Post Code is incomplete or incorrect.<br/>Please enter a full UK Post Code.',2);
		// getObj(field).scrollIntoView(true);
	} else {
		return true;
	}
	return false;
}


function validatePostcode2(field, wrapper, callback, errorDiv)
	{
	
	//getObjStyle(field).border = "none";
	
	var toCheck = getObj(field).value;

	var pcode_length = toCheck.length;		
	var last_chars = (pcode_length - 3);
	var space_char = (pcode_length - 4);

	//check if a space exists in the postcode otherwise put it there
	if(toCheck.substr(space_char, 1) == " "){
		toCheck = toCheck;
	} else {
		var postcode_start = toCheck.substr(toCheck, last_chars);
		var postcode_end = toCheck.substr(last_chars);
		getObj(field).value = postcode_start + " " + postcode_end;
		toCheck = postcode_start + " " + postcode_end;
	}

	var errorsRet = checkPostCode(toCheck);
	if (errorsRet == true){
		inlineMsg('drop','Your Post Code is incomplete or incorrect.<br/>Please enter a full UK Post Code.',2);
		// getObj(field).scrollIntoView(true);
	} else {
		loadMap(field, wrapper, callback);
	}
	return false;
}
function validatePostcode3(field, wrapper, callback, errorDiv)
	{
	
	//getObjStyle(field).border = "none";
	
	var toCheck = getObj(field).value;

	var pcode_length = toCheck.length;		
	var last_chars = (pcode_length - 3);
	var space_char = (pcode_length - 4);

	//check if a space exists in the postcode otherwise put it there
	if(toCheck.substr(space_char, 1) == " "){
		toCheck = toCheck;
	} else {
		var postcode_start = toCheck.substr(toCheck, last_chars);
		var postcode_end = toCheck.substr(last_chars);
		getObj(field).value = postcode_start + " " + postcode_end;
		toCheck = postcode_start + " " + postcode_end;
	}

	var errorsRet = checkPostCode(toCheck);
	if (errorsRet == true){
		inlineMsg('searchBox','Your Post Code is incomplete or incorrect.<br/>Please enter a full UK Post Code.',2);
	} else {
		loadMap(field, wrapper, callback);
	}
	return false;
}


function validatePostcode4(field, wrapper, callback, errorDiv)
	{
	
	//getObjStyle(field).border = "none";
	
	var toCheck = getObj(field).value;

	var pcode_length = toCheck.length;		
	var last_chars = (pcode_length - 3);
	var space_char = (pcode_length - 4);

	//check if a space exists in the postcode otherwise put it there
	if(toCheck.substr(space_char, 1) == " "){
		toCheck = toCheck;
	} else {
		var postcode_start = toCheck.substr(toCheck, last_chars);
		var postcode_end = toCheck.substr(last_chars);
		getObj(field).value = postcode_start + " " + postcode_end;
		toCheck = postcode_start + " " + postcode_end;
	}

	var errorsRet = checkPostCode(toCheck);
	if (errorsRet == true){
		inlineMsg('searchBox','Your Post Code is incomplete or incorrect.<br/>Please enter a full UK Post Code.',2);
		// getObj(field).scrollIntoView(true);
	} else {
		//loadMap(field, wrapper, callback);
	}
	return false;
}

function validatePostcode5(field, wrapper, callback, errorDiv)
	{
	
	//getObjStyle(field).border = "none";
	
	var toCheck = getObj(field).value;

	var pcode_length = toCheck.length;		
	var last_chars = (pcode_length - 3);
	var space_char = (pcode_length - 4);

	//check if a space exists in the postcode otherwise put it there
	if(toCheck.substr(space_char, 1) == " "){
		toCheck = toCheck;
	} else {
		var postcode_start = toCheck.substr(toCheck, last_chars);
		var postcode_end = toCheck.substr(last_chars);
		getObj(field).value = postcode_start + " " + postcode_end;
		toCheck = postcode_start + " " + postcode_end;
	}

	var errorsRet = checkPostCode(toCheck);
	if (errorsRet == true){
		inlineMsg('error','Your Post Code is incomplete or incorrect.<br/>Please enter a full UK Post Code.',2);
		// getObj(field).scrollIntoView(true);
	} else {
		loadMap(field, wrapper, callback);
	}
	return false;
}

function checkPostCode(toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  

  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
	var errors = false;
  if (valid) {
		errors = false;
	  } else {
		errors = true;
	}
	return errors;
}


function showHide() {
	document.getElementById('trackingImage').style.visibility = 'visible';
		}

function changeDisplay(id, display) {
	        obj = document.getElementsByTagName("div");
	        obj[id].style.display = display;
	    }


/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   inlineMsg('error','Invalid E-mail Address.',2);
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   inlineMsg('error','Invalid E-mail Address.',2);
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   inlineMsg('error','Invalid E-mail Address.',2);
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    inlineMsg('error','Invalid E-mail Address.',2);
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    inlineMsg('error','Invalid E-mail Address.',2);
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    inlineMsg('error','Invalid E-mail Address.',2);
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    inlineMsg('error','Invalid E-mail Address.',2);
		    return false;
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.emailSubscribe.emailAddress
	
	if ((emailID.value==null)||(emailID.value=="")){
		inlineMsg('emailAddress','Invalid E-mail Address.',2);
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
