if(document.implementation && document.implementation.createDocument){
    var isMozilla=true;
}else{
    var isMozilla=false;
}

var host = '';

function showCalendar() 
{
    
    window.affiliateDatumPos = findPos($('affiliateAuftragsdatum'));
    contactPos = findPos($('contact-form'));
    
	$('calendar-div').style.top = (window.affiliateDatumPos[1]-contactPos[1])+'px';
    $('calendar-div').style.left = (window.affiliateDatumPos[0]-contactPos[0]+75)+'px';

    new Ajax.Updater('calendar-div', host+'/kalender');
    Element.show('calendar-div');
    //if(!isMozilla)$('affiliateInternet-eingangsnummer').style.visibility = 'hidden';
    
}
function showCalendarPrev (url)
{
    new Ajax.Updater('calendar-div', '..'+url);
}
function showCalendarNext (url)
{
    new Ajax.Updater('calendar-div', '..'+url);
}

function selectDate(mydate)
{
    if ($('affiliateAuftragsdatum'))
    {
        $('affiliateAuftragsdatum').value = mydate;
        hideCalendar();
    }
}
function hideCalendar() 
{
    if ($('calendar-div'))
    {
        Element.hide('calendar-div');
        //$('affiliateInternet-eingangsnummer').style.visibility = 'visible';
    }
}

function findPos(myCaller) 
{

    var curleft = curtop = 0;
    if (myCaller.offsetParent) {
        curleft = myCaller.offsetLeft
        curtop = myCaller.offsetTop
        while (myCaller = myCaller.offsetParent) {
            curleft += myCaller.offsetLeft
            curtop += myCaller.offsetTop
        }
    }

    return [curleft,curtop];
}

/**
* neue fieldsets werden gebaut
*/

var delay;

function reqContentDelayed(clean, fieldset, caller)
{
    window.caller = caller;
    
    if (delay != null) {
        window.clearTimeout(delay);
    }
    
    Element.show('loading');
    $('sendbtn').disabled = 'true';
    delay = window.setTimeout('reqContent(\''+clean+'\',\''+fieldset+'\');',1000);
}


function reqContent (clean, fieldset)
{
	    var formular = fieldset+':'+window.caller.options[window.caller.selectedIndex].value;
	    new Ajax.Updater('contactContainer', host+'/affiliates/kontakt/'+formular+'/element',{
		                 parameters:Form.serialize('contact-form'),
                          method:'POST',
		                 onLoading:function(request){
	if (clean=='all') {
       cleanElem(1, 4);
    } else{
        cleanElem(3, 4);
    }	                    
		                    
	                      
	                     },
	                     onComplete:function(request){
	                         new Ajax.Updater('contactContainer', host+'/affiliates/kontakt/'+formular+':Themen/element',{
	                             onComplete:function(request){
	                                //darstellung der themenauswahl abhängigen elemente
	                                showContent(formular+':Themen:'+ $('affiliateThema').options[$('affiliateThema').selectedIndex].value);
	                                Element.hide('loading');
	                                $('sendbtn').disabled = '';
	                             },
	                             onException:function(request){
	                                Element.hide('loading');
	                                $('sendbtn').disabled = '';
	                                
	                             },
	                             onFailure:function(request){
	                                Element.hide('loading');
	                                $('sendbtn').disabled = '';
	                             },
	                             asynchronous:true,
                                 method:'post',
                                 requestHeaders: ['Content-Type','text/html; charset=UTF-8'],
                                 insertion: Insertion.Bottom
	                         }); 
	                     },		    
	                      evalScripts:true,
	                      asynchronous:true,
	                      method:'post',
                          requestHeaders: ['Content-Type','text/html; charset=UTF-8'],
	                      insertion: Insertion.Bottom}
	                      );
	    
	    
}

function cleanElem(nextstep, depth)
{
    for (i=0;i<=depth;i++){
    try{
        $('step'+parseInt(nextstep+i)).remove();
    }catch(e){}
    }
     /*try{
        $('form-buttons').remove();
     } catch(e){}*/
}

function disableFields(disable, tagname){
    var inputFields = document.getElementsByTagName(tagname);
    for(i=0;i<inputFields.length;i++){
        if(disable){
            inputFields[i].disabled =true;
        } else {
            inputFields[i].disabled =false;
        }
    }

}


function showContent (formular)
{
    
    new Ajax.Updater('contactContainer', host+'/affiliates/kontakt/'+formular+'/element',{
                         onLoading:function(request){  
                                                       
                         },
                         onComplete:function(request){  
                             Element.hide('loading');
                             $('sendbtn').disabled = '';
                         },
                         
                        asynchronous:true,
                        method:'post',
                        requestHeaders: ['Content-Type','text/html; charset=UTF-8'],
                        insertion: Insertion.Bottom
                        });
   
}

function showIt(caller)
{
    var childs = caller.parentNode.childNodes;
    if (window.opnd){
        window.opnd.style.display = 'none';
    }
    childs[2].style.display = 'block';
    
    if (window.opnd && window.opnd == childs[2]) {
        window.opnd = '';
        childs[2].style.display = 'none';
    } else {
        window.opnd = childs[2];
    }
    
}

function showSubmitButton()
{
    try{
        $('sendbtn').remove();
    }catch(e){}
        var html = '<input type="submit" class="link-mbutton-forward" value="Senden" id="sendbtn"/>';
    try{
        new Insertion.Bottom($('form-buttons'),html);
    }catch(e){}
}

function readSelectfields()
{
    var selectFields = document.getElementsByTagName('select');
    if (selectFields.length>1 && window.selectedEvent != ''){
        var selectedEvent = selectFields[1].getAttribute('onchange');
        eval(selectedEvent);
    }
    
    
}


function showInfolayer(caller, infolayer)
{
    window.affiliateInfoPos = findPos(caller);
    
    contactPos = findPos($('contact-form'));

	var topPosition = (window.affiliateInfoPos[1]-contactPos[1]-33);
	var leftPosition = (window.affiliateInfoPos[0]-contactPos[0]+25);
	
	if ( !isMozilla ) {
		topPosition += 10;
		
		switch (infolayer) {
			case "Benutzername":
				leftPosition += 85;
			break;
			case "Interneteingangsnummer":
				leftPosition += 145;
			break;
			case "Bearbeitungsnummer":
				leftPosition += 125;
			break;
		}
	}

    $("infolayer"+infolayer).style.top = topPosition+'px';
    $("infolayer"+infolayer).style.left = leftPosition+'px';
    
    Element.show("infolayer"+infolayer);

}

window.onload = function()
{
	//showCalendar();
}

