﻿/*************************************************************************/
//
//		Landingpage Utilities v 2.0
//		Javascript Document
// 		2009 Copyright (c) GFB & Partner Marketing Services
//		www.gfb.at
//
//		Unauthorized copying is not allowed
//
//		28.10.2009
/*************************************************************************/

	//-------------------------------------------------------------------
	// ADD LISTENER
	// addListener(window, "load", changeContent);
	//-------------------------------------------------------------------
	
	
	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;
			};
		}
	}();
	
	//-------------------------------------------------------------------
	// ADD LISTENER
	// removeListener(window, "load", changeContent);
	//-------------------------------------------------------------------
	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;
			};
		}
	}();
		
	//--------------------------------------------------------------------------------------
	// SET ALL CHECKBOXES
	//--------------------------------------------------------------------------------------
	function fixCheckbox(e) {	
		
		if(e.type == 'click'){	
			globals.partnerList[this.index].IDList[0].checked = this.checked
			globals.partnerList[this.index].cb.checked = this.checked
		}else if (e.type == 'thumbCB'){			
			// id click from checkbox in thumb then do nothing			
		}else{
			//console.log('Object: ', e);
			if(globals.setCB == true){
			globals.partnerList[e.index].IDList[0].checked = true;
			globals.partnerList[e.index].cb.checked = true;
			}
		}
		
		globals.setCB = true;
		
		// variable to set the visible of default text in partner area of form 
		var  selectPartnerError= "block";			
				
		 
		for ( var i = 0; globals.partnerList[i]; i++){	
			
			if (globals.partnerList[i].IDList[0].checked){
				//console.log('formPartnerList: ', globals.formPartnerList[i].parentNode);
				selectPartnerError = 'none';
				globals.formPartnerList[i].parentNode.style.display = "block";
				globals.formPartnerList[i].checked = true;
				globals.partnerList[i].div.className = globals.partnerList[i].div.selectedClassName;
				if(globals.partnerList[i].div.mapPoint){globals.partnerList[i].div.mapPoint.className = globals.partnerList[i].div.mapPoint.selectedClassName;}
			}else{
				//console.log('CB not checked index: ', i);
				globals.formPartnerList[i].parentNode.style.display = "none";
				globals.formPartnerList[i].checked = false;
				globals.partnerList[i].div.className = globals.partnerList[i].div.normalClassName;
				if(globals.partnerList[i].div.mapPoint){globals.partnerList[i].div.mapPoint.className = globals.partnerList[i].div.mapPoint.normalClassName;}
			}
			
			for ( var thisXlayerCB in globals.xLayerCBList ){
				
				if (globals.xLayerCBList[thisXlayerCB].name == globals.partnerList[i].IDList[0].name) {
					//console.log('MATCH XLAYER', globals.partnerList[i].IDList[0].checked);
					globals.xLayerCBList[thisXlayerCB].checked = globals.partnerList[i].IDList[0].checked;
				}
			}
			
		};
		
		
		
		// set the visible of default text in partner area of form 
		document.getElementById("selectPartnerError").style.display = selectPartnerError;
		
		
		
	};
	

	
	//--------------------------------------------------------------------------------------
	// PARTNER HOMEPAGE POPUP
	// Called from xLayer  
	//--------------------------------------------------------------------------------------
	
	var checkPoint;
	
	// Sets variable checkPoint for goPartnerPage()
	function setCheckPoint(wValue, wHotel){
		checkPoint = wValue;
	};
				
	// used to open new browser window 
	function goPartnerPage(wURL){
		// Change "_blank" to something like "newWindow" to load all links in the same new window
		if(arguments.length){checkPoint = wURL;}
		var newWindow = window.open(checkPoint, '_blank');
		newWindow.focus();
	};
		
	//--------------------------------------------------------------------------------------
	// CHILD AGE INPUT FUNCTIONALITY - For multiple children age input
	//--------------------------------------------------------------------------------------
	function initAgeGroup(){
		var ageElement = document.getElementById("form_P6");//options field reference
		var childAgeInput = document.getElementById("childAgeInputFields");		
		hide_LIst(childAgeInput);
		ageElement.onchange = function(){upDateAgeGroup(this)};		
		upDateAgeGroup(ageElement);		
	};
	
	function fillAgeGroups(){
		var childrensAgeField = document.getElementById("form_P7");
		childrensAgeField.value = "";
		var whichUL = document.getElementById("childAgeInputFields");
		var tagList = whichUL.getElementsByTagName("select");
		//skip first ul because it is only a lable
		for(i=0; tagList[i]; i++){
			var inputTag = tagList[i];
			writeSelectValue(inputTag);
			//if(inputTag.value==""){inputTag.value="Kind " + (i)};
		}	
	};
	
	// Fill hidden Field with Children's ages
	// Reporting uses this field
	function writeSelectValue(wSelectElement){	
		for (var i = 0; i < wSelectElement.options.length; ++i){
			if(wSelectElement[i].selected && wSelectElement[i].text != "0"){
				document.getElementById("form_P7").value +=  wSelectElement[i].text +";";				
			}
		}
	};
	
	function upDateAgeGroup (wElement) {
	  	for (i = 0; i < wElement.length; ++i){
			if (wElement.options[i].selected == true){
				if (wElement.options[i].text == "-"){break;}
				var maxRange = eval(wElement.options[i].text)-1;
				if(maxRange < 0){
					hide_LIst(document.getElementById("childAgeInputFields"));
					continue;
				}
				var childAgeInput = document.getElementById("childAgeInputFields");
				show_LIst(childAgeInput, 0, maxRange+1);
				document.getElementById("childAgeInputFields").style.display="block";
				break;
			}	
		}		 	
	};

	//--------------------------------------------------------------------------------------
	// CHECK ERRORS
	//--------------------------------------------------------------------------------------
	
	// V3 Error Check overide
	// Add Error style class .showError and .showErrorBackground to label and Input field respectively	
	// Show Error Text
		
	function checkErrors() {	
		 showErrorFields(document.getElementsByTagName("select"));
		 showErrorFields(document.getElementsByTagName("input"));		 
	};
		
		
	function showErrorFields(tagList, allErrors){
			
		labelObject = new Object();
		var isError = false;
			
		if(allErrors){isError = true;}
		
		// See if V3 has set an error class and then use our class
		for(i=0;i<tagList.length;i++){
			if(tagList[i].className.indexOf("mandatoryerror") > 0 || tagList[i].className.indexOf("validationerror") > 0 || allErrors == true){
			tagList[i].className = "showErrorBackground";
			labelObject[tagList[i].name] = true;
			isError = true;
			}
		}
		
		// change label class if it has a for property 
		// matching property in labelObject
		tagList = document.getElementsByTagName("label");		
		for(i=0;i<tagList.length;i++){	
			tagList[i].className= "";
			if(labelObject[tagList[i].htmlFor] == true){
				tagList[i].className= "showError";
			}
		}	
			
		if(isError){
			var errorString = document.getElementById('errorMessages').getElementsByTagName('li')[0].innerHTML;
			showErrorText(errorString);
		}		
	};
		
	// Custom Check Form Fields
	function checkform ( form ){
		
		var checkFieldsList = [];
		var thisIndex = null;
		for(thisIndex in globals.checkFieldsList.date){checkFieldsList.push(globals.checkFieldsList.date[thisIndex])}
		for(thisIndex in globals.checkFieldsList.required){checkFieldsList.push(globals.checkFieldsList.required[thisIndex])}
			
		// add catalog dependent checkbox fields to checkFieldsList	
		var katalogChecked = false;
		for(var i=0; globals.katalogIDList[i]; i++){			
			if(form[  globals.katalogIDList[i] ]){
				if ( form[  globals.katalogIDList[i] ].checked ){
					katalogChecked = true;
					for(var thisIndex in globals.checkFieldsList.katalog){
						checkFieldsList.push(globals.checkFieldsList.katalog[thisIndex])
					}
					break;
				}
			}
		}		
		
		// Set Default Error Text		
		var errorString = document.getElementById('errorMessages').getElementsByTagName('li')[1].innerHTML;	
				  
		var error = false;
		var firstErrorField;//used to set focus to the first field in error list	
		var idList = [];
		
		
		// Manditory Fields Check
		for (thisField = 0; thisField < checkFieldsList.length; thisField++ ){			
			if (form[checkFieldsList[thisField]].value == "" || form[checkFieldsList[thisField]].value == 0){					
				if(!error){firstErrorField = checkFieldsList[thisField];}	
				idList.push(form[checkFieldsList[thisField]]);	
				error = true;			
			}else{					
				form[checkFieldsList[thisField]].className = "";
			}	
		}
		
		if(error){
			form[firstErrorField].focus();//set focus to the first field in error list
			showErrorFields(idList, true);
		}
		// END MFC
		
		//Partner Required Test
		var inputs = document.getElementById("formular_partner_list").getElementsByTagName('input');
		var partnerChecked = false;
		for (var i=0; inputs[i]; i++) {
				if(inputs[i].checked ){
					partnerChecked = true;
				}	
		}
		if(globals.checkFieldsList.partnerRequired){	
			if(partnerChecked){error = false;}		
		}else{
			partnerChecked = true;
		}// END PRT
		
				
		if(error){
			showErrorText(errorString);
			//console.log('do not send form. check fields');
			return false;
		}else if(partnerChecked){
			//console.log('send form');
			return true;
		}
	};
	
	// Puts error text into Div "formError"
	function showErrorText(errorString){
		var element = document.getElementById("formError");
		element.innerHTML = errorString;
		element.style.color = "rgb(255,0,0)";
	};
		
	//--------------------------------------------------------------------------------------
	// UTILITIES
	//--------------------------------------------------------------------------------------
	
	function getParam(wParam){
		var params = window.location.search.substring( 1 ).split( '&' );
		for(var i=0; params[i]; i++){			
			var segments = 	params[i].split( '=' );			
			if(segments[0] == wParam){
				return segments[1];
			}
		}
	};
	
	function getPartnerIndex(wKey, wValue){	
		for(thisIndex in globals.partnerList){	
			var entry = globals.partnerList[thisIndex][wKey];
			if(entry){
				if(entry == wValue){return thisIndex}
			}else{
				return -1;	
			}	
		}
	};
	
	//-------------------------------------------------------------------
	// SHOW / HIDE LIST ELEMENTS
	//-------------------------------------------------------------------
	function hide_LIst(whichUL){
		var tagList = whichUL.getElementsByTagName("li");	
		for(i=0; tagList[i]; i++){
			tagList[i].style.display="none"; 
		}		
	};
	
	function show_LIst(whichUL, wRangeMin, wRangeMax){
		hide_LIst(whichUL);
		var tagList = whichUL.getElementsByTagName("li");
		for(var i=wRangeMin; i<=wRangeMax; i++){
			tagList[i].style.display="block";	
			var selectList = tagList[i].getElementsByTagName("select");		
			if(selectList.length){
				selectList[0].onchange = function(){fillAgeGroups()};
			}
		}
	};
	
	//-------------------------------------------------------------------
	// GET STYLE OF ELEMENT
	//-------------------------------------------------------------------
	function getStyle(el,styleProp){
		if (el.currentStyle)
			var y = el.currentStyle[styleProp];
		else if (window.getComputedStyle)
			var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
		return y;
	};

