function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
}

function selectItem(li) {
	findValue(li);
}

$(document).ready(function() {
	setCurrentBreadCrumb();
	if($(".col-719 .MJFutilNav li.send").length) sendToFriend();
	if ($(".col-719 #browse #browseEdu").length && $(".col-719 #browse #browseAlpha").length) {
		setCareers(getQueryVariable('browse'));
		setAlphaListing();
		setNOCListing(getQueryVariable('type'));	
		setEduListing();
	}
	if($(".col-719 .content #community").length) setCommunityListing();
	if($(".col-719 #sections").length) setProfileSection();
	if ($("#jobChartLevel").length && $("#jobChartSkill").length) browseJobChart();
	if($(".col-719 #areaShowRegion1").length) setAreaFacts();
	if($(".col-719 #communityFactsLink").length) setCommunityFacts();
	if($(".col-719 #IndSections").length) setIndustrySection();
	if($(".titles-full").length || $(".titles").length) title();
});

function titles(){
	$('.titles-full').show();
	$('.titles').hide();
	
}

function setCurrentBreadCrumb(){
	$("div#header a").click(
		function(){
			$("div#breadcrumbs").html($(this).attr('alt'))
			$("#crumbContent").val();
		}
	);
}

function getQueryVariable(qryVar) {  
	var query = window.location.search.substring(1);  
	var vars = query.split("&");  
	for (var i=0;i<vars.length;i++) {    
		var pair = vars[i].split("=");		
		if (pair[0] == qryVar) {   
			return pair[1];    
		}  
	} 
	return 0;
}

function setAlphaListing(){	
	$("div#alphaList > div").css({display: "none"});
	$("div#alpha a").click(
		function(){			
			$("#alphaList").show();
			$("div#alpha li").removeClass("selected");
			$(this).addClass('selected');
			$("div#alphaList > div").css({display: "none"});
			$("div#alphaList div." + $(this).attr('id')).show();
			return false;
		}
	);
}

function setCommunityListing(){	
	$("div#communityList div").css({display: "none"});
	$("div#community a").click(
		function(){
			$("div#community li").removeClass("selected");
			$(this).addClass('selected');
			$("div#communityList > div").css({display: "none"});
			$("div#communityList div." + $(this).attr('id')).show();
			return false;
		}
	);
}

function setNOCListing(selected){
	$("div#nocList > div").css({display: "none"});
	if (selected != ""){
		$(".noc"+selected).show();	
	}
	$("#noc a").click(
		function(){
			$("#nocList").show();
			$("div#noc li").removeClass("selected");
			$(this).addClass('selected');
			$("div#nocList > div").css({display: "none"});
			$("div#nocList div." + $(this).attr('id')).show();
			return false;
		}
	);
}

function setEduListing(){
	$("div#eduList > div").css({display: "none"});
	$("#edu a").click(
		function(){
			var thisId = $(this).attr('id');
			$(".Skill").remove();
			$(".edu1 form span").append('<input type="text" class="text Skill" id="Skill'+ thisId + '" />');
			var querySkillList = $("#list" + $(this).attr('id')).val();
			var qryArray = new Array();
			qryArray = querySkillList.split(';');	
			$("#Skill"+thisId).autocompleteArray(
				qryArray,
				{
					delay:10,
					minChars:1,
					matchSubset:1,
					onItemSelect:selectItem,
					onFindValue:findValue,
					width:400
				}
			);			
			$("#eduList").show();
			$("div#edu li").removeClass("selected");
			$(this).addClass('selected');
			$("div#eduList > div").css({display: "none"});
			$("div#eduList div." + $(this).attr('id')).show();
			return false;
		}
	);
}

function setProfileSection(){
	$("div#section > div").css({display: "none"});	
	$("div#sections li#profile0").addClass("selected");
	$("div#section div.profile0").show();
	$("#sections a").click(
		function(){
			$("#sections a").removeClass('selected');
			$(this).addClass('selected');
			$("div#section > div").css({display: "none"});
			$("div#section div." + $(this).attr('id')).show();
			return false;
		}
	);

}

