/*
Koppers, Inc.
Career Section Javascript
*/

$(document).ready(function(){
	//Attach tooltip to <abbr> tags - displays the title attribute
	$('abbr').Tooltip({ 
	    track: false, 
	    delay: 0, 
	    showURL: false, 
	    showBody: " - ", 
	    extraClass: "pretty", 
	    fixPNG: true, 
	    opacity: 1,
		top: -78,
	    left: 0 
	});
	
	//Modal dialog - results page
	$('#dialog').jqm();
	//Modal - Search agent
	$('#dialog_search').jqm();
	//Modal - forgot password
	$('#dialog_forgot').jqm();
	//Modal - Apply to Job
	$('#dialog_apply').jqm({
	    trigger: '#openApply', opacity: 30});
	//Modal - Send to friend
	$('#dialog_friend').jqm({
	    trigger: '#openFriend'});
	
	//Show Modal if errors
	if($('div.message').length){
		if(($('div.message').text()).search("application") > 0){
			$('#dialog_apply').jqmShow();
		} else if(($('div.message').text()).search("friend") > 0) {
			$('#dialog_friend').jqmShow();
		} else {
			$('#dialog').jqmShow();
		}
	};
	
	//Resume Show/Hide
	$('#showResume').click(function(){
		$('#resumeinfo').toggle();
	});
	$('#hideResume').click(function(){
		$('#resumeinfo').hide();
	});
	
	// Manipulate and attach behavior to Cake flash messages.
	$("div.message").click(function(){
		$(this).fadeOut(200);
	}).css("cursor", "pointer").attr("title", "Click here to close this message.").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});
	var flash_duration = 4000;
	//Hide Flash Messages
	setTimeout('hideMessages()', flash_duration);
	
	//If an error scroll to the error
	if(error==true){
		$.scrollTo('#login', 1000);
	};
	
	var word_match_count = 0;
	var has_words = false;
	
	$("#account_submit").click(function(){
		if(!old_account || $('#AccountPw').val() != ''){
			if(!passwordTest()){
				$("#password_results").text('Please enter a stronger password').addClass('error');
				return false;
			}else{
				$("#password_results").text('Testing Password');
				word_matches = $('#AccountPw').val().match(/\D+/g);
				word_match_count = word_matches.length;
				for(var i=0;i<word_match_count;i++){
					$.post("/careers/words/checkWord", { word: word_matches[i]},
					  function(data){
						word_match_count--;
						if(data == 1){
							has_words = true;
						}
						if(word_match_count == 0 && has_words){
							$("#password_results").text('Please remove dictionary words from your password');
							has_words = false;
						}else{
							$("#account_submit").unbind('click');
							$("#account_submit").click();
						}
					  });			
				}
			}
			return false;
		}else{
			return true;
		}
	});	
});

function hideMessages() {
	$("div.message").filter(function(){
		return $(this).css("display") != "none"; // don't do the fade action if it's already hidden!
	})
	// .hide();
	.animate({top: '-=75px'}, 'normal', 'swing', function(){$(this).remove();});
}


function passwordTest() {
	var strongRegex = new RegExp("^(?=.{7,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
	var mediumRegex = new RegExp("^(?=.{6,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
	var enoughRegex = new RegExp("(?=.{5,}).*", "g");
	pwd = $('#AccountPw');
	if (pwd.val().length<5) {
		return false;
	} else if (false == enoughRegex.test(pwd.val())) {
		return false;
	} else if (strongRegex.test(pwd.val())) {
		return true;
	} else if (mediumRegex.test(pwd.val())) {
		return true;
	} else { 
		return false;
	}
}