var skyD = document,
	skyAjax = {

	/** Returns XMLHttpRequest instance. */
	getR: function(){
		var r;

		if (window.XMLHttpRequest)
			r = new XMLHttpRequest();
		else if (window.ActiveXObject)
			r = new ActiveXObject('Microsoft.XMLHTTP');

		return r;
	},
			
	/** Enables/disables anination e=true/false. */
	doAnim: function(e) {
		skyD.getElementById('loader').style.display = e ? '' : 'none';
	},		
			
	/** Performs AJAX call. */
	ajax: function(u, p, c) {
		var t = this, r = t.getR(), d = '';

		if (!r)
			return;

		r.onreadystatechange = c ? function() { t.onChange(r, c); } : null;

		if (p) {
			for (var n in p) {
				if (d) d += '&';

				d += n + '=' + encodeURI(p[n]);
			}
		}
		t.doAnim(1);	
		
		r.open('POST', u, true);
		r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		r.setRequestHeader("Content-length", d.length);
		r.setRequestHeader("Connection", "close");
		r.send(d);
	},

	/** Handles AJAX response. */
	onChange: function(r, c) {		
			if (r.readyState != 4) return;			
			this.doAnim(0);						
			if (r.responseXML.getElementsByTagName('root')[0]) {	
				if(r.responseXML.getElementsByTagName('root')[0].childNodes.length == 1 && r.responseXML.getElementsByTagName('root')[0].childNodes[0].nodeName == 'message'){					
					skyD.getElementById("buildContent").innerHTML = r.responseXML.getElementsByTagName('root')[0].childNodes[0].attributes[0].nodeValue;
					return;
				}	
				else{	
					c(r);		
					return;
				}	
			}
			this.onError();			
	},

	/** Hanles errors. */
	onError: function (e) {
		alert(e ? e : 'Internal error.');
	}
}


function buildPopup(productType, shopId, day, productAge) {	
	try {		
			skyAjax.ajax('../../buildContentPopup.php', {'productType': productType, 'shopId': shopId, 'day': day, 'productAge': productAge}, function (r) { skyGoPopup(r); return 1; });
	}	
	catch(e) {
		skyAjax.onError();
	}
}

function skyReplaceStr(s1, s2, s3){
	if(skyTrim(s2) == "")
		s2 = " ";
	var s = "", i;	
	while(true){
		i = s1.indexOf(s2)
		if(i >= 0){
			s = s + s1.substr(0, i) + s3;
			s1 = s1.substr(i + s2.length);
		}
		else
			break;
	}
	return s + s1;	
}

function skyTrim(s) {
	return s.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
}

/** Check  validation of csv file */
function validation_form(form) {
	if(typeof(form) == 'object') {
		if(skyTrim(form['fileCsv'].value) != '') {	
			if(form['fileCsv'].value.split(".")[1] != 'csv') {				
				skyD.getElementById("fileCsvMessage").style.display = '';				
				return false;
			}
			return true;
		}
	}	
		else   			
			skyD.getElementById(form + 'Message').style.display = 'none';
	return false;
	
} 


function getYCoordinateScroll() {
	yScroll = 0;

	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX)
	{
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = skyD.documentElement;
		var wff = (deff&&deff.clientWidth) || skyD.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || skyD.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} 
	else if (skyD.body.scrollHeight > skyD.body.offsetHeight || skyD.body.scrollWidth > skyD.body.offsetWidth)
		{ // all but Explorer Mac
			yScroll = skyD.body.scrollHeight;
			xScroll = skyD.body.scrollWidth;
		} 
		else 
		{ // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			yScroll = skyD.body.offsetHeight;
			xScroll = skyD.body.offsetWidth;
		}
	return yScroll;
}


function createContentForm(id) {	
	if(id == 0){
		var html = '';		
		html += '<table border="0" cellspacing="0" cellpadding="0">';	
		html += '<tr><td align="center">' + words_en['Loading'] + '<td></tr>';
		html += '<tr><td align="center"><img src="../images/loading.gif" border="0"/></td></tr>';	
		html += '</table>';		
		skyD.getElementById("formPopupInner").innerHTML = html;		
	}	
	showPopupForm(24,id);
}


function hidePopupForm(id) {	
	if(id == 0)
		skyD.getElementById("formPopup").style.display = 'none';
	//if(skyD.getElementById("backgroundDisplayForm").style.display)	
		skyD.getElementById("backgroundDisplayForm").style.display = 'none';
		skyD.getElementById('kat_wahl_box').style.display = "none";		
}

