$(document).ready(function(){

    // Billing address same as mailing
    // Toggles billing address form
   $('#same_as_mailing').live('click', function(){
       if($(this).attr('checked') == true){

            $('#billing_name').val($('#first_name').val() + " " + $('#last_name').val());
            $('#billing_address1').val($('#address').val());
            $('#billing_address2').val($('#address_2').val());
            $('#billing_city').val($('#city').val());
            $('#billing_state_province').val($('#state_province').val());
            $('#billing_postcode').val($('#zip').val());
            if($('#state_id').length){
                $('#billing_state_id').val($('#state_id').val());                
            }
       }
   }); 
   
});

$('#how_did_you_hear').live('change', function(){

   if($(this).val() == 'Other'){
       $('#how_did_you_hear_other_div').css('display', 'block');
   }else{
       $('#how_did_you_hear_other_div').css('display', 'none');           
   }

});

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImagesArray(array) {
	if (preloadFlag == true) {
		var d = document; var img;
		for (var i=0; i<array.length; i+=2) {
			img = null; var n = array[i];
			if (d.images) {img = d.images[n];}
			if (!img && d.getElementById) {img = d.getElementById(n);}
			if (img) {img.src = array[i+1];}
		}
	}
}

function changeImages() {
	changeImagesArray(changeImages.arguments);
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		pre_header_button1_over = newImage('images/header_button1_over.jpg');
		pre_header_button2_over = newImage('images/header_button2_over.jpg');
		pre_header2_04_over = newImage('images/header2_04_over.jpg');
		pre_menu_10_over = newImage('images/menu_10_over.jpg');
		pre_menu_11_over = newImage('images/menu_11_over.jpg');
		pre_menu_12_over = newImage('images/menu_12_over.jpg');
		pre_menu_13_over = newImage('images/menu_13_over.jpg');
		pre_menu_14_over = newImage('images/menu_14_over.jpg');
		preloadFlag = true;
	}
}


function decision(message, url) 
{ 
	answer = confirm(message)
	if (answer !=0) 
	{ 
		location = url 
	} 
}


/* CHECK FORM */

function checkform(fieldIds)
{
	var fieldId=fieldIds.split(';');
	var d = new Date();
	var error;
	var focusField='';

	for(var a=0; a<fieldId.length; a++)
	{
		if ($('#'+fieldId[a]).val() == '')
		{
			checkFormDisplayMessage(fieldId[a]);
			error='Y';
			if(focusField==''){focusField=fieldId[a];}
		}
		
		
		/// password length
		else if(fieldId[a]=='password')
		{
			if($('#'+fieldId[a]).val().length<6)
			{
				$('#form_warning').html();
				checkFormDisplayMessage(fieldId[a],'Please enter at least 6 characters for your password.');
				$('#'+fieldId[a]).select();

				error='Y';
				if(focusField==''){focusField=fieldId[a];}
			}
			else
			{
				checkFormRemoveDisplayMessage(fieldId[a]);
			}
		}
		
		/// email valid format
		else if (fieldId[a]=='email')
		{
			var emailaddress=$('#'+fieldId[a]).val();
			var filter=/^.+@.+\..{2,4}$/;
	
			if (!filter.test(emailaddress))
			{
				checkFormDisplayMessage(fieldId[a],'Please enter a valid email address.');
				error='Y';
				if(focusField==''){focusField=fieldId[a];}
			}
			else
			{
				checkFormRemoveDisplayMessage(fieldId[a]);
			}
		}
		
		/// zip length - at least 5 characters
		else if (fieldId[a]=='zip')
		{
			if($('#'+fieldId[a]).val().length<5)
			{
				checkFormDisplayMessage(fieldId[a],'Please enter a valid Postal Code');
				error='Y';
				if(focusField==''){focusField=fieldId[a];}
			}
			else
			{
				checkFormRemoveDisplayMessage(fieldId[a]);
			}
		}
		
		else if(fieldId[a]=='work_phone')
		{
			var phone=$('#work_phone').val().replace(/[^0-9\-]/g,'');
		
			if(phone.length!=12)
			{
				checkFormDisplayMessage(fieldId[a],'Please enter a valid 10 digit phone number (xxx-xxx-xxxx).');
				error='Y';
				if(focusField==''){focusField=fieldId[a];}
			}
			else
			{
				checkFormRemoveDisplayMessage(fieldId[a]);
			}
			
			$('#work_phone').val(phone);
		}
		
		else
		{
			checkFormRemoveDisplayMessage(fieldId[a]);
		}
	}
	
	
	/// username verify if setting up account
	if($('#username_verified').val()=='N' && $('#username').val()!='')
	{
		checkFormDisplayMessage('username','Please try another username.');
		error='Y';
		if(focusField==''){focusField='username';}
	}

	/// email verify
	if($('#email_verified').val()=='N' && $('#email').val()!='')
	{
		checkFormDisplayMessage('email','Please try a different email.');
		error='Y';
		if(focusField==''){focusField='email';}
	}
	
	
	/// return error \\\
	if(error=='Y')
	{
		$('#'+focusField).focus();
		return false;
	}
	
	fade_box('form_warning','out');
	$('#submit_form_button').hide();
	fade_box('form_processing_display');
	
	return true;
	
}