//-------------------------------------------------------------------
// AJAX
//-------------------------------------------------------------------

ajaxClass = function() {	

    this.http;
	
    try {
        this.http = new XMLHttpRequest();
    }
    catch(e) {
        var msxml = [
            'MSXML2.XMLHTTP.3.0', 
            'MSXML2.XMLHTTP', 
            'Microsoft.XMLHTTP'
        ];
        for ( var i=0, len = msxml.length; i < len; ++i ) {
            try {
                this.http = new ActiveXObject(msxml[i]);
                break;
            }
            catch(e) {}
        }
    }
	
	// Request Handling
	this.handleReadyState = function (o, callback) {
        var poll = window.setInterval(function() {
            if(o && o.readyState == 4) {
                window.clearInterval(poll);
                if ( callback ){
                    callback(o);
                }
            }
        },
        50);
    }
	
	// Request
    this.asyncRequest =  function(method, uri, callback, postData) {
        this.http.open(method, uri, true);
		this.http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        this.handleReadyState(this.http, callback);
        this.http.send(postData || null);
        return this.http;
    };
};

var asyncRequest = new ajaxClass();


//-------------------------------------------------------------------
// RANDOMIZE DIV ORDER
//-------------------------------------------------------------------
	function randomLayerOrder(wID) {	
		var obj = document.getElementById(wID);	
		if(!obj){return null;}
		var divList = obj.getElementsByTagName('div');
		var myParentContainer;
		var spacerNode;
				
		//Make Old Array
		var currentLayers=new Array();
		//new array order
		var newLayerOrder = new Array();
				
		for(var i=divList.length-1; i >= 0; i--){
			
			var thisNode = divList[i];
			if (thisNode.className.indexOf('clearer') >= 0 ){spacerNode=thisNode.parentNode.removeChild(thisNode);continue;}
			if (thisNode.className.indexOf('cbParent') >= 0 ){				
				myParentContainer = thisNode.parentNode;				
				currentLayers.push(thisNode.parentNode.removeChild(thisNode));
			}
		}	
		
		
		while(currentLayers.length > 0){
			var randomNum = Math.floor( Math.random()*currentLayers.length);
			myParentContainer.appendChild(currentLayers[randomNum]);
			currentLayers.splice(randomNum, 1);				
		}	
		
		if(spacerNode){myParentContainer.appendChild(spacerNode);}
	};
	
