var site_url = '';

function id(id){
	return document.getElementById(id);
}

function trim(str) {
	return str.replace(/^\s*|\s*$/g, "");
}

function getOffset(o)
{
	for(var r = {l: o.offsetLeft, t: o.offsetTop, r: o.offsetWidth, b: o.offsetHeight};
	o = o.offsetParent; r.l += o.offsetLeft, r.t += o.offsetTop);
	return r.r += r.l, r.b += r.t, r;
}

function is_overlapping(a, b)
{
	return (a.l == b.l || (a.l > b.l ? a.l <= b.r : b.l <= a.r))
	&& (a.t == b.t || (a.t > b.t ? a.t <= b.b : b.t <= a.b));
}
/*
* Written by Jonathan Snook, http://www.snook.ca/jonathan
*/
function getElementsByClassName(node, classname, tag)
{
    var a = [];
    var t = (tag == null) ? '*' : tag;
    var re = new RegExp("(^|\\s)" + classname + "(\\s|$)");
    var els = node.getElementsByTagName(t);
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}


/*
* Cross-browser event handling, by Scott Andrew
*/
function addEvent(element, eventType, lamdaFunction, useCapture) {
	if (element.addEventListener) {
		element.addEventListener(eventType, lamdaFunction, useCapture);
		return true;
	} else if (element.attachEvent) {
		var r = element.attachEvent('on' + eventType, lamdaFunction);
		return r;
	} else {
		return false;
	}
}

/*
* Kills an event's propagation and default action
*/
function knackerEvent(eventObject) {
	if (eventObject && eventObject.stopPropagation) {
		eventObject.stopPropagation();
	}
	if (window.event && window.event.cancelBubble ) {
		window.event.cancelBubble = true;
	}

	if (eventObject && eventObject.preventDefault) {
		eventObject.preventDefault();
	}
	if (window.event) {
		window.event.returnValue = false;
	}
}

/*
* Safari doesn't support canceling events in the standard way, so we must
* hard-code a return of false for it to work.
*/
function cancelEventSafari() {
	return false;
}

/*
* Cross-browser style extraction, from the JavaScript & DHTML Cookbook
* <http://www.oreillynet.com/pub/a/javascript/excerpt/JSDHTMLCkbk_chap5/index5.html>
*/
function getElementStyle(elementID, CssStyleProperty) {
	var element = document.getElementById(elementID);
	if (element.currentStyle) {
		return element.currentStyle[toCamelCase(CssStyleProperty)];
	} else if (window.getComputedStyle) {
		var compStyle = window.getComputedStyle(element, '');
		return compStyle.getPropertyValue(CssStyleProperty);
	} else {
		return '';
	}
}

/*
* CamelCases CSS property names. Useful in conjunction with 'getElementStyle()'
* From <http://dhtmlkitchen.com/learn/js/setstyle/index4.jsp>
*/
function toCamelCase(CssProperty) {
	var stringArray = CssProperty.toLowerCase().split('-');
	if (stringArray.length == 1) {
		return stringArray[0];
	}
	var ret = (CssProperty.indexOf("-") == 0)
	? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
	: stringArray[0];
	for (var i = 1; i < stringArray.length; i++) {
		var s = stringArray[i];
		ret += s.charAt(0).toUpperCase() + s.substring(1);
	}
	return ret;
}