function checkFormCreditCard()
{

	var d = new Date();
	var checkform;
	var error;
	var focusField='';

	
	/// check form if payment profile exists and not true
	//if($('#payment_profile_id').is(':checked'))
	//{
	if(document.payment_form.payment_profile_id)
	{
		if(document.payment_form.payment_profile_id.checked == true )
		{
			checkform='N';
		}
	}
	
	if(checkform!='N')
	{
		/// credit card length
		var digits=16;
		
		if(document.payment_form.reg_cc_type[3].checked == true )
		{
			digits=15;
		}
	
		var card_number=$('#reg_cc').val().replace(/\D/g,'');
	
		if(card_number.length!=digits)
		{
			checkFormDisplayMessage('reg_cc','Please enter a valid credit card number');
			error='Y';
			focusField='reg_cc';
		}
		else
		{
			checkFormRemoveDisplayMessage('reg_cc');
		}
		
	
		
		/// cvv length
		var cvvdigits=3;
			
		if(document.payment_form.reg_cc_type[3].checked == true )
		{
			cvvdigits=4;
		}
			
		var cvv_number=$('#reg_cc_cvv').val().replace(/\D/g,'');
	
		if(cvv_number.length<cvvdigits)
		{
			checkFormDisplayMessage('reg_cc_cvv','Enter a valid CVV');
			error='Y';
			if(focusField==''){focusField='reg_cc_cvv';}
		}
		else
		{
			checkFormRemoveDisplayMessage('reg_cc_cvv');
		}
		
		
		
		/// check date current
		if(d.getFullYear()=='20'+$('#reg_cc_exp_year').val() && $('#reg_cc_exp_month').val()<d.getMonth()+1)
		{
			checkFormDisplayMessage('exp_date','Enter a current date');
			error='Y';
			if(focusField==''){focusField='exp_date';}
		}
		else
		{
			checkFormRemoveDisplayMessage('exp_date');
		}
			

		/// return error \\\
		if(error=='Y')
		{
			$('#'+focusField).focus();
			return false;
		}
	}
	

	fade_box('form_warning','out');
	fade_box('form_processing_display');
	
	return true;
	
}


