/**
 * Autotab - jQuery plugin 1.1b
 * http://www.lousyllama.com/sandbox/jquery-autotab
 * 
 * Copyright (c) 2008 Matthew Miller
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 * 
 * Revised: 2008-09-10 16:55:08
 */

(function($) {
// Look for an element based on ID or name
var check_element = function(name) {
	var obj = null;
	var check_id = $('#' + name);
	var check_name = $('input[name=' + name + ']');

	if(check_id.length)
		obj = check_id;
	else if(check_name != undefined)
		obj = check_name;

	return obj;
};

/**
 * autotab_magic automatically establishes autotabbing with the
 * next and previous elements as provided by :input.
 * 
 * autotab_magic should called after applying filters, if used.
 * If any filters are applied after calling autotab_magic, then
 * Autotab may not protect against brute force typing.
 * 
 * @name	autotab_magic
 * @param	focus	Applies focus on the specified element
 * @example	$(':input').autotab_magic();
 */
$.fn.autotab_magic = function(focus) {
	for(var i = 0; i < this.length; i++)
	{
		var n = i + 1;
		var p = i - 1;

		if(i > 0 && n < this.length)
			$(this[i]).autotab({ target: $(this[n]), previous: $(this[p]) });
		else if(i > 0)
			$(this[i]).autotab({ previous: $(this[p]) });
		else
			$(this[i]).autotab({ target: $(this[n]) });

		// Set the focus on the specified element
		if(focus != null && (isNaN(focus) && focus == $(this[i]).attr('id')) || (!isNaN(focus) && focus == i))
			$(this[i]).focus();
	}
	return this;
};

/**
 * This will take any of the text that is typed and
 * format it according to the options specified.
 * 
 * Option values:
 *	format		text|number|alphanumeric|all|custom
 *	- Text			Allows all characters except numbers
 *	- Number		Allows only numbers
 *	- Alphanumeric	Allows only letters and numbers
 *	- All			Allows any and all characters
 *	- Custom		Allows developer to provide their own filter
 *
 *	uppercase	true|false
 *	- Converts a string to UPPERCASE
 * 
 *	lowercase	true|false
 *	- Converts a string to lowecase
 * 
 *	nospace		true|false
 *	- Remove spaces in the user input
 * 
 *	pattern		null|(regular expression)
 *	- Custom regular expression for the filter
 * 
 * @name	autotab_filter
 * @param	options		Can be a string, function or a list of options. If a string or
 *						function is passed, it will be assumed to be a format option.
 * @example	$('#number1, #number2, #number3').autotab_filter('number');
 * @example	$('#product_key').autotab_filter({ format: 'alphanumeric', nospace: true });
 * @example	$('#unique_id').autotab_filter({ format: 'custom', pattern: '[^0-9\.]' });
 */
$.fn.autotab_filter = function(options) {
	var defaults = {
		format: 'all',
		uppercase: false,
		lowercase: false,
		nospace: false,
		pattern: null
	};

	if(typeof options == 'string' || typeof options == 'function')
		defaults.format = options;
	else
		$.extend(defaults, options);

	for(var i = 0; i < this.length; i++)
	{
		$(this[i]).bind('keyup', function(e) {
			var val = this.value;

			switch(defaults.format)
			{
				case 'text':
					var pattern = new RegExp('[0-9]+', 'g');
					val = val.replace(pattern, '');
					break;

				case 'alpha':
					var pattern = new RegExp('[^a-zA-Z]+', 'g');
					val = val.replace(pattern, '');
					break;

				case 'number':
				case 'numeric':
					var pattern = new RegExp('[^0-9]+', 'g');
					val = val.replace(pattern, '');
					break;

				case 'alphanumeric':
					var pattern = new RegExp('[^0-9a-zA-Z]+', 'g');
					val = val.replace(pattern, '');
					break;

				case 'custom':
					var pattern = new RegExp(defaults.pattern, 'g');
					val = val.replace(pattern, '');
					break;

				case 'all':
				default:
					if(typeof defaults.format == 'function')
						var val = defaults.format(val);

					break;
			}

			if(defaults.nospace)
			{
				var pattern = new RegExp('[ ]+', 'g');
				val = val.replace(pattern, '');
			}

			if(defaults.uppercase)
				val = val.toUpperCase();

			if(defaults.lowercase)
				val = val.toLowerCase();

			if(val != this.value)
				this.value = val;
		});
	}
};

/**
 * Provides the autotabbing mechanism for the supplied element and passes
 * any formatting options to autotab_filter.
 * 
 * Refer to autotab_filter's description for a detailed explanation of
 * the options available.
 * 
 * @name	autotab
 * @param	options
 * @example	$('#phone').autotab({ format: 'number' });
 * @example	$('#username').autotab({ format: 'alphanumeric', target: 'password' });
 * @example	$('#password').autotab({ previous: 'username', target: 'confirm' });
 */
$.fn.autotab = function(options) {
	var defaults = {
		format: 'all',
		maxlength: 2147483647,
		uppercase: false,
		lowercase: false,
		nospace: false,
		target: null,
		previous: null,
		pattern: null
	};

	$.extend(defaults, options);

	// Sets targets to element based on the name or ID
	// passed if they are not currently objects
	if(typeof defaults.target == 'string')
		defaults.target = check_element(defaults.target);

	if(typeof defaults.previous == 'string')
		defaults.previous = check_element(defaults.previous);

	var maxlength = $(this).attr('maxlength');

	// defaults.maxlength has not changed and maxlength was specified
	if(defaults.maxlength == 2147483647 && maxlength != 2147483647)
		defaults.maxlength = maxlength;
	// defaults.maxlength overrides maxlength
	else if(defaults.maxlength > 0)
		$(this).attr('maxlength', defaults.maxlength)
	// defaults.maxlength and maxlength have not been specified
	// A target cannot be used since there is no defined maxlength
	else
		defaults.target = null;

	if(defaults.format != 'all')
		$(this).autotab_filter(defaults);

	// Go to the previous element when backspace
	// is pressed in an empty input field
	return $(this).bind('keydown', function(e) {
		if(e.which == 8 && this.value.length == 0 && defaults.previous)
			defaults.previous.focus().val(defaults.previous.val());
	}).bind('keyup', function(e) {
		/**
		 * Do not auto tab when the following keys are pressed
		 * 8:	Backspace
		 * 9:	Tab
		 * 16:	Shift
		 * 17:	Ctrl
		 * 18:	Alt
		 * 19:	Pause Break
		 * 20:	Caps Lock
		 * 27:	Esc
		 * 33:	Page Up
		 * 34:	Page Down
		 * 35:	End
		 * 36:	Home
		 * 37:	Left Arrow
		 * 38:	Up Arrow
		 * 39:	Right Arrow
		 * 40:	Down Arrow
		 * 45:	Insert
		 * 46:	Delete
		 * 144:	Num Lock
		 * 145:	Scroll Lock
		 */
		var keys = [8, 9, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];

		if(e.which != 8)
		{
			var val = $(this).val();

			if($.inArray(e.which, keys) == -1 && val.length == defaults.maxlength && defaults.target)
				defaults.target.focus();
		}
	});
};

})(jQuery);




