var installpath = "vraag.europeesche.nl/chatbot/selfservice/";
var basepath = "http://vraag.europeesche.nl/selfservice/";
var chatbotOnSelect = 0;
function startChatbot()
{
	var vars = "";
	var ary=window.location.search.substr(1).split("&");
	
	if(ary != "")
	{
		for (ary_i=0;ary_i<ary.length;ary_i++) 
		{
			ary[ary_i]=ary[ary_i].split("=");
		}
		
		var qStr=new Array();
		for (ary_i2=0;ary_i2<ary.length;ary_i2++)
		{
			ary[ary_i2][0]=ary[ary_i2][0].replace(/\+/g," ");
			ary[ary_i2][0]=unescape(ary[ary_i2][0]);
			ary[ary_i2][1]=ary[ary_i2][1].replace(/\+/g," ");
			ary[ary_i2][1]=unescape(ary[ary_i2][1]);
			qStr[ary[ary_i2][0]]=ary[ary_i2][1];
		}
		vars = qStr["vars"];
	}
	
	if(vars == undefined)
	{
		fillChat('', '', '', '', '','', '', '');
	}
	else
	{
		var ca = vars.split(';');
		for(var i=0;i < ca.length;i++)
		{
				c = ca[i];
				c = trim(c);
				
				if(i == 0)
				{
					var catname = c;
				}
				else if(i == 1)
				{
					var catid = c;
				}
				else if(i == 2)
				{
					var taxtype = c;
				}
				else if(i == 3)
				{
					var taxid = c;
				}
				else if(i == 4)
				{
					var topid = c;
				}
				else if(i == 5)
				{
					var obj = c;
				}
				else if(i == 6)
				{
					var q = c;
				}
				else if(i == 7)
				{
					var target = c;
				}
				else if(i == 8)
				{
					var lang = c;
				}
				else if(i == 9)
				{
					var channel = c;
				}
		}
		fillChat(catname, catid, taxtype, taxid, topid, obj, q, target, lang, channel);

		if(catid != undefined && catid != "")
		{
			//alert(catid);
			document.getElementById("cat").value = catid;
		}
		if(catname != undefined && catname != "")
		{
			//alert(catid);
			document.getElementById("catname").value = catname;
		}
	}
	this.focus();
}

var myWidth = 0, myHeight = 0, posX = 0;
  
function getWindowSize() {
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.offsetWidth || document.documentElement.offsetHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.offsetWidth;
    myHeight = document.documentElement.offsetHeight; 
  }
}

function getScreenPosition() {
   if( typeof( window.screenX ) == 'number' ) {
    //Non-IE
    posX = window.screenX;
  } else if( document.documentElement ) {
    //IE 6+ in 'standards compliant mode'
   	posX = window.screenLeft;
  }
  //window.alert( 'posX = ' + posX );
}

function showAssistant(scrw, scrh, w, h, namewindow, Assistant_Category, Assistant_CategoryID, Tax_type, TaxID, ToplistID, objID, Question, TargetID, LanguageID, ChannelID)
{
	getScreenPosition();
	getWindowSize();
	window.name = "opener_Hylo_Chatbot";

        scrw = '';
		scrh = 0;
		h = 770;
	// present dutch version as default
	var location = basepath+"chatbot.htm?vars="+Assistant_Category+";"+Assistant_CategoryID+";"+Tax_type+";"+TaxID+";"+ToplistID+";"+objID+";"+Question+";"+TargetID+";"+LanguageID+";"+ChannelID;
	

	// check for left position; if not there, set in middle
	if(scrw == "" || scrw == undefined)
	{
		var scrw= posX + myWidth - 282;
		//var scrw=(screen.width)-(310);
	}
	// check for top position; if not there, set in middle
	if(scrh == "" || scrh == undefined)
	{
		//var scrh=(screen.height)-(h/2);
		var scrh= 0;
	}
        
	window.open(location,namewindow,('toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,menubar=0,copyhistory=0,left='+scrw+',top='+scrh+',width='+w+',height='+h));
}

