function place_order()
{
    var btn1 = getElem( 'place1' );
    var btn2 = getElem( 'place2' );

    btn1.disabled = true;
    btn2.disabled = true;

    
    btn1.src = '/images/buttons/button-place-order_off.gif';
    btn2.src = '/images/buttons/button-place-order_off.gif';
    
    return true;
} // place_order

function isDefined(v) {
	var undef;
	return v !== undef;
}

function numElements( o )
{
    if( typeof o != 'object' )
      {
	  return undefined;
      }
    
    var n = 0;
    for ( var i in o )
      {
	  n++;
      }
    return n;
}

function redirectform_fromlink(f,a,t,h) {
	if ( redirectform(f,a,t,h) ) {
		f.submit();
	}
	return false;
}

function redirectform(f,a,t,h)
{
    // Form, Action, Target, Hidden
    if( !isDefined(f) ) {return false; }
    if( isDefined(a) && a.length ) { f.attributes["action"].value = a; } else { return false; }
    if( isDefined(t) && t.length ) { f.attributes["target"].value = t; } else { return false; }
    if( isDefined(h) && h.length ) { f.elements["action"].value = h; } else { return true; }
    return true;
}

function getElem(s)
{
	var doc = document;
	if(doc.all && !doc.getElementById) {
		return doc.all[s];
	} else {
		return doc.getElementById(s);
	}
}

function newImage(arg) {
    if (document.images) {	
	rslt = new Image();	
	rslt.src = arg;	
	return rslt;	
    }
}

function changeImages() {
    if (document.images && (preloadFlag == true)) {
	for (var i=0; i<changeImages.arguments.length; i+=2) {
	    document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
	}
    }
}

var preloadFlag = false;
var preLoads = new Array();
function preloadImages() {
	if (document.images) {
	for (var i=0; i<preloadImages.arguments.length; i++) {
	preLoads[i] = newImage(preloadImages.arguments[i]);
	}
	preloadFlag = true;
	}
}

function popScrollableWindow( url, width, height )
{
	popWindowCustom( url, width, height, 'location=no,menubar=no,scrollbars=yes,resizable=no,toolbar=no' );
}

function popWindow( url, width, height )
{
	var wnd = window.open( url,
			       '_blank', 
			       'height=' + height + ',width=' + width + ',location=no,menubar=no,scrollbars=no,resizable=no,toolbar=no'
			       );
	wnd.focus();	
} // popWindow

function popWindowCustom( url, width, height, features )
{
	var wnd = window.open( url,
			       '_blank', 
			       'height=' + height + ',width=' + width + ( features ? ',' + features : '' )
			       );
	wnd.focus();	
} // popWindow


var _POPUP_FEATURES = 'location=0, statusbar=0, menubar=0, width=400, height=300';

