// Aggregated JavaScript page: http://www.alice.nl/templates/freecall/js

// ---- freecall.js ----
;/* Extra function, added for Alice.nl */
function AliceStartFreecall(numfield, object)
{
    var num = document.getElementById(numfield).value;

    if (num.length < 10 || num == 'telefoonnummer') {
        return alert('Vul alstublieft uw telefoonnummer in');
    }

    var statusdivs = jQuery(object).parents('div.freecallstatus');
    if (statusdivs.length == 1)
    {
        var statusdiv = statusdivs[0];
        var i = 1;
        jQuery('div.freecallstatus').each(function(){
            this.id = 'freecallstatus_tmp_' + i++;
        });
        statusdiv.id = 'freecallstatus';

        statusdiv.innerHTML = 'Een moment alstublieft...';

        var numhidden = document.createElement('input');
        numhidden.type = 'hidden';
        numhidden.value = num;
        numhidden.id = numhidden.name = numfield;
        statusdiv.appendChild(numhidden);
    }

    StartFreecall(numfield);
}

// ------------------- end of added code ---------------------

function StartFreecall(numfield)
{
	var fc = window.document['freecall'];
	var num = document.getElementById(numfield).value;

	fc.SetVariable("telnr", num);
	fc.SetVariable("StartSignal", 1);
}

function StartFreecall2(numfield, customerdata)
{
	var fc = window.document['freecall'];

	var num = document.getElementById(numfield).value;

	fc.SetVariable("customerdata", customerdata);
	fc.SetVariable("telnr", num);

	fc.SetVariable("StartSignal", 1);
};

function ShowInput()
{
	window.document.freecall.SetVariable("StartSignal", 2);
};

