	// The purpose of this function is to check whether email is in valid format
	function isEmail(email){
  		return email.match(/@.*\./);
	}	
	
	//This is lists related Javascript used in wish lists and cool green stuff lists
    
    //shows more of description text 
    
    function more(id){
        Element.show('more-'+id);
        Element.hide('less-'+id);		
    }
    
    //hides extra description text
    
    function less(id){
            Element.show('less-'+id);
            Element.hide('more-'+id);
     }
        
	//toggles form view
	
	function toggle_form(form,id){
		var divList = $('form-div-'+id).getElementsByTagName('div');
		var divs = $A(divList);
		divs.each(function(element){
					//alert(element.id)
					arr = element.id.split('-');
					if(arr[1]=='div'){
						if(arr[0] != form){
							Element.hide(arr[0]+'-div-'+id);	
							//Element.hide(arr[0]+'-action-div-'+id);	
							//Element.hide('last-div-'+id);	
						}else{
							Element.toggle(form+'-div-'+id);	
							//Element.toggle(form+'-action-div-'+id);	
							//Element.toggle('last-div-'+id);	
						}
					}
				}					  
		)		
	}
	    
  	//show who list for a product
	
	function showwho(id) {
	  if($('who-div-'+id).style.display == 'none'){
	  	new Ajax.Updater('who-div-'+id, '/product/who_list', {
	    	parameters: { productid: id },	    	
	    	evalScripts: true,
			  onComplete: function() {
			    Element.toggle('who-div-'+id);
			    Element.scrollTo("product_list_item_" + id);
			  }
	  	});
	  }else{			
			Element.toggle('who-div-'+id);
			Element.scrollTo("product_list_item_" + id);
		}
	};
	
    	//shows list of comments for a product
    
    	function showcomments(id) {
	  if($('comment-div-'+id).style.display == 'none'){
	  	new Ajax.Updater('comment-div-'+id, '/comment/list', {
	    	parameters: { productid: id },
	    	evalScripts: true,
	    	onLoading: $('comment-div-'+id).innerHTML = 'Loading ...',
			onComplete: function() {
			  toggle_form('comment',id);
			  Element.scrollTo("product_list_item_" + id);
		  }
	  	});
	  }else{
			toggle_form('comment',id);
			Element.scrollTo("product_list_item_" + id);
		}
	};
		
	//saves comments 
	
	function savecomments(id){
	  if($F('comment-text-'+id) != "Type your review here"){
		  new Ajax.Request('/comment/new', {
			parameters: {
			  productid: id,
			  comment: $F('comment-text-'+id)
			},			
			onComplete: toggle_form('comment',id)
		  });		
	  }else{
			clear_field('comment-text-'+id);  
	  }
	};
	
	//saves tags
	
	function savetags(id){
	  new Ajax.Request('/tag/save_tags', {
	    parameters: {
	       productid: id, 
	       tags: $F('tags-'+id)
	    },		
		onComplete: toggle_form('tag',id)
	  });
	};
	
	//save url from admin interface
	
	function saveurl(id){
	  new Ajax.Request('/admin/urls/' + id, {
	    parameters: {
	       url: $F('url-'+id)
	    },
  	  method: 'put',
  		onLoading: $('last-div-'+id).innerHTML = 'Saving ...',
  		onComplete: toggle_form('url',id)
	  });
	};
	
	//shows rate a product form
	
	function showrating(id) {
	  if($('rate-div-'+id).style.display  == 'none'){
	  	new Ajax.Updater('rate-div-'+id, '/product/product_rating', {
	    	parameters: { productid: id },
	    	evalScripts: true,
	    	onComplete: toggle_form('rate',id)
	  	});
	  }else{
			toggle_form('rate',id)			  
		}
	};
	
	//updates rating in JS
	
	function changevote(vnum, lastrate, id){
    	if(lastrate != vnum){    	   
    	   for (var x=1; x<=5; x++){
    	       $('star_' + id + '_' + x).className = (x<=vnum) ? 'star' : 'stardim';
    	   }	
    	}			
	}
	
	//save rating to database
	
	function saverating(rate, lastrate, id){
		new Ajax.Request('/product/update_rating', {
	    parameters: {
	      productid: id,
	      rating: rate
	    },
	    onComplete: changevote(rate, lastrate, id) 
	  });		
	}
	
	//shows rate a product form
	
	function showedit(id, uri) {
	   if($('edit-div-'+id).style.display == 'none'){
	  		new Ajax.Updater('edit-div-'+id, '/product/edit', {
	    		parameters: { productid: id, uri: uri},
				onLoading: $('edit-div-'+id).innerHTML = 'Loading ...',
				onComplete: toggle_form('edit',id)
	  		});
	   }else{
		   toggle_form('edit',id);
		}
	};
	
	//displays hover message for rating
	
	function hoverMessage(i, id){
        if(i == 1){
            $('rate_message_'+id).innerHTML = "I hate it";
        } 
        if(i == 2){
            $('rate_message_'+id).innerHTML = "I don't like it";
        } 
        if(i == 3){
            $('rate_message_'+id).innerHTML = "It's OK";
        } 
        if(i == 4){
            $('rate_message_'+id).innerHTML = "I like it";
        } 
        if(i == 5){
            $('rate_message_'+id).innerHTML = "I love it";
        } 
    }
	
	//select text box content
	function selText(control_id){
		 var control = document.getElementById(control_id);
  		 //control.select();
		 control.focus();
		 control.select();		 		 		
	}
	
	//enable disable function for lists
	
	function disable_forms(id, action)
	{		
		$(action+'_disabler_'+id).style.height = $('item_'+id).offsetHeight+"px"
		$(action+'_disabler_'+id).style.display = '';
		$(action+'_inner_text_'+id).style.display = '';
	}
	function enable_forms(id, action)
	{	
		$(action+'_disabler_'+id).style.display = 'none';		
		$(action+'_inner_text_'+id).style.display = 'none';				
	}
	
	function clear_field(control_id){
		$(control_id).innerHTML = "";
		$(control_id).className = "";
	}
	
	function clear_text_field(control_id){
		$(control_id).value = "";
		//$(control_id).className = "";
	}
	function move(id){
		var list = $('sortable_items').getElementsByTagName('li');
		var items = $A(list);
		items.each(function(it) {
			it.className = 'sortable_li';			
		});
		Sortable.create("sortable_items", {onUpdate:function(){new Ajax.Request('/user_list/update_product_order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("sortable_items")})}, only:'sortable_li'});
		disable_forms(id, 'move');
	}
	function unmove(id){
		var list = $('sortable_items').getElementsByTagName('li');
		var items = $A(list);
		items.each(function(it) {
			it.className = '';			
		});
		Sortable.create("sortable_items", {onUpdate:function(){new Ajax.Request('/user_list/update_product_order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("sortable_items")})}, only:'sortable_li'});
		enable_forms(id, 'move');
	}
	
	//the function handles buy clicked action
	function buy(id){
		location.href ='/user_list/purchase/'+id;		
	}
	
	function buy_it(id){
		location.href = '/user_list/purchase_item/'+id;		
	}
	
	//email envelope click functions
	/*
	function share(id){
		window.open('/user/share_product/'+id);	
	}
	
	function share_list(id){
		window.open('/user/share_list/'+id);	
	}
	
	function share_profile(id){
		window.open('/user/share_profile/'+id);		
	}
	
	function send_reminder(id){
		window.open('/user/create_list_reminder/'+id);			
	}
	
	function share_es_list(type){
		window.open('/user/share_es_list?list_type='+type);	
	}
	
	function share_tag_list(tag){
		window.open('/user/share_tag_list?tag='+tag);	
	}
	
	function share_member_list(){
		window.open('/user/share_member_list');
	}
	
	function share_rating_list(){
		window.open('/user/share_rating_list');	
	}
	
	function share_comment_list(){
		window.open('/user/share_comment_list');	
	}	
	*/
		
	function add_friend_to_email_box(address){
		if($('emails').innerHTML != ""){
			$('emails').innerHTML += ', ';
			$('emails').innerHTML += address; 			
		}else{
			$('emails').innerHTML += address;
		}	
	}
	
	function disable_add_forms()
	{		
		$('add_disabler').style.height = $('add_form_container').offsetHeight-20+"px";		
		$('add_disabler').style.display = '';		
		$('add_inner_text').style.display = '';				
	}
	
	function enable_add_forms()
	{	
		$('add_disabler').style.display = 'none';		
		$('add_inner_text').style.display = 'none';				
	}
	//creates a product photo popup
	function create_popup(id, x, y)
	{	//var x = 400
		//var y = 200	
		var w = screen.width/2 - x/2;
		var h = screen.height/2 -y/2;
		
		var x1 = x + 5;
		var y1 = y + 5;		
		window.open('/product/picture_view/'+id, null, "height="+y1+",width="+x1+",status=no,toolbar=no,status=no,menubar=no,location=no,top="+h+", left="+w);		
	}
	//two variations of resend conformation function		
	function resend_confirmation_mail_from_myaccount(id){
		new Ajax.Request('/user/resend_confirm', {
			parameters: {
			  userid: id
			},			
			onComplete: function(){disable_acc(); Effect.Fade('resend-form', {duration: 0.5});Element.hide('resend-hide-link');Element.show('resend-show-link');}
		  });	
	}
	
	function resend_confirmation_mail(id){
		new Ajax.Request('/user/resend_confirm', {
			parameters: {
			  userid: id
			},			
			onComplete: function () {Element.hide('resend-link'); Element.show('wait-text');}
		  });	
	}
	
	Object.extend(Position, {
	   /* adapted from Control.Modal (http://livepipe.net/downloads/control.modal.1.2.12.js)
  	  	* @author Ryan Johnson <ryan@livepipe.net>
      		* @copyright 2007 LivePipe LLC
      		* @license MIT
      		* @url http://livepipe.net/projects/control_modal/
  	  */
    	getWindowWidth: function(){
  		return (self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0);
  	},
  	getWindowHeight: function(){
  		return (self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0);
  	},
  	getDocumentWidth: function(){
  		return Math.max(document.body.scrollWidth,Position.getWindowWidth());
  	},
  	getDocumentHeight: function(){
  		return Math.max(document.body.scrollHeight,Position.getWindowHeight());
  	}
	});
	
	disabled_stack = 0;
	function disable_acc(name)
	{
	  if(!name){name = "acc_inner_text"};
    $(name).show();
    
    disabled_stack += 1;
    if(disabled_stack > 1)
      return;
          
	  position_acc_disabler();
	  $('acc_disabler').show();
	  Event.observe(window, 'resize', position_acc_disabler, false);
	}
	
	function position_acc_disabler() {
	  $('acc_disabler').setStyle({
	    height: Position.getDocumentHeight() + 'px',
	    width:  Position.getDocumentWidth() + 'px'
	  });
	}
	
	function enable_acc(name)
	{
	  if(!name){name = "acc_inner_text"};
	  $(name).hide();
	  
	  disabled_stack -= 1;
	  if(disabled_stack > 0)
	    return;
	    	  
		$('acc_disabler').hide();
		Event.stopObserving(window, 'resize', position_acc_disabler, false);
	}
	//selects first input field on page
	function select_form_element()
	{
		if(document.forms.length > 1){
			var form_elements = document.forms[1].getElementsByTagName('input');
			form_elements[0].focus();
		}				
	}
	//load user list functions
	function load_list(user_id, order_by, page, psize) {
	  	new Ajax.Updater('list-container', '/user_list/users_list/'+user_id, {
	    	parameters: { order: order_by, page: page, pagesize: psize},
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>'
	  	})
	}
	function load_my_list(user_id, order_by, page, psize) {
		new Ajax.Updater('list-container', '/user_list/my_wish_list/'+user_id, {
	  	parameters: { order: order_by, page: page, pagesize: psize},
	  	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>', 
	    evalScripts: true
		});
	}
	//user message AJAX functions
	function load_message(id){
		new Ajax.Updater('list-container', '/user/user_message', {
	    	parameters: { userid: id },
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>'
	  	})
	}
	//list reminder functions
	function load_reminder(id){
		new Ajax.Updater('list-container', '/user_list/create_list_reminder', {
	    	parameters: { userid: id },
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>'
	  	})
	}
	
	//load list AJAX functions
	function load_tag_list(tag, order_by, page, psize) {
	  	new Ajax.Updater('list-container', '/tag/tag_list/'+tag, {
	    	parameters: { order: order_by, page: page, pagesize: psize},
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Searching ...</span>'
	  	})
	}
	function load_search_list(item, order_by, page, psize) {
	  	new Ajax.Updater('list-container', '/product/search_list', {
	    	parameters: { order: order_by, q: item, page: page, pagesize: psize },
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Search ...</span>'
	  	})
	}
	//product email function
	function load_email_product(pid){
		new Ajax.Updater('email-div-'+pid, '/product/product_email_form/'+pid, {evalScripts: true,
		  onComplete: function(){toggle_form('email',pid)}	});
	}
	function send_product(id){
		new Ajax.Updater('email-div-'+id, '/product/send_product/'+id, {
	    	parameters: { emails: $F('emails-'+id), message: $F('message-'+id) },
	    	//onLoading: $('email-div-'+id).innerHTML = '<span class="loading">Loading ...</span>',
		onComplete: setTimeout("toggle_form('email', '"+ id +"')",3000)	
	  	})
	}
	
	//not sure where this is called from probably from my list
	function send_new_profile_from_list(id){
		new Ajax.Updater('list-container', '/group/send_new_profile', {
	    	parameters: { emails: $F('emails'), message: $F('message') },
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>',
		onComplete: setTimeout("load_my_list('"+ id +"', "+ null +")",3000)	
	  	})
	}

	//profile messages AJAX functions
	function load_new_profile_message(id, firsttime, container){
	  container = $(container || 'list-container');
		new Ajax.Updater(container, '/group/share_new_profile/'+id, {
	    	parameters: {ft: firsttime},
	    	evalScripts: true,
		    onLoading: function(){container.innerHTML = '<span class="loading">Loading ...</span>'}
	  	})
	}
	function load_new_profile_message_from_confirm(id){
		new Ajax.Updater('list-container', '/group/share_new_profile_from_confirm/'+id, {
	    	onLoading: function(){$('list-container').innerHTML = '<span class="loading">Loading ...</span>'}
	  	})
	}
	function send_new_profile_from_confirm(id){
		new Ajax.Updater('list-container', '/group/send_new_profile_from_confirm', {
		    evalScripts: true,
	    	parameters: { emails: $F('emails'), message: $F('message'), orig_message: $F('orig_message')},
	    	onLoading: function(){$('list-container').innerHTML = '<span class="loading">Loading ...</span>'},
		    onComplete: setTimeout("load_my_list('"+ id +"', null, 1, 0)",3000)	
	  	})
	}
	function send_new_profile(id, container){
	  container = $(container || 'list-container');
		new Ajax.Request('/group/send_new_profile', {
	    	parameters: { emails: $F('emails'), message: $F('message'), orig_message: $F('orig_message'),
	    	 container: container},
	    	onLoading: container.innerHTML = '<span class="loading">Loading ...</span>'
	  	})
	}	
	//cool green stuff list message	
	function load_es_message(listtype){
		new Ajax.Updater('list-container', '/group/share_es_list', {
	    	parameters: {list_type: listtype},
	    	evalScripts: true,
		    onLoading: function() {$('list-container').innerHTML = '<span class="loading">Loading ...</span>'}
	  	})
	}
	function send_es_message(listtype){
		new Ajax.Updater('list-container', '/group/send_es_list/'+listtype, {
	    	parameters: { emails: $F('emails'), message: $F('message') },
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>',
		onComplete: setTimeout("new Ajax.Request('/coolgreenstuff');",3000)	
	  	})
	}
	//tag list message 
	function load_tag_message(tagname){
		new Ajax.Updater('list-container', '/group/share_tag_list', {
	    	parameters: {tag: tagname},
	    	evalScripts: true,
		    onLoading: function(){$('list-container').innerHTML = '<span class="loading">Loading ...</span>'}
	  	})
	}
	function send_tag_message(tagname){
		new Ajax.Updater('list-container', '/group/send_tag_list', {
	    	parameters: { emails: $F('emails'), message: $F('message'), tag: tagname },
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>',
		onComplete: setTimeout("load_tag_list('"+ tagname +"','comment_count DESC', 1)",3000)	
	  	})
	}
	//other functions
	function load_arch_list(page) {
	  	new Ajax.Updater('list-container', '/user_list/archived_items_list', {
	    	parameters: {page: page },
	    	onLoading: $('list-container').innerHTML = '<span class="loading">Loading ...</span>'
	  	})
	}
	
	function add_friend_to_email(email, domid, checked){
		email_addr = $F(domid);
		if(checked) {
  		if(/(^|,\s*)$/.test(email_addr)){
  			$(domid).innerHTML +=  email;	
  		}else{
  			$(domid).innerHTML += ', ' + email;
  		}
	  } else {
	    $(domid).innerHTML = email_addr.replace(email, '').replace(/(\s*,\s*,)/, ',').replace(/^\s*,\s*|\s*,\s*$/,"");
	  }
	}
	
	function show_overlay(name, url) {
	  if($('overlay_' + name)) {
	    position_overlay(name);
	  } else {
	    new Ajax.Updater(document.body, url, {
	      evalScripts: true,
	      insertion: Insertion.Top,
	      onComplete: function(){ position_overlay(name) }
	    });
	  }
	}
	
	function position_overlay(name) {
	  disable_acc('overlay_' + name);
	  Position.centerInViewport('overlay_' + name);
	}
	