var myAjaxReq       = new Array();

// Checks if a call is in progress
function _isCallInProgress (ajaxRequest) {
    switch (ajaxRequest.readyState) {
        case 1: 
        case 2: 
        case 3:
            return true;
            break;
        default:
            return false;
            break;
    }
}

// Timeout message
function _showTimeoutMessage() {
    alert('Zeitüberschreitung!\n\nDer Server reagiert auf die Anfrage nicht.');
}

// Append responders for aborting previous calls and timeout
if (typeof(Ajax) == 'object') {
    Ajax.Responders.register( 
    {
        onCreate:       function (request) {
                            // Abort all previous ajax calls
                            for (i = 0; i < myAjaxReq.length; i++) {
                                if (_isCallInProgress(myAjaxReq[i].transport)) {
                                    myAjaxReq[i].transport.abort();
                                }
                            }
                            // Set timeout for this call
                            request['timeoutId'] = window.setTimeout(
                                                        function() {
                                                            if (_isCallInProgress(request.transport)) {
                                                                request.transport.abort();
                                                                _showTimeoutMessage();
                                                                if (request.options['onFailure']) {
                                                                    request.options['onFailure'](request.transport, request.json);
                                                                }
                                                            }
                                                        },
                                                        10000
                                                    );
                        },
        onComplete:     function (request) {
                            window.clearTimeout(request['timeoutId']);
                            Behaviour.apply();
                            initLightbox();
                        }
    }
    );
}

// AJAX: Load content with ajax
function loadContent (url)
{
	fields = new Array('company', 'person', 'street', 'street_num', 'zip', 'city', 'mail', 'amount', 'terms');
	params = '';
	
	for (i = 0; i < fields.length; i++) {
		if (fields[i] == 'terms') {
			value = $('form_' + fields[i]).checked;
		} else {
			value = $('form_' + fields[i]).value;
		}
		params += fields[i] + '=' + value + '&';
	}
	
    myAjaxReq.push(new Ajax.Updater(
                        'ajaxContent',
                        url,
                        {
                            method:'get',
							parameters:params,
                            onComplete:function() { 
                                //displayLoadingBox(false);
                            },
                            onSuccess:_loadContentSuccess,
                            onFailure:_loadContentError
                        }
                  )
    );
}

// AJAX: Error function of "_loadContent"
function _loadContentError (response)
{
    // displayLoadingBox(false);    
    
    alert('Es ist ein Fehler beim Laden der Seite aufgetreten. Bitte laden Sie die Seite erneut.\n\nError: ' + response.status + '\n' + response.statusText);
}

// AJAX: Success function of "_loadContent"
function _loadContentSuccess (response)
{
	if (response.responseText == '') {
		$('order_form').submit();
	} else {
		eval(response.responseText);
	}
}

function base64decode (encStr)
{
    var base64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';

    var bits;
    var decOut = '';
    var i = 0;
 
    for (; i < encStr.length; i += 4) {
        bits = (base64s.indexOf(encStr.charAt(i))    & 0xff) <<18 |
               (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 |
               (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
                base64s.indexOf(encStr.charAt(i +3)) & 0xff;
  
        decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
    }
    
    if (encStr.charCodeAt(i -2) == 61) {
        return (decOut.substring(0, decOut.length -2));
    } else if(encStr.charCodeAt(i -1) == 61) {
        return (decOut.substring(0, decOut.length -1));
    } else {
        return(decOut);
    }
}

function debugWindow (file, pass)
{
    openCleanWindow(file + '?core=displayDebug&debugPass=' + pass, screen.width, screen.height, 0, 0, 'yes');
}

function hoverLangImage (element)
{
	if (element.src.search(/_bw.+/) != -1) {
		element.src = element.src.replace(/_bw.gif/, '.gif');
    } else {
		element.src = element.src.replace(/.gif/, '_bw.gif');
	}
}

function toggleOrderVolume () 
{
	if ($('volume').style.display != 'inline')
		Element.show('volume');
	else
		$('volume').style.display = 'none';//Element.hide('volume');
	//Element.toggle('volume');
}

function openCleanWindow (url, width, height, left, top, scroll)
{
    window.open(url, 'myWindow', 'height=' + height + ',width=' + width + ',left=' + left + ',top=' + top + ',location=no,menubar=no,status=no,toolbar=no,scrollbars=' + scroll);
}

function changeFaq (elmId) {
	for (i = 0; i <= 7; i++) {
        $('answer:' + i).style.display = 'none';
    }
    
	Element.show('answer:' + elmId.substring(9, 10));
	//new Effect.Appear('answer:' + elmId.substring(9, 10), {duration:1.0});
    //new Effect.Appear('answer:' + element.id.substring(9, 10), {duration:1.5});
}

function changeReferences (elmId) {
	for (i = 1; i <= 2; i++) {
        $('reference:' + i).style.display = 'none';
    }

	Element.show('reference:' + elmId);
//new Effect.Appear('reference:' + elmId, {duration:1.0});
}

function zoomImage (file, imgId, width, height)
{
    file    = base64decode(file);
    imgId   = base64decode(imgId);
    
    width   += 20;
    height  += 30;
    
    if (width > screen.width)
        width = screen.width;
    if (height > screen.height)
        height = screen.height;
    
    left    = (screen.width - width) / 2;
    top     = (screen.height - height) / 2 - 30;   
    
    if (!isNaN(imgId)) {
        
        openCleanWindow(file + '?core=displayImage&objId=' + imgId, width, height, left, top, 'no');
    }
}
