// Type definitions
CMNSTATEDEF = function()
{
    this.IDLE         = 0;    // idle state entered
    this.CALLA        = 1;    // calling A entered
    this.CALLB        = 2;    // calling B entered
    this.ABUSY        = 3;
    this.ANOANS       = 4;
    this.CONN         = 5;
    this.BBUSY        = 6;
    this.BNOANS       = 7;    // no-answer state
    this.DISCONN      = 8;    // diconnected state entered
    this.ERROR        = 9;    // error occured, err message displayed

    this.INFO         = 10;   // info message displayed
    this.INFOBLINK    = 11;   // info message blinking displayed
    this.NONUM        = 12;   // message no number displayed
    this.INVALNUM     = 13;   // message invalid number displayed
    this.NOSERVNUM    = 14;   // message no serv. nr. allowed displayed
    this.NO06NUM      = 15;   // message no 06 number allowed displayed
    this.NOMSG        = 16;   // current message is removed    
}
var CMNSTATE = new CMNSTATEDEF();

// CMN type ctor
CMN = function(userFunc)
{
    // public members (set/get accessor funtions)
   	this._imageIdle = "";
	this._imageCallingA = "";
	this._imageCallingB = "";
	this._imageABusy = "";        
	this._imageANoAnswer = "";
	this._imageABConnected = "";
	this._imageBBusy = "";        
	this._imageBNoAnswer = "";
	this._imageABDisConnected = "";
	this._imageError = "";     
	
	this._imageInfo = "";  
	this._imageInfoBlinking = "";
	
    this._msgValNoNumber = "";
	this._msgValInvalidNumber = "";
	this._msgValNoServiceNumber = "";
	this._msgValNo06Number = "";
	this._msgCallingA = "";
	this._msgCallingB = "";
	this._msgABusy = "";
	this._msgANoAnswer = "";
	this._msgABConnected = "";
	this._msgBBusy = "";
	this._msgBNoAnswer = "";
	this._msgABDisConnected = "";
	this._msgError = "";
	
	this._msgInfoYourNumber = "";
	this._msgInfoBBusy = "";
	this._msgInfoBNoAnswer = "";
	this._msgInfoTitle = "";
	this._msgInfoError = "";
	this._msgInfoTitleText = "";
	this._msgInfoText = "";
	
	this._waterMarkTextTel = "Uw nummer";

    // private members (no accessor functions, use in 'non-public' code)
    this._isWorking = false;
	this._bCheck06 = false;
	this._regId = "";
	this._url = "";
	this._baseurl = "";
	this._urlCMNStepStartCall = "";
	this._urlCMNStepRetryCall = "";
	this._urlCMNStepEndCall = "";
	this._urlCMNStepCallState = "";
	this._urlCMNPhoneNumberB = "";
	this._previousState = 0;
	this._counter = 0;
	this._CHECKCALLSTATE_DELAY = 1000;
	this._connectedState = 510;
	this._statusImage = "";
	this._renderStyle = "";
	this._frontEndUrl = "";
	this._phoneNumberB = "";
	this._initButtonToggle = 1;
	this._callEnded = 0;
	this._callRetry = 0;
	
	this._userInit = userFunc;
	this._stateChangedHandler = null;
	this._knownElements = new Array();
	this._inService = true;
	
	// initialize object
	this.init();
}