// Copyright 2010 Website Talking Heads
// JavaScript Document
if (typeof wthvideo == "undefined") {
	wthvideo = new Object();
}
wthvideo.params = {
	width:256,
	height:384,
	position:"fixed",
	doctype:"strict",
	left:"auto",
	right:"0px",
	top:"auto",
	bottom:"0px",
	centeroffset:"auto",
	color:0xCCCCCC,
	volume:70,
	autostart:"yes",
	fadein:0,
	fadeout:2,
	flip:"no",
	delay:0,
	delayclose:0,
	buffertime:3,
	controlbar:"mouse",
	exitbtn:"yes",
	playbtn:"PlayVideo.png",
	playposition:"center",
	playtop:"center",
	exitoncomplete:"yes",
	oncepersession:"no",
	vidlink:"no",
	openin:"_blank",
	path:"wthvideo",
	actorpic:"chantelhealthyhabitsweb2231.png",
	flv:"chantelhealthyhabitsweb2231.flv"};
	
var topPx = parseFloat(wthvideo.params.top);
var bottomPx = parseFloat(wthvideo.params.bottom);

wthvideo.hideDiv = function(){
	document.getElementById('wthvideo').style.visibility = 'hidden';	
}
function onlyOnce() {
if (document.cookie.indexOf("hasSeen=true") == -1) {
var later = new Date();
later.setFullYear(later.getFullYear()+10);
document.cookie = 'hasSeen=true;path=/;';
wthvideo.drawVideo();
}
}