function checkFormCreditCardMERQB()
{

	var d = new Date();
	var checkform;
	var error;
	var focusField='';

	
	/// check form if payment profile exists and not true
	//if($('#payment_profile_id').is(':checked'))
	//{
	if(document.payment_form.payment_profile_id)
	{
		if(document.payment_form.payment_profile_id.checked == true )
		{
			checkform='N';
		}
	}
	
	if(checkform!='N')
	{
		/// check to see if payment type is selected
		
		if($('input:radio[name=isphysician]:checked').val())
		{
			checkFormRemoveDisplayMessage('isphysician');
			$('#field_input_isphysician').css('color','');
		}
		else
		{
			error='Y';
			checkFormDisplayMessage('isphysician','Please select a payment type');
			$('#field_input_isphysician').css('color','#990000');
		}
		
		
		/// credit card length
		var digits=16;
		
		if(document.payment_form.reg_cc_type[3].checked == true )
		{
			digits=15;
		}
	
		var card_number=$('#reg_cc').val().replace(/\D/g,'');
	
		if(card_number.length!=digits)
		{
			checkFormDisplayMessage('reg_cc','Please enter a valid credit card number');
			error='Y';
			focusField='reg_cc';
		}
		else
		{
			checkFormRemoveDisplayMessage('reg_cc');
		}
		
	
		
		/// cvv length
		var cvvdigits=3;
			
		if(document.payment_form.reg_cc_type[3].checked == true )
		{
			cvvdigits=4;
		}
			
		var cvv_number=$('#reg_cc_cvv').val().replace(/\D/g,'');
	
		if(cvv_number.length<cvvdigits)
		{
			checkFormDisplayMessage('reg_cc_cvv','Enter a valid CVV');
			error='Y';
			if(focusField==''){focusField='reg_cc_cvv';}
		}
		else
		{
			checkFormRemoveDisplayMessage('reg_cc_cvv');
		}
		
		
		
		/// check date current
		if(d.getFullYear()=='20'+$('#reg_cc_exp_year').val() && $('#reg_cc_exp_month').val()<d.getMonth()+1)
		{
			checkFormDisplayMessage('exp_date','Enter a current date');
			error='Y';
			if(focusField==''){focusField='exp_date';}
		}
		else
		{
			checkFormRemoveDisplayMessage('exp_date');
		}
			

		/// return error \\\
		if(error=='Y')
		{
			if(focusField!='')
			{
				$('#'+focusField).focus();
			}
			return false;
		}
	}
	

	fade_box('form_warning','out');
	fade_box('form_processing_display');
	
	return true;
	
}

function checkFormDisplayMessage(field,message)
{
	if(message && message!='')
	{
		$('#form_field_warning_'+field).html(message);
		$('#form_field_warning_'+field).show();
	}
	fade_box('form_warning');
	$('#field_name_'+field).addClass('form_field_alert');
	//$('#field_input_'+field).addClass('form_input_alert');
	$('#'+field).addClass('form_input_warning');
}

function checkFormRemoveDisplayMessage(field)
{
	$('#form_field_warning_'+field).html('');
	$('#form_field_warning_'+field).hide();
	$('#field_name_'+field).removeClass('form_field_alert');
	//$('#field_input_'+field).removeClass('form_input_alert');
	$('#'+field).removeClass('form_input_warning');
}

/* --- END CHECK FORM --- */


function checkLogin(){
	
	if ($('#login_username').val()=='')
	{
		$('#login_username').focus();
		$('#login_form_warning').html('Please enter your Username');
		$('#login_form_warning').slideDown(300);
		return false;
	}
	
	else if ($('#login_password').val()=='')
	{
		$('#login_password').focus();
		$('#login_form_warning').html('Please enter your Password');
		$('#login_form_warning').slideDown(300);
		return false;
	}
	
	$('#login_form_warning').slideUp(300);
	return true;
}


function displayDiv(onDiv,action,offDiv) {
	
	if(offDiv!='')
	{
		var offDivs=offDiv.split(';');

		for (var i=0; i<offDivs.length; i++)
		{
			if(document.getElementById(offDivs[i]))
			{
				document.getElementById(offDivs[i]).style.display='none';
			}
		}
	}
	
	if(document.getElementById(onDiv))
	{
		if(action=='flip')
		{
			if(document.getElementById(onDiv).style.display=='none')
			{
				document.getElementById(onDiv).style.display='block';
			}
			else
			{
				document.getElementById(onDiv).style.display='none';
			}
		}
		else
		{
			document.getElementById(onDiv).style.display=action;
		}
	}
}

/* toggles class of a div*/
function changeClass(onDiv,onClass,offClass)
{
	if(document.getElementById(onDiv))
	{
		if(document.getElementById(onDiv).className==onClass)
		{
			document.getElementById(onDiv).className=offClass;
		}
		else
		{
			document.getElementById(onDiv).className=onClass;
		}
	}
}