// initializer
CMN.prototype.init = function()
{
    // function to initialize customerspecific stuff
    this.getDefaultState();

    // statemachine querystring verbs    
    this._urlCMNStepStartCall = '&CMN_Step=StartCall';
    this._urlCMNStepRetryCall = '&CMN_Step=RetryCall';
    this._urlCMNStepEndCall = '&CMN_Step=EndCall';
    this._urlCMNStepCallState = '&CMN_Step=CallState';
    this._urlCMNPhoneNumberB = '&PhoneNumberB=';
    
    // default messages
    this._msgCallingA = 'Bezig verbinding te maken';
    this._msgCallingB = 'U wordt nu gebeld';
    this._msgABusy = 'Al onze medewerkers zijn in gesprek';
    this._msgANoAnswer = 'Geen vrije medewerker gevonden';
    this._msgABConnected = 'Verbonden';
    this._msgBBusy = 'Uw toestel is bezet';
    this._msgBNoAnswer = 'Wij kunnen u niet bereiken';
    this._msgABDisConnected = 'De verbinding is verbroken';
    this._msgError = 'Geen verbinding';
    this._msgValNoNumber = 'Geen nummer ingevuld';
    this._msgValInvalidNumber = 'Ongeldig nummer ingevuld';
    this._msgValNoServiceNumber = 'Geen servicenummers toegestaan';
    this._msgValNo06Number = '06 nummers zijn niet toegestaan';
    this._msgInfoYourNumber = 'Vul hier uw eigen telefoonnummer in en klik op de "BEL MIJ" knop. U wordt dan direct door ons gebeld.';
    this._msgInfoBBusy = 'Maakt u gebruik van een inbellijn? Klik dan op de opnieuw-knop en verbreek daarna direct uw internet-connectie';
    this._msgInfoBNoAnswer = 'Er wordt niet opgenomen op het door u ingevoerde nummer. Controleer het nummer aub.';
    this._msgInfoTitle = 'Informatie';
    this._msgInfoError = 'Heeft u het juiste nummer ingevuld?';
    this._msgInfoTitleText = this._msgInfoTitle;
    this._msgInfoText = this._msgInfoYourNumber;
    this._msgInfoConnecting = 'Bezig verbinding tot stand te brengen';
    this._msgInfoConnected = 'De verbinding is actief';
	
    // create obligatory status image
    this._statusImage = new Image();
    this._statusImage.id = 'CMN_StsImg';
    //    this._statusImage.style.display = "none";
    this._statusImage.style.visibility = "hidden";
    this._statusImage.onload = function() { handleResponse(); }  
    
    // create list of elements that are defined ('known') in document
    this.addKnownElement('CMN_Images');
    this.addKnownElement('CMN_PhoneNumber');
    this.addKnownElement('CMN_ButtonClear');
    this.addKnownElement('CMN_ButtonRetry');
    this.addKnownElement('CMN_Button');
    this.addKnownElement('CMN_MessageArea');
    this.addKnownElement('CMN_Info');
    this.addKnownElement('CMN_InfoHeader');
    this.addKnownElement('CMN_InfoText');
    this.addKnownElement('CMN_InfoImage');
    this.addKnownElement('CMN_InfoPopup');
    this.addKnownElement('CMN_InfoContainer');
    
    // attach obligatory events
    if (this.isKnownElement('CMN_PhoneNumber'))
    {
        this.getElement('CMN_PhoneNumber').onblur = function() { FillTextBox(this); }
        this.getElement('CMN_PhoneNumber').onfocus = function() { ClearTextBox(this); }
    }
    if (this.isKnownElement('CMN_ButtonRetry'))
    {
        this.getElement('CMN_ButtonRetry').onclick = function() { return retryCall(); }
        this.getElement('CMN_ButtonRetry').style.display = 'none';
    }
    if (this.isKnownElement('CMN_ButtonClear'))
    {
        this.getElement('CMN_ButtonClear').onclick = function() { cancelCall(); return resetInitForm(); }
        this.getElement('CMN_ButtonClear').style.display = 'none';
    }
    if (this.isKnownElement('CMN_Button'))
        this.getElement('CMN_Button').onclick = function() { return checkPhonenumber(); }
       
    // run user defined initialization, if initialization function is provided
    if (typeof(this._userInit) == 'function')
        this._userInit(this);
        
    // some default intializations of container elements
    if (this.isKnownElement('CMN_PhoneNumber'))
        this.getElement('CMN_PhoneNumber').value = this._waterMarkTextTel;

    if (this.isKnownElement('CMN_Images'))
        this.getElement('CMN_Images').innerHTML = this._imageIdle;
    if (this.isKnownElement('CMN_Info'))
    {
	    this.getElement('CMN_Info').innerHTML = this._imageInfo;
    }
    if (this.isKnownElement('CMN_InfoContainer'))
    {
		// default infoPopup
	    this.getElement('CMN_InfoContainer').innerHTML = getInfoPopupContent();
    }
    
    // initial event
    this.FireStateChangedEvent(CMNSTATE.IDLE);
}

