

var query;
var redinput;
var defaultvideo;
var lastvideo;
var mycomment;
var vspiritname;
var combiAnswer;
var serverAnswer;
var serverTips;
var videoname;
var standbyvideo;
var linkid;
var mute;
var new_visitor;
var text_param;
var vspiriturl;
var pageurl;

var image1 = new Image(); // to force the caching of the image
// image1.src = "http://www.myvspirits.com/img/progress-animation-white.gif";
image1.src = "http://www.searchiko.com/img/progress-animation-white.gif";

muteSound = new Image(120,120);
muteSound.src = "images/soundoff.gif"
normalSound = new Image(120,120);
normalSound.src = "images/soundon.gif";


function keepScrollPositionBottom() 
{ 
var divObj = document.getElementById('responsebox'); 
var txtObj1 = document.getElementById('history'); 
var txtObj2 = document.getElementById('new'); 
//divObj.scrollTop = divObj.scrollHeight-txtObj2.scrollHeight-50; 
//divObj.scrollTop = txtObj1.scrollHeight + 35; 
//divObj.scrollTop = txtObj1.scrollHeight; 
divObj.scrollTop = divObj.scrollHeight;
} 


function focusBox(){
document.all('asb').focus();
}

function load()
{
	   	
	defaultvideo = 0;
	standbyvideo = 0;
    vspiritstate(); // read cookie mute/speak
    vspiriturl = document.getElementById("vspiriturl").value;
    pageurl = document.getElementById("pageurl").value;
    
    load_navigation();
            
   	// refreshing all values
   	combiAnswer = tempvspirit_faq.GetCombinedExistingValues().value;
	var temp = new Array();
	temp = combiAnswer.split('[-]');
	
	// custom for searchiko - answer and tips
	serverAnswer= temp[0];
	serverTips= temp[1];
	videoname = temp[2];
	linkid = temp[3];
	
	// add the session history - special for searchiko
	//if (sessvars.history != undefined) {
	//serverAnswer = sessvars.history+"<br>"+serverAnswer;
	//}
   	
   	//document.getElementById("lblAnswer").innerHTML = serverAnswer;
   
   
	text_param = gup('q');
   	text_param = decodeURI(text_param)
   	
   //	if (mute==0){
	//var e = "<h5><i>>>You wrote: " + text_param + "</i></h5>";
	//}else{
	//var e = "<h5><i>>>You wrote: " + text_param + "</i></h5><h5>"+serverAnswer+"</h5>";
	//}
	
	var e = "<h5><i>>>You wrote: " + text_param + "</i></h5><h5>"+serverAnswer+"</h5>";
	
	addline(e); // the paramter from google search query
   	
   	var tipstable;
   	if (serverTips=="")
   	{
   	tipstable = "";
   	}
   	else{
   	tipstable = "<table style='border-collapse: collapse;background: url(images/searchiko-suggest.png) no-repeat;'>";
   	tipstable = tipstable+"<tr><td valign='top' style='padding-top:20px;padding-left:20px;padding-right:10px;width:250px;height:180px;'>";
   	tipstable = tipstable+serverTips;
	tipstable = tipstable+"</td></tr><tr><td valign='top' style='padding-left:20px;padding-right:10px;height:30px;'>";
	tipstable = tipstable+"<a href='#' onclick=startsubmit('searchiko'); return false; title='Click here to add your search tips and share your knowledge'><img src='images/listarrow.gif' border=0/> Add your suggestions</a>";
    tipstable = tipstable+"</td></tr></table>";
      	}
   	
   	document.getElementById("lblTips").innerHTML = tipstable;
   	
   	keepScrollPositionBottom(); // in order to force the scrolling in case there is history
	
	// The normal way to work is addvideo().
	// But when you work with Media Semantics Flash Multifiles!!! it doesn't work, since the forst file has to load
	// The workaround is to set a time out of 500millisec so it can load
	//addvideo (videoname);
	
	setTimeout('addinitialvideo()',500);
	
	// never do a reditect on the load for searchiko
	//redirect(linkid);
}

