$(function(){
	
	$.browser.iOS = (navigator.userAgent).match(/(iphone|ipod|ipad)/i) ? true : false;
	
	// Init brs
	brs.hero = $('#page-container');

	// If no content exists in the lhs col, remove it and remove the rhs class from the main content
	if ($('div.content-lhs').children().length == 0) {
		$('div.content-lhs').css('display', 'none');
		$('div.content').removeClass('content-rhs');
	}
	
	// Add rounded corners to the navigation
	$('ul.navigation-lhs').find('li:first a').addClass('navigation-lhs-first');
	$('ul.navigation-lhs').find('li:last a').addClass('navigation-lhs-last');
	
	// Defaults for the tooltips
	//$.fn.qtip.defaults.position.corner.target = 'topMiddle';
	//$.fn.qtip.defaults.position.corner.tooltip = 'bottomMiddle';
	
/*
	$.fn.qtip.styles.brs = {
		border: {
			width:5,
			radius:10,
			color:'#333'
		},
		tip:{
			corner:'bottomMiddle',
			color:'#333',
			size:{x:10,y:5}
		},
		width:300,
		background:'#333',
		color:'#FFF',
		size:11
	}
	
	$('.tooltip').qtip({style:'brs'});
*/
	
	// Add events to form buttons	
	$('button.button')
		.each(function(i,o){
			var el = $(this);
			var iconPrimary = el.attr('iconprimary');
			var iconSecondary = el.attr('iconsecondary');
			
			el
				.button({
					icons:{
						primary:iconPrimary,
						secondary:iconSecondary
					}
				});
			
		});
	
	// Common validate function	
	$.fn.validate = function(__regex) {
		var valid = $(this).val().match(__regex) ? true : false;
		
		if (valid) {
			$(this).removeClass('field-error');
		} else {
			$(this).addClass('field-error');
		}
		
		return valid;
	}
	
	// Single onload function
	$(window)
		.load(function(){
			
			$('<img src="media/images/hero.png">')
				.load(function(){
					brs.hero
						.addClass('page-container');
					//brs.refresh();
					//setInterval(brs.refresh, 1000);
				});
			
				
		})
		.resize(function(){
			brs.refresh();
		});
		
	// Subnavigation
	if ($('#sn-button-set').length > 0) {
		$('#sn-button-set')
			.find('label')
				.click(function(){
					if ($('#'+$(this).attr('for')).is(':disabled') == false) location.href = $('#' + $(this).attr('for')).val();
				})
				.end()
			.buttonset();
	}

});

var brs = {

	hero:null,
	validateUser:function(){
		
		$(function(){
			
			$.get('media/connections/brs/is-logged-in.ashx', function(r){
				if (r.match(/success/)) {
					setTimeout(brs.validateUser, 60000);
				} else {
					document.location.href = '/default.aspx';
				}
			});
			
		});
		
	},
	serializeFormData:function() {
		var newData = false;
		var fields = {};
		
		$('form.form input, form.form select, form.form textarea').each(function(){
			var val;
			var fEl = $(this);
			
			switch(fEl.tagName()) {
			
				case 'checkbox':
					val = fEl.is(':checked').toString();
				break;
				
				default:
					val = fEl.val();
				break;
			
			}
			
			fields[fEl.attr('id')] = val;
			
		});
		
		return fields;
	},
	refresh:function(){
		
	},
	fancybox:function(_o){
		
		var uid = 'f'+$.uid();
		var o = $.extend(true, {
		
			overlayColor:'#FFF',
			overlayOpacity:0.9,
			icon:'',
			width: 600,
			height: 'auto',
			autoDimensions: /\d+/.test(_o['height']) ? false : true,
			scrolling: 'no',
			padding: 10,
			title: '',
			titleShow: false,
			message: '',
			modal:false,
			buttonLabel: 'yep - please continue',
			click: function(){},
			onComplete:function(){}
		
		}, _o);
		
		o.content = $("<form id='"+ uid +"'><div class='fancybox-prompt' style='width:"+o.width+"px'><h2 class='"+ o.icon +"'>"+ o.title +"</h2><div class='message'>"+ o.message +"</div><button type='submit' class='yes'>"+ o.buttonLabel +"</button></div></form>");
		
		if (/\w+/.test(o.message) == false) o.content.find('div.message').remove();
		if (o.buttonLabel == '') {
			o.content
				.find('button.yes')
					.remove()
					.end()
				.find('div.message')
					.css('border-bottom', 'none');
			
		}
		
		$.fancybox(o);
		
		if ($.browser.iOS) $(window).unbind("resize.fb scroll.fb");
		
		$('#'+uid)
			.find('button')
				.button()
				.end()
			.bind('submit', function(e){
				e.preventDefault();
				o.click(e);
			});
		
		return o;
		
	}

}