function raw_popup(url, target, features) {
    if (!isDefined(features))
    {
	features = _POPUP_FEATURES;
    }
    if (!isDefined(target))
    {
	target = '_blank';
    }
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features)
{
    return  raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

// For repopulating a select field; pass in associative array of new values
function repop( oselect, ooptions, dstr, selectedval ) {
    oselect.length = 0;
    if( !isDefined(dstr) )
	dstr = "-";

    oselect.options[oselect.options.length] = new Option(dstr,'', false, false);

    for ( var i in ooptions ) {
	var s = false;
	s = ( isDefined(selectedval) && i == selectedval ? true : false );
	var o = new Option( ooptions[i], i , false, s);
	oselect.options[oselect.options.length] = o;
	oselect.options[oselect.options.length-1].selected = s; // needed for IE 6.0
    }
    
    if( numElements(ooptions) < 1 ) {
	oselect.style.display = 'none';
    }
    else {
	oselect.style.display = 'inline';
    }
}

function changeAction( formName, actionValue )
{
	document.forms[formName].elements['action'].value = actionValue;
} // changeAction


function dateChanged(calendar,targetInput)
{
    // Beware that this function is called even if the end-user only;
    // changed the month/year.  In order to determine if a date was;
    // clicked you can use the dateClicked property of the calendar:
    if (calendar.dateClicked)
    {
	var y = calendar.date.getFullYear();
	var m = calendar.date.getMonth() + 1;
	var d = calendar.date.getDate();
	document.getElementById(targetInput).value = y + "/" + ( m < 10 ? '0':'') + m + "/" + ( d < 10 ? '0':'') +  d;
    }
};

function isWeekend(d)
{
    return ( d.getDay() == 0 || d.getDay() == 6 ? true :  false);
}

function isPast(d)
{
    var today  = new Date();
    var yester = new Date( today.valueOf() - (60*60*24*1000) );
    return ( d < yester ? true : false);
}

function isFuture(d)
{
    var today = new Date();
    var tomor = new Date( today.valueOf() + (60*60*24*1000) );
    return ( d > tomor ? true :  false);
}

function showElem( elemName, displayType )
{
    var elem = getElem( elemName );
    if( displayType == null )
	displayType = 'block';
    if( elem && elem.style ) {
	elem.style.display = displayType;
    }
}	

function hideElem( elemName )
{
    var elem = getElem( elemName );
    if( elem && elem.style ) {
	elem.style.display = 'none';
    }
}	

function limitText( txaContent, txtCount, nLimit )
{
    var nLength = txaContent.value.length;

    if( nLength > nLimit ) { // then we're over our limit
	txaContent.value = txaContent.value.substring( 0, nLimit );
    } else { // update counter
	//txtCount.value = nLimit - nLength;
	txtCount.innerHTML = (nLimit - nLength) + ' characters left';
    }
} // limitText


// on the contact page, selecting "Duplicate ..." from the dropdown
// should redirect you immediately to duplicate.html, and carry forward
// any form data to the new form. server-side code also exists for
// building the url and and doing the redirect, in case a direct POST
// with a "Duplicate ..." subject occurs.

function onchangeContactSubject(val, htmlselect)
{
  var option = htmlselect.options[htmlselect.selectedIndex];

  if (option.text == 'Duplicate For Eyes Glasses') {

    var url = '/about/duplicate.html';

    var params = getFormData(htmlselect.form);
    delete params['action'];
    delete params['subject'];

    var query = buildQueryString(params);
    if (query != '') url += '?' + query;

    window.location = url;
  }
}

// FIXME *lame* reliance on jquery.form.js browser hacks here

function getFormData(form) {
  var data = {};
  var A = $(form).formToArray();

  for (var i in A) {
    var elem = A[i];
    data[elem.name] = elem.value;
  }

  return data;
}

function buildQueryString(params) {
  var query = '';

  for (var key in params) {
    if (query != '') query += '&';
    query += encodeURIComponent(key);
    query += '=';
    query += encodeURIComponent(params[key]);
  }

  return query;
}


function getModalHeight() {
	var modalHeight = 688;
	if ( window.innerHeight ) {
		modalHeight = window.innerHeight*0.8;
	}
	else if ( document.documentElement.clientHeight ) { 
		modalHeight = document.documentElement.clientHeight*0.8;
	}
	
	return modalHeight;
}

$(document).ready(function() {
	loadMain();

/*
	// ie6 transp. png replacement
	$('div#main').supersleight({shim: '/images/transparent.gif'});
*/

	$('input#text-1').focus(function() {
		$('input#text-1').addClass('hover');
	});
	$('input#text-1').blur(function() {
		$('input#text-1').removeClass('hover');
	});
	
	if (!($.browser.msie && $.browser.version.substr(0,1)<7)) {
		$('select.frmSelect:not(.month-year-dropdown)').selectmenu({
			style : 'dropdown',
			maxHeight : 180,
			menuWidth: 397
		});
		$('div.date-dropdowns select.date-dropdown').selectmenu({
			style : 'dropdown',
			maxHeight : 180,
			menuWidth: 124
		});
		$('div.month-year-dropdowns select.month-year-dropdown').selectmenu({
			style : 'dropdown',
			maxHeight : 180,
			menuWidth: 189
		});
		$('select.productlistwide').selectmenu({
			style : 'dropdown',
			maxHeight : 300,
			menuWidth: 294

		});		
		$('select.locateSelect').selectmenu({
			style : 'dropdown',
			maxHeight : 180,
			menuWidth: 114
		});
		$('.homecol select.productlist').selectmenu({
			style : 'dropdown',
			maxHeight : 180,
			menuWidth: 218
		});
		$('.homecol #mapForm select').selectmenu({
			style : 'dropdown',
			maxHeight : 180,
			menuWidth: 58
		});
	}
	

	
	
	$('a.lightbox').nyroModal({
		width: 770,
		height: getModalHeight(),
		windowResize: false,
		resizable: false,
		autoSizeable: false,
		beforeHideContent: function(elts, settings, callback) {
			$('.close').hide();
			callback();
		},
	      processHandler: function(settings) {
		    // turn off url-hash-triggered filtering
		    if( settings.selector ) {
			settings.url += settings.selector;
			settings.selector = null;
		    }
		},
	        endShowContent: function(elts, settings) {
		    // lightbox has been loaded -- call Javascript here
		    
		    // Get hash from url
		    var url  = this.url;
		    var hash = null;
		    if (typeof url == 'string') {
			var hashPos = url.indexOf('#');
			if (hashPos > -1)
			    hash = url.substring(hashPos);
		    }
		    
		    // If valid hash
		    if( hash && hash.length > 1 ) {
			// Then scroll to it
			var lbox = $( '#nyroModalContent .popup' );
			lbox.scrollTo( hash, 650 );
		    }
		} // endShowContent
	});

    $('input').keydown(function(e){
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });


});

