//Functions used by most of the sites!


function AdvanceCursor(field)
			{       
				var elementid=field.id;  //get field id
				var idlength= field.id.length - 1;  //get the length of the string and -1
				var idstring=elementid.substr(0,idlength);  //get all the charcaters of the string except last in variable idstring
				var next_num = Number(field.id.substr(idlength)) +1; //get the last charcater of the phone id, which is a number and add 1 
				
				if (next_num < 3 && field.value.length == 3)
				
				{  
					document.getElementById(idstring+next_num).select();
					document.getElementById(idstring+next_num).focus();
				}
			}      

var ssl_pop_width = 300;
var ssl_pop_height= 330;
function popup(url,width,height,windowID) {
  if (!url) {
      alert("Error. open_centered_window() is called w/o url");
      return false;
  }
  if (!width) {
      alert("Error. open_centered_window() is called w/ invalid width");
      return false;
  }
  if (!height) {
      alert("Error. open_centered_window() is called w/ invalid height");
      return false;
  }
  if (!windowID)
      windowID = "centered_window";
  var scrheight=screen.availHeight;
  var scrwidth=screen.availWidth;
  var winwidth=width;
  var winheight=height;
  var leftpos= (scrwidth/2)-(winwidth/2);
  var toppos = (scrheight/2)-(winheight/2);
  var windowName = window.open(url,"window"
          , "height=" + winheight
          + ",width=" + winwidth
          + ",left=" + leftpos
          + ",top=" + toppos
          + ", status=0, modal=yes" );

  if (!windowName) {
      alert("Error. cannot open a new window");
      return false;
  }
  else {
      windowName.focus();
      return windowName;
  }
}

var doc_done;
var check_timer;
var record_timer;
var record_done;
var total_time = 0;
var page_id = 0;

function processing()
{
	total_time = 0;
	check_timer = window.setInterval('increment_timer();',500);
	
	if(window.beforeunloadfunction)
	{
		window.onbeforeunload = beforeunloadfunction;
	}
}


function increment_timer()
{
	total_time = total_time + 0.5;
	if(typeof(doc_done)!='undefined')
	{
		total_time = new String(total_time);
		window.onbeforeunload = function(){};
		window.clearInterval(check_timer);
		if(doc_done == 'success')
		{
			record_timer = window.setInterval('record_ready();',100);
		
			record_done = try_ajax('record',"pageaction=1&session_id=" + $('lead_session_id').value + "&cluster_id=" + $('lead_cluster_id').value + "&page_id=" + page_id + "&total_time=" + total_time);
		}
	}
}

function record_ready()
{
	if(typeof(record_done)!='undefined')
	{
		window.clearInterval(record_timer);
	}
}

function try_ajax(url,query)
{	
	var attempts = 0;
	var failure = true;
	var xmlhttp;
	while(failure&&attempts<5)
	{
		try
		{
			failure = false;
			delete xmlhttp;
		/*	
			try
			{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(E)
				{
					xmlhttp = false;
				}
			}
			
			if (!xmlhttp && typeof XMLHttpRequest!='undefined')
			{
				try
				{
					xmlhttp = new XMLHttpRequest();
				}
				catch(e)
				{
						xmlhttp=false;
				}
			}
			*/
			
			
			if (window.XMLHttpRequest) // code for Mozilla, etc.
			{
				xmlhttp=new XMLHttpRequest();
			}
			else if (window.ActiveXObject) // code for IE
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			
			xmlhttp.open('POST',url,false);
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.setRequestHeader('Content-length', query.length);
			xmlhttp.setRequestHeader('Connection', 'close');
			xmlhttp.send(query);
		}
		catch(e)
		{
			attempts++;
			failure = true;
		}
	}
		
	if (!failure)
	{
		return xmlhttp.responseText;
	}
	else
	{
		return false;
	}
}

function swap_divs(one, two)
{
	one = $(one);
	two = $(two);
	if(one)
	{
		one.style.display = 'none';
	}
	if(two)
	{
		two.style.display = 'block';
	}
}

function javascript_enabled()
{
	$('js_enabled').value = 'enabled';
}

function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

