// dojomain.js   19 May 2009

   	dojo.require("dojo.io.iframe");
	dojo.require("dojo.parser");

			
	var callCount = 0;
	var ioResponse;
    var response;
    

	function formProcessorSubmit(formName, formDiv, thankyouDiv, errorDiv, exceptionDiv){
		
		 console.log("Starting FormProcessor submit");

		 var lead = "";
		 var scenarioName = "";
		  var mmrep="";

		// alert ("FormProcessorSubmit called w/ " + formName + ",");
    	console.log ("FormProcessorSubmit called w/ " + formName + ",");

		var formObjectName = document.getElementById(formName);
    	//alert(formObjectName);
	    console.log ("FormObjectName: " + formObjectName);

    	var TKM = document.getElementById(thankyouDiv).innerHTML;
    	console.log ("TKM is: " + TKM);
    	
    	AddInputParameter(formObjectName, "input", "webFormPage_ThankYouPage", TKM, document);
    	
		// Pass contentGroupDirectory and subContentGroupDirectory as basis for pageFrom.
		// The variables contentGroup and subContentGroup (w/o directory are structure group, which is
		//   not what is wanted for pageFrom).
		
		// get the value which is needed for MM Prelaunch form
	       if ( document.getElementById('beginapp-rep') != null) 
	       {
	         mmrep= document.getElementById('beginapp-rep').value; 
	       }
	       console.log ("MMREP is: " + mmrep);
	       
              if (mmrep != null && mmrep != "") {
		   //BA or CR
	       console.log (subContentGroupDirectory);
	       console.log (contentGroupDirectory);
	       console.log (audience);

	       var lsubContentGroupDirectory="";
	       var lcontentGroupDirectory="";
	       var laudience="";

	        if (subContentGroupDirectory != "") 
				{
	            console.log ("1.1"+subContentGroupDirectory)
	              lsubContentGroupDirectory= subContentGroupDirectory+"-"+mmrep;
	              lcontentGroupDirectory=contentGroupDirectory;
	              laudience=audience;
	            
	        } 
			else if (contentGroupDirectory != "") 
				{ 
	            console.log ("2.1"+contentGroupDirectory)
	              lcontentGroupDirectory=contentGroupDirectory+"-"+mmrep;
	              lsubContentGroupDirectory= subContentGroupDirectory;
	              laudience=audience;          
	        } 
			else if (audience != "") 
				{ 
				console.log ("3.1"+audience)
	              laudience=audience+"-"+mmrep;	              
	              lcontentGroupDirectory=contentGroupDirectory;
	              lsubContentGroupDirectory= subContentGroupDirectory;	              
	           
	        }
	       console.log (lsubContentGroupDirectory);
	       console.log (lcontentGroupDirectory);
	       console.log (laudience);       
	     	     
			if(typeof contentGroupDirectory=="undefined") 
			{
					 AddInputParameter(formObjectName, "input", "contentGroup", "", document);
			} 
			else 
			{
					 AddInputParameter(formObjectName, "input", "contentGroup", lcontentGroupDirectory, document);
			}

			if(typeof subContentGroupDirectory=="undefined") 
			{
					 AddInputParameter(formObjectName, "input", "subcontentGroup", "", document);
			} else {
					 AddInputParameter(formObjectName, "input", "subcontentGroup", lsubContentGroupDirectory, document);
			}

			if(typeof audience=="undefined") 
				{
					 AddInputParameter(formObjectName, "input", "audience", "", document);
			} 
			else 
			{
					 AddInputParameter(formObjectName, "input", "audience", laudience, document);
			}
    	} 
		
		else {
			
		//check for parameter in Querystring to populate 'PageFrom' vault.
		// We will add an input parameter here so that WebFormProcess can pick it up.
		// Added by Silvester
		
		var CGFrQS = "";
		var SCGFrQS = "";
		var AUFrQS = "";
		CGFrQS = getQueryString("CG");
		SCGFrQS = getQueryString("SCG");
		AUFrQS = getQueryString("AU");
		
		
		//first if condition added for "PageFrom" work
		if (CGFrQS != ""){
			AddInputParameter(formObjectName, "input", "contentGroup", CGFrQS, document);
	 	}else if(typeof contentGroupDirectory=="undefined") {
	  			 AddInputParameter(formObjectName, "input", "contentGroup", "", document);
	    } else {
	  			 AddInputParameter(formObjectName, "input", "contentGroup", contentGroupDirectory, document);
	    }
	  	//first if condition added for "PageFrom" work  
		if (SCGFrQS != ""){
			AddInputParameter(formObjectName, "input", "subcontentGroup", SCGFrQS, document);
	 	}else if (typeof subContentGroupDirectory == "undefined") {
			AddInputParameter(formObjectName, "input", "subcontentGroup", "", document);
		}else {
			AddInputParameter(formObjectName, "input", "subcontentGroup", subContentGroupDirectory, document);
		}
	   
	   //first if condition added for "PageFrom" work
	    if (AUFrQS != ""){
			AddInputParameter(formObjectName, "input", "audience", AUFrQS, document);
	 	}else if(typeof audience=="undefined") {
	  			 AddInputParameter(formObjectName, "input", "audience", "", document);
	    } else {
	  			 AddInputParameter(formObjectName, "input", "audience", audience, document);
    	  }
    	}




		// Determine the type of form.
		// Leads have four buckets in terms of tracking:
		//  1) "New Lead" Forms
		//  2) "Service Lead" forms, 
		//  3) Non-Lead Forms (w/o scenario)
		//  4) Non-Lead Form (w/ scenario)
		//  If it isn't 1) or 2), then it must be a Non-Lead form.  After determing if it is Non-Lead form, test
		//     special case of non-lead form w/ scenario.

		if (formName == "requestFormRightNav_Acc")
		{
			 var prodType=document.getElementById('requestType').value;
			 
			 // alert ("requestType.value: " + prodType);

			 if (prodType != "") {
				  if(prodType == "Existing Product/Policy") 
				  {
					   lead = "ServiceLead";
				  }
				  else if ((prodType.length > 11) && 
						   (prodType.substr(0,11) == "New Product"))
				  {
					   lead = "NewLead";
				  }
			 }
		}
		else if (formName == "requestFormRightNav")
		{
			 // alert ("Determining Lead Type for requestFormRightNav");

			 var prodType = "";
			 if(document.getElementById('requestTypeQuote'))
			 {
				  prodType = document.getElementById('requestTypeQuote').value;
			 }
			 else if (document.getElementById('requestType'))
			 {
				  prodType = document.getElementById('requestType').value;
			 }
 
			 // alert ("prodType: " + prodType);

			 if (prodType != "") {
				  if(prodType == "Existing Product/Policy") 
				  {
					   lead = "ServiceLead";
				  }
				  else if ((prodType.length >= 11) &&
						   (prodType.substr(0,11) == "New Product"))
				  {
					   lead = "NewLead";
				  }
			 }
		}
		else
		{		
			 var prodType = "";
			 if (document.getElementById('requestType')) {
				  prodType=document.getElementById('requestType').value;
			 }
			 
			 if (prodType != "") {
				  if ((prodType.length >= 11) &&
					  (prodType.substr(0,11) == "New Product"))
				  {
					   lead = "NewLead";
				  }
				  else if (prodType == "Existing Product/Policy") 
				  {
					   lead = "ServiceLead";
				  }
			 }
		}

		// If none of the above conditions determined that it was a lead or service lead form,
		//   then it falls into the Non-Lead Form bucket.
		// In here, test if it non-lead form w/ scenario.

		if (lead != "NewLead" && lead != "ServiceLead") {
			 lead = "NonLeadForm";
								  
			 /* Check if this is a non-lead form w/ a scenario. */
			 if ((document.getElementById('scenarioName') &&
				  document.getElementById('scenarioName').value != "")) {
				  scenarioName = document.getElementById('scenarioName').value;
			 }
		}

		console.log ("Lead type: " + lead);
    	
    	var results = document.cookie.match ( '(^|;) ?' + "WT_FPC" + '=([^;]*)(;|$)' );

		if(results != null)
		{
			//  	  	alert(results);
					var fullID = unescape (results[2]);
			//  	  	alert(fullID);
					var partID = fullID.split(":");
			//  	  	alert(partID);
					var visitorID = partID[0].split("=");
			//  	  	alert(visitorID[1]);
		}

  	  	if(typeof visitorID=="undefined") {
  	  		AddInputParameter(formObjectName, "input", "visitorIDReq", "", document);
  	  	} else {
  	  		//alert(visitorID[1]);
			AddInputParameter(formObjectName, "input", "visitorIDReq", visitorID[1], document);
  	  	}
  	  	
    	//alert("Adding URL node");
		
		
       //Added the webtrend code setting for - Phase 2 of middle market req - dsravanthi - 6/2/2009 
		//AddInputParameter(formObjectName, "input", "wb_code","webTrendTestCode", document);  
		//changes by dsravanthi - for page fro on 6/12/2009
		
		   var urlNode = document.URL;
		
		// check for the MMPrelaunch pageFrom in the URL.
			
		 urlNode=getPageFromURLNode(formObjectName,mmrep);
				
		AddInputParameter(formObjectName, "input", "webFormPage_urlPagevalue", urlNode, document);

    	//Extracting the span tag text
    	//var spanTagText = document.getElementById("webFormPage_spantag").innerHTML;
    	
    	//AddInputParameter(formObjectName, "input", "webFormPage_spantagPageSGvalue", spanTagText, document);
    	    	
       //https://dev.prv.webforms.metlife.com
       //https://localhost:9443
    	
    	var validationSuccess = true;
	//validateSubmitAll(formObjectName, errorDiv);
    
		if(validationSuccess) {
			 //alert("inside validation success");
			 
			       //Added the following piece of code to fix the federaldental site webforms prod issue.The issue was due to cert error
			       
			       var tempURL="www.metlife.com" ;
			       	if(location.host.match("prv")=="prv"){
			       	
			       			       		tempURL="prv.metlife.com";
			       			       	}else {
			       			       	
			       			       		tempURL="www.metlife.com";
			       			       	}
			       			       	
			       			       	if(location.host.split(".",1)=="qa"){
			       			       		tempURL ="qa."+tempURL;
			       			       	
			       			       	}else if(location.host.split(".",1)=="dev"){
			       			       		tempURL ="dev."+tempURL;
			       			      	}
			       			       	
			       			       
			    	var submitUrl = "https://"+tempURL+"/wps/proxy/WebFormServletAction";
			      
			         console.log("Doing Webform submit to: " + submitUrl);

				dojo.io.iframe.send({
					form: dojo.byId(formName),
					url: submitUrl,
					method: "POST",
					// mimetype: "text/plain",
					handleAs: "html",
					enctype: "multipart/form-data",
					content: {
						increment: callCount++,
						fileFields: "attachURL"
					},
				handle: function(response, ioArgs){
					   console.info('First line of handle function');
						var submitError = false;
						// var returnValues = dojo.toJson(response,false);
						// console.info('back from call data = ' + returnValues);

						// if(response instanceof Error){
						if (1 == 0) {
                             console.info("response was instanceof Error");
                             submitError = true;

							 /* Submitting a secure post from a non-secure one causes error to be thrown in IE */
							 /*   and in Chrome (only returns "Success" in Firefox).  This same error is given even */
							 /*   if the webform processor is turned off.  So, the error is purely about a domain (really protocol) */
							 /*   mismatch, and nothing else. */
                             /* First, check to see if page is currently http:.  If it isn't then assume all errors are */
                             /*   related to protocol mismatch. */
                             if (location.protocol == "http:") {
                                 var ieErrorMessage = "Access is denied.";
							     var chromeErrorType = "non_object_property_load";
							     if (typeof(response.message) != "undefined" && dojo.isIE) {
									  if (response.message.substr(0,ieErrorMessage.length) == ieErrorMessage) {
										   console.info("isIE is true, and error was " + ieErrorMessage + "so passing through.");
										   submitError = false;
									  }
							     }
                                 /* Chrome test case. */
							     else if ((typeof(response.type) != "undefined") && (typeof(response.message) != "undefined")) {
									  if (response.type == chromeErrorType && response.message == "") {
										   console.info("Got the chrome error type and message was blank, so assuming Chrome.");
										   submitError = false;
									  }
							     }
                             }
						}
						else {
							 console.info("Dojo submit successful condition.");
						}

						if (submitError == true) {
							 /* Old error handling code -- currently inadequate. */
							 dojo.byId(exceptionDiv).innerHTML = response;
							 dojo.byId(exceptionDiv).style.display = "block";
							 dojo.byId(formName).style.visibility="hidden";
							 document.getElementById(errorDiv).style.visibility="hidden";
						}
						else if (submitError == false) {

							 console.info("Begin post submit processing.");

							 //alert("calling lead");
							 if(lead == "NewLead") {
							
								  try {
									   trackEvent("webform","ScenarioName:Lead;scenarioStep:1;scenarioLead:1");
								  } catch (ex) {
									   // alert(ex.description);
								  }

								  // Call the JavaScript function to fire any dynamic tags on this page for this form type.
								  try {

									   console.log("Trying out calling New Lead function.");

									   if (typeof (formTaggingNewLead) == "function") {
											formTaggingNewLead();
									   }
								  } catch (ex) {
									   // alert(ex.description);
								  }

								  //alert(lead);
							 } else if (lead == "ServiceLead") {
								  
								  try {
									   trackEvent("webform","ScenarioName:ServiceLead;scenarioStep:1;scenarioLead:0");
								  } catch(ex1) {
									   // alert(ex1.description);
								  }

								  // Call the JavaScript function to fire any dynamic tags on this page for this form type.
								  try {
									   if (typeof (formTaggingServiceLead) == "function") {
											formTaggingServiceLead();
									   }
								  } catch (ex) {
									   // alert(ex.description);
								  }


							 } else if (lead == "NonLeadForm") {

								  try {
									   if (scenarioName == "") {
											trackEvent("webform","FormName:"+formName+";scenarioName:;scenarioStep:;scenarioLead:;");
											
											if (typeof (formTaggingNonLead) == "function") {
												 formTaggingNonLead();
											}
									   }
									   else {
											
											trackEvent("webform","FormName:"+formName+";scenarioName:"+scenarioName+";scenarioStep:1;scenarioLead:0;cg:"+scenarioName+";");
											// Call the JavaScript function to fire any dynamic tags on this page for this form type.
											if (typeof (formTaggingNonLeadwScenario) == "function") {
												 formTaggingNonLeadwScenario();
											}
									   }
								  } catch(ex1) {
									   // alert("webform trackEvent for NonLeadForm: " + ex1.description);
								  }
							 }

							if(response == "Attachment") {
								dojo.byId(errorDiv).innerHTML = "<font color='#FF0033'>Your attachment has exceeded maximum File size limit of 10Mb.</font>";
								dojo.byId(errorDiv).style.display = "block";
							} else {
							 //alert("displaying messages");
							 // Displaying TKM, rather than response.  This is because submitting a secure page from insecure
							 //   protocol will prevent you from seeing thank you.
								 // dojo.byId(thankyouDiv).innerHTML = response;
								dojo.byId(thankyouDiv).innerHTML = TKM;
	          					dojo.byId(thankyouDiv).style.display = "block";
	          					dojo.byId(formName).style.visibility="hidden";
	          					dojo.byId(formDiv).style.visibility="hidden";
	          					dojo.byId(formDiv).style.display = "none";
	          					dojo.byId(exceptionDiv).style.visibility="hidden";
	          					dojo.byId(errorDiv).style.visibility="hidden";
							if(formName == "requestFormRightNav_Acc")
															{
							  slideContentAccordion(accordion_activeId,-10);
							}
							return false;
							//alert("displayed messages");
							}
							
          				} // end else on if (submitError == false)
						ioResponse = response; // to get at afterwards in debugger.
						  }
					 });   // end handle function for io.frame.send()
		}
	}