function showPopupForm(width, id) { 			
	if(id == 0){
		skyD.getElementById("formPopup").style.display = 'block';		
		skyD.getElementById("formPopup").style.left = 50 - width / 2 + '%';
		skyD.getElementById("formPopup").style.top = skyD.body.clientHeight / 2 - (skyD.getElementById("formPopup").offsetHeight / 2 + 100) + 350 + "px";
		skyD.getElementById("formPopup").style.width = width + "%";	
	}	
	//if(id == 0)
		//skyD.getElementById("backgroundDisplayForm").style.height = 100 + '%';
	//else	
		skyD.getElementById("backgroundDisplayForm").style.height = getYCoordinateScroll() + 'px';
	skyD.getElementById("backgroundDisplayForm").style.width = 100 + '%';
	skyD.getElementById("backgroundDisplayForm").style.display = 'block';
}

function checkDays() {
	return(skyD.getElementById("Rdays").value);
}

function checkPersons() {	
	return(skyD.getElementById('personenid').value);
}


function showPerson(n) {	
	var i = 1;
	var mkt = 0;
	var online = 0,one,two;
	var countPersons = checkPersons();
	while(i <= countPersons) {
		if(skyD.getElementById('person'+i).style.display == "block") {		
			one    = skyD.getElementById("MktPrice" + i).innerHTML;			
			two    = skyD.getElementById("OnLine" + i).innerHTML;
			one    = one.escapeString();			
			two    = two.escapeString();			
			mkt    = parseFloat(mkt) + parseFloat(one);
			online = parseFloat(online) + parseFloat(two);
		}
		i=i+1;
	}	
	var onDiscount = mkt-online;
	onDiscount = Math.round(onDiscount*n)/n;
	online = Math.round(online*n)/n;
	if(n == 100)
		mkt = Math.round(mkt*n)/n;
		
	skyD.getElementById("priceWithout").innerHTML = "€ " + mkt +",-";
	skyD.getElementById("priceDisount").innerHTML = "€ " + onDiscount +",-";
	skyD.getElementById("finalPrice").innerHTML   = "€ " + online +",-";
	
	skyD.getElementById("finalPricehidden").value = online;

	skyD.getElementById("hidden_price_without_rate").value = mkt;
	skyD.getElementById("hidden_tot_discount").value = onDiscount;
	hidePopupForm(0);	
}



function changeValue(productType, productStars, cat, discount, catid) {		
	
	var nameProduct = '';
	var typeProduct = productType == 1 ? words_en['fur_Erwachsene'] : (productType == 2 ? words_en['fur_Jugendliche'] : (productType == 3 ? words_en['fur_Kinder'] : ""));
	nameProduct += "  " + cat + " " + typeProduct;	
	var countDay = checkDays();		
	var block = countBlock;
	
	
	var day	 = skyD.getElementById("day" + countDay + "_price_" + catid).value;	
	var ss=percent(discount, day, 1, 10);
	var tt=day-ss;
		tt=Math.round(tt*100)/100;
		
	skyD.getElementById("MktPrice"+block).innerHTML="€ "+day+",-";
	
	skyD.getElementById("OnLine"+block).innerHTML="€ "+tt+",-";	
	skyD.getElementById("mktprice_hidden_"+block).value = day;
	skyD.getElementById("online_hidden_"+block).value=tt;
	skyD.getElementById("catProductId" + block).innerHTML = cat;
	skyD.getElementById("catName"+block).innerHTML = nameProduct;	
	skyD.getElementById("checkCatName" + block).value = 1;			
	
	skyD.getElementById("Cat" + block).value = catid;	
	showPerson(100);

	box_schliessen();
	
	if(numberPrevious < 4){
		skyD.getElementById("subSki" + numberPrevious).style.display = "none";
		skyD.getElementById("subDelimeter" + numberPrevious).style.display = "none";
		skyD.getElementById("subSnowboard" + numberPrevious).style.display = "none";
	}	
}




function forsession(productAge, productStars, productType, day,discount,popid,catid) {	
	
	var nameProduct = '';
	var typeOfUser = productAge == 1 ? words_en['fur_Erwachsene'] : (productAge == 2 ? words_en['fur_Jugendliche'] : (productAge == 3 ? words_en['fur_Kinder'] : ""));	
	var cat =  equipment_en[productStars][productType];	
	nameProduct = " " + cat + " " + typeOfUser;		
	
	var ss=percent(discount, day, 1, 10);
	var tt=day-ss;
	tt=Math.round(tt*100)/100;
	var block = popid;	
	skyD.getElementById("MktPrice"+block).innerHTML="€ "+day+",-";	
	skyD.getElementById("OnLine"+block).innerHTML="€ "+tt+",-";

	skyD.getElementById("mktprice_hidden_"+block).value=day;
	skyD.getElementById("online_hidden_"+block).value=tt;	
	skyD.getElementById("catProductId" + block).innerHTML = cat;
	skyD.getElementById("catName" + block).innerHTML = nameProduct;
	skyD.getElementById("checkCatName" + block).value = 1;			
	
	skyD.getElementById("Cat"+block).value=catid;			
	showPerson(100);
}


