// JavaScript Document
jQuery.noConflict();
jQuery(document).ready(function(){
// Sets example text for login box and search form.
	jQuery('#loginBox input.username').example('');
	jQuery('#loginBox input.password').example('');  
//	jQuery('#ctl00_SearchField_SearchTerms').example(''); 
	jQuery('#googleSearch').example(''); 
	jQuery("#parentDrivenTabs").tabs({fx:{opacity:'toggle'}});

	jQuery("#contentItem1").tabs({fx:{opacity:'toggle'}});
	jQuery("#contentItem1").tabs("rotate",9400,false);
	jQuery("#contentItem1").hover(
		function() {jQuery("#contentItem1").tabs("rotate",0,false); },
		function() {jQuery("#contentItem1").tabs("rotate",9400,false); }
	);
	jQuery("#contentItem2").tabs({fx:{opacity:'toggle'}});
	jQuery("#contentItem2").tabs("rotate",9400,false);
	jQuery("#contentItem2").hover(
		function() {jQuery("#contentItem2").tabs("rotate",0,false); },
		function() {jQuery("#contentItem2").tabs("rotate",9400,false); }
	);
	jQuery("#parentDrivenPhoto").tabs({fx:{opacity:'toggle'}});
	jQuery("#parentDrivenPhoto").tabs("rotate",7000,false);
	jQuery("#parentDrivenPhoto").hover(
		function() {jQuery("#parentDrivenPhoto").tabs("rotate",0,false); },
		function() {jQuery("#parentDrivenPhoto").tabs("rotate",7000,false); }
	);
	jQuery("#t2PhotoHeadline").tabs();
	jQuery("#t2PhotoHeadline").tabs("rotate",7000,false);
	jQuery("#t2PhotoHeadline").hover(
		function() {jQuery("#t2PhotoHeadline").tabs("rotate",0,false); },
		function() {jQuery("#t2PhotoHeadline").tabs("rotate",7000,false); }
	);

// Overrides the functionality of hitting the enter key while in the Login Form so that it doesn't submit the Search form
	jQuery("#Password").keypress(function(event) {
		if (event.keyCode == '13') {
			event.preventDefault();
			loginForm();
		}
	});
	jQuery("#LoginSubmit").click(function() { loginForm(); });

// Sets the selected tab style based on the current URL. It will need to be changed if the base URL changes from /iMIS15/
	var pathSplit = window.location.pathname.split("/",3);
	var section = pathSplit[2];
	switch(section) {
		case 'Professional':jQuery("#quickProfessional").addClass('selected');break;
		case 'Clinical':jQuery("#quickProfessional").addClass('selected');break;
		case 'Trainee':jQuery("#quickTrainee").addClass('selected');break;
		case 'Student':jQuery("#quickTrainee").addClass('selected');break;
		case 'Associate':jQuery("#quickAssociate").addClass('selected');break;
		default:jQuery("#quickPublic").addClass('selected');break;
	}

// Adds rounded corner DIV tags to any DIV with the style of "roundedCorners"
	jQuery("div.roundedcorners").each(function(index,domEle){
		var hasChildren = jQuery(this).children("div.round_top").length;
		if (hasChildren == 0) {
			jQuery(this).append("<div class='round_top'></div><div class='round_left'></div><div class='round_right'></div><div class='round_btm'></div>");
			jQuery(this).append("<div class='round_tr'></div><div class='round_tl'></div><div class='round_br'></div><div class='round_bl'></div>");
		};
	});

//	jQuery("div.twocolumn").each(function(index,domEle){
//		var hasChildren = jQuery(this).children("div.contentItemTop").length;
//		if (hasChildren == 0) {
//			jQuery(this).append("<div class='round_top'></div><div class='round_left'></div><div class='round_right'></div><div class='round_btm'></div>");
//			jQuery(this).append("<div class='round_tr'></div><div class='round_tl'></div><div class='round_br'></div><div class='round_bl'></div>");
//		};
//	});

	jQuery('#searchSubmit').click(function(){
	// Create new form HTML element
		var searchform = document.createElement('form');
		searchform.setAttribute('name','googleSearch');
		searchform.setAttribute('method','get');
		searchform.setAttribute('action','http://search.aamft.org/search');
		searchform.style.display = 'none';
		jQuery('body').append(searchform);
	// Create hidden attributes
		var input1 = document.createElement('input');
		input1.setAttribute('name','site');
		input1.setAttribute('value','default_collection');
		searchform.appendChild(input1);
		var input2 = document.createElement('input');
		input2.setAttribute('name','proxystylesheet');
		input2.setAttribute('value','default_frontend');
		searchform.appendChild(input2);
		var input3 = document.createElement('input');
		input3.setAttribute('name','client');
		input3.setAttribute('value','default_frontend');
		searchform.appendChild(input3);
		var input4 = document.createElement('input');
		input4.setAttribute('name','output');
		input4.setAttribute('value','xml_no_dtd');
		searchform.appendChild(input4);
		var input5 = document.createElement('input');
		input5.setAttribute('name','q');
		var searchTerms = jQuery('#googleSearch').val();
		input5.setAttribute('value',searchTerms);
		searchform.appendChild(input5);
	// Append elements to form
		searchform.submit();
	});

	replaceUserID();
	checkLogin();
});

