// Helper Functions
/* Start: String Format */
var string =
{
	format: function( text )
	{
		//check if there are two arguments in the arguments list
		if ( arguments.length <= 1 )
		{
			//if there are not 2 or more arguments there's nothing to replace
			//just return the original text
			return text;
		}
		//decrement to move to the second argument in the array
		var tokenCount = arguments.length - 2;
		for( var token = 0; token <= tokenCount; token++ )
		{
			//iterate through the tokens and replace their placeholders from the original text in order
			text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
		}
		return text;
	}
}
/* End: String Format */

// Robichaux Main Class
var r =
{
	google: function ()
	{
		
	},
	validation:
	{
		isEmailValid: function (args)
		{
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			return pattern.test(args);
		},
		isSpam: function (args)
		{
			var out = false;
			for (var i in args)
			{
				if(!isNaN(args[i]))
				{
					out = true;
				}
				else if(args.indexOf('>') != -1)
				{
					out = true;
				}
				else if(args.indexOf('<') != -1)
				{
					out = true;
				}
			}
			return out;
		}
	},
	xml: function (args)
	{
		$.ajax({
			type: "GET",
			url: args.file,
			dataType: "xml",
			error: function (XMLHttpRequest, textStatus, errorThrown)
			{
				alert
				(
					string.format
					(
						"{0}\n\n{1}\n\n{2}",
						XMLHttpRequest,
						textStatus,
						errorThrown
					)
				)
			},
			success: function (xml)
			{
				if(false)
				{
					var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");  
					xmlDoc.loadXML(xml); 
					xml = xmlDoc;
					args.set(xml);
				}
				else
				{
					args.set(xml);
				}
			}
		});
	},
	get:
	{
		states: function ()
		{
			if ($("#State").length != 0)
			{
				r.xml
				({
					file: "/xml/states.xml",
					set: r.set.states
				});
			}
		},
		countries: function ()
		{
			if($("#Country").length != 0)
			{
				r.xml
				({
					file: "/xml/countries.xml",
					set: r.set.countries
				});
			}
		}
	},
	set:
	{
		states: function (xml)
		{
			var html = "";
			$(xml).find('state').each
			(
				function ()
				{
					html += string.format
					(
						"<option value=\"{1}\">{0}</option>\n",
						$(this).attr('name'),
						$(this).attr('abbreviation')
					);
				}
			);
			$("#State").html(html);
		},
		countries: function (xml)
		{
			var html = "";
			$(xml).find('country').each
			(
				function ()
				{
					html += string.format
					(
						"<option value=\"{1}\">{0}</option>\n",
						$(this).attr('name'),
						$(this).attr('code')
					);
				}
			);
			$("#Country").html(html);
		}
	},
	contact:
	{
		init: function ()
		{
			//r.get.states();
			//r.get.countries();
			$('#IBDOtherCheck').click
			(
				function ()
				{
					r.contact.toggleIBD();
				}
			);
			if($('#IBDOtherCheck').attr('checked'))
			{
				r.contact.toggleIBD();
			}
			$('button').first().click
			(
				function ()
				{
					r.contact.validate();
				}
			)
		},
		clearErrors: function ()
		{
			$('.formErrors').first().fadeOut('slow');
			$('input, textarea, select').each
			(
				function ()
				{
					$(this).css('background-color', '#ffffff');
				}
			)
		},
		showErrors: function (args)
		{
			r.contact.hadErrors = true;
			if (!$.browser.opera)
			{
				$('html, body').animate({scrollTop:0}, 'slow');
			}
			else
			{
				scroll(0,0);
			}
			if (!$.browser.msie)
			{
				$('.formErrors').first().fadeIn(1500);
			}
			else
			{
				$('.formErrors').first().css('display', 'block');
			}
			$('.formErrors').first().css('margin-bottom', '10px');
			var ThenWhoWasPhone = function ()
			{
				var a = args;
				var r = new Array();
				o:for(var i = 0, n = a.length; i < n; i++)
				{
					for(var x = 0, y = r.length; x < y; x++)
					{
						if(r[x]==a[i]) continue o;
					}
					r[r.length] = a[i];
				}
				return r;
			}
			
			var html = "";
			$(ThenWhoWasPhone()).each
			(
				function ()
				{
					html += string.format("<li><span style=\"color: #000000;\">{0}</span></li>", this);
				}
			);
			$('.formErrors ul').html(html);
		},
		submit: function ()
		{
			$('button').first().attr('disabled', 'true');
			$('button').first().html('<img src="/img/loader.gif" style="position: relative; top: 2px;">');
			$('form').submit();
		},
		toggleIBD: function ()
		{
			var obj = $('#IBDOther');
			var cssVal = obj.css('display');
			if ( cssVal == "none" )
			{
				obj.fadeIn('fast')
			}
			else
			{
				obj.fadeOut('fast');
			}
		},
		validate: function ()
		{
			var isValid = true;
			var aErrors = [];
			var Required =
			{
				City: "City",
				Confirm: "Read and understood the Terms &amp; Conditions.",
				Country: "Country",
				DateOfUse: "Date you began using accutane",
				Description: "Description of the nature and extent of your injuries resulting from your use of Accutane if no diagnosis or other option is selected.",
				EMail: "Email Address",
				FirstName: "First Name",
				LastName: "Last Name",
				PrimaryPhone_1: 'Primary Phone Number is incomplete.',
				PrimaryPhone_2: 'Primary Phone Number is incomplete.',
				PrimaryPhone_3: 'Primary Phone Number is incomplete.',
				SpamFLC: "First Name, Last Name, and City cannot contain numerical values.",
				SpamDescription: "Description and Comments cannot contain greater than (>) or less than (<) characters.",
				State: "State",
				Zip: "Zip Code"
			}
			$('input, textarea, select').each
			(
				function ()
				{
					var doError = function (args)
					{
						$(args).css("background-color", "#fbf49b");
						isValid = false;
						var name = args.name;
						var getName = Required[args.name];
						if (getName)
						{
							name = getName;
						}
						aErrors.push(name);
					}
					if (this.value == "" || this.value== "Select a State" || this.value== "Select a Country")
					{
						if (Required[this.name] && this.name != "Description")
						{
							doError(this);
						}
						else if(this.name == "Description")
						{
							if ($('#isDescriptionNeeded > input[type=checkbox]:checked').size() == 0)
							{
								doError(this);
							}
						}
					}
					else if (this.name == "EMail")
					{
						if(!r.validation.isEmailValid(this.value))
						{
							Required.EMail = "Email specified is invalid.";
							doError(this);
						}
						else
						{
							$(this).css("background-color", "#ffffff");
						}
					}
					//else if (this.name == "FirstName" || this.name == "LastName" || this.name == "City"&& r.validation.isSpam(this.value))
					//{
					//	var orgName = this.name;
					//	this.name = "SpamFLC";
					//	doError(this);
					//	this.name = orgName;
					//}
					//else if (this.name == "Description" || this.name == "Comments" && r.validation.isSpam(this.value))
					//{
					//	var orgName = this.name;
					//	this.name = "SpamDescription";
					//	doError(this);
					//	this.name = orgName;
					//}
					else
					{
						$(this).css("background-color", "#ffffff");
					}
					if (this.type == "checkbox")
					{
						if (this.name == "Confirm" && !this.checked)
						{
							doError(this);
						}
						if (this.name == "hasOther" && this.checked)
						{
							if (!$('textarea[name=Description]').val())
							{
								doError($('textarea[name=Description]')[0]);
							}
						}
					}
				}
			);
			if (!isValid)
			{
				r.contact.showErrors(aErrors);
			}
			else
			{
				r.contact.clearErrors();
				r.contact.submit();
			}
			return isValid;
		}
	},
	home:
	{
		init: function ()
		{
			$('div.title div div').each
			(
				function ()
				{
					$(this).click
					(
						function ()
						{
							window.location = $(this).attr("title");
						}
					)
				}
			)
		}
	},
	recommend:
	{
		init: function ()
		{
			if (r.recommend.isCurrentPage)
			{
				$('input[type=text]').each
				(
					function ()
					{
						$(this).keyup(r.recommend.get);
						$(this).keypress(r.recommend.get);
					}
				)
				$('form').submit
				(
					function ()
					{
						return r.recommend.validation();
					}
				)
			}
		},
		validation: function ()
		{
			var val = true;
			var toggleErrDisp = function (args)
			{
				if (true)
				{
					$('.error').css('display', 'block');
				}
				else
				{
					$('.error').css('display', 'none');
				}
			}
			var errors = function (obj)
			{
				val = false;
				obj.css('background-color', '#fbf49b');
			}
			$('input[type="text"]').each
			(
				function ()
				{
					if(!$(this).val().toString().replace(/ /, '').length > 0)
					{
						errors($(this));
					}
					if($(this).attr('name').toString().toLowerCase().indexOf('email') > -1)
					{
						if (!r.validation.isEmailValid($(this).val()))
						{
							errors($(this));
						}
					}
				}
			);
			if(!val)
			{
				toggleErrDisp(true);
			}
			else
			{
				toggleErrDisp();
			}
			return val;
		},
		isCurrentPage: window.location.toString().indexOf('recommend.asp') > -1,
		get: function ()
		{
			var isNotEmpty = function (jobj)
			{
				//return jobj.val().length > 0
				return true;
			}
			$('input[type="text"]').each
			(
				function ()
				{
					if(isNotEmpty($(this)))
					{
						var val = string.format(' {0}',$(this).val());
						var id = string.format('#{0}',$(this).attr('name'));
						$(id).text(val == ' ' ? '' : val);
					}
				}
			)
		}
	},
	init: function ()
	{
		try
		{
		$(document).ready
		(
			function ()
			{
				r.home.init();
				r.contact.init();
				r.recommend.init();
				//r.google();
			}
		)
		}
		catch (e)
		{
			alert(e);
		}
	}
}
r.init();