/*
 * Copyright (c) 2005 - 2007 Nomasystems, S.L. All Rights Reserved.
 *
 * This file contains Original Code and/or Modifications of Original Code as 
 * defined in and that are subject to the Nomasystems Public License version 1.0 
 * (the 'License'). You may not use this file except in compliance with the 
 * License. BY USING THIS FILE YOU AGREE TO ALL TERMS AND CONDITIONS OF THE 
 * LICENSE. A copy of the License is provided with the Original Code and 
 * Modifications, and is also available at www.nomasystems.com/license.txt. 
 * 
 * The Original Code and all software distributed under the License are 
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS 
 * OR IMPLIED, AND NOMASYSTEMS AND ALL CONTRIBUTORS HEREBY DISCLAIM ALL SUCH 
 * WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please 
 * see the License for the specific language governing rights and limitations 
 * under the License.
 *
 * Constantes del API
 */
var SOFTPHONE_PATH = "softphone.jar";

/* 
 * Funciones del API
 */
 
function abortAttendedTransfer(srcLine) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.abortAttendedTransfer(srcLine);
    } else return false;
}
 
function answered(line) {
	return true;
}
 
function call(line,number) {
    var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.call(line,number);
    } else return false;
}

function cypheredCall(line, number) {
	if (isUnsignedInteger(number)) {
		var hexNumber = "";
		for (i = 0; i < number.length; i++) {
			hexNumber += toHexString(number.charCodeAt(i));
		}

		var paddedHexNumber = pkcs5Pad(hexNumber);

		formatedNumber = hex2s(paddedHexNumber);
		formatedKey = hex2s(HEXKEY);
		hexEncryptedNumber = byteArrayToHex(rijndaelEncrypt(formatedNumber, formatedKey, "ECB"));
		call(line, hexEncryptedNumber);
	} else {
		alert("El nœmero introducido es incorrecto. Tiene que ser un entero [0-9]+");
	}
}

function endAttendedTransfer(srcLine) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.endAttendedTransfer(srcLine);
    } else return false;
}

function error(message) {
	alert(message);
}

function hangup(line) {
    var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.hangup(line);
    } else return false;
}

function hold(line) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.hold(line);
    } else return false;
}
 
function hungup(line) {
}

function keypressed(e){
        var unicode = e.charCode ? e.charCode : e.keyCode;
        if ((unicode == 35) || (unicode == 42) || ((unicode > 47) && (unicode < 58))) {
            sendDTMF(1, unicode);
        }
}


function mute(line) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.mute(line);
    } else return false;
}


function sendDTMF(line,tone) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	   	phonejs.sendDTMF(line,tone);
    } else return false;
}

function startAttendedTransfer(srcLine, destNumber) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.startAttendedTransfer(srcLine, destNumber);
    } else return false;
}

function unAttendedTransfer(srcLine, destNumber) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.unAttendedTransfer(srcLine, destNumber);
    } else return false;
}

function unHold(line) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.unHold(line);
    } else return false;
}

function unMute(line) {
	var phonejs = document.getElementById('phonejs');
    if (phonejs) {
	    phonejs.unMute(line);
    } else return false;
}

function loaded() {
    return true;
}

function transferred() {
	alert("Transferencia OK");
	return true;
}

/*
 * Funci—n para cargar el sofphone
 */
function loadPhone(host, user, pass, codec) {
   var phone = 
     '<!--[if !IE]> Firefox and others will use outer object -->' +
     '<object id="phonejs" classid="java:iax.phone.applet.PhoneJS.class"' + 
         'type="application/x-java-applet" archive=\"' + SOFTPHONE_PATH + '\" ' +
         'height="1" width="1" >' +
         '<!-- Konqueror browser needs the following param -->' +
         '<param name="archive" value=\"' + SOFTPHONE_PATH + '\" ' +'/>' +
         '<param name="mayscript" value="true" />' +
         '<param name="user" value=\"' + user + '\" ' +'/>' +
         '<param name="pass" value=\"' + pass + '\" ' +'/>' +
         '<param name="host" value=\"' + host + '\" ' +'/>' +
         '<param name="codec" value=\"' + codec + '\" ' +'/>' +
     '<!--<![endif]-->' +
     '<!-- MSIE (Microsoft Internet Explorer) will use inner object -->' +
     '<object id="phonejs" classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"' + 
         'codebase="http://java.sun.com/update/1.5.0/jinstall-1_5_0-windows-i586.cab"' +
         'height="1" width="1" >' + 
         '<param name="code" value="iax.phone.applet.PhoneJS" />' +
         '<param name="archive" value=\"' + SOFTPHONE_PATH +'\" ' + '/>' +
         '<param name="mayscript" value="true" />' + 
         '<param name="user" value=\"' + user + '\" ' +'/>' +
         '<param name="pass" value=\"' + pass + '\" ' +'/>' +
         '<param name="host" value=\"' + host + '\" ' +'/>' +
         '<param name="codec" value=\"' + codec + '\" ' +'/>' +
         '<strong> This browser does not have a Java Plug-in.<br />' +
             '<a href="http://java.sun.com/products/plugin/downloads/index.html">' +
                 'Get the latest Java Plug-in here.' +
             '</a>' +
         '</strong>' +
      '</object>' +
      '<!--[if !IE]> close outer object -->' +
      '</object>' +
     '<!--<![endif]-->';

   document.write(phone);
}