///
/// PRIVATE METHODS
///

// Add user defined element to array of known elements by id
CMN.prototype.addKnownElement = function(id)
{
    this._knownElements[id] = (this.getElement(id) != null ? true : false);
}

// Determine whether or not an element is known in this page
// (mainly to prevent assigning values to unknown/undefined page elements)
CMN.prototype.isKnownElement = function(id)
{
    // if not in array at all, return false
    if (typeof(this._knownElements[id]) == 'undefined')
        return false;
    return this._knownElements[id];
}

CMN.prototype.getImgMarkup = function(url)
{
	var img = new Image();
	if (!img.outerHTML || typeof(img.outerHTML) == 'undefined')
	{
	    var s = '<img src="'+url+'"/>';
	    return s;
	}
	else // ie style
	{
	    img.src = url;
	    return img.outerHTML;
	}
}

// abbr. for document.getElementById 
CMN.prototype.getElement = function(id)
{
    if (id)
        return document.getElementById(id);
    return null;        
}

CMN.prototype.FireStateChangedEvent = function(evt_id)
{
    if (typeof(this._stateChangedHandler) == 'function')
        this._stateChangedHandler(evt_id);
}

CMN.prototype.CMNInfo_outerHTML = function (element) 
{
   var attrs = element.attributes;
   var str = "<" + element.tagName;
   for (var i = 0; i < attrs.length; i++)
      str += " " + attrs[i].name + "=\"" + attrs[i].value + "\"";
      
   // add events: NOTE not generic
    str += " " + 'onmouseover="ShowTooltip(event, oCMN_V09$1309._msgInfoTitleText, oCMN_V09$1309._msgInfoText);" onmouseout="HideTooltip();"';

   //if (_emptyTags[this.tagName])
   //   return str + ">";
      return str + "/>";

   //return str + ">" + this.innerHTML + "</" + this.tagName + ">";
}



///
/// PUBLIC METHODS
///
CMN.prototype.RegisterStateChangedHandler = function(handleFunc)
{
    if (typeof(handleFunc) == 'function')
        this._stateChangedHandler = handleFunc;
}

// Boolean method to determine if service is active
CMN.prototype.isInService = function()
{
    return this._inService;
}

// obsolete
CMN.prototype.registerRef = function(ref)
{
	oCMN_V09$1309 = ref;
}
///
/// PROPERTY get/set accessors
///
CMN.prototype.set_imageIdle = function(url)
{
    this._imageIdle = this.getImgMarkup(url);
}
CMN.prototype.set_imageCallingA = function(url)
{
    this._imageCallingA = this.getImgMarkup(url);
}
CMN.prototype.set_imageCallingB = function(url)
{
    this._imageCallingB = this.getImgMarkup(url);
}
CMN.prototype.set_imageABusy = function(url)
{
    this._imageABusy = this.getImgMarkup(url);
}
CMN.prototype.set_imageABConnected = function(url)
{
    this._imageABConnected = this.getImgMarkup(url);
}
CMN.prototype.set_imageBBusy = function(url)
{
    this._imageBBusy = this.getImgMarkup(url);
}
CMN.prototype.set_imageBNoAnswer = function(url)
{
    this._imageBNoAnswer = this.getImgMarkup(url);
}
CMN.prototype.set_imageABDisConnected = function(url)
{
    this._imageABDisConnected = this.getImgMarkup(url);
}
CMN.prototype.set_imageError = function(url)
{
    this._imageError = this.getImgMarkup(url);
}