function get_cookie (name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function checkLogin () {
	var login = get_cookie("Login");
	if (login != null) {
		var firstName = get_cookie("firstName");
		var lastName = get_cookie("lastName");
		jQuery('#loginBox').html("<span class='loggedIn'>Welcome back " + firstName + " | <span onclick='Logout();' class='logout'>Logout</span>")		
	}
}
function loginForm() {
	jQuery("#loginBox").append("<div id='loginScreen' style='position:absolute; left:0px; top:0px; width:480px; height:30px; background:url(/iMIS15/App_Themes/AAMFT/images/80whitescreen.png) repeat center center; z-index:100; text-align:center; vertical-align:middle; font-size:16px; font-weight:bold;padding:10px 0px;'>Processing</div>");								  
	var userName = jQuery("#UserName").val();
	var Password = jQuery("#Password").val();
	var d = new Date();
	var paramList = "USER_ID="+userName+"&USER_Password="+Password+"&t="+d.getTime();
	jQuery.ajax({
		type: "POST",
		dataType:"xml",
		url: "/cgi-shl/TWServer.exe?Security:AuthorizeAjax:SIGNIN_1",
		data:paramList,
		success: function(data) {
			var xmlResponse = jQuery(data).find("authorizeResponse").first();
			var pass = jQuery(xmlResponse).find("pass").text();
			if (pass == 'True') {
				var firstName = jQuery(xmlResponse).find("firstName").text();
				var lastName = jQuery(xmlResponse).find("lastName").text();
				var memid = jQuery(xmlResponse).find("memid").text();
				var userid = jQuery(xmlResponse).find("userid").text();
				var login = jQuery(xmlResponse).find("imislogin").text();
				var memType = jQuery(xmlResponse).find("membertype").text();
				if (login == "") {
					alert("There is a problem with your login. Please contact AAMFT for assistance.");
					jQuery("#loginScreen").remove();
				}
				else {
					document.cookie="Login="+login+"; path=/; domain=.aamft.org; domain=.aamft.org;";
					document.cookie="SecurityLogin="+userid+"|"+Password+"|"+memid+"; path=/; domain=.aamft.org;";
					document.cookie="firstName="+firstName+"; path=/; domain=.aamft.org; domain=.aamft.org;";
					document.cookie="lastName="+lastName+"; path=/; domain=.aamft.org; domain=.aamft.org;";
					document.cookie="memType="+memType+"; path=/; domain=.aamft.org; domain=.aamft.org;";
					var sURL = unescape(window.location.pathname);
					checkLogin();
					if (memType == 'CM' || memType == 'MB' || memType == "AF" ) {
						window.location="/iMIS15/Professional/";
					}
					if (memType == 'AC' || memType == 'AM' || memType == "ST" ) {
						window.location="/iMIS15/Trainee/";
					}
				}
			}
			else {
				alert("Your login or password is incorrect. Please verify the information you have entered and try logging in again.");
				jQuery("#loginScreen").remove();
			}
		},
		error: function(jqXHR, status, error) {
			jQuery("#loginScreen").remove();
			alert("An error occurred during login. Please try your login again. If the error persists, please contact support@ccservices.com with the following error message. ("+status+")("+error+")");
		}
	});
}


function bookmark(){
	var title = "AAMFT"
	var url = top.location.href;
	if (window.sidebar) 
	    window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ 
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
	}
	else if(document.all)// ie
	    window.external.AddFavorite(url, title);
}

function replaceUserID() {
	var login = get_cookie("SecurityLogin");
	if (login != null) {
		var loginSplit = login.split('|');
		if (loginSplit.length == 3) {
			var id = loginSplit[2]; 
			jQuery('a.ApplyClass').each(function() {
				var oldhref = jQuery(this).attr('href')
				var newhref = oldhref.replace("#memid#","_1:PASSPOSTS:id="+id);
				jQuery(this).attr('href',newhref);
			});
	
		}
	}
}

function Logout() {
// This clears all cookies from the current domain
	var c=document.cookie.split(";");
	for(var i=0; i<c.length;i++){
		var e=c[i].indexOf("=");
		var n=e>-1?c[i].substr(0,e):c[i];
		document.cookie=n+"=;path=/;expires=Thu, 01 Jan 1970 00:00:00 GMT";
		document.cookie=n+"=;path=/;domain=.aamft.org;expires=Thu, 01 Jan 1970 00:00:00 GMT";
		document.cookie=n+"=;path=/;domain=workgroup.aamft.org;expires=Thu, 01 Jan 1970 00:00:00 GMT";
	}
	window.location="http://workgroup.aamft.org/27/logout.html";
}

