
var aInitFunctions = new Array();
var ie6;
var cookiesDomain;
var currentLocation = null;
var tunnelID = null;
var proxyID = "swfProxy";
var flashvars = {
	tunnelID: tunnelID
};

function getTunnelIDParam()
{
	return "tunnelID="+getTunnelID();
}
function createTunnelID()
{
	eraseCookie("tunnelID");
	var rand = Math.random()*50000;
	tunnelID = "nos_tunnel_"+Math.floor(rand);
	rand = Math.random()*10000;
	tunnelID += "_"+Math.floor(rand);
	createCookie("tunnelID",tunnelID,1);
}
function getFromParam(param)
{
	var str = location.search;
	var paramsAlpha = str.substr(1).split('&');
	var params = new Array();
	var newQuery = "";	
	
	for(i=0; i < paramsAlpha.length; i++)
	{
		tempParam = paramsAlpha[i].split('=');
		params[tempParam[0]] = tempParam[1];
	}
	return params[param];
}
function getTunnelIDFromParam()
{
	return getFromParam("tunnelID");
}

function getTunnelID()
{
	if(tunnelID != null)
	{
		createCookie("tunnelID",tunnelID,1);
	}
	else
	{
		tunnelID = getTunnelIDFromParam();
		if(tunnelID != null)
		{
			eraseCookie("tunnelID");
			createCookie("tunnelID",tunnelID,1);
		}
		else
		{
			tunnelID = readCookie("tunnelID");
		}
	}

	return tunnelID;
}

function startProxyDiv()
{
	var obj = document.getElementById(proxyID);
	if(obj)
	{
		obj.parentNode.removeChild(obj);
	}
	obj = document.body.appendChild(document.createElement("div"));
	obj.setAttribute("id",proxyID);
}

function startReceiver()
{
	if(ie6)
	{
		startProxyDiv();
	}
	swfobject.embedSWF(homeBase+"swf/swfTunnel_receiver.swf", proxyID, "1", "1", "9.0.0", homeBase+"swf/expressinstall.swf",{},{bgcolor: '#000000', menu: 'false', wmode: 'opaque', allowScriptAccess:"always"},{id:proxyID,name:proxyID});
}

function startSender(isRoot)
{
	if(ie6)
	{
		startProxyDiv();
	}
	var path = (isRoot == true)?"":"../../";
	swfobject.embedSWF(homeBase+"swf/swfTunnel_sender.swf", proxyID, "1", "1", "9.0.0", homeBase+"swf/expressinstall.swf",{},{bgcolor: '#000000', menu: 'false', wmode: 'opaque', allowScriptAccess:"always"},{id:proxyID,name:proxyID});
}

function getCurrentLocationParam()
{
	return "locate="+currentLocation;
}
function getCurrentLocation()
{
	if(currentLocation != null)
	{
		createCookie("currentLocation",currentLocation,1);
	}
	else
	{
		currentLocation = getFromParam("locate");
		if(currentLocation != null)
		{
			eraseCookie("currentLocation");
			createCookie("currentLocation",currentLocation,1);
		}
		else
		{
			currentLocation = readCookie("currentLocation");
		}
	}

	return currentLocation;
}
function saveCurrentLocation(url)
{
	eraseCookie("currentLocation");
	currentLocation = Url.encode(url);
	createCookie("currentLocation",currentLocation,1);
}
function initCurrentLocation()
{
	var str = location.href+"";
	saveCurrentLocation(str);
}
function changeCurrentLocationTo(from,to)
{
	var str = location.href+"";
	str = str.replace(from,to);
	saveCurrentLocation(str);
}
function changeToSecureLocation()
{
	changeCurrentLocationTo(homeBase,homeSSL);
}
function getParentLocation()
{
	if(getCurrentLocation() != null)
		return Url.decode(getCurrentLocation());
	else
		return homeBase;
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Url = {
 
	encode : function (str) {
		return escape(this._utf8_encode(str));
	},
 
	decode : function (str) {
		return this._utf8_decode(unescape(str));
	},
 
	_utf8_encode : function (str) {
		str = str.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < str.length; n++) {
 
			var c = str.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	_utf8_decode : function (utftext) {
		var str = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				str += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				str += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				str += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return str;
	}
 
}


/* COOKIES */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";


	var domain = (cookiesDomain && cookiesDomain != '') ? "; domain=" + cookiesDomain : "";
	document.cookie = name+"="+value+expires+domain+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function addBoot(fnc)
{
	if(fnc)
	{
		aInitFunctions.push(fnc);
	}
}
function execBoot()
{
	var i = 0;
	for(i = 0; i < aInitFunctions.length; i++)
	{
		aInitFunctions[i]();
	}
	aInitFunction = new Array();
}

/** JAVA FIELDS - SDK **/
function setCheckBoxAnswer(questionId,answerId,value) {
  if (document.getElementById(questionId+'_'+answerId).checked) {
      document.getElementById(questionId).value = value;  }
  else {    document.getElementById(questionId).value='';
  }
}

function processRadioButtonInfo(questionId,answerId,answerText) {
  document.getElementById(questionId).value= answerText;
  document.getElementById(questionId+'_answer').value= answerId;
}
function setInitialSelectBoxIndex(selectBox,value) {
	var options = selectBox.options;
	for (var i = 0; i < options.length; i++) {
	 if (options[i].value == value) {
	 selectBox.selectedIndex = i;
	  break;
	 }
	}
}

function updateMainPhoneField(name){
	
	  var phone1 = 	completeStringLength(document.getElementById("phone1").value, 3, " ");
	  var phone2 = 	completeStringLength(document.getElementById("phone2").value, 3, " ");
	  var phone3 = 	completeStringLength(document.getElementById("phone3").value, 4, " ");
	  
	  document.getElementById(name).value =  phone1 + phone2 + phone3 ;
	  
	}

	
function completeStringLength(str,size,fill){

	if(str.length < size){
		str = completeStringLength(str+fill,size,fill);
	}
	return str;
}