/*
* Disables all 'test' links, that point to the href '#', by Ross Shannon
*/
function disableTestLinks() {
	var pageLinks = document.getElementsByTagName('a');
	for (var i=0; i<pageLinks.length; i++) {
		if (pageLinks[i].href.match(/[^#]#$/)) {
		addEvent(pageLinks[i], 'click', knackerEvent, false);
	}
}
}

/*
* Cookie functions
*/
function createCookie(name, value, days) {
	var expires = '';
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days*24*60*60*1000));
		var expires = '; expires=' + date.toGMTString();
	}
	document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
	var cookieCrumbs = document.cookie.split(';');
	var nameToFind = name + '=';
	for (var i = 0; i < cookieCrumbs.length; i++) {
		var crumb = cookieCrumbs[i];
		while (crumb.charAt(0) == ' ') {
			crumb = crumb.substring(1, crumb.length); /* delete spaces */
		}
		if (crumb.indexOf(nameToFind) == 0) {
			return crumb.substring(nameToFind.length, crumb.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, '', -1);
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

var timerID = null;
var delay = 200;
 
function resetFunctionTimer(function_name)
{
	if (timerID != null)
	{
		clearTimeout(timerID)
	}
	timerID = setTimeout(function_name,delay);
}

function stopFunctionTimer(function_name, function_params)
{
	params = '';
	for (i = 0; i < function_params.length; i++){
		if (i > 0) {
			params += ",";
		}
		params += ("'" + function_params[i] + "'");
	}
	eval (function_name + "(" + params + ")");
}

function getNextSibling(elt)
{
	while (elt.nodeType !=1)
	{
	   elt = elt.nextSibling;
	   if (elt == null){
	   		break;
	   }
	}
	return elt;
}

var blkout = null;
var divpop = null;
var pop_head = null;
var pop_content = null;

function addPopup()
{
	document.write(
	'<div id="blackout"></div>' +
	'<div id="divpopup">' +
	'	<div id="divpop_head"><img src="images/head.png" /></div>' +
	'	<div id="divpop_content"><div id="popup_content"></div></div>' +
	'</div>'
	);
	
	blkout = id('blackout');
	divpop = id('divpopup');
	pop_head = id('divpop_head');
	pop_content = id('divpop_content');
}


function resizePopup(extra_height)
{
	var p_content = id('popup_content');
	pop_width = p_content.offsetWidth;	
	pop_height = p_content.offsetHeight + parseInt(extra_height);
	divpop.style.width = '0px';
	divpop.style.height = '0px';
	divpop.style.marginTop = '10px';
	divpop.style.marginLeft = '0px';
	showPopup();
	
	var millisec = 55;	
	if (BrowserDetect.browser == "Explorer"){
		millisec = 35;	
	}	
    
	var w_inc = Math.ceil(pop_width/millisec);
	var h_inc = Math.ceil(pop_height/millisec);	
	var timer = Math.ceil(millisec/pop_width);
	var this_width = 0;
	var this_height = 0;

	sizePopup(this_width,this_height,pop_width,millisec, w_inc, h_inc);
}

function sizePopup(width, height, pop_width, timer, w_inc, h_inc)
{
	if (width > pop_width){		
		return;
	}
	
	divpop.style.width = parseInt(width + 5) + 'px';
	divpop.style.height = (height + 5) + 'px';
	divpop.style.marginTop = parseInt(-width/2 - 10) + 'px';
	divpop.style.marginLeft = parseInt(-height/2) + 'px';
		
	width+= w_inc;
	height+= h_inc;
	
	setTimeout("sizePopup(" + width + ", " + height + ", " + pop_width + "," + timer + "," + w_inc + "," + h_inc + ")");
}

function initPopup()
{
	blkout.style.visibility = 'hidden';
	divpop.style.visibility = 'hidden';
	blkout.style.display = 'block';
	divpop.style.display = 'block';
}

function showPopup()
{
	blkout.style.visibility = 'visible';
	divpop.style.visibility = 'visible';
	blkout.style.display = 'block';
	divpop.style.display = 'block';
}

function hidePopup()
{
	// Hide popup
	blkout.style.visibility = 'hidden';
	divpop.style.visibility = 'hidden';
	blkout.style.display = 'none';
	divpop.style.display = 'none';
	if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7){
		var cont = id('main_2col');
		if (cont != null){
			cont.style.visibility = 'visible';
		}
	}
}

function showProcessingPopupMessage(message){
	pop_content.style.textAlign = 'center';
	pop_content.innerHTML = '<div id="popup_content" style="width:250px; height:30px;padding:20px;background-color:#fff;border:1px solid #ccc"><img src="' + site_url + 'images/ajax-loader.gif" border="0" align="absmiddle"/>&nbsp;' + message + '</div>';
	p_content = id('popup_content');
	pop_head.innerHTML = '';
	resizePopup(30);
}

function doValidateNotBlank(e)
{
	var field = window.event ? window.event.srcElement : e ? e.target : null;
	return validateNotBlank(field);
}

function validateNotBlank(field)
{
	var fld_error = id(field.id + '_error');	
	var fld_bg = id(field.id + '_bg');
	var base_class = (fld_bg.className.indexOf('error') > 0 ? fld_bg.className.substring(0, fld_bg.className.indexOf('error') - 1) : fld_bg.className);
	var is_valid = true;
	if(!field.value.length){
		fld_error.style.display = 'block';
		fld_bg.className = base_class + '_error';
		is_valid = false;
		
	} else {
		fld_error.style.display = 'none';
		fld_bg.className = base_class;
	}
	if (divpop != null && divpop.style.visibility == 'visible'){
		resizePopup(30);
	}
	return is_valid;
}

function doValidatePhoneNumber(e)
{
	var field = window.event ? window.event.srcElement : e ? e.target : null;
	return validatePhoneNumber(field);
}

function validatePhoneNumber(field)
{
	var fld_error = id(field.id + '_error');
	var spacefix = / /gi;
	var value = field.value.replace(spacefix,"");
	var fld_bg = id(field.id + '_bg');
	var is_valid = true;
	if(isNaN(value) || value.length < 8){
		fld_error.style.display = 'block';
		fld_bg.className = 'bg_field_error';
		is_valid =  false;
	} else	{
		fld_error.style.display = 'none';
		fld_bg.className = 'bg_field';
	}
	if (divpop != null && divpop.style.visibility == 'visible'){
		resizePopup(30);
	}
	return is_valid;
}

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: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			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.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

// IE6 Specific Functions

function resizePopupIE6(extra_height)
{
	var p_content = id('popup_content');
	pop_width = p_content.offsetWidth +10;
	pop_height = p_content.offsetHeight + parseInt(extra_height);
	hidePopup();
	divpop.style.width = pop_width + 'px';
	divpop.style.height = pop_height + 'px';
	divpop.style.marginTop = parseInt(-pop_height/2 - 30) + 'px';
	divpop.style.marginLeft = parseInt(-pop_width/2) + 'px';
	showPopupIE6();
}

function showPopupIE6()
{
	blkout.style.position = 'absolute';
	blkout.style.height = id('container').offsetHeight + 'px';
	blkout.style.visibility = 'visible';
	divpop.style.visibility = 'visible';
	divpop.style.position = 'absolute';
	blkout.style.display = 'block';
	divpop.style.display = 'block';
	
	var cont = id('main_2col');
	if (cont != null){
		cont.style.visibility = 'hidden';
	}
}

function showProcessingPopupMessageIE6(message){
	pop_content.style.textAlign = 'center';
	pop_content.innerHTML = '<div id="popup_content" style="width:300px;padding:20px"><img src="' + site_url + 'images/ajax-loader.gif" border="0" align="absmiddle"/>&nbsp;' + message + '</div>';
	p_content = id('popup_content');
	pop_head.innerHTML = 'Processing Request';
	resizePopupIE6(30);
}

function collectFormVars(form)
{
	var input_types = ['hidden','text','select-one','radio'];
	var params = Array();
	for (i = 0; i < form.elements.length; i++)
	{
		if (form.elements[i].type == 'radio'){
			if (form.elements[i].checked){
				params[form.elements[i].name] = form.elements[i].value;
			}				
		} else if (form.elements[i].type == 'checkbox'){
			if (form.elements[i].checked){
				if(form[form.elements[i].name].length > 1){
					
					if (params[form.elements[i].name] == null){
						params[form.elements[i].name] = Array();
					}
					params[form.elements[i].name].push(form.elements[i].value);				
				} else {
					params[form.elements[i].name] = form.elements[i].value;	
				}
				
			}
		} else {
			params[form.elements[i].name] = form.elements[i].value;
		}
	}		

	return params;
}

function selectOptionExists(select_list, value)
{
	var returnVal = false;
	for (var idx = 0; idx < select_list.options.length; idx++){
		if (select_list.options[idx].value == value){
			returnVal = true;
		}
	}
	return returnVal;
}

function selectToString(select_field)
{
	var list = '';
	for (var idx = 0; idx < select_field.options.length; idx++){
		list += ((idx > 0 ? ',' : '') + select_field.options[idx].value);
	}
	return list;
}

function pick_n_pack(source_select, target_select)
{
	for (var i = 0; i < source_select.options.length; i++){
		if (source_select.options[i].selected){
			if (!selectOptionExists(target_select, source_select.options[i].value)){
				target_select.options[target_select.options.length] = new Option(source_select.options[i].text, source_select.options[i].value);
				var hiddenField = id(target_select.id.replace('_trg',''));
				if (hiddenField != null){
					hiddenField.value = selectToString(target_select);
				}
			}
		}
	}
}

function removeOption(select_field)
{
	for (var i = 0; i < select_field.options.length; i++){
		if (select_field.options[i].selected){
			select_field.options[i] = null;
			if (select_field.selectedIndex >= 0){
				removeOption(select_field);
			} else {
				var hiddenField = id(select_field.id.replace('_trg',''));
				if (hiddenField != null){
					hiddenField.value = selectToString(select_field);
				}
			}
			break;
		}
	}
}

