// JavaScript Document
self.focus();

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

var img_cache = new Object();

function parse_images() {
    if (document.getElementById) {
        var ar = document.getElementsByTagName('img');
        for( var x = 0; ar[x]; x++ ) {
            var im = ar[x];
            if(im.getAttribute) { 
                im.hoversrc = im.getAttribute('hoversrc');
            }
            if(im.hoversrc) {
                im.rootsrc = im.src;                
                im.onmouseout = function () {
                    this.src = this.rootsrc;
                }
                
                if(!img_cache[im.hoversrc]) {
                    img_cache[im.hoversrc] = new Image();
                    img_cache[im.hoversrc].src = im.hoversrc;
                }

                im.onmouseover = function () {            
                    this.src = this.hoversrc;                                
                }
            }
        }
    }
}

function doRedirect(sTargetURL)
{
  TargetURL = sTargetURL;
  setTimeout("timedRedirect(TargetURL)", 1*1000 );
}

function timedRedirect(TargetURL)
{
  parent.location.replace(TargetURL);
}


function toggle(id)
{
    if(this.innerText=="More Info")
     this.innerText="Hide Info";
     else
     this.innerText="More Info";
    var obj = document.getElementById(id);
    if(obj.style.display=='block'){
        obj.style.display='none';
    }else{
        obj.style.display='block';
    }
}


// grey out effect
function grayOut(vis, options) {
// Pass true to gray out screen, false to ungray
// options are optional. This is a JSON object with the following (optional) properties
// opacity:0-100 // Lower number = less grayout higher = more of a blackout 
// zindex: # // HTML elements with a higher zindex appear on top of the gray out
// bgcolor: (#xxxxxx) // Standard RGB Hex color code
// grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
// Because options is JSON opacity/zindex/bgcolor are all optional and can appear
// in any order. Pass only the properties you need to set.
var options = options || {}; 
var zindex = options.zindex || 50;
var opacity = options.opacity || 70;
var opaque = (opacity / 100);
var bgcolor = options.bgcolor || '#000000';
var dark=document.getElementById('darkenScreenObject');
if (!dark) {
// The dark layer doesn't exist, it's never been created. So we'll
// create it here and apply some basic styles.
// If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
var tbody = document.getElementsByTagName("body")[0];
var tnode = document.createElement('div'); // Create the layer.
tnode.style.position='absolute'; // Position absolutely
tnode.style.top='0px'; // In the top
tnode.style.left='0px'; // Left corner of the page
tnode.style.overflow='hidden'; // Try to avoid making scroll bars 
tnode.style.display='none'; // Start out Hidden
tnode.id='darkenScreenObject'; // Name it so we can find it later
tbody.appendChild(tnode); // Add it to the web page
dark=document.getElementById('darkenScreenObject'); // Get the object.
}
if (vis) {
// Calculate the page width and height 
if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
var pageWidth = document.body.scrollWidth+'px';
var pageHeight = document.body.scrollHeight+'px';
} else if( document.body.offsetWidth ) {
var pageWidth = document.body.offsetWidth+'px';
var pageHeight = document.body.offsetHeight+'px';
} else {
var pageWidth='100%';
var pageHeight='100%';
} 
//set the shader to cover the entire page and make it visible.
dark.style.opacity=opaque; 
dark.style.MozOpacity=opaque; 
dark.style.filter='alpha(opacity='+opacity+')'; 
dark.style.zIndex=zindex; 
dark.style.backgroundColor=bgcolor; 
dark.style.width= pageWidth;
dark.style.height= pageHeight;
dark.style.display='block'; 
} else {
dark.style.display='none';
}
}

function releaseorder(info) {
    grayOut(true);
    setTimeout("document.billing.submit();", 2000);
    
    var winW = document.body.clientWidth;
    var winH = document.body.clientHeight;

    document.getElementById('div_pleasewait').style.left=(winW/2)-200+'px';
    document.getElementById('div_pleasewait').style.top=200+document.documentElement.scrollTop+'px';
    document.getElementById('orderinfo').innerHTML = info;
    document.getElementById('div_pleasewait').style.visibility = 'visible';
}

