(function()
{
	PROFILE = ({
	
		settings:{formid:null},
		createProfile: function()
		{
			PROFILE.settings.formid = jQuery("#profile_frm");
			
			GSM_LOADER.show("Please Wait.....");
			
			jQuery.ajax({
			
				type	:	PROFILE.settings.formid.attr("method"),
				url		:	PROFILE.settings.formid.attr("action"),
				data	:	PROFILE.settings.formid.serialize(),
				dataType:	'json',
				success	:	function(rs)
				{
					if(rs.error == 'false')
					{
						location.href = rs.redirect;
					}
					else
					{
						GSM_LOADER.hideWithMsg(rs.message);
					}
				},
				
				error	:	function()
				{
					GSM_LOADER.hideWithMsg('Unable to process your request');
				}
					
			});			
		}	
	});  
  
})(jQuery);

(function()
{
	GSM_PROFILE = ({
	is_error:false,
	
	updateProfile: function()
	{				
		if(!this.is_error)
		{
			GSM_LOADER.show("Saving your account");
			var param = this.getParam();
			
			jQuery.ajax({
				type:'post',
				data:param,
				url:'/account/update-profile',
				success: function(rs)
				{
					GSM_LOADER.hideWithMsg(rs);
				}
						
			});
						
		}
	},


	createProfile: function()
	{				
		if(this.validate())
		{
			GSM_LOADER.show("Creating your account");
			var param = $('#profile_new_frm').serialize();
			
			jQuery.ajax({
				type:'post',
				data:param,
				dataType:'json',	
				url:'/account/create-profile',
				success: function(rs)
				{
					if(rs.error == "false")
					{
						GSM_LOADER.hideWithMsg(rs.message);	
					}
					else
					{
						GSM_LOADER.hideWithMsg(rs.message);	
					}
					//GSM_LOADER.hideWithMsg(rs);
				}
						
			});
						
		}
	},
	
	validate: function()
	{
		GSM_LOADER.show("Please wait...");
		
		var errors = new Array();
		
		var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;

		
		if(jQuery.trim(jQuery("#email_address").val())=='')
		{
			errors.push("- Email Address");	
		}
		else if(!regex.test(jQuery("#email_address").val()))
		{
			errors.push("- Email Address");	
		}
		
		if(jQuery.trim(jQuery("#password").val())=='')
		{
			errors.push("- Password");	
		}
		
		if(jQuery.trim(jQuery("#first_name").val())=='')
		{
			errors.push("- First Name");	
		}
		
		if(jQuery.trim(jQuery("#last_name").val())=='')
		{
			errors.push("- Last Name");	
		}
		
		if(jQuery.trim(jQuery("#phone_number").val())=='')
		{
			errors.push("- Phone Number");	
		}
		
		if(jQuery.trim(jQuery("#address").val())=='')
		{
			errors.push("- Address");	
		}
		
		if(jQuery.trim(jQuery("#city").val())=='')
		{
			errors.push("- City");	
		}
		
		if(jQuery.trim(jQuery("#zip").val())=='')
		{
			errors.push("- Zip");	
		}
		
		if(jQuery.trim(jQuery("#mobile_phone_number").val())=='')
		{
			errors.push("- Mobile Number");	
		}
		
		if(jQuery("#hear_from").val()=="Other")
		{
			if(jQuery.trim(jQuery("#hear_from_text").val())=='')
			{
				errors.push("- Hear From");	
			}		    
		}
		else if(jQuery("#hear_from").val()==0)
		{
		    errors.push("- Hear From");	 
		}
		
		if(jQuery("#scode").is(":visible"))
		{		
			if(jQuery.trim(jQuery("#scode").val())=='')
			{
				errors.push("- Security Code");	
			}
		}
		
		if(errors.length)
		{
			var str = "Following fields have invalid values.<br><br>";
			str +=  errors.join("<br>");
			GSM_LOADER.hideWithMsg(str);
			return false;	
		}
		return true;
	},
	
	setHearFrom:function()
	{
		if(jQuery("#hear_from").val()=="Other")
		{
		    jQuery("#hear_from_text").css({display:''});
		}
		else
		{
		    jQuery("#hear_from_text").css({display:'none'});    
		}
	},
	
	

	setStateField: function(country, state)
	{
		GSM_LOADER.show("Loading content...");
		var param = 'country='+escape(country)+'&state='+escape(state);
		
		jQuery.ajax({
			type:'post',
			data:param,
			url:'/account/set-statecombo',
			success: function(rs)
			{
				GSM_LOADER.hide(10);
				jQuery("#stateCombo").html(rs);
				jQuery("#stateComboTr").fadeIn(300);
			}
					
		});
	},
	
	setStateField_withoutlogin: function(country, state)
	{
		GSM_LOADER.show("Loading content...");
		var param = 'country='+escape(country)+'&state='+escape(state);
		
		jQuery.ajax({
			type:'post',
			data:param,
			url:'/account/set-statecombowithoutlogin',
			success: function(rs)
			{
				GSM_LOADER.hide(1000);
				jQuery("#stateCombo").html(rs);
				jQuery("#stateComboTr").fadeIn(300);
				
			}
					
		});
	}
	
	});

})(jQuery);

jQuery(document).ready(function()
{
	jQuery("#profile_edit_frm INPUT").bind("keyup click",function(e)
	{ 
		if(e.keyCode == 13)
		{
			GSM_PROFILE.updateProfile();  
		}
		else
		{
			GSM_MSG.hide(10);
		} 
	});
	
	jQuery("#profile_new_frm INPUT").bind("keyup click",function(e)
	{ 
		if(e.keyCode == 13)
		{
			GSM_PROFILE.createProfile();  
		}
		else
		{
			GSM_MSG.hide(10);
		} 
	});
		
});