//-------------------------------------------------------------------
// SCROLL PAGE
//-------------------------------------------------------------------
function getElementPos( oElement ) {
  if( typeof( oElement.offsetParent ) != 'undefined' ) {
    for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
      posX += oElement.offsetLeft;
      posY += oElement.offsetTop;
    }
    return {x:posX, y:posY };
  } else {
    return {x:oElement.x, y:oElement.y };
  }
};

function getScrollXY() {
  var scrX = 0, scrY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrY = window.pageYOffset;
    scrX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrY = document.body.scrollTop;
    scrX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrY = document.documentElement.scrollTop;
    scrX = document.documentElement.scrollLeft;
  }
  return {x:scrX, y:scrY };
};

function scroller(){
	
	this.currentPos = 0;
	this.endPos = 0;
	this.dist = this.endPos;	

	this.nextPos =  function(e){	
		var distToMove = this.dist/15;
		this.currentPos -= distToMove;
		if ( this.currentPos >= this.endPos ){
			clearInterval(this.intervalID);
			this.currentPos = this.endPos;
			}
		window.scrollTo(0, this.currentPos);
	};
	
	this.startScroll = function(point){
		this.currentPos = point.start;
		this.endPos = point.end;
		this.dist = this.currentPos - this.endPos;
		if(this.intervalID){
			clearInterval(this.intervalID);
			}
		this.intervalID = setInterval("scrollWindow()",10);
		};

};

