/***************************************************************************
|  FUNC: callback(int, int, str, str, str, str, str, int)
|
|  DESC: 
|       Creates creates a javascrit popup window so the 
|       user can input a phone number where the Fonality
|       Click2call functionality can reach them.
|
| PARAM:
|       server_id       (PBXtra server id)
|       extension       (who to call on the PBXtra server.  
|                        Not used when using the "submenu" parameter      )
|       username        (PBXtra server web login username                 )
|       password        (PBXtra server web login password                 )
|       success_redir   (After successful authentication with Click2call,
|                        a users browser will be pointed to this URL      )
|       failed_redir    (After failed authentication with Click2call,
|                        a users browser will be pointed to this URL      )
|       submenu         (Instead of calling an extension, the call can be
|                        directed straight to a PBXtra submenu            )
|       step            (When usig a submenu, a specific step of that 
|                        menu can be called directly with this option     )
****************************************************************************/  

function callback(sid, ext, user, pass, s_redir, f_redir, smenu, step)
{
	make_prompt('Please enter your call back phone number and click on the Ok button. Proceed through our phone menu once our system calls you back after a few seconds.', '', function(cbNumber){
		if(cbNumber){
			var urlQuery = 'server_id=' + sid + '&number=' + cbNumber + '&username=' + user + '&password=' + pass ;
			// optional parameters
			if ( ext != '' && ext != null ) {
				urlQuery += '&extension=' + ext ;
			}
			if ( smenu != '' && smenu != null ) {
				urlQuery += '&submenu=' + smenu ;
			}
			if ( step != '' && step != null ) {
				urlQuery += '&step=' + step ;
			}
			if ( s_redir != '' && s_redir != null ) {
				urlQuery += '&success_redir=' + s_redir ;
			}
			if ( f_redir != '' && f_redir != null ) {
				urlQuery += '&failed_redir=' + f_redir ;
			}

			window.location = 'http://cp.fonality.com/call.cgi?' + urlQuery ;
			return true;
		}
		else if (cbNumber != 'null' && cbNumber != null) {
			return false;
		}
		else{
			alert("Please specify the number where you can be reached.");
			return false;
		}
	
	});
}

function callbackState()
{
	make_prompt('Please enter your call back phone number and click on the Ok button. Proceed through our phone menu once our system calls you back after a few seconds.', '', function(cbNumber){
		if(cbNumber){
  		if(cbNumber.substr(0,1) == 0){
  		  alert("No International calls please.");
  		  return false;
  		}
  		else if(cbNumber.length > 10){
  		  alert("Only 10 digit calls please.");
  		  return false;
  		}
  		else if(cbNumber == 911){
  		  alert("Only 10 digit calls please.");
  		  return false;
  		}
  		else{
  			var urlQuery = '' ;
  			urlQuery += '&server_id=' + '4435' ;
  			urlQuery += '&username=' + '4435_7333' ;
  			urlQuery += '&submenu=' + 'MAIN' ;
  			urlQuery += '&step=' + '7' ;
  			urlQuery += '&number=' + cbNumber ;
  			urlQuery += '&password=' + '1234' ;
  			urlQuery += '&dontmodify=' + '0' ;
  			urlQuery += '&success_redir=' + 'http://www.texvisions.com/' ;
  			urlQuery += '&failed_redir=' + 'http://www.texvisions.com/contact/' ;
  
  			window.location = 'http://cp.fonality.com/call.cgi?' + urlQuery ;
  			return true;
      }
		}
		else if (cbNumber != 'null' && cbNumber != null) {
			return false;
		}
		else{
			alert("Please specify the number where you can be reached.");
			return false;
		}
	
	});
}

// ie7 prompt replacement
var new_prompt;
prompt_q = new Image(100,25); 
prompt_q.src="prompt_q.gif"; 
var name_id = 0;

function make_prompt(text, default_value, callback, title)
{
	title = title || 'Prompt'
	new_prompt = document.body.appendChild(document.createElement("div"));
	new_prompt.id = 'prompt_'+name_id;
	name_id++;
	new_prompt.callback = callback;
	new_prompt.style.backgroundColor="#EBE9ED";
	new_prompt.style.width = '355px';
	new_prompt.style.height = '150px';
	new_prompt.style.position = "absolute"; 
	new_prompt.style.top = (document.body.clientHeight/2+document.body.scrollTop)-(120/2);
	new_prompt.style.left = (document.body.clientWidth/2+document.body.scrollLeft)-(355/2);
	new_prompt.style.border = '1px solid #C0C0C0';
	new_prompt.style.zIndex  = "999999999";
	//new_prompt.style.paddingTop = '5px';
	//new_prompt.style.paddingLeft = '5px';
	new_prompt.innerHTML = '<div style="width:100%;height:15px;padding-top:2px;border-bottom:1px solid #C0C0C0;background-color:#5593DE;font-size:12px;font-family:arial"><div valign="middle" style="cursor: pointer; cursor: move;padding-left:5px;color:white;" onmousedown="dragStart(event, \''+new_prompt.id+'\')"><b>'+title+'</b></div></div><form onsubmit="if(new_prompt.callback(this.text.value || null)){document.body.removeChild(document.getElementById(\''+new_prompt.id+'\'))};return false;"><table style="padding-top:5px" width="100%" border=0><tr><td width="29" style="padding-left:10px"><img src="./prompt_q.gif" width="29" height="28" border="0" alt=""></td><td style="cursor: default;font-size:12px;font-family:arial">'+text+'</td></tr><tr><td></td><td style="padding-top:10px"><input id=\'prompt_text\' name="text" value="'+default_value+'" style="width:290px" type="text"></td></td><tr><td colspan=2 style="padding-top:5px"><center><input style="width:50px" type="submit" value="Ok"><span style="width:8px">&nbsp;</span><input style="width:50px" type="button" value="Cancel" onclick="document.body.removeChild(document.getElementById(\''+new_prompt.id+'\'));new_prompt.callback(false);"></center></td></td></table></form>';
	//document.write(new_prompt.innerHTML);
	document.getElementById('prompt_text').focus();
	document.getElementById('prompt_text').select();
}



//*****************************************************************************
// Do not remove this notice.
//
// Copyright 2001 by Mike Hall.
// See http://www.brainjar.com for terms of use.
//*****************************************************************************

// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
//dragObj.zIndex = 0;

function dragStart(event, id) {

  var el;
  var x, y;

  // If an element id was given, find it. Otherwise use the element being
  // clicked on.

  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;

    // If this is a text node, use its parent element.

    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Save starting positions of cursor and element.

  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);

  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;

  // Update element's z-index.

  //dragObj.elNode.style.zIndex = ++dragObj.zIndex;

  // Capture mousemove and mouseup events on the page.

  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}

function dragGo(event) {

  var x, y;

  // Get cursor position with respect to the page.

  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }

  // Move drag element by the same amount the cursor has moved.

  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event)
{

  // Stop capturing mousemove and mouseup events.

  if (browser.isIE)
  {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS)
  {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}