$.fn.disable = function() {
	//alert('deprecated');
	//return;
/*     return $(this).attr('disabled', 'disabled').addClass('disabled');  */
} 

$.fn.enable = function() { 
	//alert('deprecated');
/*     return $(this).removeAttr('disabled').removeClass('disabled'); */
} 

$.fn.tagName = function() {
	return this.get(0).tagName.toLowerCase();
}

$.fn.paging = function(__url, __options){

	var limit = 50;
	var url = document.location.toString();
	var page = /page=\d+/i.test(url) ? parseFloat(url.match(/page=(\d+)/i)[1]) : 1;
	var totalPages = 0;
	var total = 0;
	var uid = new Date().getTime();
	var pagingList = $('<div class="paging-list-'+uid+'">');
	var options = $.extend({}, {click:function(){},complete:function(){}}, __options);
	var codeFile = location.href.match(/\/(\w+\.aspx)/)[1];
	
	$('button.previous, button.next').css('display', 'none');
	
	$(this)
		.css({
			'padding':'0px'
		})
		.append(pagingList);
	
	$.get(__url, {
		page:page,
		limit:limit
	}, function(r){
	
		pagingList.html(r);
		
		pagingList.find('tbody tr:odd').addClass('tr-odd');
		
		pagingList.find('tbody tr td')
			.click(options.click);
			
		total = parseFloat($('.total-records').text());
		totalPages = Math.ceil(total/limit);
		
		updatePaging();
		
		options.complete();
			
	});
	
	updatePaging = function() {
	
		if (totalPages > 1) {
			
			$('button.previous, button.next').css('display', '');
			
			$('button.next')
				.button({
					icons:{
						secondary:'ui-icon-circle-triangle-e'
					}
				})
				.click(function(){
					document.location.href = codeFile + '?page=' + (page+1);
				});
								
			$('button.previous')
				.button({
					icons:{
						primary:'ui-icon-circle-triangle-w'
					}
				})
				.click(function(){
					document.location.href = codeFile + '?page=' + (page-1);
				});
			
			var buttonSetWrapper = $('<span>');
			
			for (var i=0; i<totalPages; i++) {
				$('<input type="radio" id="pb-'+i+'" '+(i+1==page?'checked="checked"':'')+'><label for="pb-'+i+'">'+(i+1)+'</label>')
					.click(function(e){
						e.preventDefault();
						document.location.href = codeFile + '?page=' + $(this).text();
					})
					.appendTo(buttonSetWrapper);
			}
			
			buttonSetWrapper
				.buttonset()
				.appendTo('div.page-buttons');
			
			if (page == 1) $('button.previous').button('disable');
			if (page >= totalPages) $('button.next').button('disable');
		
		}
		
	}
	
	return this;
};



$.extend({
	
	clientID: function(id) {
		var el = $("[id$='" + id + "']");
		
		if (el.length < 1) {
			el = $(id);
		}
		
		return el;
	}
	
});


function roundTo(__number, __places) {
	var pow = Math.pow(10, __places);
	return Math.round(__number * pow) / pow;
}

function parseNumber(__number, __revertTo) {
	
	var revertTo = isNaN( parseFloat(__revertTo) ) ? 0 : parseFloat(__revertTo);
	
	return isNaN(parseFloat(__number)) ? revertTo : parseFloat(__number);
}