wthvideo.callme = function(){
	// Add to callback
	//console.log("call action");
	submitClickToCall();
	$("#c2cnum").html("You will be called back as soon as possible.");
	$("#c2c").html("Thank You!");
}

wthvideo.drawCallme = function(){
	var open = "false";
	$.ajax({
		url: "/wthvideo/open.php",
		async: false,
		success: function(data){
			open = ""+data;
			//console.log(data);
		}
	});
	// display pretty graphic
	$(document).ready(
		function(){
			var wthpos = $("#wthvideo").offset();
			var wthwidth = $("#wthvideo").width();
			$("body").append("<div id='c2c' style='cursor: pointer;'><img src='/images/c2c.jpg' alt='Click To Call'/></div>");
			$("#c2c").css( { "left": (wthpos.left+155) + "px", "top": (wthpos.top-45) + "px" } );
			// if click, display enter phone form
			$("#c2c").ready(function(){
				if (open == "true"){
					$("#c2c").click(function(){
						if ($("#c2cnum").length <= 0) {
							$("body").append("<div id='c2cnum'><input id='c2c1' type='text' size='3' maxlength='3' value=''/> <input id='c2c2' type='text' size='3' maxlength='3' value=''/> <input id='c2c3' type='text' size='4' maxlength='4' value=''/></div>");
							$("#c2cnum").css( { "left": (wthpos.left+80) + "px", "top": (wthpos.top-25) + "px" } );
							$("#c2cnum").ready(function(){
								$("#c2c").html("Enter Your #");
								$("#c2c1").focus();
								$("#c2c1, #c2c2, #c2c3").autotab_magic().autotab_filter('numeric');
								$("#c2c3").keyup(function(e){
									var code = (e.KeyCode ? e.KeyCode : e.which);
									//console.log("KeyCode: "+code+"\nValue: "+$(this).val());
									var num = String.fromCharCode(code);
									/*if (num.match(/[0-9]/g)){
										//console.log("KeyCode: "+code+"\nInvalid: "+num);
										return;
									}*/
									if ($(this).val().length == 4){
										wthvideo.callme();
									}
								});
							});
						}
					});
				} else {
					$("#c2c").click(function(){
						alert("Hours of operation are from 8-5pm Arizona Time.\nYou will be called back during business hours.\nHowever, Please feel free to leave a message.");
						window.location = "http://www.healthysolutionsweb.com/contact-us";
					});
					
					//console.log("false");
				}				
			});
		}
	);
	
}

