$(document).ready(function() {
	jQuery.validator.messages.required = "";
	$("#loginForm").validate({
		onkeyup: false,
		submitHandler: function() {
			$("div.error").hide();
			checkLogin();
			return false;
		},
		messages: {
			loginEmail: {
				required: "Please enter your email.",
				email: "Please enter a valid email address."
			},
			loginPassword: {
				required: "Please enter your password."
			}
		}
	});
});

var urldata = new Array();
	
var getdata = window.location.search;
if(getdata) {
	getdata = getdata.substr(1);
	
	var pairs = getdata.split("&");
	
	for(var i=0; i<pairs.length; i++) {
		var pair = pairs[i].split("=");
		var pairname = pair[0];
		var pairvalue = pair[1];
		urldata[pairname] = pairvalue;
	}
}

function checkLogin() { 
	var enteredEmail = "";
	var enteredPassword = "";
	var blogID = "";
	var postID = "";
	var datastring = "";
	
	if(urldata["BlogID"] != "") blogID = urldata["BlogID"];
	if(urldata["PostID"] != "") postID = urldata["PostID"];
	
	$("#loginSubmit").val("Logging In...");
	$("#loginSubmit").attr("disabled", true);
	
	enteredEmail = $("input#loginEmail").val();
	enteredPassword = $("input#loginPassword").val();
	
	if(enteredEmail == "" || enteredEmail == "") {
		$("#loginSubmit").val("Log In");
		$("#loginSubmit").removeAttr("disabled");
		$("#PasswordError").html("The email or password you entered is incorrect.");
		$("#PasswordError").fadeIn(1000);
	} else {
		enteredPassword = hex_sha1(enteredPassword);	// for security purposes, hash the password before sending it out
		datastring = "email=" + enteredEmail + "&hash=" + enteredPassword;
		
		$.ajax({
			type: "POST",
			url: "/wwwadmin/globals/templates/9937/scripts/checkLogin.cfm",
			data: {
				'Email':enteredEmail,
				'Password':enteredPassword,
				'BlogID':blogID,
				'PostID':postID
			},
			error: function (xhr, desc, exceptionobj) {
				$("#loginSubmit").val("Log In");
				$("#loginSubmit").removeAttr("disabled");
				$("#PasswordError").html("There was a problem logging you in, please try again.");
				$("#PasswordError").fadeIn(1000);
			},
			success: function(cfmResponse) {
				var responseObj = $.secureEvalJSON(cfmResponse);
				
				if(responseObj.type == "ValidLogin") {
					loginSuccess(responseObj.type, responseObj.message, responseObj.toplogin, responseObj.bottomlogin, responseObj.modallogin, responseObj.commentlogin, responseObj.sidebarlogin);
				} else if(responseObj.type == "LoggedIn") {
					$.nyroModalRemove();
				} else if(responseObj.type == "InvalidLogin" || responseObj.type == "Error") {
					$("#loginSubmit").val("Log In");
					$("#loginSubmit").removeAttr("disabled");
					$("#PasswordError").html(responseObj.message);
					$("#PasswordError").fadeIn(1000);
				} else {
					$("#loginSubmit").val("Log In");
					$("#loginSubmit").removeAttr("disabled");
					$("#PasswordError").html("There was a problem logging you in, please try again.");
					$("#PasswordError").fadeIn(1000);
				}
			}
		});
	}
}

function changeContent(topcode, bottomcode, modalcode, commentcode, sidebarcode) {
	if(topcode != "" && bottomcode != "" && modalcode != "" && commentcode != "" && sidebarcode != "") {
		$(".top_login_container").html(topcode);
		$(".bottom_login_container").html(bottomcode);
		$("#modal_login_container").html(modalcode);
		$(".reply_container").html(commentcode);
		$(".icon_blog_text").html(sidebarcode);
	}
}