function calculateComposition() {
	var isMale = $('#Male').val().match(/true/i) ? true : false;
	
	var test_weight = parseNumber($('#Test_Weight').val());
	var test_leanmass = parseNumber($('#Test_LeanMass').val());
	var test_fatmass = parseNumber($('#Test_FatMass').val());
	var test_bodyfat = parseNumber($('#Test_BodyFat').val());
	var test_chest = parseNumber($('#Test_Chest').val());
	var test_ab = parseNumber($('#Test_Ab').val());
	var test_thigh = parseNumber($('#Test_Thigh').val());
	var test_tricep = parseNumber($('#Test_Tricep').val());
	var test_hip = parseNumber($('#Test_Hip').val());
	var goal_weight = parseNumber($('#Goal_Weight').val());
	var goal_leanmass = parseNumber($('#Goal_LeanMass').val());
	var goal_fatmass = parseNumber($('#Goal_FatMass').val());
	var goal_bodyfat = parseNumber($('#Goal_BodyFat').val());
	var change_weight = parseNumber($('#Change_Weight').val());
	var change_leanmass = parseNumber($('#Change_LeanMass').val());
	var change_fatmass = parseNumber($('#Change_FatMass').val());
	var change_bodyfat = parseNumber($('#Change_BodyFat').val());
	var test_bench = parseNumber($('#Test_Bench').val());
	var test_squat = parseNumber($('#Test_Squat').val());
	var test_deadlift = parseNumber($('#Test_Deadlift').val());
	var estimate_bench = parseNumber($('#Estimate_Bench').val());
	var estimate_squat = parseNumber($('#Estimate_Squat').val());
	var estimate_deadlift = parseNumber($('#Estimate_Deadlift').val());
	var goal_bench = parseNumber($('#Goal_Bench').val());
	var goal_squat = parseNumber($('#Goal_Squat').val());
	var goal_deadlift = parseNumber($('#Goal_Deadlift').val());
	var change_bench = parseNumber($('#Change_Bench').val());
	var change_squat = parseNumber($('#Change_Squat').val());
	var change_deadlift = parseNumber($('#Change_Deadlift').val());
	
	if (!isNaN(test_weight) && !isNaN(test_bodyfat)) {
		test_leanmass = roundTo(test_weight * (1-(test_bodyfat/100)), 2);
		$('#Test_LeanMass').val(test_leanmass);
	}
	
	if (!isNaN(test_weight) && !isNaN(test_leanmass)) {
		test_fatmass = roundTo(test_weight - test_leanmass, 2);
		$('#Test_FatMass').val(test_fatmass);
	}
	
	if (!isNaN(test_bodyfat) && !isNaN(goal_bodyfat)) {
		change_bodyfat = roundTo(goal_bodyfat - test_bodyfat, 2);
		$('#Change_BodyFat').val(change_bodyfat);
	}
	
	if (!isNaN(goal_weight) && !isNaN(goal_bodyfat)) {
		goal_leanmass = roundTo(goal_weight * (1-(goal_bodyfat/100)), 2);
		$('#Goal_LeanMass').val(goal_leanmass);
	}
	
	if (!isNaN(goal_weight) && !isNaN(goal_leanmass)) {
		goal_fatmass = roundTo(goal_weight - goal_leanmass, 2);
		$('#Goal_FatMass').val(goal_fatmass);
	}
	
	if (!isNaN(goal_weight) && !isNaN(test_weight)) {
		$('#Change_Weight_label').html((goal_weight-test_weight > 0 ? 'eat on: ' : 'diet off: '));
		change_weight = roundTo(Math.abs(goal_weight-test_weight), 2);
		$('#Change_Weight').val(change_weight);
	}
	
	if (!isNaN(goal_leanmass) && !isNaN(test_leanmass)) {
		$('#Change_LeanMass_label').html((goal_leanmass-test_leanmass > 0 ? 'train on: ' : 'cannibalise: '));
		//change_leanmass = roundTo(Math.abs(goal_leanmass-test_leanmass), 2);
		change_leanmass = roundTo((goal_leanmass-test_leanmass), 2);
		$('#Change_LeanMass').val(change_leanmass);
	}
	
	if (!isNaN(goal_fatmass) && !isNaN(test_fatmass)) {
		$('#Change_FatMass_label').html((goal_fatmass-test_fatmass > 0 ? 'net fat gain: ' : 'net fat loss: '));
		change_fatmass = roundTo(Math.abs(goal_fatmass-test_fatmass), 2);
		$('#Change_FatMass').val(change_fatmass);
	}
	
	var bench, squat, deadlift;
	
	// If there is more than one test. Use the test. Otherwise use the estimate.
	bench = estimate_bench > 0 ? estimate_bench : test_bench;
	squat = estimate_squat > 0 ? estimate_squat : test_squat;
	deadlift = estimate_deadlift > 0 ? estimate_deadlift : test_deadlift;
	
	//if (!isNaN(test_bench)) {
	
		if (bench > 0 && !isNaN(change_bench)) {
			goal_bench = roundTo(bench+change_bench, 2)
			$('#Goal_Bench').val(goal_bench);
		} else if (!isNaN(change_bench) && !isNaN(estimate_bench)) {
			goal_bench = roundTo(estimate_bench+change_bench, 2);
			$('#Goal_Bench').val(goal_bench);
		}
		
	//}
	
	//if (!isNaN(test_squat)) {
	
		if (squat > 0 && !isNaN(change_squat)) {
			goal_squat = roundTo(squat+change_squat, 2);
			$('#Goal_Squat').val(goal_squat);
		} else if (!isNaN(change_squat) && !isNaN(estimate_squat)) {
			goal_squat = roundTo(estimate_squat+change_squat, 2);
			$('#Goal_Squat').val(goal_squat);
		}
		
	//}
	
	//if (!isNaN(test_deadlift)) {
	
		if (deadlift > 0 && !isNaN(change_deadlift)) {
			goal_deadlift = roundTo(deadlift+change_deadlift, 2);
			$('#Goal_Deadlift').val(goal_deadlift);
		} else if (!isNaN(change_deadlift) && !isNaN(estimate_deadlift)) {
			goal_deadlift = roundTo(estimate_deadlift+change_deadlift, 2);
			$('#Goal_Deadlift').val(goal_deadlift);
		}
		
	//}
	
	if (!isNaN(change_leanmass)) {
		change_bench = roundTo(change_leanmass * 3, 2);
		$('#Change_Bench').val(change_bench);
	}
	
	if (!isNaN(change_leanmass)) {
		change_squat = roundTo(change_leanmass * 4, 2);
		$('#Change_Squat').val(change_squat);
	}
	
	if (!isNaN(change_leanmass)) {
		change_deadlift = roundTo(change_leanmass * 4.5, 2);
		$('#Change_Deadlift').val(change_deadlift);
	}
	
	// Validate fields for body fat
	var test_ok = isMale ? (test_chest > 0 && test_ab > 0 && test_thigh > 0) : (test_tricep > 0 && test_hip > 0 && test_thigh > 0);
	var test_sum = isMale ? test_chest + test_ab + test_thigh : test_tricep + test_hip + test_thigh;
	var dob_useCurrentYear = ($('#DOB').val().match(/\d{1,2}\/\d{1,2}\/\d{4}/) == null);
	var dob_year = dob_useCurrentYear ? new Date().getFullYear() : parseFloat($('#DOB').val().match(/\/(\d{4})/)[1]);
	var ageyear = new Date().getFullYear() - dob_year;
	var co1 = isMale ? 1.10938 : 1.0994921;
	var co2 = isMale ? 0.0008267 : 0.0009929;
	var co3 = isMale ? 0.0000016 : 0.0000023;
	var co4 = isMale ? 0.0002574 : 0.0001392;
	var calc1;
	var calc2;
	
	if (isMale) {
		calc1 = (0.02+((495/(co1-(co2*test_sum)+(co3*Math.pow(test_sum,2))-(co4*ageyear))-450)/100)) * 100;
		calc2 = (0.02+(4.57/(co1-(co2*test_sum)+(co3*Math.pow(test_sum,2))-(co4*ageyear)))-4.142) * 100;
	} else {
		calc1 = (0.02+((495/(co1-(co2*test_sum)+(co3*Math.pow(test_sum,2))-(co4*(ageyear)))-450)/100)) * 100;
		calc2 = (0.02+(4.57/(co1-(co2*test_sum)+(co3*test_sum)-(co4*ageyear)))-4.142) * 100;
	}
	
	if (test_ok) {
		test_bodyfat = roundTo((calc1+calc2) / 2, 2);
		$('#Test_BodyFat').val(test_bodyfat);
	}
	
	$('.Test_Ab, .Test_Chest').css('display', isMale ? '' : 'none');
	$('.Test_Tricep, .Test_Hip, .UnderOverEater').css('display', isMale ? 'none' : '');
	
	// Until the calculations are provided hide these fields
	$('.Test_Subscapular, .Test_Midaxillary, .Test_Bicep, .Test_LowerBack, .Test_Calf, .Test_Neck, .Test_Abdomen, .Test_Waist').css('display', 'none');
	
}


