var error_image_filename = "x.png";
var success_image_filename = "tick.png";
var blank_image_filename = "blank.png";
var imageDir = ROOT_URL+"/images";
if(!error_image_width)
        var error_image_width = 12;
if(!error_image_height)
        var error_image_height = 12;
        
var error_html_image = "<img src='"+imageDir+"/"+error_image_filename+"' height='"+error_image_height+"' width='"+error_image_width+"'>";
var success_html_image = "<img src='"+imageDir+"/"+success_image_filename+"' height='"+error_image_height+"' width='"+error_image_width+"'>";
var blank_html_image = "<img src='"+imageDir+"/"+blank_image_filename+"' height='"+error_image_height+"' width='"+error_image_width+"'>";
var green = "#A7FF9F";
var red = "#FF8885";

function autoLogout(l,s)
{
	x = "window.location='"+l+"'";

	setTimeout(x,	s);
}

function popUp(URL)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=820,height=720,left = 50,top = 50');");
}
function checkobject(inParent,theVal) {
	if(inParent){
		if (window.opener.document.getElementById(theVal) != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		if (document.getElementById(theVal) != null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}
function imgSwap(id, img)
{
	if(checkobject(false, id )){
		document.getElementById( id ).src = img;
	}
}
function highlight(id, myclass)
{
	if(checkobject(false, id )){
		document.getElementById( id ).className = myclass;
	}
}
function isValidPostcode(postcode)
{
	var postcoderegex = /(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|[0-9][A-HJKS-UW])(\ )?[0-9][ABD-HJLNP-UW-Z]{2})/i;
	return postcoderegex.test(postcode);
}
function trim(str, chars)
{
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars)
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function formatPostcode(postcode)
{
	postcode = postcode.toUpperCase();
	if(postcode.length > 8)
	{
			postcodesplit = postcode.split(' ');
			postcode = trim(postcodesplit[0]);
	}
	if (isValidPostcode(postcode))
		return postcode;
	else
		return false;
}

function pageOnLoad()
{
        if(window.loadGoogleMap)
                loadGoogleMap();
                
        var x = document.getElementsByTagName("form");
        
        if(!x)
                return;

        for (var i=0;i<x.length;i++)
        {
                y = x[i];
                for (var j=0;j<y.length;j++)
                {
                        if(y.elements[j].onload)
                        {
                                var functionCode = String(y.elements[j].onload);
                                functionCode = functionCode.replace(/function onload\(event\) {/,"");
                                functionCode = functionCode.replace(/}/,"");
                                eval(functionCode);
                        }
                }
        }
}

function pageOnUnLoad()
{       
        if(window.GUnload)
                GUnload();
}

function anchorOnClick(myinput)
{
        if(document.getElementById(myinput))
                document.getElementById(myinput).focus();
}

function formOnLoad(field,defaultValue,regex,min,max,error_image)
{
	validate_field(field,regex,min,max,error_image,defaultValue);
}
function formOnFocus(field,defaultValue,regex,min,max,error_image)
{
	clear_field(field,defaultValue);
	highlight_field(field,defaultValue,error_image);
}
function formOnBlur(field,defaultValue,regex,min,max,error_image,requiredColor)
{
	clear_field(field,defaultValue);
	unhighlight_field(field,defaultValue,error_image,requiredColor);
	validate_field(field,regex,min,max,error_image,defaultValue);
}
function validate_field(field,type,min,max,error_image,defaultValue)
{
	var alpha=/^((\ |\-|\')*([a-zA-Z])*(\ |\-|\')*)*$/;
	var email=/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z]{2,3})$/;
	var digit=/^[0-9]*$/;
	var weakdigit=/^\+?(([\(\-\ ])?([0-9])+([\)\-\ ])?){1,6}$/;
	var strictalphanumeric=/^(([a-zA-Z]|[0-9])*)*$/;
	var weakalphanumeric=/^((\ |\-|\'|\&|\(|\))*([a-zA-Z]|[0-9])*(\ |\-|\'|\&|\(|\))*)*$/;
	var urlregex = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&amp;?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	var required = "#D1F1FF";
	var url = window.location.href;

	var isValid = false;
	value = trim(document.getElementById(field).value);

	if(type == "number" || type == "digit" && value != "" && value != defaultValue)
	{
		if(value.search(digit)==-1)
			isValid = false;
		else
		{
                        max = parseInt(max);
                        min = parseInt(min);
                        value = parseInt(value);
                        
			while( value[0] == 0)
			        value = value.substring(1,value.length);
		             
                        if(min && (value < min))
                                aisValid = false;
                        else if (max && value > max)        
                                isValid = false;
                        else                                 
                                isValid = true;
                                                        
		}
	}
	else if(type == "alpha" && value != "")
	{
		if(value.search(alpha)==-1)
			isValid = false;
		else
			isValid = true;
	}
	else if(type == "email" && value != "")
	{
		if(value.search(email)==-1)
			isValid = false;
		else
			isValid = true;
	}
	else if(type == "weakdigit" && value != "")
	{
		if(value.search(weakdigit)==-1)
			isValid = false;
		else
			isValid = true;
	}
	else if(type == "strictalphanumeric" && value != "")
	{
		if(value.search(strictalphanumeric)==-1)
			isValid = false;
		else
			isValid = true;
	}
	else if(type == "url" || type == "website" || type == "webaddress" && value != "")
	{
		if(value.search(urlregex)==-1)
			isValid = false;
		else
			isValid = true;
	}
	else if(type == "weakalphanumeric" && value != "")
	{
		if(value.search(weakalphanumeric)==-1)
			isValid = false;
		else
			isValid = true;
	}
	else if(type == "postcode" && value != "")
	{
		newPostcode = formatPostcode(value);
		if(!newPostcode)
			isValid = false;
		else
		{
			isValid = true;
			document.getElementById(field).value=newPostcode;
		}
	}
	else if(type == "any" && value != "")
			isValid = true;

 	if(error_image)
 		current_html_image = document.getElementById(error_image).innerHTML;
 	else
 		current_html_image = false;

	if(!isValid && value != "" && value != defaultValue)
	{
		document.getElementById(field).style.background = red;
		document.getElementById(field).style.color = "black";
		if(error_image)
			document.getElementById(error_image).innerHTML = error_html_image;

	}
	else if(isValid && value != "" && value != defaultValue && (!current_html_image || current_html_image.search(error_image_filename) == -1))
	{
		document.getElementById(field).style.background = green;
		document.getElementById(field).style.color = "black";
		if(error_image)
			document.getElementById(error_image).innerHTML = success_html_image;
	}
	else if(isValid && value != "" && value != defaultValue)
	{
		document.getElementById(field).style.background = green;
		document.getElementById(field).style.color = "black";
	}
	if(value == "" && error_image)
		document.getElementById(error_image).innerHTML = blank_html_image;
}

function highlight_field(field,defaultValue,error_image)
{
	document.getElementById(field).style.border = "1px solid blue";

	document.getElementById(field).style.background = "white";
	document.getElementById(field).style.color = "black";

	if(error_image)
		document.getElementById(error_image).innerHTML = blank_html_image;
}

function unhighlight_field(field,defaultValue,error_image,requiredColor)
{
	if(!requiredColor)
		document.getElementById(field).style.background = "white";
	else
		document.getElementById(field).style.background = requiredColor;

	document.getElementById(field).style.border = "1px solid black";

	if(error_image)
		document.getElementById(error_image).innerHTML = blank_html_image;

	if(document.getElementById(field).value != defaultValue)
		document.getElementById(field).style.color = "black";
}

function void_validation_style(field)
{
	document.getElementById(field).style.background = "white";

}

function clear_field(field,defaulttext)
{
	if(document.getElementById(field).value == defaulttext)
	{
		document.getElementById(field).value = "";
		document.getElementById(field).style.color = 'black';
	}
	else if(document.getElementById(field).value == "")
	{
		document.getElementById(field).value = defaulttext;
		document.getElementById(field).style.color = '#CCCCCC';
	}
	else if(document.getElementById(field).value != defaulttext)
		document.getElementById(field).style.color = 'black';

}
function disable_button( buttonid )
{
	document.getElementById(buttonid).disabled = true;
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

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 var 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 null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}