function loginSuccess(type, message, topcode, bottomcode, modalcode, commentcode, sidebarcode) {
	if(type != "" && message != "" && topcode != "" && bottomcode != "" && modalcode != "" && commentcode != "" && sidebarcode != "") {
		changeContent(topcode, bottomcode, modalcode, commentcode, sidebarcode);
		$(".modal_header").fadeOut(200);
		$(".modal_body").fadeOut(200);
		$("#closeBut").fadeOut(200, showMessage);
	} else {
		$("#loginSubmit").val("Log In");
		$("#loginSubmit").removeAttr("disabled");
		$("#PasswordError").html("There was a problem logging you in, please try again.");
		$("#PasswordError").fadeIn(1000);
	}
}

function showMessage() {
	$.nyroModalSettings({
		width: 200,
		height:50
	});
	
	$(".modal_container").html("<div class='modal_content_message_container'></div>");
	$(".modal_content_message_container").hide();
	$(".modal_content_message_container").html("Successfully logged in!");
	$(".modal_content_message_container").fadeIn(600, function() { setTimeout('$(".modal_content_message_container").fadeOut(300, function() { $.nyroModalRemove(); } );', 800); } );
}

var commentsShow = true;
	
function showHideComments() {
	if(commentsShow) {
		$(".comments_container").slideUp("slow");
	} else {
		$(".comments_container").slideDown("slow");
	}
	
	commentsShow = !commentsShow;
}

function flagComment(commentID, type, cssID) {
	if(commentID != "" && cssID != "") {
		$("#"+cssID).html("Reporting...");
		$.ajax({
			type: "POST",
			url: "/wwwadmin/globals/templates/9937/scripts/flagComment.cfm",
			data: {'commentID':commentID,'type':type},
			error: function (xhr, desc, exceptionobj) {
				$("#"+cssID).html("Problem Reporting");
			},
			success: function(cfmResponse) {
				var responseObj = $.secureEvalJSON(cfmResponse);
				
				if(responseObj.type == "Success") {
					$("#"+cssID).html("Reported");
				} else {
					$("#"+cssID).html("Problem Reporting");
				}
			}
		});	
	}
}

function logout(caller) {
	if(caller != "" && caller == "header_logout" || caller == "footer_logout" || caller == "comment_logout") {
		$("#"+caller).html("Logging out...");	
		$.ajax({
			type: "POST",
			url: "/wwwadmin/globals/templates/9937/scripts/logout.cfm",
			error: function (xhr, desc, exceptionobj) {
				$("#"+caller).html("Logout");
			},
			success: function(cfmResponse) {
				var responseObj = $.secureEvalJSON(cfmResponse);
				
				var urllocation = location.href;
				urllocation = urllocation.replace(self.location.hash,"");
				location.href = urllocation;
			}
		});
	}
}

var Url = {

 	// public method for URL encoding
 	encode : function (string) {
 		 return escape(this._utf8_encode(string));
 	},

 	// public method for URL decoding
	 decode : function (string) {
 	 	return this._utf8_decode(unescape(string));
 	},

 	// private method for UTF-8 encoding
 	_utf8_encode : function (string) {
  		string = string.replace(/\r\n/g,"\n");
 	 	var utftext = "";

  		for (var n = 0; n < string.length; n++) {
   			var c = string.charCodeAt(n);
   			if (c < 128) {
    				utftext += String.fromCharCode(c);
 			} else if((c > 127) && (c < 2048)) {
  				utftext += String.fromCharCode((c >> 6) | 192);
  				utftext += String.fromCharCode((c & 63) | 128);
 			} else {
  				utftext += String.fromCharCode((c >> 12) | 224);
  				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
 	 			utftext += String.fromCharCode((c & 63) | 128);
 			}
 	}

		return utftext;
	},

 	// private method for UTF-8 decoding
 	_utf8_decode : function (utftext) {
 		 var string = "";
 		 var i = 0;
 		 var c = c1 = c2 = 0;

  		while ( i < utftext.length ) {
  			 c = utftext.charCodeAt(i);
   			if (c < 128) {
    				string += String.fromCharCode(c);
    				i++;
  			 } else if((c > 191) && (c < 224)) {
 				   c2 = utftext.charCodeAt(i+1);
    				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    				i += 2;
  			 } else {
 				   c2 = utftext.charCodeAt(i+1);
    				c3 = utftext.charCodeAt(i+2);
    				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
   				 i += 3;
 			  }
		  }
		return string;
	 }
}