function calcBodyCompFirstTime() {
	var isMale = $('#Male').val().match(/true/i) ? true : false;
	
	var test_weight = parseNumber($('#Test_Weight').val());
	var test_leanmass = parseNumber($('#Test_LeanMass').val());
	var test_fatmass = parseNumber($('#Test_FatMass').val());
	var test_bodyfat = parseNumber($('#Test_BodyFat').val());
	var test_chest = parseNumber($('#Test_Chest').val());
	var test_ab = parseNumber($('#Test_Ab').val());
	var test_thigh = parseNumber($('#Test_Thigh').val());
	var test_tricep = parseNumber($('#Test_Tricep').val());
	var test_hip = parseNumber($('#Test_Hip').val());

	// Validate fields for body fat
	var test_ok = isMale ? (test_chest > 0 && test_ab > 0 && test_thigh > 0) : (test_tricep > 0 && test_hip > 0 && test_thigh > 0);
	var test_sum = isMale ? test_chest + test_ab + test_thigh : test_tricep + test_hip + test_thigh;
	var dob_useCurrentYear = ($('#DOB').val().match(/\d{1,2}\/\d{1,2}\/\d{4}/) == null);
	var dob_year = dob_useCurrentYear ? new Date().getFullYear() : parseFloat($('#DOB').val().match(/\/(\d{4})/)[1]);
	var ageyear = new Date().getFullYear() - dob_year;
	var co1 = isMale ? 1.10938 : 1.0994921;
	var co2 = isMale ? 0.0008267 : 0.0009929;
	var co3 = isMale ? 0.0000016 : 0.0000023;
	var co4 = isMale ? 0.0002574 : 0.0001392;
	var calc1;
	var calc2;
	
	if (isMale) {
		calc1 = (0.02+((495/(co1-(co2*test_sum)+(co3*Math.pow(test_sum,2))-(co4*ageyear))-450)/100)) * 100;
		calc2 = (0.02+(4.57/(co1-(co2*test_sum)+(co3*Math.pow(test_sum,2))-(co4*ageyear)))-4.142) * 100;
	} else {
		calc1 = (0.02+((495/(co1-(co2*test_sum)+(co3*Math.pow(test_sum,2))-(co4*(ageyear)))-450)/100)) * 100;
		calc2 = (0.02+(4.57/(co1-(co2*test_sum)+(co3*test_sum)-(co4*ageyear)))-4.142) * 100;
	}
	
	if (test_ok) {
		test_bodyfat = roundTo((calc1+calc2)  / 2, 2);
		$('#Test_BodyFat').val(test_bodyfat);
	}
	
	if (!isNaN(test_weight) && !isNaN(test_bodyfat)) {
		test_leanmass = roundTo(test_weight * (1-(test_bodyfat/100)), 2);
		$('#Test_LeanMass').val(test_leanmass);
	}
	
	if (!isNaN(test_weight) && !isNaN(test_leanmass)) {
		test_fatmass = roundTo(test_weight - test_leanmass, 2);
		$('#Test_FatMass').val(test_fatmass);
	}

}


