//----------------------------------------------------------
		
	if (!applesearch)	var applesearch = {};

//----------------------------------------------------------

	applesearch.init = function ()
	{
		var srchform = document.getElementById('searchform');
		
		// Paranoia! Make sure we can get the search form before trying anything
		if(srchform) {
			
			var srchinput = document.getElementById('searchthis');
			labelText = applesearch.getLabelText();
			
			// add applesearch css for non-safari, dom-capable browsers
			if ( navigator.userAgent.toLowerCase().indexOf('safari') < 0  && document.getElementById ) {
				this.clearBtn = false;
		
				// add style sheet if not safari
var appleStyle = document.createElement('link');
appleStyle.rel = 'stylesheet';
appleStyle.type = 'text/css';
appleStyle.media = 'screen';
appleStyle.href = 'applesearch.css';
document.getElementsByTagName("head")[0].appendChild(appleStyle)

				//------------------
				
				// create and attach the span
				var r_crnr = document.createElement('span');
				r_crnr.className = 'sbox_r'; // IE win doesn't like setAttribute in this case
				r_crnr.setAttribute('id','srch_clear');
		
				var fieldsets = srchform.getElementsByTagName('fieldset');
				if (fieldsets) {
					fieldsets[0].appendChild(r_crnr);
				}
		
				// Create and attach a clearing div, keeps Opera happy
				var d_clr = document.createElement('div');
				d_clr.setAttribute('class','clear');
				srchform.appendChild(d_clr);
		
				//------------------
				
				// set our text colors
				var labels = srchform.getElementsByTagName('label');
	
				if (labels) {			
					if (labels[0].currentStyle) {
						// must be using IE
						labelColor = labels[0].currentStyle.color;
						inputColor = srchinput.currentStyle.color;
					} else if (document.defaultView.getComputedStyle) {
						labelColor = document.defaultView.getComputedStyle(labels[0], null).getPropertyValue('color');
						inputColor = document.defaultView.getComputedStyle(srchinput, null).getPropertyValue('color');
					}
				}
				
				//------------------
				
				// Paranoia again! Make sure the search field exist
				if(srchinput) {
					// set the placeholder text based off the label
					srchinput.value = labelText;
					srchinput.style.color = labelColor;
	
					// add some events to the input field
					srchinput.onkeyup = function () {
							applesearch.onChange('searchthis','srch_clear');
						}
					srchinput.onfocus = function () {
							if (this.value == labelText) {
								this.value = '';
								this.style.color = inputColor;	
							}
						}
					srchinput.onblur = function () {
							if (this.value == '') {
								this.value = labelText;
								this.style.color = labelColor;
							}
						}
				}
				
				// prevent the form being submitted if the input's value is the placeholder (label) text
				srchform.onsubmit = function()
				{
					if(srchinput && srchinput != labelText) {
						return true;
					} else {
						return false;
					}
				}
			} else {
				// Paranoia again! Make sure the search field exist
				if(srchinput) {
					// Using Safari so change some attributes to get the Apple Search field
					srchinput.type = 'search';
					srchinput.setAttribute('placeholder',labelText);
					srchinput.setAttribute('autosave','bsn_srch');
					srchinput.setAttribute('results','5');
				}
			}
		}
	}

//----------------------------------------------------------

	applesearch.getLabelText = function()
	{
		var srchform = document.getElementById('searchform');
		if(srchform) {
			var labels = srchform.getElementsByTagName('label');
	
			if (labels) {
				var labelFor = labels[0].getAttribute('for');
				var labelText = labels[0].firstChild.nodeValue;
			} else {
				// just in case, set default text
				var labelText = 'Search';
			}
		} else {
			// just in case, set default text
			var labelText = 'Search';
		}
		return labelText;
	}

//----------------------------------------------------------

	// called when on user input - toggles clear fld btn
	applesearch.onChange = function (fldID, btnID)
	{
		// check whether to show delete button
		var fld = document.getElementById( fldID );
		var btn = document.getElementById( btnID );

		if (fld.value.length > 0 && fld.value != labelText && !this.clearBtn) {
			btn.className = 'sbox_r_f2';
			btn.fldID = fldID; // btn remembers it's field
			btn.onclick = this.clearBtnClick;
			this.clearBtn = true;
		} else if (fld.value.length == 0 && this.clearBtn) {
			btn.className = 'sbox_r';
			btn.onclick = null;
			this.clearBtn = false;
			// reset the field's placeholder text
			fld.value = labelText;
			fld.style.color = labelColor;
		}
	}

//----------------------------------------------------------

	applesearch.clearFld = function (fldID,btnID)
	{
		var fld = document.getElementById( fldID );
		fld.value = '';
		this.onChange(fldID,btnID);
	}


//----------------------------------------------------------

	applesearch.clearBtnClick = function ()
	{
		applesearch.clearFld(this.fldID, this.id);
	}

//----------------------------------------------------------

function initForm(){
	browserDetect();
	initializeFocus();
	ifInstructs();
	showRangeCounters();
	initAutoResize();
	checkPaypal();
	checkMechanicalTurk();
}

