/*
	==================================================================
		Function to control the users interations
		with the cart, add/remove contents.
		create/edit user accounts.
		
	==================================================================  */

	var my_goc;
	var my_gocuser;
	jQuery(document).ready(
		function ()
			{
				// init the class
				my_goc = new GoCart();
				
				/*
				==================================================================
						listeners
				==================================================================  */
					// current cart widget. 
					jQuery('.goc_cart_summary_widget_target').bind('cart_update', function () { my_goc.update_cart_widget(this); } );
					
					// add button.
					jQuery('.goc_add').bind('click', function () { my_goc.additem(this); });
					
					// update content button
					jQuery('.goc_update').bind('click', function () { my_goc.updateitem(this); });
					
					//shipto country changed.
					jQuery('.goc #shipto_country').bind('change', function () { my_goc.handle_shiptocountrychange(this); } );
					jQuery('.goc #shipto_country').bind('get_rates', function () { my_goc.get_shipping_rates(this); } );
				
				
				// show current cart contents
				jQuery('.goc_cart_summary_widget_target').trigger('cart_update');
				
				// show shipping selector if it's there. 
				jQuery('.goc #shipto_country').trigger('change');
				
				// show the create account, if it's checked.
				jQuery('#goc_create_account_flag').trigger('change');
			
			});

	/*
	==================================================================
			Main Class
	==================================================================  */
	function GoCart() {
		
		/*
		==================================================================
				Constants
		==================================================================  */
		var action = 'mygoc';
		var wait_html = '<div class = "goc_wait"></div>';

		/*
		==================================================================
				add to cart
		==================================================================  */
		this.additem = function(o) { _additem(o); }
		
		function _additem(o) {
			
			jQuery('body').addClass('goc_waitcursor');
			var item	= jQuery.parseJSON( jQuery(o).attr("role") );
			item.qty	= jQuery('.qty-'+item.prod_UID).val();
			
			var post= {
				action: action,
				a: 'additem'
			};
			
			post = ary_merge(item,post);
			
			gs_pop_window({ajaxurl:goc_ajaxurl,content:'Please wait while we process your request'+wait_html});
			
			setTimeout(function() {
			jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								jQuery('body').removeClass('goc_waitcursor');
								if (typeof(data.flash_message) != 'undefined')
								{
									//alert(data.flash_message);
									//gs_pop_window({ajaxurl:goc_ajaxurl,content:data.flash_message});
									gs_update_pop_window({ajaxurl:goc_ajaxurl,contents:data.flash_message});
								}
								jQuery('.goc_cart_summary_widget_target').trigger('cart_update');
							},"json"
					);
			},1000);
		}
									
		
		/*
		==================================================================
				Update Cart contents
		==================================================================  */
		this.updateitem = function(o) { _updateitem(o); }
		
		function _updateitem(o) {
			
			var item	= jQuery.parseJSON( jQuery(o).attr("role") );
			item.qty	= jQuery('.qty-'+item.content_UID).val();
			
			var post= {
				action: action,
				a: 'updateitem'
			};
			
			post = ary_merge(item,post);
			
						jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								window.location.reload()
							}
					);
		}
		
		/*
		==================================================================
				Update the goc cart widget contents.
		==================================================================  */
		this.update_cart_widget = function(o) { _update_cart_widget(o); }
		
		function _update_cart_widget(o) {
			
			jQuery(o).html(wait_html);
			
			var post= {
				action: action,
				a: 'cart_widget_contents',
				id: jQuery(o).attr("role")
			};
			
			jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								if (typeof(data.result) != 'undefined')
								{
									jQuery(o).html(data.result);
								}
							},"json"
					);
			
		}
		
		/*
		==================================================================
				Handle Ship to country changed
		==================================================================  */
		this.handle_shiptocountrychange = function(o)	{ _handle_shiptocountrychange(o); }
		
		function _handle_shiptocountrychange(o) {
			
			var show_class = '.is_domestic';
			var hide_class = '.is_international';
			
			// branch for US and non US ship to countries. 
			if (jQuery(o).val() != 'US')
				{
					show_class = '.is_international';
					hide_class = '.is_domestic';
				}
				
			jQuery('.goc.goc_shipping_selector '+show_class).css({'display': 'block'});
			jQuery('.goc.goc_shipping_selector '+hide_class).css({'display': 'none'});
					
			// disable the other form elements.
			// this prevents them from being submitted. 
			jQuery('.goc.goc_shipping_selector '+hide_class+' :input').attr("disabled", "disabled");
			jQuery('.goc.goc_shipping_selector '+show_class+' :input').attr("disabled", '');
			
			// click the "get_rates" button.
			jQuery('.goc #shipto_country').trigger('get_rates');
		}
		
		/*
		==================================================================
				Handle a get rates request. 
		==================================================================  */
		this.get_shipping_rates	= function(o) { _get_shipping_rates(o); }
		
		function _get_shipping_rates(o)
		{
			var country =  jQuery(o).val();
			
			// clear the current rates.
			jQuery('#goc_shipping_rates_target').html(wait_html);
			
			if (country != 'US')
			{
				// get international rates.
				var post = {
					country:	country
				};
				
				_show_shipping_rates(post);
				return; 
			}
			
			// else this is domestic.
			
			var postal_code	= jQuery('#goc_domestic_postalcode').get(0);
			
			var domestic_postalcode = jQuery(postal_code).val();
			
			if (domestic_postalcode.length < 1) { jQuery('#goc_shipping_rates_target').html('<p class = "goc_shipping_zip_needed">Enter your zipcode to get a shipping estimate</p>'); return; } // no postal code configured.
			
			var is_valid = goc_validate_field( jQuery('#goc_domestic_postalcode') );
				if ( is_valid.pass != true )
					{
						alert( is_valid.reason );
						return;
					}
					
				jQuery(postal_code).trigger('is_okay');
				
				var post = {
					country:	country,
					postalcode:	domestic_postalcode
				};
				
				_show_shipping_rates(post);
		}
		
		/*
		==================================================================
				A function to pull in shipping rates and display
				them in the shipping rate target display area. 
		=================================================================  */
		function _show_shipping_rates(post)
		{
		post.action	=  action;
		post.a		= 'get_shipping_rates';
			
			jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								if (typeof(data.result) != 'undefined')
								{
									// show result
									jQuery('#goc_shipping_rates_target').html(data.result);
									
									// bind selected shipping service listeners.
									jQuery('.goc_shipping_selector .goc_shipping_rate input[name=goc_selected_shipping_service]').bind('click', function () { _shipping_rate_selected(this); } );
									
									// click the cheapest rate.
									jQuery('.goc_shipping_selector .goc_shipping_rate.is_lowest_rate input[name=goc_selected_shipping_service]').trigger('click');
								}
								
								if (typeof(data.flash_message) != 'undefined')
								{
									gs_pop_window({ajaxurl:goc_ajaxurl,content:data.flash_message});
								}
								
							},"json"
					);
			
		}
		
	/*
	==================================================================
			ajax call to save the selected shipping rate. 
	==================================================================  */
	function _shipping_rate_selected(o)
	{
		var post = {
			action:	action,
			a:'select_shipping_rate',
			service:jQuery(o).val()
		};
		
		jQuery.post(goc_ajaxurl, post);
	}
	
	/*
	==================================================================
			Save Shipping Address Info
	==================================================================  */
	this.save_shipto_address	= function(o) { _save_shipto_address(o); }
	function _save_shipto_address(o)
		{
		// by the time this is called, the form has already been validated. 
		var form = jQuery(o).parents('form').get(0);
		var post = {
			action:	action,
			a:'save_shipto_address'
		};
		
		var formary = jQuery(form).serializeArray();
		
		serialize_toObj(formary,post);
		
		jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								
								document.location = jQuery('a.billto-address').attr("href");
							});
	}
	
	/*
	==================================================================
			Bill To Country Changed. 
	==================================================================  */
	this.handle_billto_country_change	= function(o) { _handle_billto_country_change(o); }
	function _handle_billto_country_change(o)
	{
		var post = {
			action:	action,
			a:'save_billto_country',
			country:jQuery(o).val()
		};
		
		jQuery.post(goc_ajaxurl,  post,
						function(data)
							{
								
								document.location = jQuery('a.billto-address').attr("href");
							});
	}
	
	/*
	==================================================================
			Save Bill to info.
	==================================================================  */
	this.save_billto_address 	= function(o) { _save_billto_address(o); }
	function _save_billto_address(o)
	{
		// by the time this is called, the form has already been validated. 
		var form = jQuery(o).parents('form').get(0);
		var post = {
			action:	action,
			a:'save_billto_address'
		};
		
		var formary = jQuery(form).serializeArray();
		
		serialize_toObj(formary,post);
		var now = new Date;
		jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								document.location = jQuery('a.final-review').attr("href");
							});
	}
	
	/*
	==================================================================
			Redeem coupon code
	==================================================================  */
	this.redeem_code	= function()	{ _redeem_code(); }
	function _redeem_code()
	{
		var code	= jQuery('#goc_coupon_code').val();

		if (typeof(code) == 'undefined' || code.length < 1 ) { return; }
		
		var post = {
			action:	action,
			a:'redeem_code',
			code:code
		};
		
		jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								if (typeof(data.flash_message) != 'undefined')
								{
									gs_pop_window({ajaxurl:goc_ajaxurl,content:data.flash_message});
								}
								else
								{
									window.location.reload();
								}
							},"json");
	}
	
	/*
	==================================================================
			final_payment.
			Show the CC payment form. 
	==================================================================  */
	this.final_payment	=	function()	{ _final_payment(); }
	function _final_payment()
	{
		document.location = jQuery('a.finish-pay').attr("href");
	}
	
	/*
	==================================================================
			paypal_payment
			Redirect the customer to the paypal login.
	==================================================================  */
	this.paypal_payment	=	function()	{ _paypal_payment(); }
	function _paypal_payment()
	{		
		var post = {
			action:	action,
			a:'paypal_payment'
		};
		
		jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								if (typeof(data.flash_message) != 'undefined')
								{
									gs_pop_window({ajaxurl:goc_ajaxurl,content:data.flash_message});
								}
								else
								{
									document.location = data.redirect_url;
								}
							},"json");
	}
	
	/*
	==================================================================
			submit_cc_payment
			Verify all form data is filled out.
			Then send payment, and pop window to customer
			so they know it's in process. 
	==================================================================  */
	this.submit_cc_payment 	= function(o) { _submit_cc_payment(o); }
	function _submit_cc_payment(o)
	{
		var cc_num	=	jQuery('#goc_cc_number').val();
		var cc_cvv	= 	jQuery('#goc_cc_cvv').val();
		
		cc_num = cc_num.replace(/[^0-9]/g,"");
		jQuery('#goc_cc_number').val(cc_num);
							
		// verify ccnum and cvv.
		if (cc_num.length < 13 || cc_num.length > 16) { alert("Please Enter a valid Credit Card Number"); return; }
		if (cc_cvv.length < 3 || cc_cvv.length > 4) { alert("Please Enter a valid CVV number"); return; }
		// exp_mo and exp_yer are from drop downs and can't not be set.
		
		var form = jQuery(o).parents('form').get(0);
		var post = {
			action:	action,
			a:'submit_cc_payment'
		};
		
		var formary = jQuery(form).serializeArray();
		
		serialize_toObj(formary,post);
		jQuery('body').addClass('goc_waitcursor');
		
		gs_pop_window({ajaxurl:goc_ajaxurl,content:'Please wait while we process your request'+wait_html});
		
		setTimeout(function() {
		jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								jQuery('body').removeClass('goc_waitcursor');
								if (typeof(data.flash_message) != 'undefined')
								{
								
									gs_update_pop_window({contents:data.flash_message});
								}
							} ,"json");
		},1000);
	}
	
	/*
	==================================================================
			submit_pp_payment
			The first step in the setexpresscheckout process
	==================================================================  */
	this.submit_pp_payment	= function() { _submit_pp_payment(); }
	function _submit_pp_payment()
	{
		var post = {
			action:	action,
			a:'submit_pp_payment'
		};
		
		gs_pop_window({ajaxurl:goc_ajaxurl,content:'Please wait while we process your request'});
		setTimeout(function() {
		jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								if (typeof(data.flash_message) != 'undefined')
								{
								
									gs_update_pop_window({contents:data.flash_message});
								}
							} ,"json");
		},1500);
	}
	
	/*
	==================================================================
			check the availability of user login. 
	==================================================================  */
	this.userlogin_is_available	= function(o) { _userlogin_is_available(o); }
	function _userlogin_is_available(o)
		{

		var post = {
			action:	action,
			a:'is_login_available',
			user_login: jQuery(o).val()
		};
		
		jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								if (typeof(data.result) != 'undefined' && data.result == 'false')
								{
									alert('Sorry, that User Login is not available');
									jQuery(o).val('');
								}
								else
								{
									jQuery(o).val(data.user_login); // insert the sanitized user_login.
								}
								
							},"json");
		}
		
	/*
	==================================================================
			toggle the required and disabled tags for the
			user_login and password. 
	==================================================================  */
	this.toggle_registration = function(o) { _toggle_registration(o); }
	function _toggle_registration(o)
		{
			if (jQuery(o).is(':checked'))
			{
				jQuery('#goc_register_user_login').addClass('is_required');
				jQuery('#goc_register_user_pass').addClass('is_required');
				
				jQuery('#goc_register_user_login').find(':input').attr("disabled", '');
				jQuery('#goc_register_user_pass').find(':input').attr("disabled", '');
				goc_uninit_required_field_listeners();
				goc_init_required_field_listeners();
			}
			else
			{
				jQuery('#goc_register_user_login').removeClass('is_required');
				jQuery('#goc_register_user_pass').removeClass('is_required');
				
				jQuery('#goc_register_user_login :input').attr("disabled", 'disabled')
				jQuery('#goc_register_user_pass :input').attr("disabled", 'disabled')
			}
		}
	/*
	==================================================================
			utilities
	==================================================================  */
		/*
		==================================================================
				takes a 1 demensional object/array and merges it
				with the provided object. 
		==================================================================  */
		function ary_merge(src_ary,o)
			{
				jQuery.each(src_ary, function(k, v){ o[k]	= v; })
				return o;
			}
			
		/*
		==================================================================
				takes the jquery searializeArray style
				[0] = array (name = "ABC", value = "DEF")
				and flatens it into an object provided.
				o.[abc]	= "DEF"
		==================================================================  */
		function serialize_toObj(arr,o)
			{
				jQuery.each(arr, function(){ o[this.name]	= this.value; })
				return o;
			}
	
	}
	
	
	/*
	==================================================================
			
			Class to manage the user account area.
			
			
	==================================================================  */
	function GoCartUser()
	{
		/*
		==================================================================
				Constants
		==================================================================  */
		var action = 'mygocuser';
		
		this.nav_tab_click	= function(o) { _nav_tab_click(o);}
		function _nav_tab_click(o)
			{
				
				// remove is_active from all tabs.
				jQuery('.goc-nav-tab').removeClass('is_active');
				
				// activate the new tab
				jQuery(o).addClass('is_active');
				
				// get the data
				var a		= jQuery(o).attr("role"); // the action is in the role of the tab.
				
				// test for role = a full url.
				if (typeof(a) != 'undefined' && a.match(/^http:\/\//) ) { document.location = a; return; }
				
				// remove the class from the ajax_content area.
				jQuery('#my-account-page-content').removeClass();
				jQuery('#my-account-page-content').addClass(a);
				
				var post = {
						action: action, // this never changes.
						a: a
					};
		
				jQuery.post(goc_ajaxurl, post,
							function(data)
								{
									if (typeof(data.flash_message) != 'undefined')
									{
										jQuery('#flash_message').html(data.flash_message);
										jQuery('#flash_message').show('fast');
										setTimeout(function() { jQuery('#flash_message').hide('medium'); },4000);
									}
									if (typeof(data.result) != 'undefined')
									{
										jQuery('#my-account-page-content').html(data.result);
									}
								},"json");
			}
			
		/*
		==================================================================
				save/update userinfo
		==================================================================  */
		this.update_user	= function(o) { _update_user(o); }
		function _update_user(o)
		{
			// get the form. 
			var form = jQuery(o).parents('form').get(0);
			if (goc_validate_form(o))
			{
				var post = {
						action: action, // this never changes.
						a: 'update_user'
					};
					
				var formary = jQuery(form).serializeArray();
				serialize_toObj(formary,post);
				
				jQuery.post(goc_ajaxurl, post,
							function(data)
								{
									if (typeof(data.flash_message) != 'undefined')
									{
										jQuery('#flash_message').html(data.flash_message);
										jQuery('#flash_message').show('fast');
										setTimeout(function() { jQuery('#flash_message').hide('medium'); },4000);
									}
									if (typeof(data.result) != 'undefined')
									{
										jQuery('#my-account-page-content').html(data.result);
									}
								},"json");
			}
			
		}
		
		/*
		==================================================================
				Update the users password. 
		==================================================================  */
		this.update_pw 		= function(o) { _update_pw(o); }
		function _update_pw(o)
		{
			// get the form. 
			var form = jQuery(o).parents('form').get(0);
			if (goc_validate_form(o))
			{
				var post = {
						action: action, // this never changes.
						a: 'update_userpw'
					};
					
				var formary = jQuery(form).serializeArray();
				serialize_toObj(formary,post);
				
				jQuery.post(goc_ajaxurl, post,
							function(data)
								{
									if (typeof(data.flash_message) != 'undefined')
									{
										jQuery('#flash_message').html(data.flash_message);
										jQuery('#flash_message').show('fast');
										setTimeout(function() { jQuery('#flash_message').hide('medium'); },4000);
									}
									
									jQuery('.goc-nav-tab.is_active').trigger("click");
									
								},"json");
			}
		}
		
		/*
		==================================================================
				Update/Save user meta
		==================================================================  */
		this.update_usermeta 		= function(o) { _update_usermeta(o); }
		function _update_usermeta(o)
		{
			// get the form. 
			var form = jQuery(o).parents('form').get(0);
			if (goc_validate_form(o))
			{
				var post = {
						action: action, // this never changes.
						a: 'update_usermeta'
					};
					
				var formary = jQuery(form).serializeArray();
				serialize_toObj(formary,post);
				
				jQuery.post(goc_ajaxurl, post,
							function(data)
								{
									if (typeof(data.flash_message) != 'undefined')
									{
										jQuery('#flash_message').html(data.flash_message);
										jQuery('#flash_message').show('fast');
										setTimeout(function() { jQuery('#flash_message').hide('medium'); },4000);
									}
									
									jQuery('.goc-nav-tab.is_active').trigger("click");
									
								},"json");
			}
		}
		
		/*
		==================================================================
				My Orders Section
		==================================================================  */
		
		/*
		==================================================================
				Listeners
		==================================================================  */
		this.show_order_listener		= function() { _show_order_listener()}
		function _show_order_listener()
		{
			jQuery('.goc_view_orders .orderrow td').bind('click', function () { my_gocuser.handle_order_click(this); });
		}
		
		/*
		==================================================================
				handle order row clicked.
		==================================================================  */
		this.handle_order_click 		= function(o) { _handle_order_click(o); }
		function _handle_order_click(o)
		{
			var order	= jQuery(o).parents('.orderrow').attr("role");
			if (jQuery('#goc_bom-'+order).is(':visible') ) { jQuery('#goc_bom-'+order).hide('fast'); return;}
			
			var post = {
					action: action, // this never changes.
					a: 'show_order',
					order: order
				};
				
			jQuery.post(goc_ajaxurl, post,
						function(data)
							{
								if (typeof(data.result) != 'undefined')
								{
									jQuery('#goc_bom-'+order).html(data.result);
									jQuery('#goc_bom-'+order).show('medium');
								}
									
							},"json");
		}
		
		
		
	/*
		==================================================================
			serialize function, merge. 
		==================================================================  */
			function serialize_toObj(arr,o)
			{
				jQuery.each(arr, function(){ o[this.name]	= this.value; })
				return o;
			}
	}
	
	
/*
	==================================================================
	
		Form Validation functions. 
		
	==================================================================  */

	/*
		==================================================================
			Validate a form before sending it.
			- this is caused by a submit button being clicked.
		==================================================================  */
	function goc_validate_form(o,callback)
		{
			// get the form. 
			var form = jQuery(o).parents('form').get(0);
			// trigger the change on all of them.
			var form_status = jQuery.Event('change');
			jQuery(form).find('.is_required :input').each( function ()
														  {
															if (jQuery(this).attr("id") == 'billto_country') { form_status = goc_do_validate(this); }
															else { form_status = jQuery(this).triggerHandler('change'); }
															return (form_status);
															
															} );
			
			if (form_status == false) 	{ return false; }
			else
				{
					if (typeof(callback) != 'undefined' ) { eval(callback); return true; }
					//goc_form_submit(form);
				}
			return true;
		}

	/*
		==================================================================
			is_okay, is_not_okay trigger listeners. 
		==================================================================  */
	function goc_init_required_indicator_listenters()
	{
		jQuery('.is_required').bind('is_okay',function () { jQuery(this).addClass('is_okay'); } );
		jQuery('.is_required').bind('is_not_okay',function () { jQuery(this).removeClass('is_okay'); } );
	}
	
	function goc_init_required_field_listeners()
	{
		
		jQuery('.is_required').find(':input').bind('change',
				function() { return (goc_do_validate(this)) });
	}
	
	function goc_do_validate(o)
	{
		var is_valid = goc_validate_field(o);
		if (is_valid.pass == true ) { jQuery(o).parents('.goc_sym_row').trigger('is_okay'); return true;}
		else						{ alert("Error:\n"+is_valid.reason); jQuery(o).parents('.goc_sym_row').trigger('is_not_okay'); return false;}
	}
	
	function goc_uninit_required_field_listeners()
	{
		jQuery('.is_required').find(':input').unbind('change');
	}
	
	/*
		==================================================================
			A function to set the flag on all required fields.
			and start the listeners
		==================================================================  */
	function goc_init_required_fields()
		{
			goc_init_required_indicator_listenters(); // bind is_okay, is_not_okay listeners.
			goc_init_required_field_listeners();
			
			jQuery('.is_required').each( function () {
				var input_object	= jQuery(this).find(':input').get(0);
				if(	typeof(input_object) == 'undefined' ) 	{ return; }
				
				var is_valid = goc_validate_field( input_object );
				if ( is_valid.pass == true )						 			{ jQuery(this).trigger('is_okay'); 	}
				else														   	{ jQuery(this).trigger('is_not_okay'); }
			});
		}
	
	/*
		==================================================================
			A function to validate field input and return true/false
		==================================================================  */
	function goc_validate_field(o)
		{
			var val 	= jQuery(o).val();
			var role	= jQuery(o).attr("role");
			
			if (typeof(role) == 'undefined') { return ({'pass': true}); }

			switch(role)
				{
					case 'user_login':
						{
							val = val.replace(/[^\d\w\-]/g,"");
							jQuery(o).val(val);
							if (val.length < 6) {  return ({'pass': false, 'reason' : 'User names must be atleast 6 characters long'}) }
							// verify user is available.
							my_goc.userlogin_is_available(o);
							return ({'pass': true});
						}break;
						
					case 'first_name':
						{
							if (val.length < 2) {  return ({'pass': false, 'reason' : 'Please Enter a valid First Name'}) }
							return ({'pass': true});
						}break;
						
					case 'last_name':
						{
							if (val.length < 2) {  return ({'pass': false, 'reason' : 'Please Enter a valid Last Name'}) }
							return ({'pass': true});
						}break;
						
					case 'email':
						{
							val = val.replace(/[^\d\w\-\.@]/g,"");
							jQuery(o).val(val);
							if (val.length < 6 || !val.match(/^[\w|\.|-]+@[\w|\.|-]+$/)) {  return ({'pass': false, 'reason' : 'Valid email is required'}) }
						}break;
						
					case 'password':
						{
							val = val.replace(/ /g,"");
							jQuery(o).val(val);
							if (val.length < 6 ) { return ({'pass': false, 'reason' : 'Password must be atleast 6 characters long'}); }
						}break;
						
					case 'verify_pass':
						{
							val = val.replace(/ /g,"");
							jQuery(o).val(val);
							if (val.length < 6 ) { return ({'pass': false, 'reason' : 'Password must be atleast 6 characters long'}); }
							if (val != jQuery('#password').val() ) { return ({'pass': false, 'reason' : "Passwords don't match"}); }
						}break;
						
					case 'bill_to_name':
						{
							if (val.length < 4 ) { return ({'pass': false, 'reason' : 'Please enter a valid Bill To name'}); }
						}break;
						
					case 'bill_address':
						{
							if (val.length < 6 ) { return ({'pass': false, 'reason' : 'Please enter a valid Billing Address'}); }
						}break;
						
					case 'bill_city':
						{
							if (val.length < 3 ) { return ({'pass': false, 'reason' : 'Please enter a valid Bill City'}); }
						}break;
						
					case 'bill_state':
						{
							if (val.length < 2 ) { return ({'pass': false, 'reason' : 'Please select your Billing State'}); }
						}break;
						
					case 'ship_address':
					case 'address':
						{
							if (val.length < 6 ) { return ({'pass': false, 'reason' : 'Please enter a valid Address'}); }
						}break;
						
					case 'ship_city':
					case 'city':
						{
							if (val.length < 3 ) { return ({'pass': false, 'reason' : 'Please enter a valid City'}); }
						}break;
						
					case 'ship_state':
					case 'state':
						{
							if (val.length < 2 ) { return ({'pass': false, 'reason' : 'Please select your State'}); }
						}break;
						
					case 'bill_zip':
						{
							val = val.replace(/[^0-9\-]/g,"");
							jQuery(o).val(val);
							if (val.length < 5 ) { return ({'pass': false, 'reason' : 'Please enter a valid Billing Zipcode'}); }
						}break;
						
					case 'shipto_zip':
						{
							val = val.replace(/[^0-9\-]/g,"");
							jQuery(o).val(val);
							if (val.length < 5 ) { return ({'pass': false, 'reason' : 'Please enter a valid Ship to Zipcode'}); }
						}break;
						
					case 'international_zip':
						{
							if (val.length < 5) {  return ({'pass': false, 'reason' : 'Please Enter a Zip/Postal code'}) }
						}break;
						
					case 'select_country':
						{
							if (val.length < 2) {  return ({'pass': false, 'reason' : 'Please Select Your Country'}) }
						}break;
				}
				
			 return ({'pass': true})
		}
	