var windowScroller = new scroller();
function scrollWindow(e){windowScroller.nextPos()};

//--------------------------------------------------------------------------------------
// UTILITIES
//--------------------------------------------------------------------------------------

function XlayerToRegion(wXlayer){
	for(var i = 0; globals.partnerList[i]; i++){
		if 	(globals.partnerList[i].xlayer == wXlayer){return globals.partnerList[i].region}
	}
	
}

function XlayerToPartner(wXlayer){
	for(var i = 0; globals.partnerList[i]; i++){
		if 	(globals.partnerList[i].xlayer == wXlayer){return globals.partnerList[i].div}
	}
	
}

//--------------------------------------------------------------------------------------
// DISPATCHT EVENT
//--------------------------------------------------------------------------------------
function sendClickEvent(wObj)
{
	if( document.createEvent ) {
		var evObj = document.createEvent('MouseEvents');
		evObj.initEvent( 'click', true, true );
		wObj.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
  		wObj.fireEvent('onClick');
	}
}

//--------------------------------------------------------------------------------------
// KATALOG ON/OFF SWITCH
//--------------------------------------------------------------------------------------
function showAddressFields() {
	
	var display = "none";
	// for(var i=0; i < globals.katalogIDList.length; i++){		
	for(var i=0; globals.katalogIDList[i]; i++){		
	
		if(document.forms[0][ globals.katalogIDList[i] ]){
			if(document.forms[0][ globals.katalogIDList[i] ].checked == true){ display = "block";}
		}else{alert("Catalog id " + globals.katalogIDList[i] + " is incorrect");}
	
	}
	
	var collection = document.getElementById("pers_daten").getElementsByTagName("div");
	
	for(var i=0;collection[i];i++){
		if(collection[i].className == "kat"){
		collection[i].style.display = display;
		
		crossDomainIframeResize();
		}
	}
};