CMN.prototype.set_imageInfo = function(url)
{
	var img = new Image();
	img.id = 'CMN_InfoImage';
	img.src = url;
	img.style.position = 'static';
	img.onmouseover = 'ShowTooltip(event, oCMN_V09$1309._msgInfoTitleText, oCMN_V09$1309._msgInfoText);';
	img.onmouseout = 'HideTooltip();';

    // FF doesn't support outerHTML; IE does
    if (!img.outerHTML || typeof(img.outerHTML) == 'undefined')
        this._imageInfo = this.CMNInfo_outerHTML(img);
    else
        this._imageInfo = img.outerHTML;
}
CMN.prototype.set_imageInfoBlinking = function(url)
{
	var img = new Image();
	img.id = 'CMN_InfoImage';
	img.src = url;
	img.style.position = 'static';
	img.onmouseover = 'ShowTooltip(event, oCMN_V09$1309._msgInfoTitleText, oCMN_V09$1309._msgInfoText);'; 
	img.onmouseout = 'HideTooltip();';
	
    if (!img.outerHTML || typeof(img.outerHTML) == 'undefined')
        this._imageInfoBlinking = this.CMNInfo_outerHTML(img);
    else
        this._imageInfoBlinking = img.outerHTML;
}

CMN.prototype.set_msgValNoNumber = function(s)
{
    this._msgValNoNumber = s;
}
CMN.prototype.set_msgValInvalidNumber = function(s)
{
    this._msgValInvalidNumber = s;
}
CMN.prototype.set_msgValNoServiceNumber = function(s)
{
    this._msgValNoServiceNumber = s;
}
CMN.prototype.set_msgValNo06Number = function(s)
{
    this._msgValNo06Number = s;
}
CMN.prototype.set_msgCallingA = function(s)
{
    this._msgCallingA = s;
}
CMN.prototype.set_msgCallingB = function(s)
{
    this._msgCallingB = s;
}
CMN.prototype.set_msgABusy = function(s)
{
    this._msgABusy = s;
}
CMN.prototype.set_msgANoAnswer = function(s)
{
    this._msgANoAnswer = s;
}
CMN.prototype.set_msgABConnected = function(s)
{
    this._msgABConnected = s;
}
CMN.prototype.set_msgBBusy = function(s)
{
    this._msgBBusy = s;
}
CMN.prototype.set_msgBNoAnswer = function(s)
{
    this._msgBNoAnswer = s;
}
CMN.prototype.set_msgABDisConnected = function(s)
{
    this._msgABDisConnected = s;
}
CMN.prototype.set_msgError = function(s)
{
    this._msgError = s;
}

CMN.prototype.set_msgInfoYourNumber = function(s)
{
    this._msgInfoYourNumber = s;
    this._msgInfoText = this._msgInfoYourNumber;
}
CMN.prototype.set_msgInfoBBusy = function(s)
{
    this._msgInfoBBusy = s;
}
CMN.prototype.set_msgInfoBNoAnswer = function(s)
{
    this._msgInfoBNoAnswer = s;
}
CMN.prototype.set_msgInfoTitle = function(s)
{
    this._msgInfoTitle = s;
    this._msgInfoTitleText = this._msgInfoTitle;
}
CMN.prototype.set_msgInfoError = function(s)
{
    this._msgInfoError = s;
}
CMN.prototype.set_msgInfoTitleText = function(s)
{
    this._msgInfoTitleText = s;
}
CMN.prototype.set_msgInfoText = function(s)
{
    this._msgInfoText = s;
}
CMN.prototype.get_msgInfoText = function()
{
    return this._msgInfoText;
}

CMN.prototype.set_waterMarkTextTel = function(s)
{
    this._waterMarkTextTel = s;
}

