var _redirect_PC = null;
var _redirect_data = null;
var _redirect_dataItem = null;
function loadCommonAnswersRequest(baseProductCode, targetProductCode, dataItem) {
	if(baseProductCode && targetProductCode && dataItem) {
		var contractdetailsDiv=document.getElementById("contractdetails");
		var callback = loadCommonAnswersCallback;
		if(!PC || targetProductCode != PC || (dataItem.country_code != null && dataItem.country_code != dbcountry)) {
			callback = loadCommonAnswersRedirect;
			_redirect_PC = targetProductCode;
		}
		_redirect_dataItem = dataItem;

		if(baseProductCode != targetProductCode) {
			var postData='baseProductCode='+baseProductCode+'&targetProductCode='+targetProductCode+'&webuserDataDetailID='+dataItem.webuser_data_detail_id;
			httpRequest(postData,'/CommonAnswers/index.asmx/LoadCommonAnswers',contractdetailsDiv,"Loading Answers ...",callback,true);
		}
		else {
			var postData='webuser_data_id='+dataItem.webuser_data_id;
			httpRequest(postData,'/DataService/index.asmx/LoadAnswerVarstringComplete',contractdetailsDiv,"Loading Answers ...",callback,true);
		}
   }
}

function loadCommonAnswersCallback(req,statusInd,success){
	var contractdetailsDiv=document.getElementById("contractdetails");	
	if(contractdetailsDiv && statusInd) contractdetailsDiv.removeChild(statusInd);
	if(success) {
		if(req.responseText.length>0) {
			var respText = req.responseText;
			var xml = str2XML(respText);
			if(xml.normalize)
				xml.normalize();
			var data = xml.getElementsByTagName("string")[0].firstChild;
			if(data) {
				postVariableString(top.location.href, trimString(data.nodeValue));
				hidePopWin(false);
				return;
			}
		}
	}
	else {
      alert("We are sorry, there was a problem loading your information from our server.\nPlease try refreshing the page.");
	}
	hidePopWin(false);
}

function loadCommonAnswersRedirect(req,statusInd,success){
	var contractdetailsDiv=document.getElementById("contractdetails");	
	if(contractdetailsDiv && statusInd) contractdetailsDiv.removeChild(statusInd);
	if(success) {
		if(req.responseText.length>0) {
			var respText = req.responseText;
			var xml = str2XML(respText);
			if(xml.normalize)
				xml.normalize();
			var data = xml.getElementsByTagName("string")[0].firstChild;
			if(data && _redirect_PC) {
				_redirect_data = trimString(data.nodeValue);
				var loc = _redirect_dataItem.country_code;
				if(_redirect_PC == 'DIVRCE' && _redirect_dataItem.other)
					loc += _redirect_dataItem.other;
				postVariableString('/contracts/'+_redirect_dataItem.contract_url+'/'+(_redirect_PC == 'DIVRCE' ? 'index.aspx' : '')+'?loc='+loc, _redirect_data);
				hidePopWin(false);
				return;
			}
		}
	}
	else {
      alert("We are sorry, there was a problem loading your information from our server.\nPlease try refreshing the page.");
	}
	hidePopWin(false);
}

function loadCommonAnswersRedirectCallback(req,statusInd,success){
	var contractdetailsDiv=document.getElementById("contractdetails");	
	if(contractdetailsDiv && statusInd) contractdetailsDiv.removeChild(statusInd);
	if(success) {
		if(req.responseText.length>0) {
			var respText = req.responseText;
			var xml = str2XML(respText);
			if(xml.normalize)
				xml.normalize();
			var ci = xml.getElementsByTagName("string")[0].firstChild;
			if(ci && _redirect_data) {
				var loc = _redirect_dataItem.country_code;
				if(_redirect_PC == 'DIVRCE' && _redirect_dataItem.other)
					loc += _redirect_dataItem.other;
				postVariableString('/contracts/'+trimString(ci.nodeValue)+'/'+(_redirect_PC == 'DIVRCE' ? 'index.aspx' : '')+'?loc='+loc, _redirect_data);
				hidePopWin(false);
				return;
			}
		}
	}
	else {
      alert("We are sorry, there was a problem loading your information from our server.\nPlease try refreshing the page.");
	}
	hidePopWin(false);
}

function postVariableString(url, vs) {
	if(url.substring(0,4) != 'http') {
		url = 'http://'+top.location.host+(url.substring(0,1) != '/' ? '/' : '')+url;
	}

	var form = document.createElement('form');
	form.setAttribute('method', 'post');
	form.setAttribute('action', url);

	var webuser_data_id = document.createElement('input');
	webuser_data_id.setAttribute('name', 'webuser_data_id');
	webuser_data_id.setAttribute('type', 'hidden');
	webuser_data_id.setAttribute('value', _redirect_dataItem.webuser_data_id);
	form.appendChild(webuser_data_id);

	var wdid = document.createElement('input');
	wdid.setAttribute('name', 'wdid');
	wdid.setAttribute('type', 'hidden');
	wdid.setAttribute('value', _redirect_dataItem.webuser_data_id);
	form.appendChild(wdid);

	var data_type = document.createElement('input');
	data_type.setAttribute('name', 'dataType');
	data_type.setAttribute('type', 'hidden');
	data_type.setAttribute('value', _redirect_dataItem.data_type);
	form.appendChild(data_type);

	var invoice_no = document.createElement('input');
	invoice_no.setAttribute('name', 'invoiceNo');
	invoice_no.setAttribute('type', 'hidden');
	invoice_no.setAttribute('value', _redirect_dataItem.invoice_no);
	form.appendChild(invoice_no);

	var varstring = document.createElement('input');
	varstring.setAttribute('name', 'variablestring');
	varstring.setAttribute('type', 'hidden');
	varstring.setAttribute('value', vs.replace(/\&amp;/g, '&'));
	form.appendChild(varstring);
	
	var secure_id = document.createElement('input');
	secure_id.setAttribute('name', 'SID');
	secure_id.setAttribute('type', 'hidden');
	secure_id.setAttribute('value', SID);
	form.appendChild(secure_id);

	document.body.appendChild(form);
	setLocationCookieBeforePost();
	form.submit();
}

function setLocationCookieBeforePost() {
	var x,y,ARRcookies=document.cookie.split(";"),curLoc;
	for (var i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x=='L')
		{
			curLoc = unescape(y);
		}
	}
	
	if(curLoc.substring(0,2) != _redirect_dataItem.country_code) {
		var loc = _redirect_dataItem.country_code;
		if(loc && _redirect_dataItem.other)
			loc += _redirect_dataItem.other;
		if(loc) {
			var exdate=new Date();
			exdate.setDate(exdate.getDate() + 365);
			var c_value=escape(loc) + "; expires="+exdate.toUTCString() + "; path=/";
			document.cookie="L=" + c_value;
		}
	}
}

