var Usermanage = function()
{
//privat
	var tabs = Array("nullTab", "#profileTab", "#tasteTab", "#shippingTab", "#billingTab", "#clubTab", "#registryTab", "#orderTab");
	var fields = Array();

	var InArray = function(val, array)
	{
		for (var i in array)
		{
			if (array[i] == val)
			{
				return true;
			}
		}
		return false;
	}

//public
return {
	Initialize: function(aFields)
	{
		fields = aFields;
	},

	SetPhoto: function(image)
	{ 
     
     jQuery("#imageUpload").replaceWith("<input id=\"imageUpload\" type=\"file\" onkeydown=\"return false;\" onchange=\"this.parentNode.submit()\" name=\"photo\"/>");
		image = jQuery(image).attr("photo");
		var photo = jQuery("#photo");
		photo.attr("src", jQuery("#uploadPath").val() + image);
		photo.attr("photo", image);
              $("#is_generic").val("generic");
       
      
                
	},
SetPhotofb: function(image,uid)
        {
          jQuery("#imageUpload").replaceWith("<input id=\"imageUpload\" type=\"file\" onkeydown=\"return false;\" onchange=\"this.parentNode.submit()\" name=\"photo\"/>");
                image = jQuery(image).attr("photo");
                var photo = jQuery("#photo");
                photo.attr("src", "http://graph.facebook.com/"+uid+"/picture?type=normal");

                photo.attr("photo", image);

                $("#is_generic").val("non_generic");
        },


	SetUploadPhoto: function(iframe)
	{
		image = iframe.contentWindow.document.body.innerHTML;
		if (image != "")
		{
			var photo = jQuery("#photo");
			photo.attr("src", jQuery("#photoPath").val() + image);
			photo.attr("photo", image);
		}
	},

	ValidateFields: function(tab, exception)
	{
		for (var i in fields)
		{
			var field = fields[i];
			 
			   if(field.field == "zip")
		        {  
				   
				 	var value = jQuery.trim(jQuery(tabs[tab] + " #" + field.field).val());

					if(value!="" && value.length<5)
					{
					alert("Zip code must be minimum 5 character.");
					return false;
					}
				 }
				// if (field.field == "username")
				// {
					// var value = jQuery.trim(jQuery(tabs[tab] + " #" + field.field).val());
					// if(value == "")
					// {
					// alert("Please enter username.");
					// jQuery.trim(jQuery(tabs[tab] + " #" + field.field).focus());
					// return false;
					// }
					
				// } 
			if (InArray(tab, field.tabs) !== false)
			{
				
				var value = jQuery.trim(jQuery(tabs[tab] + " #" + field.field).val());
				
				
				
				if (exception && (field.field == "pass1") && (value == ""))
				{
					continue;
				}
				if ((field.field == "username") && (value == ""))
				{
					continue;
				}
				
				if ((field.field == "zip") && (value == ""))
				{
					continue;
				}
			
			switch (field.type)
				{
					case "text":
						if (value == "")
						{
							if (field.field == "username")
							{
								continue;
							}
							if (field.field == "state")
							{	
								if($('.country_name:visible').val() == 1)
								{
									alert(field.message);
									jQuery(tabs[tab] + " #" + field.field).focus();
									return false;
								}
								if($('.country_name:visible').val()!= 1)
								{
									if($('#province').val() == "")
									{
										alert("Please fill your state");
										jQuery(tabs[tab] + " #province").focus();
										return false;
									}
									else							
									continue;
								}
							}
							else
							{
								alert(field.message);
								jQuery(tabs[tab] + " #" + field.field).focus();
								return false;
							}
						}
						  
						break;
					case "radio":
						value = jQuery.trim(jQuery(tabs[tab] + " input[name='" + field.field + "']:checked").val());
						if (value == "")
						{
							if(field.field == "display_name")
							{
								//$('#USERNAME').parent().addClass('error');
								$('#optPrivacy').focus();
								$('#CHUNKNAME').focus();
								//$('#USERNAME').parent().prev().addClass('error').prev().addClass('error');
							}

							alert(field.message);
							$(tabs[tab] + " #" + field.field).focus();
							return false;
						}
						break;
					case "int":
						if (/^\d+$/.test(value) == false)
						{
							alert(field.message);
							jQuery(tabs[tab] + " #" + field.field).focus();
							return false;
						}
						break;
					case "test":
						var exp = RegExp(field.test);
						if (exp.test(value) == false)
						{
							alert(field.message);
							jQuery(tabs[tab] + " #" + field.field).focus();
							return false;
						}
				}
			}
		}
	},

	GetAddress: function(tab)
	{
        
		return		"nickName="+ $.trim($(tab + " #nickName").val())
				+	"&firstName="+ $.trim($(tab + " #firstName").val().replace(/&/, '%26'))
				+	"&lastName="+ $.trim($(tab + " #lastName").val().replace(/&/, '%26'))
				+	"&address1=" + $.trim($(tab + " #address1").val())
				+	"&address2=" + $.trim($(tab + " #address2").val())
				+	"&city=" + $.trim($(tab + " #city").val())
                                +	"&province=" + $.trim($(tab + " #province").val())
				+	"&state=" + $(tab + " #state").val()
				+	"&zip=" + $(tab + " #zip").val()
				+	"&country=" + $(tab + " #country").val()
				+	"&mob=" + $.trim($(tab + " #mob").val())
				+	"&work=" + $.trim($(tab + " #work").val())
				+	"&home=" + $.trim($(tab + " #home").val());
	},

	Redirect: function(target)
	{
		var url = window.location.href.match(/redirect=([\da-zA-Z%.]+)/);

		switch (target)
		{
			case "profile":
				window.location = "/user/profile" + (url != null ? "?redirect=" + url[1] : "");
				break;
			case "tryRedirect":
				if (url != null)
				{
					window.location = decodeURIComponent(url[1]);
				}
				else
				{
					window.location = "/user/profile" + (url != null ? "?redirect=" + url[1] : "");
				}
				break;
			default:
				if (url != null)
				{
					window.location = decodeURIComponent(url[1]);
				}
		}
	}
};
}();