wthvideo.drawVideo= function(){
	var markUp = '';
	markUp += '<style type="text/css">';
	markUp += '#wthvideo {position:'+wthvideo.params.position+';width:'+wthvideo.params.width+'px;height:'+wthvideo.params.height+'px;margin-left:'+wthvideo.params.centeroffset+';left:'+wthvideo.params.left+';right:'+wthvideo.params.right+';top:'+wthvideo.params.top+';bottom:'+wthvideo.params.bottom+';z-index:99999;}';
	markUp += '</style>';
	markUp += '<div id="wthvideo">';
	markUp += '  <object id="objvideo" style="outline:none;" type="application/x-shockwave-flash" width="'+wthvideo.params.width+'" height="'+wthvideo.params.height+'" data="'+wthvideo.params.path+'/wthplayer.swf">';
	markUp += '    <param name="movie" value="'+wthvideo.params.path+'/wthplayer.swf" />';
	markUp += '    <param name="quality" value="high" />';
	markUp += '    <param name="flashvars" value="vurl='+wthvideo.params.flv+'&amp;vwidth='+wthvideo.params.width+'&amp;vheight='+wthvideo.params.height+'&amp;actorpic='+wthvideo.params.path+'/'+wthvideo.params.actorpic+'&amp;autostart='+wthvideo.params.autostart+'&amp;exitoncomplete='+wthvideo.params.exitoncomplete+'&amp;vbuff='+wthvideo.params.buffertime+'&amp;vdelay='+wthvideo.params.delay+'&amp;vcolor='+wthvideo.params.color+'&amp;vlink='+wthvideo.params.vidlink+'&amp;openin='+wthvideo.params.openin+'&amp;delayclose='+wthvideo.params.delayclose+'&amp;fadein='+wthvideo.params.fadein+'&amp;fadeout='+wthvideo.params.fadeout+'&amp;vvol='+wthvideo.params.volume+'&amp;playbtn='+wthvideo.params.path+'/'+wthvideo.params.playbtn+'&amp;playpos='+wthvideo.params.playposition+'&amp;playtop='+wthvideo.params.playtop+'&amp;hflip='+wthvideo.params.flip+'&amp;controlbar='+wthvideo.params.controlbar+'&amp;exitbtn='+wthvideo.params.exitbtn+'" />';
	markUp += '    <param name="wmode" value="transparent" />';
	markUp += '    <param name="allowscriptaccess" value="always" />';
	markUp += '    <param name="swfversion" value="9.0.45.0" />';
	markUp += '  </object>';
	markUp += '</div>';
	if (wthvideo.params.position == "fixed") {
		if (wthvideo.params.doctype == "quirks") {
			if (wthvideo.params.top == "auto") {
						markUp += '<!--[if IE]>';
						markUp += '<style type="text/css">';
						markUp += '#wthvideo {position:absolute; top: expression(offsetParent.scrollTop - 1 + (offsetParent.clientHeight-this.clientHeight) + '+bottomPx+' + "px")}';
						markUp += '</style>';
						markUp += '<![endif]-->';}
					else {
							markUp += '<!--[if IE]>';
							markUp += '<style type="text/css">';
							markUp += '#wthvideo {position: absolute !important;top: expression(((document.documentElement.scrollTop || document.body.scrollTop) + (!this.offsetHeight && 0)) + '+topPx+' + "px")';
							markUp += '</style>';
							markUp += '<![endif]-->';}
					}
				else {
						markUp += '<!--[if lte IE 6]>';
						markUp += '<style type="text/css">';
						markUp += 'html, body{height: 100%;overflow: auto;}#wthvideo {position: absolute;}';
						markUp += '</style>';
						markUp += '<![endif]-->';
			}
		}		
	document.write(markUp);
	wthvideo.drawCallme();
}
function hideDiv() {
	wthvideo.hideDiv();
}

if (wthvideo.params.autostart=="oncethenpic") {
	if (document.cookie.indexOf("hasSeen=true") == -1) {
		var later = new Date();
		later.setFullYear(later.getFullYear()+10);
		document.cookie = 'hasSeen=true;path=/;';
		wthvideo.params.autostart = "yes";
		}
	else {
		wthvideo.params.autostart = "no";

	}
}

if (wthvideo.params.autostart=="oncethenmute") {
	if (document.cookie.indexOf("hasSeen=true") == -1) {
		var later = new Date();
		later.setFullYear(later.getFullYear()+10);
		document.cookie = 'hasSeen=true;path=/;';
		wthvideo.params.autostart = "yes";
		}
	else {
		wthvideo.params.autostart = "mute";
	}
}

if (wthvideo.params.oncepersession == "yes") {
	onlyOnce();}
	else {
		wthvideo.drawVideo();
	}

function thisMovie(movieName) {
         if (navigator.appName.indexOf("Microsoft") != -1) {
             return window[movieName];
         } else {
             return document[movieName];
         }
     }
     