;(function($){

	$.uid=function(){
		return $.md5(Math.random(new Date().getTime()));
	}
	
})(jQuery);




/**
 * jQuery MD5 hash algorithm function
 * 
 * 	<code>
 * 		Calculate the md5 hash of a String 
 * 		String $.md5 ( String str )
 * 	</code>
 * 
 * Calculates the MD5 hash of str using the é RSA Data Security, Inc. MD5 Message-Digest Algorithm, and returns that hash. 
 * MD5 (Message-Digest algorithm 5) is a widely-used cryptographic hash function with a 128-bit hash value. MD5 has been employed in a wide variety of security applications, and is also commonly used to check the integrity of data. The generated hash is also non-reversable. Data cannot be retrieved from the message digest, the digest uniquely identifies the data.
 * MD5 was developed by Professor Ronald L. Rivest in 1994. Its 128 bit (16 byte) message digest makes it a faster implementation than SHA-1.
 * This script is used to process a variable length message into a fixed-length output of 128 bits using the MD5 algorithm. It is fully compatible with UTF-8 encoding. It is very useful when u want to transfer encrypted passwords over the internet. If you plan using UTF-8 encoding in your project don't forget to set the page encoding to UTF-8 (Content-Type meta tag). 
 * This function orginally get from the WebToolkit and rewrite for using as the jQuery plugin.
 * 
 * Example
 * 	Code
 * 		<code>
 * 			$.md5("I'm Persian."); 
 * 		</code>
 * 	Result
 * 		<code>
 * 			"b8c901d0f02223f9761016cfff9d68df"
 * 		</code>
 * 
 * @alias Muhammad Hussein Fattahizadeh < muhammad [AT] semnanweb [DOT] com >
 * @link http://www.semnanweb.com/jquery-plugin/md5.html
 * @see http://www.webtoolkit.info/
 * @license http://www.gnu.org/licenses/gpl.html [GNU General Public License]
 * @param {jQuery} {md5:function(string))
 * @return string
 */