/// 
/// LIST OF HELPER FUNCTIONS
///
//==================================================
function checkPhonenumber() 
//==================================================
{
	// html element
	var htmlphonenr = oCMN_V09$1309.getElement('CMN_PhoneNumber');
	if (htmlphonenr == null)
	    return false;

	oCMN_V09$1309._phoneNumberB = htmlphonenr.value;

	if (oCMN_V09$1309._phoneNumberB.length == 0 || oCMN_V09$1309._phoneNumberB == oCMN_V09$1309._waterMarkTextTel) 
	{
		setMessage(oCMN_V09$1309._msgValNoNumber);
		oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NONUM);
		htmlphonenr.focus();        
		return false ;
	} 

	oCMN_V09$1309._phoneNumberB = oCMN_V09$1309._phoneNumberB.replace('-','');
	oCMN_V09$1309._phoneNumberB = oCMN_V09$1309._phoneNumberB.replace(' ','');
	var reg = new RegExp("[0]{1}[0-9]{9}$");
	if (!reg.test(oCMN_V09$1309._phoneNumberB)) 
	{    
		setMessage(oCMN_V09$1309._msgValInvalidNumber);
		oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.INVALNUM);
		htmlphonenr.focus();
		return false ;    
	}
	else if (oCMN_V09$1309._phoneNumberB.charAt(1) == 8)
	{
		setMessage(oCMN_V09$1309._msgValNoServiceNumber);
        oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NOSERVNUM);
		htmlphonenr.focus();
		return false ;
	} 
	else if (oCMN_V09$1309._phoneNumberB.charAt(1) == 9)
	{
		setMessage(oCMN_V09$1309._msgValNoServiceNumber);
        oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NOSERVNUM);
		htmlphonenr.focus();
		return false ;
	} 
	else if ((oCMN_V09$1309._phoneNumberB.charAt(1) == 6) && oCMN_V09$1309._bCheck06)
	{
		setMessage(oCMN_V09$1309._msgValNo06Number);
        oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NO06NUM);
		htmlphonenr.focus();
		return false ;
	}
	else 
	{
		setMessage('');
        oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NOMSG);
		initAndStart();
		return false;
	}
}



//==================================================
function handleResponse() 
//==================================================
{
	state = oCMN_V09$1309._statusImage.width;
	
	// new state mapped to connectedState (510)
	if (state == 14)
	{
		oCMN_V09$1309._CHECKCALLSTATE_DELAY = 5000;
	}

	if (state != oCMN_V09$1309._previousState) 
	{
		if (state == 16)
		{
			// idle
			setMessage('');
            oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NOMSG);
			oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.IDLE);
			oCMN_V09$1309._callEnded = 1;
			resetInitForm();
		} 
		else if (state == 1)
		{
			// is returnvalue van elke functie, behalve getCallState
			// TODO.. is dit zo, of alleen in situatie van dummy webservice?
		} 
		else if (state == 5)
		{
			setImages(oCMN_V09$1309._imageIdle, false);
			setMessage(oCMN_V09$1309._msgABDisConnected);
            oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.DISCONN);
            setInfoTekst(' ');
		} 
		else if (state == 6 || state == 7)
		{
			// callingA
			setImages(oCMN_V09$1309._imageCallingA, false);
			setMessage(oCMN_V09$1309._msgCallingA);
            oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.CALLA);
		} 
		else if (state == 8)
		{
			// busy
			setImages(oCMN_V09$1309._imageABusy, false);
			setMessage(oCMN_V09$1309._msgABusy);
            oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.ABUSY);
            setInfoTekst(' ');
            oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NOMSG);
			oCMN_V09$1309._callRetry = 1;
		} 
		else if (state == 9)
		{
			// noanswer
			setImages(oCMN_V09$1309._imageANoAnswer, false);
			setMessage(oCMN_V09$1309._msgANoAnswer);
			oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.ANOANS);
            setInfoTekst(' ');
			oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NOMSG);
			oCMN_V09$1309._callRetry = 1;
		} 
		else if (state == 10 || state == 11)
		{
			// A connected, call B
			setImages(oCMN_V09$1309._imageCallingB, false); 
			setMessage(oCMN_V09$1309._msgCallingB);
            oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.CALLB);
		} 
		else if (state == 12)
		{
			// busy B
			setInfoTekst(oCMN_V09$1309._msgInfoBBusy);
			setImages(oCMN_V09$1309._imageBBusy, true);
			setMessage(oCMN_V09$1309._msgBBusy);
			oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.BBUSY);
			oCMN_V09$1309._callRetry = 1;
		} 
		else if (state == 13)
		{
			// noanswer B
			setInfoTekst(oCMN_V09$1309._msgInfoBNoAnswer);
			setImages(oCMN_V09$1309._imageBNoAnswer, true);
			setMessage(oCMN_V09$1309._msgBNoAnswer);
			oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.BNOANS);
			oCMN_V09$1309._callRetry = 1;
		} 
		else if (state == 14)
		{
			// verbinding met A, B
			setImages(oCMN_V09$1309._imageABConnected, false);
			setMessage(oCMN_V09$1309._msgABConnected);
            setInfoTekst(oCMN_V09$1309._msgInfoConnected);
		    oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.CONN);
        } 
		else if (state == 15 || state == 2 || state == 3 || state == 4)
		{
			// fout in verbinding
			setInfoTekst(oCMN_V09$1309._msgInfoError);
			setImages(oCMN_V09$1309._imageIdle, true);
			setMessage(oCMN_V09$1309._msgError);
			oCMN_V09$1309._callRetry = 1;
		    oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.ERROR);
		}
	 }
	oCMN_V09$1309._previousState = state;
	
    oCMN_V09$1309._isWorking = false;
}

