/**************************************************************************************************************
	stefano guagnini
	24/6/2005
 **************************************************************************************************************
 
	Interfacciamento con CCM, Object CCM

 **************************************************************************************************************/

function _CCM()
{
	//	public methods

	this.ReadData = _CCM_ReadData;
	this.NotifyActionToDS = _CCM_NotifyActionToDS;
	this.NotifyEndFunction = _CCM_NotifyEndFunction;
	this.IsCCMOpen = _CCM_IsCCMOpen;
	this.OpenCCM = _CCM_OpenCCM;
	this.CloseCCM = _CCM_CloseCCM;
	this.FocusToCCM = _CCM_FocusToCCM;
	this.CheckConnectionSpeed = _CCM_CheckConnectionSpeed;
	this.ValidateStagingArea = _CCM_ValidateStagingArea;
	this.IsCCMAvailable = _CCM_IsCCMAvailable;
	
	//	private methods
	this.CreateXml = _CCM_CreateXml;
	this.WriteData = _CCM_WriteData;
	this.VerifyGrant = _CCM_VerifyGrant;
	this.VerifyNBA = _CCM_VerifyNBA;
	
	//	private propeties
	this.timer = null;
	this.wndCCM = null;
	this.MaxCCMAttempt = 5
	this.CCMAttempt = 0;
	this.DefaultTimeout = 5;
	
	return this;
}

//*************************************************************************************************************
function _CCM_ReadData()
{
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		if ( confirm("CCM.ReadData: return any data ???" ) )
		{
			retobj = new Object();
			retobj.QueryString = _CCMDATA_QueryString;
			retobj.SuperActivityId = "PAV";
			retobj.MsisdnNum = "3471234567";
			return retobj;
		}
		else return null;
	}

	var pObj, xmlstr;
	try
	{
		pObj = new ActiveXObject("KERNELXP.WebKernel");
		xmlstr = pObj.ReadFromPos("D");
		debug( "CCM.ReadData.xml = " + xmlstr );
	}
	catch(e)
	{
		debug( "CCM.ReadData Exception: " + e.description );
		return null;
		
	}
	
	var xmlobj;
	try
	{
		xmlobj = new ActiveXObject('Microsoft.XMLDOM');
		xmlobj.loadXML(xmlstr);
	}
	catch(e)
	{
		debug( "CCM.ReadData Exception: " + e.description );
		return null;
	}

	var retobj = new Object();
	retobj.QueryString = _CCMDATA_QueryString;
	try{retobj.MsisdnNum = xmlobj.selectSingleNode("//MsisdnNum").text;}catch(e){debug("CCM.ReadData Exception: Msisdn not found");retobj.MsisdnNum="";}
	try{retobj.SuperActivityId = xmlobj.selectSingleNode("//SuperActivityId").text;}catch(e){debug("CCM.ReadData Exception: SuperActivityId not found");retobj.SuperActivityId="";}
	try{retobj.ActivityId = xmlobj.selectSingleNode("//ActivityId").text;}catch(e){debug("CCM.ReadData Exception: ActivityId not found");retobj.ActivityId="";}
	try{retobj.Sn_Code = xmlobj.selectSingleNode("//Sn_Code").text;}catch(e){debug("CCM.ReadData Exception: Sn_Code not found");retobj.Sn_Code="";}
	try{retobj.Promo_Id = xmlobj.selectSingleNode("//Promo_Id").text;}catch(e){debug("CCM.ReadData Exception: Promo_Id not found");retobj.Promo_Id="";}
	try{retobj.CustomerType = xmlobj.selectSingleNode("//TypeCustomer").text;}catch(e){debug("CCM.ReadData Exception: TypeCustomer not found");retobj.CustomerType="";}
	try{retobj.FiscalData = xmlobj.selectSingleNode("//FiscalData").text;}catch(e){debug("CCM.ReadData Exception: FiscalData not found");retobj.FiscalData="";}
	try{retobj.CheckMNP = xmlobj.selectSingleNode("//CheckMNP").text;}catch(e){debug("CCM.ReadData Exception: CheckMNP not found");retobj.CheckMNP="";}
	try{retobj.Context_Id = xmlobj.selectSingleNode("//Context_Id").text;}catch(e){debug("CCM.ReadData Exception: Context_Id not found");retobj.Context_Id="";}

	if ( retobj.CustomerType == "CF")
		retobj.CustomerType  = "P";
	else if ( retobj.CustomerType == "PI")
		retobj.CustomerType = "I";

	debug( "CCM.ReadData values" );
	debug( "  MsisdnNum = " + retobj.MsisdnNum );
	debug( "  CustomerType = " + retobj.CustomerType );
	debug( "  SuperActivityId = " + retobj.SuperActivityId  );
	debug( "  ActivityId = " +  retobj.ActivityId );
	debug( "  Sn_Code = " +  retobj.Sn_Code );
	debug( "  Promo_Id = " +  retobj.Promo_Id  );
	debug( "  FiscalData = " + retobj.FiscalData );
	debug( "  CheckMNP = " + retobj.CheckMNP );
	debug( "  Context_Id = " + retobj.Context_Id );

	if ( retobj.MsisdnNum == "" )
	{
		debug( "CCM.ReadData returning null (no msisdn)" );
		return null;
	}
	else return retobj;
}

