/*
 * Remote Redundant Form Submission
 * Capture Local Form Values to Send Redundantly to Remote Source
 * jschiltz 20090801
 *
 *
	A) Capture Constants
	B) Programmatically create a form for storing & passing values to remote host
	E) Send values to remote host
*/



//#region Constants

// use Constants to set all environment variables

var s_remoteAction = "http://gw.vtrenz.net/?method=cSurveyWebservice.submit&dkey=IYLMUSVC15&questionIDList=91445,91446,91447,1,2,15,14,16,5,6,88160,84601,88159,84614&mode=live";
var s_remoteMethod = "post";
var s_formName = "registration";	// name of the source form
var s_localTarget = "hiddenFrame";	// target page/frame to submit the form blank may get blocked by popup-blockers
var s_localFormId = "redundantForm";

// form element mapping, source->remote
var a_qMapping = {
	username:"q91445",
	password:"q91446",
	screen_name:"q91447",
	first_name:"q1",
	last_name:"q2",
	company_name:"q15",
	phone_work:"q16",
	city:"q5",
	state:"q6",
	address:null,
	affiliation:"q88160",
	products:"q84601",
	commissions:"q88159",
	current_agent:"q84614"
};
// NOTE: username is also email address (q14) and will need to be added "manually"
var s_emailMapping = "q14";

// form validation skip (these elements will be ignored in validation attempt)
var a_skipElements = {
	types: Array("hidden", "button"),
	names: Array("company_name")
}; 

//#endregion



//#region Forms

// redundantForm : gathers form values, builds a new form object in the page based on Constants
// no changes here
function redundantForm(o_aForm)
{
	var o_newDiv = document.createElement("div");
	o_newDiv.style.display = "none";

	var o_newForm = document.createElement("form");
	o_newForm.action = s_remoteAction;
	o_newForm.method = s_remoteMethod;
	o_newForm.target = s_localTarget;
	o_newForm.id = s_localFormId;
	o_newDiv.appendChild(o_newForm);

	var o_theBody = document.getElementsByTagName("body")[0];
	o_theBody.appendChild(o_newDiv);
	
	var s_elementName = null;
	var s_elementValue = null;
	var o_reusableInput = null;

	for(i=0; i<o_aForm.elements.length; i++)
	{
		s_elementName = o_aForm.elements[i].name;
		s_elementValue = o_aForm.elements[i].value;
	
		if(s_elementName in a_qMapping && a_qMapping[s_elementName]!=null)
		{
			o_reusableInput = document.createElement("input");
			o_reusableInput.name = a_qMapping[s_elementName]; // use array from constants to generate input name
			o_reusableInput.id = a_qMapping[s_elementName];
			o_reusableInput.type = "hidden";
			o_reusableInput.value = s_elementValue; // assign from source form elements
			o_newForm.appendChild(o_reusableInput);

			if(s_elementName.toString()=="username")	 // "manually" add the username->email element
			{
				o_reusableInput = document.createElement("input");
				o_reusableInput.name = s_emailMapping;
				o_reusableInput.id = s_emailMapping;
				o_reusableInput.type = "hidden";
				o_reusableInput.value = s_elementValue;
				o_newForm.appendChild(o_reusableInput);
			}
			
		}
	}
		
	// The following elements are needed for the form to work.
	// They're hidden fields that must be passed. Now it is working.
	o_reusableInput = document.createElement("input");
	o_reusableInput.name = "formRelocateURL";
	o_reusableInput.type = "hidden";
	o_reusableInput.value = "";
	o_newForm.appendChild(o_reusableInput);

	o_reusableInput = document.createElement("input");
	o_reusableInput.name = "clicksource";
	o_reusableInput.type = "hidden";
	o_reusableInput.value = "";
	o_newForm.appendChild(o_reusableInput);

	o_reusableInput = document.createElement("input");
	o_reusableInput.name = "iMarketingSyncID";
	o_reusableInput.type = "hidden";
	o_reusableInput.value = "";
	o_newForm.appendChild(o_reusableInput);

	o_reusableInput = document.createElement("input");
	o_reusableInput.name = "WebSyncID";
	o_reusableInput.type = "hidden";
	o_reusableInput.value = "";
	o_newForm.appendChild(o_reusableInput);
}

//#endregion


