/*login validation (legacy)*/
function trim(txtInput) {
	txtInput=txtInput.replace(/^[\s]+/,'');
	txtInput=txtInput.replace(/[\s]+$/,'');
	return txtInput;
}

function checkForm() { 
	document.loginForm.userName.value=trim(document.loginForm.userName.value);
	document.loginForm.password.value=trim(document.loginForm.password.value);
	var username=document.loginForm.userName.value;
	var password=document.loginForm.password.value;
	var splCharacter = '(\" \' ~).';
	if(emptyFieldValidation(username)) { 
	    alert("Email cannot be left blank."); 
	    document.loginForm.userName.focus(); 
	    return false; 
	} 
	
	if(!validateEmailWithLDAPCheck(username, "true")){
		alert("Enter your full email address, including everything after the \"@\" symbol.\nAlso, the address can't include unconventional special characters or whitespace.");
		document.loginForm.userName.focus(); 
		return false; 
	}
	      
	 if( password == ""){
	        alert("Password cannot be blank."); 
	        document.loginForm.password.focus(); 
	        return false; 
	 }     

 	/*if( password.length <6) { 
        alert("Error: Password must contain at least six characters!"); 
        document.loginForm.password.focus(); 
        return false; 
    }*/

	return true;
 }
 
/*vertical aligner*/
$.fn.vertAlign = function() {
	if (this.length > 0) {
		$parent = (arguments.length > 0) ? $(arguments[0]) : this.parent();
		var topShift = ( $parent.innerHeight() - this.outerHeight() ) / 2;
		topShift = topShift > 0 ? topShift + "px" : 0;
		this.css("marginTop",topShift);	
	}
}

/*Tip Positioning - req: dimensions.js*/
function setTipPos(trig,tip, offX, offY) {
	var trigOffset=$(trig).offset(trigOffset);
	//var parOffset=$($(tip).offsetParent()).offset(parOffset);
	var winH=$(window).height();
	var winW=$(window).width();
	var tipLeft=trigOffset.left + trig.offsetWidth + offX;
	var tipTop=trigOffset.top + offY;
	
	tipLeft = (tipLeft + tip.offsetWidth - $(window).scrollLeft() > winW) ? 
		/*
		trigOffset.left - tip.offsetWidth - offX - parOffset.left
		: tipLeft - parOffset.left;
		*/
		trigOffset.left - tip.offsetWidth - offX
		: tipLeft;
		
	tipTop = (tipTop + tip.offsetHeight - $(window).scrollTop() > winH) ? 
		/*
		trigOffset.top - tip.offsetHeight - offY - parOffset.top
		: tipTop - parOffset.top;
		*/
		trigOffset.top - tip.offsetHeight - offY
		: tipTop;
		
	tip.style.left = tipLeft + "px";
	tip.style.top = tipTop + "px";
}