function gup( name ){  
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
	var regexS = "[\\?&]"+name+"=([^&#]*)";  
	var regex = new RegExp( regexS );  
	var results = regex.exec( window.location.href );  
	if( results == null )    return "";  else return results[1];
}


function loadHome()
{
	// This is the function for AJAX Mode client
	// i.e. this will behave like a chat client

	query="start";
	redinput="";
	vspiritstate(); // read cookie mute/speak
	document.getElementById("lblAnswer").innerHTML = "";
	document.getElementById("lblTips").innerHTML = "";
	vspiriturl = document.getElementById("vspiriturl").value;
	pageurl = document.getElementById("pageurl").value;

	var x;
	x = tempvspirit_faq.doResetSearchiko(vspiriturl,pageurl).value;
   	
	combiAnswer = tempvspirit_faq.GetCombinedSearchiko(vspiriturl,query,redinput,pageurl).value;
	var temp = new Array();
	temp = combiAnswer.split('[-]');
	
	// custom for searchiko - answer and tips
	serverAnswer= temp[0];
	serverTips= temp[1];
	videoname = temp[2];
	linkid = temp[3];
	
	//if (mute==1){
	//var e="<h5>"+serverAnswer+"</h5>";
	//	addline(e);
	//}
	
	var e;
	if (new_visitor==1){ // new visitor
	e="<h5>Hello, I am Searchiko.<br><br>"+serverAnswer+"</h5>";
	} else {
	e="<h5>Welcome back!<br><br>"+serverAnswer+"</h5>";
	}
	addline(e);
		
	setTimeout('addinitialvideo()',500);
}

function Restart()
{
	// This is the function for AJAX Mode client
	// i.e. this will behave like a chat client

	var x;
	x = tempvspirit_faq.doResetSearchiko(vspiriturl,pageurl).value;
	query="start";
//	sessvars.$.clearMem();
	document.getElementById("lblAnswer").innerHTML = "";
	document.getElementById("lblTips").innerHTML = "";
   	text_param = "welcome";
	ContactingOn();
	redinput = "";
	ProcessQuery();
}

function setStandby()
{
	var x;
	x = tempvspirit_faq.setVideo("standby").value;
}

function submitQuery()
{
	// This is the function for AJAX Mode client
	// i.e. this will behave like a chat client
		
	query=document.getElementById("asb").value;
			
	redinput = "";
	document.getElementById("asb").value = "";
	// var e = "<br><div width='400' style='padding-right:10px;padding-top:2px;padding-bottom:2px;background: #eeeeee;color:ff6600;'>" + query + "</div>";
	text_param=query;
	var e = "<h5><i>>>You wrote: " + text_param + "</i></h5>";
	addline(e);
	ContactingOn();
	keepScrollPositionBottom();
	setTimeout('ProcessQuery()',100);
}

function submitLink(text,id)
{
	// This is the function for AJAX Mode client
	// i.e. this will behave like a chat client for the radio button selections
		
	query=text;
	redinput = id;
	//var e = "<br><div width='400' style='padding-right:10px;padding-top:2px;padding-bottom:2px;background: #eeeeee;color:ff6600;'>" + query + "</div>";
	text_param=query;
	var e = "<h5><i>>>You wrote: " + text_param + "</i></h5>";
	addline(e);
	ContactingOn();
	keepScrollPositionBottom();
	setTimeout('ProcessQuery()',100);
}

function submitTip(id, text, tags)
{
	//Custom Function to Navigate Tips
	// This is the function for AJAX Mode client
	// i.e. this will behave like a chat client for the Tip Navigation
	// Use the same function of ProcessQuery 
			
	query= text + " navtags_" + tags;
	redinput = "navtips_" + id;
	//var e = "<br><div width='400' style='padding-right:10px;padding-top:2px;padding-bottom:2px;background: #eeeeee;color:ff6600;'>" + text + "</div>";
	//addline(e);
	text_param=query;
	var e = "<h5><i>>>You wrote: " + text_param + "</i></h5>";
	addline(e);
	ContactingOn();
	keepScrollPositionBottom();
	setTimeout('ProcessQuery()',100);
}