function buymorecredit(info) {
    grayOut(true);
    setTimeout("document.billing.submit();", 2000);
    
    var winW = document.body.clientWidth;
    var winH = document.body.clientHeight;

    document.getElementById('div_pleasewait').style.left=(winW/2)-200+'px';
    document.getElementById('div_pleasewait').style.top=200+document.documentElement.scrollTop+'px';
    document.getElementById('orderinfo').innerHTML = info;
    document.getElementById('div_pleasewait').style.visibility = 'visible';
}

function updateforwarding(info) {
    grayOut(true);
    setTimeout("document.uptforwarding.submit();", 2000);
    
    var winW = document.body.clientWidth;
    var winH = document.body.clientHeight;

    document.getElementById('div_pleasewait').style.left=(winW/2)-200+'px';
    document.getElementById('div_pleasewait').style.top=200+document.documentElement.scrollTop+'px';
    document.getElementById('accountinfo').innerHTML = info;
    document.getElementById('div_pleasewait').style.visibility = 'visible';
}

var on_color = "#FF0000";
var off_color = "#f9f9f9";
var blink_onoff = 1;
var blinkspeed= 4000;
function blink(id)
{
    blinkid = document.getElementById(id);
     
    if( blink_onoff == 1) {
           blinkid.style.color = on_color;
       blink_onoff = 0;
    }
    else {
       blinkid.style.color = off_color;
       blink_onoff = 1;
    }
}

function printWindow(url) {
  window.open(url,'printWindow','toolbar=yes,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=700,height=650,screenX=50,screenY=10,top=10,left=50')
}

function isNumberKey(evt) {
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}


/***** toll free vanity number search *****/
function getCursorPosition(textField) 
{
    // Initialize
    var cursortPos = 0;
    // IE Support
    if (document.selection) {
        // Set focus on the element
        textField.focus();
        // To get cursor position, get empty selection range
        var selRange = document.selection.createRange();
        // Move selection start to 0 position
        selRange.moveStart('character', -textField.value.length);
        // The caret position is selection length
        cursorPos = selRange.text.length;
    }  
    // Firefox support
    else if (textField.selectionStart || textField.selectionStart == '0') {
        cursorPos = textField.selectionStart;
    }   
    // Return results
    return (cursorPos);
}
function setCursorPosition(textField, cursorPos) 
{
    // IE Support
    if (document.selection) {
        // Create empty selection range
        var selRange = document.selection.createRange();
        // Move selection start to 0 position
        selRange.moveStart('character', -textField.value.length);
        // Move selection to desired position
        selRange.move('character', cursorPos);
        selRange.select();
    }
    // Firefox support
    else if (textField.selectionStart || textField.selectionStart == '0') { 
        textField.selectionStart = cursorPos;
        textField.selectionEnd = cursorPos;
        textField.focus();
    }
}

//add vanity number when using phone key pad
function addkey(key, vanityinput) {

	var vanitynumber = document.getElementById(vanityinput);

	//clear field
	if (vanitynumber.value=='pbx****') {
	    vanitynumber.value='';
	}
	var position = 0;
	if (vanitynumber.value.length < vanitynumber.maxLength) {
	    //get the cursor position
	    position = getCursorPosition(vanitynumber);
	    var currentText = vanitynumber.value;
	    var preCursorText = currentText.substr(0, position);
	    var postCursorText = currentText.substr(position);
		vanitynumber.value = preCursorText + key + postCursorText;
		//set cursor position
		setCursorPosition(vanitynumber, position + 1);
	}
}

function vanitysearch() {
	
	vanityupdate(null, true);
	
	var prefix = document.getElementById('prefix');
	var vanitynumber = document.getElementById('vanitynumber');
	var searchnumber = prefix.value + vanitynumber.value;
	
	if (searchnumber.length < 10) {
	  alert('Please enter at least 7 numbers for your vanity number!');
	  return false;
	}

	var params = 'tollfree=' + searchnumber;
	new Ajax.Updater('checkstatus', 'vanitysearch.php', {method: 'post', parameters: params}); 
	
	vanitycheck();
}

