var EventManager = {
	addListener:function() {
		if ( window.addEventListener ) {
			return function(el, type, fn) {
				el.addEventListener(type, fn, false);
			};
		} else if ( window.attachEvent ) {

			return function(el, type, fn) {
				var f = function() {
					caller = window.event;
					caller.target = window.event.srcElement;
					caller.pageX = window.event.clientX + document.body.scrollLeft ;
					caller.pageY = window.event.clientY + document.body.scrollTop ;
					fn.call(el, caller);
				};
				if(!el.FunctionsList) el.FunctionsList = [];
				el.FunctionsList.push([fn, f]);
				el.attachEvent('on'+type, f);
			};
		} else {
			// old Browser only 1 Event!!
			return function(el, type, fn) {
				element['on'+type] = fn;
			}
		}
	}(),
	
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

	removeListener:function() {
		if ( window.removeEventListener ) {
			return function(el, type, fn) {
				el.removeEventListener(type, fn, false);
			};
		} else if ( window.detachEvent ) {
			return function(el, type, fn) {
				for(i in el.FunctionsList){
					if(el.FunctionsList[i][0] == fn){
						el.detachEvent('on'+type, el.FunctionsList[i][1]);
						el.FunctionsList.splice(i,1);
						break;
					}
				}

			};
		} else {
			// old Browser only 1 Event!!
			return function(el, type, fn) {
				el['on'+type] = null;
			}
		}
	}()
};





//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

var blinkState = true;
var intervalId = setInterval("blink()", 1000)
function blink(){
blinkState = !blinkState
var blinker = document.getElementById("blinker");
if(blinker != null){
	if (blinkState){		
		blinker.style.color = blinkerColor2;
	}else{
		blinker.style.color = errorColor;
	}
}
}



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//var newString = Url.decode(string);

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


function fillForms() {

var formList = new Array();
     
	if(window.location.search != "") {
     QUERYSTRING = window.location.search
     QUERYSTRING = QUERYSTRING.replace(/\?/,"");

     //trenne den querystring nach jedem & in teilstringe auf
     var teilstrings = QUERYSTRING.split("&");	 

     //durchlaufe jeden dieser teilstrings
     for (i=0;i<teilstrings.length;i++) {

         //trenne den teilsring in weiteren teilstring auf,
         //trenne also formListnamen vom inhalt
         vararr = teilstrings[i].split("=");
         formList[vararr[0]] = vararr[1];
         
         //Ausnahme
         

	}
}

        var formElementsCol = document.anfrage.elements
	
	for(thisElement=0; thisElement < formElementsCol.length;thisElement++)
	{
		var currentElement = formElementsCol[thisElement];

		var thisValue = formList["an_"+ currentElement.name];
		// verhindern das bereits vorhandene Fields überschrieben werden
		if(currentElement.value.length && !currentElement.value){
			continue;
		}

		if (thisValue != undefined)
		{
			//thisValue = unescape(thisValue);
			thisValue  = Url.decode(thisValue);
			
			switch(formElementsCol[thisElement].type)
			{
				case "submit": break;
				case "select-one":      currentElement.value            = thisValue ;break;
				case "radio":           currentElement.checked          = thisValue ;break;
				case "text":            currentElement.value            = thisValue ; break;
				case "checkbox":        currentElement.checked          = thisValue ; break;	
				case "hidden":	
				break;
			}
		}
   }
   
  
};


function resetFormList(){
        formList = [];
        var formElementsCol = document.anfrage.elements
        for(thisElement=0;formElementsCol[thisElement];thisElement++)
        {
                var currentElement = formElementsCol[thisElement];
                thisValue = "";
                switch(currentElement.type)
                        {
                        case "submit": break;
                        case "select-one":      thisValue = currentElement.value                ; break;
                        case "radio":           thisValue = ""                                  ; break;
                        case "text":            thisValue = currentElement.value                ; break;
						case "hidden":          thisValue = currentElement.value                ; break;
                        case "checkbox":        thisValue = currentElement.checked              ; break;
                        }
                if(thisValue!="")formList[currentElement.name] = thisValue ;

        }
                return true;
};



//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function checkErrors() {
	
        var errorCheck1 = showErrors(document.getElementsByTagName("select"));
        var errorCheck2 = showErrors(document.getElementsByTagName("input"));




        if( errorCheck1 || errorCheck2){
        	return false;
        }else{
			return true;
		}
}



function showErrors(tagList){	

        labelObject = new Object();
		
                var errors = false;

/*
				 for(thisLabelName in inputList){
					if ( ("form_P3 form_P12 form_P6 form_P7 form_P8").indexOf(inputList[thisLabelName]) >= 0){
					inputList.splice(thisLabelName, 1);					
					}
				 }
				
*/
				if(document.anfrage['form_P2'].checked) 
				{
				
				   inputList.push("form_ADDRESS", "form_STREETNUMBER", "form_ZIPCODE", "form_CITY");
				   
				}else{
				
				var listLength = inputList.length;
				
				for(var thisFormIndex = listLength; thisFormIndex >= 0 ; thisFormIndex --){
					
				
					if ( ("form_ADDRESS form_STREETNUMBER form_ZIPCODE form_CITY ").indexOf(inputList[thisFormIndex]) >= 0){
					inputList.splice(thisFormIndex, 1);
					
					}
				
				}
				
				}



                for(i=0;i<tagList.length;i++){
                        for(var j=0; inputList[j]; j++){

                              if(inputList[j] == "form_EMAIL" && tagList[i].name == "form_EMAIL"){
                                        if(checkEmail(tagList[i].value)){
                                                document.getElementById('formularerror').style.display='block';
                                                labelObject[tagList[i].name] = true;
                                                errors = true;
                                                }
                                        }

if ( externalLabelList.indexOf(tagList[i].name) >= 0 && tagList[i].value.length < 1 ){labelObject['form_Anreise'] = true;}

                                if( tagList[i].name == inputList[j] && tagList[i].value.length < 1 ){								
                                document.getElementById('formularerror').style.display='block';
                                labelObject[tagList[i].name] = true;
                                errors = true;
                                }

                                if(document.getElementById('form_SALUTATION').value == 0){
                                document.getElementById('formularerror').style.display='block';
                                labelObject['form_SALUTATION'] = true;

                                errors = true;
                                        }



                        }

                }

                tagList = document.getElementsByTagName("label");
                                

                for(i=0;i<tagList.length;i++){
                        
                        
                        tagList[i].style.color = resetColor;
                        

                        if(labelObject[tagList[i].htmlFor] == true){tagList[i].style.color = errorColor};


						/*if( tagList[i].htmlFor == "form_Anreise"){
							

							for(var j=0; externalLabelList[j];j++){
								console.log(labelObject[externalLabelList[j]]);
								if(labelObject[externalLabelList[j]] == true){tagList[i].style.color = errorColor};
							}
						}*/


                }


                        return errors;


        }
//########################################################################
//   VALIDATE EMAIL
//########################################################################

function checkEmail (strng) {
        var error=false;
        if (strng == "") {
           //error = "You didn't enter an email address.\n";
           error = true;
        }

                var emailFilter=/^.+@.+\..{2,3}$/;
                if (!(emailFilter.test(strng))) {
                   //error = "Please enter a valid email address.\n";
                   error = true;
                }
                else {
        //test email for illegal characters
                   var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
                         if (strng.match(illegalChars)) {
                          //error = "The email address contains illegal characters.\n";
                          error = true;
                   }
                }
        return error;
}




EventManager.addListener(window,'load',fillForms);