// Funtion to getattributes from QueryString addded by Silvester
function getQueryString( name )
{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );

	if( results == null )
		return "";
	else
		return results[1];
}	

function emailFormProcessorSubmit(emailformName, emailformDiv, emailthankyouDiv, emailerrorDiv, emailexceptionDiv) {

	
	var emailformObjectName = document.getElementById(emailformName);
	
	var emailpageTKM = document.getElementById(emailthankyouDiv).innerHTML;
	//Adding Thank You Message node
	AddInputParameter(emailformObjectName, "input", "webFormPage_ThankYouPage", emailpageTKM, document);

	//Adding URL node
	var urlNode = document.URL;
	AddInputParameter(emailformObjectName, "input", "webFormPage_urlPagevalue", urlNode, document);

	//Extracting the span tag text
	var spanTagText = document.getElementById("webFormPage_spantag").innerHTML;
	AddInputParameter(emailformObjectName, "input", "webFormPage_spantagPageSGvalue", spanTagText, document);
	
	var emailpagevalidationSuccess = emailpageValidateSubmitAll(emailformObjectName, emailerrorDiv);
	
	if (emailpagevalidationSuccess) {

		dojo.xhrGet( {
					// The following URL must match that used to test the server.
					url :"/wps/emailproxy/EmailPageServletAction",

					// Name of the Form we want to submit
					form : dojo.byId(emailformName),

					// The LOAD function will be called on a successful response.
					load : function(response) {
						//dojo.byId("ThankYouMessageField_div").innerHTML = response;
						dojo.byId(emailthankyouDiv).style.display = "block";
						dojo.byId(emailformName).style.visibility = "hidden";
						dojo.byId(emailformDiv).style.visibility="hidden";
						dojo.byId(emailformDiv).style.display = "none";
						dojo.byId(emailexceptionDiv).style.visibility = "hidden";
						dojo.byId(emailerrorDiv).style.visibility = "hidden";			
						return response;
			
			
					},

					// The ERROR function will be called in an error case.
					error : function(response, ioArgs) {
						alert("Error" + response);
						dojo.byId(emailexceptionDiv).innerHTML = response;
						dojo.byId(emailexceptionDiv).style.display = "block";
						dojo.byId(emailformName).style.visibility = "hidden";
						document.getElementById(emailerrorDiv).style.visibility = "hidden";
						dojo.byId(emailexceptionDiv).style.visibility = "hidden";
						
						
						return response;
					}
				});
	}
}