$(document).ready(function(){	

	//InfoTip functions
	if ( $("a.infoLink").length > 0 ) {
		//create tip modal
		var $tipModal = $("<div id='tipModal'></div>").appendTo("body");
		$tipModal.html("<div class='tipClose'><a href='#' class='close' title='Close'>Close</a></div><div class='tipHdr'><h3></h3></div><div class='tipBody'></div>");
		$tipModal.find("a.close").click(function(){
			$tipModal.css("left","-9999px");
			$("div.overlayLight").remove();
			return false;
		});
		
		$("a.infoLink").click(function(){
			var overlayH = ( $(window).height() > $(document).height() ) ? $(window).height() : $(document).height();
			$("<div class='overlayLight'></div>").appendTo("body").css("height", overlayH + "px").click(function(){
				$tipModal.css("left","-9999px");
				$("div.overlayLight").remove();
			});
			var $link = $(this);
			$tipModal.css("left","-9999px");
			$tipModal.find("h3").html($link.html());
			$tipModal.find(".tipBody").html( $( $link.attr("href") ).html() );
			setTipPos($link.get(0), $tipModal.get(0), 10, 10);
			return false;
		});		
	}
	
	//Lightbox functions
	if ( $("a.lightbox").length > 0 ) {
		function setLightBox(){
			var modTop=( $(window).height() - $lightbox.height() ) / 2 + $(window).scrollTop();
			var modShift= $lightbox.width() / -2;
			modTop = modTop < 0 ? 0 : modTop;
			$lightbox.css({top:modTop+"px",left:"50%",marginLeft:modShift});
		}
		
		//preload
		$("a.lightbox").each(function(i){
			$("<img />").attr("src",$(this).attr("href"));
		});
		
		//create lightbox (prepopulate with 1st image)
		var $lightbox = $("<div id='lightbox'></div>").appendTo("body");
		$lightbox.html("<div id='lightboxImg'><img /></div><a class='lightboxClose' href='#' title='Close'>Close</a>");
		$("#lightboxImg img").attr("src",$("a.lightbox").eq(0).attr("href"));
		$lightbox.find("a.lightboxClose").click(function(){
			$lightbox.hide();
			$("div.overlay").remove();
			return false;
		});
		
		$("a.lightbox").click(function(){
			var overlayH = ( $(window).height() > $(document).height() ) ? $(window).height() : $(document).height();
			$("<div class='overlay'></div>").appendTo("body").css("height", overlayH + "px").click(function(){
				$lightbox.hide();
				$("div.overlay").remove();
			});
			//populate and show lightbox
			$('#lightboxImg').html("<img src='" + $(this).attr("href") + "'/>");
			$('#lightboxImg img').load(function(){
				setLightBox();
			});
			
			$lightbox.show();
			setLightBox();
		
			return false;
		});
	}
	
	//vertical align text branding;
	$("#brandingHTML").vertAlign();
	
	//set copyright date
	if ($('#writeyear').length > 0){
		var date = new Date();
		$('#writeyear').text(date.getFullYear() + " ");
	}
	
	//login form initialize
	$("input#userName").focus();

	//login page sniffer initialize
	if ($("#hdrLogin").length > 0 || $("#pageMainContainer").length > 0) {
		if (typeof sniffer != "undefined") {
			var cookieVal=getCookie("ESCAPE_UNSUPPORTED_BROWSER");
			var landingPass = false;
			if(cookieVal == "true") {
				landingPass = sniffer.cookie().pass;
				var url = document.location.href;
				var logout = url.indexOf('logout=true');
				var sessionTimeOut = url.indexOf('isSessionTimeout=true');
				if(logout != -1 || sessionTimeOut != -1) {
					deleteCookie("ESCAPE_UNSUPPORTED_BROWSER");
				}
			} else {
				landingPass = (sniffer.browser().pass && sniffer.cookie().pass);
			}
			if (!landingPass) {
				document.location.replace('/connect/shortUrl.do?accessUrl=' + accessUrl + '&autoSniffer=true');
			} else {
				$("#hdrLogin").css("display","block");
			}
		}
		else {
			$("#hdrLogin").css("display","block");
		}
	}
	
	//troubleshooting pop up
	$('a.troubleshoot').click(function() {
		var popupUrl = $(this).attr("href");
		var newWin=window.open(popupUrl,"troubleshoot","width=900,height=480,menubar=yes,scrollbars,resizable");
		try {
			newWin.focus();
			return false;
		}
		catch (e){
			return true
		}
	});

	var dName = document.domain;
	if ((dName.indexOf(".com") > -1) && (dName.indexOf(".") > -1)) {
		var dNameArr = dName.split(".");
		if(dNameArr.length>1)
		dName = dNameArr[dNameArr.length-2]+"."+dNameArr[dNameArr.length-1];
		try {
			document.domain = dName;
		}
		catch(err) {
		}
	}
});

function displayToolTip(linkref,bodyId,contentId,tooltipTxt){	
	var reloadIEFlag = false;
	try{
		if(!$(linkref).hasClass('tooltipAdded')){
			reloadIEFlag = true;
			$(linkref).tooltip({
				top:-20,
				left:-280,
				track: true,
				showBody: " - ",
				showURL: false,
				extraClass: "fancy",
				fixPNG: true
			 }).addClass('tooltipAdded');
			contenttooltip=$("#"+contentId).html();	
			$("#"+bodyId+" .headingtext").html(tooltipTxt);
			$("#"+bodyId+" .bodydiv").html(contenttooltip);
			$('#tooltip').html($("#"+bodyId));
			$(linkref).trigger('mouseover');
		} else{
			contenttooltip=$("#"+contentId).html();	
			$("#"+bodyId+" .headingtext").html(tooltipTxt);
			$("#"+bodyId+" .bodydiv").html(contenttooltip);
			$('#tooltip').html($("#"+bodyId));
		}
	} catch(e) {
		if($.browser.msie && reloadIEFlag){
			reloadIEFlag = false;
			$(linkref).attr('title','');
			$(linkref).trigger('mouseover');
		}
	}
}