(function($){
	
	var rotateLeft = function(lValue, iShiftBits) {
		return (lValue << iShiftBits) | (lValue >>> (32 - iShiftBits));
	}
	
	var addUnsigned = function(lX, lY) {
		var lX4, lY4, lX8, lY8, lResult;
		lX8 = (lX & 0x80000000);
		lY8 = (lY & 0x80000000);
		lX4 = (lX & 0x40000000);
		lY4 = (lY & 0x40000000);
		lResult = (lX & 0x3FFFFFFF) + (lY & 0x3FFFFFFF);
		if (lX4 & lY4) return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
		if (lX4 | lY4) {
			if (lResult & 0x40000000) return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
			else return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
		} else {
			return (lResult ^ lX8 ^ lY8);
		}
	}
	
	var F = function(x, y, z) {
		return (x & y) | ((~ x) & z);
	}
	
	var G = function(x, y, z) {
		return (x & z) | (y & (~ z));
	}
	
	var H = function(x, y, z) {
		return (x ^ y ^ z);
	}
	
	var I = function(x, y, z) {
		return (y ^ (x | (~ z)));
	}
	
	var FF = function(a, b, c, d, x, s, ac) {
		a = addUnsigned(a, addUnsigned(addUnsigned(F(b, c, d), x), ac));
		return addUnsigned(rotateLeft(a, s), b);
	};
	
	var GG = function(a, b, c, d, x, s, ac) {
		a = addUnsigned(a, addUnsigned(addUnsigned(G(b, c, d), x), ac));
		return addUnsigned(rotateLeft(a, s), b);
	};
	
	var HH = function(a, b, c, d, x, s, ac) {
		a = addUnsigned(a, addUnsigned(addUnsigned(H(b, c, d), x), ac));
		return addUnsigned(rotateLeft(a, s), b);
	};
	
	var II = function(a, b, c, d, x, s, ac) {
		a = addUnsigned(a, addUnsigned(addUnsigned(I(b, c, d), x), ac));
		return addUnsigned(rotateLeft(a, s), b);
	};
	
	var convertToWordArray = function(string) {
		var lWordCount;
		var lMessageLength = string.length;
		var lNumberOfWordsTempOne = lMessageLength + 8;
		var lNumberOfWordsTempTwo = (lNumberOfWordsTempOne - (lNumberOfWordsTempOne % 64)) / 64;
		var lNumberOfWords = (lNumberOfWordsTempTwo + 1) * 16;
		var lWordArray = Array(lNumberOfWords - 1);
		var lBytePosition = 0;
		var lByteCount = 0;
		while (lByteCount < lMessageLength) {
			lWordCount = (lByteCount - (lByteCount % 4)) / 4;
			lBytePosition = (lByteCount % 4) * 8;
			lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount) << lBytePosition));
			lByteCount++;
		}
		lWordCount = (lByteCount - (lByteCount % 4)) / 4;
		lBytePosition = (lByteCount % 4) * 8;
		lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80 << lBytePosition);
		lWordArray[lNumberOfWords - 2] = lMessageLength << 3;
		lWordArray[lNumberOfWords - 1] = lMessageLength >>> 29;
		return lWordArray;
	};
	
	var wordToHex = function(lValue) {
		var WordToHexValue = "", WordToHexValueTemp = "", lByte, lCount;
		for (lCount = 0; lCount <= 3; lCount++) {
			lByte = (lValue >>> (lCount * 8)) & 255;
			WordToHexValueTemp = "0" + lByte.toString(16);
			WordToHexValue = WordToHexValue + WordToHexValueTemp.substr(WordToHexValueTemp.length - 2, 2);
		}
		return WordToHexValue;
	};
	
	var uTF8Encode = function(string) {
		string = string.replace(/\x0d\x0a/g, "\x0a");
		var output = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				output += String.fromCharCode(c);
			} else if ((c > 127) && (c < 2048)) {
				output += String.fromCharCode((c >> 6) | 192);
				output += String.fromCharCode((c & 63) | 128);
			} else {
				output += String.fromCharCode((c >> 12) | 224);
				output += String.fromCharCode(((c >> 6) & 63) | 128);
				output += String.fromCharCode((c & 63) | 128);
			}
		}
		return output;
	};
	
	$.extend({
		md5: function(string) {
			var x = Array();
			var k, AA, BB, CC, DD, a, b, c, d;
			var S11=7, S12=12, S13=17, S14=22;
			var S21=5, S22=9 , S23=14, S24=20;
			var S31=4, S32=11, S33=16, S34=23;
			var S41=6, S42=10, S43=15, S44=21;
			string = uTF8Encode(string.toString());
			x = convertToWordArray(string);
			a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
			for (k = 0; k < x.length; k += 16) {
				AA = a; BB = b; CC = c; DD = d;
				a = FF(a, b, c, d, x[k+0],  S11, 0xD76AA478);
				d = FF(d, a, b, c, x[k+1],  S12, 0xE8C7B756);
				c = FF(c, d, a, b, x[k+2],  S13, 0x242070DB);
				b = FF(b, c, d, a, x[k+3],  S14, 0xC1BDCEEE);
				a = FF(a, b, c, d, x[k+4],  S11, 0xF57C0FAF);
				d = FF(d, a, b, c, x[k+5],  S12, 0x4787C62A);
				c = FF(c, d, a, b, x[k+6],  S13, 0xA8304613);
				b = FF(b, c, d, a, x[k+7],  S14, 0xFD469501);
				a = FF(a, b, c, d, x[k+8],  S11, 0x698098D8);
				d = FF(d, a, b, c, x[k+9],  S12, 0x8B44F7AF);
				c = FF(c, d, a, b, x[k+10], S13, 0xFFFF5BB1);
				b = FF(b, c, d, a, x[k+11], S14, 0x895CD7BE);
				a = FF(a, b, c, d, x[k+12], S11, 0x6B901122);
				d = FF(d, a, b, c, x[k+13], S12, 0xFD987193);
				c = FF(c, d, a, b, x[k+14], S13, 0xA679438E);
				b = FF(b, c, d, a, x[k+15], S14, 0x49B40821);
				a = GG(a, b, c, d, x[k+1],  S21, 0xF61E2562);
				d = GG(d, a, b, c, x[k+6],  S22, 0xC040B340);
				c = GG(c, d, a, b, x[k+11], S23, 0x265E5A51);
				b = GG(b, c, d, a, x[k+0],  S24, 0xE9B6C7AA);
				a = GG(a, b, c, d, x[k+5],  S21, 0xD62F105D);
				d = GG(d, a, b, c, x[k+10], S22, 0x2441453);
				c = GG(c, d, a, b, x[k+15], S23, 0xD8A1E681);
				b = GG(b, c, d, a, x[k+4],  S24, 0xE7D3FBC8);
				a = GG(a, b, c, d, x[k+9],  S21, 0x21E1CDE6);
				d = GG(d, a, b, c, x[k+14], S22, 0xC33707D6);
				c = GG(c, d, a, b, x[k+3],  S23, 0xF4D50D87);
				b = GG(b, c, d, a, x[k+8],  S24, 0x455A14ED);
				a = GG(a, b, c, d, x[k+13], S21, 0xA9E3E905);
				d = GG(d, a, b, c, x[k+2],  S22, 0xFCEFA3F8);
				c = GG(c, d, a, b, x[k+7],  S23, 0x676F02D9);
				b = GG(b, c, d, a, x[k+12], S24, 0x8D2A4C8A);
				a = HH(a, b, c, d, x[k+5],  S31, 0xFFFA3942);
				d = HH(d, a, b, c, x[k+8],  S32, 0x8771F681);
				c = HH(c, d, a, b, x[k+11], S33, 0x6D9D6122);
				b = HH(b, c, d, a, x[k+14], S34, 0xFDE5380C);
				a = HH(a, b, c, d, x[k+1],  S31, 0xA4BEEA44);
				d = HH(d, a, b, c, x[k+4],  S32, 0x4BDECFA9);
				c = HH(c, d, a, b, x[k+7],  S33, 0xF6BB4B60);
				b = HH(b, c, d, a, x[k+10], S34, 0xBEBFBC70);
				a = HH(a, b, c, d, x[k+13], S31, 0x289B7EC6);
				d = HH(d, a, b, c, x[k+0],  S32, 0xEAA127FA);
				c = HH(c, d, a, b, x[k+3],  S33, 0xD4EF3085);
				b = HH(b, c, d, a, x[k+6],  S34, 0x4881D05);
				a = HH(a, b, c, d, x[k+9],  S31, 0xD9D4D039);
				d = HH(d, a, b, c, x[k+12], S32, 0xE6DB99E5);
				c = HH(c, d, a, b, x[k+15], S33, 0x1FA27CF8);
				b = HH(b, c, d, a, x[k+2],  S34, 0xC4AC5665);
				a = II(a, b, c, d, x[k+0],  S41, 0xF4292244);
				d = II(d, a, b, c, x[k+7],  S42, 0x432AFF97);
				c = II(c, d, a, b, x[k+14], S43, 0xAB9423A7);
				b = II(b, c, d, a, x[k+5],  S44, 0xFC93A039);
				a = II(a, b, c, d, x[k+12], S41, 0x655B59C3);
				d = II(d, a, b, c, x[k+3],  S42, 0x8F0CCC92);
				c = II(c, d, a, b, x[k+10], S43, 0xFFEFF47D);
				b = II(b, c, d, a, x[k+1],  S44, 0x85845DD1);
				a = II(a, b, c, d, x[k+8],  S41, 0x6FA87E4F);
				d = II(d, a, b, c, x[k+15], S42, 0xFE2CE6E0);
				c = II(c, d, a, b, x[k+6],  S43, 0xA3014314);
				b = II(b, c, d, a, x[k+13], S44, 0x4E0811A1);
				a = II(a, b, c, d, x[k+4],  S41, 0xF7537E82);
				d = II(d, a, b, c, x[k+11], S42, 0xBD3AF235);
				c = II(c, d, a, b, x[k+2],  S43, 0x2AD7D2BB);
				b = II(b, c, d, a, x[k+9],  S44, 0xEB86D391);
				a = addUnsigned(a, AA);
				b = addUnsigned(b, BB);
				c = addUnsigned(c, CC);
				d = addUnsigned(d, DD);
			}
			var tempValue = wordToHex(a) + wordToHex(b) + wordToHex(c) + wordToHex(d);
			return tempValue.toLowerCase();
		}
	});
})(jQuery);