function TellAFriendFormSubmit(emailformName, emailformDiv, emailthankyouDiv, emailerrorDiv, emailexceptionDiv){

    var emailformObjectName = document.getElementById(emailformName);
                
                var emailpageTKM = document.getElementById(emailthankyouDiv).innerHTML;
                //Adding Thank You Message node
                AddInputParameter(emailformObjectName, "input", "webFormPage_ThankYouPage", emailpageTKM, document);

                //Adding URL node
                var urlNode = document.URL;
                AddInputParameter(emailformObjectName, "input", "webFormPage_urlPagevalue", urlNode, document);

                dojo.io.iframe.send({
                           url : "https://" + location.host + "/wps/emailproxy/EmailPageServletAction",
                                                   form : dojo.byId(emailformName),
                                                                enctype: "multipart/form-data",
                                                                method : "POST",
                                                                handleAs : "text",
                                                   content: {
                                                                                increment: callCount++,
                                                                                fileFields: "attachURL"
                                                                },
                                                                handle : function(response, ioArgs){
                                                                                                                dojo.byId(emailthankyouDiv).style.display = "block";
                                                                                                                dojo.byId(emailformName).style.visibility = "hidden";
                                                                                                                dojo.byId(emailformDiv).style.visibility="hidden";
                                                                                                                dojo.byId(emailformDiv).style.display = "none";
                                                                                                                dojo.byId(emailexceptionDiv).style.visibility = "hidden";
                                                                                                                dojo.byId(emailerrorDiv).style.visibility = "hidden"; 
														// Tell A Friend form submit Track Event
														 trackEvent("TAF","type:submit");
                                                                },
                                                                
                                                                error : function(response, ioArgs){
                                                                                                                dojo.byId(emailexceptionDiv).innerHTML = response;
                                                                                                                dojo.byId(emailexceptionDiv).style.display = "block";
                                                                                                                dojo.byId(emailformName).style.visibility = "hidden";
                                                                                                                document.getElementById(emailerrorDiv).style.visibility = "hidden";
                                                                                                                dojo.byId(emailexceptionDiv).style.visibility = "hidden";
                                                                }
       }); 

}

