if (newImageMQ) {
	newImageMQ.ACTION_NONE = "NONE";
	newImageMQ.ACTION_VIEW_PHONE = "VIEW_PHONE";
	newImageMQ.ACTION_VIEW_PROFILE = "VIEW_PROFILE";
	newImageMQ.ACTION_VIEW_LOCATIONS = "VIEW_LOCATIONS";
	newImageMQ.ACTION_VIEW_BIO = "VIEW_BIO";
	newImageMQ.ACTION_SEND_EMAIL = "SEND_EMAIL";

	newImageMQ.STYPE_WEBSITE = "WEBSITE";
	newImageMQ.STYPE_QUICK = "SURGEON_SEARCH_QUICK";
	newImageMQ.STYPE_LOCATION = "SURGEON_SEARCH_LOCATION";
	newImageMQ.STYPE_PROCEDURE = "SURGEON_SEARCH_PROCEDURE";
	newImageMQ.STYPE_BOTH = "SURGEON_SEARCH_BOTH";
	newImageMQ.STYPE_FEATURED = "SURGEON_FEATURED";
	newImageMQ.STYPE_LASTNAME = "SURGEON_SEARCH_LASTNAME";
	
	newImageMQ.pvidMap = {};
	
	newImageMQ.alog = {};
	newImageMQ.alog.REQ_COM = "c";
	newImageMQ.alog.REQ_BROWSERWIDTH = "bw";
	newImageMQ.alog.REQ_BROWSERHEIGHT = "bh";
	newImageMQ.alog.REQ_SCREENWIDTH = "sw";
	newImageMQ.alog.REQ_SCREENHEIGHT = "sh";
	newImageMQ.alog.REQ_SCREENDEPTH = "sd";
	newImageMQ.alog.REQ_USERAGENT = "ua";
	newImageMQ.alog.REQ_PVID = "pvid";
	newImageMQ.alog.REQ_SRCHID = "srcid";
	newImageMQ.alog.REQ_SRCHTYPE = "ty";
	newImageMQ.alog.REQ_SID = "sid";
	newImageMQ.alog.REQ_ACTION = "a";
	newImageMQ.alog.REQ_PROCEDUREID = "pid";
	newImageMQ.alog.REQ_URL = "u";
	newImageMQ.alog.REQ_ORDER = "o";
	newImageMQ.alog.REQ_KEYWORDS = "k";
	newImageMQ.alog.REQ_STATE = "st";
	newImageMQ.alog.REQ_ZIP = "z";
	newImageMQ.alog.REQ_ADDRESS = "ad";
	newImageMQ.alog.REQ_CITY = "ct";
	newImageMQ.alog.REQ_DOCTORADDRESSID = "d";
	newImageMQ.alog.REQ_PICID = "picid";
	newImageMQ.alog.REQ_RADIUS = "rd";
	newImageMQ.alog.REQ_FID = "fid";
	newImageMQ.alog.REQ_LASTNAME = "ln";

	newImageMQ.alog.REQ_L_NAME = "l_n";
	newImageMQ.alog.REQ_L_EMAIL = "l_e";
	newImageMQ.alog.REQ_L_STATE = "l_s";
	newImageMQ.alog.REQ_L_TYPE = "l_t";

	newImageMQ.alog.REQ_F_FORMNAME = "f_n";
	newImageMQ.alog.REQ_F_EMAIL = "f_e";
	newImageMQ.alog.REQ_F_FIRSTNAME = "f_fn";
	newImageMQ.alog.REQ_F_LASTNAME = "f_ln";
	newImageMQ.alog.REQ_F_ADDR1 = "f_a1";
	newImageMQ.alog.REQ_F_ADDR2 = "f_a2";
	newImageMQ.alog.REQ_F_CITY = "f_c";
	newImageMQ.alog.REQ_F_STATE = "f_s";
	newImageMQ.alog.REQ_F_ZIP = "f_z";
	newImageMQ.alog.REQ_F_COMMENTS = "f_cm";
	newImageMQ.alog.REQ_F_PROCEDURE = "f_p";
	newImageMQ.alog.REQ_F_TYPE = "f_t";
	newImageMQ.alog.REQ_F_CONFEMAIL = "f_ec";
	newImageMQ.alog.REQ_F_COMPNAME= "f_cn";
	newImageMQ.alog.REQ_F_FRNDEMAIL = "f_ef";
	newImageMQ.alog.REQ_F_PHONE = "f_ph";
	newImageMQ.alog.REQ_F_DOCID = "f_did";
	newImageMQ.alog.REQ_F_URL = "f_ur";	
	
	newImageMQ.alog.COM_BROWSER = "BR";
	newImageMQ.alog.COM_PICBIO = "PB";
	newImageMQ.alog.COM_PICEXPAND = "PE";
	newImageMQ.alog.COM_SHARE = "SH";
	newImageMQ.alog.COM_PRINT = "PR";
	newImageMQ.alog.COM_SURBIO = "SB";
	newImageMQ.alog.COM_SITESRCH = "AS";
	newImageMQ.alog.COM_SITESRCH_RES = "ASR";
	newImageMQ.alog.COM_SURSRCH = "SS";
	newImageMQ.alog.COM_SURSRCH_RES = "SSR";

	newImageMQ.alog.COM_DATA_LEAD = "DL";
	newImageMQ.alog.COM_DATA_FORM = "DF";
	newImageMQ.alog.COM_REPORT_LEAD = "RL";
	newImageMQ.alog.COM_REPORT_FORM = "RF";

	if (window.XMLHttpRequest) {
    	newImageMQ.alog.isIE = false;
	} else {
    	newImageMQ.alog.isIE = true;
	}
	
	newImageMQ.alog._generateBody = function (parameters) {
		var x;
		var rv = "";
   		for (x in parameters) {
			rv += escape(x) + "=" + escape(parameters[x]) + "&";
   		}
   		if (rv.length==0) {
			return rv;
   		} else {
      		return rv.substr(0,rv.length-1);
   		}
	}
	
	newImageMQ.alog._generateRequest = function () {
		var rv;
		if (this.isIE) {
			try {
      			rv = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				try {
					rv = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					rv = null;
				}
   			}
		} else {
   			try {
      			rv = new XMLHttpRequest();
   			} catch (e) {
      			rv = null;
   			}
		}
		return rv;
	}
	
	newImageMQ.alog._generateURL = function (method) {
		return "/ajax.js?"+this.REQ_COM+"="+method;
	}
	
	newImageMQ.alog._logimpl = function(method,parameters,asyncf) {
		var robj = this._generateRequest();
		if (robj != null) {
			robj.open("POST", this._generateURL(method), asyncf);
			robj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			robj.send(newImageMQ.alog._generateBody(parameters));
			if (asyncf) {
				return robj;
			} else {
				var rtext = robj.responseText;
				var reval = null;
				try {
					reval = eval(rtext);
				} catch (e) {
					reval = null;
				}
				return reval;
			}
		} else {
			return null;
		}
	}
	
	newImageMQ.alog._log = function (method, parameters) {
		return this._logimpl(method,parameters,false);
	}
	
	newImageMQ.alog._logasync = function (method, parameters) {
		return this._logimpl(method,parameters,true);
	}
	
	newImageMQ.alog._figureBrowserStats = function () {
		this._s_w = "0";
		this._s_h = "0";
		this._b_w = "0";
		this._b_h = "0";
		this._ua = "unknown";
		this._s_d = "0";
		if (navigator && navigator.userAgent) {
			this._ua = navigator.userAgent;
		}
		if (screen.width) {
			this._s_w = screen.width;
			this._s_h = screen.height;
		}
		if (screen.colorDepth) {
			this._s_d = screen.colorDepth;
		}
  		if(typeof(window.innerWidth)=='number') {
    		//Fire fox
    		this._b_w = window.innerWidth;
    		this._b_h = window.innerHeight;
  		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	   		this._b_w = document.documentElement.clientWidth;
    		this._b_h = document.documentElement.clientHeight;
  		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    		this._b_w = document.body.clientWidth;
			this._b_h = document.body.clientHeight;
  		}
	
	}
	
	// declaration for browser feature reporting.
	newImageMQ.alog.logBrowser = function (sid) {
		this._figureBrowserStats();
		var params = {};
		params[this.REQ_SID] = sid;
		params[this.REQ_USERAGENT] = this._ua;
		params[this.REQ_SCREENWIDTH ] = this._s_w;
		params[this.REQ_SCREENHEIGHT ] = this._s_h;
		params[this.REQ_BROWSERWIDTH  ] =  this._b_w;
		params[this.REQ_BROWSERHEIGHT  ] =  this._b_h;
		params[this.REQ_SCREENDEPTH ] =  this._s_d;
		this._logasync(this.COM_BROWSER,params);
	}
	
	// This returns a generic logger to update further portlet details.
	newImageMQ.alog.getPortletLogger = function (pvid) {
		var rv = {};
		rv.l = this;
		rv.logPICView = function (picid) {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_PICID] = picid;
			this.l._logasync(this.l.COM_PICBIO,params);
		};
		rv.logPICExpand = function (picid) {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_PICID] = picid;
			this.l._logasync(this.l.COM_PICEXPAND, params);
		};
		rv.logShare = function () {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			this.l._log(this.l.COM_SHARE, params);
		};
		rv.logPrint = function () {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			this.l._log(this.l.COM_PRINT, params);
		};
		return rv;
	}
	
	newImageMQ.alog.quickPrint = function () {
		if (newImageMQ.pvidMap['NI_Portlet_PrintShare']) {
			var t = newImageMQ.alog.getPortletLogger(newImageMQ.pvidMap['NI_Portlet_PrintShare']);
			t.logPrint();
		}
	}
	
	newImageMQ.alog.quickShare = function () {
		if (newImageMQ.pvidMap['NI_Portlet_PrintShare']) {
			var t = newImageMQ.alog.getPortletLogger(newImageMQ.pvidMap['NI_Portlet_PrintShare']);
			t.logShare();
		}
	}
	
	newImageMQ.alog.getSurgeonBioLogger = function (pvid, ahid, srcid) {
		var rv = {};
		rv.l = this;
		rv.logAction = function (action) {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_DOCTORADDRESSID] = ahid;
			params[this.l.REQ_SRCHID] = srcid;
			params[this.l.REQ_ACTION] = action;
			this.l._logasync(this.l.COM_SURBIO, params);
		};
		rv.logActionNow = function (action) {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_DOCTORADDRESSID] = ahid;
			params[this.l.REQ_SRCHID] = srcid;
			params[this.l.REQ_ACTION] = action;
			this.l._log(this.l.COM_SURBIO, params);
		};
		rv.logSwitchAddress = function (newahid) {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_DOCTORADDRESSID] = newahid;
			params[this.l.REQ_SRCHID] = srcid;
			params[this.l.REQ_ACTION] = newImageMQ.ACTION_VIEW_LOCATIONS;
			this.l._logasync(this.l.COM_SURBIO, params);
		};
		rv.logPhone = function (newahid) {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_DOCTORADDRESSID] = newahid;
			params[this.l.REQ_SRCHID] = srcid;
			params[this.l.REQ_ACTION] = newImageMQ.ACTION_VIEW_PHONE;
			this.l._logasync(this.l.COM_SURBIO, params);
		};
		return rv;
	}
	
	newImageMQ.alog.getSiteSearchLogger = function (pvid) {
		var rv = {};
		rv.l = this;
		rv.logSearch = function (keywords) {
			var params  = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_KEYWORDS] = keywords;
			this.srcid = this.l._log(this.l.COM_SITESRCH, params);
			return this.srcid;
		};
		rv.addSrcId = function (form) {
			if ((form)&&(form.elements)) {
				if (form.elements[this.l.REQ_SRCHID]) {
					form.elements[this.l.REQ_SRCHID].value = this.srcid;
				}
			}
		};
		return rv;
	};
	
	newImageMQ.alog.getSiteSearchResultsLogger = function (srcid) {
		var rv = {};
		rv.l = this;
		rv.logResult = function (url, order) {
			var params  = {};
			params[this.l.REQ_SRCHID] = srcid;
			params[this.l.REQ_URL] = url;
			params[this.l.REQ_ORDER] = order;
			this.l._logasync(this.l.COM_SITESRCH_RES, params);
		};
		return rv;
	};
	
	newImageMQ.alog.getSurgeonSearchLogger = function (pvid, type) {
		var rv = {};
		rv.l = this;
		rv.logSearch = function (keywords, state, zip, radius, address, city, procedureid, lastname) {
			
			var params  = {};
			params[this.l.REQ_PVID] =  pvid;
			params[this.l.REQ_SRCHTYPE ] =  type;
			params[this.l.REQ_KEYWORDS ] =  keywords;
			params[this.l.REQ_STATE ] =  state;
			params[this.l.REQ_ZIP ] =  zip;
			params[this.l.REQ_RADIUS ] =  radius;
			params[this.l.REQ_ADDRESS ] =  address;
			params[this.l.REQ_PROCEDUREID ] =  procedureid;
			params[this.l.REQ_CITY ]= city;
			params[this.l.REQ_LASTNAME ]= lastname;
			this.srcid = this.l._log(this.l.COM_SURSRCH, params);
			return this.srcid;
		};
		rv.addSrcId = function (form) {
			if ((form)&&(form.elements)) {
				if (form.elements[this.l.REQ_SRCHID]) {
					form.elements[this.l.REQ_SRCHID].value = this.srcid;
				}
			}
		};
		return rv;
	};
	
	newImageMQ.alog.getSurgeonSearchResultsLogger = function (srcid) {
		var rv = {};
		rv.l = this;
		rv.logResult = function (url, ahid, order) {
			var params  = {};
			params[this.l.REQ_SRCHID] = srcid;
			params[this.l.REQ_URL] = url;
			params[this.l.REQ_DOCTORADDRESSID] = ahid;
			params[this.l.REQ_ORDER] = order;
			this.l._logasync(this.l.COM_SURSRCH_RES,params);
		};
		return rv;
	};
	
	newImageMQ.alog.getLeadLogger = function (pvid, type) {
		var rv = {};
		rv.l = this;
		rv.logLead = function (procid, name, email, state) {
			var params = {};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_L_TYPE] = type;
			params[this.l.REQ_PROCEDUREID ] = procid;
			params[this.l.REQ_L_NAME] = name;
			params[this.l.REQ_L_EMAIL] = email;
			params[this.l.REQ_L_STATE] = state;
			this.l._log(this.l.COM_DATA_LEAD, params);
		}
		return rv;
	}
	
	newImageMQ.alog.getFormLogger = function (pvid, formName) {
		var rv = {};
		rv.l = this;
		rv.logForm = function (email, fname, lname, addr1, addr2, city, state, zip, comments, procname, type, phone, confemail, company, friendemail, docid, senturl) {
			var params ={};
			params[this.l.REQ_PVID] = pvid;
			params[this.l.REQ_F_FORMNAME] = formName;
			params[this.l.REQ_F_EMAIL]= email;
			params[this.l.REQ_F_FIRSTNAME]= fname;
			params[this.l.REQ_F_LASTNAME]=  lname;
			params[this.l.REQ_F_ADDR1]=  addr1;
			params[this.l.REQ_F_ADDR2]= addr2;
			params[this.l.REQ_F_CITY]= city;
			params[this.l.REQ_F_STATE]= state;
			params[this.l.REQ_F_ZIP]= zip;
			params[this.l.REQ_F_COMMENTS]= comments ;
			params[this.l.REQ_F_PROCEDURE]= procname;
			params[this.l.REQ_F_TYPE]= type;
			params[this.l.REQ_F_PHONE]= phone;
			params[this.l.REQ_F_CONFEMAIL]= confemail;
			params[this.l.REQ_F_COMPNAME]= company;
			params[this.l.REQ_F_FRNDEMAIL]= friendemail;
			params[this.l.REQ_F_DOCID] = docid;
			params[this.l.REQ_F_URL] = senturl;
			this.l._log(this.l.COM_DATA_FORM, params);
		}
		return rv;
	}
	
	newImageMQ.formRatings = new Array("_createComment", "_DeleteCommentForm", "_AddRatingForm", "_DeleteReviewForm", "_AddReviewForm"); 
	newImageMQ.formBuilder = new Array("efb_form_");
	newImageMQ.formBuilderHandlers = {};
	
	newImageMQ.getRatingsForms = function () {
		return this.getFormsByType(this.formRatings, true);
	}
	
	newImageMQ.getBuilderForms = function () {
		var prv = this.getFormsByType(this.formBuilder, true);
		var rv = new Array();
		for (var x=0; x< prv.length; x++) {
			if (this.getBuilderFormName(prv[x])!=null) {
				rv[rv.length] = prv[x];
			}
		}
		return rv;
	}
	
	newImageMQ.handlerLeadConsultation = function (f, fname, fmap) {
		var llog = newImageMQ.alog.getLeadLogger(newImageMQ.pvidMap['NI_Portlet_Form_Consultation'],fname);
		llog.logLead(fmap["ID-ConsultationView-Procedure-control"], fmap["name"], fmap["leadgen_email"], fmap["ID-ConsultationView-State-control"]);
	}
	newImageMQ.formBuilderHandlers["ID-Consultation-form"] = newImageMQ.handlerLeadConsultation;

	newImageMQ.handlerLeadFinancing = function (f, fname, fmap) {
		var llog = newImageMQ.alog.getLeadLogger(newImageMQ.pvidMap['NI_Portlet_Form_Financing'],fname);
		llog.logLead(fmap["ID-FinancingView-Procedure-control"], fmap["name"], fmap["email"], fmap["ID-FinancingView-State-control"]);
	}
	newImageMQ.formBuilderHandlers["ID-Financing-form"] = newImageMQ.handlerLeadFinancing;
	
	newImageMQ.handlerContactUs = function (f, fname, fmap) {
		// first we create a logger. We need the portlet name here. The rest pretty much stays the same
		var llog = newImageMQ.alog.getFormLogger(newImageMQ.pvidMap['NI_Page_ContactUs'], fname);
		
		// order of parameters important. Send in blank string for data you dont care about.
		// the map is going to return items based on ID of the field (if you set one in builder or if the field is a select or something generated by builder
		// or if there is no id set then it will decode out the builder id (param_name = )
		// of if there is no builder id (it is a custom hidden field you have inserted), then it just uses the field name
		// selects get special attention. A select with a id/name of test will also populate a parameter called test_display in case you wanted what the user
		// saw rather than the internal ID builder might have used as a value.
		llog.logForm(
			fmap["ID-ContactUsView-Emailaddress-control"], // email
			fmap["ID-ContactUsView-FirstName-control"], // firstname
			fmap["ID-ContactUsView-LastName-control"], // lastname
			fmap["ID-ContactUsView-Address1-control"], // address 1
			fmap["ID-ContactUsView-Address2-control"], // address 2
			fmap["ID-ContactUsView-City-control"], // city
			fmap["ID-ContactUsView-State-control"], // state (as in the state code. Do not send in the full state name)
			fmap["ID-ContactUsView-ZIPCode-control"], // zip
			fmap["ID-ContactUsView-Comments-control"], // comments
			"", // procedurename text ( as in the human readable text).
			"", // action type. leave blank for user form
			"", // phone (of user)
			fmap["ID-ContactUsView-Confirmemail-control"], // email confirmation.
			"", // company name
			"" // friends email
		);
			
	}
	newImageMQ.formBuilderHandlers["ID-ContactUs-form"] = newImageMQ.handlerContactUs;
	
	newImageMQ.handlerNewsletter = function (f, fname, fmap) {
		// first we create a logger. We need the portlet name here. The rest pretty much stays the same
		var llog = newImageMQ.alog.getFormLogger(newImageMQ.pvidMap['NI_Portlet_NewsLetter'], fname);
		
		// order of parameters important. Send in blank string for data you dont care about.
		// the map is going to return items based on ID of the field (if you set one in builder or if the field is a select or something generated by builder
		// or if there is no id set then it will decode out the builder id (param_name = )
		// of if there is no builder id (it is a custom hidden field you have inserted), then it just uses the field name
		// selects get special attention. A select with a id/name of test will also populate a parameter called test_display in case you wanted what the user
		// saw rather than the internal ID builder might have used as a value.
		llog.logForm(
			fmap["emailAddress"], // email
			"", // firstname
			"", // lastname
			"", // address 1
			"", // address 2
			"", // city
			"", // state (as in the state code. Do not send in the full state name)
			"", // zip
			"", // comments
			"", // procedurename text ( as in the human readable text).
			"", // action type. leave blank for user form
			"", // phone (of user)
			"", // email confirmation.
			"", // company name
			""  // friends email
		);
			
	}
	newImageMQ.formBuilderHandlers["ID-Newsletter-form"] = newImageMQ.handlerNewsletter;
	
	
	
	newImageMQ.formBuildFieldMap = function (f) {
		var rv = {};
		if (f && f.elements) {
			for (var x= 0; x<f.elements.length; x++) {
				var field = f.elements[x];
				var field_name = null;
				var field_value = null;
				if (field.id) {
					field_name = field.id;
				} else if (field.name) {
					if (field.name.indexOf("param_name=")!=-1) {
						field_name = field.name.substring(field.name.indexOf("param_name=")+1+"param_name".length,field.name.length);
					} else {
						field_name = field.name;
					}
				}
				if (field_name) {
					if (field.options) {
						field_value = field.options[field.selectedIndex].value;
						if (field_value.indexOf("=")!=-1) {
							field_value = field_value.substring(field_value.indexOf("=")+1,field_value.length);
							rv[field_name] = field_value;
							rv[field_name+"_display"] = field.options[field.selectedIndex].text;
						} else {
							rv[field_name] = field_value;
							rv[field_name+"_display"] = field.options[field.selectedIndex].text;
						}
					} else {
						field_value = field.value;
						rv[field_name] = field_value;
					}
				}
			}
			return rv;
		} else {
			return null;
		}
	}
	
	newImageMQ.getBuilderIDNode = function (r) {
		if ((r)&&(r.childNodes)) {
			if ((r.tagName)&&(r.tagName.toLowerCase()=='table')) {
				if ((r.id)&&(r.id.indexOf("ID-")==0)&&(r.id.indexOf("-form")!=-1)) {
					return r;
				}
			}
			// not a table or wrong table...
			for (var x=0; x< r.childNodes.length; x++) {
				var crv = newImageMQ.getBuilderIDNode(r.childNodes[x]);
				if (crv != null) {
					return crv;
				}
			}
			// not in any children here. return null.
			return null;
		} else {
			// no children. this cannot be it.
			return null;
		}
	}
	
	newImageMQ.getBuilderFormName = function (f) {
		// given a builder object this method discovers the name
		if ((f)&&(f.childNodes)) {
			var bidNode = this.getBuilderIDNode(f);
			if (bidNode != null) {
				return bidNode.id;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}
	
	newImageMQ.getFormsByType = function (typeArr, nf) {
		var rv = new Array();
		if (typeArr && typeArr.length) {
			var allForms = document.getElementsByTagName("form");
			for (var c =0; c< allForms.length; c++) {
				if (allForms[c] && allForms[c].getAttribute && allForms[c].getAttribute('name')) {
					var fname = allForms[c].getAttribute('name');
					if (fname.indexOf) {
						for (var x=0;x<typeArr.length; x++) {
							if (fname.indexOf(typeArr[x])!=-1) {
								rv[rv.length] = allForms[c];
							}
						}
					} else {
						// form has a field called name
						if (nf)
							rv[rv.length]  = allForms[c];
					}
				}
			}
		}
		return rv;
	}
	
	newImageMQ.wireBuilder = function () {
		var builderForms = this.getBuilderForms();
		for (var c=0; c<builderForms.length; c++) {
			YAHOO.util.Event.addListener(builderForms[c], "submit", this.preActionBuilder);
		}
	}
	
	newImageMQ.preActionBuilder = function () {
		//Identify the exact nature of this form . This is a yahoo handler so "this" means the form
		var fname = newImageMQ.getBuilderFormName(this);
		//alert("Builder handler called for "+fname);
		if (newImageMQ.formBuilderHandlers[fname]) {
			//alert("Pre builder handler invoke for "+fname);
			newImageMQ.formBuilderHandlers[fname](this,fname, newImageMQ.formBuildFieldMap(this));
			//alert("Post builder handler invoke for "+fname);
		}
	}
	
	newImageMQ.preActionRating = function () {
		//alert("Rating Wire call back!");
		newImageMQ.setCookie("tni_u_r",location.href,1,"/");
	}
	
	newImageMQ.setCookie = function (name, value,expiration,path) {
		var expires = "";
		var paths = "";
		if (expiration) {
			var date = new Date();
			date.setTime(date.getTime()+(expiration*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		if (path) {
			paths = "; path="+path;
		}
		document.cookie = name+"="+value+expires+paths;
	}
	
	newImageMQ.fixCookieString = function (s) {
		if (s.indexOf("expires=")!=-1) {
			var se = s.indexOf("expires=")+("expires=".length);
			var ds = "";
			if (s.indexOf(";",se)!=-1) {
				ds = s.substring(se,s.indexOf(";",se));
			} else {
				ds = s.substring(se,s.length);
			}
			d = new Date(ds);
			ns = s.substring(0,se)+d.toUTCString()+";";
			return ns;
		} else {
			return s;
		}
	}
	
	newImageMQ.readCookie = function (name) {
		var cookieArr = document.cookie.split(';');
		var cnlen = (name+"=").length;
		for(var c=0;c < cookieArr.length;c++) {
			var this_ck = cookieArr[c];
			while (this_ck.charAt(0)==' ') this_ck = this_ck.substring(1,this_ck.length);
			if (this_ck.indexOf(name+"=") == 0) return this_ck.substring(cnlen,this_ck.length);
		}
		return null;
	}
	
	newImageMQ.retrieveAndClearRating = function () {
		var rv = this.readCookie("tni_u_r");
		this.setCookie("tni_u_r","",-1,"/");
		return rv;
	}
	
	newImageMQ.wireRatings = function () {
		//alert("Wiring ratings");
		var ratingForms = this.getRatingsForms();
		// for every ratings form
		for (var c=0; c<ratingForms.length; c++) {
			YAHOO.util.Event.addListener(ratingForms[c], "submit", this.preActionRating);
			//alert("Wired "+ratingForms[c].name);
		}
	}
	
	newImageMQ.wireForms = function () {
		try {
		newImageMQ.wireRatings();
		newImageMQ.wireBuilder();
        newImageMQ.wireLeadSubmit();
		} catch (e) {}
	}
	
	newImageMQ.urlFix = function () {
		// Fix for URLs that end up using non friendly forms.
		var dahr = location.href;
		// make sure this is not builder URL. Those should not be manipulated
		if (dahr.indexOf("_wsrp")==-1) {
			// make sure this is not a post manipulation url
			if (dahr.indexOf("/portal/site/")!=-1) {
				if (   (dahr.indexOf("/template.PAGE/surgeonbio")!=-1) 
					|| (dahr.indexOf("/template.PAGE/article")!=-1) 
					|| (dahr.indexOf("/template.PAGE/pressrelease")!=-1) ) {
					//history.back();
					var targetUrl = newImageMQ.retrieveAndClearRating();
					if (targetUrl)
						location.href = targetUrl;
				}
			}
		}
	}
    
    newImageMQ.handlerEmailaDoctor = function (f, fname, fmap) {
		// first we create a logger. We need the portlet name here. The rest pretty much stays the same
		var llog = newImageMQ.alog.getFormLogger(newImageMQ.pvidMap['Email a Doctor Portlet'], fname);
		// order of parameters important. Send in blank string for data you dont care about.
		// the map is going to return items based on ID of the field (if you set one in builder or if the field is a select or something generated by builder
		// or if there is no id set then it will decode out the builder id (param_name = )
		// of if there is no builder id (it is a custom hidden field you have inserted), then it just uses the field name
		// selects get special attention. A select with a id/name of test will also populate a parameter called test_display in case you wanted what the user
		// saw rather than the internal ID builder might have used as a value.
		llog.logForm(
			fmap["ID-EmailaDoctorView-Email-control"], // email
			fmap["ID-EmailaDoctorView-FirstName-control"], // firstname
			fmap["ID-EmailaDoctorView-LastName-control"], // lastname
			fmap["ID-EmailaDoctorView-Address1-control"], // address 1
			fmap["ID-EmailaDoctorView-Address2-control"], // address 2
			fmap["ID-EmailaDoctorView-City-control"], // city
			fmap["ID-EmailaDoctorView-State-control"], // state (as in the state code. Do not send in the full state name)
			fmap["ID-EmailaDoctorView-Zip-control"], // zip
			"", // comments
            
			fmap["ID-EmailaDoctorView-Procedure-control_display"], // procedurename text ( as in the human readable text).
			"", // action type. leave blank for user form
			fmap["ID-EmailaDoctorView-Phone-control"], // phone (of user)
			"", // email confirmation.
			"", // company name
			"", // friends email
			fmap["vardoctorsid"], //doctor_id
			"" //url
            );
	}
	newImageMQ.formBuilderHandlers["ID-EmailaDoctor-form"] = newImageMQ.handlerEmailaDoctor;
    
    	newImageMQ.handlerEmailaFriend = function (f, fname, fmap) {
		// first we create a logger. We need the portlet name here. The rest pretty much stays the same
		var llog = newImageMQ.alog.getFormLogger(newImageMQ.pvidMap['Email a Friend Portlet'], fname);
		// order of parameters important. Send in blank string for data you dont care about.
		// the map is going to return items based on ID of the field (if you set one in builder or if the field is a select or something generated by builder
		// or if there is no id set then it will decode out the builder id (param_name = )
		// of if there is no builder id (it is a custom hidden field you have inserted), then it just uses the field name
		// selects get special attention. A select with a id/name of test will also populate a parameter called test_display in case you wanted what the user
		// saw rather than the internal ID builder might have used as a value.

		llog.logForm(
			fmap["ID-EmailaFriendView-YourEmail-control"], // email
			fmap["ID-EmailaFriendView-YourFirstName-control"], // firstname
			fmap["ID-EmailaFriendView-YourLastName-control"], // lastname
			"", // address 1
			"", // address 2
			"", // city
			"", // state (as in the state code. Do not send in the full state name)
			"", // zip
			fmap["ID-EmailaFriendView-YourMessage-control"], // comments
			"", // procedurename text ( as in the human readable text).
			"", // action type. leave blank for user form
			"", // phone (of user)
			"", // email confirmation.
			"", // company name
			fmap["ID-EmailaFriendView-FriendsEmail-control"], // friends email
			"", // doctor_id
			unescape(fmap["varshareLink"])  //shared_link		
		);
	}
	newImageMQ.formBuilderHandlers["ID-EmailaFriend-form"] = newImageMQ.handlerEmailaFriend;

//----------------------------------------	
	newImageMQ.handlerPartnerWithUs = function (f, fname, fmap) {
		// first we create a logger. We need the portlet name here. The rest pretty much stays the same
		var llog = newImageMQ.alog.getFormLogger(newImageMQ.pvidMap['Partner with NewImage Portlet'], fname);
		// order of parameters important. Send in blank string for data you dont care about.
		// the map is going to return items based on ID of the field (if you set one in builder or if the field is a select or something generated by builder
		// or if there is no id set then it will decode out the builder id (param_name = )
		// of if there is no builder id (it is a custom hidden field you have inserted), then it just uses the field name
		// selects get special attention. A select with a id/name of test will also populate a parameter called test_display in case you wanted what the user
		// saw rather than the internal ID builder might have used as a value.
		llog.logForm(
			fmap["ID-PartnerwithNewImageView-Email-control"], // email
			fmap["ID-PartnerwithNewImageView-FirstName-control"], // firstname
			fmap["ID-PartnerwithNewImageView-LastName-control"], // lastname
			fmap["ID-PartnerwithNewImageView-Address1-control"], // address 1
			fmap["ID-PartnerwithNewImageView-Address2-control"], // address 2
			fmap["ID-PartnerwithNewImageView-City-control"], // city
			fmap["ID-PartnerwithNewImageView-State-control"], // state (as in the state code. Do not send in the full state name)
			fmap["ID-PartnerwithNewImageView-Zip-control"], // zip
			fmap["ID-PartnerwithNewImageView-Comments-control"], // comments
			"", // procedurename text ( as in the human readable text).
			"", // action type. leave blank for user form
			fmap["ID-PartnerwithNewImageView-Phone-control"], // phone (of user)
			fmap["ID-PartnerwithNewImageView-ConfirmEmail-control"], // email confirmation.
			fmap["ID-PartnerwithNewImageView-CompanyName-control"], // company name
			"" // friends email
		);
	}
	newImageMQ.formBuilderHandlers["ID-PartnerwithNewImage-form"] = newImageMQ.handlerPartnerWithUs;

    //----------------------------------

	newImageMQ.handlerForSurgeons = function (f, fname, fmap) {
		// first we create a logger. We need the portlet name here. The rest pretty much stays the same
		var llog = newImageMQ.alog.getFormLogger(newImageMQ.pvidMap['For Surgeons Portlet'], fname);
		// order of parameters important. Send in blank string for data you dont care about.
		// the map is going to return items based on ID of the field (if you set one in builder or if the field is a select or something generated by builder
		// or if there is no id set then it will decode out the builder id (param_name = )
		// of if there is no builder id (it is a custom hidden field you have inserted), then it just uses the field name
		// selects get special attention. A select with a id/name of test will also populate a parameter called test_display in case you wanted what the user
		// saw rather than the internal ID builder might have used as a value.
		llog.logForm(
			fmap["ID-ForSurgeonsView-Email-control"], // email
			fmap["ID-ForSurgeonsView-FirstName-control"], // firstname
			fmap["ID-ForSurgeonsView-LastName-control"], // lastname
			fmap["ID-ForSurgeonsView-Address1-control"], // address 1
			fmap["ID-ForSurgeonsView-Address2-control"], // address 2
			fmap["ID-ForSurgeonsView-City-control"], // city
			fmap["ID-ForSurgeonsView-State-control"], // state (as in the state code. Do not send in the full state name)
			fmap["ID-ForSurgeonsView-Zip-control"], // zip
			fmap["ID-ForSurgeonsView-Comments-control"], // comments
			"", // procedurename text ( as in the human readable text).
			"", // action type. leave blank for user form
			fmap["ID-ForSurgeonsView-Phone-control"], // phone (of user)
			fmap["ID-ForSurgeonsView-ConfirmEmail-control"], // email confirmation.
			fmap["ID-ForSurgeonsView-PracticeName-control"], // company name
			"" // friends email
		);
	}
	newImageMQ.formBuilderHandlers["ID-ForSurgeons-form"] = newImageMQ.handlerForSurgeons;
}
