function compare(a,b) {
	if(a==b) return 0;
	var c=0;
	while(true) {
		if(a.charAt(c)>b.charAt(c)) return +1;
		else if(a.charAt(c)<b.charAt(c)) return -1;
		else{
			if(c==a.length-1) return -1;
			if(c==b.length-1) return +1;
		}
		c++;
	}
}
	

Array.prototype.unique = function() {
	var temp=new Array();
	temp=this.sort(compare);
	if(temp.length<2) return temp;
	
	result=new Array()
	result.push(temp[0]);
	for(var i=1;i<temp.length;i++) {
		if(temp[i]!=temp[i-1]) result.push(temp[i]);
	}
	return result;
};


Array.prototype.intersect = function(arg) {
	var a = this.unique();
	var b = arg.unique();
	var result = Array();
	// a and b are also now sorted
	for(var i=0;i<a.length;i++) {
		for(var j=0;j<b.length;j++) {
			if(a[i]==b[j]) {
				result.push(a[i]);
				break;
			}
		}
	}
	return result;
};


Array.prototype.difference = function(arg) {
	var a = this.unique();
	var b = arg.unique();
	var result = Array();
	// a and b are also now sorted
	for(var i=0;i<a.length;i++) {
		var p=true;
		for(var j=0;j<b.length;j++) {
			if(a[i]==b[j]) {
				p=false;
				break;
			}
		}
		if(p) result.push(a[i]);
	}
	return result;
};

Array.prototype.union = function(arg) {
	return this.concat(arg).unique();
};


String.prototype.trim = function() {
	return this.ltrim().rtrim();
}
 
String.prototype.ltrim = function() {
	return this.replace(new RegExp("^[\\s]+", "g"), "");
}
 
String.prototype.rtrim = function() {
	return this.replace(new RegExp("[\\s]+$", "g"), "");
}




function execute() {
		var inputa=document.getElementsByName('inputa')[0];
		var inputb=document.getElementsByName('inputb')[0];
		var output=document.getElementsByName('output')[0];
		var optionpreservecase=document.getElementsByName('preservecase')[0];
                var optionstripwhitespace=document.getElementsByName('stripwhitespace')[0];
                var optioncommatonewline=document.getElementsByName('commatonewline')[0];

		var f=document.getElementsByName('f')[0].value;

		var ainfo=document.getElementById('ainfo');
                var binfo=document.getElementById('binfo');
                var oinfo=document.getElementById('oinfo');


	
		var ta = inputa.value;
		var tb = inputb.value;
		
		var a=null;
		var b=null;


		if(optioncommatonewline.checked) {
			ta=ta.replace(/,/g,"\n");
                        tb=tb.replace(/,/g,"\n");
		}
		
		if(optionpreservecase.checked) {
			a = ta.split('\n');
			b = tb.split('\n');
		}
		else {
			a=ta.toLowerCase().split('\n');
			b=tb.toLowerCase().split('\n');		
		}		

		ainfo.innerHTML=a.length;
                binfo.innerHTML=b.length;


		if(optionstripwhitespace.checked) {
			for(var i=0;i<a.length;i++) a[i]=a[i].trim();
			for(var i=0;i<b.length;i++) b[i]=b[i].trim();
		}

		var temp=null;
		
		if(f=='sort') {
			temp=a.sort(compare);
		}
		else if(f=='unique') {
			temp=a.unique();
		}
		else if(f=='union'){
			temp=a.union(b);
		}
		else if(f=='intersect') {
			temp=a.intersect(b);
		}
		else if(f=='difference A-B') {
			temp=a.difference(b);
		}
		else if(f=='difference B-A') {
			temp=b.difference(a);
		}
		else temp=new Array();
		
                oinfo.innerHTML=temp.length;

		output.value=temp.join('\n');			


}