// Custom onClick for Ordering Catalog
function catalogClick(){
	
	for(var i=0; globals.katalogIDList[i]; i++){		
		if(document.forms[0][  globals.katalogIDList[i] ]){
			addListener(document.forms[0][  globals.katalogIDList[i] ], "click", showAddressFields);
		}
	
	}
	
	
}

//--------------------------------------------------------------------------------------
// INSERT FLASH CONTENT
// Requires swObject 2.0
//--------------------------------------------------------------------------------------
	
function insertFlashHeader(){
	
	var id = "sujet";	
	var flashURL = globals.headerFlashPath;
			
	var flashvars = {};	
	flashvars.files = globals.headerFlashPicts;
	flashvars.checkpoint = globals.headerFlashCheckpoint;
	flashvars.path = globals.headerFlashDataFolder;
	flashvars.fadeSpeed = "2";	
	flashvars.pauseTime = "5";
		
	var params = {};
	params.allowfullscreen = "false";
	params.wmode = "transparent";
	params.scale = "noscale";
	params.align = "left";
	params.salign = "lt";
		
	var attributes = {};
	attributes.id = id;
	attributes.name = id;
		
	swfobject.embedSWF(flashURL, id, globals.headerFlash.width, globals.headerFlash.height, globals.headerFlash.version, "", flashvars, params, attributes);

};

function insertXlayerFlash(wDiv){
	
	
	var id = wDiv.id;	
	var flashURL = globals.xLayerFlashPath;
	
	var partnerIndex = getPartnerIndex('xlayer',globals.xLayerNum);
	
	var filesString = "";
	if(partnerIndex >= 0){
		filesString=globals.partnerList[partnerIndex].flashPicts;
		}
		
	var checkPoint = "";
	if(partnerIndex >= 0){
		checkPoint=globals.partnerList[partnerIndex].checkPoint;
		}	
		
	
	var flashvars = {};	
	flashvars.files = filesString;
	flashvars.checkpoint = checkPoint;
	flashvars.path = globals.xLayerFlashDataFolder;
	flashvars.fadeSpeed = "2";	
	flashvars.pauseTime = "5";
		
	var params = {};
	params.allowfullscreen = "false";
	params.wmode = "transparent";
	params.scale = "noscale";
	params.align = "left";
	params.salign = "lt";
		
	var attributes = {};
	attributes.id = id;
	attributes.name = id;
		
	swfobject.embedSWF(flashURL, id, globals.xLayerFlash.width, globals.xLayerFlash.height, globals.xLayerFlash.version, "", flashvars, params, attributes);
	
	
};


// EOS	