/* changes class of a div*/
function newClass(onDiv,onClass)
{
	if(document.getElementById(onDiv))
	{
		document.getElementById(onDiv).className=onClass;
	}
}

/* referral header display */
function referHeader(cid)
{
	for(var i=0; i<partner.length; i++)
	{
		if(cid==partner[i])
		{
			document.getElementById('header_button_referral_0').style.display='none';
			document.getElementById('header_button_referral_1').style.display='block';
			
			return true;
		}
	}
	
	document.getElementById('header_button_referral_0').style.display='block';
	document.getElementById('header_button_referral_1').style.display='none';
	
	return true;
}

function fade_box(fade_element,display,time)
{
	if(time=='')
	{
		time=300;
	}
	
	if(display=='out')
	{
		$('#'+fade_element).fadeOut(time);
	}
	else
	{
		$('#'+fade_element).fadeIn(time);
	}
}

function toggle_slide(toggle_element)
{
	$('#'+toggle_element).slideToggle(300);
}

function scheduleChangeView(onDiv,offDiv)
{
	if(offDiv!='')
	{
		var offDivs=offDiv.split(';');

		for (var i=0; i<offDivs.length; i++)
		{
			$('#'+offDivs[i]+'s').hide();
			document.getElementById('schedule_tab_'+offDivs[i]).className='schedule_tab';
		}
	}
	
	$('#'+onDiv+'s').fadeIn(300);
	document.getElementById('schedule_tab_'+onDiv).className='schedule_tab_on';
}

function displaySeminar(onDiv,offDiv) {
	
	if(offDiv!='')
	{
		var offDivs=offDiv.split(';');

		for (var i=0; i<offDivs.length; i++)
		{
			$('#'+offDivs[i]).hide();
		}
	}

	$('#'+onDiv).fadeIn(300);
}


function checkUsername(username)
{
	$('#username_verified').val('N');
	
	if(username!='')
	{
		$('#username_response').html('<img src="/images/loading24.gif"> Checking username...');
		
		$.post("/members/check_username/", { username: username },
		function(msg){
			//alert('msg: '+msg);
			if(msg=='N')
			{
				$('#username_response').html('<span style="color:#ff0000;">Please try a different username</span>');
				checkFormDisplayMessage('username','Please try a different username')
				$('#username').select();
			}
			else
			{
				$('#username_verified').val('Y');
				$('#username_response').html('<span style="color:#0000ff;">Good Username!</span>');
				checkFormRemoveDisplayMessage('username');
				fade_box('form_warning','out');
			}
		 });
	}
}


function updateUsername()
{
	$('#update_username_warning').hide();
	
	var username=$('#update_username').val();
	
	if(username=='')
	{
		$('#update_username_warning').html('Please enter a username');
		$('#update_username_warning').fadeIn(300);
		$('#update_username').focus();
	}
	
	else if(username.length<4)
	{
		$('#update_username_warning').html('Please enter at least 4 characters');
		$('#update_username_warning').fadeIn(300);
		$('#update_username').focus();
	}
	
	else
	{
		$('#update_username_button').hide();
		$('#update_username_box_loading').html('<img src="/images/loading24.gif"> Checking username...');
		$('#update_username_box_loading').fadeIn(300);
		
		$.post("/members/check_username/", { username: username },
		function(msg){
			//alert('msg: '+msg);
			if(msg=='N')
			{
				$('#update_username_box_loading').hide();
				$('#update_username_warning').html('Please try a different username');
				$('#update_username_warning').fadeIn(300);
				$('#update_username_button').fadeIn(300);
				$('#update_username').select();
			}
			else
			{
				$('#update_username_box_loading').hide();
				$('#update_username_box_loading').html('<img src="/images/loading24.gif"> Updating username...');
				$('#update_username_box_loading').fadeIn(300);
				$('#update_username_form').submit();
			}
		 });
	}
}


