function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		// otherwise, update 'characters left' counter
	else 
		countfield.value = maxlimit - field.value.length;
}

function openWin( windowURL, windowName, windowFeatures ) { 
	return window.open( windowURL, windowName, windowFeatures ) ; 
} 

var amp = '&';
var loading = false;

loadMenu = function() {
	window.addEvent('load',function(){
		$$("#header li.menu").each(function(el){
			el.set({
				'events': {
					'mouseover': function(){
						el.getElement('ul').setStyle('display','block');
					},
					'mouseout': function(){
						el.getElement('ul').setStyle('display','none');
					}
				}
			});
		});
	});
};

function init(){
	
	var theGoogleFrames = $$('iframe[name=google_ads_frame]');
	if ( theGoogleFrames.length > 0 ) {
		theGoogleFrames.each(function(theFrame){
			var thisHeight = parseInt(theFrame.getProperty('height'),10);
			theFrame.setProperty('height',thisHeight-16);
			theFrame.height = thisHeight-16;
		});
	}
	
	
	if ( $('demogrid') ) {
		$('demogrid').getElements('a.smoothbox').each(function(el){
			var arHref = el.href.split('/');
			el.href = 'popups/' + arHref[arHref.length-1] + '?height=540&width=540';
		});
	}
	
	if ( $$('a.smoothbox') && !$$('head')[0].getElement('script[src=js/smoothbox.js]') ) {
		new Element('link',{rel:"stylesheet", type:"text/css", href:"css/smoothbox.css"}).injectInside($$('head')[0]);
		new Asset.javascript('js/smoothbox.js',{onload: function(){
			TB_init();
		}});
	}
	
	if ( $('builder-chat') ) {
		$('builder-chat').addEvent('click',function(e){
			this.newWindow = window.open('https://livechat.boldchat.com/aid/1593940048381369671/bc.chat?cwdid=3989877143406858234&url=' + escape(document.location.href), 'Chat', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=640,height=480');
			this.newWindow.focus();
			this.newWindow.opener=window;
			e = new Event(e).stop();
		});
	}
	/*
	if ( document.all ) {
		$$("#topnav li").each(function(li){
			li.onmouseover = function() {
				this.addClassName("over");
			};
			li.onmouseout = function() {
				this.removeClassName("over");
			};
		});
	}*/
	
	if ( $('builder-trial') ) {
		$('builder-trial').setProperty('href','demo_ajax.htm?width=530&height=580');
	}
	
	$$('div.lazyLoadable').addEvent('scroll',function(e){
		var theLoader = this.getElement('a[class=lazy]');
		var theLoaderID, theLoaderHref, theContainer;
		var theScroll = this.scrollTop;
		var theGallery = this;
		if ( this.scrollHeight - this.scrollTop - this.offsetHeight < 20 && theLoader && !loading ) {
			loading = true;
			theLoaderID = theLoader.getProperty('id');
			theLoaderHref = theLoader.getProperty('href');
			theContainer = new Element('div',{'id':'container-'+theLoaderID,'styles':'display: inline;'});
			theGallery.appendChild(theContainer);
			new Ajax(theLoaderHref,{
				method: 'get',
				async: false,
				update: 'container-'+theLoaderID,
				onComplete: function() {
					if ( $(theLoaderID) ) 
						$(theLoaderID).remove();
					theGallery.scrollTop = theScroll;
					//TB_init();
					loading = false;
				}              
			}).request();
			
		}
	});
	
	$$('a[name=email]').each(function(a){
		theLink = a.innerHTML.replace('[at]','@');
		a.setProperty('href','mailto:'+theLink);
		a.innerHTML = theLink;
	});
	
	$$('a[class^=popup_]').addEvent('click',function(e){
		var width = this.className.split('_')[1];
		var height = this.className.split('_')[2];
		newWindow = openWin( this.href, 'popup', 'width='+width+',height='+height+',toolbar=0,location=0,directories=0,status=1,menuBar=0,scrollBars=1,resizable=1' ); 
		newWindow.focus();
		e = new Event(e).stop();
	});
	
	$$('form[class^=oneclick_]').addEvent('submit',function(){
		var theButton = this.className.split('_')[1];
		if ( $(theButton) ) {
			$(theButton).setProperties({
				'value': $(theButton).getProperty('title'),
				'disabled': 'disabled'
			});
		}
	});
	
	if ( $('galleryJump') ) {
		$('galleryJump').addEvent('click',function(e){
			$('gallery-templates').setStyle('zIndex','20');
			$('gallery-samples').setStyle('zIndex','15'); 
			this.getElement('img').setProperty('src','images/btn.templates.active.gif');
			$('samplesJump').getElement('img').setProperty('src','images/btn.samples.inactive.gif');
			e = new Event(e).stop();
		});
	}
	if ( $('samplesJump') ) {
		$('samplesJump').addEvent('click',function(e){
			$('gallery-templates').setStyle('zIndex','15');
			$('gallery-samples').setStyle('zIndex','20'); 
			this.getElement('img').setProperty('src','images/btn.samples.active.gif');
			$('galleryJump').getElement('img').setProperty('src','images/btn.templates.inactive.gif');
			e = new Event(e).stop();
		});
	}
	$$('table.sortable').each(function(f){
		var theForm = 'frm_'+f.id;
		if ( $(theForm) ) {
			new Element('input').setProperties({name:theForm+'_sortby',id:theForm+'_sortby',type:'hidden',value:$$('#'+f.id+' th.sortby')[0].title}).injectTop($(theForm));
		}
		f.getElements('th.sortby').setStyles("color: #23939c; text-decoration: underline; cursor: pointer;").addEvent('click',function(){
			document.getElementById('frm_'+this.parentNode.parentNode.parentNode.id+'_sortby').value = this.title;
			//alert(document.getElementById('frm_'+this.parentNode.parentNode.parentNode.id+'_sortby').value);
			document.getElementById('frm_'+this.parentNode.parentNode.parentNode.id).submit();
		});
	});
	
	$$('dl.tabbed').each(function(dl){
		dl.getElements('dt').each(function(dt,i){
			dt.set({
				'styles':{
					'cursor': 'pointer',
					'position': 'absolute',
					'top': '0px',
					'left': i * 155 + 'px'
				},
				'events':{
					'click':function(){
						$(this.parentNode.id).getChildren().removeClass('active').addClass('inactive');
						$(this.id).removeClass('inactive').addClass('active');
						$(this.id+'_body').removeClass('inactive').addClass('active');
					}
				}
			});
			if ( i > 0 ) {
				dt.removeClass('active').addClass('inactive');
			}
		});
		dl.getElements('dd').setStyles({
			'position': 'absolute',
			'top': '24px'
		});
		dl.getElement('dd').addClass('active');
	});
	
	$$('input.calendarPopup').each(function(el,c){
		if ( el.getAttribute('id').length > 0 ) {
			popup = new Element('img',{
				'src' : "images/calendar_16.gif",
				'class' : "cal_btn",
				'id' : "_btn_" + el.id,
				'styles': { 'margin-left' : "5px" },
				'title' : "Click here pick a date from the Calendar"
			}).injectAfter(el.id);
			Calendar.setup({
				inputField     :    el.id,        // id of the input field
				ifFormat       :    "%m/%d/%y",             // format of the input field (even if hidden, this format will be honored)
				button         :    "_btn_" + el.id,               // trigger for the calendar (button ID)
				align          :    "Br",                   // alignment (defaults to "Bl")
				singleClick    :    true,
				range          :    [2000,2010]
			});
		}
	});
	
	if ( document.getElementsByTagName('textarea') ) {
		textareas = document.getElementsByTagName('textarea');
		for ( var e = 0 ; e < textareas.length ; e++ ) {
			if ( textareas[e].className.indexOf('maxlength_') > -1 ) {
				var theseClasses = textareas[e].className.split(' ');
				var maxLength = 0;
				for ( var c = 0 ; c < theseClasses.length ; c++ ) {
					if ( theseClasses[c].indexOf('maxlength_') == 0 ) {
						maxLength = theseClasses[c].split('_')[1];
					}
				}
				var maxdiv = document.createElement('em');
				maxdiv.className = "maxlength";
				maxdiv.innerHTML = '<input type="text" size="3" readonly="readonly" value="'+maxLength+'" name="'+textareas[e].name+'_maxlength" /> chars';
				insertAfter(textareas[e].parentNode,maxdiv,textareas[e]);
				textareas[e].maxlength = maxLength;
				textareas[e].countfield = textareas[e].name + '_maxlength';
				textareas[e].onkeydown = function() {textCounter(this,this.form[this.countfield],this.maxlength)};
				textareas[e].onkeyup = function() {textCounter(this,this.form[this.countfield],this.maxlength)};
			}
		}
	}
	
	if ( document.getElementsByTagName('form') ) {
		theForms = document.getElementsByTagName('form');
		for ( f = 0 ; f < theForms.length ; f++ ) {
			theForms[f].oldsubmit = ( typeof theForms[f].onsubmit == 'function' ) ? theForms[f].onsubmit : function(){};
			theForms[f].onsubmit = function() {
				if ( domValidateForm(this) ) {
					return this.oldsubmit();
				} else return false;
			}
		}
	}
	
	function domValidateForm(thisForm) {
		var theReturn = true;
		var thisClass = "";
		var thisName = "";
		var thisValue = "";
		var emailReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var dateReg = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2,4}$/;
		var handled = {};
		var checked = false;
		for ( var r = 0 ; r < thisForm.elements.length ; r++ ) {
			thisClass = " " + thisForm.elements[r].className + " ";
			thisName = ( thisForm.elements[r].title != '' ) ? thisForm.elements[r].title : thisForm.elements[r].name;
			if ( thisClass.indexOf(' required ') > -1 ) {
				if ( ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' || thisForm.elements[r].type == 'textarea' || thisForm.elements[r].type == 'file' ) && thisForm.elements[r].value.length == 0 ) {
					alert(thisName + ' is required before submitting the form');
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'select-one' && thisForm.elements[r].options[thisForm.elements[r].selectedIndex].value == '' ) {
					alert(thisName + ' is required before submitting the form');
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'select-multiple' && thisForm.elements[r].selectedIndex == -1 ) {
					alert(thisName + ' is required before submitting the form');
					theReturn = false;
				} else if ( thisForm.elements[r].type == 'radio' || thisForm.elements[r].type == 'checkbox' ) {
					if ( typeof handled[thisForm.elements[r].name] == 'undefined' ) {
						checked = false;
						if ( typeof thisForm[thisForm.elements[r].name].length == 'undefined' ) {
							checked = ( thisForm.elements[r].checked == true );
						} else {
							for ( var e = 0 ; e < thisForm[thisForm.elements[r].name].length ; e++ ) {
								if ( thisForm[thisForm.elements[r].name][e].checked == true ) {
									checked = true;
									break;
								}
							}
						}
						if ( !checked ) {
							alert(thisName+' is required before submitting the form');
							theReturn = false;
						}
						handled[thisForm.elements[r].name] = true;
					}
				}
			}
			if ( thisClass.indexOf(' integer ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( thisForm.elements[r].value.length > 0 && ( parseInt(thisForm.elements[r].value) != thisForm.elements[r].value-0 ) ) {
					alert(thisName + ' must be a proper number');
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' numeric ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( thisForm.elements[r].value.length > 0 && ( parseFloat(thisForm.elements[r].value) != thisForm.elements[r].value-0 ) ) {
					alert(thisName + ' must be a proper number');
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' email ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !emailReg.test(thisForm.elements[r].value) ) {
					alert(thisName + ' must be a valid email address to submit the form');
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' date ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( !dateReg.exec(thisForm.elements[r].value) ) {
					alert(thisName + ' must be a valid date to submit the form, like "mm/dd/yy"');
					theReturn = false;
				}
			}
			if ( thisClass.indexOf(' confirm ') > -1 && ( thisForm.elements[r].type == 'text' || thisForm.elements[r].type == 'password' ) && thisForm.elements[r].value.length > 0 ) {
				if ( r > 0 && thisForm.elements[r].value != thisForm.elements[r-1].value ) {
					alert(thisName + ' and ' + prevName + ' must have the same values');
					theReturn = false;
				}
			}
			prevName = thisName;
		}
		return theReturn;
	}
	if ( typeof UFO != 'undefined' && document.getElementById('flashMe') ) UFO.create(
		{ 
			movie:"home_content_loder.swf", 
			width:"355", 
			height:"189",
			majorversion:"6", 
			build:"40",
			wmode:"opaque"
		}, 
		"flashMe"
	);
	
	/*if ( typeof SMPortal != 'undefined' ) {*/
		$$('div[id^=SM_]').each(function(div){
			if ( !jimAuld.utils.flashsniffer.meetsMinVersion(8) ) {
				var theWarning = new Element('div',{
					'styles': 'border: 2px solid #ff0000; background-color: #ffff99; padding: 6px;'
				}).setHTML('<p>If you don\'t see any content below, it might be because you don\'t have the right version of Flash. <a href="http://www.adobe.com/go/EN_US-H-GET-FLASH" target="_blank">Click here to download and install the latest Flash Player</a></p>').injectBefore(div);
			}
			var theType = div.getProperty('id').split('_')[1];
			SMPortal.insertModule(theType);
		});
	/*}*/
}


window.onload = init; 