function duration(duration) {	
	
	var countPerson = checkPersons();
	var i=1, a, day_id, discount_id, catnam, day_price_val , discount_val , ss, tt;
		while(i <= countPerson)
		{  
			if(skyD.getElementById('person'+i).style.display == "block")
			{					
				if(skyD.getElementById('Cat'+i).value > 0)
				{				
					catnam = skyD.getElementById("Cat" + i).value;					
					a = skyTrim(catnam);
					
					day_id = "day" + duration + "_price_" + a;					
					discount_id = "discount_" + a;
				
					day_price_val = skyD.getElementById(day_id).value;
					discount_val = skyD.getElementById(discount_id).value;
					
					day_price_val = Math.round(day_price_val*100)/100;
					skyD.getElementById("MktPrice"+i).innerHTML="€ "+day_price_val+",-";

					ss = percent(discount_val, day_price_val, 1, 10);					
					tt = Math.round((day_price_val-ss)*100)/100;					
					skyD.getElementById("MktPrice"+i).innerHTML="€ "+day_price_val+",-";
					skyD.getElementById("OnLine"+i).innerHTML="€ "+tt+",-";

					skyD.getElementById("mktprice_hidden_"+i).value=day_price_val;
					skyD.getElementById("online_hidden_"+i).value=tt;
				}
			}
			i=i+1;
		}
	showPerson(100);
}


String.prototype.escapeString = function (){
	return this.replace(/€/,'').replace(/,/,'').replace(/-/,'');
	//return this.replace(/&euro;/,'').replace(/,/,'').replace(/-/,'');
}

function changeSubMenu(id) {
		
	for(var i = 1; i < 4; i++){
		skyD.getElementById("subSki" + i).style.display = "none";
		skyD.getElementById("subDelimeter" + i).style.display = "none";
		skyD.getElementById("subSnowboard" + i).style.display = "none";
	}		
	
	if(id < 4){
		skyD.getElementById("subSki" + id).style.display = "";
		skyD.getElementById("subDelimeter" + id).style.display = "";
		skyD.getElementById("subSnowboard" + id).style.display = "";
	}	
}


function showMainContent(id,number) {
		
	changeSubMenu(number);	
	switch(id){
		case "mainMenuLink1":
			showSubContent("subSki1", number, "linkSubSki", false);
			break;
		case "mainMenu2":
			showSubContent("subSki2", number, "linkSubSki", false);
			break;
		case "mainMenu3":
			showSubContent("subSki3", number, "linkSubSki", false);
			break;
		default :
			showSubContent("subSki4", number, "linkSubSki", false);
			break;			
	}	
}

function liteLink(id,action,number) {
	if(action)
		skyD.getElementById(id).style.background = "#002B71";
	else {		
		if(number != numberPrevious)
			skyD.getElementById(id).style.background = "#629DFF";
	}
}

