function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return a === null;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
}

function getObject(name) {
	var ns4 = (document.layers) ? true : false;
	var w3c = (document.getElementById) ? true : false;
	var ie4 = (document.all) ? true : false;
	if (ns4) return eval('document.' + name);
	if (w3c) return document.getElementById(name);
	if (ie4) return eval('document.all.' + name);
	return false;
}

function validateText(obj,list,valid){
	var Invalid = false;
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
		for(i=0;i<Len;i++){
			LetterPos = list.indexOf(Text.charAt(i),0);
			if ((valid == false) && (LetterPos >= 0))
				Invalid = true;
			if ((valid == true) && (LetterPos == -1))
				Invalid = true;
		}
	}
	return Invalid;
}

function checkEmpty(obj){
	if(isNumber(obj.length)){
		return (obj.length > 0);
	}else{
		return (obj.value.length > 0);
	}
}

function hidetoggle (e) {
    document.getElementById(e).style.display = (document.getElementById(e).style.display == 'none') ? 'block' : 'none';
    return false;
}

function hide (e) {
    document.getElementById(e).style.display = 'none';
    return false;
}

function show (e) {
    document.getElementById(e).style.display = 'block';
    return false;
}

function twodp(v){
	return Math.round(v*100)/100;
}

function sendToclipboard(s){
if( window.clipboardData && clipboardData.setData ){
clipboardData.setData("Text", s);
return true;
}else{
//alert("Internet Explorer required");
return false;
}
} 

function copyFields(str){
	// Created by Blake Kus
	// http://www.kustomwebdesigns.com
	var elementArray=str.split(",");
	var element_num=0;
	var element, a, b, tmp, join_num, strtocopy;
	while (element_num < elementArray.length){
		element = elementArray[element_num].split(":");
		if(element[0].indexOf("+") > -1){	
			tmp = element[0];
			if(tmp.indexOf("+") == 0){
				var offset=1;
				tmp = tmp.substr(1,tmp.length);
			}else{
				var offset=0;
			}
			tmp = tmp.split("+");
			join_num=0;
			strtocopy = "";
			while (join_num < tmp.length){
				if(join_num % 2 == offset){
					a = getObject(tmp[join_num]);
					if(!isNull(a)){
						strtocopy = strtocopy+a.value;
					}else{
						strtocopy = strtocopy+"!undefined";
					}
				}else{
					strtocopy = strtocopy+tmp[join_num];
				}
				join_num+=1;
			}
			b = getObject(element[1]);
			if(element.length > 1){
				if(!isNull(b)){
					b.value = strtocopy;
				}
			}
		}else{
			a = getObject(element[0]);
			b = getObject(element[1]);
			if(element.length > 1){
				if(!isNull(a) && !isNull(b)){
					b.value = a.value;
				}
			}
		}
		element_num+=1;
	}
	return false;
}

