
 /*
  * click2phone ajax client v0.1 
  *
  * Copyright (C) 2005 SalesEmotion AdSolutions GmbH
  *
  */

var xhttp;
var i = 0;

var active;

function init() {	
	// get new instance
	
	if (window.ActiveXObject) {
		try {
			//ie 6.x
			xhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			//ie 5.x
			try {
				xhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhttp = false;
			}
		}
	} else if (window.XMLHttpRequest) {
		//moz
		try {
			xhttp = new XMLHttpRequest();
		} catch(e) {
			xhhtp = false;
		}
	}
	
	active = window.setTimeout("start()", 2000);
}

function start() {
	i = i + 1;
	
	if (i >= 60) {
		// timeout, dude 8(
		xhttp = false;
		window.clearTimeout(active);
		window.location.href = '/de/site/click2phone/phone_timeout.xml';
		return;
	}
	
	xhttp.open('GET','/de/site/ajax/getstate.xml',true);
	xhttp.onreadystatechange = update_callback;
	xhttp.send(null);
}

function update_callback() {

	if (xhttp.readyState!=4) { return; }
	
	if (xhttp.responseXML) {
		
		var state = xhttp.responseXML.getElementsByTagName('result')[0].getAttribute('state');
		switch (state) {
		   case 'calling': {
		      i=0;
		      replaceContent();
		      break;
		   }
		       
			case 'busy1':
			case 'busy2':
			case 'ok':
			case 'error':
			case 'delay':
				replceContent();
         	return;
			
			case 'invalid':
				window.location.href = '/de/site/click2phone/phone_invalid.xml';
				return;
			
			case 'noanswer1':
				window.location.href = '/de/site/click2phone/phone_noanswer1.xml';
				return;
			
			case 'noanswer2':
				window.location.href = '/de/site/click2phone/phone_noanswer2.xml';
				return;
			   
		}
		
      active = window.setTimeout("start()", 2000);
	}
}//update_callback

function replaceContent() {

	var nodes = xhttp.responseXML.getElementsByTagName('result');
	if (nodes[0]) {         	
		var target = document.getElementById('box');         	
		var blank  = target.cloneNode(false);
		if (document.importNode) {
   		var tmp = document.importNode(nodes[0].firstChild,true);
   		blank.appendChild(tmp);         		
   		target.parentNode.replaceChild(blank,target);         		
   	} else{ // F*cking IE...
         target.innerHTML = list[0].firstChild.xml;
      }
		
	}         
}