function setIndustrySection(){
	$("div#indSection > div").css({display: "none"});
	$("div#IndSections li#indus1").addClass("selected");
	$("div#indSection div.indus1").show();
	$("#IndSections a").hover(
		function(){
			$(this).addClass('selected');
			$("div#indSection > div").css({display: "none"});
			$("div#indSection div." + $(this).attr('id')).show();
			return false;
		}
	);

}

function setCareers(selected){
	$(".browseEdu").hide();
	$(".browseNoc").hide();
	if (selected != 0){
		$(".browseAlpha").hide();
		$("."+selected).show();	
	}	
	$("#browse a").click(function(){		
		selected = $(this).attr('id');
		var qryNocList = $("#alphaStrings").val();
		var qryNocArray = new Array();
		qryNocArray = qryNocList.split(';');
		$(".Career").autocompleteArray(
			qryNocArray,
			{
			 delay:10,
			 minChars:1,
			 matchSubset:1,
			 onItemSelect:selectItem,
			 onFindValue:findValue,
			 width:400
			 }
		);
		$("#alphaList").hide();
		$("#eduList").hide();
		$("#nocList").hide();
		$(".browseEdu").hide();
		$(".browseNoc").hide();
		$(".browseAlpha").hide();
		$("."+selected).show();
		return false;
	})
}

function setAreaFacts(){
	$(".regions").hide();
	for (i=0;i<=5;i=i+1){
    	$("#areaShowRegion" + i).click(function(){
			$(".regions").hide();
			var v = $(this).attr('id').charAt(14)
			$("#region" + v).show();
			return false;
			
		})
	}
}


function setCommunityFacts(){	
	$("#communityFactsLink").click(function(){
		$("#communityFacts").show();
		$("#employerProfile").hide();
		return false;
	})
	
	$("#employerProfileLink").click(function(){
		$("#employerProfile").show();
		$("#communityFacts").hide();
		return false;
	})
}

function bl_OpenWindow(theURL, winName, features){
	window.open(theURL, winName, features);
}

function sendToFriend(){
	$("li.send a").click(function(){
		$(".emailFriendContainer").fadeIn();
		return false;
	})
	
	$(".emailFriendClose").click(function(){
		$(".emailFriendContainer").fadeOut();
		return false;
	})
	
	$("#emailFriend #btnSend").click(function(){
		emailFriend();
		return false;
	})
}


function emailFriend(){
	if($("#txtFromName").val() == ''){
    	if($("#emailFriend form").attr('class') == 'fr'){alert("Votre nom est exigé !")}else{alert("Your name is required!")} 
	}else if(echeck($("#txtFromEmail").val()) != true){
    	if($("#emailFriend form").attr('class') == 'fr'){alert("Votre adresse e-mail est invalide!")}else{alert("Your email address is invalid!")}
	}else if($("#txtToName").val() == ''){
    	if($("#emailFriend form").attr('class') == 'fr'){alert("Votre friend' ; le nom de s est exigé !")}else{alert("Your friend's name is required!")}
  	}else if(echeck($("#txtToEmail").val()) != true){
		if($("#emailFriend form").attr('class') == 'fr'){alert("L'adresse email de votre ami n'est pas valide!")}else{alert("Your friend's email address is invalid!")}	 	
	}else{
		$("img#loading").show();
		{
		$.ajax({
			type: "POST",
			url: "/index.cfm?event=action.sendToFriend",
			data: $("#emailFriend form").serialize(),
			success: function(data){
				$("img#loading").hide();
				$("#emailFriend .error").show();
			}
		});
	}}
}

function browseJobChart(){
	$("#jobChartLevel").click(function(){
		$(".jobChartSkill").hide();
		$(".jobChartLevel").show();
		return false;
	})
	$("#jobChartSkill").click(function(){
		$(".jobChartSkill").show();
		$(".jobChartLevel").hide();
		return false;
	})
	
	var qryJobCharts = $("#JobCharts").val();
	var qryJobChartsArray = new Array();
	qryJobChartsArray = qryJobCharts.split(';');	
	$("#JobChartText").autocompleteArray(
	qryJobChartsArray,
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			onItemSelect:selectItem,
			onFindValue:findValue,
			width:400
		}
	);
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}