function checkEmail(email)
{
	$('#login_form_note').hide();
	$('#login_form_warning').hide();
	$('#login_form_message').hide();
	$('#field_name_email').css('color','');
	$('#email_response').html('');
	
	if(email!='')
	{
		var filter=/^.+@.+\..{2,4}$/;

		if (filter.test(email))
		{
			$.post("/members/check_email/", { email: email },
			function(msg){
				//alert('msg: '+msg);
				if(msg>0)
				{
					/* open login form */
					$('#login_form_note').html("<b>It appears that we have your email address on file from either a past program with us or an established user account. What would you like to do?</b><ul><li>If you have an account with us, enter your username and password below to log in or click &quot;Request Login&quot; to have it sent to your email address.</li><li>If do not have an account and would like to use this email address to set one up for free, click &quot;Request Login&quot; and new login information will be sent to "+email+".</li><li>Click &quot;Cancel&quot; if you want to use a different email address.</li></ul>");
					$('#login_hidden_box').addClass('hb_box');
					$('#login_sign_up_button').hide();
					$('#login_continue_button').show();
					$('#login_box').fadeIn(300);
					$('#login_username').select();
					$('#login_form_note').fadeIn(300);
					$('#login_email').val(email);
					$('#uid').val(msg);
					$('#email_verified').val('N');
				}
				else
				{
					checkFormRemoveDisplayMessage('email');
					$('#email_verified').val('Y');
				}
			});
		}
	}
}


function checkJoinEmail(email)
{
	$('#login_form_note').hide();
	$('#login_form_warning').hide();
	$('#login_form_message').hide();
	$('#field_name_email').css('color','');
	$('#email_response').html('');
	
	if(email!='')
	{
		var filter=/^.+@.+\..{2,4}$/;

		if (filter.test(email))
		{
			$.post("/members/check_email/", { email: email },
			function(msg){
				//alert('msg: '+msg);
				if(msg>0)
				{
					/* open login form */
					$('#login_form_note').html("<b>It appears that we have your email address on file from either a past program with us or an established user account. What would you like to do?</b><ul><li>If you have an account with us, enter your username and password below to log in or click &quot;Request Login&quot; to have it sent to your email address.</li><li>If do not have an account and would like to set one up for free, click &quot;Request Login&quot; and an account will be set up for you and temporary login information will be sent to "+email+".</li></ul>");
					$('#login_hidden_box').addClass('hb_box');
					$('#login_sign_up_button').hide();
					$('#login_box').fadeIn(300);
					$('#login_username').select();
					$('#login_form_note').fadeIn(300);
					$('#login_email').val(email);
					$('#uid').val(msg);
					$('#email_verified').val('N');
				}
				else
				{
					checkFormRemoveDisplayMessage('email');
					$('#email_verified').val('Y');
				}
			});
		}
	}
}

function requestLoginInformation()
{
	if($('#login_email').val()!='')
	{
		var filter=/^.+@.+\..{2,4}$/;

		if (!filter.test($('#login_email').val()))
		{
			$('#login_form_warning').html('Please submit a valid email address');
			$('#login_form_warning').slideDown(300);
			$('#login_email').select();
		}
		else
		{
			sendLoginInformation($('#login_email').val());
		}
	}
	
	else
	{
		$('#login_email_request_box').slideDown(300);
		$('#login_email').focus();
	}
		
}

function sendLoginInformation(email)
{
	
	$('#login_form_warning').hide();
	$('#login_email_request_box').hide();
	$('#login_email_request_box_loading').fadeIn(300);
	
	$.post("/members/request_login/", { email: email },
		function(msg){
			//alert('msg: '+msg);
			if(msg=='Y')
			{
				$('#login_form_message').html('Your login information has been sent to '+email);
				$('#login_form_message').fadeIn(300);
				$('#login_username').select();
				$('#login_email_request_box_loading').slideUp(300);
				$('#login_email').val('');
			}
			else
			{
				$('#login_form_warning').html('We could not find your email address in the database. Please try a different email or call MER for help.');
				$('#login_form_warning').slideDown(300);
				$('#login_email_request_box_loading').hide();
				$('#login_email_request_box').slideDown(300);
				$('#login_email').select();
			}
		});		
}