//==================================================
function initAndStart()
//==================================================
{
	// zetten van variabelen
	oCMN_V09$1309._previousState = 0;
	oCMN_V09$1309._callEnded = 0;
	oCMN_V09$1309._counter = 0;

	setInitFormWorking();

	// zet URL voor starten call
	oCMN_V09$1309._url = oCMN_V09$1309._baseurl + oCMN_V09$1309._urlCMNStepStartCall + oCMN_V09$1309._urlCMNPhoneNumberB + oCMN_V09$1309._phoneNumberB;
	callCMNServer();    

	setTimeout("checkCallState()", oCMN_V09$1309._CHECKCALLSTATE_DELAY);
}

//==================================================
function retryCall()
//==================================================
{
	// zetten van variabelen
	oCMN_V09$1309._previousState = 0;
	oCMN_V09$1309._callEnded = 0;
	oCMN_V09$1309._counter = 0;

	setRetryFormWorking();

	// zet URL voor retrycall
	oCMN_V09$1309._url = oCMN_V09$1309._baseurl + oCMN_V09$1309._urlCMNStepRetryCall + oCMN_V09$1309._urlCMNPhoneNumberB + oCMN_V09$1309._phoneNumberB;
	callCMNServer();    

	setTimeout("checkCallState()", oCMN_V09$1309._CHECKCALLSTATE_DELAY);
	return true;
}

//==================================================
function cancelCall()
//==================================================
{
	// zet URL voor endcall
	oCMN_V09$1309._url = oCMN_V09$1309._baseurl + oCMN_V09$1309._urlCMNStepEndCall + oCMN_V09$1309._urlCMNPhoneNumberB + oCMN_V09$1309._phoneNumberB;
	callCMNServer();    

	setMessage('');
	setImages(oCMN_V09$1309._imageIdle,false);   
	oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.NOMSG);
	oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.IDLE);

	return true;
}

//==================================================
function resetInitForm()
//==================================================
{
    var elem = oCMN_V09$1309.getElement('CMN_PhoneNumber');
    if (elem != null)
    {
	    elem.readOnly = false;
	    elem.value = oCMN_V09$1309._waterMarkTextTel;
	}
    
	if (oCMN_V09$1309.isKnownElement('CMN_ButtonClear'))
	    oCMN_V09$1309.getElement('CMN_ButtonClear').style.display = 'none';
	if (oCMN_V09$1309.isKnownElement('CMN_ButtonRetry'))
    	document.getElementById('CMN_ButtonRetry').style.display = 'none';
	if (oCMN_V09$1309.isKnownElement('CMN_Button'))
	{
	    document.getElementById('CMN_Button').style.display = '';
	    document.getElementById('CMN_Button').disabled = false;
    }
    
	setImages(oCMN_V09$1309._imageIdle,false);
    oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.IDLE);
	setInfoTekst(oCMN_V09$1309._msgInfoYourNumber);
	return true;
}

//==================================================
function setInitFormWorking()
//==================================================
{
	// update form
	if (oCMN_V09$1309.isKnownElement('CMN_Button'))
    	document.getElementById('CMN_Button').disabled = true;
	if (oCMN_V09$1309.isKnownElement('CMN_PhoneNumber'))
	    document.getElementById('CMN_PhoneNumber').readOnly = true;

    setInfoTekst(oCMN_V09$1309._msgInfoConnecting);
	return true;
}