function GetIErequest()
{
	try
	{
		request = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch(err)
	{
		request = new ActiveXObject('Microsoft.XMLHTTP');
	};
	return request;
};

function freecall_DoFSCommand(command, args) 
{
	ProcessFreecallStatus(command, args);
};

function IncludeFreecall(subscription, telnr, startform, width, height, templates)
{
	var flashname = '';

	flashname = '/templates/freecall/freecall.swf?subscrid='+
    subscription+
		'&telnr='+telnr+
		'&openstartform='+startform+
// Changed by InterNLnet: why keep this file from caching?
//		'&timestamp='+new Date().getTime()+ 
		'&templates='+templates +
		'&'+GetCustomerData();

	if (QueryString('callid'))
	{
		flashname = flashname +
			'&callid='+QueryString('callid')+
			'&status='+QueryString('status')+
			'&prevstatus='+QueryString('status');
	}

  var code = '<OBJECT id="freecall" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' +
	           ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' +
	           '  WIDTH="'+width+'" HEIGHT="'+height+'" ALIGN="">' +
	           ' <PARAM NAME=movie VALUE="'+flashname+'"> ' +
             ' <PARAM NAME=quality VALUE=high>' +
	           ' <PARAM NAME=bgcolor VALUE=#FF6633>' +
	           ' <EMBED ID="freecallobject" src="'+flashname+'" quality=high bgcolor=#FF6633  WIDTH="'+width+'" HEIGHT="'+height+'" swLiveConnect=true ID="freecall" NAME="freecall" ALIGN=""' +
	           ' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>' +
	           '</OBJECT>';

  document.write(code);
}

function IncludeFreecall2(subscription, telnr, startform, width, height, templates, cc)
{
	var flashname = '';

	flashname = '/templates/freecall/freecall19.swf?subscrid='+subscription+
		'&openstartform='+startform+
// Changed by InterNLnet: why keep this file from caching?
//		'&timestamp='+new Date().getTime()+ 
		'&templates='+templates+
		'&'+GetCustomerData();

	if (QueryString('callid'))
	{
		flashname = flashname +
			'&callid='+QueryString('callid')+
			'&status='+QueryString('status')+
			'&prevstatus='+QueryString('status');
	};

	document.write('<OBJECT id="freecall" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
	document.write(' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"');
	document.write('  WIDTH="'+width+'" HEIGHT="'+height+'" ALIGN="">');
	document.write(' <PARAM NAME=movie VALUE="'+flashname+'"> ');
	document.write(' <PARAM NAME=quality VALUE=high>');
	document.write(' <PARAM NAME=bgcolor VALUE=#FF6633>');
	document.write(' <EMBED ID="freecallobject" src="'+flashname+'" quality=high bgcolor=#FF6633  WIDTH="'+width+'" HEIGHT="'+height+'" swLiveConnect=true ID="freecall" NAME="freecall" ALIGN=""');
	document.write(' TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer"></EMBED>');
	document.write('</OBJECT>');
};


function QueryString(ji) 
{
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) 
	{
		ft = gy[i].split("=");
		if (ft[0] == ji) 
		{
			return ft[1];
		}
	}
};

function GetCustomerData()
{
	var s = '';
	var r = '';
	var i = 0;

	s = QueryString('data' + i);
	while (s)
	{
		r = r + 'data' + i + '=' + s + '&';
		i++;
		s = QueryString('data' + i);
	};

	s = QueryString('button');
	if (s) r = r + 'button=' + s + '&';
	s = QueryString('gjdata');
	if (s) r = r + 'gjdata=' + s + '&';
	s = QueryString('postcode');
	if (s) r = r + 'postcode=' + s + '&';
	s = QueryString('Postcode');
	if (s) r = r + 'postcode=' + s + '&';
	s = QueryString('ref');
	if (s) r = r + 'ref=' + s + '&';
	return r;
};


// ---- freecallstatus.js ----
;function AddImage(target, name)
{
	var img = document.createElement('IMG');
	img.setAttribute('src', name);
	target.appendChild(img);
}

function AddNewLine(parent)
{
	var newLine = document.createElement('BR');
	parent.appendChild(newLine);
}

function AddLink(parent, title, url, target)
{
	var newLink = document.createElement('A');
	var newText = document.createTextNode(title);
	newLink.setAttribute('href', url);
	newLink.setAttribute('target', target);
	newLink.appendChild(newText);
	parent.appendChild(newLink);
}


function ProcessInlineHtml(obj, text)
{
	var i = 0;
	var html = '';
	obj.innerHTML = '';
	while (text != '')
	{
		i = text.indexOf('[');
		if (i == -1)
		{
			obj.innerHTML = obj.innerHTML + text;
			text = '';
		}
		else
		{
			s = text.substring(0, i);
			obj.innerHTML = obj.innerHTML + s;
			j = text.indexOf(']');
			html = text.substring(i + 1, j);
			if (html == 'back')
			{
				AddNewLine(obj);
        //AddLink(obj, 'terug', 'javascript:ShowInput()', '');
			}
      else
			{
				switch (html.charAt(0))
				{
					case 'i':
					{
						i = html.indexOf(' ');
						tmp = html.substring(i + 1, html.length);
						AddImage(obj, tmp);
						break;
					}
					case 'b':
					{
						AddNewLine(obj);
						break;
					}
				};
			};
			text = text.substring(j + 1, text.length);
		};

	};

}

function ProcessFreecallStatus(command, url) 
{
  var dest = 'freecallstatus';
  var obj = document.getElementById(dest);

  if (command == 'UpdateStatusText')
  {
	obj.innerHTML = url;
  };

  if (command == 'UpdateStatusText2')
  {
	ProcessInlineHtml(obj, url);
  };

  if (command == 'UpdateStatus')
  {
		var request = null;

		if (obj)
		{
			if (url != '')
			{
				try
				{
					if (window.XMLHttpRequest) 
						request = new XMLHttpRequest();
					else
						request = GetIErequest();

					if (request)
					{	
						request.open('GET', url, false);
						request.send(null);
						obj.innerHTML = request.responseText;
					};
				}
				catch(err)
				{
					alert(err);
				}
			}
		};
  };
}