function validateForm(checkString){
	// Created by Blake Kus
	// http://www.kustomwebdesigns.com
	// Validate form objects from string
	// field:longname:allowedcharacters:check
	// eg: name:long name:alpha:empty - Check textfield "name", alpha characters (a-z) only, check if empty, display "long name" to user
	// allowedcharacters: text, azAZ, azAZ09, 09, phone, email
	// check: empty
	var elementArray=checkString.split(",");
	var element_num=0;
	var element, field, name, longname, allowedcharacters, check, text, valid, err, f, c, minreq;
	err = "";
	valid = true;
	while (element_num < elementArray.length){
		element = elementArray[element_num].split(":");
		field = getObject(element[0]);
		name = element[0];
		longname = element[1];
		allowedcharacters = element[2];
		check = element[3];
		if(element.length > 1){
			if(isNull(field)){
				switch(name){
					case "minimumrequired":
						minreq = check.split(";");
						present = 0;
						for(i=0; i < minreq.length; i++) {
							if(checkEmpty(getObject(minreq[i]))){
								present++;
							}
						}
						if(present < allowedcharacters){
							err += "You need to fill in atleast "+allowedcharacters+" "+longname+"!\n";
							valid = false;
						}
					break;
					case "equaleachother":
						if(!checkConfirmation(getObject(allowedcharacters).value,getObject(check).value)){
							err += longname+" do not match!\n";
							valid = false;
						}
					break;
					default:
						err += longname+" ("+field+") could not be found! Please contact us.\n";
						valid = false;
					break;
				}
			}else{
				switch(check){
					case "empty":
						if(field.type == "radio"){
							f = field.form;
							c = false;
							for(i=0; i < f.length; i++) {
								if((f[i].type == "radio") && (f[i].name == field.name)) {
									if(f[i].checked){
										c = f[i].value;
									}
								}
							}
							if(c == false){
								valid = false;
								err += longname+" needs to be filled in!\n";
							}
						}else if(!checkEmpty(field)){
							valid = false;
							err += longname+" needs to be filled in!\n";
						}
					break;
				}
				switch(allowedcharacters){
					case "text":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890`~!@#$%^&*()_+=-/\,.<>;':[]{}| ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "azAZ":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "azAZ09":
						if(validateText(field,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,- ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "09":
						if(validateText(field,"0123456789.,- ",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "phone":
						if(validateText(field,"+() 1234567890-.",true)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
					case "email":
						if(!checkEmail(field)){
							valid = false;
							err += longname+" is invalid!\n";
						}
					break;
				}
			}
		}
		element_num+=1;
	}
	if(err.length > 0){
		alert("There was an error processing your form:\n\n"+err);
	}
	return valid;
}

function AuthenticateForm(f){
	// Created by Blake Kus
	// http://www.kustomwebdesigns.com
	var validate = null;
	for(i=0; i < f.length; i++) {
		if(f[i].name == "validate[]"){
			validate = f[i].value;
		}
	}
	if(validate != null){
		return validateForm(validate);
	}else{
		return true;
	}
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title){
	a.disabled = false;
	createCookie("style", title, 365);
      }
    }
  }
}

function loadActiveStyleSheet(title) {
  createCookie("style", title, 365);
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  return ('A');
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return false;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

//window.onunload = function(e) {
  //var title = getActiveStyleSheet();
  //createCookie("style", title, 365);
//}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
if (title == 'null') {
  title = getPreferredStyleSheet();
}

setActiveStyleSheet(title);

function sort(what){
	document.FormSearch.sort.value = what;
	document.FormSearch.dosort.value = 1;
	document.FormSearch.submit();
}

function checkAll(object){
	var form = object.form;
	for (i = 0; i < form.length; i++){
		if((form[i].name.substr(0,object.name.length) == object.name) && (form[i].type == "checkbox")) {
			form[i].checked = object.checked;
		}
	}
}

function getpage ( selectedpage ){
	document.FormSearch.page.value = selectedpage;
	document.FormSearch.submit();
}

function changeAllSelect(object){
	var form = object.form;
	for (i = 0; i < form.length; i++){
		if((form[i].name.substr(0,object.name.length) == object.name) && (form[i].type == "select-one")) {
			if(form[i].name != object.name){
				form[i].selectedIndex = object.selectedIndex;
			}
		}
	}
}

function validateQuiz(f){
	var i, n, c, a;
	n = "";
	for(i=0; i < f.length; i++) {
		if(f[i].type == "radio") {
			if(f[i].name != n) {
				if(n != "" && c == false){
					alert("ALL questions must be answered!");
					f[a].focus();
					return false;
				}
				n = f[i].name;
				c = f[i].checked;
				a = i;
			} else {
				if(f[i].checked)
					c = true;
			}
		} else {
			if(n != "" && c == false) {
				alert("ALL questions must be answered!");
				f[a].focus();
				return false;
			}
		}
	}
	return true;
}

function selectGo(where,file){
	if(!checkEmpty(file)){
		return false;
	}else{
		location.href=where+file;
		return true;
	}
}

function validateEmail(obj){
	var address = obj.value;
	if(address.length > 0){
		if((address == "") || (address.indexOf('@') == -1) || (address.indexOf('.') == -1)){
			alert("Invalid email address.");
			obj.focus();
			obj.select();
		}else if(validateText(obj,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-",true)){
			alert("Invalid email address.");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function validateAlpha(obj){
	var Invalid = false;
	var InvalidLetters ="`~!@#$%^&*()_+=-/\,.<>;':[]{}|1234567890";
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
	for(i=0;i<Len;i++){
		LetterPos = InvalidLetters.indexOf(Text.charAt(i),0);
		if (LetterPos >= 0)
			Invalid = true;
		}
		if (Invalid){
			alert("The text entered includes an illegal character");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function validateNumeric(obj){
	var Invalid = false;
	var ValidLetters ="1234567890";
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
		for(i=0;i<Len;i++){
			LetterPos = ValidLetters.indexOf(Text.charAt(i),0);
			if (LetterPos == -1)
				Invalid = true;
		}
		if (Invalid){
			alert("You can only enter numeric values only.");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function validatePhone(obj){
	var Invalid = false;
	var ValidLetters = "+() 1234567890-.";
	var Text = obj.value;
	var Len = Text.length;
	var i;
	var LetterPos;
	if(Len > 0){
		for(i=0;i<Len;i++){
			LetterPos = ValidLetters.indexOf(Text.charAt(i),0);
			if (LetterPos == -1)
				Invalid = true;
		}
		if (Invalid){
			alert("The text entered includes an illegal character");
			obj.focus();
			obj.select();
		}
	}
	return;
}

function checkConfirmation(v1, v2) {
    if (v1 == v2)
        return true;
    return false;
}

function checkEmail(obj){
	var address = obj.value;
	var valid = true;
	if(address.length > 0){
		if((address == "") || (address.indexOf('@') == -1) || (address.indexOf('.') == -1)){
			valid = false;
		}else if(validateText(obj,"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-",true)){
			valid = false;
		}
	}
	return valid;
}	

var req = null;

function loadXMLDoc(url,onready) {
	// Internet Explorer
	try { req = new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
		try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
		catch(oc) { req = null; }
	}

	// Mozailla/Safari
	if (req == null && typeof XMLHttpRequest != "undefined") {
		req = new XMLHttpRequest();
	}
	if (req != null) {
		req.onreadystatechange = eval(onready);
		req.open("GET", url, true);
		req.send(null);
	}
}

function xmlpostcode(evt) {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var data = req.responseText;
			if (data.substring(0,3) == "<ul") {
				getObject("postcodeResult").innerHTML = data;
				getObject("postcodeResult").style.display = "block";
			} else {
				getObject("postcodeResult").style.display = "none";
			}
		}
	}
}

function updateSelect(obj,v){
	getObject(obj).value=v;
	getObject(obj+"Result").style.display = "none";
	show("storesearchsubmit");
}