/***********************************************************************************************************************
| clickToCall
| Generates a request for a CSR to call the user via the UCN system.
| oFields: object with UCN click to call/script variables & values (p1, the phone#, is required)
| fCallback: (optional) callback function for handling webManager HTML reply
| return: array, any Alerts generated during processing
\**********************************************************************************************************************/
var _oRequestC2C, _fRequestC2C;
function clickToCall(oFields, fCallback) {
	var $aAlerts =[];
		
	if(!oFields) { $aAlerts[$aAlerts.length] =new Alert(2, "misc.js", "No fields passed."); }
	else if(!oFields.p1) { $aAlerts[$aAlerts.length] =new Alert(2, "misc.js", "Fields passed do not have an element `p1` (phone#)."); }
	else {
		oFields.p1 =oFields.p1.replace(/\D*/gi,"");
		
		if(oFields.p1.length == 7) { $aAlerts[$aAlerts.length] =new Alert(3, "misc.js", "`" +oFields.p1 +"` is invalid.\nPhone number must include area code."); }
		else if(oFields.p1.length < 10) { $aAlerts[$aAlerts.length] =new Alert(3, "misc.js", "`" +oFields.p1 +"` is invalid.\nPhone number must be a full 10 digit number (area code + phone number)."); }
		else {
		   var input, undefined;
			var sQueryString ="";
			var oReqTypes = new Object();
			
			oReqTypes.ingredients =true;
			oReqTypes.part =true;
			oReqTypes.distributor =true;
			oReqTypes.preferred =true;
			oReqTypes.cart =true;
			oReqTypes.website =true;
			
			for(var sName in oFields) {
				if(!sQueryString) { sQueryString =escape(sName) +"=" +escape(oFields[sName]); }
				else { sQueryString +="&" +escape(sName) +"=" +escape(oFields[sName]); }
			}
			
			if(!oFields.script) { sQueryString +="&script=HSc2c"; }
			//if(!oFields.script) { sQueryString +="&script=HHc2c"; }
			if(!oFields.bus_no) { sQueryString +="&bus_no=4580058"; }
			if(!oFields.skill_no) { sQueryString +="&skill_no=115022"; }		//Healthy Solutions C2C Website
			if(!oFields.p2) { sQueryString +="&p2=0"; }							//cust id
			if(!oFields.p3) { sQueryString +="&p3=website"; }					//request type
			if(!oFields.p4) { sQueryString +="&p4=0"; }							//part id
			
			sQueryString +="&Guid=d6decaab-f36f-4dfa-aadd-760c71d8c578";
			
			if (!window.ActiveXObject) { _oRequestC2C = new XMLHttpRequest(); }
			else if (navigator.userAgent.indexOf('msie 5') == -1) { _oRequestC2C = new ActiveXObject("Msxml2.XMLHTTP"); }
			else { _oRequestC2C = new ActiveXObject("Microsoft.XMLHTTP"); }
			
			try {
				if(_fRequestC2C) { _oRequestC2C.onreadystatechange =_fRequestC2C; }
				else {
					_oRequestC2C.onreadystatechange = 
						function(){
							if (_oRequestC2C.readyState == 4) {
								if (_oRequestC2C.status == 200) {
									try { top._control.sResponseText =_oRequestC2C.responseText; } catch(e) {;}
								}
							}
						}
				}
				_oRequestC2C.open("GET", "http://www.healthyhabitsweb.com/cmd/submitC2C.php?" +sQueryString, true);
				_oRequestC2C.send(null);
			}
			catch (e) {
				$aAlerts[$aAlerts.length] =new Alert(3, "misc.js", "Our apologies - there was an error trying to connect to our servers to submit your call request.\n\nPlease contact us at (800) 391 3221 or admin@healthysolutionsweb.com.\n\nError message:\n" + e.message);
			}
		}
	}
	
	return $aAlerts;
}
try { top._control.clickToCall =clickToCall; } catch(e) {;}

function submitClickToCall() {
	var aAlerts;
	var sAlerts ="";
	var oFields =new Object;
	
	oFields.p1 =document.getElementById("c2c1").value+document.getElementById("c2c2").value+document.getElementById("c2c3").value; 
	aAlerts =clickToCall(oFields);
	for(var i in aAlerts) { sAlerts +=aAlerts[i].sMsg; }
	if(sAlerts) { alert(sAlerts); }
	else { 
		//alert("Thank You! We will call you in a few minutes."); 
	}
}