// validForm : verifies form's elements have values
function validForm(o_formToCheck, a_elementsToSkip)
{
	var s_checkType = null;
	var s_checkName  = null;
	var s_checkValue = null;
	var s_alertMsg = "";
	var b_hasValue = false;
	var s_labelHTML = "";
	
	s_typesToSkip = a_elementsToSkip.types.toString();
	s_namesToSkip = a_elementsToSkip.names.toString();

	for(i=0; i<o_formToCheck.elements.length; i++)
	{
		s_checkType = o_formToCheck.elements[i].type.toString();
		s_checkName = o_formToCheck.elements[i].name.toString();
		s_checkValue = o_formToCheck.elements[i].value.toString();
		b_hasValue = false;
		
		// exclude those in the 'skip' list
		if(s_checkType != "" && s_checkName != "" && s_typesToSkip.indexOf(s_checkType)<0 && s_namesToSkip.indexOf(s_checkName)<0)
		{
			// alert(s_checkName + ':' + o_formToCheck.elements[s_checkName].length)
			
			if(o_formToCheck.elements[s_checkName].length>1) // indicates a form element which has multiple value options
			{
				checkForValue : for(j=0; j<o_formToCheck.elements[s_checkName].length; j++)
				{
					if(s_checkType.indexOf("select")>=0 && o_formToCheck.elements[s_checkName][j].selected && o_formToCheck.elements[s_checkName][j].value != "")
					{
						b_hasValue = true;
						break checkForValue;
					}
					else if((s_checkType == "checkbox" || s_checkType == "radio") && o_formToCheck.elements[s_checkName][j].checked)
					{
						b_hasValue = true;
						break checkForValue;
					}
				}
			}
			else if(o_formToCheck.elements[s_checkName].value && o_formToCheck.elements[s_checkName].value.toString() != "" && o_formToCheck.elements[s_checkName].value.length>0)
			{
				b_hasValue = true;
			}
			
			if(!b_hasValue)
			{
				s_labelHTML = highlightLabel(o_formToCheck, s_checkName);
				s_alertMsg += s_labelHTML + "\r\n";
			}
			else
				resetLabel(o_formToCheck, s_checkName);
		}
	}
	
	if(s_alertMsg == "")
		return true;
	else
	{
		alert("Please complete all required fields before submitting this form.");
		return false;
	}
}

// highlightLabel : highlight label for form element
function highlightLabel(o_formToCheck, s_formElementName)
{
	var o_labelElements = o_formToCheck.getElementsByTagName("label");
	var s_labelText = "";
	
	labelsLoop : for (l=0; l<o_labelElements.length; l++)
	{
		if (o_labelElements[l].getAttribute("for") == s_formElementName)
		{
			s_labelText = o_labelElements[l].innerHTML;
			o_labelElements[l].innerHTML = "<span style=\"background:#ff6600;color:#fff;font-weight:bold;\">" + o_labelElements[l].innerHTML + "</span>";
			return s_labelText;
		}
	}
	
	return s_formElementName
}

// resetLabel : reset label for form element
function resetLabel(o_formToCheck, s_formElementName)
{
	var o_labelElements = o_formToCheck.getElementsByTagName("label");
	labelsLoop : for (l=0; l<o_labelElements.length; l++)
	{
		if (o_labelElements[l].getAttribute("for") == s_formElementName)
		{
			if(o_labelElements[l].getElementsByTagName("span").length>0)
				o_labelElements[l].getElementsByTagName("span")[0].setAttribute("style", "");
			
			break labelsLoop;
		}
	}
}

//#endregion


//#region FormSubmission

// doFormsSubmit : call this from your page's submit
function doFormsSubmit()
{
	var o_aForm = document.forms[s_formName];
	var b_FormIsValid = validForm(o_aForm, a_skipElements);

	if(b_FormIsValid)
	{
		redundantForm(o_aForm);
		document.forms[s_localFormId].submit();
		setTimeout("doLocalSubmit()", 3000);
	}
}

// doLocalSubmit : submit the form on the local page
function doLocalSubmit()
{
	document.forms[s_formName].submit();
}
//#endregion


/*
window.onload = function()
{
	document.getElementById("reg_submit").style.display = "none";
	var o_submitParent = document.getElementById("reg_submit").parentNode;
	
	o_newSubmit = document.createElement("input");
	o_newSubmit.type = "button";
	o_newSubmit.value = "submit";
	o_newSubmit.onclick = doFormsSubmit;
	o_submitParent.appendChild(o_newSubmit);
	//alert(document.forms[s_formName].onsubmit);
	
}
*/