function similar_text (first, second) {
    // Calculates the similarity between two strings  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/similar_text
    // +   original by: Rafał Kukawski (http://blog.kukawski.pl)
    // +   bugfixed by: Chris McMacken
    // *     example 1: similar_text('Hello World!', 'Hello phpjs!');
    // *     returns 1: 7
    // *     example 2: similar_text('Hello World!', null);
    // *     returns 2: 0
    if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined') {
        return 0;
    }
 
    first += '';
    second += '';
 
    var pos1 = 0,
        pos2 = 0,
        max = 0,
        firstLength = first.length,
        secondLength = second.length,
        p, q, l, sum;
 
    max = 0;
 
    for (p = 0; p < firstLength; p++) {
        for (q = 0; q < secondLength; q++) {
            for (l = 0;
            (p + l < firstLength) && (q + l < secondLength) && (first.charAt(p + l) === second.charAt(q + l)); l++);
            if (l > max) {
                max = l;
                pos1 = p;
                pos2 = q;
            }
        }
    }
 
    sum = max;
 
    if (sum) {
        if (pos1 && pos2) {
            sum += this.similar_text(first.substr(0, pos2), second.substr(0, pos2));
        }
 
        if ((pos1 + max < firstLength) && (pos2 + max < secondLength)) {
            sum += this.similar_text(first.substr(pos1 + max, firstLength - pos1 - max), second.substr(pos2 + max, secondLength - pos2 - max));
        }
    }
 
    return sum;
}

function updateEmail( correct ) 
{
	$("#email").val( correct );
	$("#CorrectEmail").slideUp(500).html('');
}

function replaceStringWithSimilar( elem, type )
{
	// requires: jQuery 
	// splits elem.value with @ and searches for correct domains

	var string = $(elem).val();
	var corrects = new Array;
	var results = new Array;

	if(type == 'email') 
	{	
		string = string.replace(/ /g,"");
		string = string.replace (/\,/g, '.');
		string = string.replace('(at)','@');
		string = string.replace('(a)','@');

		corrects.push('gmail.com');
		corrects.push('hotmail.com');
		corrects.push('luukku.com');
		corrects.push('yahoo.com');
		corrects.push('yahoo.co.uk');
		corrects.push('hotmail.co.uk');

		var stringsplit = string.split('@');
		str = stringsplit[1];
	}

	var exact = corrects.indexOf( str );
	if(exact >= 0)
	{
		$(elem).val(string);
		$("#CorrectEmail").slideUp(500).html('');
		return;
	}

	var highest = 0;
	var highestsimsum = 0;
	for(var x in corrects)
	{
		var simsum = similar_text( str, corrects[x] );
		if( simsum/corrects[x].length > highestsimsum)
		{	
			highestsimsum = simsum/corrects[x].length;
			highest = x;
		}	
	}
	if(highestsimsum > 0.6) 
	{
		$("#CorrectEmail").html("<div style='background-color:#c0c0c0' onclick=\"updateEmail('" + stringsplit[0] + '@' + corrects[highest] + "');\"> klikkaa tästä jos tarkoitit " + stringsplit[0] + "@" + corrects[highest] + " ?</div>" );
		$("#CorrectEmail").slideDown(500).css('color','#fff');
		return;
	}
	$(elem).val(string);
}