function AddInputParameter(obj, element, name, value, doc) {
	  	var TKMinputobj = doc.createElement(element);       	
	       	TKMinputobj.setAttribute("type", "hidden");
	       	TKMinputobj.setAttribute("name", name);
	       	TKMinputobj.setAttribute("value", value);
	       	obj.appendChild(TKMinputobj);
	  } 
	  

	  
/*
 * The javascript object "requestParameters" should be of the format - 
 * {
 * 		term : _termValue_,
 * 		age : _ageValue_,
 * 		gender : _genderValue_,
 * 		health : _healthPlanValue_,
 * 		coverage : _coverage_
 * }
 */
function getPremiumQuote(requestParameters, fnShowPremium){
	dojo.io.iframe.send({
	//url : "https://" + location.host+ "/wps/MCPremiumQuoteWS/MCPremiumQuote",
	/*
	 * Making the URL relative for the purpose of testing.
	 * Please uncomment the earlier "url" argument for all practical purposes.
	 */
	url : "/wps/MCPremiumQuoteWS/MCPremiumQuote",
	content: requestParameters,
			contentType: " multipart/form-data",
			method : "POST",
			handleAs : "text",
			handle : function(data, ioArgs){
				console.debug(data);
				var response = dojo.fromJson(data);
				console.debug(response.statuscode);
				console.debug(response.statusmsg);
				if(response.statuscode == 0)
					fnShowPremium(response.premium, requestParameters.age);
				else{
					console.debug("In handle method but the status code is invalid "+response.statusmsg);
					showRep();
				}
			},
			timeout : 9500,
			error : function(response, ioArgs){
				console.debug("Error Response "+response);
				showRep();
			}
		}
	);
}