function skyGoPopup(obj) {
	
	var html = '';
	var response = obj.responseXML.getElementsByTagName('root')[0];	
	var count = response.childNodes.length;		
	var productType = -1, productStars = 0, type = 0;
	for(var i = 0; i < count; i++) {
		html += '<tr>';
		html += '<td bgcolor="#ffffff" colspan="2">';
		html += '<table border="0" width="500px" cellpadding="0" cellspacing="0" >';
		if(productType != response.childNodes[i].childNodes[5].attributes[0].nodeValue) {
			html += '<tr>';
			productType = parseInt(response.childNodes[i].childNodes[5].attributes[0].nodeValue);			
			switch(productType){
				case 1:
					nameCatProduct = words_en['Ski'];
					break;
				case 2:
					nameCatProduct = words_en['Ski_Schuhe'];
					break;			
				case 3:
					nameCatProduct = words_en['Skischuh'];
					break;				
				case 4:
					nameCatProduct = words_en['Snowboard'];
					break;					
				case 5:
					nameCatProduct = words_en['Snowboard_Boots'];
					break;							
				case 6:
					nameCatProduct = words_en['Snowboardboots'];
					break;			
				case 11:
					nameCatProduct = words_en['Helm'];
					break;				
				default:
					nameCatProduct = 'Undefined';
					break;
			}			
			html += '<td style="border-top: 1px solid #7B7B7B ;border-bottom: 0px solid #7B7B7B;" bgcolor="#EFEFEF"><b>' + nameCatProduct + '</b></td>';
			html += '</tr>';			
		}
		
		productStars = response.childNodes[i].childNodes[3].attributes[0].nodeValue;
		html += '<tr>';
		html += '<td style="border-top: 0px solid #7B7B7B ;border-bottom: 1px solid #7B7B7B;" bgcolor="#EFEFEF"></td>';
		html += '</tr>';	
		html += '<tr style="cursor:pointer;background:#629DFF;"onMouseOver="this.style.backgroundColor=\'#ffffff\';" onMouseOut="this.style.backgroundColor=\'#629DFF\';" onClick="changeValue(' + response.childNodes[i].childNodes[8].attributes[0].nodeValue + ',' +  response.childNodes[i].childNodes[3].attributes[0].nodeValue + ',\'' + equipment_en[productStars][productType] + '\',' + response.childNodes[i].childNodes[6].attributes[0].nodeValue + ',' + response.childNodes[i].childNodes[0].attributes[0].nodeValue + ');" style="cursor: pointer">';
		html += '<td>';
		html += '<table border="0" width="500px" cellpadding="0" cellspacing="0">';
		html += '<tr>';
		html += '<td>';
		html += '<table border="0" width="100%" cellpadding="0" cellspacing="0">';		
		html += '<tr>';		
		html += '<td width="40%" align="left">&nbsp;';		
		html += equipment_en[productStars][productType];
		html += '</td>';
		html += '</tr>'
		html += '</table>';
		html += '</td>';
		html += '<td width="30%">';
		html += '<table border="0" cellpadding="0" cellspacing="0">';
		html += '<tr>';
		html += '<td align="center"><strike><font color="#990000"><b>' + words_en['Shop_Preis'] + ' ' + words_en['Euro'] + ' ' + Math.round(response.childNodes[i].childNodes[7].attributes[0].nodeValue * 100) / 100 + '</b></font></strike></td>';
		html += '</tr>';								
		html += '<tr>';
		html += '<td align="center"><b>SilvrettaSports ' + words_en['Preis'] + ' ' + words_en['Euro'] + '</b>';
		var ss=percent(response.childNodes[i].childNodes[6].attributes[0].nodeValue,response.childNodes[i].childNodes[7].attributes[0].nodeValue, 1, 10);
		var tt=Math.round((response.childNodes[i].childNodes[7].attributes[0].nodeValue - ss)*100)/100;
		html += '<b>' + tt + '</b></td>';
		html += '</tr>';
		html += '</table>';
		html += '</td>';
		html += '</tr>';
		html += '</table>';							
		html += '</td>';
		html += '</tr>';			
	}				
	skyD.getElementById("buildContent").innerHTML = html;
	createContentForm(1);
}


function box_schliessen () {
	changeSubMenu(numberPrevious);
	skyD.getElementById('kat_wahl_box').style.display = "none";
	hidePopupForm(1);
}

function percent(number, whole, inverse, rounder){	
	whole = parseFloat(whole);
	if(!whole)
		whole = 100;
	number = parseFloat(number);
	if(!number)
		number = 0;
	if(!whole || !number)
		return 0;
	rounder = parseFloat(rounder);
	rounder =( rounder && (!(rounder%10) || rounder==1) )? rounder:100;
	return (!inverse) ? Math.round( ((number*100)/whole) *rounder)/rounder : Math.round( ((whole*number)/100) *rounder)/rounder;
}


function popupe(Ereignis, personnr, xbox, ybox) {	
	
	var day = skyD.getElementById("Rdays").value;
		countBlock = personnr;
		xbox = definePosition(document.getElementById("OutwardDate")).x - 100;
		skyD.getElementById('evenBlock').value = personnr;
		skyD.getElementById('kat_wahl_box').style.display = "block";
	if (!Ereignis)
		Ereignis = window.event;
	if (skyD.getElementById) {
		skyD.getElementById("kat_wahl_box").style.left = xbox + "px";
		skyD.getElementById("kat_wahl_box").style.top = ybox + "px";
	} else if (skyD.all) {
		skyD.all.kat_wahl_box(skyD.write(day)).style.left = Ereignis.xbox;
		skyD.all.kat_wahl_box(skyD.write(day)).style.top = Ereignis.ybox;
	}
	
	skyD.getElementById("subSki1").style.display = "";	
	skyD.getElementById("subSnowboard1").style.display = "";	
	skyD.getElementById("subDelimeter1").style.display = "";
	
	showSubContent("subSki1", 1, "linkSubSki", false);	
	changeSubMenu(1);		
}