function ProcessQuery()
{
		
	// serverAnswer = tempvspirit_faq.GetReply(document.getElementById("vspiriturl").value,query,redinput).value;
   	// videoname = tempvspirit_faq.GetVideoName().value;
   	// linkid = tempvspirit_faq.GetRedirectName().value;
	
	combiAnswer = tempvspirit_faq.GetCombinedSearchiko(vspiriturl,query,redinput,pageurl).value;
	var temp = new Array();
	temp = combiAnswer.split('[-]');
	
	// custom for searchiko - answer and tips
	serverAnswer= temp[0];
	serverTips= temp[1];
	videoname = temp[2];
	linkid = temp[3];
	
	
	// special change for searchiko
	if (linkid != "") {
	redirect(linkid);
	return true;
	}
	
	addvideo(videoname);
	// changed the time out for searchiko
	setTimeout('ProcessQueryStep2()',200);
}

function ProcessQueryStep2()
{
	ContactingOff();
	// special change for searchiko
	// redirect(linkid);
	focusBox();
	
	//if (mute==0){
	//var e = "<h5><i>>>You wrote: " + text_param + "</i></h5>";
	//}else{
	//var e = "<h5><i>>>You wrote: " + text_param + "</i></h5><h5>"+serverAnswer+"</h5>";
	//}
	
	var e = "<h5><i>>>You wrote: " + text_param + "</i></h5><h5>"+serverAnswer+"</h5>";
	
	addline(e);
	document.getElementById("lblTips").innerHTML = serverTips;
   	
}

function registerLine(e)
{
	// This is only used for searchiko
	var e = "<br><div width='400' style='padding-right:10px;padding-top:2px;padding-bottom:2px;background: #eeeeee;color:ff6600;'>" + e + "</div>";
	addline(e);
	//ContactingOn();
	keepScrollPositionBottom();
	//setTimeout(100);
}


function addline(res)
{
	// This is only used in AJAX Mode
	var divObj = document.getElementById('responsebox'); 
	var heightBefore = divObj.scrollHeight;
	var prevtext = document.getElementById("lblAnswer").innerHTML;
	//res = prevtext + "<br>" + res;
	
	document.getElementById("lblAnswer").innerHTML = res;
	// sessvars.history = res;
	
	var heightAfter = divObj.scrollHeight;
	
	divObj.scrollTop = heightBefore - 70;
	// divObj.scrollTop = divObj.scrollHeight;
		
}

function replay()
{
	addvideo(lastvideo);
}

function addinitialvideo()
{
	// if visitor is new then change the video name
	if (new_visitor==0){ // new visitor
	videoname= videoname.replace("searchiko-start","searchiko-start-again");
	}
	
	addvideo (videoname);
}	

function addvideo(res)
{
	// Media Semantics Character Builder
	// use the format "hello-1" instead of "stips-hello-1"
	// if the video called is not there then play standby BUT HOW DO I KNOW IT IS NOT THERE
	
	// change default videos based on pageUrl
	if (pageurl=="video"){
	res = res.replace("searchiko-category-general-", "searchiko-video-general-");
	}
	
	if (mute==0){
	flashProxy.call('msPlay',res);
	}
	else{
	flashProxy.call('msPlay','standby');
	}
	
	// DOES NOT WORK
	// var st = flashProxy.call('msPlay',res);
	//if (st==undefined) {
	//flashProxy.call('msPlay','standby');
	//}
	
	// for testing only
	// -----------------
	
	//document.getElementById("lblTest").innerHTML = res;
}	



function actionEnterKey(e)
{
//Press enter;
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

     if(key == 13)
     {
    event.returnValue=false; 
    event.cancel = true;
    submitQuery();
     }
     else
          return true;
     }