var vanitycount = 0;

function vanitycheck() {

	var numberpool = document.getElementById('numberpool');
	var checkstatus = document.getElementById('checkstatus');
	
	if (checkstatus.innerHTML == '1') {
		
	  vanitycount = 0;

	  checkstatus.innerHTML = -1;
	  clearTimeout(timer);
	  
	  var prefix = document.getElementById('prefix');
	  var vanitynumber = document.getElementById('vanitynumber');
	  var searchnumber = prefix.value + vanitynumber.value;

	  numberpool.innerHTML = '<input type="hidden" name="vanitynum" value="'+searchnumber+'" /><strong><big>'+searchnumber+'</big> is Available!</strong> <input type="submit"  value="Click to Reserve!" />';

//	  var params = 'tollfree=' + searchnumber;
//	  new Ajax.Updater('checkstatus', 'abc.php', {method: 'post', parameters: params}); 
	  
//	  vanitycount = 0;
//	  vanityupdate(searchnumber);
	} else if (checkstatus.innerHTML == '0' || vanitycount == 15) {

	  vanitycount = 0;
	  clearTimeout(timer);

	  var prefix = document.getElementById('prefix');
	  var vanitynumber = document.getElementById('vanitynumber');
	  var searchnumber = prefix.value + vanitynumber.value;
		
	  numberpool.innerHTML = '<strong><big>'+searchnumber+'</big> is NOT available! Please try again!</strong>';
	  checkstatus.innerHTML = -1;
	  vanitynumber.value = '';

	} else {
	  numberpool.innerHTML = '<strong>Checking <img src="images/processing.gif" alt="Searching" width="97" height="7" /></strong>';
	  
	  vanitycount++;

	  timer = setTimeout("vanitycheck()",1000);  // hold on 1 sec.
	}
}

function vanityupdate(searchnumber, cleanup) {

  if (cleanup != null) {
	if (window.timer) clearTimeout(timer);
  } else {

	vanitycount++;

	var params = 'tollfree=' + searchnumber;
	new Ajax.Updater('checkstatus', 'numcheck.php', {method: 'post', parameters: params}); 

	var numberpool = document.getElementById('numberpool');
	var checkstatus = document.getElementById('checkstatus');
	checkstatus.innerHTML = 'NEW';

	if (checkstatus.innerHTML == 'Done' || checkstatus.innerHTML == ' Done') {
	  clearTimeout(timer);
	  numberpool.innerHTML = '<div id="numlist-num" class="numblock-selected"><a onclick="numlist(\'num\');"><div class="number"><input type="radio" checked="checked" name="chosennum" id="num" value="'+searchnumber+'" onfocus="numlist(\'num\');" />'+searchnumber+'</div><div class="pickme">Pick me</div></a></div>';
	} else if (checkstatus.innerHTML == 'NEW') {
	  if (numbertype == 'vanity') numberpool.innerHTML = '<center><div id="counter">'+vanitycount+'</div><strong>Pending in queue</strong><br /><img src="images/pending.gif" alt="Pending..." width="161" height="12" /><br /><br />(Note: This is a real-time search in the national database. The estimate searching time ranges from 10 sec. to a couple of mins. If the number is available, we will reserve it for you!)</center>';
	  timer = setTimeout("vanityupdate('"+searchnumber+"')",2000);  // hold on 2 sec.
	} else if (checkstatus.innerHTML == 'Running') {
	  if (numbertype == 'vanity') numberpool.innerHTML = '<center><div id="counter">'+vanitycount+'</div><strong>Searching in the National Database</strong><br /><img src="images/pending.gif" alt="Pending..." width="161" height="12" /><br /><br />(Note: This is a real-time search in the national database. The estimate searching time ranges from 10 sec. to a couple of mins. If the number is available, we will reserve it for you!)</center>';
	  timer = setTimeout("vanityupdate('"+searchnumber+"')",2000);  // hold on 2 sec.
	} else {
	  timer = setTimeout("vanityupdate('"+searchnumber+"')",2000);  // hold on 2 sec.
	}
  }
}

/***** end of vanity search *****/