// for radio and checkboxes, they have to be cleared manually, so they are added to the
// global array highlight_array so we dont have to loop through the dom every time.
function initializeFocus(){
	fields = getElementsByClassName(document, "*", "field");
	for(i = 0; i < fields.length; i++) {
		if(fields[i].type == 'radio' || fields[i].type == 'checkbox' || fields[i].type == 'file') {
			fields[i].onclick = function(){clearSafariRadios(); addClassName(this.parentNode.parentNode, "focused", true)};
			fields[i].onfocus = function(){clearSafariRadios(); addClassName(this.parentNode.parentNode, "focused", true)};
			highlight_array.splice(highlight_array.length,0,fields[i]);
		}
		if(fields[i].className.match('addr')){
			fields[i].onfocus = function(){clearSafariRadios();addClassName(this.parentNode.parentNode.parentNode, "focused", true)};
			fields[i].onblur = function(){removeClassName(this.parentNode.parentNode.parentNode, "focused")};
		}
		else {
			fields[i].onfocus = function(){clearSafariRadios();addClassName(this.parentNode.parentNode, "focused", true)};
			fields[i].onblur = function(){removeClassName(this.parentNode.parentNode, "focused")};
		}
	}
}

function initAutoResize() {
	//var par = window.location.href.toString();
	//parent.location = par+'#height='+document.body.offsetHeight;
}

function clearSafariRadios() {
	for(var i = 0; i < highlight_array.length; i++) {
		if(highlight_array[i].parentNode) {
			removeClassName(highlight_array[i].parentNode.parentNode, 'focused');
		}
	}
}

function ifInstructs(){
	var container = document.getElementById('public');
	if(container){
		removeClassName(container,'noI');
		var instructs = getElementsByClassName(document,"*","instruct");
		if(instructs == ''){
			addClassName(container,'noI',true);
		}
		if(container.offsetWidth <= 450){
			addClassName(container,'altInstruct',true);
		}
	}
}

function browserDetect(){
	var detect = navigator.userAgent.toLowerCase();
	var container = document.getElementsByTagName('html');
	if(detect.indexOf('safari') + 1){
		addClassName(container[0], 'safari', true);
	}
	if(detect.indexOf('firefox') + 1){
		addClassName(container[0], 'firefox', true);
	}
}

function checkPaypal() {
	if(document.getElementById('merchant')) {
		document.getElementById('merchantMessage').innerHTML = 'Your order is being processed. Please wait while the page redirects to the merchant checkout.';
		document.getElementById('merchantButton').style.display = 'none';
		document.getElementById('merchant').submit();
	}
}

function checkMechanicalTurk() {
	if(document.getElementById('mechanicalTurk')) {
		document.getElementById('merchantMessage').innerHTML = 'Your submission is being processed. You will be redirected shortly.';
		document.getElementById('merchantButton').style.display = 'none';
		document.getElementById('mechanicalTurk').submit();
	}
}

function showRangeCounters(){
	counters = getElementsByClassName(document, "em", "currently");
	for(i = 0; i < counters.length; i++) {
		counters[i].style.display = 'inline';
	}
}

function validateRange(ColumnId, RangeType) {
	if(document.getElementById('rangeUsedMsg'+ColumnId)) {
		var field = document.getElementById('Field'+ColumnId);
		var msg = document.getElementById('rangeUsedMsg'+ColumnId);

		switch(RangeType) {
			case 'character':
				msg.innerHTML = field.value.length;
				break;
				
			case 'word':
				var words = field.value.split(" ");
				var used = 0;
				for(i =0; i < words.length; i++) {
					if(words[i].replace(/\s+$/,"") != "") used++;
				}
				msg.innerHTML = used;
				break;
				
			case 'digit':
				msg.innerHTML = field.value.length;
				break;
		}
	}
}

/*--------------------------------------------------------------------------*/

//http://www.robertnyman.com/2005/11/07/the-ultimate-getelementsbyclassname/
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}

//http://www.bigbold.com/snippets/posts/show/2630
function addClassName(objElement, strClass, blnMayAlreadyExist){
   if ( objElement.className ){
      var arrList = objElement.className.split(' ');
      if ( blnMayAlreadyExist ){
         var strClassUpper = strClass.toUpperCase();
         for ( var i = 0; i < arrList.length; i++ ){
            if ( arrList[i].toUpperCase() == strClassUpper ){
               arrList.splice(i, 1);
               i--;
             }
           }
      }
      arrList[arrList.length] = strClass;
      objElement.className = arrList.join(' ');
   }
   else{  
      objElement.className = strClass;
      }
}

//http://www.bigbold.com/snippets/posts/show/2630
function removeClassName(objElement, strClass){
   if ( objElement.className ){
      var arrList = objElement.className.split(' ');
      var strClassUpper = strClass.toUpperCase();
      for ( var i = 0; i < arrList.length; i++ ){
         if ( arrList[i].toUpperCase() == strClassUpper ){
            arrList.splice(i, 1);
            i--;
         }
      }
      objElement.className = arrList.join(' ');
   }
}

//http://ejohn.org/projects/flexible-javascript-events/
function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn]( window.event ) };
    obj.attachEvent( "on"+type, obj[type+fn] );
  } 
  else{
    obj.addEventListener( type, fn, false );	
  }
}