var new_ProspectCreated = false;
var new_ProspectCreatedId;

function EncodeHtml(s)
{		
	s = s.replace(/&/g, "~amp;");
	s = s.replace(/</g, "~lt;");
	s = s.replace(/>/g, "~gt;");
	s = s.replace(/\"/g, "~quot;");		
	s = s.replace(/\'/g, "~apos;");	
	
	return s;
}

function DecodeHtml(s)
{		
	s = s.replace(/~amp;/g, "&");
	s = s.replace(/~lt;/g, "<");
	s = s.replace(/~gt;/g, ">");
	s = s.replace(/\~quot;/g, "&quot;");		
	s = s.replace(/\~apos;/g, "'");	
	
	return s;
}

function next(path)
{
	var strResult = xmlConverter(1);
	
	if(strResult == false)
		return;
		
	var startIndex	= path.lastIndexOf("webform");
	var new_vid		= path.substring(startIndex);
	var dotIndex	= new_vid.lastIndexOf(".");
	new_vid			= new_vid.substring(7, dotIndex);		
	var idx;
	var qString;
	try
	{
		qString = parent.location.href;
		idx		= qString.indexOf('#');
		if(idx > 0)
		{
		   qString = qString.substring(0, idx);
		}
	}
	catch(ex)//handle permission denied
	{
		qString = location.href;	
	}
	
	// This query string part is built only if a chained webform is opened from Interface URL
	var partialQS = '';
	if (new_ProspectCreated == true)
	{
		partialQS = '&newpid=' + new_ProspectCreatedId;
	}
	
	var temp = qString.split('?');
	var forwardPath = 'ChainedWebFormQSBuilder.aspx?' + temp[1] + '&newvid=' + new_vid + partialQS;
	
	try
	{
		parent.location.href = forwardPath;
	}
	catch(ex)//handle permission denied
	{
		location.href = forwardPath;
	}
}

function xmlConverter(isNext)
{
    var header = Header();
	var footer = Footer();
	var xmlData;
	var xmlString;
	var xml;
	var targetText;
	var targetIdentifers;
	var field;
	var attribute;
		
	var selectTags	= document.getElementsByTagName("select");

	var inputTags	= document.getElementsByTagName("input");

	var textAreaTag = document.getElementsByTagName("TEXTAREA");
	
	// For Select Elements

	for(j = 0; j < selectTags.length; j++)
	{
		if( selectTags.item(j).attributes["target"] )   // ** if(selectTags.item(j).target)
		{
			targetText			= selectTags.item(j).attributes["target"].value;	// ** text= selectTags.item(j).target;
			targetIdentifers	= targetText.split(".");	
			field				= targetIdentifers[0];	// prospect/webform
			attribute			= targetIdentifers[1];  // firstname/company/country or any custom attribute
		}
		else
		{
			field		= "";
			attribute	= "";
		}

		xmlData = '<record'+
					' id="' + selectTags.item(j).id + '"' + 
					' name="' + selectTags.item(j).name + '"' + 
					' type="' + selectTags.item(j).type + '"' + 
					' value="' + EncodeHtml(selectTags.item(j).options[selectTags.item(j).selectedIndex].text) + '"' + 
					' Field="' + field + '"' + 
					' Attribute="' + attribute.toLowerCase() + '"' + 
					'/>';
		xmlString += xmlData;			
	}

	// For TextArea Elements
		
	for(j = 0; j < textAreaTag.length; j++)
	{
		if( textAreaTag.item(j).attributes["target"] )	// ** if(textAreaTag.item(j).target)
		{
			targetText			= textAreaTag.item(j).attributes["target"].value;	// ** text= textAreaTag.item(j).target;
			targetIdentifers	= targetText.split(".");	
			field				= targetIdentifers[0]; // prospect/webform
			attribute			= targetIdentifers[1]; // firstname/company/country or any custom attribute
		}
		else
		{
			field		= "";
			attribute	= "";
		}

		xmlData = '<record' + 
					' id="' + textAreaTag.item(j).id + '"' + 
					' name="' + textAreaTag.item(j).name + '"' + 
					' type="' + textAreaTag.item(j).type + '"' + 
					' value="' + EncodeHtml(textAreaTag.item(j).value) + '"' + 
					' Field="' + field + '"' + 
					' Attribute="' + attribute.toLowerCase() + '"' + 
					'/>';
		xmlString += xmlData;				
	}

	// For Input Elements
	xmlData = "";
	var inputType;
	
	for(i = 0; i < inputTags.length; i++)
	{
		inputType = inputTags.item(i).type;
		
		if(inputTags.item(i).attributes["target"])        // ** if(inputTags.item(i).target)
		{
			targetText			= inputTags.item(i).attributes["target"].value;	// ** text= inputTags.item(i).target;
			targetIdentifers	= targetText.split(".");	
			field				= targetIdentifers[0]; // prospect/webform
			attribute			= targetIdentifers[1]; // firstname/company/country or any custom attribute
		}
		else
		{
			field		= "";
			attribute	= "";
		}
		
		inputValue = inputTags.item(i).value;

		// For radio button value			
		if(inputType == 'radio')
		{
			var inputid;
			var loop;
			loop = 1;
			inputValue = "";
			try
			{
				inputid = inputTags.item(i).name;
				while ( loop == 1)
				{
					if(inputTags.item(i).checked)
					{
						inputValue = inputTags.item(i).value;
						loop = 0;
						
						try
						{
							while(inputTags.item(i + 1).type == 'radio' && inputTags.item(i + 1).name == inputid)
								i++;
						}
						catch(ex){}
					}
					else
					{
						try
						{
							if (inputTags.item(i + 1).type == 'radio' && inputTags.item(i + 1).name == inputid)
								i++;
							else
								loop = 0;
						}
						catch(ex)
						{
							loop = 0;
						}
					}
				}
			}
			catch(Ex){}
		}

		// For checkbox value

		if(inputType == 'checkbox')
		{
			if(inputTags.item(i).checked)
			{		
				inputValue = "True";
			}
			else
			{
				inputValue = "False";
			}
		}
			
		// Build xml string at here with attributes.
			
		xmlData = '<record' + 
					' id="' + inputTags.item(i).id + '"' + 
					' name="' + inputTags.item(i).name + '"' + 
					' type="' + inputTags.item(i).type + '"' + 
					' value="' + EncodeHtml(inputValue) + '"' + 
					' Field="' + field + '"' + 
					' Attribute="' + attribute.toLowerCase() + '"' + 
					'/>';
		xmlString += xmlData;		
	}
		
	var xml =  header + xmlString + footer;	
	var qString;
	
	try
	{
		qString = parent.location.href;	
	}
	catch(ex)//handle permission denied
	{
		qString = location.href;	
	}
	
	var temp = qString.split('?');
	
	var forwardPath = 'WebFormListener.aspx?' + temp[1] ;
	var errDesc		= sendData(xml, forwardPath);
	
	if(errDesc != '')
	{
		alert('Error: ' + errDesc);
		return false;
	}
	else if (isNext != 1)
	{
		alert("Your Information Has Been Saved.");
		window.close();
	}
	return true;
}

function sendData(xml, forwardPath)
{		
	var browser = window.navigator.appName;
	var agent	= window.navigator.userAgent;
	var result  = '';
	
	if (browser == 'Netscape')
	{
		result = sendData_NS(xml, forwardPath);
	}
	else
	{
		if(agent.indexOf('Opera') > 0)
		{
			result = sendData_NS(xml, forwardPath);
		}
		else
		{
			result = sendData_IE(xml, forwardPath);
		}
	}
	return result;
}	

function sendData_IE(xml, forwardPath)
{
	var objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	objXMLHTTP.Open("POST", forwardPath, false);
	objXMLHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");		
	objXMLHTTP.Send(xml);
	
	var objXMLDOM = new ActiveXObject("Microsoft.XMLDOM");			
	objXMLDOM.loadXML(objXMLHTTP.responseText);			

	var ActionNode = objXMLDOM.getElementsByTagName("action");
	var errDesc	   = '';
		
	if(ActionNode(0).text.toLowerCase() == "insert")
	{
		var IDNode  = objXMLDOM.getElementsByTagName("id");
		id			= IDNode(0).text;
		new_ProspectCreated		= true;
		new_ProspectCreatedId	= id;
	}
	else if(ActionNode(0).text.toLowerCase() == "error")
	{
		errDesc = objXMLDOM.getElementsByTagName("id")(0).text;
	}	
	
	ActionNode	= null;	   
	objXMLDOM	= null;
	objHMLHTTP	= null;

	return errDesc;
}

function sendData_NS(xml, forwardPath)
{
	try
	{
		var oXML = new XMLHttpRequest();
		oXML.open("POST", forwardPath, false);
		oXML.send(xml);
		
		var resText	= oXML.responseText;
		var errDesc = '';
		
		var tempStart = resText.indexOf("<id>");
		var tempEnd	  = resText.indexOf("</id>");
		var tempID	  = resText.substr(tempStart + 4, tempEnd - (tempStart + 4));
		if(resText.indexOf("error") > 0)
		{
			errDesc = tempID;		
		}
		else
		{
			new_ProspectCreated		= true;
			new_ProspectCreatedId	= tempID;
		}
		
		return errDesc;	
	}
	catch(ex)
	{
		return ex;
	}
}

function updateProspectData(fsXML)
{
	var qString;
	
	try
	{
		qString = parent.location.href;	
	}
	catch(ex)//handle permission denied
	{
		qString = location.href;	
	}
	
	var temp = qString.split('?');
	
	var forwardPath = 'WebFormListener.aspx?' + temp[1] ;
	sendData(fsXML, forwardPath);	
}	
	
function Header()	
{
	var head = "<mmm>";
	return head;
}
		
function Footer()
{
	var foot = "</mmm>";
	return foot;
}

function onLoad()	// ** an ordinary function which is assigned to 'onload event' for the window
{
	try
	{
		var inputTags = document.getElementsByTagName("input");
		for(i = 0; i < inputTags.length; i++)
		{
			var objCtrl		= inputTags.item(i);
			var inputType	= objCtrl.type;
			
			if(inputType == "checkbox")
			{
				// if(objCtrl.customValue && objCtrl.customValue == "True")
				if(objCtrl.attributes["customValue"])
				{
				    var customValue = objCtrl.attributes["customValue"].value.toLowerCase();
				    if((customValue == "true") || (customValue == "t") || (customValue == "yes") || (customValue == "y") || (customValue == "1"))
				    {
					    inputTags.item(i).checked = true;
				    }
				    else if ((customValue == "false") || (customValue == "f") || (customValue == "no") || (customValue == "n") || (customValue == "0"))
				    {
				        inputTags.item(i).checked = false;
				    }
				}
			}
			
			if(inputType == "radio")
			{
				// if(objCtrl.customValue && objCtrl.customValue == objCtrl.value)
				if(objCtrl.attributes["customValue"] && objCtrl.attributes["customValue"].value == objCtrl.value)
				{							
					inputTags.item(i).checked = true;							
				}
			}				
		}
	}
	catch(ex){}
}

// ** newly added line of code
window.onload = onLoad;