function getObj(name){    
if (document.getElementById){
	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
	}
else if (document.all){
    this.obj = document.all[name];
    this.style = document.all[name].style;    
    }
}

function getWinSize()
{   
var iWidth = 0, iHeight = 0;   
if (document.documentElement && document.documentElement.clientHeight){
       // Used by Firefox
       
       iWidth = parseInt(window.innerWidth);
       iHeight = parseInt(window.innerHeight);
       iWidth = iWidth - 460;
       iHeight = iHeight - 200;
       
       if (iHeight<500){iHeight = 500;}
       if (iWidth<550){iWidth = 550;}
              
}
else if (document.body){
		// Used by IE
		
       iWidth = parseInt(document.body.offsetWidth);
       iHeight = parseInt(document.body.offsetHeight);   
       iWidth = iWidth - 450;
       iHeight = iHeight - 200;
       
       if (iHeight<340){iHeight = 340;}
       if (iWidth<500){iWidth = 500;}
       
} 


return {width:iWidth,height:iHeight};
}

function resize_id(obj) {
var oContent = new getObj(obj);
var oWinSize = getWinSize();
var storew =  parseInt(window.innerWidth) - 360;
//if ((oWinSize.height - parseInt(oContent.obj.offsetTop,10))>0)        oContent.style.height = (oWinSize.height - parseInt(oContent.obj.offsetTop,10));
//oContent.style.width =  oWinSize.width;
oContent.style.height = oWinSize.height;

}

function ContactingOn()
{
	var e = "<br><center><img src='"+image1.src+"' /> <span style='color:#333;'></span></center>";
	document.getElementById("lblTimer").innerHTML = e;

}

function ContactingOff()
{
	document.getElementById("lblTimer").innerHTML = " ";
}


function redirect(linkurl)
{ /// special Redirect function only used for searchiko
if (linkurl != "") {
	//linkurl = "?cx=partner-pub-7210792553264612%3A956lnug6yxi&cof=FORID%3A11&ie=UTF-8&q="+linkurl
	//parent.location.href="results.aspx"+linkurl;
	parent.location.href=linkurl;
	return false;
	}
}


function setmute()
{
if (mute == 0) {
mute = 1;
createCookie('searchikostatus','hide',7);
}
else {
mute = 0;
//eraseCookie ('searchikostatus');
createCookie('searchikostatus','talk',7);
}
}

function changeImage()
{
  if (mute==0) {
  document.images["jsbutton"].src= normalSound.src;
  }
  else {
  document.images["jsbutton"].src= muteSound.src;
  }
  return true;
}

function handleMUp()
{
 setmute();
 changeImage();
 addvideo(videoname);
 return true;
}


function load_navigation() {
var x;
var query_param = gup('q');

x = document.getElementById("weblink");
if (x) {document.getElementById("weblink").href="webresults.aspx?cx=partner-pub-7210792553264612%3A956lnug6yxi&cof=FORID%3A11&ie=UTF-8&q="+query_param+"&t="+query_param; }

x = document.getElementById("videolink");
if (x) {document.getElementById("videolink").href="videoresults.aspx?q="+query_param+"&t="+query_param; }

x = document.getElementById("shoppinglink");
if (x) {document.getElementById("shoppinglink").href="shoppingresults.aspx?q="+query_param+"&t="+query_param; }
}

function vspiritstate(){
var x = readCookie('searchikostatus');
if (x) {
	// a cookie is written if the user is not new
	new_visitor=0;
	if (x=="hide"){
	document.images["jsbutton"].src= muteSound.src; // show mute button
	mute=1; // true
	} else {
	mute=0; // false
	}
}
else {
	mute=0; //false
	new_visitor=1; // visitor is new
	createCookie('searchikostatus','talk',7); // write the cookie
	}
}

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 = "";
	document.cookie = name+"="+value+expires+"; 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);
}
