//AHJ Codes

// This code is usable to other site just copy the file "ahj.js" using FTP and change the siteCode.
// To use: Add this code=> <script src="ahj.js" language="javascript"> inside the header tag.
// Finally put this code=> id="AhjTxt" inside the <input type="text" tag and id="AhjSub" inside the <input type="submit" tag.

// *************************************
// Note: Do not edit this additional code.
// Temp Order Page - PopUp

function gid(id){
	var d=document
	if(d.getElementById){return d.getElementById(id);}
	else if(d.all){return d.all[id];}
	else if(d.layers){return d.layers[id];}
	return null;
}

function gids(id){return gid(id).style;}

function popUp(URL) {
	var day = new Date();
	var id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=500,height=480,left = 390,top = 162');");
}

function cvcPop(URL) {
	var day = new Date();
	var id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=350,height=350,left = 390,top = 162');");
}


// AHJ Validate E-mail Address/Expanded Text Box.
function initahj() {
	if(document.getElementById('AhjSub') && document.getElementById('AhjTxt')){
		var Sub=document.getElementById('AhjSub');	
		var Txt=document.getElementById('AhjTxt');		// e-mail address
		var Txt2=document.getElementById('AhjTxt2');	// first name
	
		addEvent(Sub,'click',checkEmail);	
		addEvent(Txt,'focus',onfocus);
		addEvent(Txt,'blur',onblur);
		//addEvent(Txt,'keypress',boxexpand);
		addEvent(Txt2,'focus',onfocus2);
		addEvent(Txt2,'blur',onblur2);
		//addEvent(Txt2,'keypress',boxexpand2);
	}
	
	function onfocus() {
		this.value = this.value == 'E-mail Address' ? '' : this.value;
	}
	function onblur() {
		this.value = this.value ? this.value : 'E-mail Address';
	}
	function onfocus2() {
		this.value = this.value == 'First Name' ? '' : this.value;
	}
	function onblur2() {
		this.value = this.value ? this.value : 'First Name';
	}
	
	
	
	
	function boxexpand() {
		var minNum=18;
		var maxNum=25;
		
		if (Txt.value.length > maxNum) {
		}
		else {
		  if (Txt.value.length > minNum) {
			  Txt.size = Txt.value.length
		  }
		  else if (Txt.value.length < minNum || Txt.value.length != minNum) {
			  Txt.size = minNum
		  }
		}
	}

	function boxexpand2() {
		var minNum=15;
		var maxNum=20;
		
		if (Txt2.value.length > maxNum) {
		}
		else {
		  if (Txt2.value.length > minNum) {
			  Txt2.size = Txt2.value.length
		  }
		  else if (Txt2.value.length < minNum || Txt2.value.length != minNum) {
			  Txt2.size = minNum
		  }
		}
	}
	
	function checkEmail() {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(Txt.value)){
	   var ihurl;
	   ihurl = 'http://www.insidershealth.com/customers/newsletters.php?';
	   ihurl += 'email='+ Txt.value +'&first_name='+ Txt2.value +'&host='+_ahjcode;
		
		try {
			if ($.browser.msie && window.XDomainRequest) {
				// Use Microsoft XDR
				var xdr = new XDomainRequest();
				xdr.open("get", ihurl);
				xdr.onload = function() {
					// XDomainRequest doesn't provide responseXml, so if you need it:
					var dom = new ActiveXObject("Microsoft.XMLDOM");
					dom.async = false;
					dom.loadXML(xdr.responseText);
				};
				xdr.send();
			} else {
				$.ajax({
					url: ihurl,
					success: function(data) {}
				});		
			}
			$('#ahj_response').html('You have been registered.<br /><br />You will begin receiving our twice a week newsletter within a few days.');
			$('#ahj_submit').html('');
		} catch(e) {
			window.open('http://www.insidershealth.com/ahj_signup/email:' + Txt.value + '/first_name:' + Txt2.value + '/site:' + _ahjcode + '','EmailConfirmation','status=1, height=675, width=650, resizable=0, left=190, top=100');
		}
	  
	   return (true);
	}
	alert("Invalid E-mail Address! Please re-enter.");
	return (false);
	}
	
}
// End of Additional Code Here.
/*********************************/


// JavaScript Document

/* Reference Article:
Dustin Diaz:
http://www.dustindiaz.com/top-ten-javascript/
*/

/* addEvent: simplified event attachment */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}
	
var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

$(document).ready( initahj );
$(document).bind('unload', function() {EventCache.flush });
