$(document).ready(function() {

	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) {
		}
	}

	$(".requestDemo").hide();
	/*$(".emailFld").each(function() {
		if($(this).val()!="") {
			$(this).removeClass("emailImg");
		}
	});
	$(".passwordFld").each(function() {
		if($(this).val()!="") {
			$(this).removeClass("passwordImg");
		}
	});*/
    $("#submitBtn,#submitBtnErr").click(function() {
    	var userName = $.trim($(this).parent().parent().find(".emailFld").val());
    	var password = $.trim($(this).parent().parent().find(".passwordFld").val());
    	$(this).parent().parent().find(".emailFld").val(userName);
    	$(this).parent().parent().find(".passwordFld").val(password);
    	$(".emailFld").val(userName)
    	$(".passwordFld").val(password);
        if (userName == "") {
			displayLoginErrorBlock("Please enter your email address.");
	    	$("#utlErrs .emailFld").focus();
            return false;
        } else if(!validateEmailWithLDAPCheck(userName, "true")) {
            displayLoginErrorBlock("Enter your full email address, including everything after the \"@\" symbol.\nAlso, the address can't include unconventional special characters or whitespace.");
            $("#utlErrs .emailFld").focus();
			return false;
        } else if(password == "") {
            displayLoginErrorBlock("Please enter your password.");
	    	$("#utlErrs .passwordFld").focus();
            return false;
        } else {
        	eraseCookie("sysMsg");
            /* AJAX call to server side validation goes here */
            document.loginForm.userName.value =userName;
            document.loginForm.password.value = password;
            document.loginForm.submit();
            return false;
        }
    });
    $("#loginContainer .utlLinks").attr('onclick','');
    $(".utlInnerCntr .utlErrsClose").addClass("utlCloseIcon");
    $("#loginContainer .utlLinks").click(function() {
    	$(".utlInstruction").css("display","block");
		$(".utlInnerMsg").css('background','none');
		$(".utlInnerMsgBtm, #utilErrMsg").hide();
		$(".utlInstruction").css('padding-top','0px');
		$(".utlInstruction p:first").text("TROUBLE SIGNING IN? TRY THE FOLLOWING:");
		$(".utlInnerCntr .utlErrsClose").addClass("utlCloseIcon");
		$("#utlUsrCntr span.emailLbl").css('color','#CC0033');
		$("#utlPwdCntr span.passwordLbl").css('color','#CC0033');
		$("#utlErrs").show();
    });
    $(".utlInnerCntr .utlErrsClose").removeClass("utlCloseIcon");
	/*$(".emailFld").click(function(){
		if ($(this).hasClass("emailImg")) {
			$(this).removeClass("emailImg");
			$(this).val('');
		}
	}).focus(function(){
		if ( $(this).hasClass("emailImg")) {
			$(this).removeClass("emailImg");
		}
	}).blur(function(){
		if ( $(this).val() == "" ) {
			$(this).addClass("emailImg");
		} else {
			$(this).removeClass("emailImg");
		}
		return false;
	});
	$(".passwordFld").click(function(){
		if ($(this).hasClass("passwordImg")) {
			$(this).removeClass("passwordImg");
			$(this).val('');
		}
	}).focus(function(){
		if ( $(this).hasClass("passwordImg")) {
			$(this).removeClass("passwordImg");
		}
	}).blur(function(){
		if ( $(this).val() == "" ) {
			$(this).addClass("passwordImg");
		} else {
			$(this).removeClass("passwordImg");
		}
		return false;
	});*/
    $(".utlErrsClose").click(function() {
		$("#utlErrs").hide();
    });
    $(".emailFld").attr("maxlength","50");
    $(".passwordFld").attr("maxlength","20");
   	if($("#utlErrs").css("display")=="none")
    $("#usrCont .emailFld").focus();
    else
    $("#utlUsrCntr .emailFld").focus();
   	$(".compositionURL").attr({
   		//target: "_blank",
        href: "http://mhconnectcomposition.com"
      });
   	$(".lucasURL").attr({
   		//target: "_blank",
        href: "http://connectlucas.com"
   	});
   	$(".writingURL").attr({
   		//target: "_blank",
        href: "http://mhconnectwriting.com"
   	});
});

function displayLoginErrorBlock(errorMessage) {
	$(".utlInnerMsg").css('background-color','#F2B2B3');
	$(".utlInnerMsgBtm, #utilErrMsg").show();
	$(".utlInstruction").css('padding-top','25px');
	$(".utlInstruction p:first").text("YOU CAN ALSO TRY THE FOLLOWING:");
	$(".utlInnerCntr .utlErrsClose").removeClass("utlCloseIcon");
	var offset = $("#submitBtn").offset();
	//$("#utlErrs").css({"top": $("#loginContainer").offset().top,"left":$("#loginContainer").offset().left-4 });
	$(".utlInstruction").show();
	$("#utilErrMsg").text("");
	$("#utilErrMsg").text(errorMessage);
	$("#utlUsrCntr span.emailLbl").css('color','#9A2A0A');
	$("#utlPwdCntr span.passwordLbl").css('color','#9A2A0A');
	$("#utlErrs").show();
}