$(document).ready(

    function () {



		$("#usePreWritten").click( function() {

			$("textarea[@name='msg']").val('Thank you for showing me kindness.  It made my day!');

			var msg = $("textarea[@name='msg']").val();

			$("input[@name='txt_num']").val( (2000 - msg.length) );

		});

		

		$("#whatToWrite").click( function() {

			$("#helpWriteAck").slideToggle(400);

			$("textarea[@name='msg']").val();

			

			var msg = $("textarea[@name='msg']").val();

			$("input[@name='txt_num']").val( (2000 - msg.length) );

		});

		

		$("#googleMapHelp").click( function() {

			$("#mapHelp").slideToggle(400);

		});

		

		$("#createAck").click( function() {

			

			

			var youReceived = $("input[@name='youReceived']").val();

			if ( youReceived != "" ) {

				var ln = youReceived.length -1;

				youReceived = addPunctuation( youReceived, ln );

			}

			

			var theDifference = $("input[@name='theDifference']").val();

			if ( theDifference != "" ) {

				ln = theDifference.length -1;

				theDifference = addPunctuation( theDifference, ln );

			}

			

			var theFeeling = $("input[@name='theFeeling']").val();

			if ( theFeeling != "" ) {

				ln = theFeeling.length -1;

				theFeeling = addPunctuation( theFeeling, ln );

			}

			

			var anythingElse = $("input[@name='anythingElse']").val();

			if ( anythingElse != "" ) {

				ln = anythingElse.length -1;

				anythingElse = addPunctuation( anythingElse, ln );

			}

			

			var newMsg = youReceived + theDifference + theFeeling + anythingElse;

			

			if ( newMsg != "" ) {

				$("textarea[@name='msg']").val( newMsg );

				$("#helpWriteAck").slideToggle(400);

			} else {

				alert("Please answer at least one of the four questions above.");

				$("input[@name='youReceived']").focus();

			}

			

		});

		

		

		

		$("#btnInviteFriend").click( function() {

			$("#slideInviteFriend").slideToggle(600);

		});

	

		$("a.share").click( function() {

			

			var ackId = $(this)./*parent("span").parent("div").*/parent("div").find("input[@name='ackId']").attr("value");

			var pageId = $(this)./*parent("span").parent("div").*/parent("div").find("input[@name='pageId']").attr("value");

			//alert(ackId);

			//alert(pageId);

			var frmHTML = '<div class="clearBoth"></div><form action="' + pageId + '" method="post" name="share"><input type="hidden" name="_share_check" value="1" /><input type="hidden" name="ackId" value="' + ackId + '" /><table border="0" cellpadding="2" class="tblShare"><tr><td>Your Name:</td><td><input type="text" name="fromName" class="small" /></td></tr><tr><td>Your Email Address:</td><td><input type="text" name="fromEmail" class="small" /></td></tr><tr><td>Friends\' Name:</td><td><input type="text" name="friendName" class="small" /></td></tr><tr><td>Friends\' Email Address:</td><td><input type="text" name="friendEmail" class="small" /></td></tr><tr><td></td><td><button type="submit" onClick="return validate_ShareFriend();"> Share </button></td></tr></table></form>';

			

			if ( $(this)./*parent("span").*/parent("div").next("div").html() == "" ) {

				$(this)./*parent("span").*/parent("div").next("div").html( frmHTML );

				$(this)./*parent("span").*/parent("div").next("div").find("input[@name='fromName']").focus();

			} else {

				$(this)./*parent("span").*/parent("div").next("div").html( "" );

			}

		

		});

		

		$("#sectionLink_Login").click( function() {			// input[@type="radio"]

            $(this).next("#dropDownSect_Login").slideToggle(600);

			$("input[@name='username']").focus();

        });

		

		/*

		$("#sectionLink_Ack").click( function() {			// input[@type="radio"]

            $(this).next("#dropDownSect_Ack").slideToggle(600);

			$("input[@name='tag_id']").focus();

        });

		*/

		

        // Update chat when the form is used:

        $('#btnCalcShipping').click(function(){

			qty = $("input[@name='packs']").get(0).value;

			

			if ( $("input[@name='sameBilling']").is(":checked") ) {

				address = $("input[@name='shipAddress']").get(0).value;

				city = $("input[@name='shipCity']").get(0).value;

				provState = $("select[@name='shipProvState']").val();

				country = $("select[@name='shipCountry']").val();

				postalCode = $("input[@name='shipAddressCode']").get(0).value;

			} else {

				address = $("input[@name='billAddress']").get(0).value;

				city = $("input[@name='billCity']").get(0).value;

				provState = $("select[@name='billProvState']").val();

				country = $("select[@name='billCountry']").val();

				postalCode = $("input[@name='billAddressCode']").get(0).value;

			}

			

			errResponse = 'Please ensure the Quantity of Tag Packages, Address, City, Province/State, Country and Postal/Zip Code fields are populated. <select name="shippingMethod"><option></option></select>';

		

			//alert(city);

			//if ( $('input[@name=sameBilling]').checked() ) {

			//	alert('checked');

			//}

			if ( address == "" || city == "" || provState == "" || country == "" || postalCode == "" || qty == "" )

				$("#calcShipping").html( errResponse );

			else {

				//$("#calcShipping").html("packs: "+qty+"<br />city:"+ city +"<br />country: "+country+"<br />address: "+address+"<br />postal: "+postalCode+"<br />provState: "+provState);

				$("#calcShipping").html('<img src="img/loading.gif" /> &nbsp; Please wait...');

				$("#calcShipping").load('cls/ShippingDO.php', {Address: address, City: city, ProvState: provState, Country: country, PostalCode: postalCode, Qty: qty});

			}

			if ( $("#calcShipping").is(':visible') ) {

				// nothing

			} else {

				$("#calcShipping").slideToggle(200);

			}

			/*

			element = $('input[@name=input]').get(0);

			mymessage   = element.value;

			myname = $('input[@name=name]').get(0).value;

			$.post('jq_chat.php', {message: mymessage, name: myname});

			element.value = '';

			return false;

			*/

        });

        //updateChat();

		

		$("input[@name='packs']").change(function(){

			$("#calcShipping").html('<select name="shippingMethod"><option></option></select>');

			$("#calcShipping").hide();

		});

		

		$("input[@name='shipAddressCode']").change(function(){

			$("#calcShipping").html('<select name="shippingMethod"><option></option></select>');

			$("#calcShipping").hide();

		});

		$("select[@name='shipCountry']").change(function(){

			$("#calcShipping").html('<select name="shippingMethod"><option></option></select>');

			$("#calcShipping").hide();

		});

		$("input[@name='billAddressCode']").change(function(){

			$("#calcShipping").html('<select name="shippingMethod"><option></option></select>');

			$("#calcShipping").hide();

		});

		$("select[@name='billCountry']").change(function(){

			$("#calcShipping").html('<select name="shippingMethod"><option></option></select>');

			$("#calcShipping").hide();

		});

		

		toggleClient();

		

		

      // Chat must also be updated when nothing is happening:

      // We have to provide a random number or else IE will not refresh properly:



	}

); 

	

function toggleClient() {

	$("#calcShipping").hide();

	$("#dropDownSect_Login").hide();

	$("#dropDownSect_Ack").hide(); 

	$("#slideInviteFriend").hide(); 

	$("#helpWriteAck").hide();

	$("#mapHelp").hide();

}



function addPunctuation( ele, ln ) {

	var el = ele;

	if ( ele[ln] != "." && ele[ln] != "!" )

		el += ".  ";

	if ( ele[ln] == "." || ele[ln] == "!" )

		el += "  ";

	return el;

}
