/***************************************************************
*
* $Id: queries.js 25.04.2007 10:50:09
* Update: queries.version2.js 06.08.2007 07:12:11
*
*
*
*  Copyright notice
*
*  (c) 2007 Thomas Brückhändler, www.edv-service-tb.de
*  All rights reserved
*
*  This script is part of global-type.org 
*
*  Released under GNU/GPL (see license file in tslib/)
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*  This copyright notice MUST APPEAR in all copies of this script
***************************************************************/
var http_request 	= false;
var scriptBaseURL	= 'fileadmin/globaltype/phpscript/';
var imgBaseURL		= 'fileadmin/globaltype/images/';

var Err_cannot_create_instance 	= 'Es gab ein Problem!\nDie s.g. XMLHTTP Instanz konnte nicht erzeugt werden!\nSollte dieser Fehler nocheinmal auftreten, setzen Sie sich bitte mit dem Service in Verbindung!';
var Err_Problem_with_Request	= 'Die Serverantwort konnte nicht empfangen werden!\nSollte dieser Fehler nocheinmal auftreten, setzen Sie sich bitte mit dem Service in Verbindung!';

var debugMode 		= false;

function makePOSTRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/html');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
		   	http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert(Err_cannot_create_instance);
		return false;
	}
	http_request.onreadystatechange = SetResponseEval;

	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function SetResponseEval() {
  if (http_request.readyState == 4) {
     if (http_request.status == 200) {
		eval(unescape(http_request.responseText));
     } else {
        alert(Err_Problem_with_Request);
     }
  }
}

function SetVal(str_value,str_sourceField,str_targetBox)
{
	document.getElementById(str_sourceField).value = str_value;
	closeTargetBox(str_targetBox);
}

function EventListener(event, str_targetbox) {
	ShowResultBox 	= true;
	event 			= event || window.event;

	if (debugMode) {document.getElementById("KeyCode").innerHTML = event.keyCode;}
	if (debugMode) {document.getElementById("event").innerHTML = event;}

    switch (event.keyCode)
	{
        case 8:                     //Backspace
            break;
            
        case 13:                    //Enter
            ShowResultBox = false;
            break;
            
        case 27:                    //ESC
            ShowResultBox = false;
            break;
            
        case 38:                    //up
        	ShowResultBox = false;
            break;
            
        case 40:                    //down
        	//ShowResultBox = false;
        	document.getElementById(str_targetbox).focus();
            break;
    }
	return 	ShowResultBox;
}


function displayTargetBox(x,y,obj_listenField, str_targetBox, str_waiting)
{
	var SourceField = obj_listenField;
    var  targetBox = document.getElementById(str_targetBox);
    targetBox.style.display = "none";
    targetBox.style.display = "block";
    targetBox.style.top=(y)+'px';
    targetBox.style.left=x+'px';
	targetBox.innerHTML = str_waiting;
    var iframe = document.getElementById("hiddenIframe");
    iframe.style.display = "none";
    iframe.style.display = "block";
    iframe.style.top=(y)+'px';
    iframe.style.left=x-5+'px';
    iframe.style.height = targetBox.offsetHeight+5+'px';
    iframe.style.width = targetBox.offsetWidth+10+'px';
}

function closeTargetBox(str_targetBox)
{
    var iframe = document.getElementById("hiddenIframe");
    iframe.style.display = "none";
    var  targetBox = document.getElementById(str_targetBox);
    targetBox.style.display = "none";
}