//==================================================
function setRetryForm()
//==================================================
{
	if (oCMN_V09$1309.isKnownElement('CMN_ButtonClear'))
	    document.getElementById('CMN_ButtonClear').style.display = '';
	if (oCMN_V09$1309.isKnownElement('CMN_ButtonRetry'))
	{
	    document.getElementById('CMN_ButtonRetry').style.display = '';
    	document.getElementById('CMN_ButtonRetry').disabled = false;
    }
	if (oCMN_V09$1309.isKnownElement('CMN_Button'))
	    document.getElementById('CMN_Button').style.display = 'none';

	return true;
}

//==================================================
function setRetryFormWorking()
//==================================================
{
	// update form
	if (oCMN_V09$1309.isKnownElement('CMN_ButtonClear'))
    	document.getElementById('CMN_ButtonClear').style.display = 'none';
	if (oCMN_V09$1309.isKnownElement('CMN_ButtonRetry'))
    	document.getElementById('CMN_ButtonRetry').disabled = true;
	if (oCMN_V09$1309.isKnownElement('CMN_MessageArea'))
    	document.getElementById('CMN_MessageArea').innerHTML = '';

    setInfoTekst(oCMN_V09$1309._msgInfoConnecting);
	return true;
}

//==================================================
function setMessage(aMessage)
//==================================================
{  
	if (oCMN_V09$1309.isKnownElement('CMN_MessageArea'))
    	document.getElementById('CMN_MessageArea').innerHTML = aMessage;
}

//==================================================
function setInfoTekst(aMessage)
//==================================================
{    
	oCMN_V09$1309._msgInfoText = aMessage;
}

//==================================================
function setImages(aImage, aBlink)
//==================================================
{    
	if (oCMN_V09$1309.isKnownElement('CMN_Images'))
	    document.getElementById('CMN_Images').innerHTML = aImage;
	if (aBlink)
	{       
		if (oCMN_V09$1309.isKnownElement('CMN_Info'))
		{
            document.getElementById('CMN_Info').innerHTML = oCMN_V09$1309._imageInfoBlinking;     
           	oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.INFOBLINK);
        }
	} 
	else
	{
		if (oCMN_V09$1309.isKnownElement('CMN_Info'))
		{
            document.getElementById('CMN_Info').innerHTML = oCMN_V09$1309._imageInfo;    
        	oCMN_V09$1309.FireStateChangedEvent(CMNSTATE.INFO);
        }
	}
}

//==================================================
function checkCallState()
//==================================================
{
	if (oCMN_V09$1309._counter > 3600) 
	{ 
		return;
	}

	if (oCMN_V09$1309._callEnded == 1) 
	{
		return;
	}

	if (oCMN_V09$1309._callRetry == 1) 
	{
		oCMN_V09$1309._callRetry = 0;
		setRetryForm();
		return;
	}   

	// zet URL voor callstatae
	oCMN_V09$1309._url = oCMN_V09$1309._baseurl + oCMN_V09$1309._urlCMNStepCallState + oCMN_V09$1309._urlCMNPhoneNumberB + oCMN_V09$1309._phoneNumberB;
	callCMNServer();

	oCMN_V09$1309._counter++;

	setTimeout("checkCallState()", oCMN_V09$1309._CHECKCALLSTATE_DELAY);    
}

//==================================================
function callCMNServer() 
//==================================================
{
	if (!oCMN_V09$1309._isWorking)
	{
		oCMN_V09$1309._isWorking = true;
		oCMN_V09$1309._statusImage.src = oCMN_V09$1309._url + '&' + Math.random();
	}
}

//==================================================
function ClearTextBox(elem) 
//==================================================  
{ 
	if(elem.value == oCMN_V09$1309._waterMarkTextTel ) 
	{
    	elem.value = ''; 
	}
} 