function _CCMDATA_QueryString()
{
	try
	{
		var qs = "?MODULE_CALLER=" +
		"&MSISDN_NUM="	+ this.MsisdnNum + 
		"&ACTIVITY_ID=" + this.ActivityId +
		"&CONTEXT_ID="	+ this.Context_Id +
		"&FISCALDATA="	+ this.FiscalData +
		"&CHECKMNP="	+ this.CheckMNP +
		(this.CustomerType==""?"":"&CUSTTYPE="+this.CustomerType) + 
		"&";

		debug( "CCM.QueryString = " + qs );
		return qs;
	}
	catch(e)
	{
		debug( "CCMDATA_QueryString.Exception: " + e.description );
		return "";
	}
}

//*************************************************************************************************************

function _CCM_WriteData(xmlstr,validate)
{
	var ret;
	try
	{
		debug( "CCM.WriteData:" );
		debug( "   xml = " + xmlstr );
		debug( "   validate = " + validate );
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		ret = pObj.WriteToPos( "D", xmlstr, validate );
	}
	catch(e)
	{
		debug( "CCM.WriteData exception: " + e.description );
		ret = 100;
	}
	debug( "CCM.WriteData retcode: " + ret );
	return ret;
}

//*************************************************************************************************************

function _CCM_NotifyActionToDS(xmlstr)
{
	var ret;
	try
	{
		debug( "CCM.NotifyActionToDS(" + xmlstr + ")" );
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		ret = pObj.NotifyActionToDS( "C", xmlstr );
	}
	catch(e)
	{
		debug( "CCM.NotifyActionToDS exception: " + e.description );
		ret = 100;
	}
	debug( "CCM.NotifyActionToDS retcode: " + ret );
	return ret;
}

//*************************************************************************************************************

function _CCM_CreateXml(MsisdnNum,SuperActivityId,ActivityId,Sn_Code,Promo_Id,TypeTech,FiscalData,CheckMNP)
{
	try
	{
		var xmlstr =	"<XML>"+
							"<MsisdnNum>"+(MsisdnNum==null?"":MsisdnNum)+"</MsisdnNum>"+
							"<SuperActivityId>"+(SuperActivityId==null?"":SuperActivityId)+"</SuperActivityId>"+
							"<ActivityId>"+(ActivityId==null?"":ActivityId)+"</ActivityId>"+
							"<Sn_Code>"+(Sn_Code==null?"":Sn_Code)+"</Sn_Code>"+
							"<Promo_Id>"+(Promo_Id==null?"":Promo_Id)+"</Promo_Id>"+
							"<TypeTechnology>"+(TypeTech==null?"":TypeTech)+"</TypeTechnology>"+
							"<FiscalData>"+(FiscalData==null?"":FiscalData)+"</FiscalData>"+
							"<CheckMNP>"+(CheckMNP==null?"":CheckMNP)+"</CheckMNP>"+
							"<DealerCode>"+Config.DealerCode()+"</DealerCode>"+
						"</XML>";
										
		return xmlstr;
	}
	catch(e)
	{
		debug( "CCM.CreateXml.Exception: " + e.description );
		return "";
	}
}