function listenField(str_Formname, obj_inputfield, str_targetbox, tbl_field, event, pPHPScript, is_ie)
{

	var formObject = document[str_Formname];
	if (!formObject)	{
		formObject = document.getElementById(str_Formname);
	}

    ShowResultBox = false;
    str_waiting = 'please wait...';
    var msIE = false;
    if (is_ie.length>0)
    {
		var msIE = true;
	}
    if (event)
	{
        ShowResultBox = EventListener(event, str_targetbox);
		if (debugMode)	{document.getElementById("inputfield").innerHTML = obj_inputfield;}
	}
    if(obj_inputfield.value.length>= 3 && ShowResultBox)
    {
        searchValue = (obj_inputfield.value);
        if (debugMode)	{document.getElementById("searchValue").innerHTML = searchValue;}

		LeftPos = obj_inputfield.offsetLeft;
		TopPos  = obj_inputfield.offsetTop+obj_inputfield.offsetHeight+1;
		if (msIE)
		{
  			LeftPos +=5;
            TopPos += 85;
		}

		displayTargetBox(LeftPos, TopPos, obj_inputfield, str_targetbox, str_waiting);

		/**
		 * special script extension to get the values of the other fields
		 * usually the scripts use only one field with its value getting via
		 * parameter, like this:
		 *		var str_Post = 'suchstring='+searchValue
		 *				+'&sourcefield='+obj_inputfield.name
		 *				+'&targetBox='+str_targetbox
		 *				+'&tblField='+tbl_field;
		 *
		 * but for this querie we need more then one field value!
		 * so this is a little tricky solution:
		 */
		var fObj = '';
		var ValueFontName 	= '';
		var str_Field 		= 'fontname';
		if (formObject[str_Field])	{
			fObj 			= formObject[str_Field];
			ValueFontName	= fObj.value;
		}
		var ValueArtistName = '';
		str_Field 			= 'artist';
		if (formObject[str_Field])	{
			fObj 			= formObject[str_Field];
			ValueArtistName	= fObj.value;
		}
		var ValuePublicdate = '';
		str_Field = 'publicdate';
		if (formObject[str_Field])	{
			fObj 			= formObject[str_Field];
			ValuePublicdate	= fObj.value;
		}
		var ValueCompany 	= '';
		str_Field 			= 'company';
		if (formObject[str_Field])	{
			fObj 			= formObject[str_Field];
			ValueCompany	= fObj.value;
		}
		var ValueClassification = '';
		str_Field				= 'classification';
		if (formObject[str_Field])	{
			fObj 			= formObject[str_Field];
			ValueClassification	= fObj.value;
		}
		var ValueMedium = '';
		str_Field 		= 'medium';
		if (formObject[str_Field])	{
			fObj 			= formObject[str_Field];
			ValueMedium		= fObj.value;
		}


		var str_Post = 'suchstring='+searchValue
						+'&sourcefield='+obj_inputfield.name
						+'&targetBox='+str_targetbox
						+'&tblField='+tbl_field
						+'&fontname='+ValueFontName
						+'&artist='+ValueArtistName
						+'&publicdate='+ValuePublicdate
						+'&company='+ValueCompany
						+'&classification='+ValueClassification
						+'&medium='+ValueMedium
						;
		/** end fancy tricky */
		makePOSTRequest(scriptBaseURL+pPHPScript, str_Post);
    }
    else
    {
        closeTargetBox(str_targetbox);
    }
}

function ShowPic(str_targetBox,pic_file,pic_width,pic_height)
{
	var PicTag  	= '<img id="FontPicTag" name="FontPicImage" src="'+pic_file+'" width="'+pic_width+'" height="'+pic_height+'" vspace="0" hspace="0" border="0" alt="" />';
    
	var y 			= 300;
	var x 			= 0;
    var targetBox 	= document.getElementById(str_targetBox);

    targetBox.style.display = "none";
    targetBox.style.display = "block";
//    targetBox.style.Top=(y)+'px';
    targetBox.style.top=y+'px';
    targetBox.style.left=x+'px';
    targetBox.style.height=(pic_height+2)+'px';
    targetBox.style.width=(pic_width+2)+'px';
	targetBox.innerHTML = PicTag;

//    var iframe = document.getElementById("hiddenIframe");
//    iframe.style.display = "none";
//    iframe.style.display = "block";
//    iframe.style.top=(y-10)+'px';
//    iframe.style.left=(x-10)+'px';
//    iframe.style.height = targetBox.offsetHeight+20+'px';
//    iframe.style.width = targetBox.offsetWidth+20+'px';

}
function HidePic(str_targetBox)
{
    var  targetBox = document.getElementById(str_targetBox);
    targetBox.style.display = "none";
    var iframe = document.getElementById("hiddenIframe");
    iframe.style.display = "none";

}

function ShowPic2(str_targetBox,pic_file,pic_width,pic_height)
{
	var PicTag  	= '<img id="FontPicTag2" name="FontPicImage2" src="'+pic_file+'" width="'+pic_width+'" height="'+pic_height+'" vspace="0" hspace="0" border="0" alt="" />';

	var y 			= 200;
	var x 			= 0;
    var targetBox 	= document.getElementById(str_targetBox);

    targetBox.style.display = "none";
    targetBox.style.display = "block";
    targetBox.style.top=y+'px';
    targetBox.style.left=x+'px';
    targetBox.style.height=(pic_height+2)+'px';
    targetBox.style.width=(pic_width+2)+'px';
	targetBox.innerHTML = PicTag;


//    var iframe = document.getElementById("hiddenIframe2");
//    iframe.style.display = "none";
//    iframe.style.display = "block";
//    iframe.style.top=(y-10)+'px';
//    iframe.style.left=(x-10)+'px';
//    iframe.style.height = targetBox.offsetHeight+20+'px';
//    iframe.style.width = targetBox.offsetWidth+20+'px';

}