function goPage(url) {
	location.href = url;
}

function createMessageDiv(id, message, addPixel){
	var html = '';
		html += '<table border="0" cellpadding="0" cellspacing="0">';
		html += '<tr>';	
		html += '<td align="right"><img src="../images/conner.gif"></td><td style="padding: 1px;z-index: 100;border: 1px solid #ff0000;cursor: default;-moz-border-radius: 6px;" id="messageDiv' + id + '" bgcolor="#ffffff"><div id="messageE' + id + '" style="color:#ff0000;font-size: 12px;">' + message + '</div></td>';
		html += '</tr>';		
		html += '</table>';
	if(skyD.getElementById("positionMessageDiv" + id)){
		skyD.getElementById("messageE" + id).innerHTML = message;  
		skyD.getElementById("positionMessageDiv" + id).style.display = 'block';
	}	
	else {
		var external = document.createElement('div');
			external.id = "positionMessageDiv" + id;
			external.style.position = "absolute";
			external.style.zIndex = "100";
			external.style.display = "none";
			skyD.body.appendChild(external);
			skyD.getElementById("positionMessageDiv" + id).innerHTML = html;
		
		var positionElement = document.getElementById(id);				
			external.style.display = 'block';
			var dx = skyD.getElementById(id).offsetWidth + addPixel;
			var dy = skyD.getElementById(id).offsetHeight / 2 - 5;
		
			skyD.getElementById("positionMessageDiv" + id).style.left = definePosition(positionElement).x + dx + 'px';
			skyD.getElementById("positionMessageDiv" + id).style.top = definePosition(positionElement).y - dy + 'px';
		}	
}

function hideMessageDiv(id) {	
	if(skyD.getElementById("positionMessageDiv" + id))
		skyD.getElementById("positionMessageDiv" + id).style.display = 'none';
}

function chkform(){

	var weiter 	 = true;
	var persons  = skyD.getElementById('personenid').value;	
	var start_dt = skyD.getElementById('OutwardDate').value;
	if(skyTrim(start_dt) == ''){
		createMessageDiv('OutwardDate', words_en['wahlen_ersten_Verleihtag'], 0);		
		weiter = false;
	}	
	
	var name_title, person_name, pnames, catname_id,search_cat, groesse, schuhgroesse, gewicht;
	var addPixel = 0;
	for(var i = 1; i <= persons; i++) {		
		person_name  = skyD.getElementById('pname'+i).value;
		catname_id   = skyD.getElementById('catName'+i).innerHTML;				
		groesse      = skyD.getElementById('pgroesse'+i).value;
		schuhgroesse = skyD.getElementById('pschuhgroesse'+i).value;
		gewicht      = skyD.getElementById('pgewicht'+i).value;
		
		if(skyTrim(person_name) == ''){							
				createMessageDiv('pname' + i, words_en['Namen_eingeben'] + i + words_en['Namen_eingeben_two'], 0);				
				weiter = false;			
		}
		
		catname_id = skyD.getElementById('checkCatName'+i).value;						
		if(catname_id == 0) { 	
			addPixel = -20;
			createMessageDiv('divCatname' + i, words_en['Wahlen_Sport_Ausrustung'], addPixel);			
			skyD.getElementById('catName' + i).focus();
			weiter = false;
		}
		if(skyTrim(groesse) != ''){
			if(isNaN(groesse)){				
				createMessageDiv('pgroesse' + i, words_en['eingeben_Zahlen'], 0);							
				weiter = false;
			}				
		}
		if(skyTrim(schuhgroesse) != ''){
			if(isNaN(schuhgroesse)){				
				createMessageDiv('pschuhgroesse' + i, words_en['eingeben_Zahlen'], 0);							
				weiter = false;
			}				
		}
		if(skyTrim(gewicht) != ''){
			if(isNaN(gewicht)){				
				createMessageDiv('pgewicht' + i, words_en['eingeben_Zahlen'], 0);							
				weiter = false;
			}				
		}		
	}

	if(weiter == true)
		document.forms.skiverleih.submit();
	else
		return false;
}

function definePosition(obj) {
     var x = 0, y = 0;
     while(obj) {
        x += obj.offsetLeft;
        y += obj.offsetTop;
        obj = obj.offsetParent;
    }	
    return {x:x, y:y};
} 