$().ready(function(){
	
	/////////////////////////////////////
	////////// GLOBALS & SETUP //////////
	/////////////////////////////////////

	var cururl = window.location.pathname;
	if(cururl=="/myprofile"){ storeSavedProfileValues(); getProfileData(); }
	
	$.ajaxSetup ({
		cache: false
	});

	////////////////////////////
	////////// FOOTER //////////
	////////////////////////////

	var $footer_icon = $("#footer_icon");

	$footer_icon.css({opacity:0, width:"162px", height:"121px", top:"-50px"});
	
	$footer_icon.animate({opacity:0.2, top:"0px"}, {duration:500});
	
	$footer_icon.hover(
			function(){
				$footer_icon.stop().animate({opacity:1, width:"324px", height:"242px", top:"-60px"}, {duration:100});
			},
			function(){
				$footer_icon.stop().animate({opacity:0.2, width:"162px", height:"121px", top:"0px"}, {duration:300});
			}
		);

	////////////////////////////
	////////// BANNER //////////
	////////////////////////////

	$(".banner_container").css({opacity:0});
	$("#banner01").css({opacity:1, display:"block"});

	$(".banner_thumb").click(function()
	{
		var id = "#"+this.id.replace("_thumb", "");
		$(".banner_thumb").removeClass("banner_thumb_active");
		$("#"+this.id).addClass("banner_thumb_active");
		$(".banner_container").stop().animate({opacity:0}, {duration: 500, complete: function()
		{
			$(".banner_container").css({display:"none"});
			$(id).css({display:"block"});
			$(id).stop().animate({opacity:1}, {duration:500});
		} });
	});

	$("#banner_button").toggle(
	function()
	{
		var id = "#"+this.id;
		$(id).text("Show Banner");
		$("#banner").animate({height:"0px"});
	},
	function()
	{
		var id = "#"+this.id;
		$(id).text("Hide Banner");
		$("#banner").animate({height:"473px"});
	});

	//////////////////////////////////
	////////// PRODUCT PAGE //////////
	//////////////////////////////////

	$(".prod_btn").click(function()
	{
		productSwitcher(this.id)
	});

	productSwitcher("prod_gift");

	function productSwitcher(id)
		{
		
		$("#"+id+"_gallery_icon").removeClass("btn_icon_plus").addClass("btn_icon_minus");
		$("#"+id+"_gallery_container").show();
		$("#"+id+"_specs_icon").removeClass("btn_icon_minus").addClass("btn_icon_plus");
		$("#"+id+"_specs_container").hide();
		
		if ( $("#"+id+"_container").is(':visible')) { return; }
		else
		{
			var ajax_load = "<span style='display: block; text-align:center;'><img src='../images/loadingicon.gif' alt='loading...' style='position:relative;top:5px;' /> Loading Product Enquiry Form...";
			
			$(".product_list li").removeClass("prod_btn_active").addClass("prod_btn");
			$("#"+id).removeClass("prod_btn").addClass("prod_btn_active");
			$(".product_container").stop().animate({left:"-1000px"}, 250, function(){
					$(".product_container").hide();
					$(".prod_specs_container").hide();
					$(".prod_order_container").hide();
					$("#"+id+"_container").show();
					$("#"+id+"_enquiry").html(ajax_load);
				}).animate({left:"0px"}, 250);
		}			

	}

	////////////////////////////////////////////////////
	////////// PRODUCT PANEL - GALLERY LOADER //////////
	////////////////////////////////////////////////////

	$(".gallery_btn").click(
	function(){

		var id = this.id;
		
		if ( $("#"+id+"_container").is(':visible'))
		{
			$("#"+id+"_icon").removeClass("btn_icon_minus").addClass("btn_icon_plus");
			$("#"+id+"_container").slideUp(250);
		}
		else
		{
			$(".btn_icon_minus").removeClass("btn_icon_minus").addClass("btn_icon_plus");
			$("#"+id+"_icon").removeClass("btn_icon_plus").addClass("btn_icon_minus");
			$(".prod_order_container").slideUp(250);
			$(".prod_specs_container").slideUp(250);
			$("#"+id+"_container").slideDown(250);
		}
	});

	//////////////////////////////////////////////////
	////////// PRODUCT PANEL - SPECS LOADER //////////
	//////////////////////////////////////////////////

	$(".cardspecs_btn").click(
	function(){

		var id = "#"+this.id;
		
		if ( $(id+"_container").is(':visible'))
		{
			$(id+"_icon").removeClass("btn_icon_minus").addClass("btn_icon_plus");
			$(id+"_container").slideUp(250);
		}
		else
		{
			$(".btn_icon_minus").removeClass("btn_icon_minus").addClass("btn_icon_plus");
			$(id+"_icon").removeClass("btn_icon_plus").addClass("btn_icon_loading");
			
			var speclist = "";
			var specs = new Array();
			specs[0] = "<li><a href='/files/card_specs/SP_Card_Spec_Standard.pdf'>Standard Card</a></li>";
			specs[1] = "<li><a href='/files/card_specs/SP_Card_Spec_Standard_with_Mag_Stripe.pdf'>Standard Card with Mag Stripe</a></li>";
			specs[2] = "<li><a href='/files/card_specs/SP_Card_Spec_Standard_with_Scratch_Off_Panel.pdf'>Standard Card with Scratch-Off Panel</a></li>";
			specs[3] = "<li><a href='/files/card_specs/SP_Card_Spec_Standard_with_Lanyard_Hole.pdf'>Standard Card with Lanyard Hole</a></li>";
			specs[4] = "<li><a href='/files/card_specs/SP_Card_Spec_Keytag_2up.pdf'>Keytag - 2up</a></li>";
			specs[5] = "<li><a href='/files/card_specs/SP_Card_Spec_Keytag_3up.pdf'>Keytag - 3up</a></li>";
			specs[6] = "<li><a href='/files/card_specs/SP_Card_Spec_Keytag_Dual_Card1.pdf'>Keytag - Dual Card 1</a></li>";
			specs[7] = "<li><a href='/files/card_specs/SP_Card_Spec_Keytag_Dual_Card2.pdf'>Keytag - Dual Card 2</a></li>";
			specs[8] = "<li><a href='/files/card_specs/SP_Card_Spec_Keytag_Mini_Card.pdf'>Keytag - Mini Card</a></li>";
			specs[9] = "<li><a href='/files/card_specs/SP_Card_Spec_Keytag_Special.pdf'>Keytag - Special</a></li>";
			specs[10] = "<li><a href='/files/card_specs/SP_Card_Spec_Contact_IC.pdf'>Contact IC Card</a></li>";

			switch (id)
			{
				case '#prod_gift_specs':
					speclist = specs[0]+specs[1]+specs[10];
					break;
				case '#prod_membership_specs':
					speclist = specs[0]+specs[1];
					break;
				case '#prod_reward_specs':
					speclist = specs[0]+specs[1];
					break;
				case '#prod_business_specs':
					speclist = specs[0];
					break;
				case '#prod_licence_specs':
					speclist = specs[0]+specs[1]+specs[10];
					break;
				case '#prod_telephone_specs':
					speclist = specs[0]+specs[10];
					break;
				case '#prod_library_specs':
					speclist = specs[0]+specs[4]+specs[5]+specs[6]+specs[7];
					break;
				case '#prod_keytag_specs':
					speclist = specs[4]+specs[5]+specs[6]+specs[7]+specs[8];
					break;
				case '#prod_id_specs':
					speclist = specs[0]+specs[1];
					break;
				case '#prod_luggage_specs':
					speclist = specs[0]+specs[4];
					break;
				case '#prod_blank_specs':
					speclist = specs[0]+specs[1]+specs[2]+specs[3]+specs[4]+specs[5]+specs[6]+specs[7]+specs[8]+specs[9]+specs[10];
					break;
				case '#prod_preprinted_specs':
					speclist = specs[0]+specs[1]+specs[4]+specs[5];
					break;
				case '#prod_specialty_specs':
					speclist = specs[0]+specs[1]+specs[10]+specs[4]+specs[5]+specs[6]+specs[7];
					break;
			}
			speclist = "<p>The following card specifications are available for this card type:</p><ul>"+speclist+"</ul>";

			$(id+"_container").html(speclist);
			$(id+"_icon").removeClass("btn_icon_loading").addClass("btn_icon_minus");
			$(id+"_container").slideDown(250);
			$(".prod_gallery_container").slideUp(250);
			
		}
	});

	////////////////////////////////////////////////////
	////////// PRODUCT PANEL - ENQUIRY LOADER //////////
	////////////////////////////////////////////////////
	
	$(".prodorder_btn").click(function(){
		
		var id = getElementID(this);
		var cardtype = '';
		
		switch (id)
		{
		case 'prod_gift_order':
			cardtype = 'gift';
			break;
		case 'prod_membership_order':
			cardtype = 'membership';
			break;
		case 'prod_reward_order':
			cardtype = 'rewards';
			break;
		case 'prod_business_order':
			cardtype = 'business';
			break;
		case 'prod_licence_order':
			cardtype = 'licence';
			break;
		case 'prod_ticketing_order':
			cardtype = 'ticketing';
			break;
		case 'prod_telephone_order':
			cardtype = 'telephone';
			break;
		case 'prod_library_order':
			cardtype = 'library';
			break;
		case 'prod_keytag_order':
			cardtype = 'keytag';
			break;
		case 'prod_id_order':
			cardtype = 'id';
			break;
		case 'prod_luggage_order':
			cardtype = 'luggage';
			break;
		case 'prod_blank_order':
			cardtype = 'blank';
			break;
		case 'prod_preprinted_order':
			cardtype = 'preprinted';
			break;
		default:
			cardtype = '';
		}

		if ( $("#"+id+"_container").is(':visible'))
		{
			$("#"+id+"_icon").removeClass("btn_icon_minus").addClass("btn_icon_plus");
			$("#"+id+"_container").slideUp(250);
		}
		else
		{
			$(".btn_icon_minus").removeClass("btn_icon_minus").addClass("btn_icon_plus");
			$("#"+id+"_icon").removeClass("btn_icon_plus").addClass("btn_icon_loading");

			if(loggedin()=="true")
			{
				var url = "../includes/function_cardenquiry.php?cardtype="+cardtype;
				$("#"+id+"_container").load(url, function(){
					$("#"+id+"_icon").removeClass("btn_icon_loading").addClass("btn_icon_minus");
					$("#"+id+"_container").slideDown(250);
					$(".prod_specs_container").slideUp(250);
					$(".prod_gallery_container").slideUp(250);
				});
			} else {
				$("#"+id+"_icon").removeClass("btn_icon_loading").addClass("btn_icon_minus");
				$("#"+id+"_container").html("Please <a href='/login'>login</a> or <a href='/register'>register</a> to submit a card enquiry").slideDown(250);
				$(".prod_specs_container").slideUp(250);
				$(".prod_gallery_container").slideUp(250);
			}
		}
	});

	/////////////////////////////////////
	////////// PRODUCT GALLERY //////////
	/////////////////////////////////////

	$("a.gallery_thumb").fancybox({
		'padding'		: 0,
		'overlayShow'	: true,
		'cyclic'		: true,
		'centerOnScroll': true,
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'		: 'swing',
		'easingOut'		: 'swing',
		'titlePosition'	: 'outside',
		'overlayColor'	: '#999',
		'overlayOpacity': 0.75
	});

	///////////////////////////////////////////////
	////////// GALLERY POP-UP ANIMATIONS //////////
	///////////////////////////////////////////////

	$(".gallery_thumb").hover(
		function() { showCard(this.id) }
		,
		function() { hideCard(this.id) }
	);

	function setGallery(){
		$("gallery_thumb").rotate('-10deg');
	}

	function showCard(id) {
			
		$(".gallery_thumb").rotate('-10deg');
		$("#"+id).removeAttr("title");
		$("#"+id).removeAttr("alt");
		$("#"+id).stop().animate({top: 0, rotate: '0deg'},{duration: 250});
	}
	
	function hideCard(id) {
		// $("#"+id).stop().animate({top: 50, rotate: '-10deg'},{duration: 500, easing: 'easeOutBounce'});
		
		$("#"+id).stop().animate({top: 50, rotate: '-10deg'},{duration: 500, easing: "easeOutBounce"});

	}

	/////////////////////////////////////////
	////////// GALLERY PAGE SLIDER //////////
	/////////////////////////////////////////

	$('#contentslider').ContentSlider({
		width  : '820px',
		height : '400px',
		speed  : 600,
		easing : 'easeOutQuad'
	});

	//////////////////////////////////
	////////// TEXT POP-UPS //////////
	//////////////////////////////////

	$("a.popuptext").fancybox({
		'padding'        : 0,
		'scrolling'      : 'auto',
		'width'          : 820,
		'height'         : 'auto',
		'autoscale'	     : false,
		'autoDimensions' : false,
		'overlayColor'   : '#999',
		'overlayOpacity' : 0.75
	});

	///////////////////////////////////////////
	////////// TOP BANNER ANIMATIONS //////////
	///////////////////////////////////////////

	$("#slider1").easySlider({
		auto         : true,
		continuous   : true,
		controlsShow : false,
		speed        : 1000,
		pause        : 2000
	});

	$("#slider2").easySlider({
		auto         : true,
		continuous   : true,
		controlsShow : false,
		speed        : 2000,
		pause        : 100
	});
	
	$("#slider3").easySlider({
		auto         : true,
		continuous   : true,
		controlsShow : false,
		speed        : 3000,
		pause        : 100
	});
	
	//////////////////////////////////////////////	
	////////// MEMBER REGISTRATION FORM //////////
	//////////////////////////////////////////////
	
	// disable page reload on form submit
	$("#register").submit(function(){ return false; });
	$("#loginform").submit(function(){ return false; });
	$("#forgotpassform").submit(function(){ return false; });
	
	$("#regfname").keyup(function(){ regfname(); });
	$("#reglname").keyup(function(){ reglname(); });
	$("#regcompany").keyup(function(){ regcompany(); });
	$("#regemail1").keyup(function(){ regemail1(); });
	$("#regemail2").keyup(function(){ regemail2() });
	$("#regpass1").keyup(function(){ regpass1() });
	$("#regpass2").keyup(function(){ regpass2() });
	$("#secq").keyup(function(){ secq() });
	$("#seca").keyup(function(){ seca() });
	$("#regterms").click(function(){ regterms() });
	$("#regreset").click(function(){ regreset() });
	$("#regsend").click(function(){ regsend() });

	////////////////////////////////////////
	////////// REG FORM FUNCTIONS //////////
	////////////////////////////////////////
	
	function regfname()
	{
		var u = $("#regfname").val();
		if ( u != '' )
			{
			var n = u.length;
			if ( n > 32 )
				{ 
					$("#regfnameMsg").inputResponse("Too long", "error").data("fname", false);
				} 
			else
				{
					$("#regfnameMsg").inputResponse("Okay", "ok").data("fname", true);
				}	
			}
		else
			{
				$("#regfnameMsg").inputResponse("This field is required", "error").data("fname", false);
			}
	}	

	function reglname()
	{
		var u = $("#reglname").val();
		if ( u != '' )
			{
			var n = u.length;
			if ( n > 32 )
				{ 
					$("#reglnameMsg").inputResponse("Too long", "error").data("lname", false);
				} 
			else
				{
					$("#reglnameMsg").inputResponse("Okay", "ok").data("lname", true);
				}	
			}
		else
			{
				$("#reglnameMsg").inputResponse("This field is required", "error").data("lname", false);
			}
	}	

	function regcompany()
	{
		var u = $("#regcompany").val();
		if ( u != '' )
			{
				$("#regcompanyMsg").inputResponse("Thank you", "ok");
			}
		else
			{
				$("#regcompanyMsg").inputResponse("", "clear");
			}
	}	

	function regemail1()
	{
		var e = $("#regemail1").val();
		
		if ( e != '' )
		{
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;	
			
			if(!emailReg.test(e))
			{	
					$("#regemail1Msg").inputResponse("Invalid email", "error").data("email1", false);
			}
			else
			{
					$("#regemail1Msg").inputResponse("Checking email", "loading");
					
					if(emailExists(e)) {
						$("#regemail1Msg").inputResponse("Email is already registered", "error").data("email1", false);
					}
					else
					{
						$("#regemail1Msg").inputResponse("Okay", "ok").data("email1", true);
					}
			}
		} 
		else
			{
				$("#regemail1Msg").inputResponse("This field is required", "error").data("email1", false);
			}
	}

	function regemail2()
		{
			var e1 = $("#regemail1").val();
			var e1valid = $("#regemail1Msg").data("email1");
			var e2 = $("#regemail2").val();

			if ( e1 != '' && e2 != '' )
				{
				if (e1valid)
					{
						if (e1 == e2)
							{
							$("#regemail2Msg").inputResponse("Emails match", "ok").data("email2", true);
							}
						else
							{
							$("#regemail2Msg").inputResponse("Emails do not match", "error").data("email2", false);
						} 
					}
					else
					{
						$("#regemail2Msg").inputResponse("First email needs correcting", "error").data("email2", false);
					}
				}
				else
					{
						$("#regemail2Msg").inputResponse("This field is required", "error").data("email2", false);
					}
		}

	function regpass1()
		{
			var p = $("#regpass1").val();
			if ( p != '' )
				{
				var n = p.length;
				if ( n > 5 )
					{
					$("#regpass1Msg").inputResponse("Okay", "ok").data("pass1", true);
					}
				else
					{
					$("#regpass1Msg").inputResponse("Must be at least 6 characters", "error").data("pass1", false);
					}	 
				}
			else
				{
					$("#regpass1Msg").inputResponse("This field is required", "error").data("pass1", false);
				}
		}

	function regpass2()
		{
			var p1 = $("#regpass1").val();
			var p2 = $("#regpass2").val();
			if ( p1 != '' && p2 != '' )
				{		
				if (p1 == p2)
					{
					$("#regpass2Msg").inputResponse("Passwords match", "ok").data("pass2", true);
					//return true;
					}
				else
					{
					$("#regpass2Msg").inputResponse("Passwords do not match", "error").data("pass2", false);
					//return false;
					}
				}
				else
					{
						$("#regpass2Msg").inputResponse("This field is required", "error").data("pass2", false);
					}
		}

	function secq()
	{
		var secq = $("#secq").val();
		if ( secq != '' )
			{
				$("#secqMsg").inputResponse("Okay", "ok").data("secq", true);
			}
		else
			{
				$("#secqMsg").inputResponse("This field is required", "error").data("secq", false);
			}
	}	

	function seca()
	{
		var seca = $("#seca").val();
		if ( seca != '' )
			{
				$("#secaMsg").inputResponse("Okay", "ok").data("seca", true);
			}
		else
			{
				$("#secaMsg").inputResponse("This field is required", "error").data("seca", false);
			}
	}	

	function regterms()
		{
			if ($('#regterms').is(':checked'))
			{
				$("#regtermsMsg").inputResponse("", "ok").data("terms", true);
			} 
			else
			{ 
				$("#regtermsMsg").inputResponse("", "error").data("terms", false);
			}
		}
	
	function regsend() 
		{
			var errnotice = "";			
			var a = $("#regfnameMsg").data("fname");
			var b = $("#reglnameMsg").data("lname");
			var c = $("#regemail1Msg").data("email1");
			var d = $("#regemail2Msg").data("email2");
			var e = $("#regpass1Msg").data("pass1");
			var f = $("#regpass2Msg").data("pass2");
			var g = $("#secqMsg").data("secq");
			var h = $("#secaMsg").data("seca");
			var i = $("#regtermsMsg").data("terms");

			if (!a) { regfname(); errnotice = errnotice + "First Name, "; }
			if (!b) { reglname(); errnotice = errnotice + "Last Name, "; }
			if (!c) { regemail1(); errnotice = errnotice + "Email, "; }
			if (!d) { regemail2(); errnotice = errnotice + "Email Again, "; }
			if (!e) { regpass1(); errnotice = errnotice + "Password, "; }
			if (!f) { regpass2(); errnotice = errnotice + "Password Again, "; }
			if (!g) { secq(); errnotice = errnotice + "Security Question, "; }
			if (!h) { seca(); errnotice = errnotice + "Security Answer, "; }
			if (!i) { regterms(); errnotice = errnotice + "Terms, "; }

			errnotice = errnotice.substring(0, errnotice.length-2);

			if (a && b && c && d && e && f && g && h && i )
				{
					$("#formnotice").text("Everything checks out").css({color:'#009900'}).slideDown(200);
					submitReg();
				}
			else
				{
					$("#formnotice").html("<strong>Some fields need correcting:</strong><br>"+errnotice).css({color:'#cc3300'}).slideDown(200);
				}
		}
	
	function regreset()
		{
			$("#regfnameMsg").html('').data("fname",false);
			$("#reglnameMsg").html('').data("lname",false);
			$("#regcompanyMsg").html('');
			$("#regemail1Msg").html('').data("email1",false);
			$("#regemail2Msg").html('').data("email2",false);
			$("#regpass1Msg").html('').data("pass1",false);
			$("#regpass2Msg").html('').data("pass2",false);
			$("#secqMsg").html('').data("secq",false);
			$("#secaMsg").html('').data("seca",false);
			$("#regtermsMsg").html('').data("terms",false);
			$("#formnotice").slideUp(200);
			errnotice = "";
		}

	function submitReg()
	{
		var fname = $("#regfname").val();
		var lname = $("#reglname").val();
		var company = $("#regcompany").val();
		var email = $("#regemail1").val();
		var pass = $("#regpass1").val();
		var secq = $("#secq").val();
		var seca = $("#seca").val();
		
		if ($('#regresller').is(':checked')) { var accounttyperequested = "reseller"; } else { var accounttyperequested = "enduser"; }
		if ($('#regoptin').is(':checked')) { var optin = 1; } else { var optin = 0; }
		
		var notice = "Thank you "+fname+" "+lname+". Submitting your application now...";
		$("#formnotice").text(notice).css({color:'#009900'});
		
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
			{
				alert ("Browser does not support HTTP Request")
				return
			}
		
		var url="../includes/body_registersubmit.php";
		url=url+"?fname="+fname+"&lname="+lname+"&company="+company+"&email="+email+"&pass="+pass+"&secq="+secq+"&seca="+seca+"&optin="+optin;
		url=url+"&sid="+Math.random()
		xmlHttp.onreadystatechange=function () 
			{
				var rt = xmlHttp.responseText;
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
				{
					$("#regform").css({display:"none"});
					$("#regform").fadeIn("fast").html(rt);
				} 
			} 
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
	}

	//////////////////////////////////////////////
	////////// PROFILE UPDATE FUNCTIONS //////////
	//////////////////////////////////////////////

	$("#myprofile").submit(function(){ return false; }); // disable page reload on form submit
	$("#myprofile_update").click(function(){ profUpdate(); });
	$("#myprofile_revert").click(function(){ setTimeout( "window.location = '/myprofile'", 1); });

	function storeSavedProfileValues() {
	
		window.salutation = $("#myprofile #salutation").val();
		window.fname      = $("#myprofile #fname").val();
		window.lname      = $("#myprofile #lname").val();
		window.company    = $("#myprofile #company").val();
		window.email      = $("#myprofile #email").val();
		window.phone      = $("#myprofile #phone").val();
		window.mobile     = $("#myprofile #mobile").val();
		window.pass1      = $("#myprofile #pass1").val();
		window.pass2      = $("#myprofile #pass2").val();
		window.secq       = $("#myprofile #securityquestion").val();
		window.seca       = $("#myprofile #securityanswer").val();
		window.optin      = $("#myprofile #optin").val();

		window.billing_addressline1   = $("#myprofile #billing_addressline1").val();
		window.billing_addressline2   = $("#myprofile #billing_addressline2").val();
		window.billing_suburb         = $("#myprofile #billing_suburb").val();
		window.billing_city           = $("#myprofile #billing_city").val();
		window.billing_state          = $("#myprofile #billing_state").val();
		window.billing_postcode       = $("#myprofile #billing_postcode").val();
		window.billing_country        = $("#myprofile #billing_country").val();

		window.delivery_addressline1  = $("#myprofile #delivery_addressline1").val();
		window.delivery_addressline2  = $("#myprofile #delivery_addressline2").val();
		window.delivery_suburb        = $("#myprofile #delivery_suburb").val();
		window.delivery_city          = $("#myprofile #delivery_city").val();
		window.delivery_state         = $("#myprofile #delivery_state").val();
		window.delivery_postcode      = $("#myprofile #delivery_postcode").val();
		window.delivery_country       = $("#myprofile #delivery_country").val();
	}

	function getProfileData() {

		storeSavedProfileValues();

		if(window.salutation != "") { $("#salutation_msg").data("salutation", true); }	else { $("#salutation_msg").data("salutation", false); }
		if(window.fname != "")      { $("#fname_msg").data("fname", true); }			else { $("#fname_msg").data("fname", false); }
		if(window.lname != "")      { $("#lname_msg").data("lname", true); }			else { $("#lname_msg").data("lname", false); }
		if(window.company != "")    { $("#company_msg").data("company", true); }		else { $("#company_msg").data("company", false); }
		if(window.email != "")      { $("#email_msg").data("email", true); }			else { $("#email_msg").data("email", false); }
		if(window.phone != "")      { $("#phone_msg").data("phone", true); }			else { $("#phone_msg").data("phone", false); }
		if(window.mobile != "")     { $("#mobile_msg").data("mobile", true); }			else { $("#mobile_msg").data("mobile", false); }
		if(window.secq != "")       { $("#securityquestion_msg").data("secq", true); }	else { $("#securityquestion_msg").data("secq", false); }
		if(window.seca != "")       { $("#securityanswer_msg").data("seca", true); }	else { $("#securityanswer_msg").data("seca", false); }
		if(window.optin != "")      { $("#optin_msg").data("optin", true); }			else { $("#optin_msg").data("optin", false); }

		$("#pass1_msg").data("pass1", true);
		$("#pass2_msg").data("pass2", true);

		if(window.billing_addressline1 != "")  { $("#billing_addressline1_msg").data("billing_addressline1", true); } else { $("#billing_addressline1_msg").data("billing_addressline1", false); }
		if(window.billing_addressline2 != "")  { $("#billing_addressline2_msg").data("billing_addressline2", true); } else { $("#billing_addressline2_msg").data("billing_addressline2", false); }
		if(window.billing_suburb != "")        { $("#billing_suburb_msg").data("optin", true); } else { $("#billing_suburb_msg").data("billing_suburb", false); }
		if(window.billing_city != "")          { $("#billing_city_msg").data("billing_city", true); } else { $("#billing_city").data("billing_city", false); }
		if(window.billing_state != "")         { $("#billing_state_msg").data("billing_state", true); } else { $("#billing_state_msg").data("billing_state", false); }
		if(window.billing_postcode != "")      { $("#billing_postcode_msg").data("billing_postcode", true); } else { $("#billing_postcode_msg").data("billing_postcode", false); }
		if(window.billing_country != "")       { $("#billing_country_msg").data("billing_country", true); } else { $("#billing_country_msg").data("billing_country", false); }

		if(window.delivery_addressline1 != "") { $("#delivery_addressline1_msg").data("delivery_addressline1", true); } else { $("#delivery_addressline1_msg").data("delivery_addressline1", false); }
		if(window.delivery_addressline2 != "") { $("#delivery_addressline2_msg").data("delivery_addressline2", true); } else { $("#delivery_addressline2_msg").data("delivery_addressline2", false); }
		if(window.delivery_suburb != "")       { $("#delivery_suburb_msg").data("optin", true); } else { $("#delivery_suburb_msg").data("delivery_suburb", false); }
		if(window.delivery_city != "")         { $("#delivery_city_msg").data("delivery_city", true); } else { $("#delivery_city").data("delivery_city", false); }
		if(window.delivery_state != "")        { $("#delivery_state_msg").data("delivery_state", true); } else { $("#delivery_state_msg").data("delivery_state", false); }
		if(window.delivery_postcode != "")     { $("#delivery_postcode_msg").data("delivery_postcode", true); } else { $("#delivery_postcode_msg").data("delivery_postcode", false); }
		if(window.delivery_country != "")      { $("#delivery_country_msg").data("delivery_country", true); } else { $("#delivery_country_msg").data("delivery_country", false); }
	}

	$("#myprofile #salutation").keyup(function(){ profsalutation(); })
	$("#myprofile #fname").keyup(function(){ proffname(); })
	$("#myprofile #lname").keyup(function(){ proflname(); })
	$("#myprofile #company").keyup(function(){ profcompany(); })
	$("#myprofile #email").blur(function(){ profemail(); })
	$("#myprofile #phone").keyup(function(){ profphone(); })
	$("#myprofile #mobile").keyup(function(){ profmobile(); })
	$("#myprofile #pass1").keyup(function(){ profpass1(); profpass2(); })
	$("#myprofile #pass2").keyup(function(){ profpass2(); profpass1(); })
	$("#myprofile #securityquestion").keyup(function(){ profsecurityquestion(); })
	$("#myprofile #securityanswer").keyup(function(){ profsecurityanswer(); })
	$("#myprofile #optin").click(function(){ profoptin(); })

	$("#myprofile #billing_addressline1").keyup(function(){ profbilling_addressline1(); })
	$("#myprofile #billing_addressline2").keyup(function(){ profbilling_addressline2(); })
	$("#myprofile #billing_suburb").keyup(function(){ profbilling_suburb(); })
	$("#myprofile #billing_city").keyup(function(){ profbilling_city(); })
	$("#myprofile #billing_state").keyup(function(){ profbilling_state(); })
	$("#myprofile #billing_postcode").keyup(function(){ profbilling_postcode(); })
	$("#myprofile #billing_country").keyup(function(){ profbilling_country(); })

	$("#myprofile #delivery_addressline1").keyup(function(){ profdelivery_addressline1(); })
	$("#myprofile #delivery_addressline2").keyup(function(){ profdelivery_addressline2(); })
	$("#myprofile #delivery_suburb").keyup(function(){ profdelivery_suburb(); })
	$("#myprofile #delivery_city").keyup(function(){ profdelivery_city(); })
	$("#myprofile #delivery_state").keyup(function(){ profdelivery_state(); })
	$("#myprofile #delivery_postcode").keyup(function(){ profdelivery_postcode(); })
	$("#myprofile #delivery_country").keyup(function(){ profdelivery_country(); })

	function resetProfileMessages()
	{
		$("#salutation_msg").inputResponse("", "clear");
		$("#fname_msg").inputResponse("", "clear");
		$("#lname_msg").inputResponse("", "clear");
		$("#company_msg").inputResponse("", "clear");
		$("#email_msg").inputResponse("This is your login ID", "none");
		$("#phone_msg").inputResponse("", "clear");
		$("#mobile_msg").inputResponse("", "clear");
		$("#pass1_msg").inputResponse("Leave blank to keep your current password", "none");
		$("#pass2_msg").inputResponse("Passwords must match", "none");
		$("#securityquestion_msg").inputResponse("Example: My first pets name?", "none");
		$("#securityanswer_msg").inputResponse("Example: Fluffy", "none");
		
		$("#billing_addressline1_msg").inputResponse("", "clear");
		$("#billing_addressline2_msg").inputResponse("", "clear");
		$("#billing_suburb_msg").inputResponse("", "clear");
		$("#billing_city_msg").inputResponse("", "clear");
		$("#billing_state_msg").inputResponse("", "clear");
		$("#billing_postcode_msg").inputResponse("", "clear");
		$("#billing_country_msg").inputResponse("", "clear");

		$("#delivery_addressline1_msg").inputResponse("", "clear");
		$("#delivery_addressline2_msg").inputResponse("", "clear");
		$("#delivery_suburb_msg").inputResponse("", "clear");
		$("#delivery_city_msg").inputResponse("", "clear");
		$("#delivery_state_msg").inputResponse("", "clear");
		$("#delivery_postcode_msg").inputResponse("", "clear");
		$("#delivery_country_msg").inputResponse("", "clear");
	}

	function enableUpdateButton(){
		$("#myprofile_update").removeAttr("disabled");
		$("#myprofile_revert").removeAttr("disabled");
	}
	function disableUpdateButton(){
		$("#myprofile_update").attr("disabled");
		$("#myprofile_revert").attr("disabled");
	}

	function profsalutation(){
			var salutation = $("#myprofile #salutation").val();
			if (salutation=='') {
				$("#salutation_msg").text("");
			} else if (salutation==window.salutation) {
				$("#salutation_msg").inputResponse("", "clear");
			} else {
				$("#salutation_msg").inputResponse("Salutation will be updated", "ok");
				enableUpdateButton();
			}
	}
	function proffname(){
			var fname = $("#myprofile #fname").val();
			var lname = $("#myprofile #lname").val();
			if (fname=='')
			{
				$("#fname_msg").inputResponse("First name is required", "error").data("fname", false);
			}
			else if (fname==window.fname)
			{
				$("#fname_msg").inputResponse("", "clear").data("fname", true);
			}
			else
			{
				$("#fname_msg").inputResponse("First name will be updated", "ok").data("fname", true);
				enableUpdateButton();
			}
				$("#memberdisplayname").text(fname+" "+lname);
	}
	function proflname(){
			var fname = $("#myprofile #fname").val();
			var lname = $("#myprofile #lname").val();
			if (lname=='') {
				$("#lname_msg").inputResponse("Last name is required", "error").data("lname", false);
			} else if (lname==window.lname) {
				$("#lname_msg").inputResponse("", "clear").data("lname", true);
			} else {
				$("#lname_msg").inputResponse("Last name will be updated", "ok").data("lname", true);
				enableUpdateButton();
			}
				$("#memberdisplayname").text(fname+" "+lname);
	}
	function profcompany(){
			var company = $("#myprofile #company").val();
			if (company=='' && company==window.company) {
				$("#company_msg").inputResponse("", "clear");
			} else if (company==window.company) {
				$("#company_msg").inputResponse("", "clear");
			} else {
				$("#company_msg").inputResponse("Company will be updated", "ok");
				enableUpdateButton();
			}
	}
	function profemail(){
			var email = $("#myprofile #email").val();
			if (email=='')
			{
				$("#email_msg").inputResponse("Email is required", "error").data("email", false);
			}
			else if (email==window.email)
			{
				$("#email_msg").inputResponse("This is your login ID", "none").data("email", true);
			}
			else
			{
				if(emailExists(email))
				{
					$("#email_msg").inputResponse("This email is already in use", "error").data("email", false);
				}
				else if (!validateEmail(email))
				{
					$("#email_msg").inputResponse("This email is invalid", "error").data("email", false);
				}
				else
				{
					$("#email_msg").inputResponse("Email will be updated", "ok").data("email", true);
					enableUpdateButton();
				}
			}

	}
	function profphone(){
			var phone = $("#myprofile #phone").val();
			if (phone=='') {
				$("#phone_msg").inputResponse("", "clear");
			}
			else if (phone==window.phone)
			{
				$("#phone_msg").inputResponse("", "clear");
			}
			else
			{
				$("#phone_msg").inputResponse("Phone will be updated", "ok");
				enableUpdateButton();
			}
	}
	function profmobile(){
			var mobile = $("#myprofile #mobile").val();
			if (mobile=='')
			{
				$("#mobile_msg").inputResponse("", "clear");
			}
			else if (mobile==window.mobile)
			{
				$("#mobile_msg").inputResponse("", "clear");
			}
			else
			{
				$("#mobile_msg").inputResponse("Mobile will be updated", "ok");
				enableUpdateButton();
			}
	}
	function profpass1(){
			var pass1 = $("#myprofile #pass1").val();
			if (pass1!='')
			{
				$("#pass1_msg").inputResponse("Password will be updated", "ok").data("pass1", true);
				enableUpdateButton();
			}
			else
			{
				$("#pass1_msg").inputResponse("Leave blank to keep your current password", "none").data("pass1", true);
			}
	}
	function profpass2(){
			var pass1 = $("#myprofile #pass1").val();
			var pass2 = $("#myprofile #pass2").val();
			if(pass1==''&&pass2=='')
			{
				$("#pass2_msg").inputResponse("Passwords must match", "none").data("pass2", true);
			}
			else if (pass1==pass2)
			{
				$("#pass2_msg").inputResponse("Passwords match", "ok").data("pass2", true);
				enableUpdateButton();
			}
			else if (pass1!=pass2)
			{
				$("#pass2_msg").inputResponse("Passwords do not match", "error").data("pass2", false);
			}
	}
	function profsecurityquestion(){
			var securityquestion = $("#myprofile #securityquestion").val();
			if (securityquestion=='') {
				$("#securityquestion_msg").inputResponse("Security question is required", "ok").data("secq", false);
			} else if (securityquestion==window.secq) {
				$("#securityquestion_msg").inputResponse("Example: My first pets name?", "none").data("secq", true);
			} else {
				$("#securityquestion_msg").inputResponse("Security question will be updated", "ok").data("secq", true);
				enableUpdateButton();
			}
	}
	function profsecurityanswer(){
			var securityanswer = $("#myprofile #securityanswer").val();
			if (securityanswer=='') {
				$("#securityanswer_msg").inputResponse("Security answer is required", "error").data("seca", false);
			} else if (securityanswer==window.seca) {
				$("#securityanswer_msg").inputResponse("Example: Fluffy", "none").data("seca", true);
			} else {
				$("#securityanswer_msg").inputResponse("Security answer with be updated", "ok").data("seca", true);
				enableUpdateButton();
			}
	}
	function profoptin(){
				$("#optin_msg").inputResponse("Mailing list preference will be updated", "ok");
				enableUpdateButton();
	}
	function profbilling_addressline1(){
			var billing_addressline1 = $("#myprofile #billing_addressline1").val();
			if (billing_addressline1=='') {
				$("#billing_addressline1_msg").inputResponse("", "clear");
			}
			else if (billing_addressline1==window.billing_addressline1)
			{
				$("#billing_addressline1_msg").inputResponse("", "clear");
			}
			else
			{
				$("#billing_addressline1_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profbilling_addressline2(){
			var billing_addressline2 = $("#myprofile #billing_addressline2").val();
			if (billing_addressline2=='') {
				$("#billing_addressline2_msg").inputResponse("", "clear");
			}
			else if (billing_addressline2==window.billing_addressline2)
			{
				$("#billing_addressline2_msg").inputResponse("", "clear");
			}
			else
			{
				$("#billing_addressline2_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profbilling_suburb(){
			var billing_suburb = $("#myprofile #billing_suburb").val();
			if (billing_suburb=='') {
				$("#billing_suburb_msg").inputResponse("", "clear");
			}
			else if (billing_suburb==window.billing_suburb)
			{
				$("#billing_suburb_msg").inputResponse("", "clear");
			}
			else
			{
				$("#billing_suburb_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profbilling_city(){
			var billing_city = $("#myprofile #billing_city").val();
			if (billing_city=='') {
				$("#billing_city_msg").inputResponse("", "clear");
			}
			else if (billing_city==window.billing_city)
			{
				$("#billing_city_msg").inputResponse("", "clear");
			}
			else
			{
				$("#billing_city_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profbilling_state(){
			var billing_state = $("#myprofile #billing_state").val();
			if (billing_state=='') {
				$("#billing_state_msg").inputResponse("", "clear");
			}
			else if (billing_state==window.billing_state)
			{
				$("#billing_state_msg").inputResponse("", "clear");
			}
			else
			{
				$("#billing_state_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profbilling_postcode(){
			var billing_postcode = $("#myprofile #billing_postcode").val();
			if (billing_postcode=='') {
				$("#billing_postcode_msg").inputResponse("", "clear");
			}
			else if (billing_postcode==window.billing_postcode)
			{
				$("#billing_postcode_msg").inputResponse("", "clear");
			}
			else
			{
				$("#billing_postcode_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profbilling_country(){
			var billing_country = $("#myprofile #billing_country").val();
			if (billing_country=='') {
				$("#billing_country_msg").inputResponse("", "clear");
			}
			else if (billing_country==window.billing_country)
			{
				$("#billing_country_msg").inputResponse("", "clear");
			}
			else
			{
				$("#billing_country_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profdelivery_addressline1(){
			var delivery_addressline1 = $("#myprofile #delivery_addressline1").val();
			if (delivery_addressline1=='') {
				$("#delivery_addressline1_msg").inputResponse("", "clear");
			}
			else if (delivery_addressline1==window.delivery_addressline1)
			{
				$("#delivery_addressline1_msg").inputResponse("", "clear");
			}
			else
			{
				$("#delivery_addressline1_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profdelivery_addressline2(){
			var delivery_addressline2 = $("#myprofile #delivery_addressline2").val();
			if (delivery_addressline2=='') {
				$("#delivery_addressline2_msg").inputResponse("", "clear");
			}
			else if (delivery_addressline2==window.delivery_addressline2)
			{
				$("#delivery_addressline2_msg").inputResponse("", "clear");
			}
			else
			{
				$("#delivery_addressline2_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profdelivery_suburb(){
			var delivery_suburb = $("#myprofile #delivery_suburb").val();
			if (delivery_suburb=='') {
				$("#delivery_suburb_msg").inputResponse("", "clear");
			}
			else if (delivery_suburb==window.delivery_suburb)
			{
				$("#delivery_suburb_msg").inputResponse("", "clear");
			}
			else
			{
				$("#delivery_suburb_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profdelivery_city(){
			var delivery_city = $("#myprofile #delivery_city").val();
			if (delivery_city=='') {
				$("#delivery_city_msg").inputResponse("", "clear");
			}
			else if (delivery_city==window.delivery_city)
			{
				$("#delivery_city_msg").inputResponse("", "clear");
			}
			else
			{
				$("#delivery_city_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profdelivery_state(){
			var delivery_state = $("#myprofile #delivery_state").val();
			if (delivery_state=='') {
				$("#delivery_state_msg").inputResponse("", "clear");
			}
			else if (delivery_state==window.delivery_state)
			{
				$("#delivery_state_msg").inputResponse("", "clear");
			}
			else
			{
				$("#delivery_state_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profdelivery_postcode(){
			var delivery_postcode = $("#myprofile #delivery_postcode").val();
			if (delivery_postcode=='') {
				$("#delivery_postcode_msg").inputResponse("", "clear");
			}
			else if (delivery_postcode==window.delivery_postcode)
			{
				$("#delivery_postcode_msg").inputResponse("", "clear");
			}
			else
			{
				$("#delivery_postcode_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}
	function profdelivery_country(){
			var delivery_country = $("#myprofile #delivery_country").val();
			if (delivery_country=='') {
				$("#delivery_country_msg").inputResponse("", "clear");
			}
			else if (delivery_country==window.delivery_country)
			{
				$("#delivery_country_msg").inputResponse("", "clear");
			}
			else
			{
				$("#delivery_country_msg").inputResponse("", "ok");
				enableUpdateButton();
			}
	}

	function profUpdate() 
		{
			var errnotice = "";			
			var a = $("#fname_msg").data("fname");
			var b = $("#lname_msg").data("lname");
			var c = $("#email_msg").data("email");
			var d = $("#pass1_msg").data("pass1");
			var e = $("#pass2_msg").data("pass2");
			var f = $("#securityquestion_msg").data("secq");
			var g = $("#securityanswer_msg").data("seca");

			var j = $("#billing_addressline1_msg").data("billing_addressline1");
			var k = $("#billing_addressline2_msg").data("billing_addressline2");
			var l = $("#billing_suburb_msg").data("billing_suburb");
			var m = $("#billing_city_msg").data("billing_city");
			var n = $("#billing_state_msg").data("billing_state");
			var o = $("#billing_postcode_msg").data("billing_postcode");
			var p = $("#billing_country_msg").data("billing_country");

			var s = $("#delivery_addressline1_msg").data("delivery_addressline1");
			var t = $("#delivery_addressline2_msg").data("delivery_addressline2");
			var u = $("#delivery_suburb_msg").data("delivery_suburb");
			var v = $("#delivery_city_msg").data("delivery_city");
			var w = $("#delivery_state_msg").data("delivery_state");
			var x = $("#delivery_postcode_msg").data("delivery_postcode");
			var y = $("#delivery_country_msg").data("delivery_country");

			if (!a) { proffname(); errnotice = errnotice + "First Name, "; }
			if (!b) { proflname(); errnotice = errnotice + "Last Name, "; }
			if (!c) { profemail(); errnotice = errnotice + "Email, "; }
			if (!d) { profpass1(); errnotice = errnotice + "New Password, "; }
			if (!e) { profpass2(); errnotice = errnotice + "Confirm Password, "; }
			if (!f) { profsecurityquestion(); errnotice = errnotice + "Security Question, "; }
			if (!g) { profsecurityanswer(); errnotice = errnotice + "Security Answer, "; }

			errnotice = errnotice.substring(0, errnotice.length-2);

			if (a && b && c && d && e && f && g)
				{
					$("#formnotice").text("Everything checks out. Submitting profile update...").css({color:'#009900'}).slideDown(200);
					storeSavedProfileValues();
					submitProfUpdate();
					/* setTimeout( "window.location = '/myprofile'", 1); */
				}
			else
				{
					$("#formnotice").html("<strong>Some fields need correcting:</strong><br>"+errnotice).css({color:'#cc3300'}).slideDown(200);
				}
		}

function submitProfUpdate() {

		var salutation =	$("#myprofile #salutation").val();
		var fname =			$("#myprofile #fname").val();
		var lname =			$("#myprofile #lname").val();
		var company =		$("#myprofile #company").val();
		var email =			$("#myprofile #email").val();
		var phone =			$("#myprofile #phone").val();
		var mobile =		$("#myprofile #mobile").val();
		var pass =			$("#myprofile #pass1").val();
		var secq =			$("#myprofile #securityquestion").val();
		var seca =			$("#myprofile #securityanswer").val();
		if ($('#myprofile #optin').is(':checked')) { var optin = 1; } else { var optin = 0; }

		var billing_addressline1 =	$("#myprofile #billing_addressline1").val();
		var billing_addressline2 =	$("#myprofile #billing_addressline2").val();
		var billing_suburb =		$("#myprofile #billing_suburb").val();
		var billing_city =			$("#myprofile #billing_city").val();
		var billing_state =			$("#myprofile #billing_state").val();
		var billing_postcode =		$("#myprofile #billing_postcode").val();
		var billing_country =		$("#myprofile #billing_country").val();

		var delivery_addressline1 =	$("#myprofile #delivery_addressline1").val();
		var delivery_addressline2 =	$("#myprofile #delivery_addressline2").val();
		var delivery_suburb =		$("#myprofile #delivery_suburb").val();
		var delivery_city =			$("#myprofile #delivery_city").val();
		var delivery_state =		$("#myprofile #delivery_state").val();
		var delivery_postcode =		$("#myprofile #delivery_postcode").val();
		var delivery_country =		$("#myprofile #delivery_country").val();

		$("#memberdisplayname").text(fname+" "+lname);

		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
			{
				alert ("Browser does not support HTTP Request")
				return
			}
		
		var url="../includes/function_updateMyProfile.php";
		url=url	+"?salutation="+salutation
				+"&fname="+fname
				+"&lname="+lname
				+"&company="+company
				+"&email="+email
				+"&phone="+phone
				+"&mobile="+mobile
				+"&pass="+pass
				+"&secq="+secq
				+"&seca="+seca
				+"&optin="+optin
				+"&billing_addressline1="+billing_addressline1
				+"&billing_addressline2="+billing_addressline2
				+"&billing_suburb="+billing_suburb
				+"&billing_city="+billing_city
				+"&billing_state="+billing_state
				+"&billing_postcode="+billing_postcode
				+"&billing_country="+billing_country
				+"&delivery_addressline1="+delivery_addressline1
				+"&delivery_addressline2="+delivery_addressline2
				+"&delivery_suburb="+delivery_suburb
				+"&delivery_city="+delivery_city
				+"&delivery_state="+delivery_state
				+"&delivery_postcode="+delivery_postcode
				+"&delivery_country="+delivery_country;
				
		url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=function () 
			{ 
				var rt = xmlHttp.responseText;
				//$("#formnotice").fadeIn("fast").html(rt);
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
				{
					if(rt="1")
					{
						$("#formnotice").css({display:"none"});
						$("#formnotice").fadeIn("fast").html("<img style='position:relative;top:3px;' src='../images/ok.png' alt=''> Thank you, your profile information has been updated!");
						disableUpdateButton();
						resetProfileMessages();
					}
					else
					{
						$("#formnotice").css({display:"none"});
						$("#formnotice").fadeIn("fast").html("<img style='position:relative;top:3px;' src='../images/error.png' alt=''> An error occurred while updating your profile. Please try again later.");
					}
				} 
			}
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)

}

	function errNotice(msg)
	{
		if(msg=="reset") { return; }
	}
	
	////////////////////////////////////////////
	////////// LOGIN/LOGOUT FUNCTIONS //////////
	////////////////////////////////////////////

	$("#loginemail").blur(function(){  });
	$("#loginpass").blur(function(){  });

	$("#loginbutton").click(function(){ checkLogin() });		
	$("#logout").click(function(){ logout(); });


	function logout()
	{
	}
	
	function checkLogin()
	{
		$("#loginEmailMsg").html("<img src='/images/load.gif'>");
		$("#loginPassMsg").html("<img src='/images/load.gif'>");

		var e = $("#loginemail").val();
		var p = $("#loginpass").val();
				
		if (e=='' || p=='')
			{
				$("#loginEmailMsg").inputResponse("Please enter an email address", "error");
				$("#loginPassMsg").inputResponse("Please enter your password", "error");
				
				if (e!='') { $("#loginEmailMsg").inputResponse("", "clear"); }
				if (p!='') { $("#loginPassMsg").inputResponse("", "clear"); }
			} 
		else
			{
				xmlHttp = GetXmlHttpObject()
				if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return }
				var url = "../includes/function_checkpass.php"+"?email="+e+"&pass="+p+"&sid="+Math.random();
				xmlHttp.open("GET",url,false);
				xmlHttp.send(null);
				var rt = xmlHttp.responseText;
				/* alert(rt); */
				if (rt=="loginok")
					{
						$("#loginEmailMsg").inputResponse("Okay", "ok");
						$("#loginPassMsg").inputResponse("Okay", "ok");
						
						var urlvars = getUrlVars()["p"];
						
						setTimeout( "window.location = '/myprofile'", 1);
					}
					else
					{
						if (rt == "notactive")
							{
								$("#loginEmailMsg").inputResponse("This account is not active", "error");						
								$("#loginPassMsg").inputResponse("", "clear");
							}
						else if (rt == "bademail")
							{
								$("#loginEmailMsg").inputResponse("Invalid email", "error");
								$("#loginPassMsg").inputResponse("", "clear");
							}
						else if (rt == "badpass")
							{
								$("#loginEmailMsg").inputResponse("Okay", "ok");
								$("#loginPassMsg").inputResponse("Incorrect password", "error");
							}
						else
							{
								$("#loginEmailMsg").inputResponse("Could not connect to database", "error");						
								$("#loginPassMsg").inputResponse("", "clear");
							}					
					}
			}
	}

	///////////////////////////////////////////
	////////// FORGOT PASS FUNCTIONS //////////
	///////////////////////////////////////////
	
	$("#forgotpassbutton1").click(function(){ forgotPass() });
	$("#forgotpassbutton2").click(function(){ checkSecurityAnswer() });
	$("#forgotpassbutton3").click(function(){ resetPassword() });

	function forgotPass()
	{
		$("#forgotPassEmailMsg").html("<img src='/images/load.gif'>");

		var e = $("#loginemail").val();
		
		
		
		if (e=='')
			{
				$("#forgotPassEmailMsg").inputResponse("Please enter an email address", "ok");
				
				if (e!='')
				{
					$("#forgotPassEmailMsg").inputResponse("", "clear");
				}
			} 
		else
			{
				if (emailExists(e)) {

					$("#forgotPassEmailMsg").inputResponse("Email found", "ok");
					
					$("#forgotpassform #step1").hide();
					$("#forgotpassform #step2").show();
					
					xmlHttp=GetXmlHttpObject()
					if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return }
					var url = "../includes/function_forgotpass.php"+"?e="+e+"&p=&a="+"&sid="+Math.random();
					xmlHttp.open("GET",url,false);
					xmlHttp.send(null);
					var rt = xmlHttp.responseText;
					$("#secq").html(rt);

				} else {
					$("#forgotPassEmailMsg").inputResponse("Email not found", "error");
				}
			}
	}
	
	function checkSecurityAnswer()
	{
		var e = $("#loginemail").val();
		var seca = $("#seca").val();

		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return }
		var url = "../includes/function_forgotpass.php"+"?a="+seca+"&p=&e="+e+"&sid="+Math.random();
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		var rt = xmlHttp.responseText;
		if(rt=="true") {
		
					$("#forgotpassform #step2").hide();
					$("#forgotpassform #step3").show();

		} else {
			$("#forgotPassSecaMsg").inputResponse("Incorrect", "error");
		}
	}
	
	function resetPassword() {
		var e = $("#loginemail").val();
		var p1 = $("#newpass1").val();
		var p2 = $("#newpass2").val();
		
		if(p1==''&&p2!='') {
			$("#forgotPassnewpass2Msg").inputResponse("Okay", "ok");
			$("#forgotPassnewpass1Msg").inputResponse("Required", "error");
		} else if(p2==''&&p1!='') {
			$("#forgotPassnewpass1Msg").inputResponse("Okay", "ok");
			$("#forgotPassnewpass2Msg").inputResponse("Required", "error");
		} else if(p1=='') {
			$("#forgotPassnewpass1Msg").inputResponse("Required", "error");
		} else if(p2=='') {
			$("#forgotPassnewpass2Msg").inputResponse("Required", "error");
		} else if (p1!=p2) {
			// forgotPassnewpass1Msg
			$("#forgotPassnewpass2Msg").inputResponse("Passwords do not match", "error");
		} else {
			$("#forgotPassnewpass1Msg").inputResponse("Okay", "ok");
			$("#forgotPassnewpass2Msg").inputResponse("Okay", "ok");
			xmlHttp=GetXmlHttpObject()
			if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return }
			var url = "../includes/function_forgotpass.php?e="+e+"&p="+p1+"&a=&sid="+Math.random();
			xmlHttp.open("GET",url,false);
			xmlHttp.send(null);
			var rt = xmlHttp.responseText;
			$("#forgotpassform").slideUp();
			$("#passwordResetResponse").html(rt);
		}
	}
	
	$("#myprofile_submit").click(function(){ updateMyProfile() });
	
	function updateMyProfile()
	{
		alert("You got me!");
	}

	//////////////////////////////////////
	////////// GLOBAL FUNCTIONS //////////
	//////////////////////////////////////

	function emailExists(e)
	{
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return }
		var url = "../includes/function_emailcheck.php"+"?e="+e+"&sid="+Math.random();
		xmlHttp.open("GET",url,false);
		xmlHttp.send(null);
		if (xmlHttp.responseText == 'true') { return true; } else { return false; }
	}

	function validateEmail(e){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;	

		if(!emailReg.test(e))
		{	
			return false;
		}
		else
		{
			return true;
		}

	}

	function loggedin()
	{
		var a = null;

		$.ajax({
		url: "../includes/function_loggedin.php",
		type: "POST",
	    async: false,
		success: function(data) { a = data; }
		});
		
		return a;
	}

	function matchHeight(el1,el2)
	{	
		var x = getElementHeight(el1);
		$("#"+el2).css({height:x+"px"});
	}

	function getUrlVars()
	{
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(m,key,value) {vars[key] = value;});
		return vars;
	}

	function addCommas(msg)
	{
		if(msg!='') { msg = msg+", "; }
	}

	function getElementID(el)
	{
		var id = $(el).attr('id');
		return id;
	}

	function getElementWidth(objectId)
	{
		var x = document.getElementById(objectId);
		return x.offsetWidth;
	}
	
	function getElementHeight(objectId)
	{
		var y = document.getElementById(objectId);
		return y.offsetHeight;
	}
	
	function getElementLeft(objectId)
	{
		var o = document.getElementById(objectId)
		var oLeft = o.offsetLeft        // Get left position from the parent object
		while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent    // Get parent object reference
			oLeft += oParent.offsetLeft // Add parent left position
			o = oParent
		}
		return oLeft
	}

	function getElementTop(objectId)
	{
		var o = document.getElementById(objectId)
		var oTop = o.offsetTop        // Get top position from the parent object
		while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent  // Get parent object reference
			oTop += oParent.offsetTop // Add parent top position
			o = oParent
		}
		return oTop
	}

	/////////////////////////////////////////////////////////////////
	////////// XMLHTTP Object ///////////////////////////////////////
	/////////////////////////////////////////////////////////////////
	//                                                             //
	// Soon to be deprecated. Use JQuery's AJAX functions instead! //
	//                                                             //
	/////////////////////////////////////////////////////////////////

	function GetXmlHttpObject()
	{
		var xmlHttp=null;
		try
		{
			// Firefox, Opera 8.0+, Safari
			xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{
			//Internet Explorer
			try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}


})
