(function() {

var email_reg = new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
var phone_reg = new RegExp(/^\(\d{3}\) \d{3}\-\d{4}$/);
var pobox_reg = new RegExp(/\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b/);

// Shipping & Billing Information
function use_shipping_for_billing(e){

	var disabled = $('use_shipping_for_billing').checked;

	$('billing_first_name').disabled = disabled;
	$('billing_last_name').disabled = disabled;
	$('billing_company').disabled = disabled;
	$('billing_address1').disabled = disabled;
	$('billing_address2').disabled = disabled;
	$('billing_city').disabled = disabled;
	$('billing_state').disabled = disabled;
	$('billing_zip').disabled = disabled;
	$('billing_phone').disabled = disabled;
	$('billing_email').disabled = disabled;
	$('billing_email_confirmation').disabled = disabled;

	$('billing_first_name').value = $('shipping_first_name').value;
	$('billing_last_name').value = $('shipping_last_name').value;
	$('billing_company').value = $('shipping_company').value;
	if($('shipping_address1').value != 'We do not ship to PO Boxes')
		$('billing_address1').value = $('shipping_address1').value;
	else
		$('billing_address1').value = '';
	$('billing_address2').value = $('shipping_address2').value;
	$('billing_city').value = $('shipping_city').value;
	$('billing_state').value = $('shipping_state').value;
	$('billing_zip').value = $('shipping_zip').value;
	$('billing_phone').value = $('shipping_phone').value;
	if($('shipping_email').value != 'Where shipping confirmation is sent')
		$('billing_email').value = $('shipping_email').value;
	else
		$('billing_email').value = '';
	$('billing_email_confirmation').value = $('shipping_email_confirmation').value;
}

function shipping_address1_focus(e){
	if($('shipping_address1').value == 'We do not ship to PO Boxes')
		$('shipping_address1').value = '';
}
function shipping_email_focus(e){
	if($('shipping_email').value == 'Where shipping confirmation is sent')
		$('shipping_email').value = '';
}
function billing_email_focus(e){
	if($('billing_email').value == 'Where billing receipt is sent')
		$('billing_email').value = '';
}

function shipping_input_change(e){

	var t = YAHOO.util.Event.getTarget(e);

	if($('use_shipping_for_billing').checked){
		var id = t.id.substr(9);
		$('billing_'+id).value = t.value;
	}
}


function validate(e){

	var message = [];

	if(!$('first_name').value.length)
		message[message.length] = 'First Name is required.';
	if(!$('last_name').value.length)
		message[message.length] = 'Last Name is required.';
	if(!$('email').value.length)
		message[message.length] = 'Email is required.';
	else if($('email').value != $('email_confirm').value)
		message[message.length] = 'Email confirmation does not match email.';	
	if(!$('password').value.length)
		message[message.length] = 'Password is required.';
	else if($('password').value != $('password_confirm').value)
		message[message.length] = 'Password confirmation does not match password.';	

		
	if(!$('shipping_first_name').value.length)
		message[message.length] = 'Shipping: First Name is required.';
	if(!$('shipping_last_name').value.length)
		message[message.length] = 'Shipping: Last Name is required.';
	if(!$('shipping_address1').value.length)
		message[message.length] = 'Shipping: Address is required.';
	else if(!shipping_address1_validate())
		return;
	if($('shipping_address2').value.length && !shipping_address2_validate())
		return;
	if(!$('shipping_city').value.length)
		message[message.length] = 'Shipping: City is required.';
	if(!$('shipping_state').value.length)
		message[message.length] = 'Shipping: State is required.';
	if(!$('shipping_zip').value.length)
		message[message.length] = 'Shipping: Zip is required.';
	if(!$('shipping_phone').value.length)
		message[message.length] = 'Shipping: Phone is required.';
	else if(!shipping_phone_validate())
		return;
	if(!$('shipping_email').value.length)
		message[message.length] = 'Shipping: Email is required.';
	else if(!shipping_email_validate())
		return;
	if(!shipping_email_confirmation_validate())
		return;
	
	if(!$('billing_first_name').value.length)
		message[message.length] = 'Billing: First Name is required.';
	if(!$('billing_last_name').value.length)
		message[message.length] = 'Billing: Last Name is required.';
	if(!$('billing_address1').value.length)
		message[message.length] = 'Billing: Address is required.';
	if(!$('billing_city').value.length)
		message[message.length] = 'Billing: City is required.';
	if(!$('billing_state').value.length)
		message[message.length] = 'Billing: State is required.';
	if(!$('billing_zip').value.length)
		message[message.length] = 'Billing: Zip is required.';
	if(!$('billing_phone').value.length)
		message[message.length] = 'Billing: Phone is required.';
	else if(!phone_reg.test($('billing_phone').value))
		message[message.length] = 'Billing: Phone is invalid.';
	if(!$('billing_email').value.length)
		message[message.length] = 'Billing: Email is required.';
	else if(!billing_email_validate())
		return;
	if(!billing_email_confirmation_validate())
		return;	

	// display errors if there any
	if(message.length){

		YAHOO.util.Event.preventDefault(e);
		error_panel.setBody(message.join('<br />'));
		error_panel.show();
	}
	else{
	
		$('billing_first_name').disabled = false;
		$('billing_last_name').disabled = false;
		$('billing_company').disabled = false;
		$('billing_address1').disabled = false;
		$('billing_address2').disabled = false;
		$('billing_city').disabled = false;
		$('billing_state').disabled = false;
		$('billing_zip').disabled = false;
		$('billing_phone').disabled = false;
		$('billing_email').disabled = false;
		$('billing_email_confirmation').disabled = false;
	}
}

function shipping_address1_change(e){
	
	if(!shipping_address1_validate(e))
		return;
	
	shipping_input_change(e);
}
function shipping_address1_validate(e){

	var shipping_address1 = $('shipping_address1');
	shipping_address1.style.color = '#000';
	
	if(pobox_reg.test(shipping_address1.value)){
		shipping_address1.style.color = '#F00';
		alert('Shipping: We do not ship to PO Boxes');
		return false;
	}
	return true;
}
function shipping_address2_change(e){
	
	if(!shipping_address2_validate(e))
		return;
	
	shipping_input_change(e);
}
function shipping_address2_validate(e){

	var shipping_address2 = $('shipping_address2');
	shipping_address2.style.color = '#000';
	
	if(pobox_reg.test(shipping_address2.value)){
		shipping_address2.style.color = '#F00';
		alert('Shipping: We do not ship to PO Boxes');
		return false;
	}
	return true;
}
function shipping_phone_change(e){

	var shipping_phone = $('shipping_phone');

	// strip non-numeric chars
	var phone = shipping_phone.value.replace(/\D/g,'');
	// reformat
	phone = '(' + phone.substring(0,3) + ') ' + phone.substring(3,6) + '-' + phone.substring(6,10);

	shipping_phone.value = phone;
	
	if(!shipping_phone_validate(e))
		return;
	
	shipping_input_change(e);
}
function shipping_phone_validate(e){

	var shipping_phone = $('shipping_phone');
	shipping_phone.style.color = '#000';
	
	if(!phone_reg.test(shipping_phone.value)){
		shipping_phone.style.color = '#F00';
		alert('Shipping: Phone is invalid.');
		return false;
	}
	return true;
}
function shipping_email_change(e){
	
	if(!shipping_email_validate(e))
		return;
	
	shipping_input_change(e);
}
function shipping_email_validate(e){

	var shipping_email = $('shipping_email');
	shipping_email.style.color = '#000';
	
	if(!email_reg.test(shipping_email.value)){
		shipping_email.style.color = '#F00';
		alert('Shipping: Email is invalid.');
		return false;
	}
	return true;
}
function shipping_email_confirmation_change(e){
	
	if(!shipping_email_confirmation_validate(e))
		return;
	
	shipping_input_change(e);
}
function shipping_email_confirmation_validate(e){

	var shipping_email_confirmation = $('shipping_email_confirmation');
	shipping_email_confirmation.style.color = '#000';
	
	if($('shipping_email').value != shipping_email_confirmation.value){
		shipping_email_confirmation.style.color = '#F00';
		alert('Shipping: Email confirmation does not match email.');
		return false;
	}
	return true;
}

function billing_phone_change(e){

	var billing_phone = $('billing_phone');

	// strip non-numeric chars
	var phone = billing_phone.value.replace(/\D/g,'');
	// reformat
	phone = '(' + phone.substring(0,3) + ') ' + phone.substring(3,6) + '-' + phone.substring(6,10);

	billing_phone.value = phone;
	
	if(!billing_phone_validate(e))
		return;
}
function billing_phone_validate(e){

	var billing_phone = $('billing_phone');
	billing_phone.style.color = '#000';
	
	if(!phone_reg.test(billing_phone.value)){
		billing_phone.style.color = '#F00';
		alert('Billing: Phone is invalid.');
		return false;
	}
	return true;
}
function billing_email_validate(e){

	var billing_email = $('billing_email');
	billing_email.style.color = '#000';
	
	if(!email_reg.test(billing_email.value)){
		billing_email.style.color = '#F00';
		alert('Billing: Email is invalid.');
		return false;
	}
	return true;
}
function billing_email_confirmation_validate(e){

	var billing_email_confirmation = $('billing_email_confirmation');
	billing_email_confirmation.style.color = '#000';
	
	if($('billing_email').value != billing_email_confirmation.value){
		billing_email_confirmation.style.color = '#F00';
		alert('Billing: Email confirmation does not match email.');
		return false;
	}
	return true;
}

YAHOO.util.Event.on(window,'load',function(){

	YAHOO.util.Event.on('form','submit',validate);

	// Shipping & Billing Information
	YAHOO.util.Event.on('use_shipping_for_billing','change',use_shipping_for_billing);

	YAHOO.util.Event.on('shipping_address1','focus',shipping_address1_focus);
	YAHOO.util.Event.on('shipping_email','focus',shipping_email_focus);
	YAHOO.util.Event.on('billing_email','focus',billing_email_focus);

	// Use Shipping Address for Billing
	YAHOO.util.Event.on('shipping_first_name','change',shipping_input_change);
	YAHOO.util.Event.on('shipping_last_name','change',shipping_input_change);
	YAHOO.util.Event.on('shipping_company','change',shipping_input_change);
	YAHOO.util.Event.on('shipping_city','change',shipping_input_change);
	YAHOO.util.Event.on('shipping_state','change',shipping_input_change);
	YAHOO.util.Event.on('shipping_zip','change',shipping_input_change);

	YAHOO.util.Event.on('shipping_address1','change',shipping_address1_change);
	YAHOO.util.Event.on('shipping_address2','change',shipping_address2_change);
	YAHOO.util.Event.on('shipping_phone','change',shipping_phone_change);
	YAHOO.util.Event.on('shipping_email','change',shipping_email_change);
	YAHOO.util.Event.on('shipping_email_confirmation','change',shipping_email_confirmation_change);

	YAHOO.util.Event.on('billing_phone','change',billing_phone_change);
	YAHOO.util.Event.on('billing_email','change',billing_email_validate);
	YAHOO.util.Event.on('billing_email_confirmation','change',billing_email_confirmation_validate);

	$('first_name').focus();
});


})();