Webphone=function(varName){
	// --------------------------------------------------------
	this.varName =  varName==false || varName==null || varName==undefined || varName=="true" ? "wp" : varName;	
	this.is_active = false;
	this.data = {};
	// --------------------------------------------------------
	this.html =  '<div id="WebphoneAppletView"></div>';
	this.html += '<div id="WebphoneWaitBG" style="position:absolute;top:0;left:0;width:100%;height:100%;margin:0;background-color:black;filter:alpha(opacity=85);-moz-opacity:.85;opacity:.85;z-index:1000;display:none;">';
	this.html += '<div id="WebphoneWaitText" style="position:absolute;left:50%;top:50%;width:300px;margin-top:-25px;margin-left:-150px;text-align:center;color:white;">';
	this.html += 		"<p>This is the first time you are running the version 2.3.2 of the voipGATE webphone, if nothing happened make sure that you enable Java applets in your browser and that "+'<a style="color:#AAA;" href="http://www.java.com/fr/download/">Java</a> '+"is correctly installed on your computer...</p>";
	this.html += 		'<img src="http://www.voipgate.com/webphone/loading.gif" />';
	this.html += 		'<p>'+"Loading java Applet"+' : <span id="'+"WebphonePercent"+'"></span> %</div></p>';
	this.html += '</div>';
	// --------------------------------------------------------
	this.checkPort = function(){
		if(navigator.userAgent.toLowerCase().indexOf("mac")!=-1) return this.active(false);
		wbObj = this;
		var img = new Image();	
		img.onerror = img.onload = function () {
			if (!img) return;
			img = undefined;
			wbObj.active(true);
		};
		img.src = 'http://127.0.0.1:53419/'+String(Math.floor(Math.random()*10000000000))+'.jpg';
		setTimeout(function () {
			if (!img) return;
			img = undefined;
			wbObj.active(false);
		},200);
	}	
	// --------------------------------------------------------
	this.active = function(s){ this.is_active = s;}
	// --------------------------------------------------------
	this.html += '<iframe name="HiddenFrameWebphone" width="0" height="0" style="border:0px;"></iframe>';
	// --------------------------------------------------------	
	document.write(this.html);	this.checkPort();
	// --------------------------------------------------------
	var req = window.location.search.substr(1,window.location.search.length);
	var paramsNames = [];
	var paramsValues = [];
	var paramsNamesObj = {};	
	var paramsPairs = req.split('&');
	for (i=0;i<paramsPairs.length;i++){		
		paramsNames[i+1] = paramsPairs[i].substring(0,paramsPairs[i].indexOf('='));		
		paramsValues[i+1] = unescape(paramsPairs[i].substring(paramsPairs[i].indexOf('=')+1,paramsPairs[i].length));
		paramsNamesObj[paramsNames[i+1]] = paramsValues[i+1];
	}
	// --------------------------------------------------------
	this.displayMask=function(){
		document.getElementById("WebphoneWaitBG").style.display="block";	
	}
	// --------------------------------------------------------
	this.getQueryStringByName=function(attr){
		return eval("paramsNamesObj." + attr);
	}
	// --------------------------------------------------------
	this.loadApplet=function(call){
		this.displayMask();
		if (navigator.javaEnabled()){			
			var html = 	'<applet code="VGWebphone" width="1" height="1" MAYSCRIPT>';
			html += 		'<param NAME="ARCHIVE" VALUE="http://www.voipgate.com/webphone/VGWebphone.jar" />';
			html += 		'<param NAME="user" VALUE="'+call.user+'" />';
			html += 		'<param NAME="pass" VALUE="'+call.pass+'" />';
			html += 		'<param NAME="callnumber" VALUE="'+call.number+'" />';
			html += 		'<param NAME="server" VALUE="'+call.server+'" />';
			html += 		'<param NAME="version" VALUE="2.3.2" />';
			html += 		'<param NAME="varname" VALUE="'+this.varName+'" />';
			html += 		'<param NAME="path" VALUE="http://www.voipgate.com/webphone/software" />';
			html += 		"Your browser don't support Java or the Java plug in is not installed or obsolete...";
			html += 	'</applet>';
			document.getElementById("WebphoneAppletView").innerHTML=html;		}else this.noJava();
	}
	// --------------------------------------------------------
	this.noJava=function(){
		document.getElementById("WebphoneWaitText").innerHTML="You can download java on"+' <a href="http://java.sun.com">http://java.sun.com</a>';	}
	// --------------------------------------------------------
	var i=0;
	this.statusDisplay=function(res){
		if(res<100){
			i++;
			document.getElementById("WebphonePercent").innerHTML=res;		}else document.getElementById("WebphoneWaitBG").style.display="none";		
	}
	// --------------------------------------------------------
	this.displayInfo=function(){
		document.getElementById("WebphoneUser").value=this.getQueryStringByName("user");		document.getElementById("WebphoneNumber").value=this.getQueryStringByName("callnumber");	}
	// --------------------------------------------------------
	this.Call=function(user,pass,number,server){
		var call = {user:user,pass:pass,number:number,server:server};
		this.doCall(call);
	}
	this.call=this.Call;
	// --------------------------------------------------------
	this.Originate=function(number){
		var call = this.data;
		if(call==undefined) return false;
		call.number=number;
		this.doCall(call);
		this.checkPort();
	}
	this.originate=this.Originate;
	// --------------------------------------------------------
	this.doCall=function(call){
		this.checkPort();
		call = this.checkCallObject(call);		
		if(this.is_active) this.sendToFrame("user="+call.user+"&pass="+call.pass+"&callnumber="+call.number+"&server="+call.server+"&action=Call");
		else this.loadApplet(call);
	}	
	// --------------------------------------------------------
	this.checkCallObject=function(call){
		if(!call) return call;
		if(!call.user)		call.user	= this.getQueryStringByName("user");
		if(!call.pass) 		call.pass	= this.getQueryStringByName("pass");
		if(!call.server)	call.server	= this.getQueryStringByName("server");
		if(!call.number)	call.number	= this.getQueryStringByName("callnumber");
		return call;
	}
	// --------------------------------------------------------
	this.SendDTMF=function(user,pass,DTMF,server){
		this.sendToFrame('user='+user+'&pass='+pass+'&callnumber='+DTMF+'&server='+server+'&action=DTMF');
	}
	// --------------------------------------------------------
	this.Hangup=function(){
		this.sendToFrame('callnumber=hangup&action=DTMF');
		this.checkPort();
	}
	this.hangup=this.hangUp=this.Hangup;
	// --------------------------------------------------------
	this.sendToFrame=function(params){
		parent.frames["HiddenFrameWebphone"].location.href = 'http://127.0.0.1:53419?'+params+'&';		
	}
}