/*
 * In-Field Label jQuery Plugin
 * http://fuelyourcoding.com/scripts/infield.html
 *
 * Copyright (c) 2009 Doug Neiner
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 0.1
 */
(function($){$.InFieldLabels=function(b,c,d){var f=this;f.$label=$(b);f.label=b;f.$field=$(c);f.field=c;f.$label.data("InFieldLabels",f);f.showing=true;f.init=function(){f.options=$.extend({},$.InFieldLabels.defaultOptions,d);if(f.$field.val()!=""){f.$label.hide();f.showing=false};f.$field.focus(function(){f.fadeOnFocus()}).blur(function(){f.checkForEmpty(true)}).bind('keydown.infieldlabel',function(e){f.hideOnChange(e)}).change(function(e){f.checkForEmpty()}).bind('onPropertyChange',function(){f.checkForEmpty()})};f.fadeOnFocus=function(){if(f.showing){f.setOpacity(f.options.fadeOpacity)}};f.setOpacity=function(a){f.$label.stop().animate({opacity:a},f.options.fadeDuration);f.showing=(a>0.0)};f.checkForEmpty=function(a){if(f.$field.val()==""){f.prepForShow();f.setOpacity(a?1.0:f.options.fadeOpacity)}else{f.setOpacity(0.0)}};f.prepForShow=function(e){if(!f.showing){f.$label.css({opacity:0.0}).show();f.$field.bind('keydown.infieldlabel',function(e){f.hideOnChange(e)})}};f.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(f.showing){f.$label.hide();f.showing=false};f.$field.unbind('keydown.infieldlabel')};f.init()};$.InFieldLabels.defaultOptions={fadeOpacity:0.5,fadeDuration:300};$.fn.inFieldLabels=function(c){return this.each(function(){var a=$(this).attr('for');if(!a)return;var b=$("input#"+a+"[type='text'],"+"input#"+a+"[type='password'],"+"textarea#"+a);if(b.length==0)return;(new $.InFieldLabels(this,b[0],c))})}})(jQuery);