//==================================================
function FillTextBox(elem) 
//==================================================  
{ 
	var testValue = elem.value;    
	testValue = testValue.trim();

	if(testValue == '') 
	{
		elem.value = oCMN_V09$1309._waterMarkTextTel; 
	}
} 

//==================================================
String.prototype.trim=function String_trim() 
//==================================================
{  
	return this.replace(/^ +| +$/g,'');
}

// ==============================================================================
function ShowTooltip(evt, header, tekst)
// ==============================================================================
{
	var e = oCMN_V09$1309.getElement('CMN_InfoHeader');
	if (e)
        e.innerHTML=header;
    e = oCMN_V09$1309.getElement('CMN_InfoText');
	if (e)
        e.innerHTML=tekst;

    var plaatje = oCMN_V09$1309.getElement('CMN_InfoImage');
	if (plaatje)
	{
        var infoPopupObj = oCMN_V09$1309.getElement('CMN_InfoPopup');
	    if (infoPopupObj)
            infoPopupObj.style.display = "block";
    }
} 

// ==============================================================================
function HideTooltip()
// ==============================================================================
{
	var e = oCMN_V09$1309.getElement('CMN_InfoPopup');
	if (e)
        e.style.display="none";
}

// ==============================================================================
function mouseX(evt)
// ==============================================================================
{
	if (evt.pageX) 
		return evt.pageX;
	else if (evt.clientX)
		return evt.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	else 
		return null;
}

// ==============================================================================
function mouseY(evt)
// ==============================================================================
{
	if (evt.pageY) 
		return evt.pageY;
	else if (evt.clientY)
		return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	else 
		return null;
}

//==================================================
function getInfoPopupContent()
//==================================================
{
    var info =  "<div id=\"CMN_InfoPopup\" class=\"CMN_InfoPopup\" style=\"display:none; z-index:100; float:none; position:absolute; border: solid 1px;\">" +
                    "<div>" +
                        "<table class=\"CMN_Info\" style=\"width:150px; padding:0;\">" + 
                            "<tr>" + 
                                "<td id=\"CMN_InfoHeader\" class=\"CMN_InfoHeader\" style=\"font-weight: bold; text-align: center;\">" +
                                 "</td>" +
                            "</tr>" + 
                            "<tr>" + 
                                "<td id=\"CMN_InfoText\" class=\"CMN_InfoText\" style=\"text-align: center;\">" +
                                "</td>" + 
                            "</tr>" +
                        "</table>" +
                    "</div>" +
                "</div>";
    return info;
}


CMN.prototype.getDefaultState = function() {this._regId ='13d0c3a0-0c17-4fa3-bcef-e0ee030a3ec1';this._frontEndUrl = 'http://projects.solition.nl/CallMeNow/Frontend/';this._baseurl = 'http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/CMN_RequestHandler.ashx?CMN_RegId=' + this._regId;this._inService = false;this._bCheck06 = false;this._imageIdle = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Grey Grey.gif"/>';this._imageCallingA = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Grey_blinking Grey.gif"/>';this._imageCallingB = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Green Grey_blinking.gif"/>';this._imageABusy = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Red Grey.gif"/>';this._imageANoAnswer = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Red Grey.gif"/>';this._imageABConnected = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Green Green.gif"/>';this._imageBBusy = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Green Red.gif"/>';this._imageBNoAnswer = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Green Red.gif"/>';this._imageError = '<img src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Grey Grey.gif"/>';this._imageInfo = '<img id="CMN_InfoImage" style="position:static;" src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Info.gif" onmouseover="ShowTooltip(event, oCMN_V09$1309._msgInfoTitleText, oCMN_V09$1309._msgInfoText);" onmouseout="HideTooltip();" />';this._imageInfoBlinking = '<img id="CMN_InfoImage" style="position:static;" src="http://projects.solition.nl/CallMeNow/Frontend/CallMeNow/Images/Info_Blinking.gif" onmouseover="ShowTooltip(event, oCMN_V09$1309._msgInfoTitleText, oCMN_V09$1309._msgInfoText);" onmouseout="HideTooltip();" />';}var oCMN_V09$1309 = new CMN(CMN_USER_INIT);