// url to phase6 folder
var phase6url = "/v/";


function setCookie(c_name,value) 	{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+30);
	document.cookie=c_name + "=" +escape(value)+";expires="+exdate.toGMTString()+";path=/";
} 

function getCookie(c_name) {
	if (document.cookie.length>0) {
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1) { 
	    c_start=c_start + c_name.length+1 
	    c_end=document.cookie.indexOf(";",c_start)
	    if (c_end==-1) c_end=document.cookie.length
	    return unescape(document.cookie.substring(c_start,c_end))
		}
  }
	return ""
}

var g_iframe = null;
var g_insItem = null;

function InsertItem(question, answer, searchWord, questionLanguageCode, answerLanguageCode, el) {
  this.question = question;
  this.answer = answer;
  this.searchWord = searchWord;
  this.questionLanguageCode = questionLanguageCode;
  this.answerLanguageCode = answerLanguageCode;
  this.el = el;
  
  this.doInsert = function(p6cookie) {
    var request = dojo.io.bind({
      url: phase6url + "act_InsertItem.php",
		  transport: "XMLHTTPTransport",
			encoding: "utf-8",
			method: "POST",
			content: {
				Phase6CookieID: p6cookie,
				question: this.question,
				answer: this.answer,
				searchWord: this.searchWord,
				questionLanguageCode: this.questionLanguageCode,
				answerLanguageCode: this.answerLanguageCode
				},
			load: function(type, data, evt){
				busy = false;
				data = data.replace( /\r\n/g, "\n" );
				splitData = data.split("\n");
				if (splitData[0] != "ok")
					alert(data);
        removeIframe();
			},
			error: function(type, data, evt){
				alert("ERROR");
				removeIframe();
			},
			mimetype: "text/plain"
    });
  }
}

function phase6insert(question, answer, searchWord, questionLanguageCode, answerLanguageCode, el) {

  var p6cookie = getCookie("Phase6CookieID");
  
  g_insItem = new InsertItem(question, answer, searchWord, questionLanguageCode, answerLanguageCode, el);
  
	if (p6cookie == "") {
        // fri: noch nicht registriert: öffne Login
        showLogin();
        return true;
	}

  g_insItem.doInsert(p6cookie);
	
	/*
	var request = dojo.io.bind({
			url: phase6url + "act_InsertItem.php",
			transport: "XMLHTTPTransport", 
			encoding: "utf-8",
			method: "POST",
			content: { 
				Phase6CookieID: p6cookie,
				question: question, 
				answer: answer,
				searchWord: searchWord,
				questionLanguageCode: questionLanguageCode,
				answerLanguageCode: answerLanguageCode
				},
			load: function(type, data, evt){
				busy = false;
				data = data.replace( /\r\n/g, "\n" );
				splitData = data.split("\n");
				if (splitData[0] != "ok")
//					alert(splitData[1]);
//				else
					alert(data);
			},
			error: function(type, data, evt){
				alert("ERROR");
			},
			mimetype: "text/plain"
	});
	*/
	return false;
}

function showLogin() {
  var resultDiv = document.getElementById("dresult1");
  resultDiv.style.display = "none";
  var iframe = null;

  if (navigator.appName == "Microsoft Internet Explorer") {
    iframe = document.createElement("<iframe frameborder='0' srolling='no'>");
  }
  else {
    iframe = document.createElement("iframe");
  }

  iframe.src = phase6url + "beo_login.php";
  iframe.width = "100%";
  iframe.height = "800px";
  iframe.frameborder = "0";
  iframe.scrolling = "no";
  iframe.style.borderWidth = "0px";
  iframe.style.backgroundColor = "white";

  var obj = resultDiv.nextSibling;
  resultDiv.parentNode.insertBefore(iframe, obj);

  g_iframe = iframe;
}

function hideLogin(setCookieFlag, changeElementFlag) {

  var p6cookie = getCookie("Phase6CookieID");

  if (setCookieFlag == true && p6cookie == "") {
    // var d = new Date();
  	// p6cookie = d.getTime() + "" + Math.round(Math.random() * 100000000);
  	// setCookie("Phase6CookieID", p6cookie);
  }
  
  if (changeElementFlag == true) {
    try {
      g_insItem.doInsert(p6cookie);
      g_insItem.el.firstChild.src = pics_dir + '/vi.png';
      //alert("change img: " + g_insItem.el.firstChild.src);
    }
    catch (e) {
      alert(e);
      removeIframe();
    }
  }
  else removeIframe();
}

function removeIframe() {
  if (!g_iframe) return;
  var resultDiv = document.getElementById("dresult1");
  resultDiv.style.display = "block";
  resultDiv.parentNode.removeChild(g_iframe);
  g_iframe = null;
  window.scrollTo(0);
}

