function clearDefault(el) {
  if (el.defaultValue==el.value) { el.value = ""; }
}

function countElements(container_id, tag) {
	var container = document.getElementById(container_id);
	var elem_count = container.getElementsByTagName(tag).length;
	return elem_count;
}


function clearDefault(el) {
	if (el.defaultValue==el.value) { el.value = ""; }
}

function jsubmit() {
	if (validate())
	{
		document.emailreg.submit();
	}
}

function isGoodEmail(emailStr, thankyouflag) {
	if (thankyouflag == null) {
		thankyouflag=true;
	}
	var emailPat=/^(.+)@(.+)$/,
		specialChars="\\(\\)<>@,;:\!\\\\\\\"\\.\\[\\]",
		validChars="\[^\\s" + specialChars + "\]",
		firstChars=validChars,
		quotedUser="(\"[^\"]*\")",
		ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/,
		atom="(" + firstChars + validChars + "*" + ")",
		word="(" + atom + "|" + quotedUser + ")",
		userPat=new RegExp("^" + word + "(\\." + word + ")*$"),
		domainPat=new RegExp("^" + atom + "(\\." + atom +")*$"),
		matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1],
		domain=matchArray[2];

	// See if "user" is valid
	if (user.match(userPat)==null) {
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		// this is an IP address
		for (var i=1;i<=4;i++)
		{
			if(IPArray[i]>255)
			{
				return false;
			}
		}
		return true;
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		return false;
	}
	var atomPat=new RegExp(atom,"g"),
		domArr=domain.match(atomPat),
		len=domArr.length;

	if(domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
	   return false;
	}
	if (domArr[domArr.length-1].length==3 && len<2) {
	   return false;
	}
	if(thankyouflag) {
	   window.open('Thank You', 'newFormWindow', 'width=340,height=260');
	}
	return true;
}

function addPre(email){
 if(!isGoodEmail(email,false)){
 	alert("Please make sure that you\ninput a valid e-mail address");
 	return;
 }
 $.getJSON("/preferences.jspx",{"email":email},function(data){
 	if(data.msg) alert("Subscribe to Success");
 });
}


/* designer updates - November 09 */

$(document).ready(function() {
    try {
        $("#cancel").click(function(){
            document.customerPreferences.reset();
        });
    } catch(e){ }
    var showTimeout,
		hideTimeout;
	$("li.main-menu div").css('visibility', 'hidden');
	$("li.main-menu").hover(function(e) {
		var that = $(this);
		clearTimeout(hideTimeout);
		showTimeout = setTimeout(function () {
			$("li.main-menu div").css('visibility', 'hidden');
			$("li.main-menu").removeClass("hovered");
			var menuID = that.attr("id");
			if (menuID) menuID = menuID.toLowerCase()+"-menu";
			menuID = menuID.replace(/ /, "-");
			$("div#"+menuID+", div#"+menuID+" div").css('visibility', 'visible');
			that.addClass("hovered");
		}, 300);
	}, function(e) {
		hideTimeout = setTimeout(function () {
			$("li.main-menu div").css('visibility', 'hidden');
			$("li.main-menu").removeClass("hovered");

		}, 300);

		clearTimeout(showTimeout);
	});
});

$(document).ready(function() {
    try {
        $("#what-is-this").show();
        $(".instruction").show();
        $("#recaptcha_response_field").show();
        $("#recaptcha-output").show();
        }
    catch(e){
        // do nothing
    }

});

jQuery.cookie = function(name, value, options) {  
     if (typeof value != 'undefined') { // name and value given, set cookie  
         options = options || {};  
         if (value === null) {  
             value = '';  
             options.expires = -1;  
         }  
         var expires = '';  
         if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {  
             var date;  
             if (typeof options.expires == 'number') {  
                 date = new Date();  
                 date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));  
             } else {  
                 date = options.expires;  
             }  
             expires = '; expires=' + date.toUTCString();  
         }  
         var path = options.path ? '; path=' + (options.path) : '';  
         var domain = options.domain ? '; domain=' + (options.domain) : '';  
         var secure = options.secure ? '; secure' : '';  
         document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');  
     } else {  
         var cookieValue = null;  
         if (document.cookie && document.cookie != '') {  
             var cookies = document.cookie.split(';');  
             for (var i = 0; i < cookies.length; i++) {  
                 var cookie = jQuery.trim(cookies[i]);  
                 if (cookie.substring(0, name.length + 1) == (name + '=')) {  
                     cookieValue = decodeURIComponent(cookie.substring(name.length + 1));  
                     break;  
                 }  
             }  
         }  
         return cookieValue;  
     }  
 }



var scrolltotop={
		setting: {startline:10, scrollto: 0, scrollduration:500, fadeduration:[500, 100]},
		controlHTML: '<img src="/r/scrolltop.png"/>',
		controlattrs: {offsetx:5, offsety:5}, 
		anchorkeyword: '#top', 

		state: {isvisible:false, shouldvisible:false},

		scrollup:function(){
			if (!this.cssfixedsupport) {
				this.$control.css({opacity:0}) 
			};
			var dest=isNaN(this.setting.scrollto)? this.setting.scrollto : parseInt(this.setting.scrollto);
			if (typeof dest=="string" && jQuery('#'+dest).length==1) { 
				dest=jQuery('#'+dest).offset().top;
			} else {
				dest=this.setting.scrollto;
			};
			this.$body.animate({scrollTop: dest}, this.setting.scrollduration);
		},

		keepfixed:function(){
			var $window=jQuery(window);
			var controlx=$window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx;
			var controly=$window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety;
			this.$control.css({left:controlx+'px', top:controly+'px'});
		},

		togglecontrol:function(){
			var scrolltop=jQuery(window).scrollTop();
			if (!this.cssfixedsupport) {
				this.keepfixed();
			};
			this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false;
			if (this.state.shouldvisible && !this.state.isvisible){
				this.$control.stop().animate({opacity:1}, this.setting.fadeduration[0]);
				this.state.isvisible=true;
			}
			else if (this.state.shouldvisible==false && this.state.isvisible){
				this.$control.stop().animate({opacity:0}, this.setting.fadeduration[1]);
				this.state.isvisible=false;
			}
		},
		
		init:function(){
			jQuery(document).ready(function($){
				var mainobj=scrolltotop;
				var iebrws=document.all;
				mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest;
				mainobj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body');
				mainobj.$control=$('<div id="topcontrol">'+mainobj.controlHTML+'</div>')
					.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'pointer'})
					.attr({title:'Scroll Back to Top'})
					.click(function(){mainobj.scrollup(); return false;})
					.appendTo('body');
				if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') {
					mainobj.$control.css({width:mainobj.$control.width()}); 
				};
				mainobj.togglecontrol();
				$('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
					mainobj.scrollup();
					return false;
				});
				
				$(window).bind('scroll resize', function(e){
					mainobj.togglecontrol();
				});
			});
		}
	};
	scrolltotop.init();