function getPageFromURLNode(formObjectName,mmrep) {

	var urlNode = document.URL;
	console.log("url node is"+urlNode);
	var newPageFrom="";
	var urlString=(window.location.search).split("?");
	var urlHrefString="";
	var newURLnodeParams="";
	var pageFrmCheck=false;

	if(urlString.length==2)
	{
		var urlParams=urlString[1].split("&");
		console.log ("urlParams Length: " + urlParams.length);

		for(var i=0;i<urlParams.length;i++){
			var tempParam=urlParams[i].split("=");
			if(tempParam[0].toLowerCase()=="wt.mc_id"){
				 console.log ("value for  wt_mc_id is " + tempParam[1]);
				AddInputParameter(formObjectName, "input", "wb_code", tempParam[1], document);    
			}	 

			// update the pagefrom value if beginapp-rep value exists
		if(mmrep!=""){
			   if(tempParam[0].toLowerCase()=="pagefrom"){
				newPageFrom=tempParam[1]+"-"+mmrep;
				console.log("newPageFrom:"+newPageFrom);			
				    if(i==(urlParams.length - 1)){
					     newURLnodeParams=newURLnodeParams+tempParam[0]+"="+newPageFrom;
				    }else{
					     newURLnodeParams=newURLnodeParams+tempParam[0]+"="+newPageFrom+"&";
				    }
				pageFrmCheck=true;
			     }else{   
				 if(i==(urlParams.length - 1)){ 
					newURLnodeParams=newURLnodeParams+urlParams[i];
						}else{
					newURLnodeParams=newURLnodeParams+urlParams[i]+"&";
				  }
			     }				
		   }
	      }//For loop closes here		
	       console.log("pageFrmCheck:"+pageFrmCheck);
	       console.log("newURLnodeParams:"+newURLnodeParams);
			if(pageFrmCheck){
				var tempNode=document.URL;					
				var urlStringArray=tempNode.split("?");
				urlHrefString=window.location.href.split("#")[1];
				if(urlHrefString!=""){
					urlNode=urlStringArray[0]+"?"+newURLnodeParams;
				}else{
					urlNode=urlStringArray[0]+"?"+newURLnodeParams+"#"+urlHrefString;
				}
			 }				
	   }

			console.log("urlNode:"+urlNode);

	//till here setting for - Phase 2 of middle market req - dsravanthi - 6/1/2009
	//changes by dsravanthi - for page fro on 6/12/2009
	console.log("After Changing urlNode is "+urlNode);
	return urlNode;
}