function fillChat(Assistant_Category, Assistant_CategoryID, Tax_type, TaxID, ToplistID, objID, Question, TargetID, LanguageID, ChannelID)
{
	var Question_set = "";
	if(Assistant_Category == undefined){Assistant_Category = "";} // default categoriebeschrijving, waarde wordt bij inrichting bepaald, mag ook leeg zijn
	if(Assistant_CategoryID == undefined){Assistant_CategoryID = "";} // default categorie id, waarde wordt bij inrichting bepaald, mag ook leeg zijn
	if(Tax_type == undefined){Tax_type = "1";} // default taxonomie is vragenlijst (1 = vragenlijst / 0 = begeleiding)
	if(TaxID == undefined){TaxID = "";} // default taxonomie, waarde wordt bij inrichting bepaald, mag ook leeg zijn
	if(ToplistID == undefined){ToplistID = "";} // default toplijst, waarde wordt bij inrichting bepaald, mag ook leeg zijn
	if(objID == undefined){objID = "";} // object id is default leeg
	if(Question == undefined){Question = "";} // vraag is default leeg
	if(TargetID == undefined){TargetID = "";} // doelgroep is default leeg (worden ini waarden gebruikt)
	if(LanguageID == undefined){LanguageID = "";} // taal is default leeg (worden ini waarden gebruikt)
	if(ChannelID == undefined){ChannelID = "";} // kanaal is default leeg (worden ini waarden gebruikt)
	 
	// maak url variabelen
	if(Assistant_CategoryID !="")
	{
		Assistant_CategoryID = "&cat="+Assistant_CategoryID+"&showcatid="+Assistant_CategoryID;
	}
	
	if(ToplistID != "" && TaxID != "")
	{
	  TaxID = "";
	}
	
	if(Tax_type !="" && TaxID !="")
	{
		Tax_type = "&tt="+Tax_type;
	}
	else
	{
		Tax_type = "&tt=";
	}
	
	if(TaxID !="")
	{
		TaxID = "&tid="+TaxID;
	}
	
	if(ToplistID !="")
	{
		ToplistID = "&tlid="+ToplistID;
	}
	
	if(objID !="")
	{
		objID = "&action=show&obj_id="+objID;
	}
	
	if(Question != "")
	{
		Question_set = "&org="+Question+"&aw="+Question+"&action=results";
		document.getElementById("zv").value = Question;
	}
	
	if(TargetID !="")
	{
		TargetID = "&tr="+TargetID;
	}
	
	if(LanguageID == 4)
	{
		useinstallpath = installpath_fr;
	}
	else if(LanguageID == 3)
	{
		useinstallpath = installpath_de;
	}
	else if(LanguageID == 64)
	{
		useinstallpath = installpath_be;
	}
	else
	{
		useinstallpath = installpath;
	}

	if(LanguageID !="")
	{
		LanguageID = "&lg="+LanguageID;
	}

	if(ChannelID !="")
	{
		ChannelID = "&ch="+ChannelID;
	}
	
	// check rangorde variabelen
	if(objID != "" && TaxID != "" && Tax_type == "&tt=0")
	{
	 	TaxID = "";
		Tax_type = "";
	}
	
	if(objID != "" && Question_set != "")
	{
	 	Question_set = "&org="+Question;
	}
	
	var newhtml = '<iframe style="overflow-y:auto;overflow-x:hidden;background-color:#FFFFFF" name="Chatbot" height="365" scrolling="auto" width="225" src="http://'
	+useinstallpath
	+'hylo.search.asp?cb=1&ass_cat=' 
	+Assistant_Category
	+Assistant_CategoryID
	+Tax_type
	+TaxID
	+ToplistID
	+objID
	+Question_set
	+TargetID
	+LanguageID
	+ChannelID
	+' " frameborder="0"></iframe>';
	
	//alert(newhtml);
	//document.getElementById("av").value = Question;
	document.getElementById("chatwindow").innerHTML = newhtml;
}

function submitform()
{
	document.getElementById("ChatinputForm").submit();
}

function checksubmit()
{
	///var actionset = "http://"+installpath+"hylo.search.asp?action=results";
	var question = document.getElementById("av").value;
	if(question == "")
	{
		alert("U heeft geen vraag gesteld");
		return false;
	}
	else
	{
		submitform();
	}
}

function trim(value)
{
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}


function display() 
{
  if(chatbotOnSelect == 1)
  {
	  if(document.getSelection) 
	  {
		var str = document.getSelection();
	  } 
	  else if (document.selection && document.selection.createRange) 
	  {
		var range = document.selection.createRange();
		var str = range.text;
	  } 
	  else 
	  {
		var str = "";
	  }
	  if(str != "")
	  {
			if(str.length > 99)
			{
				str = str.substring(0,99);
			}
			showAssistant(1000,120,275,600,'hylo_chatbot', 'uw tekstselectie', '', '0', '', '', '', str,'','','')
	  }
  }
}