//*************************************************************************************************************/

function _CCM_VerifyGrant()
{
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		alert("CCM.VerifyGrant: test mode" );
		return true;
	}
	else if ( Config.IsCCMUser() )
	{
		debug( "CCM.VerifyGrant: OK" );
		return true;
	}
	else
	{
		debug( "CCM.VerifyGrant: KO" );
		return false;
	}
}

//*************************************************************************************************************/

function _CCM_VerifyNBA(xmlstr)
{
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		alert("CCM.VerifyNBA: test mode" );
		return true;
	}
	
	try{
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		var ret = pObj.VerifyNBA(xmlstr);
		debug( "CCM.VerifyNBA = " + ret );
		return ret == "Y";
	}
	catch(e){
		debug( "CCM.VerifyNBA Exception= " + e.description );
		return false;
	}
}

//*************************************************************************************************************/

function _CCM_IsCCMOpen()
{
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		return confirm("CCM.IsCCMOpen: test mode.\nreturn true ???" );
	}
	
	try{
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		var ret = pObj.CheckCCMonPOS();
		debug( "CCM.IsCCMOpen = " + ret );
		return ret == "Y";
	}
	catch(e){
		debug( "CCM.IsCCMOpen Exception= " + e.description );
		return false;
	}
}

//*************************************************************************************************************/

function _CCM_OpenCCM(szUrl)
{
	debug( "CCM.OpenCCM.begin" );
	
	try
	{
		if ( this.wndCCM == null || this.wndCCM.closed )
		{
			debug( "CCM.OpenCCM: puntatore a CCM indica chiusa" );
			if ( !this.IsCCMOpen() )	
			{
				debug( "CCM.OpenCCM: osal indica CCM chiusa" );
				if ( this.CheckConnectionSpeed() )	
				{
					debug("CCM.OpenCCM: banda ok");
					
					if ( this.IsCCMAvailable() )
					{
						if ( this.CCMAttempt < this.MaxCCMAttempt )
						{
							this.CCMAttempt++;
							this.wndCCM = window.open( szUrl, "", "status=1,location=0,menubar=0,resizable=1,scrollbars=1,toolbar=0" );
						}
						else
						{
							debug( "CCM.OpenCCM: troppi fallimenti("+this.CCMAttempt+"). Non apro pagina" );
							return false;
						}
					}
					else
					{
						debug("CCM.OpenCCM: ccm non risponde, NON apro ccm...");
						return false;
					} 
				}
				else
				{
					debug("CCM.OpenCCM: banda ko, NON apro ccm...");
					return false;
				}
			}
			else
			{
				debug( "CCM risulta aperta." );
				this.CCMAttempt = 0;
			}
		}
		return true;
	}
	catch(e)
	{
		debug( "CCM.OpenCCM.Exception: " + e.description );
		return false;
	}
	
}

//*************************************************************************************************************/

function _CCM_CloseCCM()
{
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		return true;
	}
	try{
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		var ret = pObj.CloseCCM();
		debug( "CCM.CloseCCM = " + ret );
		return ret == 0;
	}
	catch(e){
		debug( "CCM.CloseCCM.Exception= " + e.description );
		return false;
	}
}

//*************************************************************************************************************/