//Added by dsravanthi on 10/19/2009 for theInternational Form processor submit

function webformProcessorSubmit(formName, formDiv, thankyouDiv, errorDiv, exceptionDiv)
{		
	console.log("Starting webFormProcessor submit");

	var lead = "";
	var scenarioName = "";
	var mmrep="";

	// alert ("FormProcessorSubmit called w/ " + formName + ",");
	console.log ("WebFormProcessorSubmit called w/ " + formName + ",");
	var formObjectName = document.getElementById(formName);

	//alert(formObjectName);
	console.log ("FormObjectName: " + formObjectName);

	var TKM = document.getElementById(thankyouDiv).innerHTML;
	console.log ("TKM is: " + TKM);
	
	AddInputParameter(formObjectName, "input", "webFormPage_ThankYouPage", TKM, document);
	
	// Pass contentGroupDirectory and subContentGroupDirectory as basis for pageFrom.
	// The variables contentGroup and subContentGroup (w/o directory are structure group, which is
	//   not what is wanted for pageFrom).
	
	//check for parameter in Querystring to populate 'PageFrom' vault.
	// We will add an input parameter here so that WebFormProcess can pick it up.
	// Added by Silvester
	
	var CGFrQS = "";
	var SCGFrQS = "";
	var AUFrQS = "";
	CGFrQS = getQueryString("CG");
	SCGFrQS = getQueryString("SCG");
	AUFrQS = getQueryString("AU");
	
	
	//first if condition added for "PageFrom" work
	if (CGFrQS != "")
	{
		AddInputParameter(formObjectName, "input", "contentGroup", CGFrQS, document);
	}
	else if(typeof contentGroupDirectory=="undefined") 
	{
			 AddInputParameter(formObjectName, "input", "contentGroup", "", document);
	} 
	else 
	{
			 AddInputParameter(formObjectName, "input", "contentGroup", contentGroupDirectory, document);
	}
	//first if condition added for "PageFrom" work  
	if (SCGFrQS != "")
	{
		AddInputParameter(formObjectName, "input", "subcontentGroup", SCGFrQS, document);
	}
	else if (typeof subContentGroupDirectory == "undefined") 
	{
		AddInputParameter(formObjectName, "input", "subcontentGroup", "", document);
	}
	else 
	{
		AddInputParameter(formObjectName, "input", "subcontentGroup", subContentGroupDirectory, document);
	}
   
   //first if condition added for "PageFrom" work
	if (AUFrQS != "")
	{
		AddInputParameter(formObjectName, "input", "audience", AUFrQS, document);
	}
	else if(typeof audience=="undefined") 
	{
			 AddInputParameter(formObjectName, "input", "audience", "", document);
	}
	else 
	{
			 AddInputParameter(formObjectName, "input", "audience", audience, document);
	 }




	// Determine the type of form.
	// Leads have four buckets in terms of tracking:
	//  1) "New Lead" Forms
	//  2) "Service Lead" forms, 
	//  3) Non-Lead Forms (w/o scenario)
	//  4) Non-Lead Form (w/ scenario)
	//  If it isn't 1) or 2), then it must be a Non-Lead form.  After determing if it is Non-Lead form, test
	//     special case of non-lead form w/ scenario.
	/*
	if (formName == "requestFormRightNav_Acc")
	{
		 var prodType=document.getElementById('requestType').value;
		 
		 // alert ("requestType.value: " + prodType);

		 if (prodType != "") {
			  if(prodType == "Existing Product/Policy") 
			  {
				   lead = "ServiceLead";
			  }
			  else if ((prodType.length > 11) && 
					   (prodType.substr(0,11) == "New Product"))
			  {
				   lead = "NewLead";
			  }
		 }
	}
	else if (formName == "requestFormRightNav")
	{
		 // alert ("Determining Lead Type for requestFormRightNav");

		 var prodType = "";
		 if(document.getElementById('requestTypeQuote'))
		 {
			  prodType = document.getElementById('requestTypeQuote').value;
		 }
		 else if (document.getElementById('requestType'))
		 {
			  prodType = document.getElementById('requestType').value;
		 }

		 // alert ("prodType: " + prodType);

		 if (prodType != "") {
			  if(prodType == "Existing Product/Policy") 
			  {
				   lead = "ServiceLead";
			  }
			  else if ((prodType.length >= 11) &&
					   (prodType.substr(0,11) == "New Product"))
			  {
				   lead = "NewLead";
			  }
		 }
	}
	else
	{		
		 var prodType = "";
		 if (document.getElementById('requestType')) {
			  prodType=document.getElementById('requestType').value;
		 }
		 
		 if (prodType != "") {
			  if ((prodType.length >= 11) &&
				  (prodType.substr(0,11) == "New Product"))
			  {
				   lead = "NewLead";
			  }
			  else if (prodType == "Existing Product/Policy") 
			  {
				   lead = "ServiceLead";
			  }
		 }
	}
	// If none of the above conditions determined that it was a lead or service lead form,
	//   then it falls into the Non-Lead Form bucket.
	// In here, test if it non-lead form w/ scenario.

	if (lead != "NewLead" && lead != "ServiceLead") {
		 lead = "NonLeadForm";
							  
		 //Check if this is a non-lead form w/ a scenario. 
		 if ((document.getElementById('scenarioName') &&
			  document.getElementById('scenarioName').value != "")) {
			  scenarioName = document.getElementById('scenarioName').value;
		 }
	}

	console.log ("Lead type: " + lead);
	No need - till here */
	
	var results = document.cookie.match ( '(^|;) ?' + "WT_FPC" + '=([^;]*)(;|$)' );

	if(results != null)
	{
		//  	  	alert(results);
				var fullID = unescape (results[2]);
		//  	  	alert(fullID);
				var partID = fullID.split(":");
		//  	  	alert(partID);
				var visitorID = partID[0].split("=");
		//  	  	alert(visitorID[1]);
	}

	if(typeof visitorID=="undefined") {
		AddInputParameter(formObjectName, "input", "visitorIDReq", "", document);
	} else {
		//alert(visitorID[1]);
		AddInputParameter(formObjectName, "input", "visitorIDReq", visitorID[1], document);
	}
	
	//alert("Adding URL node");
	
	
	//Added the webtrend code setting for - Phase 2 of middle market req - dsravanthi - 6/2/2009 
	//AddInputParameter(formObjectName, "input", "wb_code","webTrendTestCode", document);  
	//changes by dsravanthi - for page fro on 6/12/2009
	
	   var urlNode = document.URL;
	
	// check for the MMPrelaunch pageFrom in the URL.		
	 urlNode=getPageFromURLNode(formObjectName,mmrep);
			
	AddInputParameter(formObjectName, "input", "webFormPage_urlPagevalue", urlNode, document);

	//Extracting the span tag text
	//var spanTagText = document.getElementById("webFormPage_spantag").innerHTML;
	
	//AddInputParameter(formObjectName, "input", "webFormPage_spantagPageSGvalue", spanTagText, document);
			
   //https://dev.prv.webforms.metlife.com
   //https://localhost:9443
	
	var validationSuccess = true;
	//validateSubmitAll(formObjectName, errorDiv);

	if(validationSuccess) {
		 //alert("inside validation success");
		 
			   //Added the following piece of code to fix the federaldental site webforms prod issue.The issue was due to cert error
			   
			   var tempURL="www.metlife.com" ;
				if(location.host.match("prv")=="prv"){
				
									tempURL="prv.metlife.com";
								}else {
								
									tempURL="www.metlife.com";
								}
								
								if(location.host.split(".",1)=="qa"){
									tempURL ="qa."+tempURL;
								
								}else if(location.host.split(".",1)=="dev"){
									tempURL ="dev."+tempURL;
								}
								
							   
				var submitUrl = "https://"+tempURL+"/wps/proxy/IntlWebFormServletAction";
			  
				 console.log("Doing Webform submit to: " + submitUrl);

			dojo.io.iframe.send({
				form: dojo.byId(formName),
				//url: submitUrl,
				url:"/wps/proxy/IntlWebFormServletAction",
				method: "POST",
				// mimetype: "text/plain",
				handleAs: "html",
				enctype: "multipart/form-data",
				content: {
					increment: callCount++,
					fileFields: "attachURL"
				},
			handle: function(response, ioArgs){
				   console.info('First line of handle function');
					var submitError = false;
					// var returnValues = dojo.toJson(response,false);
					// console.info('back from call data = ' + returnValues);

					// if(response instanceof Error){
					if (1 == 0) {
						 console.info("response was instanceof Error");
						 submitError = true;

						 /* Submitting a secure post from a non-secure one causes error to be thrown in IE */
						 /*   and in Chrome (only returns "Success" in Firefox).  This same error is given even */
						 /*   if the webform processor is turned off.  So, the error is purely about a domain (really protocol) */
						 /*   mismatch, and nothing else. */
						 /* First, check to see if page is currently http:.  If it isn't then assume all errors are */
						 /*   related to protocol mismatch. */
						 if (location.protocol == "http:") {
							 var ieErrorMessage = "Access is denied.";
							 var chromeErrorType = "non_object_property_load";
							 if (typeof(response.message) != "undefined" && dojo.isIE) {
								  if (response.message.substr(0,ieErrorMessage.length) == ieErrorMessage) {
									   console.info("isIE is true, and error was " + ieErrorMessage + "so passing through.");
									   submitError = false;
								  }
							 }
							 /* Chrome test case. */
							 else if ((typeof(response.type) != "undefined") && (typeof(response.message) != "undefined")) {
								  if (response.type == chromeErrorType && response.message == "") {
									   console.info("Got the chrome error type and message was blank, so assuming Chrome.");
									   submitError = false;
								  }
							 }
						 }
					}
					else {
						 console.info("Dojo submit successful condition.");
					}

					if (submitError == true) {
						 /* Old error handling code -- currently inadequate. */
						 dojo.byId(exceptionDiv).innerHTML = response;
						 dojo.byId(exceptionDiv).style.display = "block";
						 dojo.byId(formName).style.visibility="hidden";
						 document.getElementById(errorDiv).style.visibility="hidden";
					}
					else if (submitError == false) {

						 console.info("Begin post submit processing.");

						/*
						 //alert("calling lead");
						 if(lead == "NewLead") {
						
							  try {
								   trackEvent("webform","ScenarioName:Lead;scenarioStep:1;scenarioLead:1");
							  } catch (ex) {
								   // alert(ex.description);
							  }

							  // Call the JavaScript function to fire any dynamic tags on this page for this form type.
							  try {

								   console.log("Trying out calling New Lead function.");

								   if (typeof (formTaggingNewLead) == "function") {
										formTaggingNewLead();
								   }
							  } catch (ex) {
								   // alert(ex.description);
							  }

							  //alert(lead);
						 } else if (lead == "ServiceLead") {
							  
							  try {
								   trackEvent("webform","ScenarioName:ServiceLead;scenarioStep:1;scenarioLead:0");
							  } catch(ex1) {
								   // alert(ex1.description);
							  }

							  // Call the JavaScript function to fire any dynamic tags on this page for this form type.
							  try {
								   if (typeof (formTaggingServiceLead) == "function") {
										formTaggingServiceLead();
								   }
							  } catch (ex) {
								   // alert(ex.description);
							  }


						 } else if (lead == "NonLeadForm") {

							  try {
								   if (scenarioName == "") {
										trackEvent("webform","FormName:"+formName+";scenarioName:;scenarioStep:;scenarioLead:;");
										
										if (typeof (formTaggingNonLead) == "function") {
											 formTaggingNonLead();
										}
								   }
								   else {
										
										trackEvent("webform","FormName:"+formName+";scenarioName:"+scenarioName+";scenarioStep:1;scenarioLead:0;cg:"+scenarioName+";");
										// Call the JavaScript function to fire any dynamic tags on this page for this form type.
										if (typeof (formTaggingNonLeadwScenario) == "function") {
											 formTaggingNonLeadwScenario();
										}
								   }
							  } catch(ex1) {
								   // alert("webform trackEvent for NonLeadForm: " + ex1.description);
							  }
						 }
						 NO need till here */

						if(response == "Attachment") {
							dojo.byId(errorDiv).innerHTML = "<font color='#FF0033'>Your attachment has exceeded maximum File size limit of 10Mb.</font>";
							dojo.byId(errorDiv).style.display = "block";
						} else {
						 //alert("displaying messages");
						 // Displaying TKM, rather than response.  This is because submitting a secure page from insecure
						 //   protocol will prevent you from seeing thank you.
							 // dojo.byId(thankyouDiv).innerHTML = response;
							dojo.byId(thankyouDiv).innerHTML = TKM;
							dojo.byId(thankyouDiv).style.display = "block";
							dojo.byId(formName).style.visibility="hidden";
							dojo.byId(formDiv).style.visibility="hidden";
							dojo.byId(formDiv).style.display = "none";
							dojo.byId(exceptionDiv).style.visibility="hidden";
							dojo.byId(errorDiv).style.visibility="hidden";
						if(formName == "requestFormRightNav_Acc")
														{
						  slideContentAccordion(accordion_activeId,-10);
						}
						if(formName.substring(0,13) == "RRGenericForm")
						{
						  slideContentAccordion(accordion_activeId,-10);
						}
						return false;
						//alert("displayed messages");
						}
						
					} // end else on if (submitError == false)
					ioResponse = response; // to get at afterwards in debugger.
					  }
				 });   // end handle function for io.frame.send()
	}
}