﻿function FindAndCount( str, pattern ){
    var rex = new RegExp( pattern );
    var found = str.match( rex );

    if (found == null) {
        return 0;
    } else {
        return found.length;
    }
}

function IsStrong( span, args ){
	var hasCap, hasLow, hasNum, hasSym;
	var isSpan = span.nodeName == 'SPAN';

	if( isSpan ){
		input = document.getElementById( span.controltovalidate );
	}else{
		input = span;
	}

	var cell = document.getElementById( 'strength' );
	var pw = input.value;
	
	var score = pw.length >= 8 ? 1 : 0;

	if (pw.length > 12) {
	    score++;
	}

	if( FindAndCount( pw, '[A-Z]{1}' ) ){
		hasCap = true;
		score++;
	}

	if (FindAndCount(pw, '[a-z]{1}')) {
		hasLow = true;
		score++;
	}

	if (FindAndCount(pw, '\\d{1}')) {
	    hasNum = true;
	    score++;
	}
	
	switch( score ){
	    case 0:
	    case 1:
	        cell.style.borderTopColor = 'Red';
	        break;
	    case 2:
	        cell.style.borderTopColor = 'Orange';
	        break;
	    case 3:
	        cell.style.borderTopColor = 'Yellow';
	        break;
	    case 4:
	        cell.style.borderTopColor = 'DodgerBlue';
	        break;
	    default:
	        cell.style.borderTopColor = 'Green';
	        break;
	}

	if( score < 4 ){
		if( isSpan ){
			args.IsValid = false;
		
			var msg = '';
			if( pw.length < 8 ){
				msg = 'Your password must be at least 8 characters.';
			}else{
				var reasons = [];	
				if( !hasCap )
					reasons.push( ' capital letters' );

				if( !hasLow )
					reasons.push( ' lower-case letters' );

				if( !hasNum )
					reasons.push( ' numbers' );

				msg = 'Your password is not considered strong enough.  Please consider adding '+ reasons +'.'
			}
			
			span.errormessage = msg;
			span.title = msg;
			
			if( span.textContent ){
				span.textContent = msg;
			}else{
				span.innerText = msg;
			}
		}
		return false;
	}else{
		return true;
	}
}

function popup( url ){
	window.open( url, 'feature', 'height=400,location=0,menubar=0,toolbar=0,width=400', 1 );
}

function toggle( link ){
	var fs = link.parentNode;
	//find the fieldset
	while( fs.nodeName != 'FIELDSET' ){
		fs = fs.parentNode;
	}

	var img = link.firstChild;
	//find the fieldset
	while( img.nodeName != 'IMG' ){
		img = img.nextSibling;
	}


	var tables = fs.getElementsByTagName( 'table' );
	if( tables && tables.length ){
		if( tables[0].style ){
			if( tables[0].style.display == 'none' ){
				tables[0].style.display = '';
				img.src = img.src.replace( 'plus', 'minus' );
			}else{
				tables[0].style.display = 'none';
				img.src = img.src.replace( 'minus', 'plus' );
			}
		}else{
			tables[0].style.display = '';
			img.src = img.src.replace( 'minus', 'plus' );
		}
	}
}

function toggle2(link) {
    var fs = link.parentNode;
    //find the fieldset
    while (fs.nodeName != 'FIELDSET') {
        fs = fs.parentNode;
    }

    var img = link.firstChild;
    //find the fieldset
    while (img.nodeName != 'IMG') {
        img = img.nextSibling;
    }


    var tables = fs.getElementsByTagName('div');
    if (tables && tables.length) {
        if (tables[0].style) {
            if (tables[0].style.display == 'none') {
                tables[0].style.display = '';
                img.src = img.src.replace('plus', 'minus');
            } else {
                tables[0].style.display = 'none';
                img.src = img.src.replace('minus', 'plus');
            }
        } else {
            tables[0].style.display = '';
            img.src = img.src.replace('minus', 'plus');
        }
    }
}