function MarkedRecordsObject() {
	
	/**
	 * Language index
	 */
	this.lindex = 0;
	
	/**
	 * Mark a document.
	 */
	this.mark = function(uri, type) {
		var url = "";
		
		if (type == 'add'){
			url = '/session/session-uris.html?type=add&uri='+ uri;
		}
		else if (type == 'bulk-add'){
			url = '/session/session-uris.html?type=bulk-add&uri='+ uri;
		}
		
		new Ajax.Request(url, { method: 'get', onSuccess: this.markHandler.bind(this) });
		
		return false;
	};
	
	this.markHandler = function(transport){
		var count = transport.responseText;
		this.showAccount(count, 'mark');
	};

	
	/**
	 * Unmark a document
	 */
	this.unmark = function(uri, type){
		var url = "/session/session-uris.html?uri=" + uri;
		
		if (type == 'remove'){ url = url + "&type=remove"; }
		else if (type == 'bulk-remove'){ url = url + "&type=bulk-remove"; }
		 
		new Ajax.Request(url, { method: 'get', onSuccess: this.unmarkHandler.bind(this) });
		return false;
	}
	
	
	this.unmarkHandler = function(transport) {
		var count = transport.responseText;
		this.showAccount(count, 'unmark')
	}
	
	/**
	 * Check if document is in marked list
	 */
	this.lookup = function(uri){
		var url ="/session/session-uris.html?type=lookup&uri=" + uri;
		new Ajax.Request(url, { method: 'get', onSuccess: this.lookupHandler.bind(this) });
		
	};
	
	
	this.lookupHandler = function(transport){
		var check = transport.responseText;
		this.updateMarkLink(check);
	};
	
	
	/**
	 * Check if document is in marked list, then display appropriate text for details page
	 */
	this.toggleMark = function(uri, lang){
		if (lang == "fr"){ this.lindex = 1; } else { this.lindex = 0; }
		
		var url ="/session/session-uris.html?type=lookup&uri=" + uri;
		new Ajax.Request(url, { method: 'get', onSuccess: function(transport){
				var check = transport.responseText;
				if (check == 'false') { MarkedRecords.mark(uri,'add'); }
				else { MarkedRecords.unmark(uri,'remove'); }
			}
		});
		
	};
	
	/**
	 * Initialize for search results page
	 */
	this.init = function() {
		var url = '/session/session-uris.html?type=list';
		new Ajax.Request(url, { method: 'get', onSuccess: this.initHandler.bind(this) });
	};
	
	
	this.initHandler = function(transport){
		var response = transport.responseText.split("\n");	
		if (response.length > 0) {
			this.highlight(response);
		}
	};
	
	
	/***************************************************************************
	 * UI methods
	 */
	
	this.highlight = function(uris){
		$$('[name="chk"]').each(function(e) { 
			for (var j = 0; j < uris.length; j++){
				if (e.value.toString() == uris[j].toString()){
					e.checked = 1;
					break;
				}
			}
		}); 
	};
	
	/**
	 * Update Mark/Clear option in details page
	 */
	this.updateMarkLink = function(check){
		if (check == 'false'){ $('marktext').innerHTML = " Mark Record"; }
		else { $('marktext').innerHTML = " Clear Marked Record"; }
	};
	
	/**
	 * Display or hide My List and total
	 */
	this.showAccount = function(count, state){
		
		if (state == 'mark') {
			$('myaccount').show(); 
			$('myaccount-a').update(langmap['list'][this.lindex] +' (' + count + ')'); 
			if ($('marktext')){ $('marktext').innerHTML = " Clear Marked Record"; }
			
		} else if (state = 'unmark'){
			
			if (count == 'empty'){
				$('myaccount').hide(); 
			} else {
				$('myaccount-a').update(langmap['list'][this.lindex] +' (' + count + ')'); 
			}
			
			var current = window.location.toString();
			if(current.substring((current.length - 17),current.length) == 'marked/index.html'){
				window.location.reload(true);
			}
			
			if ($('marktext')){ $('marktext').innerHTML = " Mark Record"; }
		}
		
	};
	
	/**
	 * Add selected records to marked list in search results page
	 */
	this.addToList = function(lang){
		var uris = "";
		var chkBoxes = $$('[name="chk"]');
		
		chkBoxes.each (function(item){
			if (item.checked){
				if(uris.length > 0){ uris += ","; }
				uris += item.value;
			}
		});

		if (lang == "fr"){ this.lindex = 1; } else { this.lindex = 0; }
		
		if (uris.length > 0) { this.mark(uris, 'bulk-add');} else { alert(getErrorMsg(lang))}
		
	};
	
	/**
	 * Remove selected records from marked records page
	 */
	this.removeMarked = function(lang){
		var uris = "";
		
		if (lang == "fr"){ this.lindex = 1; } else { this.lindex = 0; }
		
		var chkBoxes = $$('[name="chk"]');
		
		chkBoxes.each (function(item){
			if (item.checked){
				if(uris.length > 0){ uris += ","; }
				uris += item.value;
			}
		});
		
		if (uris.length > 0){
			this.unmark(uris, 'bulk-remove');
		} else { alert(getErrorMsg(lang));	}
		
	};
	
	/**
	 * Send uris to refworks, endnote web etc
	 */
	this.checkedUrisToQuery = function(target_url, window_name,lang){
		var uris = "";
		
		if (lang == "fr"){ this.lindex = 1; } else { this.lindex = 0; }
		
		var chkBoxes = $$('[name="chk"]');
		chkBoxes.each (function(item)
				{
					if (item.checked){
						uris += item.value + ",";
					}
				}
		);
		
		uris = uris.substring(0, (uris.length-1)); //strip last comma

		if (uris.length > 0 && target_url.length > 0){
			var url = target_url + uris;
			window.open(url, window_name);
		}
		else if (uris.length > 0 && target_url.length <= 0){ return uris; }
		else { alert(getErrorMsg(lang)); } //user has not marked any survey check boxes
		
	};
	
	/**
	 * Emailed marked records
	 */
	this.validateAndEmail = function(){
	    if(this.emailValidator('email-form','to')) 
	    {
	    	var uris = this.checkedUrisToQuery('','email');
	    	var to_email = $F('to');

	    	new Ajax.Request('/marked/email-marked.html',{
		    	method:'post', 
		    	parameters: {uris: uris, to: to_email,asynchronous: true},
		    	onSuccess: function(transport){ //alert(transport.responseText); 
		    	}},
		    	true);
	    	
	    	Effect.SlideUp('email');
	    	return false;
	    }
	    else
	    {
	    	$('email-msg').innerHTML="<b style='color: red'>Please enter a valid email</b>";
	    	return false;
	    }
	};
	
	
	/**
	 * Validates email address
	 */
	this.emailValidator = function(form_id,email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   var address = $F(email);
	   
	   if(address.length > 0){ if(reg.test(address) == false) { return false; } }
	   else { return false; }
	   
	   return true;
	};
	
	
};

var MarkedRecords = new MarkedRecordsObject();