function _CCM_NotifyEndFunction(MsisdnNum, SuperActivityId, ActivityId, Sn_Code, Promo_Id, TypeTech, DelaySec, CheckNBA)
{
	if ( DelaySec == "" || DelaySec == null )
		DelaySec = this.DefaultTimeout;
		
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		alert("CCM.NotifyEndFunction: test mode\nFocus to CCM in 5 sec." );
		this.FocusToCCM(DelaySec);
		return;
	}
	
	debug("CCM.NotifyEndFunction.Begin");
	try
	{
		if ( this.VerifyGrant() )
		{
			var xmlstr = this.CreateXml(MsisdnNum, SuperActivityId, ActivityId, Sn_Code, Promo_Id, TypeTech);
			var runccm = false;
			var validate = "N";

			debug("CCM.NotifyEndFunction.xmlstr = " + xmlstr);
			debug("CCM.NotifyEndFunction.CheckNBA = " + CheckNBA);

			if ( CheckNBA == true || CheckNBA == "Y" )
			{
				if ( this.VerifyNBA(xmlstr) )
				{
					if ( this.IsCCMOpen() )
						validate = "Y";
					runccm = true;
				}

				debug("CCM.NotifyEndFunction.validate = " + validate);
				debug("CCM.NotifyEndFunction.runccm = " + runccm);
			}

			this.WriteData(xmlstr,validate);
			
			if ( runccm )
				this.FocusToCCM( DelaySec * 1000 );
		}
	}
	catch(e)
	{
		debug( "CCM.NotifyEndFunction.Exception: " + e.description );
	}
	debug("CCM.NotifyEndFunction.End");
}

//*************************************************************************************************************/

function _CCM_IsCCMAvailable()
{
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		return true;
	}
	try{
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		var ret = pObj.TestCCMLink();
		debug( "CCM.IsCCMAvailable = " + ret );
		return ret == "Y";
	}
	catch(e){
		debug( "CCM.IsCCMAvailable.Exception= " + e.description );
		return false;
	}
}

//*************************************************************************************************************/

function _CCM_FocusToCCM(timer,bSilent)
{
	if ( bSilent == null )
		bSilent = false;
	
	debug( "CCM.FocusToCCM(" + timer + "," + bSilent + ")" );
	if ( !this.CheckConnectionSpeed() )
		return;
	
	if ( timer > 0 )
	{
		debug( "CCM.FocusToCCM.setTimeout: " + timer );
		this.timer = setTimeout( "CCM.FocusToCCM(0)", timer );
		return;
	}

	if ( this.timer != null )
	{
		clearTimeout(this.timer);
		this.timer = null;
	}

	if ( !bSilent && Config.CCMFilter() )
	{
		debug("CCM_FocusToCCM.CCMFilter active");
		if ( !confirm(DSLIB[324]) )
			return;
	}

	try{
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		var ret = pObj.FocusToCCMonPos();
		debug( "CCM.FocusToCCM.FocusToCCMonPos: " + ret );
	}
	catch(e){
		debug( "CCM.FocusToCCM Exception: " + e.description );
	}
}

//*************************************************************************************************************/

function _CCM_ValidateStagingArea()
{
	try
	{
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		var ret = pObj.UpdateSAValidation();
		debug( "CCM.ValidateStagingArea: " + ret );
	}
	catch(e)
	{
		debug("ValidateStagingArea.exception: " + e.description );
	}
}

function _CCM_CheckConnectionSpeed()
{
	if ( window.ModuleInfoIsSimulation == "true" )
	{
		alert("CCM.CheckConnectionSpeed: test mode" );
		return true;
	}
	
	try
	{
		var pObj = new ActiveXObject("KERNELXP.WebKernel");
		var ret = pObj.VerifyConnection();
		debug( "CCM.CheckConnectionSpeed: " + ret );
		return ret == "H";
	}
	catch(e)
	{
		debug("CheckConnectionSpeed.exception: " + e.description );
		return false;
	}
}

//*************************************************************************************************************/

var CCM = new _CCM();


