function registerUpdateContact(user_id)
{
	$('#contact_edit_box').hide();
	$('#contact_view_box').hide();
	$('#contact_view_box').html('<div style="padding:20px;"><img src="/images/loading24.gif"> Updating...</div>');
	fade_box('contact_view_box');
	
	$.post("/register/update_contact/", {
		user_id: user_id,
		first_name: $('#first_name').val(),
		last_name: $('#last_name').val(),
		address: $('#address').val(),
		city: $('#city').val(),
		state_id: $('#state_id').val(),
		zip: $('#zip').val()},
		
		function(msg){
			//alert('msg: '+msg);

			$('#message_display_box').hide();
			$('#contact_view_box').hide();
			
			if(msg=='N')
			{
				$('#message_display_box').html('<div class="warning">We were unable to save your changes. Please try again.</div>');
				$('#message_display_box').fadeIn(1200);

				$('#contact_view_box').html(first_name+' '+last_name+'<br>'+address+'<br>'+city+', '+state_id+'&nbsp;&nbsp;&nbsp;'+zip);
				fade_box('contact_view_box');
			}
			
			else
			{
				$('#contact_view_box').html(msg);
				fade_box('contact_view_box');
				
				$('#message_display_box').html('<div class="message">Your contact information has been updated.</div>');
				$('#message_display_box').fadeIn(1200);
			}
		});
}


function profileChange(div,type,field)
{
	if(type=='view')
	{
		$('#'+div+'_edit').hide();
		$('#'+div+'_view_link').hide();
		$('#'+div+'_view').fadeIn(300);
		$('#'+div+'_edit_link').fadeIn(300);
	}
	else
	{
		$('#'+div+'_view').hide();
		$('#'+div+'_edit_link').hide();
		$('#'+div+'_edit').fadeIn(300);
		$('#'+div+'_view_link').fadeIn(300);
		$('#'+field).focus();
	}
}

function adjustAmount(cid)
{
	if(cid=='')
	{
		$('#amount').val($('#full_amount').val());
		$('#promo_item_id').val('');
		$('#amount_due_display').html('$'+eval($('#full_amount').val()).toFixed(2));
		$('#discount_display_box').slideUp(300);
	}
	else
	{
		var amount=$('#full_amount').val()-$('#credit_amount_'+cid).val();
		
		$('#amount').val(amount);
		$('#promo_item_id').val(cid);
		$('#amount_due_display').html('$'+eval(amount).toFixed(2));
		$('#discount_display').html('-'+eval($('#credit_amount_'+cid).val()).toFixed(2));
		$('#discount_display_box').slideDown(300);
	}	
}

function assignAmount(amount,type)
{
	$('#full_amount').val(amount);
	$('#registration_fee_type').val(type);
	$('#registration_fee_display').html(eval(amount).toFixed(2));
	
	adjustAmount($('#promo_item_id').val());
}

function formatPhone(id)
{	
	var phone=$('#'+id).val().replace(/[^0-9\-]/g,'');
	
	if(phone.length==3 || phone.length==7)
	{
		$('#'+id).val(phone+'-');
	}
	
	else
	{
		$('#'+id).val(phone);
	}
}

function numbersOnly(id)
{	
	$('#'+id).val($('#'+id).val().replace(/\D/g,''));
}

function dollarsOnly(id)
{
	$('#'+id).val($('#'+id).val().replace(/[^0-9\.]/g,''));
}

function setSeminar(seminar_id)
{
	$('#new_seminar_id').val(seminar_id);
	
	if(seminar_id==$('#seminar_id').val())
	{
		fade_box('see_details_button','out');
	}
	else
	{
		fade_box('see_details_button');
	}
}

function setDivHeight(divs,divHeight,static)
{
	var divID=divs.split(';');
	
	if(!static)
	{
		for(var a=0; a<divID.length; a++)
		{
			if(parseInt($('#'+divID[a]).css('height'))>parseInt(divHeight))
			{
				divHeight=parseInt($('#'+divID[a]).css('height'));
			}
		}
	}
	
	/// set heights to tallest
	for(var a=0; a<divID.length; a++)
	{
		$('#'+divID[a]).css('height',divHeight+'px');
	}
	
}

